2023-03-21 13:27:12 +00:00
|
|
|
# Nimbus
|
2024-06-10 09:05:30 +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.
|
|
|
|
|
|
|
|
import
|
|
|
|
tables,
|
|
|
|
stint,
|
2024-09-06 20:45:29 +00:00
|
|
|
eth/common,
|
|
|
|
../utils/mergeutils
|
2023-03-21 13:27:12 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
StorageTable = ref object
|
|
|
|
map: Table[UInt256, UInt256]
|
|
|
|
|
|
|
|
TransientStorage* = object
|
2024-10-16 06:51:38 +00:00
|
|
|
map: Table[Address, StorageTable]
|
2023-03-21 13:27:12 +00:00
|
|
|
|
|
|
|
#######################################################################
|
|
|
|
# Private helpers
|
|
|
|
#######################################################################
|
|
|
|
|
2024-09-06 20:45:29 +00:00
|
|
|
proc mergeAndReset*(a, b: StorageTable) =
|
|
|
|
a.map.mergeAndReset(b.map)
|
2023-03-21 13:27:12 +00:00
|
|
|
|
|
|
|
#######################################################################
|
|
|
|
# Public functions
|
|
|
|
#######################################################################
|
|
|
|
|
|
|
|
proc init*(ac: var TransientStorage) =
|
2024-10-16 06:51:38 +00:00
|
|
|
ac.map = Table[Address, StorageTable]()
|
2023-03-21 13:27:12 +00:00
|
|
|
|
|
|
|
proc init*(_: type TransientStorage): TransientStorage {.inline.} =
|
|
|
|
result.init()
|
|
|
|
|
|
|
|
func getStorage*(ac: TransientStorage,
|
2024-10-16 06:51:38 +00:00
|
|
|
address: Address, slot: UInt256): (bool, UInt256) =
|
2023-03-21 13:27:12 +00:00
|
|
|
var table = ac.map.getOrDefault(address)
|
|
|
|
if table.isNil:
|
|
|
|
return (false, 0.u256)
|
|
|
|
|
|
|
|
table.map.withValue(slot, val):
|
|
|
|
return (true, val[])
|
|
|
|
do:
|
|
|
|
return (false, 0.u256)
|
|
|
|
|
|
|
|
proc setStorage*(ac: var TransientStorage,
|
2024-10-16 06:51:38 +00:00
|
|
|
address: Address, slot, value: UInt256) =
|
2023-03-21 13:27:12 +00:00
|
|
|
var table = ac.map.getOrDefault(address)
|
|
|
|
if table.isNil:
|
|
|
|
table = StorageTable()
|
|
|
|
ac.map[address] = table
|
|
|
|
|
|
|
|
table.map[slot] = value
|
|
|
|
|
2024-09-06 20:45:29 +00:00
|
|
|
proc mergeAndReset*(ac, other: var TransientStorage) =
|
|
|
|
ac.map.mergeAndReset(other.map)
|
2023-03-21 13:27:12 +00:00
|
|
|
|
|
|
|
proc clear*(ac: var TransientStorage) {.inline.} =
|
|
|
|
ac.map.clear()
|