Skip to content

Commit 114672b

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 9195542 commit 114672b

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
@@ -6800,8 +6800,21 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
68006800
for (_, monitor) in args.channel_monitors.iter() {
68016801
for (payment_hash, payment_preimage) in monitor.get_stored_preimages() {
68026802
if let Some(claimable_htlcs) = claimable_htlcs.remove(&payment_hash) {
6803+
68036804
log_info!(args.logger, "Re-claimaing HTLCs with payment hash {} due to partial-claim.", log_bytes!(payment_hash.0));
6805+
let mut purpose = None;
6806+
let mut claimable_amt_msat = 0;
68046807
for claimable_htlc in claimable_htlcs {
6808+
if purpose.is_none() {
6809+
purpose = Some(match claimable_htlc.onion_payload {
6810+
OnionPayload::Spontaneous(preimage) => events::PaymentPurpose::SpontaneousPayment(preimage),
6811+
OnionPayload::Invoice(hop_data) => events::PaymentPurpose::InvoicePayment {
6812+
payment_secret: hop_data.payment_secret,
6813+
payment_preimage: Some(payment_preimage),
6814+
}
6815+
});
6816+
}
6817+
claimable_amt_msat += claimable_htlc.value;
68056818
let previous_channel_id = claimable_htlc.prev_hop.outpoint.to_channel_id();
68066819
if let Some(channel) = by_id.get_mut(&previous_channel_id) {
68076820
channel.claim_htlc_while_disconnected_dropping_mon_update(claimable_htlc.prev_hop.htlc_id, payment_preimage, &args.logger);
@@ -6810,6 +6823,11 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
68106823
previous_hop_monitor.provide_payment_preimage(&payment_hash, &payment_preimage, &args.tx_broadcaster, &args.fee_estimator, &args.logger);
68116824
}
68126825
}
6826+
pending_events_read.push(events::Event::PaymentClaimed {
6827+
payment_hash,
6828+
purpose: purpose.unwrap(),
6829+
amt: claimable_amt_msat,
6830+
});
68136831
}
68146832
}
68156833
}

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)