- for batch in batches.into_iter() {
- // Per spec, the initial first_blocknum needs to be <= the query's first_blocknum.
- // Use the query's values since we don't use pre-processed reply ranges.
- let first_blocknum = if batch_index == 0 {
- msg.first_blocknum
- }
- // Subsequent replies must be >= the last sent first_blocknum. Use the first block
- // in the new batch.
- else {
- block_from_scid(batch.first().unwrap())
- };
-
- // Per spec, the last end_block needs to be >= the query's end_block. Last
- // reply calculates difference between the query's end_blocknum 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 number_of_blocks = if batch_index == batch_count-1 {
- msg.end_blocknum() - first_blocknum
+ let mut prev_batch_endblock = msg.first_blocknum;
+ for (batch_index, batch) in batches.into_iter().enumerate() {
+ // 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)