Skip to content

Commit ea96e54

Browse files
committed
Store available routing amounts per channel to use it in routing decisions
1 parent 3defcc8 commit ea96e54

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

fuzz/src/router.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
232232
},
233233
cltv_expiry_delta: slice_to_be16(get_slice!(2)),
234234
htlc_minimum_msat: slice_to_be64(get_slice!(8)),
235+
htlc_maximum_msat: None,
235236
});
236237
}
237238
&last_hops_vec[..]

lightning/src/routing/router.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ pub struct RouteHint {
124124
pub cltv_expiry_delta: u16,
125125
/// The minimum value, in msat, which must be relayed to the next hop.
126126
pub htlc_minimum_msat: u64,
127+
/// The maximum value in msat available for routing with a single HTLC.
128+
pub htlc_maximum_msat: Option<u64>,
127129
}
128130

129131
#[derive(Eq, PartialEq)]
@@ -149,6 +151,7 @@ impl cmp::PartialOrd for RouteGraphNode {
149151
struct DummyDirectionalChannelInfo {
150152
cltv_expiry_delta: u32,
151153
htlc_minimum_msat: u64,
154+
htlc_maximum_msat: Option<u64>,
152155
fees: RoutingFees,
153156
}
154157

@@ -190,6 +193,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, targ
190193
let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
191194
cltv_expiry_delta: 0,
192195
htlc_minimum_msat: 0,
196+
htlc_maximum_msat: None,
193197
fees: RoutingFees {
194198
base_msat: 0,
195199
proportional_millionths: 0,
@@ -226,14 +230,24 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, targ
226230
// Adds entry which goes from $src_node_id to $dest_node_id
227231
// over the channel with id $chan_id with fees described in
228232
// $directional_info.
229-
( $chan_id: expr, $src_node_id: expr, $dest_node_id: expr, $directional_info: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
233+
( $chan_id: expr, $src_node_id: expr, $dest_node_id: expr, $directional_info: expr, $capacity_sats: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
230234
//TODO: Explore simply adding fee to hit htlc_minimum_msat
231235
if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
232236
let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fees.proportional_millionths as u64);
233237
if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
234238
($directional_info.fees.base_msat as u64).checked_add(part / 1000000) })
235239
{
236240
let mut total_fee = $starting_fee_msat as u64;
241+
242+
let mut available_msat = $capacity_sats;
243+
if let Some(htlc_maximum_msat) = $directional_info.htlc_maximum_msat {
244+
if let Some(capacity_sats) = $capacity_sats {
245+
available_msat = Some(cmp::min(capacity_sats * 1000, htlc_maximum_msat));
246+
} else {
247+
available_msat = Some(htlc_maximum_msat);
248+
}
249+
}
250+
237251
let hm_entry = dist.entry(&$src_node_id);
238252
let old_entry = hm_entry.or_insert_with(|| {
239253
let node = network.get_nodes().get(&$src_node_id).unwrap();
@@ -254,6 +268,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, targ
254268
fee_msat: 0,
255269
cltv_expiry_delta: 0,
256270
},
271+
None,
257272
)
258273
});
259274
if $src_node_id != *our_node_id {
@@ -282,7 +297,8 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, targ
282297
channel_features: $chan_features.clone(),
283298
fee_msat: new_fee, // This field is ignored on the last-hop anyway
284299
cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
285-
}
300+
};
301+
old_entry.4 = available_msat;
286302
}
287303
}
288304
}
@@ -293,7 +309,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, targ
293309
( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
294310
if first_hops.is_some() {
295311
if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) {
296-
add_entry!(first_hop, *our_node_id, $node_id, dummy_directional_info, features.to_context(), $fee_to_target_msat);
312+
add_entry!(first_hop, *our_node_id, $node_id, dummy_directional_info, None::<u64>, features.to_context(), $fee_to_target_msat);
297313
}
298314
}
299315

@@ -313,15 +329,15 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, targ
313329
if first_hops.is_none() || chan.node_two != *our_node_id {
314330
if let Some(two_to_one) = chan.two_to_one.as_ref() {
315331
if two_to_one.enabled {
316-
add_entry!(chan_id, chan.node_two, chan.node_one, two_to_one, chan.features, $fee_to_target_msat);
332+
add_entry!(chan_id, chan.node_two, chan.node_one, two_to_one, chan.capacity_sats, chan.features, $fee_to_target_msat);
317333
}
318334
}
319335
}
320336
} else {
321337
if first_hops.is_none() || chan.node_one != *our_node_id {
322338
if let Some(one_to_two) = chan.one_to_two.as_ref() {
323339
if one_to_two.enabled {
324-
add_entry!(chan_id, chan.node_one, chan.node_two, one_to_two, chan.features, $fee_to_target_msat);
340+
add_entry!(chan_id, chan.node_one, chan.node_two, one_to_two, chan.capacity_sats, chan.features, $fee_to_target_msat);
325341
}
326342
}
327343

@@ -349,12 +365,12 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, targ
349365
// bit lazy here. In the future, we should pull them out via our
350366
// ChannelManager, but there's no reason to waste the space until we
351367
// need them.
352-
add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, features.to_context(), 0);
368+
add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, None::<u64>, features.to_context(), 0);
353369
}
354370
}
355371
// BOLT 11 doesn't allow inclusion of features for the last hop hints, which
356372
// really sucks, cause we're gonna need that eventually.
357-
add_entry!(hop.short_channel_id, hop.src_node_id, target, hop, ChannelFeatures::empty(), 0);
373+
add_entry!(hop.short_channel_id, hop.src_node_id, target, hop, None::<u64>, ChannelFeatures::empty(), 0);
358374
}
359375
}
360376
}
@@ -1047,6 +1063,7 @@ mod tests {
10471063
fees: zero_fees,
10481064
cltv_expiry_delta: (8 << 8) | 1,
10491065
htlc_minimum_msat: 0,
1066+
htlc_maximum_msat: None,
10501067
}, RouteHint {
10511068
src_node_id: nodes[4].clone(),
10521069
short_channel_id: 9,
@@ -1056,12 +1073,14 @@ mod tests {
10561073
},
10571074
cltv_expiry_delta: (9 << 8) | 1,
10581075
htlc_minimum_msat: 0,
1076+
htlc_maximum_msat: None,
10591077
}, RouteHint {
10601078
src_node_id: nodes[5].clone(),
10611079
short_channel_id: 10,
10621080
fees: zero_fees,
10631081
cltv_expiry_delta: (10 << 8) | 1,
10641082
htlc_minimum_msat: 0,
1083+
htlc_maximum_msat: None,
10651084
})
10661085
}
10671086

0 commit comments

Comments
 (0)