Skip to content

Commit c7bd863

Browse files
committed
Change getStorageProof impl
1 parent d6aa26c commit c7bd863

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

src/it/scala/io/iohk/ethereum/txExecTest/util/DumpChainApp.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class BlockchainMock(genesisHash: ByteString) extends Blockchain {
150150
rootHash: NodeHash,
151151
position: BigInt,
152152
ethCompatibleStorage: Boolean
153-
): Option[(BigInt, Seq[MptNode])] = None
153+
): (BigInt, Seq[MptNode]) = (1, Seq.empty)
154154

155155
override protected def getHashByBlockNumber(number: BigInt): Option[ByteString] = Some(genesisHash)
156156

src/main/scala/io/iohk/ethereum/domain/Blockchain.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,17 @@ trait Blockchain {
9595
*/
9696
def getAccountStorageAt(rootHash: ByteString, position: BigInt, ethCompatibleStorage: Boolean): ByteString
9797

98+
/**
99+
* Get a storage-value and its proof being the path from the root node until the last matching node.
100+
*
101+
* @param rootHash storage root hash
102+
* @param position storage position
103+
*/
98104
def getStorageProofAt(
99105
rootHash: ByteString,
100106
position: BigInt,
101107
ethCompatibleStorage: Boolean
102-
): Option[(BigInt, Seq[MptNode])]
108+
): (BigInt, Seq[MptNode])
103109

