mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-12 13:24:21 +00:00
6ef9bfd21b
Perform byte-endian conversion for 256-bit numeric values, but not 256-bit hashes. These conversions are necessary for EVMC binary compatibility. In new EVMC, all host-side conversions are explicit, calling `flip256`. These conversions are performed in the EVMC "glue" code, which deals with the binary interface, so the host services aren't aware of conversions. We intend to skip these conversions when Nimbus host calls Nimbus EVM, even when it's a shared library, using a negotiated EVMC extension. But for now we're focused on correctness and cross-validation with third party EVMs. The overhead of endian conversion is not too high because most EVMC host calls access the database anyway. `getTxContext` does not, so the conversions from that are cached here. Also, well-optimised EVMs don't call it often. It is arguable whether endian conversion should occur for storage slots (`key`). In favour of no conversion: Slot keys are 32-byte blobs, and this is clear in the EVMC definition where slot keys are `evmc_bytes32` (not `evmc_uint256be`), meaning treating as a number is _not_ expected by EVMC. Although they are often small numbers, sometimes they are a hash from the contract code plus a number. Slot keys are hashed on the host side with Keccak256 before any database calls, so the host side does not look at them numerically. In favour of conversion: They are often small numbers and it is helpful to log them as such, rather than a long string of zero digits with 1-2 non-zero. The representation in JSON has leading zeros removed, like a number rather than a 32-byte blob. There is also an interesting space optimisation when the keys are used unhashed in storage. Nimbus currently treats slot keys on the host side as numbers, and the tests pass when endian conversion is done. So to remain consistent with other parts of Nimbus we convert slot keys. Signed-off-by: Jamie Lokier <jamie@shareable.org>
49 lines
1.2 KiB
Nim
49 lines
1.2 KiB
Nim
import eth/common, stint, evmc/evmc, ../utils
|
|
|
|
const
|
|
evmc_native* {.booldefine.} = false
|
|
|
|
func toEvmc*(a: EthAddress): evmc_address {.inline.} =
|
|
cast[evmc_address](a)
|
|
|
|
func toEvmc*(h: Hash256 | ContractSalt): evmc_bytes32 {.inline.} =
|
|
doAssert sizeof(h) == sizeof(evmc_bytes32)
|
|
cast[evmc_bytes32](h)
|
|
|
|
func toEvmc*(n: Uint256): evmc_uint256be {.inline.} =
|
|
when evmc_native:
|
|
cast[evmc_uint256be](n)
|
|
else:
|
|
cast[evmc_uint256be](n.toByteArrayBE)
|
|
|
|
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".}
|
|
|
|
func fromEvmc*(a: evmc_address): EthAddress {.inline.} =
|
|
cast[EthAddress](a)
|
|
|
|
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](BLANK_ROOT_HASH)
|
|
var es = toEvmc(s)
|
|
assert(s == fromEvmc(ContractSalt, es))
|