Skip to content

ETCM-845: Through rpc expose only blocks that are saved to storage #999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2021
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
3 changes: 2 additions & 1 deletion src/main/scala/io/iohk/ethereum/domain/Blockchain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,11 @@ class BlockchainImpl(
override def getLatestCheckpointBlockNumber(): BigInt =
bestKnownBlockAndLatestCheckpoint.get().latestCheckpointNumber

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

override def getAccount(address: Address, blockNumber: BigInt): Option[Account] =
Expand Down
13 changes: 8 additions & 5 deletions src/main/scala/io/iohk/ethereum/jsonrpc/EthProofService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import io.iohk.ethereum.jsonrpc.ProofService.{
GetProofResponse,
ProofAccount,
StorageProof,
StorageProofKey,
StorageValueProof
StorageProofKey
}
import io.iohk.ethereum.mpt.{MptNode, MptTraversals}
import monix.eval.Task
Expand Down Expand Up @@ -201,16 +200,20 @@ class EthProofService(blockchain: Blockchain, blockGenerator: BlockGenerator, et
ByteString(MptTraversals.encodeNode(node))

private def resolveBlock(blockParam: BlockParam): Either[JsonRpcError, ResolvedBlock] = {
def getBlock(number: BigInt): Either[JsonRpcError, Block] = {
def getBlock(number: BigInt): Either[JsonRpcError, Block] =
blockchain
.getBlockByNumber(number)
.toRight(JsonRpcError.InvalidParams(s"Block $number not found"))
}

def getLatestBlock(): Either[JsonRpcError, Block] =
blockchain
.getBestBlock()
.toRight(JsonRpcError.InvalidParams("Latest block not found"))

blockParam match {
case BlockParam.WithNumber(blockNumber) => getBlock(blockNumber).map(ResolvedBlock(_, pendingState = None))
case BlockParam.Earliest => getBlock(0).map(ResolvedBlock(_, pendingState = None))
case BlockParam.Latest => getBlock(blockchain.getBestBlockNumber()).map(ResolvedBlock(_, pendingState = None))
case BlockParam.Latest => getLatestBlock().map(ResolvedBlock(_, pendingState = None))
case BlockParam.Pending =>
blockGenerator.getPendingBlockAndState
.map(pb => ResolvedBlock(pb.pendingBlock.block, pendingState = Some(pb.worldState)))
Expand Down
10 changes: 7 additions & 3 deletions src/main/scala/io/iohk/ethereum/jsonrpc/ResolveBlock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait ResolveBlock {
blockParam match {
case BlockParam.WithNumber(blockNumber) => getBlock(blockNumber).map(ResolvedBlock(_, pendingState = None))
case BlockParam.Earliest => getBlock(0).map(ResolvedBlock(_, pendingState = None))
case BlockParam.Latest => getBlock(blockchain.getBestBlockNumber()).map(ResolvedBlock(_, pendingState = None))
case BlockParam.Latest => getLatestBlock().map(ResolvedBlock(_, pendingState = None))
case BlockParam.Pending =>
ledger.consensus.blockGenerator.getPendingBlockAndState
.map(pb => ResolvedBlock(pb.pendingBlock.block, pendingState = Some(pb.worldState)))
Expand All @@ -34,7 +34,11 @@ trait ResolveBlock {
private def getBlock(number: BigInt): Either[JsonRpcError, Block] = {
blockchain
.getBlockByNumber(number)
.map(Right.apply)
.getOrElse(Left(JsonRpcError.InvalidParams(s"Block $number not found")))
.toRight(JsonRpcError.InvalidParams(s"Block $number not found"))
}

private def getLatestBlock(): Either[JsonRpcError, Block] =
blockchain
.getBestBlock()
.toRight(JsonRpcError.InvalidParams("Latest block not found"))
}
4 changes: 2 additions & 2 deletions src/test/scala/io/iohk/ethereum/ledger/BlockImportSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class BlockImportSpec extends AnyFlatSpec with Matchers with ScalaFutures {
blockQueue.isQueued(oldBlock3.header.hash) shouldBe true
}

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 {
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 {
val block1: Block = getBlock(bestNum - 2)
// new chain is shorter but has a higher weight
val newBlock2: Block = getBlock(bestNum - 1, difficulty = 101, parent = block1.header.hash)
Expand Down Expand Up @@ -192,7 +192,7 @@ class BlockImportSpec extends AnyFlatSpec with Matchers with ScalaFutures {
// dying before updating the storage but after updating the cache, inconsistency is created
blockchain.saveBestKnownBlocks(oldBlock4.number)

blockchain.getBestBlock() shouldBe None
blockchain.getBestBlock() shouldBe Some(ancestorForValidation)
}

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