Skip to content

Commit f4adb9f

Browse files
committed
Use constant MAX_REPLY_SCID
Modifies NetGraphMsgHandler::handle_query_channel_range to use a constant max value in replies. Modifies tests to generate 8000 channels instead of making this value configurable.
1 parent 1809ef1 commit f4adb9f

File tree

1 file changed

+62
-150
lines changed

1 file changed

+62
-150
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 62 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ use bitcoin::hashes::hex::ToHex;
4545
/// refuse to relay the message.
4646
const MAX_EXCESS_BYTES_FOR_RELAY: usize = 1024;
4747

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+
4852
/// Represents the network as nodes and channels between them
4953
#[derive(Clone, PartialEq)]
5054
pub struct NetworkGraph {
@@ -71,11 +75,6 @@ pub struct NetGraphMsgHandler<C: Deref, L: Deref> where C::Target: chain::Access
7175
full_syncs_requested: AtomicUsize,
7276
pending_events: Mutex<Vec<MessageSendEvent>>,
7377
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,
7978
}
8079

8180
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
9291
chain_access,
9392
pending_events: Mutex::new(vec![]),
9493
logger,
95-
max_reply_scids: 8000,
9694
}
9795
}
9896

@@ -106,7 +104,6 @@ impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access
106104
chain_access,
107105
pending_events: Mutex::new(vec![]),
108106
logger,
109-
max_reply_scids: 8000,
110107
}
111108
}
112109

@@ -354,12 +351,12 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
354351
// Creates channel batches. We are not checking if the channel is routable
355352
// (has at least one update). A peer may still want to know the channel
356353
// 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)];
358355
for (_, ref chan) in network_graph.get_channels().range(start_scid.unwrap()..exclusive_end_scid.unwrap()) {
359356
if let Some(chan_announcement) = &chan.announcement_message {
360357
// Construct a new batch if last one is full
361358
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));
363360
}
364361

