Skip to content

Commit

Permalink
Return zero hash for codeHash and storageHash if account doesn't exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhartnett committed Jan 8, 2024
1 parent 2375dfb commit 3f11e22
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
23 changes: 15 additions & 8 deletions nimbus/rpc/p2p.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand Down
13 changes: 8 additions & 5 deletions tests/test_rpc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 3f11e22

Please sign in to comment.