@@ -45,7 +45,7 @@ use chain::transaction::{OutPoint, TransactionData};
4545use ln:: { PaymentHash , PaymentPreimage , PaymentSecret } ;
4646use ln:: channel:: { Channel , ChannelError , ChannelUpdateStatus , UpdateFulfillCommitFetch } ;
4747use ln:: features:: { InitFeatures , NodeFeatures } ;
48- use routing:: router:: { Payee , Route , RouteHop , RouteParameters } ;
48+ use routing:: router:: { Payee , Route , RouteHop , RoutePath , RouteParameters } ;
4949use ln:: msgs;
5050use ln:: msgs:: NetAddress ;
5151use ln:: onion_utils;
@@ -436,6 +436,8 @@ pub(crate) enum PendingOutboundPayment {
436436 payment_hash : PaymentHash ,
437437 payment_secret : Option < PaymentSecret > ,
438438 pending_amt_msat : u64 ,
439+ /// Used to track the fee paid. Only present if the payment was serialized on 0.0.103+.
440+ pending_fee_msat : Option < u64 > ,
439441 /// The total payment amount across all paths, used to verify that a retry is not overpaying.
440442 total_msat : u64 ,
441443 /// Our best known block height at the time this payment was initiated.
@@ -462,6 +464,12 @@ impl PendingOutboundPayment {
462464 _ => false ,
463465 }
464466 }
467+ fn get_pending_fee_msat ( & self ) -> Option < u64 > {
468+ match self {
469+ PendingOutboundPayment :: Retryable { pending_fee_msat, .. } => pending_fee_msat. clone ( ) ,
470+ _ => None ,
471+ }
472+ }
465473
466474 fn mark_fulfilled ( & mut self ) {
467475 let mut session_privs = HashSet :: new ( ) ;
@@ -484,10 +492,13 @@ impl PendingOutboundPayment {
484492 }
485493 } ;
486494 if remove_res {
487- if let PendingOutboundPayment :: Retryable { ref mut pending_amt_msat, .. } = self {
495+ if let PendingOutboundPayment :: Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat , .. } = self {
488496 let path = path. expect ( "Fulfilling a payment should always come with a path" ) ;
489497 let path_last_hop = path. last ( ) . expect ( "Outbound payments must have had a valid path" ) ;
490498 * pending_amt_msat -= path_last_hop. fee_msat ;
499+ if let Some ( fee_msat) = pending_fee_msat. as_mut ( ) {
500+ * fee_msat -= path. get_path_fees ( ) ;
501+ }
491502 }
492503 }
493504 remove_res
@@ -502,9 +513,12 @@ impl PendingOutboundPayment {
502513 PendingOutboundPayment :: Fulfilled { .. } => false
503514 } ;
504515 if insert_res {
505- if let PendingOutboundPayment :: Retryable { ref mut pending_amt_msat, .. } = self {
516+ if let PendingOutboundPayment :: Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat , .. } = self {
506517 let path_last_hop = path. last ( ) . expect ( "Outbound payments must have had a valid path" ) ;
507518 * pending_amt_msat += path_last_hop. fee_msat ;
519+ if let Some ( fee_msat) = pending_fee_msat. as_mut ( ) {
520+ * fee_msat += path. get_path_fees ( ) ;
521+ }
508522 }
509523 }
510524 insert_res
@@ -2084,6 +2098,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
20842098 let payment = payment_entry. or_insert_with ( || PendingOutboundPayment :: Retryable {
20852099 session_privs : HashSet :: new ( ) ,
20862100 pending_amt_msat : 0 ,
2101+ pending_fee_msat : Some ( 0 ) ,
20872102 payment_hash : * payment_hash,
20882103 payment_secret : * payment_secret,
20892104 starting_block_height : self . best_block . read ( ) . unwrap ( ) . height ( ) ,
@@ -3458,8 +3473,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34583473 let mut session_priv_bytes = [ 0 ; 32 ] ;
34593474 session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
34603475 let mut outbounds = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
3461- let found_payment = if let hash_map:: Entry :: Occupied ( mut payment) = outbounds. entry ( payment_id) {
3476+ if let hash_map:: Entry :: Occupied ( mut payment) = outbounds. entry ( payment_id) {
34623477 let found_payment = !payment. get ( ) . is_fulfilled ( ) ;
3478+ let fee_paid_msat = payment. get ( ) . get_pending_fee_msat ( ) ;
34633479 payment. get_mut ( ) . mark_fulfilled ( ) ;
34643480 if from_onchain {
34653481 // We currently immediately remove HTLCs which were fulfilled on-chain.
@@ -3473,17 +3489,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34733489 payment. remove ( ) ;
34743490 }
34753491 }
3476- found_payment
3477- } else { false } ;
3478- if found_payment {
3479- let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage . 0 ) . into_inner ( ) ) ;
3480- self . pending_events . lock ( ) . unwrap ( ) . push (
3481- events :: Event :: PaymentSent {
3482- payment_id : Some ( payment_id ) ,
3483- payment_preimage ,
3484- payment_hash : payment_hash
3485- }
3486- ) ;
3492+ if found_payment {
3493+ let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage . 0 ) . into_inner ( ) ) ;
3494+ self . pending_events . lock ( ) . unwrap ( ) . push (
3495+ events :: Event :: PaymentSent {
3496+ payment_id : Some ( payment_id ) ,
3497+ payment_preimage ,
3498+ payment_hash : payment_hash ,
3499+ fee_paid_msat ,
3500+ }
3501+ ) ;
3502+ }
34873503 } else {
34883504 log_trace ! ( self . logger, "Received duplicative fulfill for HTLC with payment_preimage {}" , log_bytes!( payment_preimage. 0 ) ) ;
34893505 }
@@ -5496,6 +5512,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
54965512 } ,
54975513 ( 2 , Retryable ) => {
54985514 ( 0 , session_privs, required) ,
5515+ ( 1 , pending_fee_msat, option) ,
54995516 ( 2 , payment_hash, required) ,
55005517 ( 4 , payment_secret, option) ,
55015518 ( 6 , total_msat, required) ,
@@ -5957,11 +5974,13 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
59575974 if newly_added { "Added" } else { "Had" } , path_amt, log_bytes!( session_priv_bytes) , log_bytes!( htlc. payment_hash. 0 ) ) ;
59585975 } ,
59595976 hash_map:: Entry :: Vacant ( entry) => {
5977+ let path_fee = path. get_path_fees ( ) ;
59605978 entry. insert ( PendingOutboundPayment :: Retryable {
59615979 session_privs : [ session_priv_bytes] . iter ( ) . map ( |a| * a) . collect ( ) ,
59625980 payment_hash : htlc. payment_hash ,
59635981 payment_secret,
59645982 pending_amt_msat : path_amt,
5983+ pending_fee_msat : Some ( path_fee) ,
59655984 total_msat : path_amt,
59665985 starting_block_height : best_block_height,
59675986 } ) ;
@@ -6260,7 +6279,7 @@ mod tests {
62606279 // further events will be generated for subsequence path successes.
62616280 let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
62626281 match events[ 0 ] {
6263- Event :: PaymentSent { payment_id : ref id, payment_preimage : ref preimage, payment_hash : ref hash } => {
6282+ Event :: PaymentSent { payment_id : ref id, payment_preimage : ref preimage, payment_hash : ref hash, .. } => {
62646283 assert_eq ! ( Some ( payment_id) , * id) ;
62656284 assert_eq ! ( payment_preimage, * preimage) ;
62666285 assert_eq ! ( our_payment_hash, * hash) ;
0 commit comments