Skip to content

Commit 27b99ac

Browse files
Add PaymentClaimable::counterparty_skimmed_fee_msat
See its docs
1 parent 9485304 commit 27b99ac

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

lightning/src/events/mod.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,24 @@ pub enum Event {
387387
///
388388
/// Payments received on LDK versions prior to 0.0.115 will have this field unset.
389389
onion_fields: Option<RecipientOnionFields>,
390-
/// The value, in thousandths of a satoshi, that this payment is for.
390+
/// The value, in thousandths of a satoshi, that this payment is claimable for.
391+
///
392+
/// May be less than the invoice amount if [`ChannelConfig::accept_underpaying_htlcs`] is set
393+
/// and the previous hop took an extra fee.
394+
///
395+
/// # Note
396+
/// If [`ChannelConfig::accept_underpaying_htlcs`] is set and you claim without verifying this
397+
/// field, you may lose money!
398+
///
399+
/// [`ChannelConfig::accept_underpaying_htlcs`]: crate::util::config::ChannelConfig::accept_underpaying_htlcs
391400
amount_msat: u64,
401+
/// The value, in thousands of a satoshi, that was skimmed off of this payment as an extra fee
402+
/// taken by our channel counterparty.
403+
///
404+
/// Will always be 0 unless [`ChannelConfig::accept_underpaying_htlcs`] is set.
405+
///
406+
/// [`ChannelConfig::accept_underpaying_htlcs`]: crate::util::config::ChannelConfig::accept_underpaying_htlcs
407+
counterparty_skimmed_fee_msat: u64,
392408
/// Information for claiming this received payment, based on whether the purpose of the
393409
/// payment is to pay an invoice or to send a spontaneous payment.
394410
purpose: PaymentPurpose,
@@ -833,8 +849,8 @@ impl Writeable for Event {
833849
// We never write out FundingGenerationReady events as, upon disconnection, peers
834850
// drop any channels which have not yet exchanged funding_signed.
835851
},
836-
&Event::PaymentClaimable { ref payment_hash, ref amount_msat, ref purpose,
837-
ref receiver_node_id, ref via_channel_id, ref via_user_channel_id,
852+
&Event::PaymentClaimable { ref payment_hash, ref amount_msat, counterparty_skimmed_fee_msat,
853+
ref purpose, ref receiver_node_id, ref via_channel_id, ref via_user_channel_id,
838854
ref claim_deadline, ref onion_fields
839855
} => {
840856
1u8.write(writer)?;
@@ -849,6 +865,8 @@ impl Writeable for Event {
849865
payment_preimage = Some(*preimage);
850866
}
851867
}
868+
let skimmed_fee_opt = if counterparty_skimmed_fee_msat == 0 { None }
869+
else { Some(counterparty_skimmed_fee_msat) };
852870
write_tlv_fields!(writer, {
853871
(0, payment_hash, required),
854872
(1, receiver_node_id, option),
@@ -860,6 +878,7 @@ impl Writeable for Event {
860878
(7, claim_deadline, option),
861879
(8, payment_preimage, option),
862880
(9, onion_fields, option),
881+
(10, skimmed_fee_opt, option),
863882
});
864883
},
865884
&Event::PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref fee_paid_msat } => {
@@ -1059,6 +1078,7 @@ impl MaybeReadable for Event {
10591078
let mut payment_preimage = None;
10601079
let mut payment_secret = None;
10611080
let mut amount_msat = 0;
1081+
let mut counterparty_skimmed_fee_msat_opt = None;
10621082
let mut receiver_node_id = None;
10631083
let mut _user_payment_id = None::<u64>; // For compatibility with 0.0.103 and earlier
10641084
let mut via_channel_id = None;
@@ -1076,6 +1096,7 @@ impl MaybeReadable for Event {
10761096
(7, claim_deadline, option),
10771097
(8, payment_preimage, option),
10781098
(9, onion_fields, option),
1099+
(10, counterparty_skimmed_fee_msat_opt, option),
10791100
});
10801101
let purpose = match payment_secret {
10811102
Some(secret) => PaymentPurpose::InvoicePayment {
@@ -1089,6 +1110,7 @@ impl MaybeReadable for Event {
10891110
receiver_node_id,
10901111
payment_hash,
10911112
amount_msat,
1113+
counterparty_skimmed_fee_msat: counterparty_skimmed_fee_msat_opt.unwrap_or(0),
10921114
purpose,
10931115
via_channel_id,
10941116
via_user_channel_id,

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3923,11 +3923,14 @@ where
39233923
htlcs.push(claimable_htlc);
39243924
let amount_msat = htlcs.iter().map(|htlc| htlc.value).sum();
39253925
htlcs.iter_mut().for_each(|htlc| htlc.total_value_received = Some(amount_msat));
3926+
let counterparty_skimmed_fee_msat = htlcs.iter()
3927+
.map(|htlc| htlc.counterparty_skimmed_fee_msat.unwrap_or(0)).sum();
39263928
new_events.push_back((events::Event::PaymentClaimable {
39273929
receiver_node_id: Some(receiver_node_id),
39283930
payment_hash,
39293931
purpose: $purpose,
39303932
amount_msat,
3933+
counterparty_skimmed_fee_msat,
39313934
via_channel_id: Some(prev_channel_id),
39323935
via_user_channel_id: Some(prev_user_channel_id),
39333936
claim_deadline: Some(earliest_expiry - HTLC_FAIL_BACK_BUFFER),

lightning/src/ln/functional_test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,7 @@ pub fn do_pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_p
21242124
match &events_2[0] {
21252125
Event::PaymentClaimable { ref payment_hash, ref purpose, amount_msat,
21262126
receiver_node_id, ref via_channel_id, ref via_user_channel_id,
2127-
claim_deadline, onion_fields,
2127+
claim_deadline, onion_fields, ..
21282128
} => {
21292129
assert_eq!(our_payment_hash, *payment_hash);
21302130
assert_eq!(node.node.get_our_node_id(), receiver_node_id.unwrap());

lightning/src/util/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,12 @@ pub struct ChannelConfig {
412412
/// # Note
413413
/// It's important for payee wallet software to verify that [`PaymentClaimable::amount_msat`] is
414414
/// as-expected if this feature is activated, otherwise they may lose money!
415+
/// [`PaymentClaimable::counterparty_skimmed_fee_msat`] provides the fee taken by the
416+
/// counterparty.
415417
///
416418
/// # Note
417419
/// Switching this config flag on may break compatibility with versions of LDK prior to 0.0.116.
420+
/// Unsetting this flag between restarts may lead to payment receive failures.
418421
///
419422
/// Default value: false.
420423
///
@@ -423,6 +426,7 @@ pub struct ChannelConfig {
423426
/// [`HTLCIntercepted`]: crate::events::Event::HTLCIntercepted
424427
/// [`HTLCIntercepted::expected_outbound_amount_msat`]: crate::events::Event::HTLCIntercepted::expected_outbound_amount_msat
425428
/// [`PaymentClaimable::amount_msat`]: crate::events::Event::PaymentClaimable::amount_msat
429+
/// [`PaymentClaimable::counterparty_skimmed_fee_msat`]: crate::events::Event::PaymentClaimable::counterparty_skimmed_fee_msat
426430
// TODO: link to bLIP when it's merged
427431
pub accept_underpaying_htlcs: bool,
428432
}

pending_changelog/forward-underpaying-htlc.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
* Forwarding less than the expected amount in `ChannelManager::forward_intercepted_htlc` may break
44
compatibility with versions of LDK prior to 0.0.116
5+
* Setting `ChannelConfig::accept_underpaying_htlcs` may break compatibility with versions of LDK
6+
prior to 0.0.116, and unsetting the feature between restarts may lead to payment failures.

0 commit comments

Comments
 (0)