Skip to content

Commit

Permalink
[ETCM-186] Fix strict pick
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Tallar committed Oct 5, 2020
1 parent ba42652 commit 3c6e02f
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import io.iohk.ethereum.network.{Peer, PeerId}
import io.iohk.ethereum.network.p2p.messages.PV62.BlockHash
import BlockFetcherState._
import cats.syntax.either._
import cats.syntax.option._

import scala.collection.immutable.Queue

Expand Down Expand Up @@ -110,11 +109,14 @@ case class BlockFetcherState(
def strictPickBlocks(from: BigInt, atLeastWith: BigInt): Option[(NonEmptyList[Block], BlockFetcherState)] = {
val lower = from.min(atLeastWith)
val upper = from.max(atLeastWith)
readyBlocks.some
.filter(_.headOption.exists(block => block.number <= lower))
.filter(_.lastOption.exists(block => block.number >= upper))
.filter(_.nonEmpty)
.map(blocks => (NonEmptyList(blocks.head, blocks.tail.toList), copy(readyBlocks = Queue())))

val readyBlocksAfterLower = readyBlocks.dropWhile( _.number < lower)

val amount: Int = (upper - lower + 1).toInt
val (strickedPickedBlock, readyBlocksAfterUpper) = readyBlocksAfterLower.splitAt(amount)
NonEmptyList.fromList(strickedPickedBlock.toList).map { nelStrickedPickedBlock =>
(nelStrickedPickedBlock, copy(readyBlocks = readyBlocksAfterUpper))
}
}

def invalidateBlocksFrom(nr: BigInt): (Option[PeerId], BlockFetcherState) = invalidateBlocksFrom(nr, Some(nr))
Expand Down

0 comments on commit 3c6e02f

Please sign in to comment.