Skip to content

Commit 0a43626

Browse files
committed
f Fail duplicate inbound payments
1 parent b55b9b7 commit 0a43626

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/event.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,22 @@ where
295295
via_channel_id: _,
296296
via_user_channel_id: _,
297297
} => {
298+
if let Some(info) = self.payment_store.get(&payment_hash) {
299+
if info.status == PaymentStatus::Succeeded {
300+
log_info!(
301+
self.logger,
302+
"Refused duplicate inbound payment from payment hash {} of {}msat",
303+
hex_utils::to_string(&payment_hash.0),
304+
amount_msat,
305+
);
306+
self.channel_manager.fail_htlc_backwards(&payment_hash);
307+
self.payment_store
308+
.set_status(&payment_hash, PaymentStatus::Failed)
309+
.expect("Failed to access payment store");
310+
return;
311+
}
312+
}
313+
298314
log_info!(
299315
self.logger,
300316
"Received payment from payment hash {} of {}msat",

src/tests/functional_tests.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,23 @@ fn channel_full_cycle() {
219219
};
220220

221221
println!("\nB receive_payment");
222-
let invoice = node_b.receive_payment(1000000, &"asdf", 9217).unwrap();
222+
let invoice_amount = 1000000;
223+
let invoice = node_b.receive_payment(invoice_amount, &"asdf", 9217).unwrap();
223224

224225
println!("\nA send_payment");
225-
node_a.send_payment(invoice).unwrap();
226+
let payment_hash = node_a.send_payment(invoice.clone()).unwrap();
226227

227228
expect_event!(node_a, PaymentSuccessful);
228229
expect_event!(node_b, PaymentReceived);
230+
assert_eq!(node_a.payment_info(&payment_hash).unwrap().status, PaymentStatus::Succeeded);
231+
assert_eq!(node_a.payment_info(&payment_hash).unwrap().direction, PaymentDirection::Outbound);
232+
assert_eq!(node_a.payment_info(&payment_hash).unwrap().amount_msat, Some(invoice_amount));
233+
assert_eq!(node_b.payment_info(&payment_hash).unwrap().status, PaymentStatus::Succeeded);
234+
assert_eq!(node_b.payment_info(&payment_hash).unwrap().direction, PaymentDirection::Inbound);
235+
assert_eq!(node_b.payment_info(&payment_hash).unwrap().amount_msat, Some(invoice_amount));
236+
237+
// Assert we fail duplicate outbound payments.
238+
assert_eq!(Err(Error::NonUniquePaymentHash), node_a.send_payment(invoice));
229239

230240
// Test under-/overpayment
231241
let invoice_amount = 1000000;

0 commit comments

Comments
 (0)