Skip to content

Commit

Permalink
cache latest checkpoint block
Browse files Browse the repository at this point in the history
  • Loading branch information
bsuieric committed Apr 15, 2021
1 parent 52a626a commit 8894e49
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/main/scala/io/iohk/ethereum/domain/Blockchain.scala
Expand Up @@ -266,6 +266,9 @@ class BlockchainImpl(
override def getLatestCheckpointBlockNumber(): BigInt =
getBlockByHash(getLatestCheckpointBlockHash()).get.number

def getLatestCheckpointBlock(): Option[Block] =
getBlockByHash(getLatestCheckpointBlockHash())

override def getBestBlock(): Option[Block] = {
val bestBlock = getBlockByHash(getBestBlockHash())

Expand All @@ -278,7 +281,8 @@ class BlockchainImpl(
// appStateStorage.getBestBlockHash().getOrElse(genesisBlock.header.hash)

override def getLatestCheckpointBlockHash(): ByteString =
appStateStorage.getLatestCheckpointBlockHash().getOrElse(genesisBlock.header.hash)
bestKnownBlockAndLatestCheckpoint.get().latestCheckpointHash
// appStateStorage.getLatestCheckpointBlockHash().getOrElse(genesisBlock.header.hash)

override def getAccount(address: Address, blockHash: ByteString): Option[Account] =
getAccountMpt(blockHash) >>= (_.get(address))
Expand Down Expand Up @@ -345,6 +349,9 @@ class BlockchainImpl(
.and(storeChainWeight(block.header.hash, weight))
.commit()

if (block.hasCheckpoint) {
bestKnownBlockAndLatestCheckpoint.updateAndGet(_.copy(latestCheckpointHash = block.hash))
}
if (block.number != 0) {
if (
getChainWeightByHash(block.header.hash)
Expand Down
42 changes: 33 additions & 9 deletions src/main/scala/io/iohk/ethereum/ledger/BlockImport.scala
Expand Up @@ -40,15 +40,39 @@ class BlockImport(
}
}

private def importBlockToTop(block: Block, currentWeight: ChainWeight): BlockImportResult = {
val execResult: (List[BlockData], Option[BlockExecutionError]) =
blockExecution.executeAndValidateBlocks(List(block), currentWeight)
execResult._2 match {
case None =>
BlockImportedToTop(execResult._1)

case Some(error) =>
BlockImportFailed(error.toString)
private def importBlockToTop(block: Block, bestBlockNumber: BigInt, currentWeight: ChainWeight): BlockImportResult = {
val executionResult = for {
topBlock <- blockQueue.enqueueBlock(block, bestBlockNumber)
topBlocks = blockQueue.getBranch(topBlock.hash, dequeue = true)
(executed, errors) = blockExecution.executeAndValidateBlocks(topBlocks, currentWeight)
} yield (executed, errors, topBlocks)

executionResult match {
case Some((importedBlocks, maybeError, topBlocks)) =>
val result = maybeError match {
case None =>
BlockImportedToTop(importedBlocks)

case Some(error) if importedBlocks.isEmpty =>
blockQueue.removeSubtree(block.header.hash)
BlockImportFailed(error.toString)

case Some(_) =>
topBlocks.drop(importedBlocks.length).headOption.foreach { failedBlock =>
blockQueue.removeSubtree(failedBlock.header.hash)
}
BlockImportedToTop(importedBlocks)
}
log.debug(
"{}", {
val result = importedBlocks.map { blockData =>
val header = blockData.block.header
s"Imported new block (${header.number}: ${Hex.toHexString(header.hash.toArray)}) to the top of chain \n"
}
result.toString
}
)
result
}
}

Expand Down

0 comments on commit 8894e49

Please sign in to comment.