2023-03-21 13:27:12 +00:00
|
|
|
# Nimbus
|
2024-02-29 11:16:47 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-03-21 13:27:12 +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-12-09 05:24:37 +00:00
|
|
|
import
|
2024-02-29 11:16:47 +00:00
|
|
|
std/[tables, sets],
|
2020-12-09 05:24:37 +00:00
|
|
|
stint,
|
|
|
|
eth/common
|
|
|
|
|
|
|
|
type
|
|
|
|
SlotSet = HashSet[UInt256]
|
|
|
|
|
|
|
|
AccessList* = object
|
|
|
|
slots: Table[EthAddress, SlotSet]
|
|
|
|
|
2024-02-29 11:16:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func toStorageKeys(slots: SlotSet): seq[StorageKey] =
|
|
|
|
for slot in slots:
|
|
|
|
result.add slot.toBytesBE
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructors
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2020-12-09 05:24:37 +00:00
|
|
|
proc init*(ac: var AccessList) =
|
|
|
|
ac.slots = initTable[EthAddress, SlotSet]()
|
|
|
|
|
|
|
|
proc init*(_: type AccessList): AccessList {.inline.} =
|
|
|
|
result.init()
|
|
|
|
|
2024-02-29 11:16:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2020-12-09 05:24:37 +00:00
|
|
|
func contains*(ac: AccessList, address: EthAddress): bool {.inline.} =
|
|
|
|
address in ac.slots
|
|
|
|
|
|
|
|
# returnValue: (addressPresent, slotPresent)
|
|
|
|
func contains*(ac: var AccessList, address: EthAddress, slot: UInt256): bool =
|
|
|
|
ac.slots.withValue(address, val):
|
|
|
|
result = slot in val[]
|
|
|
|
|
|
|
|
proc merge*(ac: var AccessList, other: AccessList) {.inline.} =
|
|
|
|
for k, v in other.slots:
|
|
|
|
ac.slots.withValue(k, val):
|
|
|
|
val[].incl v
|
|
|
|
do:
|
|
|
|
ac.slots[k] = v
|
|
|
|
|
|
|
|
proc add*(ac: var AccessList, address: EthAddress) =
|
|
|
|
if address notin ac.slots:
|
|
|
|
ac.slots[address] = initHashSet[UInt256]()
|
|
|
|
|
|
|
|
proc add*(ac: var AccessList, address: EthAddress, slot: UInt256) =
|
|
|
|
ac.slots.withValue(address, val):
|
|
|
|
val[].incl slot
|
|
|
|
do:
|
|
|
|
ac.slots[address] = toHashSet([slot])
|
2020-12-11 10:51:17 +00:00
|
|
|
|
|
|
|
proc clear*(ac: var AccessList) {.inline.} =
|
|
|
|
ac.slots.clear()
|
2024-02-29 11:16:47 +00:00
|
|
|
|
|
|
|
func getAccessList*(ac: AccessList): common.AccessList =
|
|
|
|
for address, slots in ac.slots:
|
|
|
|
result.add common.AccessPair(
|
|
|
|
address : address,
|
|
|
|
storageKeys: slots.toStorageKeys,
|
|
|
|
)
|