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,
|
2024-12-18 10:56:46 +00:00
|
|
|
eth/common/[addresses, transactions],
|
2024-09-06 20:45:29 +00:00
|
|
|
../utils/mergeutils
|
2020-12-09 05:24:37 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
SlotSet = HashSet[UInt256]
|
|
|
|
|
|
|
|
AccessList* = object
|
2024-10-16 06:51:38 +00:00
|
|
|
slots: Table[Address, SlotSet]
|
2020-12-09 05:24:37 +00:00
|
|
|
|
2024-02-29 11:16:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
func toStorageKeys(slots: SlotSet): seq[Bytes32] =
|
2024-02-29 11:16:47 +00:00
|
|
|
for slot in slots:
|
2024-09-29 12:37:09 +00:00
|
|
|
result.add slot.to(Bytes32)
|
2024-02-29 11:16:47 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructors
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2020-12-09 05:24:37 +00:00
|
|
|
proc init*(ac: var AccessList) =
|
2024-10-16 06:51:38 +00:00
|
|
|
ac.slots = Table[Address, SlotSet]()
|
2020-12-09 05:24:37 +00:00
|
|
|
|
|
|
|
proc init*(_: type AccessList): AccessList {.inline.} =
|
|
|
|
result.init()
|
|
|
|
|
2024-02-29 11:16:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-10-16 06:51:38 +00:00
|
|
|
func contains*(ac: AccessList, address: Address): bool {.inline.} =
|
2020-12-09 05:24:37 +00:00
|
|
|
address in ac.slots
|
|
|
|
|
|
|
|
# returnValue: (addressPresent, slotPresent)
|
2024-10-16 06:51:38 +00:00
|
|
|
func contains*(ac: var AccessList, address: Address, slot: UInt256): bool =
|
2020-12-09 05:24:37 +00:00
|
|
|
ac.slots.withValue(address, val):
|
|
|
|
result = slot in val[]
|
|
|
|
|
2024-09-06 20:45:29 +00:00
|
|
|
proc mergeAndReset*(ac, other: var AccessList) =
|
|
|
|
# move values in `other` to `ac`
|
|
|
|
ac.slots.mergeAndReset(other.slots)
|
2020-12-09 05:24:37 +00:00
|
|
|
|
2024-10-16 06:51:38 +00:00
|
|
|
proc add*(ac: var AccessList, address: Address) =
|
2020-12-09 05:24:37 +00:00
|
|
|
if address notin ac.slots:
|
2024-06-10 09:05:30 +00:00
|
|
|
ac.slots[address] = HashSet[UInt256]()
|
2020-12-09 05:24:37 +00:00
|
|
|
|
2024-10-16 06:51:38 +00:00
|
|
|
proc add*(ac: var AccessList, address: Address, slot: UInt256) =
|
2020-12-09 05:24:37 +00:00
|
|
|
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
|
|
|
|
2024-12-18 10:56:46 +00:00
|
|
|
func getAccessList*(ac: AccessList): transactions.AccessList =
|
2024-02-29 11:16:47 +00:00
|
|
|
for address, slots in ac.slots:
|
2024-12-18 10:56:46 +00:00
|
|
|
result.add transactions.AccessPair(
|
2024-02-29 11:16:47 +00:00
|
|
|
address : address,
|
|
|
|
storageKeys: slots.toStorageKeys,
|
|
|
|
)
|
2024-03-21 11:24:32 +00:00
|
|
|
|
|
|
|
func equal*(ac: AccessList, other: var AccessList): bool =
|
|
|
|
if ac.slots.len != other.slots.len:
|
|
|
|
return false
|
|
|
|
|
|
|
|
for address, slots in ac.slots:
|
|
|
|
other.slots.withValue(address, otherSlots):
|
|
|
|
if slots.len != otherSlots[].len:
|
|
|
|
return false
|
|
|
|
|
|
|
|
for slot in slots:
|
|
|
|
if slot notin otherSlots[]:
|
|
|
|
return false
|
|
|
|
do:
|
|
|
|
return false
|
|
|
|
|
|
|
|
true
|