Skip to content

Commit f779c60

Browse files
Igor Grahovacdzajkowski
authored andcommitted
ETCM-697: Added initial account range implementation
1 parent cf5e5f4 commit f779c60

File tree

6 files changed

+55
-21
lines changed

6 files changed

+55
-21
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ trait BaseBlockResponse {
3030
def timestamp: BigInt
3131
def transactions: Either[Seq[ByteString], Seq[TransactionResponse]]
3232
def uncles: Seq[ByteString]
33-
def coinbase: Option[ByteString]
3433
}
3534

3635
case class EthBlockResponse(
@@ -53,8 +52,7 @@ case class EthBlockResponse(
5352
gasUsed: BigInt,
5453
timestamp: BigInt,
5554
transactions: Either[Seq[ByteString], Seq[TransactionResponse]],
56-
uncles: Seq[ByteString],
57-
coinbase: Option[ByteString]
55+
uncles: Seq[ByteString]
5856
) extends BaseBlockResponse
5957

6058
//scalastyle:off method.length
@@ -83,8 +81,7 @@ case class BlockResponse(
8381
transactions: Either[Seq[ByteString], Seq[TransactionResponse]],
8482
uncles: Seq[ByteString],
8583
signature: String,
86-
signer: String,
87-
coinbase: Option[ByteString]
84+
signer: String
8885
) extends BaseBlockResponse {
8986
val chainWeight: Option[ChainWeight] = for {
9087
lcn <- lastCheckpointNumber
@@ -155,8 +152,7 @@ object BlockResponse {
155152
transactions = transactions,
156153
uncles = block.body.uncleNodesList.map(_.hash),
157154
signature = signatureStr,
158-
signer = signerStr,
159-
coinbase = None
155+
signer = signerStr
160156
)
161157
}
162158

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.iohk.ethereum.jsonrpc
22

33
import java.time.Duration
4-
54
import akka.util.ByteString
65
import io.iohk.ethereum.crypto.ECDSASignature
76
import io.iohk.ethereum.domain.Address
@@ -13,13 +12,14 @@ import io.iohk.ethereum.jsonrpc.serialization.JsonMethodDecoder.NoParamsMethodDe
1312
import io.iohk.ethereum.jsonrpc.serialization.{JsonEncoder, JsonMethodCodec, JsonMethodDecoder, JsonSerializers}
1413
import io.iohk.ethereum.utils.BigIntExtensionMethods.BigIntAsUnsigned
1514
import org.bouncycastle.util.encoders.Hex
15+
import org.json4s.Formats
1616
import org.json4s.JsonAST._
1717
import org.json4s.JsonDSL._
1818

1919
import scala.util.Try
2020

2121
trait JsonMethodsImplicits {
22-
implicit val formats = JsonSerializers.formats
22+
implicit val formats: Formats = JsonSerializers.formats
2323

2424
def encodeAsHex(input: ByteString): JString =
2525
JString(s"0x${Hex.toHexString(input.toArray[Byte])}")

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ import io.iohk.ethereum.jsonrpc.TestService._
66
import io.iohk.ethereum.jsonrpc.serialization.{JsonEncoder, JsonMethodDecoder}
77
import org.json4s.JsonAST._
88
import org.json4s.JsonDSL._
9-
import org.bouncycastle.util.encoders.Hex
109
import cats.implicits._
1110
import io.iohk.ethereum.blockchain.data.GenesisAccount
1211

1312
import scala.util.Try
1413
import io.iohk.ethereum.domain.UInt256
15-
import org.json4s
16-
import org.json4s.Extraction
1714

1815
object TestJsonMethodsImplicits extends JsonMethodsImplicits {
1916

@@ -183,6 +180,11 @@ object TestJsonMethodsImplicits extends JsonMethodsImplicits {
183180
extractHash(blockHash)
184181
.fold(_ => Left(BigInt(blockHash)), Right(_))
185182

186-
override def encodeJson(t: AccountsInRangeResponse): JValue = Extraction.decompose(t)
183+
override def encodeJson(t: AccountsInRangeResponse): JValue = JObject(
184+
"addressMap" -> JObject(
185+
t.addressMap.toList.map(addressPair => encodeAsHex(addressPair._1).values -> encodeAsHex(addressPair._2))
186+
),
187+
"nextKey" -> encodeAsHex(t.nextKey)
188+
)
187189
}
188190
}

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import akka.util.{ByteString, Timeout}
55
import io.iohk.ethereum.blockchain.data.{GenesisAccount, GenesisData, GenesisDataLoader}
66
import io.iohk.ethereum.consensus.ConsensusConfig
77
import io.iohk.ethereum.consensus.blocks._
8+
import io.iohk.ethereum.crypto
89
import io.iohk.ethereum.domain.Block._
910
import io.iohk.ethereum.domain.{Address, Block, BlockchainImpl, UInt256}
1011
import io.iohk.ethereum.ledger._
@@ -80,7 +81,7 @@ object TestService {
8081
case class ImportRawBlockResponse(blockHash: String)
8182

8283
case class AccountsInRangeRequest(parameters: AccountsInRangeRequestParams)
83-
case class AccountsInRangeResponse(addressMap: Map[String, String], nextKey: String)
84+
case class AccountsInRangeResponse(addressMap: Map[ByteString, ByteString], nextKey: ByteString)
8485
}
8586

8687
class TestService(
@@ -97,6 +98,8 @@ class TestService(
9798
import io.iohk.ethereum.jsonrpc.AkkaTaskOps._
9899

99100
private var etherbase: Address = consensusConfig.coinbase
101+
private var accountAddresses: List[String] = List()
102+
private var accountRangeOffset = 0
100103

101104
def setChainParams(request: SetChainParamsRequest): ServiceResponse[SetChainParamsResponse] = {
102105
val newBlockchainConfig = testLedgerWrapper.blockchainConfig.copy(
@@ -130,6 +133,9 @@ class TestService(
130133
// update test ledger with new config
131134
testLedgerWrapper.blockchainConfig = newBlockchainConfig
132135

136+
accountAddresses = genesisData.alloc.keys.toList
137+
accountRangeOffset = 0
138+
133139
Task.now(Right(SetChainParamsResponse()))
134140
}
135141

@@ -211,5 +217,38 @@ class TestService(
211217
.timeout(timeout.duration)
212218
}
213219

214-
def getAccountsInRange(request: AccountsInRangeRequest): ServiceResponse[AccountsInRangeResponse] = ???
220+
def getAccountsInRange(request: AccountsInRangeRequest): ServiceResponse[AccountsInRangeResponse] = {
221+
val blockOpt = request.parameters.blockHashOrNumber
222+
.fold(number => blockchain.getBlockByNumber(number), blockHash => blockchain.getBlockByHash(blockHash))
223+
224+
if (blockOpt.isEmpty) {
225+
Task.now(Right(AccountsInRangeResponse(Map(), ByteString(0))))
226+
}
227+
228+
val accountBatch = accountAddresses
229+
.slice(accountRangeOffset, accountRangeOffset + request.parameters.maxResults.toInt + 1)
230+
231+
val addressesForExistingAccounts = accountBatch
232+
.filter(key => {
233+
val accountOpt = blockchain.getAccount(Address(key), blockOpt.get.header.number)
234+
accountOpt.isDefined
235+
})
236+
.map(key => (key, Address(crypto.kec256(Hex.decode(key)))))
237+
238+
Task.now(
239+
Right(
240+
AccountsInRangeResponse(
241+
addressMap = addressesForExistingAccounts
242+
.take(request.parameters.maxResults.toInt)
243+
.foldLeft(Map[ByteString, ByteString]())((el, addressPair) =>
244+
el + (addressPair._2.bytes -> ByteStringUtils.string2hash(addressPair._1))
245+
),
246+
nextKey =
247+
if (accountBatch.size > request.parameters.maxResults)
248+
ByteStringUtils.string2hash(addressesForExistingAccounts.last._1)
249+
else UInt256(0).bytes
250+
)
251+
)
252+
)
253+
}
215254
}

src/main/scala/io/iohk/ethereum/jsonrpc/serialization/JsonSerializers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.json4s.{CustomSerializer, DefaultFormats, Formats, JNull, JString}
88

99
object JsonSerializers {
1010
implicit val formats: Formats =
11-
DefaultFormats.preservingEmptyValues + UnformattedDataJsonSerializer + QuantitiesSerializer + OptionNoneToJNullSerializer + AddressJsonSerializer
11+
DefaultFormats + UnformattedDataJsonSerializer + QuantitiesSerializer + OptionNoneToJNullSerializer + AddressJsonSerializer
1212

1313
object UnformattedDataJsonSerializer
1414
extends CustomSerializer[ByteString](_ =>

src/main/scala/io/iohk/ethereum/testmode/TestEthBlockServiceWrapper.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ class TestEthBlockServiceWrapper(blockchain: Blockchain, ledger: Ledger, consens
4444
_.map(blockByBlockResponse =>
4545
BlockByNumberResponse(
4646
blockByBlockResponse.blockResponse
47-
.map(response =>
48-
toEthResponse(response).copy(coinbase = Some(ByteString(consensus.config.generic.coinbase.toArray)))
49-
)
47+
.map(response => toEthResponse(response))
5048
)
5149
)
5250
)
@@ -71,7 +69,6 @@ class TestEthBlockServiceWrapper(blockchain: Blockchain, ledger: Ledger, consens
7169
response.gasUsed,
7270
response.timestamp,
7371
response.transactions,
74-
response.uncles,
75-
response.coinbase
72+
response.uncles
7673
)
7774
}

0 commit comments

Comments
 (0)