Skip to content

Commit

Permalink
Merge branch 'master' into dev/etan/sz-7495
Browse files Browse the repository at this point in the history
  • Loading branch information
etan-status committed Feb 12, 2025
2 parents c9ad37a + d755201 commit e7614c2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
3 changes: 1 addition & 2 deletions ssz_serialization.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ task test, "Run all tests":
for blst in [false, true]:
for hashtree in [false, true]:
let opts = "--threads:on -d:PREFER_BLST_SHA256=" & $blst & " -d:PREFER_HASHTREE_SHA256=" & $hashtree
run "--mm:refc " & opts, "tests/test_all"
run "--mm:orc " & opts, "tests/test_all"
run opts, "tests/test_all"

task fuzzHashtree, "Run fuzzing test":
# TODO We don't run because the timeout parameter doesn't seem to work so
Expand Down
52 changes: 52 additions & 0 deletions ssz_serialization/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,58 @@ func fixedPortionSize*(T0: type): int {.compileTime.} =
else:
unsupported T0

func minSize*(T0: type): int {.compileTime.} =
mixin enumAllSerializedFields, toSszType

type T = type toSszType(declval T0)

when isFixedSize(T):
fixedPortionSize(T)
elif T is array|HashArray:
type E = ElemType(T)
static: doAssert not isFixedSize(E)
T.len * (offsetSize + minSize(E))
elif T is List|HashList:
0
elif T is BitList:
1 # Trailing 1-bit
elif T is object:
var res = fixedPortionSize(T)
enumAllSerializedFields(T):
when not isFixedSize(FieldType):
res += minSize(FieldType)
res
else:
unsupported T0

func maxSize*(T0: type): int {.compileTime.} =
mixin enumAllSerializedFields, toSszType

type T = type toSszType(declval T0)

when isFixedSize(T):
fixedPortionSize(T)
elif T is array|HashArray:
type E = ElemType(T)
static: doAssert not isFixedSize(E)
T.len * (offsetSize + maxSize(E))
elif T is List|HashList:
type E = ElemType(T)
when isFixedSize(E):
T.maxLen * fixedPortionSize(E)
else:
T.maxLen * (offsetSize + maxSize(E))
elif T is BitList:
((T.maxLen + 1) + 7) div 8
elif T is object:
var res = fixedPortionSize(T)
enumAllSerializedFields(T):
when not isFixedSize(FieldType):
res += maxSize(FieldType)
res
else:
unsupported T0

iterator fieldInfos(RecordType: type): tuple[name: string,
offset: int,
fixedSize: int,
Expand Down
32 changes: 32 additions & 0 deletions tests/test_ssz_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,45 @@ static:
doAssert fixedPortionSize(array[SomeEnum, DistinctInt]) == 24
doAssert fixedPortionSize(array[3..5, List[byte, 256]]) == 12

doAssert minSize(array[10, bool]) == 10
doAssert minSize(array[SomeEnum, uint64]) == 24
doAssert minSize(array[SomeEnum, DistinctInt]) == 24
doAssert minSize(array[3..5, List[byte, 256]]) == 3 * 4

doAssert maxSize(array[10, bool]) == 10
doAssert maxSize(array[SomeEnum, uint64]) == 24
doAssert maxSize(array[SomeEnum, DistinctInt]) == 24
doAssert maxSize(array[3..5, List[byte, 256]]) == 3 * (4 + 256)

doAssert isFixedSize(array[20, bool]) == true
doAssert isFixedSize(Simple) == true
doAssert isFixedSize(List[bool, 128]) == false

doAssert minSize(array[20, bool]) == 20
doAssert minSize(Simple) == 1 + 256 + 256
doAssert minSize(List[bool, 128]) == 0

doAssert maxSize(array[20, bool]) == 20
doAssert maxSize(Simple) == 1 + 256 + 256
doAssert maxSize(List[bool, 128]) == 128

doAssert isFixedSize(NonFixed) == false

doAssert minSize(NonFixed) == 4

doAssert maxSize(NonFixed) == 4 + 1024 * 8

doAssert minSize(array[20, BitList[24]]) == 20 * (4 + 1)
doAssert minSize(HashList[BitList[24], 20]) == 0
doAssert minSize(HashList[NonFixed, 20]) == 0

doAssert maxSize(array[20, BitList[24]]) == 20 * (4 + 4)
doAssert maxSize(HashList[BitList[24], 20]) == 20 * (4 + 4)
doAssert maxSize(HashList[NonFixed, 20]) == 20 * (4 + (4 + 1024 * 8))

reject fixedPortionSize(int)
reject minSize(int)
reject maxSize(int)

type
ObjWithFields = object
Expand Down

0 comments on commit e7614c2

Please sign in to comment.