Skip to content

Commit 580206e

Browse files
committed
ETCM-845: With best block fallback to the ones saved to storage
Fallback to stored best block
1 parent 9d44cf8 commit 580206e

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

src/main/scala/io/iohk/ethereum/domain/Blockchain.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,11 @@ class BlockchainImpl(
289289
override def getLatestCheckpointBlockNumber(): BigInt =
290290
bestKnownBlockAndLatestCheckpoint.get().latestCheckpointNumber
291291

292+
//returns the best known block if it's available in the storage, otherwise the best stored block
292293
override def getBestBlock(): Option[Block] = {
293294
val bestBlockNumber = getBestBlockNumber()
294295
log.debug("Trying to get best block with number {}", bestBlockNumber)
295-
getBlockByNumber(bestBlockNumber)
296+
getBlockByNumber(bestBlockNumber) orElse getBlockByNumber(appStateStorage.getBestBlockNumber())
296297
}
297298

298299
override def getAccount(address: Address, blockNumber: BigInt): Option[Account] =

src/main/scala/io/iohk/ethereum/jsonrpc/EthProofService.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import io.iohk.ethereum.jsonrpc.ProofService.{
1010
GetProofResponse,
1111
ProofAccount,
1212
StorageProof,
13-
StorageProofKey,
14-
StorageValueProof
13+
StorageProofKey
1514
}
1615
import io.iohk.ethereum.mpt.{MptNode, MptTraversals}
1716
import monix.eval.Task
@@ -201,16 +200,20 @@ class EthProofService(blockchain: Blockchain, blockGenerator: BlockGenerator, et
201200
ByteString(MptTraversals.encodeNode(node))
202201

203202
private def resolveBlock(blockParam: BlockParam): Either[JsonRpcError, ResolvedBlock] = {
204-
def getBlock(number: BigInt): Either[JsonRpcError, Block] = {
203+
def getBlock(number: BigInt): Either[JsonRpcError, Block] =
205204
blockchain
206205
.getBlockByNumber(number)
207206
.toRight(JsonRpcError.InvalidParams(s"Block $number not found"))
208-
}
207+
208+
def getLatestBlock(): Either[JsonRpcError, Block] =
209+
blockchain
210+
.getBestBlock()
211+
.toRight(JsonRpcError.InvalidParams("Latest block not found"))
209212

210213
blockParam match {
211214
case BlockParam.WithNumber(blockNumber) => getBlock(blockNumber).map(ResolvedBlock(_, pendingState = None))
212215
case BlockParam.Earliest => getBlock(0).map(ResolvedBlock(_, pendingState = None))
213-
case BlockParam.Latest => getBlock(blockchain.getBestBlockNumber()).map(ResolvedBlock(_, pendingState = None))
216+
case BlockParam.Latest => getLatestBlock().map(ResolvedBlock(_, pendingState = None))
214217
case BlockParam.Pending =>
215218
blockGenerator.getPendingBlockAndState
216219
.map(pb => ResolvedBlock(pb.pendingBlock.block, pendingState = Some(pb.worldState)))

src/main/scala/io/iohk/ethereum/jsonrpc/ResolveBlock.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ trait ResolveBlock {
2222
blockParam match {
2323
case BlockParam.WithNumber(blockNumber) => getBlock(blockNumber).map(ResolvedBlock(_, pendingState = None))
2424
case BlockParam.Earliest => getBlock(0).map(ResolvedBlock(_, pendingState = None))
25-
case BlockParam.Latest => getBlock(blockchain.getBestBlockNumber()).map(ResolvedBlock(_, pendingState = None))
25+
case BlockParam.Latest => getLatestBlock().map(ResolvedBlock(_, pendingState = None))
2626
case BlockParam.Pending =>
2727
ledger.consensus.blockGenerator.getPendingBlockAndState
2828
.map(pb => ResolvedBlock(pb.pendingBlock.block, pendingState = Some(pb.worldState)))
@@ -34,7 +34,11 @@ trait ResolveBlock {
3434
private def getBlock(number: BigInt): Either[JsonRpcError, Block] = {
3535
blockchain
3636
.getBlockByNumber(number)
37-
.map(Right.apply)
38-
.getOrElse(Left(JsonRpcError.InvalidParams(s"Block $number not found")))
37+
.toRight(JsonRpcError.InvalidParams(s"Block $number not found"))
3938
}
39+
40+
private def getLatestBlock(): Either[JsonRpcError, Block] =
41+
blockchain
42+
.getBestBlock()
43+
.toRight(JsonRpcError.InvalidParams("Latest block not found"))
4044
}

src/test/scala/io/iohk/ethereum/ledger/BlockImportSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class BlockImportSpec extends AnyFlatSpec with Matchers with ScalaFutures {
146146
blockQueue.isQueued(oldBlock3.header.hash) shouldBe true
147147
}
148148

149-
it should "fail to get bestblock after reorganisation of the longer chain to a shorter one if desync state happened between cache and db" in new EphemBlockchain {
149+
it should "get best stored block after reorganisation of the longer chain to a shorter one if desync state happened between cache and db" in new EphemBlockchain {
150150
val block1: Block = getBlock(bestNum - 2)
151151
// new chain is shorter but has a higher weight
152152
val newBlock2: Block = getBlock(bestNum - 1, difficulty = 101, parent = block1.header.hash)
@@ -192,7 +192,7 @@ class BlockImportSpec extends AnyFlatSpec with Matchers with ScalaFutures {
192192
// dying before updating the storage but after updating the cache, inconsistency is created
193193
blockchain.saveBestKnownBlocks(oldBlock4.number)
194194

195-
blockchain.getBestBlock() shouldBe None
195+
blockchain.getBestBlock() shouldBe Some(ancestorForValidation)
196196
}
197197

198198
it should "handle error when trying to reorganise chain" in new EphemBlockchain {

0 commit comments

Comments
 (0)