Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EIP-1153: completing transient storage on EVMC side #1689

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions nimbus/evm/computation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,14 @@ template getCode*(c: Computation, address: EthAddress): seq[byte] =

template setTransientStorage*(c: Computation, slot, val: UInt256) =
when evmc_enabled:
# TODO: EIP-1153
discard
c.host.setTransientStorage(c.msg.contractAddress, slot, val)
else:
c.vmState.stateDB.
setTransientStorage(c.msg.contractAddress, slot, val)

template getTransientStorage*(c: Computation, slot: UInt256): UInt256 =
when evmc_enabled:
# TODO: EIP-1153
0.u256
c.host.getTransientStorage(c.msg.contractAddress, slot)
else:
c.vmState.readOnlyStateDB.
getTransientStorage(c.msg.contractAddress, slot)
Expand Down
19 changes: 18 additions & 1 deletion nimbus/evm/evmc_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type
block_base_fee* : evmc_uint256be # The block base fee.
blob_hashes* : ptr evmc_bytes32 # The array of blob hashes (EIP-4844).
blob_hashes_count*: csize_t # The number of blob hashes (EIP-4844).

nimbus_message* = object
kind* : evmc_call_kind
flags* : uint32
Expand Down Expand Up @@ -77,6 +77,10 @@ type
address: EthAddress): evmc_access_status {.cdecl, gcsafe, raises: [CatchableError].}
access_storage*: proc(context: evmc_host_context, address: EthAddress,
key: var evmc_bytes32): evmc_access_status {.cdecl, gcsafe, raises: [CatchableError].}
get_transient_storage*: proc(context: evmc_host_context, address: EthAddress,
key: ptr evmc_uint256be): evmc_uint256be {.cdecl, gcsafe, raises: [CatchableError].}
set_transient_storage*: proc(context: evmc_host_context, address: EthAddress,
key, value: ptr evmc_uint256be) {.cdecl, gcsafe, raises: [CatchableError].}

proc nim_host_get_interface*(): ptr nimbus_host_interface {.importc, cdecl.}
proc nim_host_create_context*(vmstate: pointer, msg: ptr evmc_message): evmc_host_context {.importc, cdecl.}
Expand Down Expand Up @@ -165,3 +169,16 @@ proc accessStorage*(ctx: HostContext, address: EthAddress,
{.gcsafe, raises: [CatchableError].} =
var key = toEvmc(key)
ctx.host.access_storage(ctx.context, address, key)

proc getTransientStorage*(ctx: HostContext, address: EthAddress, key: UInt256): UInt256
{.gcsafe, raises: [CatchableError].} =
var key = toEvmc(key)
UInt256.fromEvmc ctx.host.get_transient_storage(ctx.context, address, key.addr)

proc setTransientStorage*(ctx: HostContext, address: EthAddress,
key, value: UInt256)
{.gcsafe, raises: [CatchableError].} =
var
key = toEvmc(key)
value = toEvmc(value)
ctx.host.set_transient_storage(ctx.context, address, key.addr, value.addr)
11 changes: 11 additions & 0 deletions nimbus/transaction/evmc_host_glue.nim
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ proc accessStorage(p: evmc_host_context, address: var evmc_address,
key: var evmc_bytes32): evmc_access_status {.cdecl.} =
toHost(p).accessStorage(address.fromEvmc, key.flip256.fromEvmc)

proc getTransientStorage(p: evmc_host_context, address: var evmc_address,
key: var evmc_bytes32): evmc_bytes32
{.cdecl, raises: [RlpError].} =
toHost(p).getTransientStorage(address.fromEvmc, key.flip256.fromEvmc).toEvmc.flip256

proc setTransientStorage(p: evmc_host_context, address: var evmc_address,
key, value: var evmc_bytes32) {.cdecl, raises: [RlpError].} =
toHost(p).setTransientStorage(address.fromEvmc, key.flip256.fromEvmc, value.flip256.fromEvmc)

let hostInterface = evmc_host_interface(
account_exists: accountExists,
get_storage: getStorage,
Expand All @@ -96,6 +105,8 @@ let hostInterface = evmc_host_interface(
emit_log: emitLog,
access_account: accessAccount,
access_storage: accessStorage,
get_transient_storage: getTransientStorage,
set_transient_storage: setTransientStorage,
)

proc evmcExecComputation*(host: TransactionHost): EvmcResult
Expand Down
9 changes: 9 additions & 0 deletions nimbus/transaction/host_services.nim
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,15 @@ proc accessStorage(host: TransactionHost, address: HostAddress,
else:
return EVMC_ACCESS_WARM

proc getTransientStorage(host: TransactionHost,
address: HostAddress, key: HostKey): HostValue {.show.} =
host.vmState.readOnlyStateDB.getTransientStorage(address, key)

proc setTransientStorage(host: TransactionHost, address: HostAddress,
key: HostKey, newVal: HostValue) {.show.} =
host.vmState.mutateStateDB:
db.setTransientStorage(address, key, newVal)

when use_evmc_glue:
{.pop: inline.}
const included_from_host_services = true
Expand Down
2 changes: 1 addition & 1 deletion vendor/nim-evmc