2024-01-22 09:11:37 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2024 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[os, sequtils],
|
|
|
|
unittest2,
|
|
|
|
stew/byteutils,
|
|
|
|
web3/eth_api,
|
|
|
|
nimcrypto/[keccak, hash],
|
2024-10-16 01:34:12 +00:00
|
|
|
eth/common/[keys, eth_types_rlp],
|
|
|
|
eth/[rlp, trie/trie_defs, trie/hexary_proof_verification],
|
2024-01-22 09:11:37 +00:00
|
|
|
../nimbus/db/[ledger, core_db],
|
|
|
|
../nimbus/common/chain_config,
|
|
|
|
../nimbus/rpc/p2p
|
|
|
|
|
|
|
|
type
|
2024-10-16 01:34:12 +00:00
|
|
|
Hash32 = eth_types.Hash32
|
2024-09-29 12:37:09 +00:00
|
|
|
Address = primitives.Address
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
template toHash32(hash: untyped): Hash32 =
|
|
|
|
fromHex(Hash32, hash.toHex())
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc verifyAccountProof(trustedStateRoot: Hash32, res: ProofResponse): MptProofVerificationResult =
|
2024-01-22 09:11:37 +00:00
|
|
|
let
|
2024-10-16 01:34:12 +00:00
|
|
|
key = toSeq(keccakHash(res.address).data)
|
2024-01-22 09:11:37 +00:00
|
|
|
value = rlp.encode(Account(
|
|
|
|
nonce: res.nonce.uint64,
|
|
|
|
balance: res.balance,
|
2024-10-16 01:34:12 +00:00
|
|
|
storageRoot: res.storageHash.toHash32(),
|
|
|
|
codeHash: res.codeHash.toHash32()))
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
verifyMptProof(
|
|
|
|
seq[seq[byte]](res.accountProof),
|
|
|
|
trustedStateRoot,
|
|
|
|
key,
|
|
|
|
value)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc verifySlotProof(trustedStorageRoot: Hash32, slot: StorageProof): MptProofVerificationResult =
|
2024-01-22 09:11:37 +00:00
|
|
|
let
|
|
|
|
key = toSeq(keccakHash(toBytesBE(slot.key)).data)
|
|
|
|
value = rlp.encode(slot.value)
|
|
|
|
|
|
|
|
verifyMptProof(
|
|
|
|
seq[seq[byte]](slot.proof),
|
|
|
|
trustedStorageRoot,
|
|
|
|
key,
|
|
|
|
value)
|
|
|
|
|
|
|
|
proc getGenesisAlloc(filePath: string): GenesisAlloc =
|
|
|
|
var cn: NetworkParams
|
|
|
|
if not loadNetworkParams(filePath, cn):
|
|
|
|
quit(1)
|
|
|
|
|
|
|
|
cn.genesis.alloc
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc setupStateDB(genAccounts: GenesisAlloc, stateDB: LedgerRef): Hash32 =
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
for address, genAccount in genAccounts:
|
|
|
|
for slotKey, slotValue in genAccount.storage:
|
|
|
|
stateDB.setStorage(address, slotKey, slotValue)
|
|
|
|
|
|
|
|
stateDB.setNonce(address, genAccount.nonce)
|
|
|
|
stateDB.setCode(address, genAccount.code)
|
|
|
|
stateDB.setBalance(address, genAccount.balance)
|
|
|
|
|
|
|
|
stateDB.persist()
|
|
|
|
|
|
|
|
stateDB.rootHash
|
|
|
|
|
|
|
|
proc checkProofsForExistingLeafs(
|
|
|
|
genAccounts: GenesisAlloc,
|
2024-06-16 03:21:02 +00:00
|
|
|
accDB: LedgerRef,
|
2024-10-16 01:34:12 +00:00
|
|
|
stateRoot: Hash32) =
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
for address, account in genAccounts:
|
|
|
|
var slots = newSeq[UInt256]()
|
|
|
|
for k in account.storage.keys():
|
|
|
|
slots.add(k)
|
|
|
|
|
|
|
|
let
|
|
|
|
proofResponse = getProof(accDB, address, slots)
|
|
|
|
slotProofs = proofResponse.storageProof
|
|
|
|
|
|
|
|
check:
|
|
|
|
proofResponse.balance == account.balance
|
2024-10-16 01:34:12 +00:00
|
|
|
proofResponse.codeHash.toHash32() == accDB.getCodeHash(address)
|
|
|
|
proofResponse.storageHash.toHash32() == accDB.getStorageRoot(address)
|
2024-01-22 09:11:37 +00:00
|
|
|
verifyAccountProof(stateRoot, proofResponse).isValid()
|
|
|
|
slotProofs.len() == account.storage.len()
|
|
|
|
|
|
|
|
for i, slotProof in slotProofs:
|
|
|
|
check:
|
|
|
|
slotProof.key == slots[i]
|
|
|
|
slotProof.value == account.storage[slotProof.key]
|
2024-10-16 01:34:12 +00:00
|
|
|
verifySlotProof(proofResponse.storageHash.toHash32(), slotProof).isValid()
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
proc checkProofsForMissingLeafs(
|
|
|
|
genAccounts: GenesisAlloc,
|
2024-06-16 03:21:02 +00:00
|
|
|
accDB: LedgerRef,
|
2024-10-16 01:34:12 +00:00
|
|
|
stateRoot: Hash32) =
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
let
|
2024-10-16 01:34:12 +00:00
|
|
|
missingAddress = Address.fromHex("0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E")
|
2024-01-22 09:11:37 +00:00
|
|
|
proofResponse = getProof(accDB, missingAddress, @[])
|
|
|
|
check verifyAccountProof(stateRoot, proofResponse).isMissing()
|
|
|
|
|
|
|
|
for address, account in genAccounts:
|
|
|
|
let
|
|
|
|
missingSlot = u256("987654321123456676466544")
|
|
|
|
proofResponse2 = getProof(accDB, address, @[missingSlot])
|
|
|
|
slotProofs = proofResponse2.storageProof
|
|
|
|
|
|
|
|
check slotProofs.len() == 1
|
|
|
|
if account.storage.len() > 0:
|
2024-10-16 01:34:12 +00:00
|
|
|
check verifySlotProof(proofResponse2.storageHash.toHash32(), slotProofs[0]).isMissing()
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
proc getProofJsonMain*() =
|
|
|
|
suite "Get proof json tests":
|
|
|
|
|
|
|
|
let genesisFiles = ["berlin2000.json", "chainid1.json", "chainid7.json", "merge.json", "devnet4.json", "devnet5.json", "holesky.json"]
|
|
|
|
|
|
|
|
test "Get proofs for existing leafs":
|
|
|
|
for file in genesisFiles:
|
|
|
|
|
|
|
|
let
|
|
|
|
accounts = getGenesisAlloc("tests" / "customgenesis" / file)
|
2024-05-20 10:17:51 +00:00
|
|
|
coreDb = newCoreDbRef(DefaultDbMemory)
|
2024-05-29 11:06:49 +00:00
|
|
|
accountsCache = LedgerRef.init(coreDb, emptyRlpHash)
|
2024-01-22 09:11:37 +00:00
|
|
|
stateRootHash = setupStateDB(accounts, accountsCache)
|
2024-06-16 03:21:02 +00:00
|
|
|
accountDb = LedgerRef.init(coreDb, stateRootHash)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-16 03:21:02 +00:00
|
|
|
checkProofsForExistingLeafs(accounts, accountDb, stateRootHash)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
test "Get proofs for missing leafs":
|
|
|
|
for file in genesisFiles:
|
|
|
|
|
|
|
|
let
|
|
|
|
accounts = getGenesisAlloc("tests" / "customgenesis" / file)
|
2024-05-20 10:17:51 +00:00
|
|
|
coreDb = newCoreDbRef(DefaultDbMemory)
|
2024-05-29 11:06:49 +00:00
|
|
|
accountsCache = LedgerRef.init(coreDb, emptyRlpHash)
|
2024-01-22 09:11:37 +00:00
|
|
|
stateRootHash = setupStateDB(accounts, accountsCache)
|
2024-06-16 03:21:02 +00:00
|
|
|
accountDb = LedgerRef.init(coreDb, stateRootHash)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-16 03:21:02 +00:00
|
|
|
checkProofsForMissingLeafs(accounts, accountDb, stateRootHash)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
2024-05-20 10:17:51 +00:00
|
|
|
getProofJsonMain()
|