Skip to content

Commit

Permalink
[ETCM-468] revert change with BlockResolver - it was breaking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lemastero committed Jan 19, 2021
1 parent 1ec2956 commit 8e54c82
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
Expand Up @@ -4,7 +4,7 @@ import io.iohk.ethereum.consensus.blocks.BlockGenerator
import io.iohk.ethereum.domain.{Block, Blockchain}
import io.iohk.ethereum.jsonrpc.EthService.{BlockParam, ResolvedBlock}

class BlockResolver(blockchain: Blockchain, blockGenerator: BlockGenerator) {
class BlockResolver(blockchain: Blockchain, blockGenerator: BlockGenerator) { // TODO tests fails when I extracted this guy, not sure why

def resolveBlock(blockParam: BlockParam): Either[JsonRpcError, ResolvedBlock] = {
blockParam match {
Expand Down
27 changes: 24 additions & 3 deletions src/main/scala/io/iohk/ethereum/jsonrpc/EthGetProof.scala
Expand Up @@ -2,7 +2,8 @@ package io.iohk.ethereum.jsonrpc

import akka.util.ByteString
import cats.implicits._
import io.iohk.ethereum.domain.{Account, Address, Blockchain, UInt256}
import io.iohk.ethereum.consensus.blocks.BlockGenerator
import io.iohk.ethereum.domain.{Account, Address, Block, Blockchain, UInt256}
import io.iohk.ethereum.jsonrpc.EthService._
import monix.eval.Task

Expand Down Expand Up @@ -54,7 +55,7 @@ final case class ProofAccount(
* openethereum: https://github.com/openethereum/openethereum/pull/9001/files
* go-ethereum: https://github.com/openethereum/parity-ethereum/pull/9001/files
*/
class EthGetProof(blockchain: Blockchain, resolver: BlockResolver, ethCompatibleStorage: Boolean) {
class EthGetProof(blockchain: Blockchain, blockGenerator: BlockGenerator, ethCompatibleStorage: Boolean) {

/**
* Get account and storage values for account including Merkle Proof.
Expand All @@ -70,7 +71,7 @@ class EthGetProof(blockchain: Blockchain, resolver: BlockResolver, ethCompatible
blockNumber: BlockParam
): Task[Either[JsonRpcError, ProofAccount]] = Task {
for {
blockNumber <- resolver.resolveBlock(blockNumber).map(_.block.number)
blockNumber <- resolveBlock(blockNumber).map(_.block.number)
account <- Either.fromOption(
blockchain.getAccount(address, blockNumber),
noAccount(address, blockNumber)
Expand Down Expand Up @@ -126,4 +127,24 @@ class EthGetProof(blockchain: Blockchain, resolver: BlockResolver, ethCompatible
storageHash = account.storageRoot,
storageProof = storageProof
)

private def resolveBlock(blockParam: BlockParam): Either[JsonRpcError, ResolvedBlock] = {
def getBlock(number: BigInt): Either[JsonRpcError, Block] = {
blockchain
.getBlockByNumber(number)
.map(Right.apply)
.getOrElse(Left(JsonRpcError.InvalidParams(s"Block $number 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.Pending =>
blockGenerator.getPendingBlockAndState
.map(pb => ResolvedBlock(pb.pendingBlock.block, pendingState = Some(pb.worldState)))
.map(Right.apply)
.getOrElse(resolveBlock(BlockParam.Latest)) //Default behavior in other clients
}
}
}
26 changes: 22 additions & 4 deletions src/main/scala/io/iohk/ethereum/jsonrpc/EthService.scala
Expand Up @@ -235,6 +235,7 @@ class EthService(
getTransactionFromPoolTimeout: FiniteDuration,
askTimeout: Timeout
) extends Logger {

import EthService._

val hashRate: ConcurrentMap[ByteString, (BigInt, Date)] = new TrieMap[ByteString, (BigInt, Date)]()
Expand All @@ -245,9 +246,6 @@ class EthService(
private[this] def fullConsensusConfig = consensus.config
private[this] def consensusConfig: ConsensusConfig = fullConsensusConfig.generic

private val blockResolver = new BlockResolver(blockchain, blockGenerator)
import blockResolver.resolveBlock

private[this] def ifEthash[Req, Res](req: Req)(f: Req => Res): ServiceResponse[Res] = {
consensus.ifEthash[ServiceResponse[Res]](_ => Task.now(Right(f(req))))(
Task.now(Left(JsonRpcError.ConsensusIsNotEthash))
Expand Down Expand Up @@ -875,6 +873,26 @@ class EthService(
Left(JsonRpcError.NodeNotFound)
}

private def resolveBlock(blockParam: BlockParam): Either[JsonRpcError, ResolvedBlock] = {
def getBlock(number: BigInt): Either[JsonRpcError, Block] = {
blockchain
.getBlockByNumber(number)
.map(Right.apply)
.getOrElse(Left(JsonRpcError.InvalidParams(s"Block $number 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.Pending =>
blockGenerator.getPendingBlockAndState
.map(pb => ResolvedBlock(pb.pendingBlock.block, pendingState = Some(pb.worldState)))
.map(Right.apply)
.getOrElse(resolveBlock(BlockParam.Latest)) //Default behavior in other clients
}
}

private def doCall[A](req: CallRequest)(
f: (SignedTransactionWithSender, BlockHeader, Option[InMemoryWorldStateProxy]) => A
): Either[JsonRpcError, A] = for {
Expand Down Expand Up @@ -926,7 +944,7 @@ class EthService(
* Returns the account- and storage-values of the specified account including the Merkle-proof.
*/
def getProof(req: GetProofRequest): ServiceResponse[GetProofResponse] = {
new EthGetProof(blockchain, blockResolver, blockchainConfig.ethCompatibleStorage)
new EthGetProof(blockchain, consensus.blockGenerator, blockchainConfig.ethCompatibleStorage)
.run(req.address, req.storageKeys, req.blockNumber)
.map(_.map(GetProofResponse.apply))
}
Expand Down

0 comments on commit 8e54c82

Please sign in to comment.