fix evmc imports

This commit is contained in:
jangko 2024-06-16 00:09:02 +07:00
parent 53ce79a9b1
commit d3f6ec2e6d
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
9 changed files with 43 additions and 86 deletions

View File

@ -87,7 +87,7 @@ template getBlockNumber*(c: Computation): UInt256 =
template getDifficulty*(c: Computation): DifficultyInt =
when evmc_enabled:
UInt256.fromEvmc c.host.getTxContext().block_prev_randao
fromEvmc c.host.getTxContext().block_prev_randao
else:
c.vmState.difficultyOrPrevRandao
@ -99,7 +99,7 @@ template getGasLimit*(c: Computation): GasInt =
template getBaseFee*(c: Computation): UInt256 =
when evmc_enabled:
UInt256.fromEvmc c.host.getTxContext().block_base_fee
fromEvmc c.host.getTxContext().block_base_fee
else:
c.vmState.blockCtx.baseFeePerGas.get(0.u256)
@ -117,7 +117,7 @@ template getOrigin*(c: Computation): EthAddress =
template getGasPrice*(c: Computation): GasInt =
when evmc_enabled:
UInt256.fromEvmc(c.host.getTxContext().tx_gas_price).truncate(GasInt)
fromEvmc(c.host.getTxContext().tx_gas_price).truncate(GasInt)
else:
c.vmState.txCtx.gasPrice
@ -135,7 +135,7 @@ template getVersionedHashesLen*(c: Computation): int =
template getBlobBaseFee*(c: Computation): UInt256 =
when evmc_enabled:
UInt256.fromEvmc c.host.getTxContext().blob_base_fee
fromEvmc c.host.getTxContext().blob_base_fee
else:
c.vmState.txCtx.blobBaseFee

View File

@ -115,7 +115,7 @@ proc accountExists*(ctx: HostContext, address: EthAddress): bool =
proc getStorage*(ctx: HostContext, address: EthAddress, key: UInt256): UInt256 =
var key = toEvmc(key)
UInt256.fromEvmc ctx.host.get_storage(ctx.context, address, key.addr)
fromEvmc ctx.host.get_storage(ctx.context, address, key.addr)
proc setStorage*(ctx: HostContext, address: EthAddress,
key, value: UInt256): evmc_storage_status =
@ -125,7 +125,7 @@ proc setStorage*(ctx: HostContext, address: EthAddress,
ctx.host.set_storage(ctx.context, address, key.addr, value.addr)
proc getBalance*(ctx: HostContext, address: EthAddress): UInt256 =
UInt256.fromEvmc ctx.host.get_balance(ctx.context, address)
fromEvmc ctx.host.get_balance(ctx.context, address)
proc getCodeSize*(ctx: HostContext, address: EthAddress): uint =
ctx.host.get_code_size(ctx.context, address)
@ -163,7 +163,7 @@ proc accessStorage*(ctx: HostContext, address: EthAddress,
proc getTransientStorage*(ctx: HostContext, address: EthAddress, key: UInt256): UInt256 =
var key = toEvmc(key)
UInt256.fromEvmc ctx.host.get_transient_storage(ctx.context, address, key.addr)
fromEvmc ctx.host.get_transient_storage(ctx.context, address, key.addr)
proc setTransientStorage*(ctx: HostContext, address: EthAddress,
key, value: UInt256) =
@ -179,4 +179,4 @@ template getBlockNumber*(ctx: HostContext): uint64 =
ctx.getTxContext().block_number.uint64
template getChainId*(ctx: HostContext): uint64 =
UInt256.fromEvmc(ctx.getTxContext().chain_id).truncate(uint64)
fromEvmc(ctx.getTxContext().chain_id).truncate(uint64)

View File

@ -14,49 +14,26 @@ import
evmc/evmc,
../utils/utils
const
evmc_native* {.booldefine.} = false
template toEvmc*(n: UInt256): evmc_uint256be =
cast[evmc_uint256be](n)
func toEvmc*(a: EthAddress): evmc_address {.inline.} =
cast[evmc_address](a)
template toEvmc*(n: Hash256): evmc_bytes32 =
cast[evmc_bytes32](n)
func toEvmc*(h: Hash256 | ContractSalt): evmc_bytes32 {.inline.} =
doAssert sizeof(h) == sizeof(evmc_bytes32)
cast[evmc_bytes32](h)
template toEvmc*(address: EthAddress): evmc_address =
cast[evmc_address](address)
func toEvmc*(n: UInt256): evmc_uint256be {.inline.} =
when evmc_native:
cast[evmc_uint256be](n)
else:
cast[evmc_uint256be](n.toBytesBE)
template fromEvmc*(n: evmc_uint256be): UInt256 =
cast[UInt256](n)
func fromEvmc*(T: type, n: evmc_bytes32): T {.inline.} =
when T is Hash256 | ContractSalt:
doAssert sizeof(n) == sizeof(T)
cast[T](n)
elif T is UInt256:
when evmc_native:
cast[UInt256](n)
else:
UInt256.fromBytesBE(n.bytes)
else:
{.error: "cannot convert unsupported evmc type".}
template fromEvmc*(address: evmc_address): EthAddress =
cast[EthAddress](address)
func fromEvmc*(a: evmc_address): EthAddress {.inline.} =
cast[EthAddress](a)
template flip256*(word256: evmc_uint256be): evmc_uint256be =
cast[evmc_uint256be](UInt256.fromBytesBE(word256.bytes).toBytes(cpuEndian))
when isMainModule:
import ../constants
var a: evmc_address
a.bytes[19] = 3.byte
var na = fromEvmc(a)
assert(a == toEvmc(na))
var b = stuint(10, 256)
var eb = b.toEvmc
assert(b == fromEvmc(UInt256, eb))
var h = EMPTY_SHA3
var eh = toEvmc(h)
assert(h == fromEvmc(Hash256, eh))
var s = cast[ContractSalt](EMPTY_ROOT_HASH)
var es = toEvmc(s)
assert(s == fromEvmc(ContractSalt, es))
template isCreate*(kind: evmc_call_kind): bool =
kind in {EVMC_CREATE, EVMC_CREATE2}
template isZero*(n: evmc_bytes32): bool =
n == default(evmc_bytes32)

View File

@ -145,7 +145,7 @@ const
input_data: cpt.memory.readPtr(memPos),
input_size: memLen.uint,
value: toEvmc(endowment),
create2_salt: toEvmc(ZERO_CONTRACTSALT),
create2_salt: ZERO_CONTRACTSALT,
)
c.execSubCreate(msg)
else:
@ -228,7 +228,7 @@ const
input_data: cpt.memory.readPtr(memPos),
input_size: memLen.uint,
value: toEvmc(endowment),
create2_salt: toEvmc(salt),
create2_salt: salt,
)
c.execSubCreate(msg)
else:

