2021-04-15 13:40:31 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 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)
|
|
|
|
# * 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.
|
|
|
|
|
2021-04-23 10:47:58 +00:00
|
|
|
## EVM Opcode Handlers: Common Helper Functions
|
|
|
|
## ============================================
|
2021-04-15 13:40:31 +00:00
|
|
|
##
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
when defined(evmc_enabled):
|
|
|
|
{.push raises: [CatchableError].} # basically the annotation type of a `Vm2OpFn`
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2023-02-14 20:27:17 +00:00
|
|
|
|
2021-04-15 13:40:31 +00:00
|
|
|
import
|
|
|
|
../../../errors,
|
2021-04-26 12:03:20 +00:00
|
|
|
../../types,
|
|
|
|
../gas_costs,
|
|
|
|
eth/common,
|
2021-04-21 17:04:54 +00:00
|
|
|
eth/common/eth_types,
|
2021-04-23 10:47:58 +00:00
|
|
|
stint
|
2021-04-15 13:40:31 +00:00
|
|
|
|
2022-09-26 04:56:54 +00:00
|
|
|
when defined(evmc_enabled):
|
2023-02-14 20:27:17 +00:00
|
|
|
import ../../evmc_api, evmc/evmc
|
2022-09-28 06:09:33 +00:00
|
|
|
else:
|
|
|
|
import
|
|
|
|
../../state,
|
|
|
|
../../../db/accounts_cache
|
2022-09-26 04:56:54 +00:00
|
|
|
|
2021-04-15 13:40:31 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-27 06:13:37 +00:00
|
|
|
proc gasEip2929AccountCheck*(c: Computation; address: EthAddress): GasInt =
|
2022-09-26 04:56:54 +00:00
|
|
|
when defined(evmc_enabled):
|
2023-08-27 06:13:37 +00:00
|
|
|
result = if c.host.accessAccount(address) == EVMC_ACCESS_COLD:
|
|
|
|
ColdAccountAccessCost
|
|
|
|
else:
|
|
|
|
WarmStorageReadCost
|
2022-09-26 04:56:54 +00:00
|
|
|
else:
|
|
|
|
c.vmState.mutateStateDB:
|
2023-08-27 06:13:37 +00:00
|
|
|
result = if not db.inAccessList(address):
|
|
|
|
db.accessList(address)
|
|
|
|
ColdAccountAccessCost
|
|
|
|
else:
|
|
|
|
WarmStorageReadCost
|
2021-04-15 16:42:19 +00:00
|
|
|
|
2023-08-27 06:13:37 +00:00
|
|
|
proc gasEip2929AccountCheck*(c: Computation; address: EthAddress, slot: UInt256): GasInt =
|
|
|
|
when defined(evmc_enabled):
|
|
|
|
result = if c.host.accessStorage(address, slot) == EVMC_ACCESS_COLD:
|
|
|
|
ColdSloadCost
|
|
|
|
else:
|
|
|
|
WarmStorageReadCost
|
|
|
|
else:
|
|
|
|
c.vmState.mutateStateDB:
|
|
|
|
result = if not db.inAccessList(address, slot):
|
|
|
|
db.accessList(address, slot)
|
|
|
|
ColdSloadCost
|
|
|
|
else:
|
|
|
|
WarmStorageReadCost
|
2021-04-15 16:42:19 +00:00
|
|
|
|
2021-04-15 13:40:31 +00:00
|
|
|
template checkInStaticContext*(c: Computation) =
|
|
|
|
## Verify static context in handler function, raise an error otherwise
|
2023-08-28 12:10:31 +00:00
|
|
|
if EVMC_STATIC in c.msg.flags:
|
2021-04-15 13:40:31 +00:00
|
|
|
# TODO: if possible, this check only appear
|
|
|
|
# when fork >= FkByzantium
|
|
|
|
raise newException(
|
|
|
|
StaticContextError,
|
|
|
|
"Cannot modify state while inside of STATICCALL context")
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|