2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
2024-01-24 17:18:45 +00:00
|
|
|
# Copyright (c) 2020-2024 Status Research & Development GmbH
|
2023-11-01 03:32:09 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2020-04-21 10:21:06 +00:00
|
|
|
import
|
|
|
|
unittest2, os, json, strutils,
|
2023-08-04 11:10:09 +00:00
|
|
|
eth/[common, rlp], eth/trie/trie_defs,
|
2023-01-31 12:38:08 +00:00
|
|
|
stew/byteutils,
|
2020-04-28 02:30:10 +00:00
|
|
|
../tests/[test_helpers, test_config],
|
2023-12-12 19:12:56 +00:00
|
|
|
../nimbus/db/[ledger, core_db, distinct_tries], ./witness_types,
|
2020-05-05 06:50:31 +00:00
|
|
|
../stateless/[witness_from_tree, tree_from_witness],
|
|
|
|
./multi_keys
|
2020-04-21 10:21:06 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Tester = object
|
2024-02-21 16:04:59 +00:00
|
|
|
keys: MultiKeysRef
|
2023-08-04 11:10:09 +00:00
|
|
|
memDB: CoreDbRef
|
2020-04-21 10:21:06 +00:00
|
|
|
|
|
|
|
proc testGetBranch(tester: Tester, rootHash: KeccakHash, testStatusIMPL: var TestStatus) =
|
2024-02-21 16:04:59 +00:00
|
|
|
var trie = initAccountsTrie(tester.memDB, rootHash)
|
2024-01-24 17:18:45 +00:00
|
|
|
let flags = {wfNoFlag}
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-29 05:46:50 +00:00
|
|
|
try:
|
2024-02-21 16:04:59 +00:00
|
|
|
var wb = initWitnessBuilder(tester.memDB, rootHash, flags)
|
2020-05-05 06:50:31 +00:00
|
|
|
var witness = wb.buildWitness(tester.keys)
|
2020-04-29 05:46:50 +00:00
|
|
|
|
2024-05-20 10:17:51 +00:00
|
|
|
var db = newCoreDbRef(DefaultDbMemory)
|
2020-05-05 06:50:31 +00:00
|
|
|
when defined(useInputStream):
|
|
|
|
var input = memoryInput(witness)
|
|
|
|
var tb = initTreeBuilder(input, db, flags)
|
|
|
|
else:
|
|
|
|
var tb = initTreeBuilder(witness, db, flags)
|
2020-04-29 05:46:50 +00:00
|
|
|
|
2020-05-05 06:50:31 +00:00
|
|
|
var root = tb.buildTree()
|
|
|
|
check root.data == rootHash.data
|
2020-05-03 01:11:04 +00:00
|
|
|
|
2023-03-10 18:42:37 +00:00
|
|
|
let newTrie = initAccountsTrie(tb.getDB(), root)
|
2020-05-05 08:21:39 +00:00
|
|
|
for kd in tester.keys.keys:
|
2023-03-10 18:42:37 +00:00
|
|
|
let account = rlp.decode(trie.getAccountBytes(kd.address), Account)
|
|
|
|
let recordFound = newTrie.getAccountBytes(kd.address)
|
2020-05-03 01:11:04 +00:00
|
|
|
if recordFound.len > 0:
|
|
|
|
let acc = rlp.decode(recordFound, Account)
|
|
|
|
doAssert acc == account
|
|
|
|
else:
|
2020-05-13 23:58:34 +00:00
|
|
|
doAssert(false, "BUG IN WITNESS/TREE BUILDER")
|
2020-05-03 01:11:04 +00:00
|
|
|
|
2020-04-29 05:46:50 +00:00
|
|
|
except ContractCodeError as e:
|
|
|
|
debugEcho "CONTRACT CODE ERROR: ", e.msg
|
2020-04-21 10:21:06 +00:00
|
|
|
|
|
|
|
func parseHash256(n: JsonNode, name: string): Hash256 =
|
|
|
|
hexToByteArray(n[name].getStr(), result.data)
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
proc setupStateDB(tester: var Tester, wantedState: JsonNode, stateDB: LedgerRef): Hash256 =
|
2020-05-05 06:50:31 +00:00
|
|
|
var keys = newSeqOfCap[AccountKey](wantedState.len)
|
|
|
|
|
2020-04-21 10:21:06 +00:00
|
|
|
for ac, accountData in wantedState:
|
|
|
|
let account = ethAddressFromHex(ac)
|
2020-05-05 06:50:31 +00:00
|
|
|
let slotVals = accountData{"storage"}
|
|
|
|
var storageKeys = newSeqOfCap[StorageSlot](slotVals.len)
|
|
|
|
|
|
|
|
for slotStr, value in slotVals:
|
|
|
|
let slot = fromHex(UInt256, slotStr)
|
|
|
|
storageKeys.add(slot.toBytesBE)
|
|
|
|
stateDB.setStorage(account, slot, fromHex(UInt256, value.getStr))
|
|
|
|
|
2020-04-21 10:21:06 +00:00
|
|
|
let nonce = accountData{"nonce"}.getHexadecimalInt.AccountNonce
|
|
|
|
let code = accountData{"code"}.getStr.safeHexToSeqByte
|
|
|
|
let balance = UInt256.fromHex accountData{"balance"}.getStr
|
|
|
|
|
|
|
|
stateDB.setNonce(account, nonce)
|
|
|
|
stateDB.setCode(account, code)
|
|
|
|
stateDB.setBalance(account, balance)
|
|
|
|
|
2024-02-21 16:04:59 +00:00
|
|
|
let sKeys = if storageKeys.len != 0: newMultiKeys(storageKeys) else: MultiKeysRef(nil)
|
2020-05-05 08:05:17 +00:00
|
|
|
let codeTouched = code.len > 0
|
2020-07-09 01:25:18 +00:00
|
|
|
keys.add(AccountKey(address: account, codeTouched: codeTouched, storageKeys: sKeys))
|
2020-05-05 08:05:17 +00:00
|
|
|
|
2020-05-05 06:50:31 +00:00
|
|
|
tester.keys = newMultiKeys(keys)
|
2020-04-21 10:21:06 +00:00
|
|
|
stateDB.persist()
|
|
|
|
result = stateDB.rootHash
|
|
|
|
|
|
|
|
proc testBlockWitness(node: JsonNode, rootHash: Hash256, testStatusIMPL: var TestStatus) =
|
|
|
|
var
|
2024-05-20 10:17:51 +00:00
|
|
|
tester = Tester(memDB: newCoreDbRef(DefaultDbMemory))
|
2024-05-29 11:06:49 +00:00
|
|
|
ac = LedgerRef.init(tester.memDB, emptyRlpHash)
|
2020-04-21 10:21:06 +00:00
|
|
|
|
|
|
|
let root = tester.setupStateDB(node, ac)
|
|
|
|
if rootHash != emptyRlpHash:
|
|
|
|
check root == rootHash
|
|
|
|
|
|
|
|
tester.testGetBranch(root, testStatusIMPL)
|
|
|
|
|
|
|
|
proc testFixtureBC(node: JsonNode, testStatusIMPL: var TestStatus) =
|
|
|
|
for fixtureName, fixture in node:
|
|
|
|
let rootHash = parseHash256(fixture["genesisBlockHeader"], "stateRoot")
|
|
|
|
fixture["pre"].testBlockWitness(rootHash, testStatusIMPL)
|
|
|
|
|
|
|
|
proc testFixtureGST(node: JsonNode, testStatusIMPL: var TestStatus) =
|
|
|
|
var fixture: JsonNode
|
|
|
|
|
|
|
|
for fixtureName, child in node:
|
|
|
|
fixture = child
|
|
|
|
break
|
|
|
|
|
|
|
|
fixture["pre"].testBlockWitness(emptyRlpHash, testStatusIMPL)
|
|
|
|
|
2020-04-28 02:30:10 +00:00
|
|
|
proc blockWitnessMain*(debugMode = false) =
|
2021-01-06 11:47:44 +00:00
|
|
|
const
|
|
|
|
legacyGSTFolder = "eth_tests" / "LegacyTests" / "Constantinople" / "GeneralStateTests"
|
|
|
|
newGSTFolder = "eth_tests" / "GeneralStateTests"
|
2021-01-06 14:45:48 +00:00
|
|
|
newBCFolder = "eth_tests" / "BlockchainTests"
|
2021-01-06 11:47:44 +00:00
|
|
|
|
2020-04-28 02:30:10 +00:00
|
|
|
if paramCount() == 0 or not debugMode:
|
|
|
|
# run all test fixtures
|
|
|
|
suite "Block Witness":
|
2021-01-06 11:47:44 +00:00
|
|
|
jsonTest(newBCFolder, "witnessBuilderBC", testFixtureBC)
|
2020-04-28 02:30:10 +00:00
|
|
|
suite "Block Witness":
|
2021-01-06 11:47:44 +00:00
|
|
|
jsonTest(newGSTFolder, "witnessBuilderGST", testFixtureGST)
|
2020-04-28 02:30:10 +00:00
|
|
|
else:
|
|
|
|
# execute single test in debug mode
|
|
|
|
let config = getConfiguration()
|
|
|
|
if config.testSubject.len == 0:
|
|
|
|
echo "missing test subject"
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
2021-01-06 11:47:44 +00:00
|
|
|
let folder = if config.legacy: legacyGSTFolder else: newGSTFolder
|
2020-04-28 02:30:10 +00:00
|
|
|
let path = "tests" / "fixtures" / folder
|
|
|
|
let n = json.parseFile(path / config.testSubject)
|
|
|
|
var testStatusIMPL: TestStatus
|
2020-04-29 05:46:50 +00:00
|
|
|
testFixtureGST(n, testStatusIMPL)
|
2020-04-28 02:30:10 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
var message: string
|
|
|
|
|
|
|
|
## Processing command line arguments
|
|
|
|
if processArguments(message) != Success:
|
|
|
|
echo message
|
|
|
|
quit(QuitFailure)
|
|
|
|
else:
|
|
|
|
if len(message) > 0:
|
|
|
|
echo message
|
|
|
|
quit(QuitSuccess)
|
|
|
|
blockWitnessMain(true)
|