Skip to content

Commit

Permalink
Implement getAccessList of db/ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Feb 29, 2024
1 parent 66c0f92 commit d2eae45
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 6 deletions.
27 changes: 25 additions & 2 deletions nimbus/db/access_list.nim
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -9,7 +9,7 @@
# according to those terms.

import
tables, sets,
std/[tables, sets],
stint,
eth/common

Expand All @@ -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

Expand Down Expand Up @@ -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,
)
5 changes: 5 additions & 0 deletions nimbus/db/ledger/accounts_cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.}
Expand Down
5 changes: 5 additions & 0 deletions nimbus/db/ledger/accounts_ledger.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.}
Expand Down
9 changes: 6 additions & 3 deletions nimbus/db/ledger/backend/accounts_cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand All @@ -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),

Expand Down Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion nimbus/db/ledger/backend/accounts_ledger.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions nimbus/db/ledger/base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions nimbus/db/ledger/base/api_tracking.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type
LdgSubBalanceFn = "subBalance"
LdgGetMptFn = "getMpt"
LdgRawRootHashFn = "rawRootHash"
LdgGetAccessListFn = "getAcessList"

LdgAccountsIt = "accounts"
LdgAdressesIt = "addresses"
Expand Down
2 changes: 2 additions & 0 deletions nimbus/db/ledger/base/base_desc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -136,5 +137,6 @@ type
setStorageFn*: SetStorageFn
setTransientStorageFn*: SetTransientStorageFn
subBalanceFn*: SubBalanceFn
getAccessListFn*: GetAccessListFn

# End

0 comments on commit d2eae45

Please sign in to comment.