Skip to content

Only avoid duplicate payments if we didn't fail sending (and expose list_payments) #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ class LibraryTest {
assert(paymentReceivedEvent is Event.PaymentReceived)
node2.eventHandled()

assert(node1.listPayments().size == 1)
assert(node2.listPayments().size == 1)

node2.closeChannel(channelId, nodeId1)

val channelClosedEvent1 = node1.waitNextEvent()
Expand All @@ -196,7 +199,7 @@ class LibraryTest {
mine(1u)

// Sleep a bit to allow for the block to propagate to esplora
Thread.sleep(3_000)
Thread.sleep(5_000)

node1.syncWallets()
node2.syncWallets()
Expand Down
6 changes: 4 additions & 2 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ interface LDKNode {
PaymentDetails? payment([ByRef]PaymentHash payment_hash);
[Throws=NodeError]
boolean remove_payment([ByRef]PaymentHash payment_hash);
sequence<PaymentDetails> list_payments();
sequence<PeerDetails> list_peers();
sequence<ChannelDetails> list_channels();
[Throws=NodeError]
Expand All @@ -81,7 +82,7 @@ enum NodeError {
"OnchainTxCreationFailed",
"ConnectionFailed",
"InvoiceCreationFailed",
"PaymentFailed",
"PaymentSendingFailed",
"ChannelCreationFailed",
"ChannelClosingFailed",
"PersistenceFailed",
Expand All @@ -101,7 +102,7 @@ enum NodeError {
"InvalidInvoice",
"InvalidChannelId",
"InvalidNetwork",
"NonUniquePaymentHash",
"DuplicatePayment",
"InsufficientFunds",
};

Expand All @@ -122,6 +123,7 @@ enum PaymentDirection {

enum PaymentStatus {
"Pending",
"SendingFailed",
"Succeeded",
"Failed",
};
Expand Down
14 changes: 8 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub enum Error {
ConnectionFailed,
/// Invoice creation failed.
InvoiceCreationFailed,
/// An attempted payment has failed.
PaymentFailed,
/// Sending a payment has failed.
PaymentSendingFailed,
/// A channel could not be opened.
ChannelCreationFailed,
/// A channel could not be closed.
Expand Down Expand Up @@ -53,8 +53,8 @@ pub enum Error {
InvalidChannelId,
/// The given network is invalid.
InvalidNetwork,
/// Payment of the given invoice has already been intiated.
NonUniquePaymentHash,
/// A payment with the given hash has already been intiated.
DuplicatePayment,
/// There are insufficient funds to complete the given operation.
InsufficientFunds,
}
Expand All @@ -69,7 +69,7 @@ impl fmt::Display for Error {
}
Self::ConnectionFailed => write!(f, "Network connection closed."),
Self::InvoiceCreationFailed => write!(f, "Failed to create invoice."),
Self::PaymentFailed => write!(f, "Failed to send the given payment."),
Self::PaymentSendingFailed => write!(f, "Failed to send the given payment."),
Self::ChannelCreationFailed => write!(f, "Failed to create channel."),
Self::ChannelClosingFailed => write!(f, "Failed to close channel."),
Self::PersistenceFailed => write!(f, "Failed to persist data."),
Expand All @@ -89,7 +89,9 @@ impl fmt::Display for Error {
Self::InvalidInvoice => write!(f, "The given invoice is invalid."),
Self::InvalidChannelId => write!(f, "The given channel ID is invalid."),
Self::InvalidNetwork => write!(f, "The given network is invalid."),
Self::NonUniquePaymentHash => write!(f, "An invoice must not get payed twice."),
Self::DuplicatePayment => {
write!(f, "A payment with the given hash has already been initiated.")
}
Self::InsufficientFunds => {
write!(f, "There are insufficient funds to complete the given operation.")
}
Expand Down
116 changes: 76 additions & 40 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,9 +1402,11 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {

let payment_hash = PaymentHash((*invoice.payment_hash()).into_inner());

if self.payment_store.contains(&payment_hash) {
log_error!(self.logger, "Payment error: an invoice must not get paid twice.");
return Err(Error::NonUniquePaymentHash);
if let Some(payment) = self.payment_store.get(&payment_hash) {
if payment.status != PaymentStatus::SendingFailed {
log_error!(self.logger, "Payment error: an invoice must not be paid twice.");
return Err(Error::DuplicatePayment);
}
}

let payment_secret = Some(*invoice.payment_secret());
Expand Down Expand Up @@ -1437,18 +1439,24 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
}
Err(payment::PaymentError::Sending(e)) => {
log_error!(self.logger, "Failed to send payment: {:?}", e);

let payment = PaymentDetails {
preimage: None,
hash: payment_hash,
secret: payment_secret,
amount_msat: invoice.amount_milli_satoshis(),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Failed,
};
self.payment_store.insert(payment)?;

Err(Error::PaymentFailed)
match e {
channelmanager::RetryableSendFailure::DuplicatePayment => {
Err(Error::DuplicatePayment)
}
_ => {
let payment = PaymentDetails {
preimage: None,
hash: payment_hash,
secret: payment_secret,
amount_msat: invoice.amount_milli_satoshis(),
direction: PaymentDirection::Outbound,
status: PaymentStatus::SendingFailed,
};

self.payment_store.insert(payment)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we have any tests that check the payment store? Might be worth adding a test case for duplicate payments.

Copy link
Collaborator Author

@tnull tnull Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, but checking that we fail to send duplicate payments is already covered in channel_full_cycle? Not entirely sure how we'd check the behavior on the payment store alone as the logic for handling duplicates lives (and has to live I think) in send_payment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we don't check that the status remains the same as before in case of a duplicate error, IIUC.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, fair enough, now asserting that the payment details are unchanged after we receive Error::DuplicatePayment.

Err(Error::PaymentSendingFailed)
}
}
}
}
}
Expand Down Expand Up @@ -1477,9 +1485,11 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
}

let payment_hash = PaymentHash((*invoice.payment_hash()).into_inner());
if self.payment_store.contains(&payment_hash) {
log_error!(self.logger, "Payment error: an invoice must not get paid twice.");
return Err(Error::NonUniquePaymentHash);
if let Some(payment) = self.payment_store.get(&payment_hash) {
if payment.status != PaymentStatus::SendingFailed {
log_error!(self.logger, "Payment error: an invoice must not be paid twice.");
return Err(Error::DuplicatePayment);
}
}

let payment_id = PaymentId(invoice.payment_hash().into_inner());
Expand Down Expand Up @@ -1532,17 +1542,24 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
Err(payment::PaymentError::Sending(e)) => {
log_error!(self.logger, "Failed to send payment: {:?}", e);

let payment = PaymentDetails {
hash: payment_hash,
preimage: None,
secret: payment_secret,
amount_msat: Some(amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Failed,
};
self.payment_store.insert(payment)?;

Err(Error::PaymentFailed)
match e {
channelmanager::RetryableSendFailure::DuplicatePayment => {
Err(Error::DuplicatePayment)
}
_ => {
let payment = PaymentDetails {
hash: payment_hash,
preimage: None,
secret: payment_secret,
amount_msat: Some(amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::SendingFailed,
};
self.payment_store.insert(payment)?;

Err(Error::PaymentSendingFailed)
}
}
}
}
}
Expand All @@ -1559,6 +1576,13 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
let payment_preimage = PaymentPreimage(self.keys_manager.get_secure_random_bytes());
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());

if let Some(payment) = self.payment_store.get(&payment_hash) {
if payment.status != PaymentStatus::SendingFailed {
log_error!(self.logger, "Payment error: must not send duplicate payments.");
return Err(Error::DuplicatePayment);
}
}

let route_params = RouteParameters {
payment_params: PaymentParameters::from_node_id(
node_id,
Expand Down Expand Up @@ -1593,17 +1617,24 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
Err(e) => {
log_error!(self.logger, "Failed to send payment: {:?}", e);

let payment = PaymentDetails {
hash: payment_hash,
preimage: Some(payment_preimage),
secret: None,
status: PaymentStatus::Failed,
direction: PaymentDirection::Outbound,
amount_msat: Some(amount_msat),
};
self.payment_store.insert(payment)?;

Err(Error::PaymentFailed)
match e {
channelmanager::RetryableSendFailure::DuplicatePayment => {
Err(Error::DuplicatePayment)
}
_ => {
let payment = PaymentDetails {
hash: payment_hash,
preimage: Some(payment_preimage),
secret: None,
status: PaymentStatus::SendingFailed,
direction: PaymentDirection::Outbound,
amount_msat: Some(amount_msat),
};

self.payment_store.insert(payment)?;
Err(Error::PaymentSendingFailed)
}
}
}
}
}
Expand Down Expand Up @@ -1697,6 +1728,11 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
self.payment_store.list_filter(f)
}

/// Retrieves all payments.
pub fn list_payments(&self) -> Vec<PaymentDetails> {
self.payment_store.list_filter(|_| true)
}

/// Retrieves a list of known peers.
pub fn list_peers(&self) -> Vec<PeerDetails> {
let active_connected_peers: Vec<PublicKey> =
Expand Down
17 changes: 8 additions & 9 deletions src/payment_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,19 @@ impl_writeable_tlv_based_enum!(PaymentDirection,
pub enum PaymentStatus {
/// The payment is still pending.
Pending,
/// The sending of the payment failed and is safe to be retried.
SendingFailed,
/// The payment suceeded.
Succeeded,
/// The payment failed.
/// The payment failed and is not retryable.
Failed,
}

impl_writeable_tlv_based_enum!(PaymentStatus,
(0, Pending) => {},
(1, Succeeded) => {},
(2, Failed) => {};
(2, SendingFailed) => {},
(4, Succeeded) => {},
(6, Failed) => {};
);

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -139,10 +142,6 @@ where
self.payments.lock().unwrap().get(hash).cloned()
}

pub(crate) fn contains(&self, hash: &PaymentHash) -> bool {
self.payments.lock().unwrap().contains_key(hash)
}

pub(crate) fn update(&self, update: &PaymentDetailsUpdate) -> Result<bool, Error> {
let mut updated = false;
let mut locked_payments = self.payments.lock().unwrap();
Expand Down Expand Up @@ -210,13 +209,13 @@ mod tests {
use std::sync::Arc;

#[test]
fn persistence_guard_persists_on_drop() {
fn payment_info_is_persisted() {
let store = Arc::new(TestStore::new());
let logger = Arc::new(TestLogger::new());
let payment_store = PaymentStore::new(Vec::new(), Arc::clone(&store), logger);

let hash = PaymentHash([42u8; 32]);
assert!(!payment_store.contains(&hash));
assert!(!payment_store.get(&hash).is_some());

let payment = PaymentDetails {
hash,
Expand Down
13 changes: 11 additions & 2 deletions src/test/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ fn channel_full_cycle() {

println!("\nA send_payment");
let payment_hash = node_a.send_payment(&invoice).unwrap();
assert_eq!(node_a.send_payment(&invoice), Err(Error::DuplicatePayment));

assert_eq!(node_a.list_payments().first().unwrap().hash, payment_hash);

let outbound_payments_a =
node_a.list_payments_with_filter(|p| p.direction == PaymentDirection::Outbound);
Expand All @@ -130,8 +133,14 @@ fn channel_full_cycle() {
assert_eq!(node_b.payment(&payment_hash).unwrap().direction, PaymentDirection::Inbound);
assert_eq!(node_b.payment(&payment_hash).unwrap().amount_msat, Some(invoice_amount_1_msat));

// Assert we fail duplicate outbound payments.
assert_eq!(Err(Error::NonUniquePaymentHash), node_a.send_payment(&invoice));
// Assert we fail duplicate outbound payments and check the status hasn't changed.
assert_eq!(Err(Error::DuplicatePayment), node_a.send_payment(&invoice));
assert_eq!(node_a.payment(&payment_hash).unwrap().status, PaymentStatus::Succeeded);
assert_eq!(node_a.payment(&payment_hash).unwrap().direction, PaymentDirection::Outbound);
assert_eq!(node_a.payment(&payment_hash).unwrap().amount_msat, Some(invoice_amount_1_msat));
assert_eq!(node_b.payment(&payment_hash).unwrap().status, PaymentStatus::Succeeded);
assert_eq!(node_b.payment(&payment_hash).unwrap().direction, PaymentDirection::Inbound);
assert_eq!(node_b.payment(&payment_hash).unwrap().amount_msat, Some(invoice_amount_1_msat));

// Test under-/overpayment
let invoice_amount_2_msat = 1000_000;
Expand Down