Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
arnetheduck committed Sep 27, 2024
1 parent 83d639d commit cdb3601
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion eth/common/accounts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Account* = object

const
EMPTY_ROOT_HASH* = emptyRoot
EMPTY_CODE_HASH* = emptyHash32
EMPTY_CODE_HASH* = emptyKeccak256

func init*(
T: type Account,
Expand Down
33 changes: 23 additions & 10 deletions eth/common/base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type
##
## This type is specialized to `Bytes4`, `Bytes8` etc below.
# A distinct array is used to avoid copying on trivial type conversions
# to and from other array-based types

ChainId* = distinct uint64
## Chain identifier used for transaction signing to guard against replay
Expand All @@ -57,12 +58,7 @@ type
template to*[N: static int](v: array[N, byte], T: type FixedBytes[N]): T =
T(v)

template default*[N](T: type FixedBytes[N]): T =
# Avoid bad codegen where fixed bytes are zeroed byte-by-byte at call site
const def = system.default(T)
def

template data*(v: FixedBytes): array =
template data*[N: static int](v: FixedBytes[N]): array[N, byte] =
distinctBase(v)

template `data=`*[N: static int](a: FixedBytes[N], b: array[N, byte]) =
Expand All @@ -79,6 +75,11 @@ func copyFrom*[N: static int](T: type FixedBytes[N], v: openArray[byte], start =
result.data, v.toOpenArray(min(start, v.len), min(start + sizeof(T), v.len()) - 1)
)

template default*[N](T: type FixedBytes[N]): T =
# Avoid bad codegen where fixed bytes are zeroed byte-by-byte at call site
const def = system.default(T)
def

func `==`*(a, b: FixedBytes): bool {.inline.} =
equalMem(addr a.data[0], addr b.data[0], a.N)

Expand Down Expand Up @@ -116,6 +117,9 @@ template makeFixedBytesN(N: static int) =
type `Bytes N`* = FixedBytes[N]

const `zeroBytes N`* = system.default(`Bytes N`)
template default*(T: type `Bytes N`): `Bytes N` =
# reuse single constant for precomputed N
`zeroBytes N`

template `bytes N`*(s: static string): `Bytes N` =
`Bytes N`.fromHex(s)
Expand All @@ -129,6 +133,19 @@ makeFixedBytesN(64)
makeFixedBytesN(96)
makeFixedBytesN(256)

# Ethereum keeps integers as big-endian
template to*(v: uint32, T: type Bytes4): T =
T v.toBytesBE()

template to*(v: Bytes4, T: type uint32): T =
T.fromBytesBE(v)

template to*(v: uint64, T: type Bytes8): T =
T v.toBytesBE()

template to*(v: Bytes8, T: type uint64): T =
T.fromBytesBE(v)

template to*[M, N: static int](v: FixedBytes[M], T: type StUint[N]): T =
static:
assert N == M * 8
Expand All @@ -152,7 +169,6 @@ type
# In most other cases, code is easier to read and more flexible when it
# doesn't use these aliases.
AccountNonce* = uint64
BlockNonce* = Bytes8
BlockNumber* = uint64
Bloom* = Bytes256
Bytes* = seq[byte] # TODO distinct?
Expand All @@ -161,9 +177,6 @@ type

ForkID* = tuple[crc: uint32, nextFork: uint64] ## EIP 2364/2124

func toBlockNonce*(n: uint64): BlockNonce =
BlockNonce(n.toBytesBE())

func `==`*(a, b: NetworkId): bool {.borrow.}
func `$`*(x: NetworkId): string {.borrow.}
func `==`*(a, b: ChainId): bool {.borrow.}
Expand Down
2 changes: 2 additions & 0 deletions eth/common/eth_hash.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# Minimal compatibility layer with earlier versions of this file, to be removed
# when users have upgraded

{.deprecated.}

import ./[addresses, hashes]

export hashes
Expand Down
4 changes: 4 additions & 0 deletions eth/common/eth_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type
StorageKey* {.deprecated.} = Bytes32
Blob* {.deprecated.} = seq[byte]
VersionedHashes* {.deprecated.} = seq[VersionedHash]
BlockNonce* {.deprecated.} = Bytes8

func toBlockNonce*(n: uint64): BlockNonce =
n.to(BlockNonce)

func newAccount*(
nonce: AccountNonce = 0, balance: UInt256 = 0.u256
Expand Down
4 changes: 3 additions & 1 deletion eth/common/hashes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type
Hash32* = distinct Bytes32
## https://github.com/ethereum/execution-specs/blob/51fac24740e662844446439ceeb96a460aae0ba0/src/ethereum/crypto/hash.py#L19
Root* = Hash32
## Alias used for MPT roots

const zeroHash32* = system.default(Hash32) ## Hash32 value consisting of all zeroes

Expand Down Expand Up @@ -52,6 +53,7 @@ template default*(_: type Hash32): Hash32 =
func `==`*(a, b: Hash32): bool {.borrow.}

func hash*(a: Hash32): Hash {.inline.} =
# Hashes are already supposed to be random so we use a faster mixing function
var tmp {.noinit.}: array[4, uint64]
copyMem(addr tmp[0], addr a.data[0], sizeof(a))
cast[Hash](tmp[0] + tmp[1] + tmp[2] + tmp[3])
Expand Down Expand Up @@ -79,7 +81,7 @@ template to*(v: Hash32, _: type MDigest[256]): MDigest[256] =
tmp

const
emptyHash32* =
emptyKeccak256* =
hash32"c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
## Hash value of `keccak256([])`, ie the empty string
emptyRoot* = hash32"56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
Expand Down
8 changes: 4 additions & 4 deletions eth/common/headers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export addresses, base, hashes, times
type
DifficultyInt* = UInt256

# https://github.com/ethereum/execution-specs/blob/51fac24740e662844446439ceeb96a460aae0ba0/src/ethereum/paris/blocks.py#L22
Header* = object
# https://github.com/ethereum/execution-specs/blob/51fac24740e662844446439ceeb96a460aae0ba0/src/ethereum/cancun/blocks.py
parentHash*: Hash32
ommersHash*: Hash32
coinbase*: Address
Expand All @@ -30,7 +30,7 @@ type
timestamp*: EthTime
extraData*: seq[byte]
mixHash*: Hash32
nonce*: BlockNonce
nonce*: Bytes8
baseFeePerGas*: Opt[UInt256] # EIP-1559
withdrawalsRoot*: Opt[Hash32] # EIP-4895
blobGasUsed*: Opt[uint64] # EIP-4844
Expand All @@ -40,11 +40,11 @@ type

BlockHeader*{.deprecated: "Header".} = Header

# starting from EIP-4399, `mixHash`/`mixDigest` field will be called `prevRandao`
# starting from EIP-4399, `mixDigest` field is called `prevRandao`
template prevRandao*(h: Header): Hash32 =
h.mixHash

template `prevRandao=`*(h: Header, hash: Hash32) =
h.mixHash = hash

template txRoot*(h: Header): Root = h.transactionsRoot
template txRoot*(h: Header): Root = h.transactionsRoot
3 changes: 0 additions & 3 deletions eth/common/keys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@

{.push raises: [].}


# This module contains adaptations of the general secp interface to help make
# working with keys and signatures as they appear in Ethereum in particular:
#
# * Public keys as serialized in uncompressed format without the initial byte
# * Shared secrets are serialized in raw format without the initial byte
# * distinct types are used to avoid confusion with the "standard" secp types

{.push raises: [].}

import
std/strformat,
secp256k1, bearssl/rand,
Expand Down

0 comments on commit cdb3601

Please sign in to comment.