Compare commits

..

6 Commits

Author SHA1 Message Date
andri lim 8249af6a01
Merge d9d01c9f28 into af34f90fe4 2024-06-16 09:17:36 +07:00
jangko d9d01c9f28
Add missing flip256 call 2024-06-16 08:42:24 +07:00
jangko d3f6ec2e6d
fix evmc imports 2024-06-16 00:09:02 +07:00
jangko 53ce79a9b1
Remove VMError 2024-06-15 23:19:27 +07:00
jangko 351be21c5b
Fix copyright year 2024-06-15 23:08:24 +07:00
jangko f693a55056
Remove EVM indirect imports and unused EVM errors
Those indirect imports are used when there was two EVMs.
2024-06-15 23:03:02 +07:00
9 changed files with 45 additions and 94 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.flip256
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.flip256
else:
c.vmState.blockCtx.baseFeePerGas.get(0.u256)
@ -117,7 +117,8 @@ 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.flip256).
truncate(GasInt)
else:
c.vmState.txCtx.gasPrice
@ -135,7 +136,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.flip256
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).flip256
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).flip256
proc getCodeSize*(ctx: HostContext, address: EthAddress): uint =
ctx.host.get_code_size(ctx.context, address)
@ -159,11 +159,11 @@ proc accessAccount*(ctx: HostContext,
proc accessStorage*(ctx: HostContext, address: EthAddress,
key: UInt256): evmc_access_status =
var key = toEvmc(key)
ctx.host.access_storage(ctx.context, address, key)
ctx.host.access_storage(ctx.context, address, key.addr)
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).flip256
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.flip256).truncate(uint64)

View File

@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2020-2023 Status Research & Development GmbH
# Copyright (c) 2020-2024 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)
@ -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

@ -12,20 +12,17 @@ import
eth/common/eth_types, stint, stew/ptrops,
chronos,
results,
../evm/[types, state, state_transactions],
../evm/[types, state, computation],
../evm/[precompiles, internals],
../evm/state_transactions,
../db/ledger,
../common/evmforks,
../core/eip4844,
./host_types
import ../evm/computation except fromEvmc, toEvmc
when defined(evmc_enabled):
import ../utils/utils
import ./host_services
#else:
#import ../evm/state_transactions
type
# Standard call parameters.

View File

@ -13,12 +13,11 @@ import
stew/ptrops,
stint,
../evm/types,
../evm/computation,
../evm/interpreter_dispatch,
../utils/utils,
"."/[host_types, host_trace]
import ../evm/computation except fromEvmc, toEvmc
proc evmcResultRelease(res: var EvmcResult) {.cdecl, gcsafe.} =
dealloc(res.output_data)

View File

@ -13,12 +13,10 @@ import
stint, chronicles,
eth/common/eth_types, ../db/ledger,
../common/[evmforks, common],
../evm/[state, internals],
../evm/[state, computation, internals],
./host_types, ./host_trace, ./host_call_nested,
stew/saturation_arith
import ../evm/computation except fromEvmc, toEvmc
proc setupTxContext(host: TransactionHost) =
# Conversion issues:
#

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)