Skip to content

Commit

Permalink
fix test API for retesteth WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaap van der Plas authored and Igor Grahovac committed Apr 15, 2021
1 parent 4a4af51 commit e0eb860
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 42 deletions.
Expand Up @@ -90,13 +90,13 @@ class GenesisDataLoader(blockchain: Blockchain, blockchainConfig: BlockchainConf
val initalRootHash = MerklePatriciaTrie.EmptyRootHash

val stateMptRootHash = genesisData.alloc.zipWithIndex.foldLeft(initalRootHash) {
case (rootHash, (((address, AllocAccount(balance)), idx))) =>
case (rootHash, (((address, genesisAccount), idx))) =>
val mpt = MerklePatriciaTrie[Array[Byte], Account](rootHash, storage)
val paddedAddress = address.reverse.padTo(addressLength, "0").reverse.mkString
val stateRoot = mpt
.put(
crypto.kec256(Hex.decode(paddedAddress)),
Account(blockchainConfig.accountStartNonce, UInt256(BigInt(balance)), emptyTrieRootHash, emptyEvmHash)
Account(nonce = genesisAccount.nonce, balance = genesisAccount.balance, codeHash = genesisAccount.code)
)
.getRootHash
stateRoot
Expand Down
13 changes: 11 additions & 2 deletions src/main/scala/io/iohk/ethereum/blockchain/data/genesis.scala
@@ -1,8 +1,17 @@
package io.iohk.ethereum.blockchain.data

import akka.util.ByteString
import io.iohk.ethereum.domain.UInt256

case class AllocAccount(balance: String)
case class PrecompiledAccountConfig(name: String)

case class GenesisAccount(
precompiled: Option[PrecompiledAccountConfig],
balance: UInt256,
code: ByteString,
nonce: UInt256,
storage: Map[UInt256, UInt256]
)

case class GenesisData(
nonce: ByteString,
Expand All @@ -12,5 +21,5 @@ case class GenesisData(
gasLimit: String,
coinbase: ByteString,
timestamp: String,
alloc: Map[String, AllocAccount]
alloc: Map[String, GenesisAccount]
)
2 changes: 2 additions & 0 deletions src/main/scala/io/iohk/ethereum/jsonrpc/BlockResponse.scala
Expand Up @@ -13,6 +13,7 @@ case class CheckpointResponse(signatures: Seq[ECDSASignature], signers: Seq[Byte
case class BlockResponse(
number: BigInt,
hash: Option[ByteString],
mixHash: Option[ByteString],
parentHash: ByteString,
nonce: Option[ByteString],
sha3Uncles: ByteString,
Expand Down Expand Up @@ -82,6 +83,7 @@ object BlockResponse {
BlockResponse(
number = block.header.number,
hash = if (pendingBlock) None else Some(block.header.hash),
mixHash = if (pendingBlock) None else Some(block.header.mixHash),
parentHash = block.header.parentHash,
nonce = if (pendingBlock) None else Some(block.header.nonce),
sha3Uncles = block.header.ommersHash,
Expand Down
10 changes: 9 additions & 1 deletion src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcRequest.scala
@@ -1,6 +1,9 @@
package io.iohk.ethereum.jsonrpc

import org.json4s.JsonAST.{JArray, JValue}
import org.json4s.native.Serialization.write
import org.json4s.DefaultFormats
import org.json4s.Formats

//TODO: work on a more elegant solution
trait SensitiveInformationToString {
Expand All @@ -14,4 +17,9 @@ trait SensitiveInformationToString {
}

case class JsonRpcRequest(jsonrpc: String, method: String, params: Option[JArray], id: Option[JValue])
extends SensitiveInformationToString
extends SensitiveInformationToString {
override def toString(): String = {
implicit val formats: Formats = DefaultFormats
"JsonRpcRequest" + (jsonrpc, method, params.map(write(_)), id.map(write(_))).toString
}
}
Expand Up @@ -7,45 +7,67 @@ import io.iohk.ethereum.jsonrpc.serialization.{JsonEncoder, JsonMethodDecoder}
import org.json4s.JsonAST._
import org.json4s.JsonDSL._
import org.bouncycastle.util.encoders.Hex
import cats.implicits._
import io.iohk.ethereum.blockchain.data.GenesisAccount

import scala.util.Try
import io.iohk.ethereum.domain.UInt256

object TestJsonMethodsImplicits extends JsonMethodsImplicits {

implicit val test_setChainParams = new JsonMethodDecoder[SetChainParamsRequest]
with JsonEncoder[SetChainParamsResponse] {
private def extractAccounts(accountsJson: JValue): Either[JsonRpcError, Map[ByteString, AccountConfig]] = {
val accounts = accountsJson.asInstanceOf[JObject].values.collect { case (key, accObj: Map[_, _]) =>
val rawWei = accObj.asInstanceOf[Map[String, Any]]("wei").asInstanceOf[String]
val wei =
if (rawWei.startsWith("0x")) BigInt(rawWei.replace("0x", ""), 16)
else BigInt(rawWei, 10)
ByteString(Hex.decode(key.replace("0x", ""))) -> AccountConfig(None, wei)
}
Right(accounts)
}

private def extractAccount(accountJson: JValue): Either[JsonRpcError, GenesisAccount] =
for {
storageObject <- Try((accountJson \ "storage").extract[JObject]).toEither.left.map(e =>
InvalidParams(e.toString())
)
storage <- storageObject.obj.traverse {
case (key, JString(value)) =>
Try(UInt256(decode(key)) -> UInt256(decode(value))).toEither.left.map(e => InvalidParams(e.toString()))
case _ => Left(InvalidParams())
}
balance = UInt256(decode((accountJson \ "balance").extract[String]))
code = ByteString(decode((accountJson \ "code").extract[String]))
nonce = UInt256(decode((accountJson \ "nonce").extract[String]))
} yield GenesisAccount(
None,
balance,
code,
nonce,
storage.toMap
)

private def extractAccounts(accountsJson: JValue): Either[JsonRpcError, Map[ByteString, GenesisAccount]] =
for {
mapping <- Try(accountsJson.extract[JObject]).toEither.left.map(e => InvalidParams(e.toString()))
accounts <- mapping.obj.traverse { case (key, value) =>
for {
address <- extractBytes(key)
account <- extractAccount(value)
} yield address -> account
}
} yield accounts.toMap

private def extractBlockchainParams(blockchainParamsJson: JValue): Either[JsonRpcError, BlockchainParams] = {
for {
eIP150ForkBlock <- extractQuantity(blockchainParamsJson \ "EIP150ForkBlock")
eIP158ForkBlock <- extractQuantity(blockchainParamsJson \ "EIP158ForkBlock")
accountStartNonce <- extractQuantity(blockchainParamsJson \ "accountStartNonce")
allowFutureBlocks <- Try((blockchainParamsJson \ "allowFutureBlocks").extract[Boolean]).toEither.left.map(_ =>
InvalidParams()
)
blockReward <- extractQuantity(blockchainParamsJson \ "blockReward")
accountStartNonce <- optionalQuantity(blockchainParamsJson \ "accountStartNonce")
allowFutureBlocks = (blockchainParamsJson \ "allowFutureBlocks").extractOrElse(true)
blockReward <- optionalQuantity(blockchainParamsJson \ "blockReward")
byzantiumForkBlock <- extractQuantity(blockchainParamsJson \ "byzantiumForkBlock")
homesteadForkBlock <- extractQuantity(blockchainParamsJson \ "homesteadForkBlock")
maximumExtraDataSize <- extractQuantity(blockchainParamsJson \ "maximumExtraDataSize")
} yield BlockchainParams(
eIP150ForkBlock,
eIP158ForkBlock,
accountStartNonce,
accountStartNonce.getOrElse(0),
allowFutureBlocks,
blockReward,
blockReward.getOrElse(0),
byzantiumForkBlock,
homesteadForkBlock,
maximumExtraDataSize
0
)
}

Expand All @@ -54,7 +76,7 @@ object TestJsonMethodsImplicits extends JsonMethodsImplicits {
author <- extractBytes((genesisJson \ "author").extract[String])
extraData <- extractBytes((genesisJson \ "extraData").extract[String])
gasLimit <- extractQuantity(genesisJson \ "gasLimit")
parentHash <- extractBytes((genesisJson \ "parentHash").extract[String])
parentHash <- extractBytes((genesisJson \ "parentHash").extractOrElse(""))
timestamp <- extractBytes((genesisJson \ "timestamp").extract[String])
} yield GenesisParams(author, extraData, gasLimit, parentHash, timestamp)
}
Expand Down
31 changes: 15 additions & 16 deletions src/main/scala/io/iohk/ethereum/jsonrpc/TestService.scala
Expand Up @@ -3,7 +3,7 @@ package io.iohk.ethereum.jsonrpc
import akka.actor.ActorRef
import akka.util.{ByteString, Timeout}
import cats.syntax.functor._
import io.iohk.ethereum.blockchain.data.{AllocAccount, GenesisData, GenesisDataLoader}
import io.iohk.ethereum.blockchain.data.{GenesisAccount, GenesisData, GenesisDataLoader}
import io.iohk.ethereum.consensus.ConsensusConfig
import io.iohk.ethereum.consensus.blocks._
import io.iohk.ethereum.domain.{Address, Block, BlockchainImpl, UInt256}
Expand Down Expand Up @@ -35,13 +35,11 @@ object TestService {
homesteadForkBlock: BigInt,
maximumExtraDataSize: BigInt
)
case class PrecompiledAccountConfig(name: String)
case class AccountConfig(precompiled: Option[PrecompiledAccountConfig], wei: BigInt)
case class ChainParams(
genesis: GenesisParams,
blockchainParams: BlockchainParams,
sealEngine: String,
accounts: Map[ByteString, AccountConfig]
accounts: Map[ByteString, GenesisAccount]
)

case class SetChainParamsRequest(chainParams: ChainParams)
Expand Down Expand Up @@ -93,7 +91,7 @@ class TestService(
coinbase = request.chainParams.genesis.author,
timestamp = Hex.toHexString(request.chainParams.genesis.timestamp.toArray[Byte]),
alloc = request.chainParams.accounts.map { case (addr, acc) =>
Hex.toHexString(addr.toArray[Byte]) -> AllocAccount(acc.wei.toString)
Hex.toHexString(addr.toArray[Byte]) -> acc
}
)

Expand Down Expand Up @@ -150,18 +148,19 @@ class TestService(
implicit val timeout = Timeout(5.seconds)
pendingTransactionsManager
.askFor[PendingTransactionsResponse](PendingTransactionsManager.GetPendingTransactions)
.timeout(timeout.duration)
// .timeout(timeout.duration)
.onErrorRecover { case _ => PendingTransactionsResponse(Nil) }
.flatMap { pendingTxs =>
val pb = consensus.blockGenerator.generateBlock(
parentBlock,
pendingTxs.pendingTransactions.map(_.stx.tx),
etherbase,
Nil,
None
)
Task.now(pb.pendingBlock)
.map { pendingTxs =>
consensus.blockGenerator
.generateBlock(
parentBlock,
pendingTxs.pendingTransactions.map(_.stx.tx),
etherbase,
Nil,
None
)
.pendingBlock
}
.timeout(timeout.duration)
// .timeout(timeout.duration)
}
}
Expand Up @@ -49,6 +49,8 @@ trait JsonRpcBaseController {
def handleRequest(request: JsonRpcRequest): Task[JsonRpcResponse] = {
val startTimeNanos = System.nanoTime()

log.debug("received request {}", request)

val notFoundFn: PartialFunction[JsonRpcRequest, Task[JsonRpcResponse]] = { case _ =>
JsonRpcControllerMetrics.NotFoundMethodsCounter.increment()
Task.now(errorResponse(request, MethodNotFound))
Expand Down
Expand Up @@ -554,7 +554,7 @@ trait JSONRpcControllerBuilder {
with CheckpointingServiceBuilder
with MantisServiceBuilder =>

private val testService =
private lazy val testService =
if (Config.testmode) Some(this.asInstanceOf[TestServiceBuilder].testService)
else None

Expand Down

0 comments on commit e0eb860

Please sign in to comment.