Skip to content

Commit 2c3352f

Browse files
author
Nicolas Tallar
committed
[ETCM-186] Fix strict pick
1 parent ba42652 commit 2c3352f

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

src/main/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcher.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ class BlockFetcher(
6868
private def handleCommands(state: BlockFetcherState): Receive = {
6969
case PickBlocks(amount) => state.pickBlocks(amount) |> handlePickedBlocks(state) |> fetchBlocks
7070
case StrictPickBlocks(from, atLeastWith) =>
71-
val minBlock = from.min(atLeastWith).max(1)
72-
log.debug("Strict Pick blocks from {} to {}", from, atLeastWith)
71+
val fromCapped = from.max(1)
72+
val minBlock = fromCapped.min(atLeastWith).max(1)
73+
log.debug("Strict Pick blocks from {} to {}", fromCapped, atLeastWith)
7374
log.debug("Lowest available block is {}", state.lowestBlock)
7475

7576
val newState = if (minBlock < state.lowestBlock) {
7677
state.invalidateBlocksFrom(minBlock, None)._2
7778
} else {
78-
state.strictPickBlocks(from, atLeastWith) |> handlePickedBlocks(state)
79+
state.strictPickBlocks(fromCapped, atLeastWith) |> handlePickedBlocks(state)
7980
}
8081

8182
fetchBlocks(newState)

src/main/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherState.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,15 @@ case class BlockFetcherState(
107107
None
108108
}
109109

110+
/**
111+
* Returns all the ready blocks but only if 2 block number bounds are included:
112+
* - lower = min(from, atLeastWith)
113+
* - upper = max(from, atLeastWith)
114+
*/
110115
def strictPickBlocks(from: BigInt, atLeastWith: BigInt): Option[(NonEmptyList[Block], BlockFetcherState)] = {
111116
val lower = from.min(atLeastWith)
112117
val upper = from.max(atLeastWith)
118+
113119
readyBlocks.some
114120
.filter(_.headOption.exists(block => block.number <= lower))
115121
.filter(_.lastOption.exists(block => block.number >= upper))

src/main/scala/io/iohk/ethereum/blockchain/sync/regular/BlockImporter.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ class BlockImporter(
105105

106106
private def importBlocks(blocks: NonEmptyList[Block]): ImportFn =
107107
importWith {
108-
log.debug("Attempting to import blocks starting from {}", blocks.head.number)
108+
log.debug(
109+
"Attempting to import blocks starting from {} and ending with {}",
110+
blocks.head.number,
111+
blocks.last.number
112+
)
109113
Future
110114
.successful(resolveBranch(blocks))
111115
.flatMap {

src/test/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherStateSpec.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@ class BlockFetcherStateSpec extends TestKit(ActorSystem()) with AnyWordSpecLike
3333
newState.knownTop shouldEqual newBestBlock.number
3434
}
3535
}
36-
3736
}
3837
}

0 commit comments

Comments
 (0)