@@ -45,7 +45,7 @@ use chain::transaction::{OutPoint, TransactionData};
45
45
use ln:: { PaymentHash , PaymentPreimage , PaymentSecret } ;
46
46
use ln:: channel:: { Channel , ChannelError , ChannelUpdateStatus , UpdateFulfillCommitFetch } ;
47
47
use ln:: features:: { InitFeatures , NodeFeatures } ;
48
- use routing:: router:: { Payee , Route , RouteHop , RouteParameters } ;
48
+ use routing:: router:: { Payee , Route , RouteHop , RoutePath , RouteParameters } ;
49
49
use ln:: msgs;
50
50
use ln:: msgs:: NetAddress ;
51
51
use ln:: onion_utils;
@@ -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_pending_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 ( ) ;
@@ -484,10 +492,13 @@ 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
let path = path. expect ( "Fulfilling a payment should always come with a path" ) ;
489
497
let path_last_hop = path. last ( ) . expect ( "Outbound payments must have had a valid path" ) ;
490
498
* 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
+ }
491
502
}
492
503
}
493
504
remove_res
@@ -502,9 +513,12 @@ impl PendingOutboundPayment {
502
513
PendingOutboundPayment :: Fulfilled { .. } => false
503
514
} ;
504
515
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 {
506
517
let path_last_hop = path. last ( ) . expect ( "Outbound payments must have had a valid path" ) ;
507
518
* 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
+ }
508
522
}
509
523
}
510
524
insert_res
@@ -2084,6 +2098,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2084
2098
let payment = payment_entry. or_insert_with ( || PendingOutboundPayment :: Retryable {
2085
2099
session_privs : HashSet :: new ( ) ,
2086
2100
pending_amt_msat : 0 ,
2101
+ pending_fee_msat : Some ( 0 ) ,
2087
2102
payment_hash : * payment_hash,
2088
2103
payment_secret : * payment_secret,
2089
2104
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
3458
3473
let mut session_priv_bytes = [ 0 ; 32 ] ;
3459
3474
session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
3460
3475
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) {
3462
3477
let found_payment = !payment. get ( ) . is_fulfilled ( ) ;
3478
+ let fee_paid_msat = payment. get ( ) . get_pending_fee_msat ( ) ;
3463
3479
payment. get_mut ( ) . mark_fulfilled ( ) ;
3464
3480
if from_onchain {
3465
3481
// 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
3473
3489
payment. remove ( ) ;
3474
3490
}
3475
3491
}
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
+ }
3487
3503
} else {
3488
3504
log_trace ! ( self . logger, "Received duplicative fulfill for HTLC with payment_preimage {}" , log_bytes!( payment_preimage. 0 ) ) ;
3489
3505
}
@@ -5496,6 +5512,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
5496
5512
} ,
5497
5513
( 2 , Retryable ) => {
5498
5514
( 0 , session_privs, required) ,
5515
+ ( 1 , pending_fee_msat, option) ,
5499
5516
( 2 , payment_hash, required) ,
5500
5517
( 4 , payment_secret, option) ,
5501
5518
( 6 , total_msat, required) ,
@@ -5957,11 +5974,13 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
5957
5974
if newly_added { "Added" } else { "Had" } , path_amt, log_bytes!( session_priv_bytes) , log_bytes!( htlc. payment_hash. 0 ) ) ;
5958
5975
} ,
5959
5976
hash_map:: Entry :: Vacant ( entry) => {
5977
+ let path_fee = path. get_path_fees ( ) ;
5960
5978
entry. insert ( PendingOutboundPayment :: Retryable {
5961
5979
session_privs : [ session_priv_bytes] . iter ( ) . map ( |a| * a) . collect ( ) ,
5962
5980
payment_hash : htlc. payment_hash ,
5963
5981
payment_secret,
5964
5982
pending_amt_msat : path_amt,
5983
+ pending_fee_msat : Some ( path_fee) ,
5965
5984
total_msat : path_amt,
5966
5985
starting_block_height : best_block_height,
5967
5986
} ) ;
@@ -6260,7 +6279,7 @@ mod tests {
6260
6279
// further events will be generated for subsequence path successes.
6261
6280
let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
6262
6281
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, .. } => {
6264
6283
assert_eq ! ( Some ( payment_id) , * id) ;
6265
6284
assert_eq ! ( payment_preimage, * preimage) ;
6266
6285
assert_eq ! ( our_payment_hash, * hash) ;
0 commit comments