Skip to content

Commit 2fbda5b

Browse files
committed
Provide a redundant Event::PaymentClaimed on restart if needed
If we crashed during a payment claim and then detected a partial claim on restart, we should ensure the user is aware that the payment has been claimed. We do so here by using the new partial-claim detection logic to create a `PaymentClaimed` event.
1 parent 49a3883 commit 2fbda5b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6798,7 +6798,20 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
67986798
for (payment_hash, payment_preimage) in monitor.get_stored_preimages() {
67996799
if let Some(claimable_htlcs) = claimable_htlcs.remove(&payment_hash) {
68006800
log_info!(args.logger, "Re-claiming HTLCs with payment hash {} as we've released the preimage to a ChannelMonitor!", log_bytes!(payment_hash.0));
6801+
let mut purpose = None;
6802+
let mut claimable_amt_msat = 0;
68016803
for claimable_htlc in claimable_htlcs {
6804+
if purpose.is_none() {
6805+
purpose = Some(match claimable_htlc.onion_payload {
6806+
OnionPayload::Spontaneous(preimage) => events::PaymentPurpose::SpontaneousPayment(preimage),
6807+
OnionPayload::Invoice(hop_data) => events::PaymentPurpose::InvoicePayment {
6808+
payment_secret: hop_data.payment_secret,
6809+
payment_preimage: Some(payment_preimage),
6810+
}
6811+
});
6812+
}
6813+
claimable_amt_msat += claimable_htlc.value;
6814+
68026815
// Add a holding-cell claim of the payment to the Channel, which should be
68036816
// applied ~immediately on peer reconnection. Because it won't generate a
68046817
// new commitment transaction we can just provide the payment preimage to
@@ -6822,6 +6835,11 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
68226835
previous_hop_monitor.provide_payment_preimage(&payment_hash, &payment_preimage, &args.tx_broadcaster, &args.fee_estimator, &args.logger);
68236836
}
68246837
}
6838+
pending_events_read.push(events::Event::PaymentClaimed {
6839+
payment_hash,
6840+
purpose: purpose.unwrap(),
6841+
amt: claimable_amt_msat,
6842+
});
68256843
}
68266844
}
68276845
}

lightning/src/ln/functional_tests.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9614,6 +9614,9 @@ fn do_test_partial_claim_before_restart(persist_both_monitors: bool) {
96149614
// To get to the correct state, on startup we should propagate the preimage to the
96159615
// still-off-chain channel, claiming the HTLC as soon as the peer connects, with the monitor
96169616
// receiving the preimage without a state update.
9617+
//
9618+
// Further, we should generate a `PaymentClaimed` event to inform the user that the payment was
9619+
// definitely claimed.
96179620
let chanmon_cfgs = create_chanmon_cfgs(4);
96189621
let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
96199622
let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
@@ -9731,13 +9734,19 @@ fn do_test_partial_claim_before_restart(persist_both_monitors: bool) {
97319734
// commitment transaction. We should also still have the original PaymentReceived event we
97329735
// never finished processing.
97339736
let events = nodes[3].node.get_and_clear_pending_events();
9734-
assert_eq!(events.len(), if persist_both_monitors { 3 } else { 2 });
9737+
assert_eq!(events.len(), if persist_both_monitors { 4 } else { 3 });
97359738
if let Event::PaymentReceived { amt: 15_000_000, .. } = events[0] { } else { panic!(); }
97369739
if let Event::ChannelClosed { reason: ClosureReason::OutdatedChannelManager, .. } = events[1] { } else { panic!(); }
97379740
if persist_both_monitors {
97389741
if let Event::ChannelClosed { reason: ClosureReason::OutdatedChannelManager, .. } = events[2] { } else { panic!(); }
97399742
}
97409743

9744+
// On restart, we should also get a duplicate PaymentClaimed event as we persisted the
9745+
// ChannelManager prior to handling the original one.
9746+
if let Event::PaymentClaimed { payment_hash: our_payment_hash, amt: 15_000_000, .. } = events[if persist_both_monitors { 3 } else { 2 }] {
9747+
assert_eq!(payment_hash, our_payment_hash);
9748+
} else { panic!(); }
9749+
97419750
assert_eq!(nodes[3].node.list_channels().len(), if persist_both_monitors { 0 } else { 1 });
97429751
if !persist_both_monitors {
97439752
// If one of the two channels is still live, reveal the payment preimage over it.

0 commit comments

Comments
 (0)