@@ -436,6 +436,7 @@ pub(crate) enum PendingOutboundPayment {
436436 payment_hash : PaymentHash ,
437437 payment_secret : Option < PaymentSecret > ,
438438 pending_amt_msat : u64 ,
439+ pending_fee_msat : u64 ,
439440 /// The total payment amount across all paths, used to verify that a retry is not overpaying.
440441 total_msat : u64 ,
441442 /// Our best known block height at the time this payment was initiated.
@@ -462,6 +463,12 @@ impl PendingOutboundPayment {
462463 _ => false ,
463464 }
464465 }
466+ fn get_total_fee_msat ( & self ) -> Option < u64 > {
467+ match self {
468+ PendingOutboundPayment :: Retryable { pending_fee_msat, .. } => Some ( * pending_fee_msat) ,
469+ _ => None ,
470+ }
471+ }
465472
466473 fn mark_fulfilled ( & mut self ) {
467474 let mut session_privs = HashSet :: new ( ) ;
@@ -475,7 +482,7 @@ impl PendingOutboundPayment {
475482 }
476483
477484 /// panics if part_amt_msat is None and !self.is_fulfilled
478- fn remove ( & mut self , session_priv : & [ u8 ; 32 ] , part_amt_msat : Option < u64 > ) -> bool {
485+ fn remove ( & mut self , session_priv : & [ u8 ; 32 ] , part_amt_msat : Option < u64 > , part_fee_msat : Option < u64 > ) -> bool {
479486 let remove_res = match self {
480487 PendingOutboundPayment :: Legacy { session_privs } |
481488 PendingOutboundPayment :: Retryable { session_privs, .. } |
@@ -484,14 +491,15 @@ impl PendingOutboundPayment {
484491 }
485492 } ;
486493 if remove_res {
487- if let PendingOutboundPayment :: Retryable { ref mut pending_amt_msat, .. } = self {
494+ if let PendingOutboundPayment :: Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat , .. } = self {
488495 * pending_amt_msat -= part_amt_msat. expect ( "We must only not provide an amount if the payment was already fulfilled" ) ;
496+ * pending_fee_msat -= part_fee_msat. expect ( "We must only not provide a fee if the payment was already fulfilled" ) ;
489497 }
490498 }
491499 remove_res
492500 }
493501
494- fn insert ( & mut self , session_priv : [ u8 ; 32 ] , part_amt_msat : u64 ) -> bool {
502+ fn insert ( & mut self , session_priv : [ u8 ; 32 ] , part_amt_msat : u64 , part_fee_msat : u64 ) -> bool {
495503 let insert_res = match self {
496504 PendingOutboundPayment :: Legacy { session_privs } |
497505 PendingOutboundPayment :: Retryable { session_privs, .. } => {
@@ -500,8 +508,9 @@ impl PendingOutboundPayment {
500508 PendingOutboundPayment :: Fulfilled { .. } => false
501509 } ;
502510 if insert_res {
503- if let PendingOutboundPayment :: Retryable { ref mut pending_amt_msat, .. } = self {
511+ if let PendingOutboundPayment :: Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat , .. } = self {
504512 * pending_amt_msat += part_amt_msat;
513+ * pending_fee_msat += part_fee_msat;
505514 }
506515 }
507516 insert_res
@@ -2081,12 +2090,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
20812090 let payment = payment_entry. or_insert_with ( || PendingOutboundPayment :: Retryable {
20822091 session_privs : HashSet :: new ( ) ,
20832092 pending_amt_msat : 0 ,
2093+ pending_fee_msat : 0 ,
20842094 payment_hash : * payment_hash,
20852095 payment_secret : * payment_secret,
20862096 starting_block_height : self . best_block . read ( ) . unwrap ( ) . height ( ) ,
20872097 total_msat : total_value,
20882098 } ) ;
2089- assert ! ( payment. insert( session_priv_bytes, path. last( ) . unwrap( ) . fee_msat) ) ;
2099+ assert ! ( payment. insert( session_priv_bytes, path. last( ) . unwrap( ) . fee_msat,
2100+ path. split_last( ) . map( |( _, path_prefix) | path_prefix) . unwrap_or( & [ ] ) . iter( ) . map( |hop| hop. fee_msat) . sum( ) ) ) ;
20902101
20912102 send_res
20922103 } {
@@ -2206,6 +2217,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
22062217 return Err ( PaymentSendFailure :: PathParameterError ( path_errs) ) ;
22072218 }
22082219 if let Some ( amt_msat) = recv_value_msat {
2220+ eprintln ! ( "{} {}" , amt_msat, total_value) ;
2221+ eprintln ! ( "{:?}" , route) ;
22092222 debug_assert ! ( amt_msat >= total_value) ;
22102223 total_value = amt_msat;
22112224 }
@@ -3108,7 +3121,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31083121 let mut outbounds = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
31093122 if let hash_map:: Entry :: Occupied ( mut payment) = outbounds. entry ( payment_id) {
31103123 let path_last_hop = path. last ( ) . expect ( "Outbound payments must have had a valid path" ) ;
3111- if payment. get_mut ( ) . remove ( & session_priv_bytes, Some ( path_last_hop. fee_msat ) ) &&
3124+ if payment. get_mut ( ) . remove ( & session_priv_bytes, Some ( path_last_hop. fee_msat ) ,
3125+ Some ( path. split_last ( ) . map ( |( _, path_prefix) | path_prefix) . unwrap_or ( & [ ] ) . iter ( ) . map ( |hop| hop. fee_msat ) . sum ( ) ) ) &&
31123126 !payment. get ( ) . is_fulfilled ( )
31133127 {
31143128 let retry = if let Some ( payee_data) = payee {
@@ -3165,7 +3179,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31653179 let mut all_paths_failed = false ;
31663180 let path_last_hop = path. last ( ) . expect ( "Outbound payments must have had a valid path" ) ;
31673181 if let hash_map:: Entry :: Occupied ( mut payment) = outbounds. entry ( payment_id) {
3168- if !payment. get_mut ( ) . remove ( & session_priv_bytes, Some ( path_last_hop. fee_msat ) ) {
3182+ if !payment. get_mut ( ) . remove ( & session_priv_bytes, Some ( path_last_hop. fee_msat ) ,
3183+ Some ( path. split_last ( ) . map ( |( _, path_prefix) | path_prefix) . unwrap_or ( & [ ] ) . iter ( ) . map ( |hop| hop. fee_msat ) . sum ( ) ) )
3184+ {
31693185 log_trace ! ( self . logger, "Received duplicative fail for HTLC with payment_hash {}" , log_bytes!( payment_hash. 0 ) ) ;
31703186 return ;
31713187 }
@@ -3438,7 +3454,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34383454 let mut outbounds = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
34393455 if let hash_map:: Entry :: Occupied ( mut payment) = outbounds. entry ( payment_id) {
34403456 assert ! ( payment. get( ) . is_fulfilled( ) ) ;
3441- payment. get_mut ( ) . remove ( & session_priv_bytes, None ) ;
3457+ payment. get_mut ( ) . remove ( & session_priv_bytes, None , None ) ;
34423458 if payment. get ( ) . remaining_parts ( ) == 0 {
34433459 payment. remove ( ) ;
34443460 }
@@ -3454,8 +3470,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34543470 let mut session_priv_bytes = [ 0 ; 32 ] ;
34553471 session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
34563472 let mut outbounds = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
3457- let found_payment = if let hash_map:: Entry :: Occupied ( mut payment) = outbounds. entry ( payment_id) {
3473+ if let hash_map:: Entry :: Occupied ( mut payment) = outbounds. entry ( payment_id) {
34583474 let found_payment = !payment. get ( ) . is_fulfilled ( ) ;
3475+ let fee_paid_msat = payment. get ( ) . get_total_fee_msat ( ) ;
34593476 payment. get_mut ( ) . mark_fulfilled ( ) ;
34603477 if from_onchain {
34613478 // We currently immediately remove HTLCs which were fulfilled on-chain.
@@ -3464,21 +3481,22 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34643481 // restart.
34653482 // TODO: We should have a second monitor event that informs us of payments
34663483 // irrevocably fulfilled.
3467- payment. get_mut ( ) . remove ( & session_priv_bytes, Some ( path. last ( ) . unwrap ( ) . fee_msat ) ) ;
3484+ payment. get_mut ( ) . remove ( & session_priv_bytes, Some ( path. last ( ) . unwrap ( ) . fee_msat ) ,
3485+ Some ( path. split_last ( ) . map ( |( _, path_prefix) | path_prefix) . unwrap_or ( & [ ] ) . iter ( ) . map ( |hop| hop. fee_msat ) . sum ( ) ) ) ;
34683486 if payment. get ( ) . remaining_parts ( ) == 0 {
34693487 payment. remove ( ) ;
34703488 }
34713489 }
3472- found_payment
3473- } else { false } ;
3474- if found_payment {
3475- let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage . 0 ) . into_inner ( ) ) ;
3476- self . pending_events . lock ( ) . unwrap ( ) . push (
3477- events :: Event :: PaymentSent {
3478- payment_preimage ,
3479- payment_hash : payment_hash
3480- }
3481- ) ;
3490+ if found_payment {
3491+ let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage . 0 ) . into_inner ( ) ) ;
3492+ self . pending_events . lock ( ) . unwrap ( ) . push (
3493+ events :: Event :: PaymentSent {
3494+ payment_preimage ,
3495+ payment_hash : payment_hash ,
3496+ fee_paid_msat ,
3497+ }
3498+ ) ;
3499+ }
34823500 } else {
34833501 log_trace ! ( self . logger, "Received duplicative fulfill for HTLC with payment_preimage {}" , log_bytes!( payment_preimage. 0 ) ) ;
34843502 }
@@ -5496,6 +5514,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
54965514 ( 6 , total_msat, required) ,
54975515 ( 8 , pending_amt_msat, required) ,
54985516 ( 10 , starting_block_height, required) ,
5517+ ( 12 , pending_fee_msat, required) ,
54995518 } ,
55005519) ;
55015520
@@ -5943,11 +5962,12 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
59435962 return Err ( DecodeError :: InvalidValue ) ;
59445963 }
59455964 let path_amt = path. last ( ) . unwrap ( ) . fee_msat ;
5965+ let path_fee = path. split_last ( ) . map ( |( _, path_prefix) | path_prefix) . unwrap_or ( & [ ] ) . iter ( ) . map ( |hop| hop. fee_msat ) . sum ( ) ;
59465966 let mut session_priv_bytes = [ 0 ; 32 ] ;
59475967 session_priv_bytes[ ..] . copy_from_slice ( & session_priv[ ..] ) ;
59485968 match pending_outbound_payments. as_mut ( ) . unwrap ( ) . entry ( payment_id) {
59495969 hash_map:: Entry :: Occupied ( mut entry) => {
5950- let newly_added = entry. get_mut ( ) . insert ( session_priv_bytes, path_amt) ;
5970+ let newly_added = entry. get_mut ( ) . insert ( session_priv_bytes, path_amt, path_fee ) ;
59515971 log_info ! ( args. logger, "{} a pending payment path for {} msat for session priv {} on an existing pending payment with payment hash {}" ,
59525972 if newly_added { "Added" } else { "Had" } , path_amt, log_bytes!( session_priv_bytes) , log_bytes!( htlc. payment_hash. 0 ) ) ;
59535973 } ,
@@ -5957,6 +5977,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
59575977 payment_hash : htlc. payment_hash ,
59585978 payment_secret,
59595979 pending_amt_msat : path_amt,
5980+ pending_fee_msat : path_fee,
59605981 total_msat : path_amt,
59615982 starting_block_height : best_block_height,
59625983 } ) ;
@@ -6256,7 +6277,7 @@ mod tests {
62566277 // further events will be generated for subsequence path successes.
62576278 let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
62586279 match events[ 0 ] {
6259- Event :: PaymentSent { payment_preimage : ref preimage, payment_hash : ref hash } => {
6280+ Event :: PaymentSent { payment_preimage : ref preimage, payment_hash : ref hash, .. } => {
62606281 assert_eq ! ( payment_preimage, * preimage) ;
62616282 assert_eq ! ( our_payment_hash, * hash) ;
62626283 } ,
0 commit comments