Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ trait OmmersValidator {
)(implicit blockchainConfig: BlockchainConfig): Either[OmmersError, OmmersValid] = {

val getBlockHeaderByHash: ByteString => Option[BlockHeader] = blockchainReader.getBlockHeaderByHash
val bestBranch = blockchainReader.getBestBranch()
val getNBlocksBack: (ByteString, Int) => List[Block] =
(_, n) =>
((blockNumber - n) until blockNumber).toList
.flatMap(nb => blockchainReader.getBlockByNumber(bestBranch, nb))
(tailBlockHash, n) =>
Iterator
.iterate(blockchainReader.getBlockByHash(tailBlockHash))(
_.filter(_.number > 0) // avoid trying to fetch parent of genesis
.flatMap(block => blockchainReader.getBlockByHash(block.header.parentHash))
)
.collect { case Some(block) => block }
.take(n)
.toList
.reverse

validate(parentHash, blockNumber, ommers, getBlockHeaderByHash, getNBlocksBack)
}
Expand Down
17 changes: 10 additions & 7 deletions src/main/scala/io/iohk/ethereum/ledger/BlockValidation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ class BlockValidation(
// The in memory blocks aren't connected to the db ones, we don't have n blocks to return so we return none
Nil

case Some(block) =>
case Some(highestBlockInStorage) =>
// We already have |block +: queuedBlocks|
val remaining = n - queuedBlocks.length - 1

val numbers = (block.header.number - remaining) until block.header.number
val bestBranch = blockchainReader.getBestBranch()
val blocks =
(numbers.toList.flatMap(nb => blockchainReader.getBlockByNumber(bestBranch, nb)) :+ block) ::: queuedBlocks
blocks
val remainingBlocks = Iterator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same remarks as in OmmersValidation

.iterate(blockchainReader.getBlockByHash(highestBlockInStorage.header.parentHash))(
_.filter(_.number > 0) // avoid trying to fetch parent of genesis
.flatMap(p => blockchainReader.getBlockByHash(p.header.parentHash))
)
.take(remaining)
.collect { case Some(block) => block }
.toList
(remainingBlocks :+ highestBlockInStorage) ::: queuedBlocks
}
}
}
Expand Down