365362
let batch = batches.last_mut().unwrap();
@@ -1121,6 +1118,7 @@ mod tests {
11211118
use util::logger::Logger;
11221119
use util::ser::{Readable, Writeable};
11231120
use util::events::{MessageSendEvent, MessageSendEventsProvider};
1121+
use util::scid_utils::scid_from_parts;
11241122

11251123
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
11261124
use bitcoin::hashes::Hash;
@@ -2181,7 +2179,7 @@ mod tests {
21812179

21822180
#[test]
21832181
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();
21852183

21862184
let chain_hash = genesis_block(Network::Testnet).header.block_hash();
21872185
let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
@@ -2193,17 +2191,20 @@ mod tests {
21932191
let bitcoin_key_1 = PublicKey::from_secret_key(&secp_ctx, node_1_btckey);
21942192
let bitcoin_key_2 = PublicKey::from_secret_key(&secp_ctx, node_2_btckey);
21952193

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
22052197
];
22062198

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+
22072208
for scid in scids {
22082209
let unsigned_announcement = UnsignedChannelAnnouncement {
22092210
features: ChannelFeatures::known(),
@@ -2231,7 +2232,7 @@ mod tests {
22312232
}
22322233

22332234
// Empty reply when number_of_blocks=0
2234-
test_handling_query_channel_range(
2235+
do_handling_query_channel_range(
22352236
&net_graph_msg_handler,
22362237
&node_id_2,
22372238
QueryChannelRange {
@@ -2249,7 +2250,7 @@ mod tests {
22492250
);
22502251

22512252
// Empty when wrong chain
2252-
test_handling_query_channel_range(
2253+
do_handling_query_channel_range(
22532254
&net_graph_msg_handler,
22542255
&node_id_2,
22552256
QueryChannelRange {
@@ -2267,26 +2268,26 @@ mod tests {
22672268
);
22682269

22692270
// Empty reply when first_blocknum > 0xffffff
2270-
test_handling_query_channel_range(
2271+
do_handling_query_channel_range(
22712272
&net_graph_msg_handler,
22722273
&node_id_2,
22732274
QueryChannelRange {
22742275
chain_hash: chain_hash.clone(),
22752276
first_blocknum: 0x01000000,
2276-
number_of_blocks: 0xffffffff,
2277+
number_of_blocks: 0xffff_ffff,
22772278
},
22782279
vec![ReplyChannelRange {
22792280
chain_hash: chain_hash.clone(),
22802281
first_blocknum: 0x01000000,
2281-
number_of_blocks: 0xffffffff,
2282+
number_of_blocks: 0xffff_ffff,
22822283
sync_complete: true,
22832284
short_channel_ids: vec![]
22842285
}]
22852286
);
22862287

22872288
// 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(
22902291
&net_graph_msg_handler,
22912292
&node_id_2,
22922293
QueryChannelRange {
@@ -2306,201 +2307,112 @@ mod tests {
23062307
);
23072308

23082309
// No results in valid query range
2309-
test_handling_query_channel_range(
2310+
do_handling_query_channel_range(
23102311
&net_graph_msg_handler,
23112312
&node_id_2,
23122313
QueryChannelRange {
23132314
chain_hash: chain_hash.clone(),
2314-
first_blocknum: 0x00100000,
2315+
first_blocknum: 0x00800000,
23152316
number_of_blocks: 1000,
23162317
},
23172318
vec![
23182319
ReplyChannelRange {
23192320
chain_hash: chain_hash.clone(),
2320-
first_blocknum: 0x00100000,
2321+
first_blocknum: 0x00800000,
23212322
number_of_blocks: 1000,
23222323
sync_complete: true,
23232324
short_channel_ids: vec![],
23242325
}
23252326
]
23262327
);
23272328

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(
23582331
&net_graph_msg_handler,
23592332
&node_id_2,
23602333
QueryChannelRange {
23612334
chain_hash: chain_hash.clone(),
2362-
first_blocknum: 1,
2335+
first_blocknum: 0xfe0000,
23632336
number_of_blocks: 0xffffffff,
23642337
},
23652338
vec![
23662339
ReplyChannelRange {
23672340
chain_hash: chain_hash.clone(),
2368-
first_blocknum: 1,
2369-
number_of_blocks: 0xfffffffe,
2341+
first_blocknum: 0xfe0000,
2342+
number_of_blocks: 0xffffffff - 0xfe0000,
23702343
sync_complete: true,
23712344
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
23772345
0xfffffe_ffffff_ffff, // max
23782346
]
23792347
}
23802348
]
23812349
);
23822350

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(
24422353
&net_graph_msg_handler,
24432354
&node_id_2,
24442355
QueryChannelRange {
24452356
chain_hash: chain_hash.clone(),
2446-
first_blocknum: 2,
2447-
number_of_blocks: 1,
2357+
first_blocknum: 100000,
2358+
number_of_blocks: 8001,
24482359
},
24492360
vec![
24502361
ReplyChannelRange {
24512362
chain_hash: chain_hash.clone(),
2452-
first_blocknum: 2,
2453-
number_of_blocks: 1,
2363+
first_blocknum: 100000,
2364+
number_of_blocks: 8000,
24542365
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(),
24582369
},
24592370
ReplyChannelRange {
24602371
chain_hash: chain_hash.clone(),
2461-
first_blocknum: 2,
2372+
first_blocknum: 108000,
24622373
number_of_blocks: 1,
24632374
sync_complete: true,
24642375
short_channel_ids: vec![
2465-
0x000002_000001_0000, // 2x1x0
2466-
]
2376+
scid_from_parts(108000, 0, 0).unwrap(),
2377+
],
24672378
}
24682379
]
24692380
);
24702381

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(
24732384
&net_graph_msg_handler,
24742385
&node_id_2,
24752386
QueryChannelRange {
24762387
chain_hash: chain_hash.clone(),
2477-
first_blocknum: 100,
2478-
number_of_blocks: 1000,
2388+
first_blocknum: 100002,
2389+
number_of_blocks: 8000,
24792390
},
24802391
vec![
24812392
ReplyChannelRange {
24822393
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,
24852396
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(),
24892400
},
24902401
ReplyChannelRange {
24912402
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,
24942405
sync_complete: true,
24952406
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+
],
24982410
}
24992411
]
25002412
);
25012413
}
25022414

2503-
fn test_handling_query_channel_range(
2415+
fn do_handling_query_channel_range(
25042416
net_graph_msg_handler: &NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
25052417
test_node_id: &PublicKey,
25062418
msg: QueryChannelRange,

0 commit comments

Comments
 (0)