Skip to content

Commit 34b3ac5

Browse files
Igor Grahovacdzajkowski
authored andcommitted
ETCM-697: Fixed code hash parsing bug. Added debug account range method definition
1 parent 00cb050 commit 34b3ac5

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/main/scala/io/iohk/ethereum/blockchain/data/GenesisDataLoader.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class GenesisDataLoader(blockchain: Blockchain, blockchainConfig: BlockchainConf
102102
Account(
103103
nonce = genesisAccount.nonce
104104
.getOrElse(blockchainConfig.accountStartNonce),
105-
balance = genesisAccount.balance
105+
balance = genesisAccount.balance,
106+
codeHash = genesisAccount.code.map(codeValue => crypto.kec256(codeValue)).getOrElse(Account.EmptyCodeHash)
106107
)
107108
)
108109
.getRootHash

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ case class JsonRpcController(
249249
handle[ImportRawBlockRequest, ImportRawBlockResponse](testService.importRawBlock, req)
250250
case req @ JsonRpcRequest(_, "miner_setEtherbase", _, _) =>
251251
handle[SetEtherbaseRequest, SetEtherbaseResponse](testService.setEtherbase, req)
252+
case req @ JsonRpcRequest(_, "debug_accountRange", _, _) =>
253+
handle[AccountsInRangeRequest, AccountsInRangeResponse](testService.getAccountsInRange, req)
252254
}
253255

254256
private def handleIeleRequest: PartialFunction[JsonRpcRequest, Task[JsonRpcResponse]] = {

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import io.iohk.ethereum.blockchain.data.GenesisAccount
1212

1313
import scala.util.Try
1414
import io.iohk.ethereum.domain.UInt256
15+
import org.json4s
16+
import org.json4s.Extraction
1517

1618
object TestJsonMethodsImplicits extends JsonMethodsImplicits {
1719

@@ -29,13 +31,15 @@ object TestJsonMethodsImplicits extends JsonMethodsImplicits {
2931
case _ => Left(InvalidParams())
3032
}
3133
balance = UInt256(decode((accountJson \ "balance").extract[String]))
32-
code = Some(ByteString(decode((accountJson \ "code").extract[String])))
33-
nonce = Some(UInt256(decode((accountJson \ "nonce").extract[String])))
34+
code = decode((accountJson \ "code").extract[String])
35+
codeOpt = if (code.isEmpty) None else Some(ByteString(code))
36+
nonce = decode((accountJson \ "nonce").extract[String])
37+
nonceOpt = if (nonce.isEmpty || UInt256(nonce) == UInt256.Zero) None else Some(UInt256(nonce))
3438
} yield GenesisAccount(
3539
None,
3640
balance,
37-
code,
38-
nonce,
41+
codeOpt,
42+
nonceOpt,
3943
Some(storage.toMap)
4044
)
4145

@@ -158,4 +162,27 @@ object TestJsonMethodsImplicits extends JsonMethodsImplicits {
158162

159163
def encodeJson(t: SetEtherbaseResponse): JValue = true
160164
}
165+
166+
implicit val debug_accountRange = new JsonMethodDecoder[AccountsInRangeRequest]
167+
with JsonEncoder[AccountsInRangeResponse] {
168+
override def decodeJson(params: Option[JArray]): Either[JsonRpcError, AccountsInRangeRequest] =
169+
params match {
170+
case Some(JArray(blockHashOrNumber :: txIndex :: addressHash :: maxResults :: Nil)) =>
171+
for {
172+
txIndex <- extractQuantity(txIndex)
173+
maxResults <- extractQuantity(maxResults)
174+
addressHash <- extractBytes(addressHash.extract[String])
175+
blockHashOrNumberEither = extractBlockHashOrNumber(blockHashOrNumber.extract[String])
176+
} yield AccountsInRangeRequest(
177+
AccountsInRangeRequestParams(blockHashOrNumberEither, txIndex, addressHash, maxResults)
178+
)
179+
case _ => Left(InvalidParams())
180+
}
181+
182+
private def extractBlockHashOrNumber(blockHash: String): Either[BigInt, ByteString] =
183+
extractHash(blockHash)
184+
.fold(_ => Left(BigInt(blockHash)), Right(_))
185+
186+
override def encodeJson(t: AccountsInRangeResponse): JValue = Extraction.decompose(t)
187+
}
161188
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,26 @@ object TestService {
4141
homesteadForkBlock: BigInt,
4242
maximumExtraDataSize: BigInt
4343
)
44+
4445
case class ChainParams(
4546
genesis: GenesisParams,
4647
blockchainParams: BlockchainParams,
4748
sealEngine: String,
4849
accounts: Map[ByteString, GenesisAccount]
4950
)
5051

52+
case class AccountsInRangeRequestParams(
53+
blockHashOrNumber: Either[BigInt, ByteString],
54+
txIndex: BigInt,
55+
addressHash: ByteString,
56+
maxResults: BigInt
57+
)
58+
59+
case class AccountsInRange(
60+
addressMap: Map[ByteString, ByteString],
61+
nextKey: ByteString
62+
)
63+
5164
case class SetChainParamsRequest(chainParams: ChainParams)
5265
case class SetChainParamsResponse()
5366

@@ -65,6 +78,9 @@ object TestService {
6578

6679
case class ImportRawBlockRequest(blockRlp: String)
6780
case class ImportRawBlockResponse(blockHash: String)
81+
82+
case class AccountsInRangeRequest(parameters: AccountsInRangeRequestParams)
83+
case class AccountsInRangeResponse(addressMap: Map[String, String], nextKey: String)
6884
}
6985

7086
class TestService(
@@ -193,4 +209,6 @@ class TestService(
193209
}
194210
// .timeout(timeout.duration)
195211
}
212+
213+
def getAccountsInRange(request: AccountsInRangeRequest): ServiceResponse[AccountsInRangeResponse] = ???
196214
}

0 commit comments

Comments
 (0)