Skip to content

Commit a445567

Browse files
committed
fix
1 parent 0d07cce commit a445567

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lightning/src/routing/router.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ struct PathBuildingHop {
200200
/// hop_use_fee_msat pays for the following hops, but is deducted on this one.
201201
/// Should not be updated manually and generally should not be accessed.
202202
effective_htlc_minimum_msat: u64,
203+
overpaid_fee_msat: u64,
203204
}
204205

205206
impl PathBuildingHop {
@@ -518,8 +519,13 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
518519
// If HTLC minimum is larger than the amount we're going to transfer, we shouldn
519520
// transfer that value instead, thus overpaying fees.
520521
let effective_htlc_minimum_msat = cmp::max($directional_info.htlc_minimum_msat, $incl_fee_next_hops_htlc_minimum_msat);
521-
amount_to_transfer_over_msat = cmp::max(amount_to_transfer_over_msat, effective_htlc_minimum_msat);
522-
if contributes_sufficient_value {
522+
println!("{} transfer over {} {}", $chan_id, amount_to_transfer_over_msat, effective_htlc_minimum_msat);
523+
let overpaid_fee_msat = match effective_htlc_minimum_msat.checked_sub(amount_to_transfer_over_msat) {
524+
Some(fee_msat) => fee_msat,
525+
None => 0,
526+
};
527+
amount_to_transfer_over_msat += overpaid_fee_msat;
528+
if contributes_sufficient_value {
523529
let hm_entry = dist.entry(&$src_node_id);
524530
let old_entry = hm_entry.or_insert_with(|| {
525531
// If there was previously no known way to access
@@ -552,6 +558,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
552558
total_fee_msat: u64::max_value(),
553559
htlc_minimum_msat: $directional_info.htlc_minimum_msat,
554560
effective_htlc_minimum_msat,
561+
overpaid_fee_msat
555562
}
556563
});
557564

@@ -613,14 +620,15 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
613620
node_features: NodeFeatures::empty(),
614621
short_channel_id: $chan_id.clone(),
615622
channel_features: $chan_features.clone(),
616-
fee_msat: 0, // This value will be later filled with hop_use_fee_msat of the following channel
623+
fee_msat: 0,
617624
cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
618625
};
619626
old_entry.channel_fees = $directional_info.fees;
620627
// It's probably fine to replace the old entry, because the new one
621628
// passed the htlc_minimum-related checks above.
622629
old_entry.htlc_minimum_msat = cmp::max($directional_info.htlc_minimum_msat, old_entry.htlc_minimum_msat);
623630
old_entry.effective_htlc_minimum_msat = effective_htlc_minimum_msat;
631+
old_entry.overpaid_fee_msat = overpaid_fee_msat;
624632
}
625633
}
626634
}
@@ -806,11 +814,11 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
806814
// We "propagate" the fees one hop backward (topologically) here,
807815
// so that fees paid for a HTLC forwarding on the current channel are
808816
// associated with the previous channel (where they will be subtracted).
809-
ordered_hops.last_mut().unwrap().route_hop.fee_msat = new_entry.hop_use_fee_msat;
817+
ordered_hops.last_mut().unwrap().route_hop.fee_msat = new_entry.hop_use_fee_msat + ordered_hops.last_mut().unwrap().overpaid_fee_msat;
810818
ordered_hops.last_mut().unwrap().route_hop.cltv_expiry_delta = new_entry.route_hop.cltv_expiry_delta;
811819
ordered_hops.push(new_entry.clone());
812820
}
813-
ordered_hops.last_mut().unwrap().route_hop.fee_msat = value_contribution_msat;
821+
ordered_hops.last_mut().unwrap().route_hop.fee_msat = value_contribution_msat + ordered_hops.last_mut().unwrap().overpaid_fee_msat;
814822
ordered_hops.last_mut().unwrap().hop_use_fee_msat = 0;
815823
ordered_hops.last_mut().unwrap().route_hop.cltv_expiry_delta = final_cltv;
816824

0 commit comments

Comments
 (0)