Skip to content

Commit

Permalink
ETCM-697: Fixed code hash parsing bug. Added debug account range meth…
Browse files Browse the repository at this point in the history
…od definition
  • Loading branch information
Igor Grahovac committed Apr 15, 2021
1 parent 8f71d32 commit 4f786e4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
Expand Up @@ -102,7 +102,8 @@ class GenesisDataLoader(blockchain: Blockchain, blockchainConfig: BlockchainConf
Account(
nonce = genesisAccount.nonce
.getOrElse(blockchainConfig.accountStartNonce),
balance = genesisAccount.balance
balance = genesisAccount.balance,
codeHash = genesisAccount.code.map(codeValue => crypto.kec256(codeValue)).getOrElse(Account.EmptyCodeHash)
)
)
.getRootHash
Expand Down
Expand Up @@ -249,6 +249,8 @@ case class JsonRpcController(
handle[ImportRawBlockRequest, ImportRawBlockResponse](testService.importRawBlock, req)
case req @ JsonRpcRequest(_, "miner_setEtherbase", _, _) =>
handle[SetEtherbaseRequest, SetEtherbaseResponse](testService.setEtherbase, req)
case req @ JsonRpcRequest(_, "debug_accountRange", _, _) =>
handle[AccountsInRangeRequest, AccountsInRangeResponse](testService.getAccountsInRange, req)
}

private def handleIeleRequest: PartialFunction[JsonRpcRequest, Task[JsonRpcResponse]] = {
Expand Down
Expand Up @@ -12,6 +12,8 @@ import io.iohk.ethereum.blockchain.data.GenesisAccount

import scala.util.Try
import io.iohk.ethereum.domain.UInt256
import org.json4s
import org.json4s.Extraction

object TestJsonMethodsImplicits extends JsonMethodsImplicits {

Expand All @@ -29,13 +31,15 @@ object TestJsonMethodsImplicits extends JsonMethodsImplicits {
case _ => Left(InvalidParams())
}
balance = UInt256(decode((accountJson \ "balance").extract[String]))
code = Some(ByteString(decode((accountJson \ "code").extract[String])))
nonce = Some(UInt256(decode((accountJson \ "nonce").extract[String])))
code = decode((accountJson \ "code").extract[String])
codeOpt = if (code.isEmpty) None else Some(ByteString(code))
nonce = decode((accountJson \ "nonce").extract[String])
nonceOpt = if (nonce.isEmpty || UInt256(nonce) == UInt256.Zero) None else Some(UInt256(nonce))
} yield GenesisAccount(
None,
balance,
code,
nonce,
codeOpt,
nonceOpt,
Some(storage.toMap)
)

Expand Down Expand Up @@ -158,4 +162,27 @@ object TestJsonMethodsImplicits extends JsonMethodsImplicits {

def encodeJson(t: SetEtherbaseResponse): JValue = true
}

implicit val debug_accountRange = new JsonMethodDecoder[AccountsInRangeRequest]
with JsonEncoder[AccountsInRangeResponse] {
override def decodeJson(params: Option[JArray]): Either[JsonRpcError, AccountsInRangeRequest] =
params match {
case Some(JArray(blockHashOrNumber :: txIndex :: addressHash :: maxResults :: Nil)) =>
for {
txIndex <- extractQuantity(txIndex)
maxResults <- extractQuantity(maxResults)
addressHash <- extractBytes(addressHash.extract[String])
blockHashOrNumberEither = extractBlockHashOrNumber(blockHashOrNumber.extract[String])
} yield AccountsInRangeRequest(
AccountsInRangeRequestParams(blockHashOrNumberEither, txIndex, addressHash, maxResults)
)
case _ => Left(InvalidParams())
}

private def extractBlockHashOrNumber(blockHash: String): Either[BigInt, ByteString] =
extractHash(blockHash)
.fold(_ => Left(BigInt(blockHash)), Right(_))

override def encodeJson(t: AccountsInRangeResponse): JValue = Extraction.decompose(t)
}
}
18 changes: 18 additions & 0 deletions src/main/scala/io/iohk/ethereum/jsonrpc/TestService.scala
Expand Up @@ -41,13 +41,26 @@ object TestService {
homesteadForkBlock: BigInt,
maximumExtraDataSize: BigInt
)

case class ChainParams(
genesis: GenesisParams,
blockchainParams: BlockchainParams,
sealEngine: String,
accounts: Map[ByteString, GenesisAccount]
)

case class AccountsInRangeRequestParams(
blockHashOrNumber: Either[BigInt, ByteString],
txIndex: BigInt,
addressHash: ByteString,
maxResults: BigInt
)

case class AccountsInRange(
addressMap: Map[ByteString, ByteString],
nextKey: ByteString
)

case class SetChainParamsRequest(chainParams: ChainParams)
case class SetChainParamsResponse()

Expand All @@ -65,6 +78,9 @@ object TestService {

case class ImportRawBlockRequest(blockRlp: String)
case class ImportRawBlockResponse(blockHash: String)

case class AccountsInRangeRequest(parameters: AccountsInRangeRequestParams)
case class AccountsInRangeResponse(addressMap: Map[String, String], nextKey: String)
}

class TestService(
Expand Down Expand Up @@ -193,4 +209,6 @@ class TestService(
}
// .timeout(timeout.duration)
}

def getAccountsInRange(request: AccountsInRangeRequest): ServiceResponse[AccountsInRangeResponse] = ???
}

0 comments on commit 4f786e4

Please sign in to comment.