Skip to content

Commit 676b135

Browse files
committed
Use the query start block for ReplyChannelRange response messages
C-Lightning versions prior to 0.10 (incorrectly) enforce that the reply_channel_range first_blocknum field is set to at least the value they sent in their query_channel_range message. Sending a 0 results in them responding with an Error message, closing open channels spuriously. This code is extracted from a previous version of the original query_channel_range PR in commit 44ba52c. The original commit is by `bmancini55 <[email protected]>`.
1 parent 4d1c1a3 commit 676b135

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,17 +375,20 @@ impl<C: Deref , L: Deref > RoutingMessageHandler for NetGraphMsgHandler<C, L> wh
375375
let batch_count = batches.len();
376376
for (batch_index, batch) in batches.into_iter().enumerate() {
377377
// 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;
378+
// must be >= the prior reply. We'll simplify this by using the query's first_blocknum since its
379+
// still spec compliant and sequence completion is now explicit.
380+
let first_blocknum = msg.first_blocknum;
381381

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.
382+
// Per spec, the last end_block needs to be >= the query's end_blocknum. Last
383+
// reply calculates difference between the query's end_blocknum and the start of the reply.
384+
// Overflow safe since end_blocknum must be >= msg.first_block_num.
385385
let number_of_blocks = if batch_index == batch_count-1 {
386-
msg.end_blocknum()
387-
} else {
388-
block_from_scid(batch.last().unwrap()) + 1
386+
msg.end_blocknum() - first_blocknum
387+
}
388+
// Prior replies should use the number of blocks that fit into the reply. Overflow
389+
// safe since first_blocknum is always <= last SCID's block.
390+
else {
391+
block_from_scid(batch.last().unwrap()) - first_blocknum + 1
389392
};
390393

391394
// Only true for the last message in a sequence

0 commit comments

Comments
 (0)