- // Per spec, the initial first_blocknum needs to be <= the query's first_blocknum and subsequent
- // must be >= the prior reply. We'll simplify this by using zero since its still spec compliant and
- // sequence completion is now explicitly.
- let first_blocknum = 0;
-
- // Per spec, the final end_blocknum needs to be >= the query's end_blocknum, so we'll use the
- // query's value. Prior batches must use the number of blocks that fit into the message. We'll
- // base this off the last SCID in the batch since we've somewhat abusing first_blocknum.
- let number_of_blocks = if batch_index == batch_count-1 {
- msg.end_blocknum()
- } else {
- block_from_scid(batch.last().unwrap()) + 1
+ // Per spec, the initial `first_blocknum` needs to be <= the query's `first_blocknum`
+ // and subsequent `first_blocknum`s must be >= the prior reply's `first_blocknum`.
+ //
+ // Additionally, c-lightning versions < 0.10 require that the `first_blocknum` of each
+ // reply is >= the previous reply's `first_blocknum` and either exactly the previous
+ // reply's `first_blocknum + number_of_blocks` or exactly one greater. This is a
+ // significant diversion from the requirements set by the spec, and, in case of blocks
+ // with no channel opens (e.g. empty blocks), requires that we use the previous value
+ // and *not* derive the first_blocknum from the actual first block of the reply.
+ let first_blocknum = prev_batch_endblock;
+
+ // Each message carries the number of blocks (from the `first_blocknum`) its contents
+ // fit in. Though there is no requirement that we use exactly the number of blocks its
+ // contents are from, except for the bogus requirements c-lightning enforces, above.
+ //
+ // Per spec, the last end block (ie `first_blocknum + number_of_blocks`) needs to be
+ // >= the query's end block. Thus, for the last reply, we calculate the difference
+ // between the query's end block and the start of the reply.
+ //
+ // Overflow safe since end_blocknum=msg.first_block_num+msg.number_of_blocks and
+ // first_blocknum will be either msg.first_blocknum or a higher block height.
+ let (sync_complete, number_of_blocks) = if batch_index == batch_count-1 {
+ (true, msg.end_blocknum() - first_blocknum)
+ }
+ // Prior replies should use the number of blocks that fit into the reply. Overflow
+ // safe since first_blocknum is always <= last SCID's block.
+ else {
+ (false, block_from_scid(batch.last().unwrap()) - first_blocknum)