Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion eth.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ requires "nim >= 2.0.10",
"unittest2",
"results",
"minilru",
"snappy"
"snappy",
"ssz_serialization"

let nimc = getEnv("NIMC", "nim") # Which nim compiler to use
let lang = getEnv("NIMLANG", "c") # Which backend (c/cpp/js)
Expand Down
13 changes: 9 additions & 4 deletions eth/common/receipts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@

import
./[addresses, base, hashes, transactions],
../bloom
../bloom,
ssz_serialization

export addresses, base, hash, transactions

const
MAX_TOPICS_PER_LOG* = 4
MAX_LOG_DATA_SIZE* = 1 shl 24

type
Topic* = Bytes32
# topic can be Hash32 or zero padded bytes array

Log* = object
address*: Address
topics*: seq[Topic]
data*: seq[byte]
address*: Address
topics*: List[Topic, MAX_TOPICS_PER_LOG]
data*: ByteList[MAX_LOG_DATA_SIZE]

# easily convertible between
# ReceiptType and TxType
Expand Down
12 changes: 12 additions & 0 deletions eth/common/ssz_utils.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ssz_serialization

proc `==`*[T; N: static[int]](a, b: List[T, N]): bool =
if a.len != b.len: # compare length first
return false
for i in 0..<a.len:
if a[i] != b[i]:
return false
true

proc `==`*[N: static[int]](a, b: ByteList[N]): bool =
a.asSeq == b.asSeq
10 changes: 9 additions & 1 deletion eth/rlp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import
stew/[byteutils, shims/macros],
results,
./rlp/[writer, object_serialization],
./rlp/priv/defs
./rlp/priv/defs,
ssz_serialization

from stew/objects import checkedEnumAssign

Expand Down Expand Up @@ -414,6 +415,13 @@ func readImpl[R, E](rlp: var Rlp, T: type array[R, E]): T =

rlp.positionAfter(item)

func readImpl*[E; N: static[int]](rlp: var Rlp, T: type List[E, N]): List[E, N] =
mixin read
T.init(rlp.read(seq[E]))

func readImpl*[N: static[int]](rlp: var Rlp, T: type ByteList[N]): ByteList[N] =
T.init(rlp.read(seq[byte]))

func readImpl[E](rlp: var Rlp, T: type seq[E]): T =
mixin read
let item = rlp.item()
Expand Down
20 changes: 19 additions & 1 deletion eth/rlp/writer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import
default_writer,
utils,
stint,
../common/hashes
../common/hashes,
ssz_serialization

export arraybuf, default_writer, length_writer, two_pass_writer, hash_writer

Expand All @@ -43,6 +44,12 @@ proc appendInt(self: var RlpWriter, i: SomeUnsignedInt) =
template appendImpl(self: var RlpWriter, data: openArray[byte]) =
self.appendBlob(data)

template appendImpl*[T; N: static[int]](self: var RlpWriter, data: List[T, N]) =
self.append(data.asSeq)

template appendImpl*[N: static[int]](self: var RlpWriter, data: ByteList[N]) =
self.appendRawBytes(data.asSeq)

template appendImpl(self: var RlpWriter, data: openArray[char]) =
self.appendBlob(data.toOpenArrayByte(0, data.high))

Expand Down Expand Up @@ -79,6 +86,13 @@ proc countNestedListsDepth(T: type): int {.compileTime.} =

when T is Option or T is Opt:
result += countNestedListsDepth(innerType(dummy))
elif T is List:
# Handle SSZ List[T, N] types - safely count depth without using elementType
inc result
# For SSZ List, we know it's one level of list nesting, but we can't safely
# access the element type here due to compilation issues, so we assume
# simple element types (like Hash32) which don't add additional depth
discard
elif T is UInt256:
discard
elif T is object or T is tuple:
Expand All @@ -91,6 +105,10 @@ proc countNestedListsDepth(T: type): int {.compileTime.} =
proc countNestedListsDepth[E](T: type openArray[E]): int =
countNestedListsDepth(seq[E])

# Specialized overload for SSZ List types
proc countNestedListsDepth[T; N: static[int]](L: type List[T, N]): int =
1 + countNestedListsDepth(T)

proc countOptionalFields(T: type): int {.compileTime.} =
mixin enumerateRlpFields

Expand Down
2 changes: 1 addition & 1 deletion tests/common/test_common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import
std/[os, json],
unittest2,
stew/byteutils,
../../eth/[common, rlp]
../../eth/[common, rlp, common/ssz_utils]

type
EthHeader = object
Expand Down