nimbus-eth1/nimbus/db/ledger/backend/accounts_ledger.nim

236 lines
6.6 KiB
Nim
Raw Normal View History

# Nimbus
# Copyright (c) 2023-2024 Status Research & Development GmbH
# 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.
{.push raises: [].}
import
eth/common,
../../../../stateless/multi_keys,
../../core_db,
../base/base_desc,
../accounts_ledger as impl,
".."/[base, distinct_ledgers],
./accounts_ledger_desc as wrp
# ------------------------------------------------------------------------------
# Private functions
# ------------------------------------------------------------------------------
func savePoint(sp: LedgerSpRef): impl.LedgerSavePoint =
wrp.LedgerSavePoint(sp).sp
# ----------------
proc ledgerMethods(lc: impl.AccountsLedgerRef): LedgerFns =
LedgerFns(
accessListFn: proc(eAddr: EthAddress) =
lc.accessList(eAddr),
accessList2Fn: proc(eAddr: EthAddress, slot: UInt256) =
lc.accessList(eAddr, slot),
accountExistsFn: proc(eAddr: EthAddress): bool =
lc.accountExists(eAddr),
addBalanceFn: proc(eAddr: EthAddress, delta: UInt256) =
lc.addBalance(eAddr, delta),
addLogEntryFn: proc(log: Log) =
lc.addLogEntry(log),
beginSavepointFn: proc(): LedgerSpRef =
wrp.LedgerSavePoint(sp: lc.beginSavepoint()),
clearStorageFn: proc(eAddr: EthAddress) =
lc.clearStorage(eAddr),
clearTransientStorageFn: proc() =
lc.clearTransientStorage(),
collectWitnessDataFn: proc() =
lc.collectWitnessData(),
commitFn: proc(sp: LedgerSpRef) =
lc.commit(sp.savePoint),
deleteAccountFn: proc(eAddr: EthAddress) =
lc.deleteAccount(eAddr),
disposeFn: proc(sp: LedgerSpRef) =
lc.dispose(sp.savePoint),
getAndClearLogEntriesFn: proc(): seq[Log] =
lc.getAndClearLogEntries(),
getBalanceFn: proc(eAddr: EthAddress): UInt256 =
lc.getBalance(eAddr),
getCodeFn: proc(eAddr: EthAddress): Blob =
lc.getCode(eAddr),
getCodeHashFn: proc(eAddr: EthAddress): Hash256 =
lc.getCodeHash(eAddr),
getCodeSizeFn: proc(eAddr: EthAddress): int =
lc.getCodeSize(eAddr),
getCommittedStorageFn: proc(eAddr: EthAddress, slot: UInt256): UInt256 =
lc.getCommittedStorage(eAddr, slot),
getNonceFn: proc(eAddr: EthAddress): AccountNonce =
lc.getNonce(eAddr),
getStorageFn: proc(eAddr: EthAddress, slot: UInt256): UInt256 =
lc.getStorage(eAddr, slot),
getStorageRootFn: proc(eAddr: EthAddress): Hash256 =
lc.getStorageRoot(eAddr),
getTransientStorageFn: proc(eAddr: EthAddress, slot: UInt256): UInt256 =
lc.getTransientStorage(eAddr, slot),
hasCodeOrNonceFn: proc(eAddr: EthAddress): bool =
lc.hasCodeOrNonce(eAddr),
inAccessListFn: proc(eAddr: EthAddress): bool =
lc.inAccessList(eAddr),
inAccessList2Fn: proc(eAddr: EthAddress, slot: UInt256): bool =
lc.inAccessList(eAddr, slot),
incNonceFn: proc(eAddr: EthAddress) =
lc.incNonce(eAddr),
isDeadAccountFn: proc(eAddr: EthAddress): bool =
lc.isDeadAccount(eAddr),
isEmptyAccountFn: proc(eAddr: EthAddress): bool =
lc.isEmptyAccount(eAddr),
isTopLevelCleanFn: proc(): bool =
lc.isTopLevelClean(),
logEntriesFn: proc(): seq[Log] =
lc.logEntries(),
makeMultiKeysFn: proc(): MultiKeysRef =
lc.makeMultiKeys(),
persistFn: proc(clearEmptyAccount: bool, clearCache: bool) =
lc.persist(clearEmptyAccount, clearCache),
ripemdSpecialFn: proc() =
lc.ripemdSpecial(),
rollbackFn: proc(sp: LedgerSpRef) =
lc.rollback(sp.savePoint),
rootHashFn: proc(): Hash256 =
lc.rootHash(),
safeDisposeFn: proc(sp: LedgerSpRef) =
lc.safeDispose(sp.savePoint),
selfDestructFn: proc(eAddr: EthAddress) =
lc.selfDestruct(eAddr),
selfDestruct6780Fn: proc(eAddr: EthAddress) =
lc.selfDestruct6780(eAddr),
selfDestructLenFn: proc(): int =
lc.selfDestructLen(),
setBalanceFn: proc(eAddr: EthAddress, balance: UInt256) =
lc.setBalance(eAddr, balance),
setCodeFn: proc(eAddr: EthAddress, code: Blob) =
lc.setCode(eAddr, code),
setNonceFn: proc(eAddr: EthAddress, nonce: AccountNonce) =
lc.setNonce(eAddr, nonce),
setStorageFn: proc(eAddr: EthAddress, slot, val: UInt256) =
lc.setStorage(eAddr, slot, val),
setTransientStorageFn: proc(eAddr: EthAddress, slot, val: UInt256) =
lc.setTransientStorage(eAddr, slot, val),
subBalanceFn: proc(eAddr: EthAddress, delta: UInt256) =
lc.subBalance(eAddr, delta),
getAccessListFn: proc(): common.AccessList =
lc.getAccessList())
proc ledgerExtras(lc: impl.AccountsLedgerRef): LedgerExtras =
LedgerExtras(
Optional accounts cache module for creating genesis (#1897) * Split off `ReadOnlyStateDB` from `AccountStateDB` from `state_db.nim` why: Apart from testing, applications use `ReadOnlyStateDB` as an easy way to access the accounts ledger. This is well supported by the `Aristo` db, but writable mode is only parially supported. The writable AccountStateDB` object for modifying accounts is not used by production code. So, for lecgacy and testing apps, the full support of the previous `AccountStateDB` is now enabled by `import db/state_db/read_write` and the `import db/state_db` provides read-only mode. * Encapsulate `AccountStateDB` as `GenesisLedgerRef` or genesis creation why: `AccountStateDB` has poor support for `Aristo` and is not widely used in favour of `AccountsLedger` (which will be abstracted as `ledger`.) Currently, using other than the `AccountStateDB` ledgers within the `GenesisLedgerRef` wrapper is experimental and test only. Eventually, the wrapper should disappear so that the `Ledger` object (which encapsulates `AccountsCache` and `AccountsLedger`) will prevail. * For the `Ledger`, provide access to raw accounts `MPT` why: This gives to the `CoreDbMptRef` descriptor from the `CoreDb` (which is the legacy version of CoreDxMptRef`.) For the new `ledger` API, the accounts are based on the `CoreDxMAccRef` descriptor which uses a particular sub-system for accounts while legacy applications use the `CoreDbPhkRef` equivalent of the `SecureHexaryTrie`. The only place where this feature will currently be used is the `genesis.nim` source file. * Fix `Aristo` bugs, missing boundary checks, typos, etc. * Verify root vertex in `MPT` and account constructors why: Was missing so far, in particular the accounts constructor must verify `VertexID(1) * Fix include file
2023-11-20 11:51:43 +00:00
getMptFn: proc(): CoreDbMptRef =
lc.rawTrie.CoreDxAccRef.getMpt.CoreDbMptRef,
Optional accounts cache module for creating genesis (#1897) * Split off `ReadOnlyStateDB` from `AccountStateDB` from `state_db.nim` why: Apart from testing, applications use `ReadOnlyStateDB` as an easy way to access the accounts ledger. This is well supported by the `Aristo` db, but writable mode is only parially supported. The writable AccountStateDB` object for modifying accounts is not used by production code. So, for lecgacy and testing apps, the full support of the previous `AccountStateDB` is now enabled by `import db/state_db/read_write` and the `import db/state_db` provides read-only mode. * Encapsulate `AccountStateDB` as `GenesisLedgerRef` or genesis creation why: `AccountStateDB` has poor support for `Aristo` and is not widely used in favour of `AccountsLedger` (which will be abstracted as `ledger`.) Currently, using other than the `AccountStateDB` ledgers within the `GenesisLedgerRef` wrapper is experimental and test only. Eventually, the wrapper should disappear so that the `Ledger` object (which encapsulates `AccountsCache` and `AccountsLedger`) will prevail. * For the `Ledger`, provide access to raw accounts `MPT` why: This gives to the `CoreDbMptRef` descriptor from the `CoreDb` (which is the legacy version of CoreDxMptRef`.) For the new `ledger` API, the accounts are based on the `CoreDxMAccRef` descriptor which uses a particular sub-system for accounts while legacy applications use the `CoreDbPhkRef` equivalent of the `SecureHexaryTrie`. The only place where this feature will currently be used is the `genesis.nim` source file. * Fix `Aristo` bugs, missing boundary checks, typos, etc. * Verify root vertex in `MPT` and account constructors why: Was missing so far, in particular the accounts constructor must verify `VertexID(1) * Fix include file
2023-11-20 11:51:43 +00:00
rawRootHashFn: proc(): Hash256 =
lc.rawTrie.rootHash())
proc newAccountsLedgerRef(
db: CoreDbRef;
root: Hash256;
pruneTrie: bool): LedgerRef =
let lc = impl.AccountsLedgerRef.init(db, root, pruneTrie)
wrp.AccountsLedgerRef(
ldgType: LedgerCache,
ac: lc,
extras: lc.ledgerExtras(),
methods: lc.ledgerMethods()).bless db
# ------------------------------------------------------------------------------
# Public iterators
# ------------------------------------------------------------------------------
iterator accountsIt*(lc: wrp.AccountsLedgerRef): Account =
for w in lc.ac.accounts():
yield w
iterator addressesIt*(lc: wrp.AccountsLedgerRef): EthAddress =
for w in lc.ac.addresses():
yield w
iterator cachedStorageIt*(
lc: wrp.AccountsLedgerRef;
eAddr: EthAddress;
): (UInt256,UInt256) =
for w in lc.ac.cachedStorage(eAddr):
yield w
iterator pairsIt*(lc: wrp.AccountsLedgerRef): (EthAddress,Account) =
for w in lc.ac.pairs():
yield w
iterator storageIt*(
lc: wrp.AccountsLedgerRef;
eAddr: EthAddress;
): (UInt256,UInt256)
{.gcsafe, raises: [CoreDbApiError].} =
for w in lc.ac.storage(eAddr):
yield w
# ------------------------------------------------------------------------------
# Public constructor
# ------------------------------------------------------------------------------
proc init*(
T: type wrp.AccountsLedgerRef;
db: CoreDbRef;
root: Hash256;
pruneTrie: bool): LedgerRef =
db.newAccountsLedgerRef(root, pruneTrie)
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------