enable uint128 / uint256 SSZ serialization tests (#2960)

So far, SSZ tests for `uint128` and `uint256` integers were disabled
due to "compile-time issues". The code has been adjusted to support
those additional test vectors, and the corresponding tests have been
enabled.
This commit is contained in:
Etan Kissling 2021-10-06 08:14:52 +02:00 committed by GitHub
parent 35a4f5b957
commit 55c2553193
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 37 deletions

View File

@ -325,6 +325,8 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
+ Overlong SSZ.decode: HashList[system.uint64, 32] OK
+ Overlong SSZ.decode: List[system.uint64, 32] OK
Overlong SSZ.decode: Simple Skip
Overlong SSZ.decode: UInt128 Skip
Overlong SSZ.decode: UInt256 Skip
Overlong SSZ.decode: array[0..31, byte] Skip
Overlong SSZ.decode: bool Skip
Overlong SSZ.decode: limb_t Skip
@ -337,6 +339,8 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
+ Overlong readSszBytes: HashList[system.uint64, 32] OK
+ Overlong readSszBytes: List[system.uint64, 32] OK
Overlong readSszBytes: Simple Skip
+ Overlong readSszBytes: UInt128 OK
+ Overlong readSszBytes: UInt256 OK
+ Overlong readSszBytes: array[0..31, byte] OK
+ Overlong readSszBytes: bool OK
+ Overlong readSszBytes: limb_t OK
@ -349,6 +353,8 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
+ Underlong SSZ.decode: HashList[system.uint64, 32] OK
+ Underlong SSZ.decode: List[system.uint64, 32] OK
+ Underlong SSZ.decode: Simple OK
+ Underlong SSZ.decode: UInt128 OK
+ Underlong SSZ.decode: UInt256 OK
+ Underlong SSZ.decode: array[0..31, byte] OK
+ Underlong SSZ.decode: bool OK
+ Underlong SSZ.decode: limb_t OK
@ -361,6 +367,8 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
+ Underlong readSszBytes: HashList[system.uint64, 32] OK
+ Underlong readSszBytes: List[system.uint64, 32] OK
+ Underlong readSszBytes: Simple OK
+ Underlong readSszBytes: UInt128 OK
+ Underlong readSszBytes: UInt256 OK
+ Underlong readSszBytes: array[0..31, byte] OK
+ Underlong readSszBytes: bool OK
+ Underlong readSszBytes: limb_t OK
@ -368,7 +376,7 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
+ Underlong readSszBytes: uint32 OK
+ Underlong readSszBytes: uint8 OK
```
OK: 36/48 Fail: 0/48 Skip: 12/48
OK: 42/56 Fail: 0/56 Skip: 14/56
---TOTAL---
OK: 201/213 Fail: 0/213 Skip: 12/213
OK: 207/221 Fail: 0/221 Skip: 14/221

View File

@ -2,8 +2,8 @@ FixtureSSZGeneric-mainnet
===
## Ethereum Foundation - SSZ generic types
```diff
Testing basic_vector inputs - invalid - skipping Vector[uint128, N] and Vector[uint256, N] Skip
+ Testing basic_vector inputs - valid - skipping Vector[uint128, N] and Vector[uint256, N] OK
Testing basic_vector inputs - invalid Skip
+ Testing basic_vector inputs - valid OK
+ Testing bitlist inputs - invalid OK
+ Testing bitlist inputs - valid OK
Testing bitvector inputs - invalid Skip
@ -12,8 +12,8 @@ FixtureSSZGeneric-mainnet
+ Testing boolean inputs - valid OK
+ Testing containers inputs - invalid - skipping BitsStruct OK
+ Testing containers inputs - valid - skipping BitsStruct OK
+ Testing uints inputs - invalid - skipping uint128 and uint256 OK
+ Testing uints inputs - valid - skipping uint128 and uint256 OK
+ Testing uints inputs - invalid OK
+ Testing uints inputs - valid OK
```
OK: 10/12 Fail: 0/12 Skip: 2/12

View File

@ -9,14 +9,13 @@ import stew/[results, base10, byteutils, endians2], presto/common,
libp2p/peerid, serialization,
json_serialization, json_serialization/std/[options, net],
nimcrypto/utils as ncrutils
import ".."/[forks, ssz_codec], ".."/datatypes/[phase0, altair, merge],
".."/eth2_ssz_serialization,
".."/".."/ssz/[ssz_serialization, codec, types],
import ".."/[eth2_ssz_serialization, forks],
".."/datatypes/[phase0, altair, merge],
"."/rest_types
export
results, peerid, common, serialization, json_serialization, options, net,
rest_types, ssz_codec, ssz_serialization, codec, types
eth2_ssz_serialization, rest_types
Json.createFlavor RestJson

View File

@ -413,7 +413,7 @@ template merkleizeFields(totalElements: static Limit, body: untyped): Eth2Digest
getFinalHash(merkleizer)
template writeBytesLE(chunk: var array[bytesPerChunk, byte], atParam: int,
val: SomeUnsignedInt) =
val: UintN) =
let at = atParam
chunk[at ..< at + sizeof(val)] = toBytesLE(val)
@ -528,7 +528,7 @@ func maxChunksCount(T: type, maxLen: Limit): Limit =
func hashTreeRootAux[T](x: T): Eth2Digest =
when T is bool|char:
result.data[0] = byte(x)
elif T is SomeUnsignedInt:
elif T is UintN:
when cpuEndian == bigEndian:
result.data[0..<sizeof(x)] = toBytesLE(x)
else:

View File

@ -9,20 +9,20 @@
import
std/[tables, typetraits, strformat],
stew/shims/macros, stew/[byteutils, bitops2, objects],
stew/shims/macros, stew/[byteutils, bitops2, objects], stint,
serialization/[object_serialization, errors],
json_serialization,
"."/[bitseqs],
../spec/digest
export bitseqs, json_serialization
export stint, bitseqs, json_serialization
const
offsetSize* = 4
bytesPerChunk* = 32
type
UintN* = SomeUnsignedInt
UintN* = SomeUnsignedInt|UInt128|UInt256
BasicType* = bool|UintN
Limit* = int64

View File

@ -10,7 +10,7 @@ import
os, strutils, streams, strformat, strscans,
macros, typetraits,
# Status libraries
faststreams, snappy, ../testutil,
faststreams, snappy, stint, ../testutil,
# Third-party
yaml,
# Beacon chain internals
@ -101,7 +101,7 @@ macro testVector(typeIdent: string, size: int): untyped =
# sizes: 1, 2, 3, 4, 5, 8, 16, 31, 512, 513
#
# We allocate in a ref array to not run out of stack space
let types = ["bool", "uint8", "uint16", "uint32", "uint64"] # "uint128", "uint256"]
let types = ["bool", "uint8", "uint16", "uint32", "uint64", "uint128", "uint256"]
let sizes = [1, 2, 3, 4, 5, 8, 16, 31, 512, 513]
var dispatcher = nnkIfStmt.newTree()
@ -111,7 +111,11 @@ macro testVector(typeIdent: string, size: int): untyped =
for s in sizes:
# if size == s // elif size == s
let T = nnkBracketExpr.newTree(
ident"array", newLit(s), ident(t)
ident"array", newLit(s),
case t
of "uint128": ident("UInt128")
of "uint256": ident("UInt256")
else: ident(t)
)
var testStmt = quote do:
checkBasic(`T`, dir, expectedHash)
@ -128,8 +132,6 @@ macro testVector(typeIdent: string, size: int): untyped =
sizeDispatch
)
dispatcher.add nnkElse.newTree quote do:
# TODO: support uint128 and uint256
if `typeIdent` != "uint128" and `typeIdent` != "uint256":
raise newException(ValueError,
"Unsupported **type** in type/size combination: array[" &
$`size` & ", " & `typeIdent` & ']')
@ -208,12 +210,8 @@ proc sszCheck(baseDir, sszType, sszSubType: string) =
of 16: checkBasic(uint16, dir, expectedHash)
of 32: checkBasic(uint32, dir, expectedHash)
of 64: checkBasic(uint64, dir, expectedHash)
of 128:
# Compile-time issues
discard # checkBasic(Stuint[128], dir, expectedHash)
of 256:
# Compile-time issues
discard # checkBasic(Stuint[256], dir, expectedHash)
of 128: checkBasic(UInt128, dir, expectedHash)
of 256: checkBasic(UInt256, dir, expectedHash)
else:
raise newException(ValueError, "unknown uint in test: " & sszSubType)
of "basic_vector": checkVector(sszSubType, dir, expectedHash)
@ -252,10 +250,6 @@ suite "Ethereum Foundation - SSZ generic types":
var skipped: string
case sszType
of "uints":
skipped = " - skipping uint128 and uint256"
of "basic_vector":
skipped = " - skipping Vector[uint128, N] and Vector[uint256, N]"
of "containers":
skipped = " - skipping BitsStruct"

View File

@ -343,6 +343,8 @@ suite "underlong values":
testit(default(uint16))
testit(default(uint32))
testit(default(uint64))
testit(default(UInt128))
testit(default(UInt256))
testit(default(array[32, uint8]))
testit(default(HashArray[32, uint8]))
testit(List[uint64, 32].init(@[42'u64]))