@@ -436,6 +436,8 @@ pub(crate) enum PendingOutboundPayment {
436
436
payment_hash : PaymentHash ,
437
437
payment_secret : Option < PaymentSecret > ,
438
438
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 > ,
439
441
/// The total payment amount across all paths, used to verify that a retry is not overpaying.
440
442
total_msat : u64 ,
441
443
/// Our best known block height at the time this payment was initiated.
@@ -462,6 +464,12 @@ impl PendingOutboundPayment {
462
464
_ => false ,
463
465
}
464
466
}
467
+ fn get_total_fee_msat ( & self ) -> Option < u64 > {
468
+ match self {
469
+ PendingOutboundPayment :: Retryable { pending_fee_msat, .. } => pending_fee_msat. clone ( ) ,
470
+ _ => None ,
471
+ }
472
+ }
465
473
466
474
fn mark_fulfilled ( & mut self ) {
467
475
let mut session_privs = HashSet :: new ( ) ;
@@ -475,7 +483,7 @@ impl PendingOutboundPayment {
475
483
}
476
484
477
485
/// 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 {
486
+ fn remove ( & mut self , session_priv : & [ u8 ; 32 ] , part_amt_msat : Option < u64 > , part_fee_msat : Option < u64 > ) -> bool {
479
487
let remove_res = match self {
480
488
PendingOutboundPayment :: Legacy { session_privs } |
481
489
PendingOutboundPayment :: Retryable { session_privs, .. } |
@@ -484,14 +492,17 @@ impl PendingOutboundPayment {
484
492
}
485
493
} ;
486
494
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 {
488
496
* pending_amt_msat -= part_amt_msat. expect ( "We must only not provide an amount if the payment was already fulfilled" ) ;
497
+ if let Some ( fee_msat) = pending_fee_msat. as_mut ( ) {
498
+ * fee_msat -= part_fee_msat. expect ( "We must only not provide a fee if the payment was already fulfilled" ) ;
499
+ }
489
500
}
490
501
}
491
502
remove_res
492
503
}
493
504
494
- fn insert ( & mut self , session_priv : [ u8 ; 32 ] , part_amt_msat : u64 ) -> bool {
505
+ fn insert ( & mut self , session_priv : [ u8 ; 32 ] , part_amt_msat : u64 , part_fee_msat : u64 ) -> bool {
495
506
let insert_res = match self {
496
507
PendingOutboundPayment :: Legacy { session_privs } |
497
508
PendingOutboundPayment :: Retryable { session_privs, .. } => {
@@ -500,8 +511,11 @@ impl PendingOutboundPayment {
500
511
PendingOutboundPayment :: Fulfilled { .. } => false
501
512
} ;
502
513
if insert_res {
503
- if let PendingOutboundPayment :: Retryable { ref mut pending_amt_msat, .. } = self {
514
+ if let PendingOutboundPayment :: Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat , .. } = self {
504
515
* pending_amt_msat += part_amt_msat;
516
+ if let Some ( fee_msat) = pending_fee_msat. as_mut ( ) {
517
+ * fee_msat += part_fee_msat;
518
+ }
505
519
}
506
520
}
507
521
insert_res
@@ -2081,12 +2095,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2081
2095
let payment = payment_entry. or_insert_with ( || PendingOutboundPayment :: Retryable {
2082
2096
session_privs : HashSet :: new ( ) ,
2083
2097
pending_amt_msat : 0 ,
2098
+ pending_fee_msat : Some ( 0 ) ,
2084
2099
payment_hash : * payment_hash,
2085
2100
payment_secret : * payment_secret,
2086
2101
starting_block_height : self . best_block . read ( ) . unwrap ( ) . height ( ) ,
2087
2102
total_msat : total_value,
2088
2103
} ) ;
2089
- assert ! ( payment. insert( session_priv_bytes, path. last( ) . unwrap( ) . fee_msat) ) ;
2104
+ assert ! ( payment. insert( session_priv_bytes, path. last( ) . unwrap( ) . fee_msat,
2105
+ path. split_last( ) . map( |( _, path_prefix) | path_prefix) . unwrap_or( & [ ] ) . iter( ) . map( |hop| hop. fee_msat) . sum( ) ) ) ;
2090
2106
2091
2107
send_res
2092
2108
} {
@@ -3108,7 +3124,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3108
3124
let mut outbounds = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
3109
3125
if let hash_map:: Entry :: Occupied ( mut payment) = outbounds. entry ( payment_id) {
3110
3126
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 ) ) &&
3127
+ if payment. get_mut ( ) . remove ( & session_priv_bytes, Some ( path_last_hop. fee_msat ) ,
3128
+ Some ( path. split_last ( ) . map ( |( _, path_prefix) | path_prefix) . unwrap_or ( & [ ] ) . iter ( ) . map ( |hop| hop. fee_msat ) . sum ( ) ) ) &&
3112
3129
!payment. get ( ) . is_fulfilled ( )
3113
3130
{
3114
3131
let retry = if let Some ( payee_data) = payee {
@@ -3165,7 +3182,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3165
3182
let mut all_paths_failed = false ;
3166
3183
let path_last_hop = path. last ( ) . expect ( "Outbound payments must have had a valid path" ) ;
3167
3184
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 ) ) {
3185
+ if !payment. get_mut ( ) . remove ( & session_priv_bytes, Some ( path_last_hop. fee_msat ) ,
3186
+ Some ( path. split_last ( ) . map ( |( _, path_prefix) | path_prefix) . unwrap_or ( & [ ] ) . iter ( ) . map ( |hop| hop. fee_msat ) . sum ( ) ) )
3187
+ {
3169
3188
log_trace ! ( self . logger, "Received duplicative fail for HTLC with payment_hash {}" , log_bytes!( payment_hash. 0 ) ) ;
3170
3189
return ;
3171
3190
}
@@ -3438,7 +3457,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3438
3457
let mut outbounds = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
3439
3458
if let hash_map:: Entry :: Occupied ( mut payment) = outbounds. entry ( payment_id) {
3440
3459
assert ! ( payment. get( ) . is_fulfilled( ) ) ;
3441
- payment. get_mut ( ) . remove ( & session_priv_bytes, None ) ;
3460
+ payment. get_mut ( ) . remove ( & session_priv_bytes, None , None ) ;
3442
3461
if payment. get ( ) . remaining_parts ( ) == 0 {
3443
3462
payment. remove ( ) ;
3444
3463
}
@@ -3454,8 +3473,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3454
3473
let mut session_priv_bytes = [ 0 ; 32 ] ;
3455
3474
session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
3456
3475
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) {
3476
+ if let hash_map:: Entry :: Occupied ( mut payment) = outbounds. entry ( payment_id) {
3458
3477
let found_payment = !payment. get ( ) . is_fulfilled ( ) ;
3478
+ let fee_paid_msat = payment. get ( ) . get_total_fee_msat ( ) ;
3459
3479
payment. get_mut ( ) . mark_fulfilled ( ) ;
3460
3480
if from_onchain {
3461
3481
// We currently immediately remove HTLCs which were fulfilled on-chain.
@@ -3464,21 +3484,22 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3464
3484
// restart.
3465
3485
// TODO: We should have a second monitor event that informs us of payments
3466
3486
// irrevocably fulfilled.
3467
- payment. get_mut ( ) . remove ( & session_priv_bytes, Some ( path. last ( ) . unwrap ( ) . fee_msat ) ) ;
3487
+ payment. get_mut ( ) . remove ( & session_priv_bytes, Some ( path. last ( ) . unwrap ( ) . fee_msat ) ,
3488
+ Some ( path. split_last ( ) . map ( |( _, path_prefix) | path_prefix) . unwrap_or ( & [ ] ) . iter ( ) . map ( |hop| hop. fee_msat ) . sum ( ) ) ) ;
3468
3489
if payment. get ( ) . remaining_parts ( ) == 0 {
3469
3490
payment. remove ( ) ;
3470
3491
}
3471
3492
}
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
- ) ;
3493
+ if found_payment {
3494
+ let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage . 0 ) . into_inner ( ) ) ;
3495
+ self . pending_events . lock ( ) . unwrap ( ) . push (
3496
+ events :: Event :: PaymentSent {
3497
+ payment_preimage ,
3498
+ payment_hash : payment_hash ,
3499
+ fee_paid_msat ,
3500
+ }
3501
+ ) ;
3502
+ }
3482
3503
} else {
3483
3504
log_trace ! ( self . logger, "Received duplicative fulfill for HTLC with payment_preimage {}" , log_bytes!( payment_preimage. 0 ) ) ;
3484
3505
}
@@ -5491,6 +5512,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
5491
5512
} ,
5492
5513
( 2 , Retryable ) => {
5493
5514
( 0 , session_privs, required) ,
5515
+ ( 1 , pending_fee_msat, option) ,
5494
5516
( 2 , payment_hash, required) ,
5495
5517
( 4 , payment_secret, option) ,
5496
5518
( 6 , total_msat, required) ,
@@ -5943,11 +5965,12 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
5943
5965
return Err ( DecodeError :: InvalidValue ) ;
5944
5966
}
5945
5967
let path_amt = path. last ( ) . unwrap ( ) . fee_msat ;
5968
+ let path_fee = path. split_last ( ) . map ( |( _, path_prefix) | path_prefix) . unwrap_or ( & [ ] ) . iter ( ) . map ( |hop| hop. fee_msat ) . sum ( ) ;
5946
5969
let mut session_priv_bytes = [ 0 ; 32 ] ;
5947
5970
session_priv_bytes[ ..] . copy_from_slice ( & session_priv[ ..] ) ;
5948
5971
match pending_outbound_payments. as_mut ( ) . unwrap ( ) . entry ( payment_id) {
5949
5972
hash_map:: Entry :: Occupied ( mut entry) => {
5950
- let newly_added = entry. get_mut ( ) . insert ( session_priv_bytes, path_amt) ;
5973
+ let newly_added = entry. get_mut ( ) . insert ( session_priv_bytes, path_amt, path_fee ) ;
5951
5974
log_info ! ( args. logger, "{} a pending payment path for {} msat for session priv {} on an existing pending payment with payment hash {}" ,
5952
5975
if newly_added { "Added" } else { "Had" } , path_amt, log_bytes!( session_priv_bytes) , log_bytes!( htlc. payment_hash. 0 ) ) ;
5953
5976
} ,
@@ -5957,6 +5980,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
5957
5980
payment_hash : htlc. payment_hash ,
5958
5981
payment_secret,
5959
5982
pending_amt_msat : path_amt,
5983
+ pending_fee_msat : Some ( path_fee) ,
5960
5984
total_msat : path_amt,
5961
5985
starting_block_height : best_block_height,
5962
5986
} ) ;
@@ -6256,7 +6280,7 @@ mod tests {
6256
6280
// further events will be generated for subsequence path successes.
6257
6281
let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
6258
6282
match events[ 0 ] {
6259
- Event :: PaymentSent { payment_preimage : ref preimage, payment_hash : ref hash } => {
6283
+ Event :: PaymentSent { payment_preimage : ref preimage, payment_hash : ref hash, .. } => {
6260
6284
assert_eq ! ( payment_preimage, * preimage) ;
6261
6285
assert_eq ! ( our_payment_hash, * hash) ;
6262
6286
} ,
0 commit comments