@@ -45,6 +45,10 @@ use bitcoin::hashes::hex::ToHex;
45
45
/// refuse to relay the message.
46
46
const MAX_EXCESS_BYTES_FOR_RELAY : usize = 1024 ;
47
47
48
+ /// Maximum number of short_channel_ids that will be encoded in one gossip reply message.
49
+ /// This value ensures a reply fits within the 65k payload limit and is consistent with other implementations.
50
+ const MAX_SCIDS_PER_REPLY : usize = 8000 ;
51
+
48
52
/// Represents the network as nodes and channels between them
49
53
#[ derive( Clone , PartialEq ) ]
50
54
pub struct NetworkGraph {
@@ -71,11 +75,6 @@ pub struct NetGraphMsgHandler<C: Deref, L: Deref> where C::Target: chain::Access
71
75
full_syncs_requested : AtomicUsize ,
72
76
pending_events : Mutex < Vec < MessageSendEvent > > ,
73
77
logger : L ,
74
-
75
- /// Maximum number of short_channel_ids that will be encoded in one gossip reply message.
76
- /// Default is 8000 which ensures a reply fits within the 65k payload limit and is
77
- /// consistent with other implementations.
78
- max_reply_scids : usize ,
79
78
}
80
79
81
80
impl < C : Deref , L : Deref > NetGraphMsgHandler < C , L > where C :: Target : chain:: Access , L :: Target : Logger {
@@ -92,7 +91,6 @@ impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access
92
91
chain_access,
93
92
pending_events : Mutex :: new ( vec ! [ ] ) ,
94
93
logger,
95
- max_reply_scids : 8000 ,
96
94
}
97
95
}
98
96
@@ -106,7 +104,6 @@ impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access
106
104
chain_access,
107
105
pending_events : Mutex :: new ( vec ! [ ] ) ,
108
106
logger,
109
- max_reply_scids : 8000 ,
110
107
}
111
108
}
112
109
@@ -354,12 +351,12 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
354
351
// Creates channel batches. We are not checking if the channel is routable
355
352
// (has at least one update). A peer may still want to know the channel
356
353
// exists even if its not yet routable.
357
- let mut batches: Vec < Vec < u64 > > = vec ! [ Vec :: with_capacity( self . max_reply_scids ) ] ;
354
+ let mut batches: Vec < Vec < u64 > > = vec ! [ Vec :: with_capacity( MAX_SCIDS_PER_REPLY ) ] ;
358
355
for ( _, ref chan) in network_graph. get_channels ( ) . range ( start_scid. unwrap ( ) ..exclusive_end_scid. unwrap ( ) ) {
359
356
if let Some ( chan_announcement) = & chan. announcement_message {
360
357
// Construct a new batch if last one is full
361
358
if batches. last ( ) . unwrap ( ) . len ( ) == batches. last ( ) . unwrap ( ) . capacity ( ) {
362
- batches. push ( Vec :: with_capacity ( self . max_reply_scids ) ) ;
359
+ batches. push ( Vec :: with_capacity ( MAX_SCIDS_PER_REPLY ) ) ;
363
360
}
364
361
365
362
let batch = batches. last_mut ( ) . unwrap ( ) ;
@@ -1121,6 +1118,7 @@ mod tests {
1121
1118
use util:: logger:: Logger ;
1122
1119
use util:: ser:: { Readable , Writeable } ;
1123
1120
use util:: events:: { MessageSendEvent , MessageSendEventsProvider } ;
1121
+ use util:: scid_utils:: scid_from_parts;
1124
1122
1125
1123
use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
1126
1124
use bitcoin:: hashes:: Hash ;
@@ -2181,7 +2179,7 @@ mod tests {
2181
2179
2182
2180
#[ test]
2183
2181
fn handling_query_channel_range ( ) {
2184
- let ( secp_ctx, mut net_graph_msg_handler) = create_net_graph_msg_handler ( ) ;
2182
+ let ( secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler ( ) ;
2185
2183
2186
2184
let chain_hash = genesis_block ( Network :: Testnet ) . header . block_hash ( ) ;
2187
2185
let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
@@ -2193,17 +2191,20 @@ mod tests {
2193
2191
let bitcoin_key_1 = PublicKey :: from_secret_key ( & secp_ctx, node_1_btckey) ;
2194
2192
let bitcoin_key_2 = PublicKey :: from_secret_key ( & secp_ctx, node_2_btckey) ;
2195
2193
2196
- let scids: Vec < u64 > = vec ! [
2197
- 0x000000_000000_0000 , // 0x0x0
2198
- 0x000001_000000_0000 , // 1x0x0
2199
- 0x000002_000000_0000 , // 2x0x0
2200
- 0x000002_000001_0000 , // 2x1x0
2201
- 0x000100_000000_0000 , // 256x0x0
2202
- 0x000101_000000_0000 , // 257x0x0
2203
- 0xfffffe_ffffff_ffff , // max
2204
- 0xffffff_ffffff_ffff , // never
2194
+ let mut scids: Vec < u64 > = vec ! [
2195
+ scid_from_parts( 0xfffffe , 0xffffff , 0xffff ) . unwrap( ) , // max
2196
+ scid_from_parts( 0xffffff , 0xffffff , 0xffff ) . unwrap( ) , // never
2205
2197
] ;
2206
2198
2199
+ // used for testing multipart reply across blocks
2200
+ for block in 100000 ..=108001 {
2201
+ scids. push ( scid_from_parts ( block, 0 , 0 ) . unwrap ( ) ) ;
2202
+ }
2203
+
2204
+ // used for testing resumption on same block
2205
+ scids. push ( scid_from_parts ( 108001 , 1 , 0 ) . unwrap ( ) ) ;
2206
+ scids. push ( scid_from_parts ( 108001 , 2 , 0 ) . unwrap ( ) ) ;
2207
+
2207
2208
for scid in scids {
2208
2209
let unsigned_announcement = UnsignedChannelAnnouncement {
2209
2210
features : ChannelFeatures :: known ( ) ,
@@ -2231,7 +2232,7 @@ mod tests {
2231
2232
}
2232
2233
2233
2234
// Empty reply when number_of_blocks=0
2234
- test_handling_query_channel_range (
2235
+ do_handling_query_channel_range (
2235
2236
& net_graph_msg_handler,
2236
2237
& node_id_2,
2237
2238
QueryChannelRange {
@@ -2249,7 +2250,7 @@ mod tests {
2249
2250
) ;
2250
2251
2251
2252
// Empty when wrong chain
2252
- test_handling_query_channel_range (
2253
+ do_handling_query_channel_range (
2253
2254
& net_graph_msg_handler,
2254
2255
& node_id_2,
2255
2256
QueryChannelRange {
@@ -2267,26 +2268,26 @@ mod tests {
2267
2268
) ;
2268
2269
2269
2270
// Empty reply when first_blocknum > 0xffffff
2270
- test_handling_query_channel_range (
2271
+ do_handling_query_channel_range (
2271
2272
& net_graph_msg_handler,
2272
2273
& node_id_2,
2273
2274
QueryChannelRange {
2274
2275
chain_hash : chain_hash. clone ( ) ,
2275
2276
first_blocknum : 0x01000000 ,
2276
- number_of_blocks : 0xffffffff ,
2277
+ number_of_blocks : 0xffff_ffff ,
2277
2278
} ,
2278
2279
vec ! [ ReplyChannelRange {
2279
2280
chain_hash: chain_hash. clone( ) ,
2280
2281
first_blocknum: 0x01000000 ,
2281
- number_of_blocks: 0xffffffff ,
2282
+ number_of_blocks: 0xffff_ffff ,
2282
2283
sync_complete: true ,
2283
2284
short_channel_ids: vec![ ]
2284
2285
} ]
2285
2286
) ;
2286
2287
2287
2288
// Empty reply when max valid SCID block num.
2288
- // Unlike prior test this is a valid but no results are found
2289
- test_handling_query_channel_range (
2289
+ // Unlike prior test this is a valid query but no results are found
2290
+ do_handling_query_channel_range (
2290
2291
& net_graph_msg_handler,
2291
2292
& node_id_2,
2292
2293
QueryChannelRange {
@@ -2306,201 +2307,112 @@ mod tests {
2306
2307
) ;
2307
2308
2308
2309
// No results in valid query range
2309
- test_handling_query_channel_range (
2310
+ do_handling_query_channel_range (
2310
2311
& net_graph_msg_handler,
2311
2312
& node_id_2,
2312
2313
QueryChannelRange {
2313
2314
chain_hash : chain_hash. clone ( ) ,
2314
- first_blocknum : 0x00100000 ,
2315
+ first_blocknum : 0x00800000 ,
2315
2316
number_of_blocks : 1000 ,
2316
2317
} ,
2317
2318
vec ! [
2318
2319
ReplyChannelRange {
2319
2320
chain_hash: chain_hash. clone( ) ,
2320
- first_blocknum: 0x00100000 ,
2321
+ first_blocknum: 0x00800000 ,
2321
2322
number_of_blocks: 1000 ,
2322
2323
sync_complete: true ,
2323
2324
short_channel_ids: vec![ ] ,
2324
2325
}
2325
2326
]
2326
2327
) ;
2327
2328
2328
- // Single reply - all blocks
2329
- test_handling_query_channel_range (
2330
- & net_graph_msg_handler,
2331
- & node_id_2,
2332
- QueryChannelRange {
2333
- chain_hash : chain_hash. clone ( ) ,
2334
- first_blocknum : 0 ,
2335
- number_of_blocks : 0xffffffff ,
2336
- } ,
2337
- vec ! [
2338
- ReplyChannelRange {
2339
- chain_hash: chain_hash. clone( ) ,
2340
- first_blocknum: 0 ,
2341
- number_of_blocks: 0xffffffff ,
2342
- sync_complete: true ,
2343
- short_channel_ids: vec![
2344
- 0x000000_000000_0000 , // 0x0x0
2345
- 0x000001_000000_0000 , // 1x0x0
2346
- 0x000002_000000_0000 , // 2x0x0
2347
- 0x000002_000001_0000 , // 2x1x0
2348
- 0x000100_000000_0000 , // 256x0x0
2349
- 0x000101_000000_0000 , // 257x0x0
2350
- 0xfffffe_ffffff_ffff , // max
2351
- ]
2352
- }
2353
- ]
2354
- ) ;
2355
-
2356
- // Single reply - overflow of first_blocknum + number_of_blocks
2357
- test_handling_query_channel_range (
2329
+ // Overflow first_blocknum + number_of_blocks
2330
+ do_handling_query_channel_range (
2358
2331
& net_graph_msg_handler,
2359
2332
& node_id_2,
2360
2333
QueryChannelRange {
2361
2334
chain_hash : chain_hash. clone ( ) ,
2362
- first_blocknum : 1 ,
2335
+ first_blocknum : 0xfe0000 ,
2363
2336
number_of_blocks : 0xffffffff ,
2364
2337
} ,
2365
2338
vec ! [
2366
2339
ReplyChannelRange {
2367
2340
chain_hash: chain_hash. clone( ) ,
2368
- first_blocknum: 1 ,
2369
- number_of_blocks: 0xfffffffe ,
2341
+ first_blocknum: 0xfe0000 ,
2342
+ number_of_blocks: 0xffffffff - 0xfe0000 ,
2370
2343
sync_complete: true ,
2371
2344
short_channel_ids: vec![
2372
- 0x000001_000000_0000 , // 1x0x0
2373
- 0x000002_000000_0000 , // 2x0x0
2374
- 0x000002_000001_0000 , // 2x1x0
2375
- 0x000100_000000_0000 , // 256x0x0
2376
- 0x000101_000000_0000 , // 257x0x0
2377
2345
0xfffffe_ffffff_ffff , // max
2378
2346
]
2379
2347
}
2380
2348
]
2381
2349
) ;
2382
2350
2383
- // Single reply - query larger than found results
2384
- test_handling_query_channel_range (
2385
- & net_graph_msg_handler,
2386
- & node_id_2,
2387
- QueryChannelRange {
2388
- chain_hash : chain_hash. clone ( ) ,
2389
- first_blocknum : 100 ,
2390
- number_of_blocks : 1000 ,
2391
- } ,
2392
- vec ! [
2393
- ReplyChannelRange {
2394
- chain_hash: chain_hash. clone( ) ,
2395
- first_blocknum: 100 ,
2396
- number_of_blocks: 1000 ,
2397
- sync_complete: true ,
2398
- short_channel_ids: vec![
2399
- 0x000100_000000_0000 , // 256x0x0
2400
- 0x000101_000000_0000 , // 257x0x0
2401
- ]
2402
- }
2403
- ]
2404
- ) ;
2405
-
2406
- // Tests below here will chunk replies
2407
- net_graph_msg_handler. max_reply_scids = 1 ;
2408
-
2409
- // Multipart - new block per messages
2410
- test_handling_query_channel_range (
2411
- & net_graph_msg_handler,
2412
- & node_id_2,
2413
- QueryChannelRange {
2414
- chain_hash : chain_hash. clone ( ) ,
2415
- first_blocknum : 0 ,
2416
- number_of_blocks : 2 ,
2417
- } ,
2418
- vec ! [
2419
- ReplyChannelRange {
2420
- chain_hash: chain_hash. clone( ) ,
2421
- first_blocknum: 0 ,
2422
- number_of_blocks: 1 ,
2423
- sync_complete: false ,
2424
- short_channel_ids: vec![
2425
- 0x000000_000000_0000 , // 0x0x0
2426
- ]
2427
- } ,
2428
- ReplyChannelRange {
2429
- chain_hash: chain_hash. clone( ) ,
2430
- first_blocknum: 1 ,
2431
- number_of_blocks: 1 ,
2432
- sync_complete: true ,
2433
- short_channel_ids: vec![
2434
- 0x000001_000000_0000 , // 1x0x0
2435
- ]
2436
- } ,
2437
- ]
2438
- ) ;
2439
-
2440
- // Multiplart - resumption of same block
2441
- test_handling_query_channel_range (
2351
+ // Multiple split on new block
2352
+ do_handling_query_channel_range (
2442
2353
& net_graph_msg_handler,
2443
2354
& node_id_2,
2444
2355
QueryChannelRange {
2445
2356
chain_hash : chain_hash. clone ( ) ,
2446
- first_blocknum : 2 ,
2447
- number_of_blocks : 1 ,
2357
+ first_blocknum : 100000 ,
2358
+ number_of_blocks : 8001 ,
2448
2359
} ,
2449
2360
vec ! [
2450
2361
ReplyChannelRange {
2451
2362
chain_hash: chain_hash. clone( ) ,
2452
- first_blocknum: 2 ,
2453
- number_of_blocks: 1 ,
2363
+ first_blocknum: 100000 ,
2364
+ number_of_blocks: 8000 ,
2454
2365
sync_complete: false ,
2455
- short_channel_ids: vec! [
2456
- 0x000002_000000_0000 , // 2x0x0
2457
- ]
2366
+ short_channel_ids: ( 100000 ..= 107999 )
2367
+ . map ( |block| scid_from_parts ( block , 0 , 0 ) . unwrap ( ) )
2368
+ . collect ( ) ,
2458
2369
} ,
2459
2370
ReplyChannelRange {
2460
2371
chain_hash: chain_hash. clone( ) ,
2461
- first_blocknum: 2 ,
2372
+ first_blocknum: 108000 ,
2462
2373
number_of_blocks: 1 ,
2463
2374
sync_complete: true ,
2464
2375
short_channel_ids: vec![
2465
- 0x000002_000001_0000 , // 2x1x0
2466
- ]
2376
+ scid_from_parts ( 108000 , 0 , 0 ) . unwrap ( ) ,
2377
+ ] ,
2467
2378
}
2468
2379
]
2469
2380
) ;
2470
2381
2471
- // Multipart - query larger than found results, similar to single reply
2472
- test_handling_query_channel_range (
2382
+ // Multiple split on same block
2383
+ do_handling_query_channel_range (
2473
2384
& net_graph_msg_handler,
2474
2385
& node_id_2,
2475
2386
QueryChannelRange {
2476
2387
chain_hash : chain_hash. clone ( ) ,
2477
- first_blocknum : 100 ,
2478
- number_of_blocks : 1000 ,
2388
+ first_blocknum : 100002 ,
2389
+ number_of_blocks : 8000 ,
2479
2390
} ,
2480
2391
vec ! [
2481
2392
ReplyChannelRange {
2482
2393
chain_hash: chain_hash. clone( ) ,
2483
- first_blocknum: 100 , // <= query first_blocknum
2484
- number_of_blocks: 157 ,
2394
+ first_blocknum: 100002 ,
2395
+ number_of_blocks: 8000 ,
2485
2396
sync_complete: false ,
2486
- short_channel_ids: vec! [
2487
- 0x000100_000000_0000 , // 256x0x0
2488
- ]
2397
+ short_channel_ids: ( 100002 ..= 108001 )
2398
+ . map ( |block| scid_from_parts ( block , 0 , 0 ) . unwrap ( ) )
2399
+ . collect ( ) ,
2489
2400
} ,
2490
2401
ReplyChannelRange {
2491
2402
chain_hash: chain_hash. clone( ) ,
2492
- first_blocknum: 257 ,
2493
- number_of_blocks: 843 , // >= query first_blocknum+number_of_blocks
2403
+ first_blocknum: 108001 ,
2404
+ number_of_blocks: 1 ,
2494
2405
sync_complete: true ,
2495
2406
short_channel_ids: vec![
2496
- 0x000101_000000_0000 , // 257x0x0
2497
- ]
2407
+ scid_from_parts( 108001 , 1 , 0 ) . unwrap( ) ,
2408
+ scid_from_parts( 108001 , 2 , 0 ) . unwrap( ) ,
2409
+ ] ,
2498
2410
}
2499
2411
]
2500
2412
) ;
2501
2413
}
2502
2414
2503
- fn test_handling_query_channel_range (
2415
+ fn do_handling_query_channel_range (
2504
2416
net_graph_msg_handler : & NetGraphMsgHandler < Arc < test_utils:: TestChainSource > , Arc < test_utils:: TestLogger > > ,
2505
2417
test_node_id : & PublicKey ,
2506
2418
msg : QueryChannelRange ,
0 commit comments