-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Align core types with execution spec
Since these types were written, we've gained an executable spec: https://github.com/ethereum/execution-specs This PR aligns some of the types we use with this spec to simplify comparisons and cross-referencing. Using a `distinct` type is a tradeoff between nim ergonomics, type safety and the ability to work around nim quirks and stdlib weaknesses. In particular, it allows us to overload common functions such as `hash` with correct and performant versions as well as maintain control over string conversions etc at the cost of a little bit of ceremony when instantiating them. Apart from distinct byte types, `Hash32`, is introduced in lieu of the existing `Hash256`, again aligning this commonly used type with the spec which picks bytes rather than bits in the name.
- Loading branch information
1 parent
aa1e738
commit a157a70
Showing
30 changed files
with
316 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
{.push raises: [].} | ||
|
||
import | ||
std/[hashes, macros, typetraits], | ||
results, | ||
stew/[byteutils, staticfor] | ||
|
||
export hashes, results | ||
|
||
type | ||
FixedBytes*[N: static int] = distinct array[N, byte] | ||
## Fixed-length byte sequence holding arbitrary data | ||
## https://github.com/ethereum/execution-specs/blob/51fac24740e662844446439ceeb96a460aae0ba0/src/ethereum/base_types.py | ||
|
||
# A distinct array is used to avoid copying on trivial type conversions | ||
|
||
Bytes4* = FixedBytes[4] | ||
Bytes8* = FixedBytes[8] | ||
Bytes20* = FixedBytes[20] | ||
Bytes32* = FixedBytes[32] | ||
Bytes48* = FixedBytes[48] | ||
Bytes64* = FixedBytes[64] | ||
Bytes96* = FixedBytes[96] | ||
Bytes256* = FixedBytes[256] | ||
|
||
template to*[N: static int](v: array[N, byte], _: type FixedBytes): FixedBytes[N] = | ||
FixedBytes(N)(v) | ||
|
||
template default*(_: type FixedBytes): FixedBytes = | ||
# Avoid bad codegen where fixed bytes are zeroed byte-by-byte at call site | ||
const def = system.default(FixedBytes) | ||
def | ||
|
||
template data*(v: FixedBytes): array = | ||
distinctBase(v) | ||
|
||
func len*(_: type FixedBytes): int = | ||
sizeof(FixedBytes) | ||
func `==`*(a, b: FixedBytes): bool {.inline.} = | ||
equalMem(addr a.data[0], addr b.data[0], a.N) | ||
|
||
func hash*[N: static int](v: FixedBytes[N]): Hash {.inline.} = | ||
copyMem(addr result, addr v.data[0], min(N, sizeof(Hash))) | ||
|
||
when N > sizeof(Hash): | ||
var tmp: Hash | ||
staticFor i, 1 ..< N div sizeof(Hash): | ||
copyMem(addr tmp, addr v.data[i * sizeof(Hash)], sizeof(Hash)) | ||
result = result !& tmp | ||
const last = N mod sizeof(Hash) | ||
when last > 0: | ||
copyMem(addr tmp, addr v.data[N - last], last) | ||
result !& tmp | ||
|
||
func toHex*(v: FixedBytes): string = | ||
toHex(v.data) | ||
|
||
func `$`*(v: FixedBytes): string = | ||
toHex(v) | ||
|
||
func fromHex*(T: type FixedBytes, c: openArray[char]): T {.raises: [ValueError].} = | ||
T(hexToByteArray(c, T.N)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.