[skip ci] Address core review comments

This commit is contained in:
zah 2020-05-24 11:17:33 +03:00
parent 4a25bec2b5
commit 626e51a91b
1 changed files with 8 additions and 3 deletions

View File

@ -182,6 +182,8 @@ template endRecord*(w: var SszWriter, ctx: var auto) =
proc writeSeq[T](w: var SszWriter, value: seq[T]) proc writeSeq[T](w: var SszWriter, value: seq[T])
{.raises: [Defect, IOError].} = {.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): when isFixedSize(T):
trs "WRITING LIST WITH FIXED SIZE ELEMENTS" trs "WRITING LIST WITH FIXED SIZE ELEMENTS"
for elem in value: for elem in value:
@ -205,8 +207,11 @@ proc writeVarSizeType(w: var SszWriter, value: auto) {.raises: [Defect, IOError]
type T = type toSszType(value) type T = type toSszType(value)
when T is List: when T is List:
# We reduce code bloat by forwarding all `List` types to a general `seq[T]` proc.
writeSeq(w, asSeq value) writeSeq(w, asSeq value)
elif T is BitList: 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) writeSeq(w, bytes value)
elif T is object|tuple|array: elif T is object|tuple|array:
trs "WRITING OBJECT OR 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, func chunkedHashTreeRootForBasicTypes[T](merkleizer: var SszChunksMerkleizer,
arr: openarray[T]): Eth2Digest = arr: openarray[T]): Eth2Digest =
static: static:
assert T is BasicType doAssert T is BasicType
when T is byte: when T is byte:
var var
@ -464,8 +469,8 @@ func chunkedHashTreeRootForBasicTypes[T](merkleizer: var SszChunksMerkleizer,
else: else:
static: static:
assert T is UintN doAssert T is UintN
assert bytesPerChunk mod sizeof(Т) == 0 doAssert bytesPerChunk mod sizeof(Т) == 0
const valuesPerChunk = bytesPerChunk div sizeof(Т) const valuesPerChunk = bytesPerChunk div sizeof(Т)