From 626e51a91b7fe01f94f276eac2c7f95cb7fb6250 Mon Sep 17 00:00:00 2001 From: zah Date: Sun, 24 May 2020 11:17:33 +0300 Subject: [PATCH] [skip ci] Address core review comments --- beacon_chain/ssz.nim | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/beacon_chain/ssz.nim b/beacon_chain/ssz.nim index daefbc16e..a5f3e4846 100644 --- a/beacon_chain/ssz.nim +++ b/beacon_chain/ssz.nim @@ -182,6 +182,8 @@ template endRecord*(w: var SszWriter, ctx: var auto) = proc writeSeq[T](w: var SszWriter, value: seq[T]) {.raises: [Defect, IOError].} = + # Please note that `writeSeq` exists in order to reduce the code bloat + # produced from generic instantiations of the unique `List[N, T]` types. when isFixedSize(T): trs "WRITING LIST WITH FIXED SIZE ELEMENTS" for elem in value: @@ -205,8 +207,11 @@ proc writeVarSizeType(w: var SszWriter, value: auto) {.raises: [Defect, IOError] type T = type toSszType(value) when T is List: + # We reduce code bloat by forwarding all `List` types to a general `seq[T]` proc. writeSeq(w, asSeq value) elif T is BitList: + # ATTENTION! We can reuse `writeSeq` only as long as our BitList type is implemented + # to internally match the binary representation of SSZ BitLists in memory. writeSeq(w, bytes value) elif T is object|tuple|array: trs "WRITING OBJECT OR ARRAY" @@ -441,7 +446,7 @@ template writeBytesLE(chunk: var array[bytesPerChunk, byte], atParam: int, func chunkedHashTreeRootForBasicTypes[T](merkleizer: var SszChunksMerkleizer, arr: openarray[T]): Eth2Digest = static: - assert T is BasicType + doAssert T is BasicType when T is byte: var @@ -464,8 +469,8 @@ func chunkedHashTreeRootForBasicTypes[T](merkleizer: var SszChunksMerkleizer, else: static: - assert T is UintN - assert bytesPerChunk mod sizeof(Т) == 0 + doAssert T is UintN + doAssert bytesPerChunk mod sizeof(Т) == 0 const valuesPerChunk = bytesPerChunk div sizeof(Т)