View File

@ -14,6 +14,7 @@ import
results,
../evm/[types, state, computation],
../evm/[precompiles, internals],
../evm/state_transactions,
../db/ledger,
../common/evmforks,
../core/eip4844,
@ -22,8 +23,6 @@ import
when defined(evmc_enabled):
import ../utils/utils
import ./host_services
else:
import ../evm/state_transactions
type
# Standard call parameters.

View File

@ -12,7 +12,9 @@ import
eth/common/eth_types,
stew/ptrops,
stint,
".."/[evm/types, evm/computation],
../evm/types,
../evm/computation,
../evm/interpreter_dispatch,
../utils/utils,
"."/[host_types, host_trace]

View File

@ -13,7 +13,7 @@ import
stint, chronicles,
eth/common/eth_types, ../db/ledger,
../common/[evmforks, common],
".."/[evm/state, evm/computation, evm/internals],
../evm/[state, computation, internals],
./host_types, ./host_trace, ./host_call_nested,
stew/saturation_arith

View File

@ -7,10 +7,13 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import
std/sets,
stint, evmc/evmc,
eth/common/eth_types,
../evm/types
../evm/types,
../evm/evmc_helpers
export
evmc_helpers
# Object `TransactionHost` represents "EVMC host" to the EVM. "Host services"
# manage account state outside EVM such as balance transfers, storage, logs and
@ -66,34 +69,6 @@ type
saveComputation*: seq[Computation]
hostInterface*: ptr evmc_host_interface
# These versions of `toEvmc` and `fromEvmc` don't flip big/little-endian like
# the older functions in `evmc_helpers`. New code only flips with _explicit_
# calls to `flip256` where it is wanted.
template toEvmc*(n: UInt256): evmc_uint256be =
cast[evmc_uint256be](n)
template toEvmc*(n: Hash256): evmc_bytes32 =
cast[evmc_bytes32](n)
template toEvmc*(address: EthAddress): evmc_address =
cast[evmc_address](address)
template fromEvmc*(n: evmc_uint256be): UInt256 =
cast[UInt256](n)
template fromEvmc*(address: evmc_address): EthAddress =
cast[EthAddress](address)
template flip256*(word256: evmc_uint256be): evmc_uint256be =
cast[evmc_uint256be](UInt256.fromBytesBE(word256.bytes).toBytes(cpuEndian))
template isCreate*(kind: EvmcCallKind): bool =
kind in {EVMC_CREATE, EVMC_CREATE2}
template isZero*(n: evmc_bytes32): bool =
n == default(evmc_bytes32)
# Nim quirks: Exporting `evmc_status_code` (etc) are needed to access the enum
# values, even though alias `EnumStatusCode` is already exported. Exporting
# `evmc_flags` won't export the flags, `evmc_flag_bit_shifts` must be used.

View File

@ -66,8 +66,12 @@ func hasBody*(h: BlockHeader): bool =
func generateAddress*(address: EthAddress, nonce: AccountNonce): EthAddress =
result[0..19] = keccakHash(rlp.encodeList(address, nonce)).data.toOpenArray(12, 31)
type ContractSalt* = object
bytes*: array[32, byte]
when defined(evmc_enabled):
import evmc/evmc
type ContractSalt* = evmc_bytes32
else:
type ContractSalt* = object
bytes*: array[32, byte]
const ZERO_CONTRACTSALT* = default(ContractSalt)