Skip to content

Commit a6b8a22

Browse files
authored
Merge pull request #3599 from tnull/2025-02-track-amount-in-payment-sent
Track `amount_msat` field in `Event::PaymentSent`
2 parents b05402a + e5d46c5 commit a6b8a22

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

lightning/src/events/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,9 +929,17 @@ pub enum Event {
929929
///
930930
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
931931
payment_hash: PaymentHash,
932+
/// The total amount that was paid, across all paths.
933+
///
934+
/// Note that, like [`Route::get_total_amount`], this does *not* include the paid fees.
935+
///
936+
/// This is only `None` for payments initiated on LDK versions prior to 0.2.
937+
///
938+
/// [`Route::get_total_amount`]: crate::routing::router::Route::get_total_amount
939+
amount_msat: Option<u64>,
932940
/// The total fee which was spent at intermediate hops in this payment, across all paths.
933941
///
934-
/// Note that, like [`Route::get_total_fees`] this does *not* include any potential
942+
/// Note that, like [`Route::get_total_fees`], this does *not* include any potential
935943
/// overpayment to the recipient node.
936944
///
937945
/// If the recipient or an intermediate node misbehaves and gives us free money, this may
@@ -1548,13 +1556,14 @@ impl Writeable for Event {
15481556
(13, payment_id, option),
15491557
});
15501558
},
1551-
&Event::PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref fee_paid_msat } => {
1559+
&Event::PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref amount_msat, ref fee_paid_msat } => {
15521560
2u8.write(writer)?;
15531561
write_tlv_fields!(writer, {
15541562
(0, payment_preimage, required),
15551563
(1, payment_hash, required),
15561564
(3, payment_id, option),
15571565
(5, fee_paid_msat, option),
1566+
(7, amount_msat, option),
15581567
});
15591568
},
15601569
&Event::PaymentPathFailed {
@@ -1887,12 +1896,14 @@ impl MaybeReadable for Event {
18871896
let mut payment_preimage = PaymentPreimage([0; 32]);
18881897
let mut payment_hash = None;
18891898
let mut payment_id = None;
1899+
let mut amount_msat = None;
18901900
let mut fee_paid_msat = None;
18911901
read_tlv_fields!(reader, {
18921902
(0, payment_preimage, required),
18931903
(1, payment_hash, option),
18941904
(3, payment_id, option),
18951905
(5, fee_paid_msat, option),
1906+
(7, amount_msat, option),
18961907
});
18971908
if payment_hash.is_none() {
18981909
payment_hash = Some(PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array()));
@@ -1901,6 +1912,7 @@ impl MaybeReadable for Event {
19011912
payment_id,
19021913
payment_preimage,
19031914
payment_hash: payment_hash.unwrap(),
1915+
amount_msat,
19041916
fee_paid_msat,
19051917
}))
19061918
};

lightning/src/ln/functional_test_utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2299,9 +2299,10 @@ pub fn expect_payment_sent<CM: AChannelManager, H: NodeHolder<CM=CM>>(node: &H,
22992299
check_added_monitors(node, 1);
23002300
}
23012301
let expected_payment_id = match events[0] {
2302-
Event::PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref fee_paid_msat } => {
2302+
Event::PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref amount_msat, ref fee_paid_msat } => {
23032303
assert_eq!(expected_payment_preimage, *payment_preimage);
23042304
assert_eq!(expected_payment_hash, *payment_hash);
2305+
assert!(amount_msat.is_some());
23052306
if let Some(expected_fee_msat) = expected_fee_msat_opt {
23062307
assert_eq!(*fee_paid_msat, expected_fee_msat);
23072308
} else {

lightning/src/ln/outbound_payment.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ pub(crate) enum PendingOutboundPayment {
122122
/// Filled in for any payment which moved to `Fulfilled` on LDK 0.0.104 or later.
123123
payment_hash: Option<PaymentHash>,
124124
timer_ticks_without_htlcs: u8,
125+
/// The total payment amount across all paths, used to be able to issue `PaymentSent`.
126+
total_msat: Option<u64>,
125127
},
126128
/// When we've decided to give up retrying a payment, we mark it as abandoned so we can eventually
127129
/// generate a `PaymentFailed` event when all HTLCs have irrevocably failed.
@@ -131,6 +133,9 @@ pub(crate) enum PendingOutboundPayment {
131133
/// Will be `None` if the payment was serialized before 0.0.115 or if downgrading to 0.0.124
132134
/// or later with a reason that was added after.
133135
reason: Option<PaymentFailureReason>,
136+
/// The total payment amount across all paths, used to be able to issue `PaymentSent` if
137+
/// an HTLC still happens to succeed after we marked the payment as abandoned.
138+
total_msat: Option<u64>,
134139
},
135140
}
136141

@@ -210,6 +215,15 @@ impl PendingOutboundPayment {
210215
}
211216
}
212217

