Make test op memory work again (#2236)

* Remove crufty `pruneTrie` arguments

* Replaced legacy `distinct_trie` logic by new `ledger` functionality

why:
  The module `distinct_trie` is supported by `Aristo` in trivial cases.

* Activate `test_op_memory`
This commit is contained in:
Jordan Hrycaj 2024-05-28 14:24:10 +00:00 committed by GitHub
parent 08e98eb385
commit 3a62250d04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 32 deletions

View File

@ -137,9 +137,9 @@ template noRlpException(info: static[string]; code: untyped) =
# The AccountsLedgerRef is modeled after TrieDatabase for it's transaction style # The AccountsLedgerRef is modeled after TrieDatabase for it's transaction style
proc init*(x: typedesc[AccountsLedgerRef], db: CoreDbRef, proc init*(x: typedesc[AccountsLedgerRef], db: CoreDbRef,
root: KeccakHash, pruneTrie = true): AccountsLedgerRef = root: KeccakHash): AccountsLedgerRef =
new result new result
result.ledger = AccountLedger.init(db, root, pruneTrie) result.ledger = AccountLedger.init(db, root)
result.kvt = db.newKvt() # save manually in `persist()` result.kvt = db.newKvt() # save manually in `persist()`
result.witnessCache = initTable[EthAddress, WitnessData]() result.witnessCache = initTable[EthAddress, WitnessData]()
discard result.beginSavepoint discard result.beginSavepoint

View File

@ -119,19 +119,18 @@ proc init*(
T: type AccountLedger; T: type AccountLedger;
db: CoreDbRef; db: CoreDbRef;
root: Hash256; root: Hash256;
pruneOk = true;
): T = ): T =
const const
info = "AccountLedger.init(): " info = "AccountLedger.init(): "
let let
ctx = db.ctx ctx = db.ctx
trie = block: col = block:
let rc = ctx.newColumn(CtAccounts, root) let rc = ctx.newColumn(CtAccounts, root)
if rc.isErr: if rc.isErr:
raiseAssert info & $$rc.error raiseAssert info & $$rc.error
rc.value rc.value
mpt = block: mpt = block:
let rc = ctx.getAcc(trie) let rc = ctx.getAcc(col)
if rc.isErr: if rc.isErr:
raiseAssert info & $$rc.error raiseAssert info & $$rc.error
rc.value rc.value
@ -170,14 +169,9 @@ proc init*(
al: AccountLedger; al: AccountLedger;
account: CoreDbAccount; account: CoreDbAccount;
reHashOk = true; reHashOk = true;
pruneOk = false;
): T = ): T =
## Storage trie constructor. ## Storage trie constructor.
## ##
## Note that the argument `pruneOk` should be left `false` on the legacy
## `CoreDb` backend. Otherwise, pruning might kill some unwanted entries from
## storage tries ending up with an unstable database leading to crashes (see
## https://github.com/status-im/nimbus-eth1/issues/932.)
const const
info = "StorageLedger/init(): " info = "StorageLedger/init(): "
let let
@ -191,7 +185,7 @@ proc init*(
ctx = db.ctx ctx = db.ctx
trie = if stt.isNil: ctx.newColumn(account.address) else: stt trie = if stt.isNil: ctx.newColumn(account.address) else: stt
mpt = block: mpt = block:
let rc = ctx.getMpt(trie, pruneOk) let rc = ctx.getMpt(trie)
if rc.isErr: if rc.isErr:
raiseAssert info & $$rc.error raiseAssert info & $$rc.error
rc.value rc.value

View File

@ -28,7 +28,7 @@ cliBuilder:
./test_op_arith, ./test_op_arith,
./test_op_bit, ./test_op_bit,
./test_op_env, ./test_op_env,
#./test_op_memory, -- fails ./test_op_memory,
./test_op_misc, ./test_op_misc,
./test_op_custom, ./test_op_custom,
#./test_state_db, -- does not compile #./test_state_db, -- does not compile

View File

@ -17,13 +17,16 @@ import
stew/shims/macros stew/shims/macros
import import
../nimbus/db/[ledger, distinct_tries], ../nimbus/db/ledger,
../nimbus/evm/types, ../nimbus/evm/types,
../nimbus/vm_internals, ../nimbus/vm_internals,
../nimbus/transaction/[call_common, call_evm], ../nimbus/transaction/[call_common, call_evm],
../nimbus/[vm_types, vm_state], ../nimbus/[vm_types, vm_state],
../nimbus/core/pow/difficulty ../nimbus/core/pow/difficulty
from ../nimbus/db/aristo
import EmptyBlob
# Ditto, for GasPrice. # Ditto, for GasPrice.
import ../nimbus/transaction except GasPrice import ../nimbus/transaction except GasPrice
import ../tools/common/helpers except LogLevel import ../tools/common/helpers except LogLevel
@ -63,15 +66,6 @@ type
const const
idToOpcode = CacheTable"NimbusMacroAssembler" idToOpcode = CacheTable"NimbusMacroAssembler"
var
coreDbType* = DefaultDbMemory
## This variable needs to be accessible for unit tests like
## `test_op_memory` which implicitely uses the `initStorageTrie()` call
## from the `distinct_tries` module. The `Aristo` API cannot handle that
## because it needs the account address for accessing the storage trie.
##
## This problem can be fixed here in the `verifyAsmResult()` function once
## there is the time to do it ...
static: static:
for n in Op: for n in Op:
@ -278,11 +272,7 @@ const
proc initVMEnv*(network: string): BaseVMState = proc initVMEnv*(network: string): BaseVMState =
let let
conf = getChainConfig(network) conf = getChainConfig(network)
cdb = block: cdb = DefaultDbMemory.newCoreDbRef()
# Need static binding
case coreDbType:
of AristoDbMemory: newCoreDbRef AristoDbMemory
else: raiseAssert "unsupported: " & $coreDbType
com = CommonRef.new( com = CommonRef.new(
cdb, cdb,
conf, conf,
@ -345,15 +335,18 @@ proc verifyAsmResult(vmState: BaseVMState, boa: Assembler, asmResult: CallResult
var stateDB = vmState.stateDB var stateDB = vmState.stateDB
stateDB.persist() stateDB.persist()
var
storageRoot = stateDB.getStorageRoot(codeAddress) let
trie = initStorageTrie(com.db, storageRoot) al = AccountLedger.init(com.db, EMPTY_ROOT_HASH)
acc = al.fetch(codeAddress).expect "Valid Account Handle"
sl = StorageLedger.init(al, acc)
for kv in boa.storage: for kv in boa.storage:
let key = kv[0].toHex() let key = kv[0].toHex()
let val = kv[1].toHex() let val = kv[1].toHex()
let keyBytes = (@(kv[0])) let slot = UInt256.fromBytesBE kv[0]
let actual = trie.getSlotBytes(keyBytes).toHex() let data = sl.fetch(slot).valueOr: EmptyBlob
let actual = data.toHex
let zerosLen = 64 - (actual.len) let zerosLen = 64 - (actual.len)
let value = repeat('0', zerosLen) & actual let value = repeat('0', zerosLen) & actual
if val != value: if val != value: