Skip to content

Commit 7b842b6

Browse files
committed
Simplify sequencing of handle_query_channel_range
Modify NetGraphMsgHandler::handle_query_channel_range to always use first_blocknum=0 in replies. This is spec compliant after changes to make sequence completion explicity using sync_complete.
1 parent 7815965 commit 7b842b6

File tree

1 file changed

+28
-40
lines changed

1 file changed

+28
-40
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -374,28 +374,18 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
374374
let mut pending_events = self.pending_events.lock().unwrap();
375375
let batch_count = batches.len();
376376
for (batch_index, batch) in batches.into_iter().enumerate() {
377-
// Per spec, the initial first_blocknum needs to be <= the query's first_blocknum.
378-
// Use the query's values since we don't use pre-processed reply ranges.
379-
let first_blocknum = if batch_index == 0 {
380-
msg.first_blocknum
381-
}
382-
// Subsequent replies must be >= the last sent first_blocknum. Use the first block
383-
// in the new batch. Batches beyond the first one cannot be empty.
384-
else {
385-
block_from_scid(batch.first().unwrap())
386-
};
387-
388-
// Per spec, the last end_blocknum needs to be >= the query's end_blocknum. Last
389-
// reply calculates difference between the query's end_blocknum and the start of the reply.
390-
// Overflow safe since end_blocknum=msg.first_block_num+msg.number_of_blocks and first_blocknum
391-
// will be either msg.first_blocknum or a higher block height.
377+
// Per spec, the initial first_blocknum needs to be <= the query's first_blocknum and subsequent
378+
// must be >= the prior reply. We'll simplify this by using zero since its still spec compliant and
379+
// sequence completion is now explicitly.
380+
let first_blocknum = 0;
381+
382+
// Per spec, the final end_blocknum needs to be >= the query's end_blocknum, so we'll use the
383+
// query's value. Prior batches must use the number of blocks that fit into the message. We'll
384+
// base this off the last SCID in the batch since we've somewhat abusing first_blocknum.
392385
let number_of_blocks = if batch_index == batch_count-1 {
393-
msg.end_blocknum() - first_blocknum
394-
}
395-
// Prior replies should use the number of blocks that fit into the reply. Overflow
396-
// safe since first_blocknum is always <= last SCID's block.
397-
else {
398-
block_from_scid(batch.last().unwrap()) - first_blocknum + 1
386+
msg.end_blocknum()
387+
} else {
388+
block_from_scid(batch.last().unwrap()) + 1
399389
};
400390

401391
// Only true for the last message in a sequence
@@ -2209,7 +2199,6 @@ mod tests {
22092199

22102200
// used for testing resumption on same block
22112201
scids.push(scid_from_parts(108001, 1, 0).unwrap());
2212-
scids.push(scid_from_parts(108001, 2, 0).unwrap());
22132202

22142203
for scid in scids {
22152204
let unsigned_announcement = UnsignedChannelAnnouncement {
@@ -2307,8 +2296,8 @@ mod tests {
23072296
vec![
23082297
ReplyChannelRange {
23092298
chain_hash: chain_hash.clone(),
2310-
first_blocknum: 0xffffff,
2311-
number_of_blocks: 1,
2299+
first_blocknum: 0,
2300+
number_of_blocks: 0x01000000,
23122301
sync_complete: true,
23132302
short_channel_ids: vec![]
23142303
},
@@ -2321,15 +2310,15 @@ mod tests {
23212310
&node_id_2,
23222311
QueryChannelRange {
23232312
chain_hash: chain_hash.clone(),
2324-
first_blocknum: 0x00800000,
2313+
first_blocknum: 1000,
23252314
number_of_blocks: 1000,
23262315
},
23272316
true,
23282317
vec![
23292318
ReplyChannelRange {
23302319
chain_hash: chain_hash.clone(),
2331-
first_blocknum: 0x00800000,
2332-
number_of_blocks: 1000,
2320+
first_blocknum: 0,
2321+
number_of_blocks: 2000,
23332322
sync_complete: true,
23342323
short_channel_ids: vec![],
23352324
}
@@ -2349,8 +2338,8 @@ mod tests {
23492338
vec![
23502339
ReplyChannelRange {
23512340
chain_hash: chain_hash.clone(),
2352-
first_blocknum: 0xfe0000,
2353-
number_of_blocks: 0xffffffff - 0xfe0000,
2341+
first_blocknum: 0,
2342+
number_of_blocks: 0xffffffff,
23542343
sync_complete: true,
23552344
short_channel_ids: vec![
23562345
0xfffffe_ffffff_ffff, // max
@@ -2372,8 +2361,8 @@ mod tests {
23722361
vec![
23732362
ReplyChannelRange {
23742363
chain_hash: chain_hash.clone(),
2375-
first_blocknum: 100000,
2376-
number_of_blocks: 8000,
2364+
first_blocknum: 0,
2365+
number_of_blocks: 108000,
23772366
sync_complete: true,
23782367
short_channel_ids: (100000..=107999)
23792368
.map(|block| scid_from_parts(block, 0, 0).unwrap())
@@ -2395,17 +2384,17 @@ mod tests {
23952384
vec![
23962385
ReplyChannelRange {
23972386
chain_hash: chain_hash.clone(),
2398-
first_blocknum: 100000,
2399-
number_of_blocks: 8000,
2387+
first_blocknum: 0,
2388+
number_of_blocks: 108000,
24002389
sync_complete: false,
24012390
short_channel_ids: (100000..=107999)
24022391
.map(|block| scid_from_parts(block, 0, 0).unwrap())
24032392
.collect(),
24042393
},
24052394
ReplyChannelRange {
24062395
chain_hash: chain_hash.clone(),
2407-
first_blocknum: 108000,
2408-
number_of_blocks: 1,
2396+
first_blocknum: 0,
2397+
number_of_blocks: 108001,
24092398
sync_complete: true,
24102399
short_channel_ids: vec![
24112400
scid_from_parts(108000, 0, 0).unwrap(),
@@ -2427,21 +2416,20 @@ mod tests {
24272416
vec![
24282417
ReplyChannelRange {
24292418
chain_hash: chain_hash.clone(),
2430-
first_blocknum: 100002,
2431-
number_of_blocks: 8000,
2419+
first_blocknum: 0,
2420+
number_of_blocks: 108002,
24322421
sync_complete: false,
24332422
short_channel_ids: (100002..=108001)
24342423
.map(|block| scid_from_parts(block, 0, 0).unwrap())
24352424
.collect(),
24362425
},
24372426
ReplyChannelRange {
24382427
chain_hash: chain_hash.clone(),
2439-
first_blocknum: 108001,
2440-
number_of_blocks: 1,
2428+
first_blocknum: 0,
2429+
number_of_blocks: 108002,
24412430
sync_complete: true,
24422431
short_channel_ids: vec![
24432432
scid_from_parts(108001, 1, 0).unwrap(),
2444-
scid_from_parts(108001, 2, 0).unwrap(),
24452433
],
24462434
}
24472435
]

0 commit comments

Comments
 (0)