forked from status-im/nimbus-eth1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_witness_verification.nim
102 lines (76 loc) · 3.64 KB
/
test_witness_verification.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
# Nimbus
# Copyright (c) 2023-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.
{.push raises: [].}
import
std/os,
unittest2,
stew/results,
eth/[common, trie/trie_defs],
../nimbus/db/[ledger, core_db],
../nimbus/common/chain_config,
./[witness_from_tree, multi_keys, witness_types, witness_verification]
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, MultiKeysRef) =
var keys = newSeqOfCap[AccountKey](genAccounts.len)
for address, genAccount in genAccounts:
var storageKeys = newSeqOfCap[StorageSlot](genAccount.storage.len)
for slotKey, slotValue in genAccount.storage:
storageKeys.add(slotKey.toBytesBE)
stateDB.setStorage(address, slotKey, slotValue)
stateDB.setNonce(address, genAccount.nonce)
stateDB.setCode(address, genAccount.code)
stateDB.setBalance(address, genAccount.balance)
let sKeys = if storageKeys.len != 0: newMultiKeys(storageKeys) else: MultiKeysRef(nil)
let codeTouched = genAccount.code.len > 0
keys.add(AccountKey(address: address, codeTouched: codeTouched, storageKeys: sKeys))
stateDB.persist()
(stateDB.rootHash, newMultiKeys(keys))
proc buildWitness(
genAccounts: GenesisAlloc): (KeccakHash, BlockWitness) {.raises: [CatchableError].} =
let
coreDb = newCoreDbRef(LegacyDbMemory)
accountsCache = AccountsCache.init(coreDb, emptyRlpHash, true)
(rootHash, multiKeys) = setupStateDB(genAccounts, accountsCache)
var wb = initWitnessBuilder(coreDb, rootHash, {wfNoFlag})
(rootHash, wb.buildWitness(multiKeys))
proc checkWitnessDataMatchesAccounts(
genAccounts: GenesisAlloc,
witnessData: TableRef[EthAddress, AccountData]) {.raises: [CatchableError].} =
for address, genAccount in genAccounts:
let accountData = witnessData[address]
check genAccount.code == accountData.code
check genAccount.storage == accountData.storage
check genAccount.balance == accountData.account.balance
check genAccount.nonce == accountData.account.nonce
proc witnessVerificationMain*() =
suite "Witness verification json tests":
let genesisFiles = ["berlin2000.json", "chainid1.json", "chainid7.json", "merge.json", "devnet4.json", "devnet5.json", "holesky.json"]
test "Block witness verification with valid state root":
for file in genesisFiles:
let
accounts = getGenesisAlloc("tests" / "customgenesis" / file)
(stateRoot, witness) = buildWitness(accounts)
verifyResult = verifyWitness(stateRoot, witness, {wfNoFlag})
check verifyResult.isOk()
checkWitnessDataMatchesAccounts(accounts, verifyResult.get())
test "Block witness verification with invalid state root":
let badStateRoot = toDigest("2cb1b80b285d09e0570fdbbb808e1d14e4ac53e36dcd95dbc268deec2915b3e7")
for file in genesisFiles:
let
accounts = getGenesisAlloc("tests" / "customgenesis" / file)
(_, witness) = buildWitness(accounts)
verifyResult = verifyWitness(badStateRoot, witness, {wfNoFlag})
check verifyResult.isErr()
check verifyResult.error() == "witness stateRoot doesn't match trustedStateRoot"
when isMainModule:
witnessVerificationMain()