fix: Include the current tip in the block locator

The state service was providing block locators starting at the parent of
the current tip. Instead, include the current tip in the block locator.

Also handle an edge case where we could include the genesis block twice,
if the current tip height was a power of two.

Fixes an instance of #818 where we re-download the current tip.
This commit is contained in:
teor 2020-08-04 17:50:19 +10:00
parent 7afd76f5fb
commit 1d3dd35175
1 changed files with 5 additions and 2 deletions

View File

@ -142,9 +142,12 @@ pub enum Response {
/// Get the heights of the blocks for constructing a block_locator list
fn block_locator_heights(tip_height: BlockHeight) -> impl Iterator<Item = BlockHeight> {
iter::successors(Some(1u32), |h| h.checked_mul(2))
let locators = iter::successors(Some(1u32), |h| h.checked_mul(2))
.flat_map(move |step| tip_height.0.checked_sub(step))
.map(BlockHeight)
.filter(|&height| height != 0)
.map(BlockHeight);
iter::once(tip_height)
.chain(locators)
.chain(iter::once(BlockHeight(0)))
}