@@ -967,33 +967,24 @@ impl_writeable_tlv_based!(RouteHintHop, {
967
967
} ) ;
968
968
969
969
#[ derive( Eq , PartialEq ) ]
970
+ #[ repr( C ) ] // Force our field ordering to keep the size 64 bytes
970
971
struct RouteGraphNode {
971
972
node_id : NodeId ,
972
- lowest_fee_to_node : u64 ,
973
- total_cltv_delta : u32 ,
973
+ score : u64 ,
974
974
// The maximum value a yet-to-be-constructed payment path might flow through this node.
975
975
// This value is upper-bounded by us by:
976
976
// - how much is needed for a path being constructed
977
977
// - how much value can channels following this node (up to the destination) can contribute,
978
978
// considering their capacity and fees
979
979
value_contribution_msat : u64 ,
980
- /// The effective htlc_minimum_msat at this hop. If a later hop on the path had a higher HTLC
981
- /// minimum, we use it, plus the fees required at each earlier hop to meet it.
982
- path_htlc_minimum_msat : u64 ,
983
- /// All penalties incurred from this hop on the way to the destination, as calculated using
984
- /// channel scoring.
985
- path_penalty_msat : u64 ,
980
+ total_cltv_delta : u32 ,
986
981
/// The number of hops walked up to this node.
987
982
path_length_to_node : u8 ,
988
983
}
989
984
990
985
impl cmp:: Ord for RouteGraphNode {
991
986
fn cmp ( & self , other : & RouteGraphNode ) -> cmp:: Ordering {
992
- let other_score = cmp:: max ( other. lowest_fee_to_node , other. path_htlc_minimum_msat )
993
- . saturating_add ( other. path_penalty_msat ) ;
994
- let self_score = cmp:: max ( self . lowest_fee_to_node , self . path_htlc_minimum_msat )
995
- . saturating_add ( self . path_penalty_msat ) ;
996
- other_score. cmp ( & self_score) . then_with ( || other. node_id . cmp ( & self . node_id ) )
987
+ other. score . cmp ( & self . score ) . then_with ( || other. node_id . cmp ( & self . node_id ) )
997
988
}
998
989
}
999
990
@@ -1003,6 +994,16 @@ impl cmp::PartialOrd for RouteGraphNode {
1003
994
}
1004
995
}
1005
996
997
+ // While RouteGraphNode can be laid out as 56 bytes, performance appears to be improved
998
+ // substantially when it is laid out at exactly 64 bytes.
999
+ //
1000
+ // Thus, we use `#[repr(C)]` on the struct to force a suboptimal layout and check that it stays 64
1001
+ // bytes here.
1002
+ #[ cfg( any( ldk_bench, not( any( test, fuzzing) ) ) ) ]
1003
+ const _GRAPH_NODE_SMALL: usize = 64 - core:: mem:: size_of :: < RouteGraphNode > ( ) ;
1004
+ #[ cfg( any( ldk_bench, not( any( test, fuzzing) ) ) ) ]
1005
+ const _GRAPH_NODE_FIXED_SIZE: usize = core:: mem:: size_of :: < RouteGraphNode > ( ) - 64 ;
1006
+
1006
1007
/// A wrapper around the various hop representations.
1007
1008
///
1008
1009
/// Can be used to examine the properties of a hop,
@@ -2119,15 +2120,6 @@ where L::Target: Logger {
2119
2120
score_params) ;
2120
2121
let path_penalty_msat = $next_hops_path_penalty_msat
2121
2122
. saturating_add( channel_penalty_msat) ;
2122
- let new_graph_node = RouteGraphNode {
2123
- node_id: src_node_id,
2124
- lowest_fee_to_node: total_fee_msat,
2125
- total_cltv_delta: hop_total_cltv_delta,
2126
- value_contribution_msat,
2127
- path_htlc_minimum_msat,
2128
- path_penalty_msat,
2129
- path_length_to_node,
2130
- } ;
2131
2123
2132
2124
// Update the way of reaching $candidate.source()
2133
2125
// with the given short_channel_id (from $candidate.target()),
@@ -2152,6 +2144,13 @@ where L::Target: Logger {
2152
2144
. saturating_add( path_penalty_msat) ;
2153
2145
2154
2146
if !old_entry. was_processed && new_cost < old_cost {
2147
+ let new_graph_node = RouteGraphNode {
2148
+ node_id: src_node_id,
2149
+ score: cmp:: max( total_fee_msat, path_htlc_minimum_msat) . saturating_add( path_penalty_msat) ,
2150
+ total_cltv_delta: hop_total_cltv_delta,
2151
+ value_contribution_msat,
2152
+ path_length_to_node,
2153
+ } ;
2155
2154
targets. push( new_graph_node) ;
2156
2155
old_entry. next_hops_fee_msat = $next_hops_fee_msat;
2157
2156
old_entry. hop_use_fee_msat = hop_use_fee_msat;
@@ -2225,18 +2224,26 @@ where L::Target: Logger {
2225
2224
// meaning how much will be paid in fees after this node (to the best of our knowledge).
2226
2225
// This data can later be helpful to optimize routing (pay lower fees).
2227
2226
macro_rules! add_entries_to_cheapest_to_target_node {
2228
- ( $node: expr, $node_id: expr, $fee_to_target_msat: expr, $next_hops_value_contribution: expr,
2229
- $next_hops_path_htlc_minimum_msat: expr, $next_hops_path_penalty_msat: expr,
2227
+ ( $node: expr, $node_id: expr, $next_hops_value_contribution: expr,
2230
2228
$next_hops_cltv_delta: expr, $next_hops_path_length: expr ) => {
2229
+ let fee_to_target_msat;
2230
+ let next_hops_path_htlc_minimum_msat;
2231
+ let next_hops_path_penalty_msat;
2231
2232
let skip_node = if let Some ( elem) = dist. get_mut( & $node_id) {
2232
2233
let was_processed = elem. was_processed;
2233
2234
elem. was_processed = true ;
2235
+ fee_to_target_msat = elem. total_fee_msat;
2236
+ next_hops_path_htlc_minimum_msat = elem. path_htlc_minimum_msat;
2237
+ next_hops_path_penalty_msat = elem. path_penalty_msat;
2234
2238
was_processed
2235
2239
} else {
2236
2240
// Entries are added to dist in add_entry!() when there is a channel from a node.
2237
2241
// Because there are no channels from payee, it will not have a dist entry at this point.
2238
2242
// If we're processing any other node, it is always be the result of a channel from it.
2239
2243
debug_assert_eq!( $node_id, maybe_dummy_payee_node_id) ;
2244
+ fee_to_target_msat = 0 ;
2245
+ next_hops_path_htlc_minimum_msat = 0 ;
2246
+ next_hops_path_penalty_msat = 0 ;
2240
2247
false
2241
2248
} ;
2242
2249
@@ -2246,9 +2253,9 @@ where L::Target: Logger {
2246
2253
let candidate = CandidateRouteHop :: FirstHop {
2247
2254
details, payer_node_id: & our_node_id,
2248
2255
} ;
2249
- add_entry!( & candidate, $ fee_to_target_msat,
2256
+ add_entry!( & candidate, fee_to_target_msat,
2250
2257
$next_hops_value_contribution,
2251
- $ next_hops_path_htlc_minimum_msat, $ next_hops_path_penalty_msat,
2258
+ next_hops_path_htlc_minimum_msat, next_hops_path_penalty_msat,
2252
2259
$next_hops_cltv_delta, $next_hops_path_length) ;
2253
2260
}
2254
2261
}
@@ -2271,10 +2278,10 @@ where L::Target: Logger {
2271
2278
short_channel_id: * chan_id,
2272
2279
} ;
2273
2280
add_entry!( & candidate,
2274
- $ fee_to_target_msat,
2281
+ fee_to_target_msat,
2275
2282
$next_hops_value_contribution,
2276
- $ next_hops_path_htlc_minimum_msat,
2277
- $ next_hops_path_penalty_msat,
2283
+ next_hops_path_htlc_minimum_msat,
2284
+ next_hops_path_penalty_msat,
2278
2285
$next_hops_cltv_delta, $next_hops_path_length) ;
2279
2286
}
2280
2287
}
@@ -2319,7 +2326,7 @@ where L::Target: Logger {
2319
2326
// If not, targets.pop() will not even let us enter the loop in step 2.
2320
2327
None => { } ,
2321
2328
Some ( node) => {
2322
- add_entries_to_cheapest_to_target_node ! ( node, payee, 0 , path_value_msat, 0 , 0u64 , 0 , 0 ) ;
2329
+ add_entries_to_cheapest_to_target_node ! ( node, payee, path_value_msat, 0 , 0 ) ;
2323
2330
} ,
2324
2331
} ) ;
2325
2332
@@ -2526,7 +2533,7 @@ where L::Target: Logger {
2526
2533
// Both these cases (and other cases except reaching recommended_value_msat) mean that
2527
2534
// paths_collection will be stopped because found_new_path==false.
2528
2535
// This is not necessarily a routing failure.
2529
- ' path_construction: while let Some ( RouteGraphNode { node_id, lowest_fee_to_node , total_cltv_delta, mut value_contribution_msat, path_htlc_minimum_msat , path_penalty_msat , path_length_to_node, .. } ) = targets. pop ( ) {
2536
+ ' path_construction: while let Some ( RouteGraphNode { node_id, total_cltv_delta, mut value_contribution_msat, path_length_to_node, .. } ) = targets. pop ( ) {
2530
2537
2531
2538
// Since we're going payee-to-payer, hitting our node as a target means we should stop
2532
2539
// traversing the graph and arrange the path out of what we found.
@@ -2661,8 +2668,8 @@ where L::Target: Logger {
2661
2668
match network_nodes. get ( & node_id) {
2662
2669
None => { } ,
2663
2670
Some ( node) => {
2664
- add_entries_to_cheapest_to_target_node ! ( node, node_id, lowest_fee_to_node ,
2665
- value_contribution_msat, path_htlc_minimum_msat , path_penalty_msat ,
2671
+ add_entries_to_cheapest_to_target_node ! ( node, node_id,
2672
+ value_contribution_msat,
2666
2673
total_cltv_delta, path_length_to_node) ;
2667
2674
} ,
2668
2675
}
0 commit comments