From 7722a907dc3e3917211664bc94cb048828cd823d Mon Sep 17 00:00:00 2001 From: andri lim Date: Thu, 29 Feb 2024 18:16:47 +0700 Subject: [PATCH] Implement getAccessList of db/ledger (#2055) --- nimbus/db/access_list.nim | 27 ++++++++++++++++++-- nimbus/db/ledger/accounts_cache.nim | 5 ++++ nimbus/db/ledger/accounts_ledger.nim | 5 ++++ nimbus/db/ledger/backend/accounts_cache.nim | 9 ++++--- nimbus/db/ledger/backend/accounts_ledger.nim | 5 +++- nimbus/db/ledger/base.nim | 5 ++++ nimbus/db/ledger/base/api_tracking.nim | 1 + nimbus/db/ledger/base/base_desc.nim | 2 ++ 8 files changed, 53 insertions(+), 6 deletions(-) diff --git a/nimbus/db/access_list.nim b/nimbus/db/access_list.nim index 54c9a8b8b..7cb5cf30f 100644 --- a/nimbus/db/access_list.nim +++ b/nimbus/db/access_list.nim @@ -1,5 +1,5 @@ # Nimbus -# Copyright (c) 2023 Status Research & Development GmbH +# 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) @@ -9,7 +9,7 @@ # according to those terms. import - tables, sets, + std/[tables, sets], stint, eth/common @@ -19,12 +19,28 @@ type AccessList* = object slots: Table[EthAddress, SlotSet] +# ------------------------------------------------------------------------------ +# Private helpers +# ------------------------------------------------------------------------------ + +func toStorageKeys(slots: SlotSet): seq[StorageKey] = + for slot in slots: + result.add slot.toBytesBE + +# ------------------------------------------------------------------------------ +# Public constructors +# ------------------------------------------------------------------------------ + proc init*(ac: var AccessList) = ac.slots = initTable[EthAddress, SlotSet]() proc init*(_: type AccessList): AccessList {.inline.} = result.init() +# ------------------------------------------------------------------------------ +# Public functions +# ------------------------------------------------------------------------------ + func contains*(ac: AccessList, address: EthAddress): bool {.inline.} = address in ac.slots @@ -52,3 +68,10 @@ proc add*(ac: var AccessList, address: EthAddress, slot: UInt256) = proc clear*(ac: var AccessList) {.inline.} = ac.slots.clear() + +func getAccessList*(ac: AccessList): common.AccessList = + for address, slots in ac.slots: + result.add common.AccessPair( + address : address, + storageKeys: slots.toStorageKeys, + ) diff --git a/nimbus/db/ledger/accounts_cache.nim b/nimbus/db/ledger/accounts_cache.nim index 28cf5db64..ec0d3c6a6 100644 --- a/nimbus/db/ledger/accounts_cache.nim +++ b/nimbus/db/ledger/accounts_cache.nim @@ -734,6 +734,11 @@ proc clearTransientStorage*(ac: AccountsCache) {.inline.} = doAssert(ac.savePoint.parentSavepoint.isNil) ac.savePoint.transientStorage.clear() +func getAccessList*(ac: AccountsCache): common.AccessList = + # make sure all savepoint already committed + doAssert(ac.savePoint.parentSavepoint.isNil) + ac.savePoint.accessList.getAccessList() + proc rootHash*(db: ReadOnlyStateDB): KeccakHash {.borrow.} proc getCodeHash*(db: ReadOnlyStateDB, address: EthAddress): Hash256 {.borrow.} proc getStorageRoot*(db: ReadOnlyStateDB, address: EthAddress): Hash256 {.borrow.} diff --git a/nimbus/db/ledger/accounts_ledger.nim b/nimbus/db/ledger/accounts_ledger.nim index bcf9b2884..25cabdacb 100644 --- a/nimbus/db/ledger/accounts_ledger.nim +++ b/nimbus/db/ledger/accounts_ledger.nim @@ -769,6 +769,11 @@ proc clearTransientStorage*(ac: AccountsLedgerRef) = doAssert(ac.savePoint.parentSavepoint.isNil) ac.savePoint.transientStorage.clear() +func getAccessList*(ac: AccountsLedgerRef): common.AccessList = + # make sure all savepoint already committed + doAssert(ac.savePoint.parentSavepoint.isNil) + ac.savePoint.accessList.getAccessList() + proc rootHash*(db: ReadOnlyStateDB): KeccakHash {.borrow.} proc getCodeHash*(db: ReadOnlyStateDB, address: EthAddress): Hash256 {.borrow.} proc getStorageRoot*(db: ReadOnlyStateDB, address: EthAddress): Hash256 {.borrow.} diff --git a/nimbus/db/ledger/backend/accounts_cache.nim b/nimbus/db/ledger/backend/accounts_cache.nim index 10993566c..f3822860a 100644 --- a/nimbus/db/ledger/backend/accounts_cache.nim +++ b/nimbus/db/ledger/backend/accounts_cache.nim @@ -32,7 +32,7 @@ func savePoint(sp: LedgerSpRef): impl.SavePoint = wrp.SavePoint(sp).sp # ---------------- - + proc ledgerMethods(lc: impl.AccountsCache): LedgerFns = LedgerFns( accessListFn: proc(eAddr: EthAddress) = @@ -52,7 +52,7 @@ proc ledgerMethods(lc: impl.AccountsCache): LedgerFns = beginSavepointFn: proc(): LedgerSpRef = wrp.SavePoint(sp: lc.beginSavepoint()), - + clearStorageFn: proc(eAddr: EthAddress) = lc.clearStorage(eAddr), @@ -176,7 +176,10 @@ proc ledgerMethods(lc: impl.AccountsCache): LedgerFns = lc.setTransientStorage(eAddr, slot, val), subBalanceFn: proc(eAddr: EthAddress, delta: UInt256) = - lc.subBalance(eAddr, delta)) + lc.subBalance(eAddr, delta), + + getAccessListFn: proc(): common.AccessList = + lc.getAccessList()) proc ledgerExtras(lc: impl.AccountsCache): LedgerExtras = LedgerExtras( diff --git a/nimbus/db/ledger/backend/accounts_ledger.nim b/nimbus/db/ledger/backend/accounts_ledger.nim index 9d0cac0b6..d5a003e8c 100644 --- a/nimbus/db/ledger/backend/accounts_ledger.nim +++ b/nimbus/db/ledger/backend/accounts_ledger.nim @@ -163,7 +163,10 @@ proc ledgerMethods(lc: impl.AccountsLedgerRef): LedgerFns = lc.setTransientStorage(eAddr, slot, val), subBalanceFn: proc(eAddr: EthAddress, delta: UInt256) = - lc.subBalance(eAddr, delta)) + lc.subBalance(eAddr, delta), + + getAccessListFn: proc(): common.AccessList = + lc.getAccessList()) proc ledgerExtras(lc: impl.AccountsLedgerRef): LedgerExtras = LedgerExtras( diff --git a/nimbus/db/ledger/base.nim b/nimbus/db/ledger/base.nim index b6395c81a..03e666298 100644 --- a/nimbus/db/ledger/base.nim +++ b/nimbus/db/ledger/base.nim @@ -346,6 +346,11 @@ proc subBalance*(ldg: LedgerRef, eAddr: EthAddress, delta: UInt256) = ldg.methods.subBalanceFn(eAddr, delta) ldg.ifTrackApi: debug apiTxt, ctx, elapsed, eAddr, delta +proc getAccessList*(ldg: LedgerRef): AccessList = + ldg.beginTrackApi LdgGetAccessListFn + result = ldg.methods.getAccessListFn() + ldg.ifTrackApi: debug apiTxt, ctx, elapsed + # ------------------------------------------------------------------------------ # Public methods, extensions to go away # ------------------------------------------------------------------------------ diff --git a/nimbus/db/ledger/base/api_tracking.nim b/nimbus/db/ledger/base/api_tracking.nim index a8aa402ba..ee7acb554 100644 --- a/nimbus/db/ledger/base/api_tracking.nim +++ b/nimbus/db/ledger/base/api_tracking.nim @@ -68,6 +68,7 @@ type LdgSubBalanceFn = "subBalance" LdgGetMptFn = "getMpt" LdgRawRootHashFn = "rawRootHash" + LdgGetAccessListFn = "getAcessList" LdgAccountsIt = "accounts" LdgAdressesIt = "addresses" diff --git a/nimbus/db/ledger/base/base_desc.nim b/nimbus/db/ledger/base/base_desc.nim index ccf87fa00..927e2c557 100644 --- a/nimbus/db/ledger/base/base_desc.nim +++ b/nimbus/db/ledger/base/base_desc.nim @@ -89,6 +89,7 @@ type SetTransientStorageFn* = proc(eAddr: EthAddress, slot, val: UInt256) {.noRaise.} SubBalanceFn* = proc(eAddr: EthAddress, delta: UInt256) {.noRaise.} + GetAccessListFn* = proc(): AccessList {.noRaise.} LedgerFns* = object accessListFn*: AccessListFn @@ -136,5 +137,6 @@ type setStorageFn*: SetStorageFn setTransientStorageFn*: SetTransientStorageFn subBalanceFn*: SubBalanceFn + getAccessListFn*: GetAccessListFn # End