Skip to content

Commit f533ddf

Browse files
author
Aurélien Richez
committed
[ETCM-1060] use getBlockByHash in getNBlocksBackFromChainOrQueue
1 parent 2cfbe1d commit f533ddf

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/main/scala/io/iohk/ethereum/ledger/BlockValidation.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ class BlockValidation(
3939
// The in memory blocks aren't connected to the db ones, we don't have n blocks to return so we return none
4040
Nil
4141

42-
case Some(block) =>
42+
case Some(highestBlockInStorage) =>
4343
// We already have |block +: queuedBlocks|
4444
val remaining = n - queuedBlocks.length - 1
45-
46-
val numbers = (block.header.number - remaining) until block.header.number
47-
val bestBranch = blockchainReader.getBestBranch()
48-
val blocks =
49-
(numbers.toList.flatMap(nb => bestBranch.getBlockByNumber(nb)) :+ block) ::: queuedBlocks
50-
blocks
45+
val remainingBlocks = Iterator
46+
.iterate(blockchainReader.getBlockByHash(highestBlockInStorage.header.parentHash))(
47+
_.flatMap(p => blockchainReader.getBlockByHash(p.header.parentHash))
48+
)
49+
.take(remaining)
50+
.collect { case Some(block) => block }
51+
.toList
52+
(remainingBlocks :+ highestBlockInStorage) ::: queuedBlocks
5153
}
5254
}
5355
}

0 commit comments

Comments
 (0)