Skip to content

Commit 368c534

Browse files
committed
Store available routing amounts per channel to use it in routing decisions
1 parent 74cd96f commit 368c534

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

fuzz/src/router.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
231231
proportional_millionths: slice_to_be32(get_slice!(4)),
232232
},
233233
cltv_expiry_delta: slice_to_be16(get_slice!(2)),
234-
htlc_minimum_msat: slice_to_be64(get_slice!(8)),
234+
htlc_minimum_msat: Some(slice_to_be64(get_slice!(8))),
235+
htlc_maximum_msat: None,
235236
});
236237
}
237238
}

lightning/src/routing/router.rs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ pub struct RouteHint {
124124
/// The difference in CLTV values between this node and the next node.
125125
pub cltv_expiry_delta: u16,
126126
/// The minimum value, in msat, which must be relayed to the next hop.
127-
pub htlc_minimum_msat: u64,
127+
pub htlc_minimum_msat: Option<u64>,
128+
/// The maximum value in msat available for routing with a single HTLC.
129+
pub htlc_maximum_msat: Option<u64>,
128130
}
129131

130132
#[derive(Eq, PartialEq)]
@@ -150,6 +152,7 @@ impl cmp::PartialOrd for RouteGraphNode {
150152
struct DummyDirectionalChannelInfo {
151153
cltv_expiry_delta: u32,
152154
htlc_minimum_msat: u64,
155+
htlc_maximum_msat: Option<u64>,
153156
fees: RoutingFees,
154157
}
155158

@@ -191,6 +194,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, targ
191194
let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
192195
cltv_expiry_delta: 0,
193196
htlc_minimum_msat: 0,
197+
htlc_maximum_msat: None,
194198
fees: RoutingFees {
195199
base_msat: 0,
196200
proportional_millionths: 0,
@@ -227,14 +231,24 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, targ
227231
// Adds entry which goes from $src_node_id to $dest_node_id
228232
// over the channel with id $chan_id with fees described in
229233
// $directional_info.
230-
( $chan_id: expr, $src_node_id: expr, $dest_node_id: expr, $directional_info: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
234+
( $chan_id: expr, $src_node_id: expr, $dest_node_id: expr, $directional_info: expr, $capacity_sats: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
231235
//TODO: Explore simply adding fee to hit htlc_minimum_msat
232236
if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
233237
let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fees.proportional_millionths as u64);
234238
if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
235239
($directional_info.fees.base_msat as u64).checked_add(part / 1000000) })
236240
{
237241
let mut total_fee = $starting_fee_msat as u64;
242+
243+
let mut available_msat = $capacity_sats;
244+
if let Some(htlc_maximum_msat) = $directional_info.htlc_maximum_msat {
245+
if let Some(capacity_sats) = $capacity_sats {
246+
available_msat = Some(cmp::min(capacity_sats * 1000, htlc_maximum_msat));
247+
} else {
248+
available_msat = Some(htlc_maximum_msat);
249+
}
250+
}
251+
238252
let hm_entry = dist.entry(&$src_node_id);
239253
let old_entry = hm_entry.or_insert_with(|| {
240254
let mut fee_base_msat = u32::max_value();
@@ -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

@@ -350,7 +366,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, targ
350366
// bit lazy here. In the future, we should pull them out via our
351367
// ChannelManager, but there's no reason to waste the space until we
352368
// need them.
353-
add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, features.to_context(), 0);
369+
add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, None::<u64>, features.to_context(), 0);
354370
true
355371
} else {
356372
// In any other case, only add the hop if the source is in the regular network
@@ -360,7 +376,17 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, targ
360376
if have_hop_src_in_graph {
361377
// BOLT 11 doesn't allow inclusion of features for the last hop hints, which
362378
// really sucks, cause we're gonna need that eventually.
363-
add_entry!(hop.short_channel_id, hop.src_node_id, target, hop, ChannelFeatures::empty(), 0);
379+
let last_hop_htlc_minimum_msat: u64 = match hop.htlc_minimum_msat {
380+
Some(htlc_minimum_msat) => htlc_minimum_msat,
381+
None => 0
382+
};
383+
let directional_info = DummyDirectionalChannelInfo {
384+
cltv_expiry_delta: hop.cltv_expiry_delta as u32,
385+
htlc_minimum_msat: last_hop_htlc_minimum_msat,
386+
htlc_maximum_msat: hop.htlc_maximum_msat,
387+
fees: hop.fees,
388+
};
389+
add_entry!(hop.short_channel_id, hop.src_node_id, target, directional_info, None::<u64>, ChannelFeatures::empty(), 0);
364390
}
365391
}
366392

@@ -1048,7 +1074,8 @@ mod tests {
10481074
short_channel_id: 8,
10491075
fees: zero_fees,
10501076
cltv_expiry_delta: (8 << 8) | 1,
1051-
htlc_minimum_msat: 0,
1077+
htlc_minimum_msat: None,
1078+
htlc_maximum_msat: None,
10521079
}, RouteHint {
10531080
src_node_id: nodes[4].clone(),
10541081
short_channel_id: 9,
@@ -1057,13 +1084,15 @@ mod tests {
10571084
proportional_millionths: 0,
10581085
},
10591086
cltv_expiry_delta: (9 << 8) | 1,
1060-
htlc_minimum_msat: 0,
1087+
htlc_minimum_msat: None,
1088+
htlc_maximum_msat: None,
10611089
}, RouteHint {
10621090
src_node_id: nodes[5].clone(),
10631091
short_channel_id: 10,
10641092
fees: zero_fees,
10651093
cltv_expiry_delta: (10 << 8) | 1,
1066-
htlc_minimum_msat: 0,
1094+
htlc_minimum_msat: None,
1095+
htlc_maximum_msat: None,
10671096
})
10681097
}
10691098

@@ -1245,7 +1274,8 @@ mod tests {
12451274
proportional_millionths: 0,
12461275
},
12471276
cltv_expiry_delta: (8 << 8) | 1,
1248-
htlc_minimum_msat: 0,
1277+
htlc_minimum_msat: None,
1278+
htlc_maximum_msat: None,
12491279
}];
12501280
let our_chans = vec![channelmanager::ChannelDetails {
12511281
channel_id: [0; 32],

0 commit comments

Comments
 (0)