-
Notifications
You must be signed in to change notification settings - Fork 106
/
test_getproof_json.nim
159 lines (125 loc) · 5.06 KB
/
test_getproof_json.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# 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],
eth/[common, rlp, keys, trie/trie_defs, trie/hexary_proof_verification],
../nimbus/db/state_db,
../nimbus/db/[ledger, core_db],
../nimbus/common/chain_config,
../nimbus/rpc/p2p
type
Hash256 = eth_types.Hash256
ReadOnlyStateDB = state_db.ReadOnlyStateDB
func ethAddr*(x: Address): EthAddress =
EthAddress x
func ethAddr(x: string): EthAddress =
hexToByteArray[20](x)
template toHash256(hash: untyped): Hash256 =
fromHex(Hash256, hash.toHex())
proc verifyAccountProof(trustedStateRoot: Hash256, res: ProofResponse): MptProofVerificationResult =
let
key = toSeq(keccakHash(res.address.ethAddr).data)
value = rlp.encode(Account(
nonce: res.nonce.uint64,
balance: res.balance,
storageRoot: res.storageHash.toHash256(),
codeHash: res.codeHash.toHash256()))
verifyMptProof(
seq[seq[byte]](res.accountProof),
trustedStateRoot,
key,
value)
proc verifySlotProof(trustedStorageRoot: Hash256, slot: StorageProof): MptProofVerificationResult =
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
proc setupStateDB(genAccounts: GenesisAlloc, stateDB: LedgerRef): Hash256 =
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,
accDB: ReadOnlyStateDB,
stateRoot: Hash256) =
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
proofResponse.codeHash.toHash256() == accDB.getCodeHash(address)
proofResponse.storageHash.toHash256() == accDB.getStorageRoot(address)
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]
verifySlotProof(proofResponse.storageHash.toHash256(), slotProof).isValid()
proc checkProofsForMissingLeafs(
genAccounts: GenesisAlloc,
accDB: ReadOnlyStateDB,
stateRoot: Hash256) =
let
missingAddress = ethAddr("0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E")
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:
check verifySlotProof(proofResponse2.storageHash.toHash256(), slotProofs[0]).isMissing()
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)
coreDb = newCoreDbRef(DefaultDbMemory)
accountsCache = LedgerRef.init(coreDb, emptyRlpHash)
stateRootHash = setupStateDB(accounts, accountsCache)
accountDb = newAccountStateDB(coreDb, stateRootHash)
readOnlyDb = ReadOnlyStateDB(accountDb)
checkProofsForExistingLeafs(accounts, readOnlyDb, stateRootHash)
test "Get proofs for missing leafs":
for file in genesisFiles:
let
accounts = getGenesisAlloc("tests" / "customgenesis" / file)
coreDb = newCoreDbRef(DefaultDbMemory)
accountsCache = LedgerRef.init(coreDb, emptyRlpHash)
stateRootHash = setupStateDB(accounts, accountsCache)
accountDb = newAccountStateDB(coreDb, stateRootHash)
readOnlyDb = ReadOnlyStateDB(accountDb)
checkProofsForMissingLeafs(accounts, readOnlyDb, stateRootHash)
when isMainModule:
getProofJsonMain()