104110
/**
105111
* Returns the receipts based on a block hash
@@ -307,16 +313,16 @@ class BlockchainImpl(
307313
rootHash: ByteString,
308314
position: BigInt,
309315
ethCompatibleStorage: Boolean
310-
): Option[(BigInt, Seq[MptNode])] = {
316+
): (BigInt, Seq[MptNode]) = {
317+
val defaultValue = BigInt(0)
311318
val storage: MptStorage = stateStorage.getBackingStorage(0)
312319
val mpt: MerklePatriciaTrie[BigInt, BigInt] = {
313320
if (ethCompatibleStorage) domain.EthereumUInt256Mpt.storageMpt(rootHash, storage)
314321
else domain.ArbitraryIntegerMpt.storageMpt(rootHash, storage)
315322
}
316-
for {
317-
value <- mpt.get(position)
318-
proof <- mpt.getProof(position)
319-
} yield (value, proof)
323+
val value = mpt.get(position).getOrElse(defaultValue)
324+
val proof = mpt.getProof(position).getOrElse(Seq.empty)
325+
(value, proof)
320326
}
321327

322328
private def persistBestBlocksData(): Unit = {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import io.iohk.ethereum.jsonrpc.JsonRpcError.InvalidParams
44
import io.iohk.ethereum.jsonrpc.ProofService.{GetProofRequest, GetProofResponse, StorageProofKey}
55
import io.iohk.ethereum.jsonrpc.serialization.JsonEncoder
66
import io.iohk.ethereum.jsonrpc.serialization.JsonMethodDecoder
7-
import org.json4s.Extraction
87
import org.json4s.JsonAST.{JArray, JString, JValue, _}
9-
import org.json4s.JsonDSL._
108

119
object EthProofJsonMethodsImplicits extends JsonMethodsImplicits {
1210
def extractStorageKeys(input: JValue): Either[JsonRpcError, Seq[StorageProofKey]] = {

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,37 +143,32 @@ class EthProofService(blockchain: Blockchain, blockGenerator: BlockGenerator, et
143143
blockchain.getAccountProof(address, blockNumber).map(_.map(asRlpSerializedNode)),
144144
noAccountProof(address, blockNumber)
145145
)
146-
storageProof <- getStorageProof(account, storageKeys)
146+
storageProof <- Either.right(getStorageProof(account, storageKeys))
147147
} yield ProofAccount(account, accountProof, storageProof, address)
148148
}
149149

150150
def getStorageProof(
151151
account: Account,
152152
storageKeys: Seq[StorageProofKey]
153-
): Either[JsonRpcError, Seq[StorageProof]] = {
153+
): Seq[StorageProof] = {
154154
storageKeys.toList
155155
.map { storageKey =>
156156
blockchain
157157
.getStorageProofAt(
158158
rootHash = account.storageRoot,
159159
position = storageKey.v,
160160
ethCompatibleStorage = ethCompatibleStorage
161-
)
162-
.map { case (value, proof) => StorageProof(storageKey, value, proof.map(asRlpSerializedNode)) }
163-
.toRight(noStorageProof(account, storageKey))
161+
) match {
162+
case (value, proof) => StorageProof(storageKey, value, proof.map(asRlpSerializedNode))
163+
}
164164
}
165-
.sequence
166-
.map(_.toSeq)
167165
}
168166

169-
private def noStorageProof(account: Account, storagekey: StorageProofKey): JsonRpcError =
170-
JsonRpcError.LogicError(s"No storage proof for [${account.toString}] storage key [${storagekey.toString}]")
171-
172167
private def noAccount(address: Address, blockNumber: BigInt): JsonRpcError =
173-
JsonRpcError.LogicError(s"No storage proof for Address [${address.toString}] blockNumber [${blockNumber.toString}]")
168+
JsonRpcError.LogicError(s"No account found for Address [${address.toString}] blockNumber [${blockNumber.toString}]")
174169

175170
private def noAccountProof(address: Address, blockNumber: BigInt): JsonRpcError =
176-
JsonRpcError.LogicError(s"No storage proof for Address [${address.toString}] blockNumber [${blockNumber.toString}]")
171+
JsonRpcError.LogicError(s"No account proof for Address [${address.toString}] blockNumber [${blockNumber.toString}]")
177172

178173
private def asRlpSerializedNode(node: MptNode): ByteString =
179174
ByteString(MptTraversals.encodeNode(node))

src/test/scala/io/iohk/ethereum/jsonrpc/EthProofServiceSpec.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import org.scalatest.concurrent.ScalaFutures
2525
import org.scalatest.flatspec.AnyFlatSpecLike
2626
import org.scalatest.matchers.should.Matchers
2727

28-
import scala.concurrent.duration.{DurationInt, FiniteDuration}
2928
import io.iohk.ethereum.jsonrpc.EthUserService.GetBalanceResponse
3029
import io.iohk.ethereum.jsonrpc.EthUserService.GetBalanceRequest
3130
import io.iohk.ethereum.jsonrpc.EthUserService.GetTransactionCountRequest

src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -803,13 +803,15 @@ class JsonRpcControllerEthSpec
803803
}
804804

805805
it should "decode and encode eth_getProof request and response" in new JsonRpcControllerFixture {
806+
val address = "0x7F0d15C7FAae65896648C8273B6d7E43f58Fa842"
807+
806808
val request: JsonRpcRequest = JsonRpcRequest(
807809
jsonrpc = "2.0",
808810
method = "eth_getProof",
809811
params = Some(
810812
JArray(
811813
List(
812-
JString("0x7F0d15C7FAae65896648C8273B6d7E43f58Fa842"),
814+
JString(address),
813815
JArray(List(JString("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"))),
814816
JString("latest")
815817
)
@@ -819,14 +821,14 @@ class JsonRpcControllerEthSpec
819821
)
820822

821823
val expectedDecodedRequest = GetProofRequest(
822-
address = Address("0x7f0d15c7faae65896648c8273b6d7e43f58fa842"),
824+
address = Address(address),
823825
storageKeys =
824826
List(StorageProofKey(BigInt("39309028074332508661983559455579427211983204215636056653337583610388178777121"))),
825827
blockNumber = BlockParam.Latest
826828
)
827829
val expectedEncodedResponse: GetProofResponse = GetProofResponse(
828830
ProofAccount(
829-
address = Address("0x7f0d15c7faae65896648c8273b6d7e43f58fa842"),
831+
address = Address(address),
830832
accountProof = Seq(ByteString(Hex.decode("1234"))),
831833
balance = BigInt(0x0),
832834
codeHash = ByteString(Hex.decode("123eeaa22a")),

0 commit comments

Comments
 (0)