From 3f11e223c1abc2f1cdd02e8cd1d17341fd6d063c Mon Sep 17 00:00:00 2001 From: web3-developer <51288821+web3-developer@users.noreply.github.com> Date: Mon, 8 Jan 2024 16:36:25 +0800 Subject: [PATCH] Return zero hash for codeHash and storageHash if account doesn't exist. --- nimbus/rpc/p2p.nim | 23 +++++++++++++++-------- tests/test_rpc.nim | 13 ++++++++----- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/nimbus/rpc/p2p.nim b/nimbus/rpc/p2p.nim index b49238fa93..f73b4fafe6 100644 --- a/nimbus/rpc/p2p.nim +++ b/nimbus/rpc/p2p.nim @@ -518,6 +518,7 @@ proc setupEthRpc*( accDB = stateDBFromTag(quantityTag) address = data.ethAddr acc = accDB.getAccount(address) + accExists = accDB.accountExists(address) accountProof = accDB.getAccountProof(address) slotProofs = accDB.getStorageProof(address, slots) @@ -530,14 +531,20 @@ proc setupEthRpc*( value: slotValue, proof: seq[RlpEncodedBytes](slotProofs[i]))) - return ProofResponse( - address: w3Addr(address), - accountProof: seq[RlpEncodedBytes](accountProof), - balance: acc.balance, - nonce: w3Qty(acc.nonce), - codeHash: w3Hash(acc.codeHash), - storageHash: w3Hash(acc.storageRoot), - storageProof: storage) + if accExists: + ProofResponse( + address: w3Addr(address), + accountProof: seq[RlpEncodedBytes](accountProof), + balance: acc.balance, + nonce: w3Qty(acc.nonce), + codeHash: w3Hash(acc.codeHash), + storageHash: w3Hash(acc.storageRoot), + storageProof: storage) + else: + ProofResponse( + address: w3Addr(address), + accountProof: seq[RlpEncodedBytes](accountProof), + storageProof: storage) #[ server.rpc("eth_newFilter") do(filterOptions: FilterOptions) -> int: diff --git a/tests/test_rpc.nim b/tests/test_rpc.nim index 0a85d6893b..29e466723a 100644 --- a/tests/test_rpc.nim +++ b/tests/test_rpc.nim @@ -47,6 +47,9 @@ func w3Addr(x: string): Web3Address = func w3Hash(x: string): Web3Hash = Web3Hash hexToByteArray[32](x) +func zeroHash(): Web3Hash = + w3Hash("0x0000000000000000000000000000000000000000000000000000000000000000") + func emptyCodeHash(): Web3Hash = w3Hash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") @@ -589,9 +592,9 @@ proc rpcMain*() = proofResponse.address == address verifyAccountProof(blockData.stateRoot, proofResponse).isMissing() proofResponse.balance == 0.u256 - proofResponse.codeHash == emptyCodeHash() + proofResponse.codeHash == zeroHash() proofResponse.nonce == w3Qty(0.uint64) - proofResponse.storageHash == emptyStorageHash() + proofResponse.storageHash == zeroHash() storageProof.len() == 0 block: @@ -692,7 +695,7 @@ proc rpcMain*() = let blockData = await client.eth_getBlockByNumber("latest", true) block: - # block 0 - empty account + # block 0 - account doesn't exist yet let address = w3Addr("0x0000000000000000000000000000000000000002") slot1Key = 100.u256 @@ -703,9 +706,9 @@ proc rpcMain*() = proofResponse.address == address verifyAccountProof(blockData.stateRoot, proofResponse).kind == InvalidProof proofResponse.balance == 0.u256 - proofResponse.codeHash == emptyCodeHash() + proofResponse.codeHash == zeroHash() proofResponse.nonce == w3Qty(0.uint64) - proofResponse.storageHash == emptyStorageHash() + proofResponse.storageHash == zeroHash() storageProof.len() == 1 verifySlotProof(proofResponse.storageHash, storageProof[0]).kind == InvalidProof