218+
fn total_msat(&self) -> Option<u64> {
219+
match self {
220+
PendingOutboundPayment::Retryable { total_msat, .. } => Some(*total_msat),
221+
PendingOutboundPayment::Fulfilled { total_msat, .. } => *total_msat,
222+
PendingOutboundPayment::Abandoned { total_msat, .. } => *total_msat,
223+
_ => None,
224+
}
225+
}
226+
213227
fn payment_hash(&self) -> Option<PaymentHash> {
214228
match self {
215229
PendingOutboundPayment::Legacy { .. } => None,
@@ -236,7 +250,8 @@ impl PendingOutboundPayment {
236250
PendingOutboundPayment::StaticInvoiceReceived { .. } => { debug_assert!(false); return; },
237251
});
238252
let payment_hash = self.payment_hash();
239-
*self = PendingOutboundPayment::Fulfilled { session_privs, payment_hash, timer_ticks_without_htlcs: 0 };
253+
let total_msat = self.total_msat();
254+
*self = PendingOutboundPayment::Fulfilled { session_privs, payment_hash, timer_ticks_without_htlcs: 0, total_msat };
240255
}
241256

242257
fn mark_abandoned(&mut self, reason: PaymentFailureReason) {
@@ -248,6 +263,7 @@ impl PendingOutboundPayment {
248263
},
249264
_ => new_hash_set(),
250265
};
266+
let total_msat = self.total_msat();
251267
match self {
252268
Self::Retryable { payment_hash, .. } |
253269
Self::InvoiceReceived { payment_hash, .. } |
@@ -257,6 +273,7 @@ impl PendingOutboundPayment {
257273
session_privs,
258274
payment_hash: *payment_hash,
259275
reason: Some(reason),
276+
total_msat,
260277
};
261278
},
262279
_ => {}
@@ -1928,10 +1945,12 @@ impl OutboundPayments {
19281945
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).to_byte_array());
19291946
log_info!(logger, "Payment with id {} and hash {} sent!", payment_id, payment_hash);
19301947
let fee_paid_msat = payment.get().get_pending_fee_msat();
1948+
let amount_msat = payment.get().total_msat();
19311949
pending_events.push_back((events::Event::PaymentSent {
19321950
payment_id: Some(payment_id),
19331951
payment_preimage,
19341952
payment_hash,
1953+
amount_msat,
19351954
fee_paid_msat,
19361955
}, Some(ev_completion_action.clone())));
19371956
payment.get_mut().mark_fulfilled();
@@ -2362,6 +2381,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
23622381
(0, session_privs, required),
23632382
(1, payment_hash, option),
23642383
(3, timer_ticks_without_htlcs, (default_value, 0)),
2384+
(5, total_msat, option),
23652385
},
23662386
(2, Retryable) => {
23672387
(0, session_privs, required),
@@ -2386,6 +2406,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
23862406
(0, session_privs, required),
23872407
(1, reason, upgradable_option),
23882408
(2, payment_hash, required),
2409+
(3, total_msat, option),
23892410
},
23902411
(5, AwaitingInvoice) => {
23912412
(0, expiration, required),

0 commit comments

Comments
 (0)