Skip to content

Commit fec4748

Browse files
authored
Merge pull request lightningdevkit#6 from lightsparkdev/waterson/settle-htlcs
Provide the HTLCs that settled a payment.
2 parents 2e429ce + e658e22 commit fec4748

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

lightning/src/events/mod.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ impl_writeable_tlv_based_enum!(PaymentPurpose,
7979
(2, SpontaneousPayment)
8080
);
8181

82+
/// Information about an HTLC that is part of a payment that can be claimed.
83+
#[derive(Clone, Debug, PartialEq, Eq)]
84+
pub struct ClaimedHTLC {
85+
/// The SCID over which the HTLC was received.
86+
pub short_channel_id: u64,
87+
/// The HTLC's ID.
88+
pub htlc_id: u64,
89+
/// The block height at which this HTLC expires.
90+
pub cltv_expiry: u32,
91+
/// The amount (in msats) of this part of an MPP.
92+
pub value: u64,
93+
/// The sender-indented sum total of all MPP parts.
94+
pub total_msat: u64,
95+
}
96+
impl_writeable_tlv_based!(ClaimedHTLC, {
97+
(0, short_channel_id, required),
98+
(1, htlc_id, required),
99+
(2, cltv_expiry, required),
100+
(3, value, required),
101+
(4, total_msat, required),
102+
});
103+
82104
/// When the payment path failure took place and extra details about it. [`PathFailure::OnPath`] may
83105
/// contain a [`NetworkUpdate`] that needs to be applied to the [`NetworkGraph`].
84106
///
@@ -454,6 +476,8 @@ pub enum Event {
454476
/// The purpose of the claimed payment, i.e. whether the payment was for an invoice or a
455477
/// spontaneous payment.
456478
purpose: PaymentPurpose,
479+
/// The HTLCs that comprise the claimed payment.
480+
htlcs: Vec<ClaimedHTLC>,
457481
},
458482
/// Indicates an outbound payment we made succeeded (i.e. it made it all the way to its target
459483
/// and we got back the payment preimage for it).
@@ -999,13 +1023,14 @@ impl Writeable for Event {
9991023
// We never write the OpenChannelRequest events as, upon disconnection, peers
10001024
// drop any channels which have not yet exchanged funding_signed.
10011025
},
1002-
&Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id } => {
1026+
&Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id, ref htlcs } => {
10031027
19u8.write(writer)?;
10041028
write_tlv_fields!(writer, {
10051029
(0, payment_hash, required),
10061030
(1, receiver_node_id, option),
10071031
(2, purpose, required),
10081032
(4, amount_msat, required),
1033+
(5, htlcs.clone(), optional_vec),
10091034
});
10101035
},
10111036
&Event::ProbeSuccessful { ref payment_id, ref payment_hash, ref path } => {
@@ -1325,17 +1350,20 @@ impl MaybeReadable for Event {
13251350
let mut purpose = UpgradableRequired(None);
13261351
let mut amount_msat = 0;
13271352
let mut receiver_node_id = None;
1353+
let mut htlcs: Option<Vec<ClaimedHTLC>> = Some(vec![]);
13281354
read_tlv_fields!(reader, {
13291355
(0, payment_hash, required),
13301356
(1, receiver_node_id, option),
13311357
(2, purpose, upgradable_required),
13321358
(4, amount_msat, required),
1359+
(5, htlcs, optional_vec),
13331360
});
13341361
Ok(Some(Event::PaymentClaimed {
13351362
receiver_node_id,
13361363
payment_hash,
13371364
purpose: _init_tlv_based_struct_field!(purpose, upgradable_required),
13381365
amount_msat,
1366+
htlcs: htlcs.unwrap_or(vec![]),
13391367
}))
13401368
};
13411369
f()

lightning/src/ln/channelmanager.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ struct ClaimableHTLC {
217217
counterparty_skimmed_fee_msat: Option<u64>,
218218
}
219219

220+
impl From<&ClaimableHTLC> for events::ClaimedHTLC {
221+
fn from(val: &ClaimableHTLC) -> Self {
222+
events::ClaimedHTLC {
223+
short_channel_id: val.prev_hop.short_channel_id,
224+
htlc_id: val.prev_hop.htlc_id,
225+
cltv_expiry: val.cltv_expiry,
226+
value: val.value,
227+
total_msat: val.total_msat,
228+
}
229+
}
230+
}
231+
220232
/// A payment identifier used to uniquely identify a payment to LDK.
221233
///
222234
/// This is not exported to bindings users as we just use [u8; 32] directly
@@ -471,11 +483,13 @@ struct ClaimingPayment {
471483
amount_msat: u64,
472484
payment_purpose: events::PaymentPurpose,
473485
receiver_node_id: PublicKey,
486+
htlcs: Vec<events::ClaimedHTLC>,
474487
}
475488
impl_writeable_tlv_based!(ClaimingPayment, {
476489
(0, amount_msat, required),
477490
(2, payment_purpose, required),
478491
(4, receiver_node_id, required),
492+
(5, htlcs, optional_vec),
479493
});
480494

481495
struct ClaimablePayment {
@@ -4753,9 +4767,10 @@ where
47534767
}
47544768
}
47554769

4770+
let htlcs = payment.htlcs.iter().map(events::ClaimedHTLC::from).collect();
47564771
let dup_purpose = claimable_payments.pending_claiming_payments.insert(payment_hash,
47574772
ClaimingPayment { amount_msat: payment.htlcs.iter().map(|source| source.value).sum(),
4758-
payment_purpose: payment.purpose, receiver_node_id,
4773+
payment_purpose: payment.purpose, receiver_node_id, htlcs,
47594774
});
47604775
if dup_purpose.is_some() {
47614776
debug_assert!(false, "Shouldn't get a duplicate pending claim event ever");
@@ -4996,9 +5011,9 @@ where
49965011
match action {
49975012
MonitorUpdateCompletionAction::PaymentClaimed { payment_hash } => {
49985013
let payment = self.claimable_payments.lock().unwrap().pending_claiming_payments.remove(&payment_hash);
4999-
if let Some(ClaimingPayment { amount_msat, payment_purpose: purpose, receiver_node_id }) = payment {
5014+
if let Some(ClaimingPayment { amount_msat, payment_purpose: purpose, receiver_node_id, htlcs }) = payment {
50005015
self.pending_events.lock().unwrap().push_back((events::Event::PaymentClaimed {
5001-
payment_hash, purpose, amount_msat, receiver_node_id: Some(receiver_node_id),
5016+
payment_hash, purpose, amount_msat, receiver_node_id: Some(receiver_node_id), htlcs,
50025017
}, None));
50035018
}
50045019
},
@@ -8992,7 +9007,7 @@ where
89929007
.expect("Failed to get node_id for phantom node recipient");
89939008
receiver_node_id = Some(phantom_pubkey)
89949009
}
8995-
for claimable_htlc in payment.htlcs {
9010+
for claimable_htlc in &payment.htlcs {
89969011
claimable_amt_msat += claimable_htlc.value;
89979012

89989013
// Add a holding-cell claim of the payment to the Channel, which should be
@@ -9028,6 +9043,7 @@ where
90289043
payment_hash,
90299044
purpose: payment.purpose,
90309045
amount_msat: claimable_amt_msat,
9046+
htlcs: payment.htlcs.iter().map(events::ClaimedHTLC::from).collect(),
90319047
}, None));
90329048
}
90339049
}

0 commit comments

Comments
 (0)