Handle the case where our block headers are synced already (#2021)

This commit is contained in:
Chris Stewart 2020-09-16 11:45:57 -05:00 committed by GitHub
parent c9e5aa9c3b
commit 841230e35c
2 changed files with 30 additions and 3 deletions

View file

@ -392,6 +392,20 @@ class ChainHandlerTest extends ChainDbUnitTest {
}
it must "return None for ChainHandler.nextBlockHeaderBatchRange if we are synced" in {
chainHandler: ChainHandler =>
val genesisHeader =
chainHandler.chainConfig.chain.genesisBlock.blockHeader
val assert1F = for {
rangeOpt <-
chainHandler.nextBlockHeaderBatchRange(genesisHeader.hashBE, 1)
count <- chainHandler.getBlockCount()
} yield {
assert(rangeOpt.isEmpty)
}
assert1F
}
it must "generate a range for a block filter header query" in {
chainHandler: ChainHandler =>
for {

View file

@ -199,9 +199,22 @@ case class ChainHandler(
for {
chains <- chainsF
} yield getBestChainAtHeight(startHeight = startHeight,
} yield {
val nextBlockHeaderOpt = getBestChainAtHeight(startHeight = startHeight,
batchSize = batchSize,
blockchains = chains)
(nextBlockHeaderOpt, prevBlockHeaderOpt) match {
case (Some(next), Some(prev)) =>
//this means we are synced, so return None
if (next.stopBlockHash == prev.hash) {
None
} else {
nextBlockHeaderOpt
}
case (Some(_), None) | (None, Some(_)) | (None, None) =>
nextBlockHeaderOpt
}
}
}
/** Given a vector of blockchains, this method finds the chain with the most chain work