diff --git a/tests/official/fixtures_utils.nim b/tests/official/fixtures_utils.nim index ae7091bf23..8b7040f7df 100644 --- a/tests/official/fixtures_utils.nim +++ b/tests/official/fixtures_utils.nim @@ -2,6 +2,7 @@ import # Status libs blscurve, nimcrypto, byteutils, eth/common, serialization, json_serialization, + stint, # Beacon chain internals # submodule in nim-beacon-chain/tests/official/fixtures/ ../../beacon_chain/spec/[datatypes, crypto, digest], @@ -9,7 +10,13 @@ import # Workarounds endians # parseHex into uint64 -export nimcrypto.toHex +export + nimcrypto.toHex, + # Workaround: + # - https://github.com/status-im/nim-serialization/issues/4 + # - https://github.com/status-im/nim-serialization/issues/5 + # - https://github.com/nim-lang/Nim/issues/11225 + serialization.readValue type # TODO: use ref object to avoid allocating @@ -20,7 +27,7 @@ type test_suite*: string fork*: string test_cases*: seq[StateTestCase] - + TestConstants* = object # TODO - 0.5.1 constants SHARD_COUNT*: int @@ -110,7 +117,7 @@ type Domain = distinct uint64 ## Domains have custom hex serialization - + BLSPrivToPub* = object input*: ValidatorPrivKey output*: ValidatorPubKey @@ -131,7 +138,14 @@ type BLSAggPubKey* = object input*: seq[ValidatorPubKey] output*: ValidatorPubKey - + + SSZUint* = object + `type`*: string + value*: string + valid*: bool + ssz*: seq[byte] + tags*: seq[string] + # ####################### # Default init proc default*(T: typedesc): T = discard @@ -163,34 +177,15 @@ proc readValue*(r: var JsonReader, a: var seq[byte]) {.inline.} = ## Custom deserializer for seq[byte] a = hexToSeqByte(r.readValue(string)) -template parseTestsImpl(T: untyped) {.dirty.} = - # TODO: workaround typedesc/generics - # being broken with nim-serialization - # - https://github.com/status-im/nim-serialization/issues/4 - # - https://github.com/status-im/nim-serialization/issues/5 +proc parseTests*(jsonPath: string, T: typedesc): Tests[T] = try: - result = Json.loadFile(jsonPath, T) + result = Json.loadFile(jsonPath, Tests[T]) except SerializationError as err: writeStackTrace() stderr.write "Json load issue for file \"", jsonPath, "\"\n" stderr.write err.formatMsg(jsonPath), "\n" quit 1 -proc parseTestsShuffling*(jsonPath: string): Tests[Shuffling] = - parseTestsImpl(Tests[Shuffling]) - -proc parseTestsBLSPrivToPub*(jsonPath: string): Tests[BLSPrivToPub] = - parseTestsImpl(Tests[BLSPrivToPub]) - -proc parseTestsBLSSignMsg*(jsonPath: string): Tests[BLSSignMsg] = - parseTestsImpl(Tests[BLSSignMsg]) - -proc parseTestsBLSAggSig*(jsonPath: string): Tests[BLSAggSig] = - parseTestsImpl(Tests[BLSAggSig]) - -proc parseTestsBLSAggPubKey*(jsonPath: string): Tests[BLSAggPubKey] = - parseTestsImpl(Tests[BLSAggPubKey]) - # ####################### # Mocking helpers # https://github.com/ethereum/eth2.0-specs/blob/75f0af45bb0613bb406fc72d10266cee4cfb402a/tests/phase0/helpers.py#L107 @@ -216,4 +211,4 @@ proc build_empty_block_for_next_slot*(state: BeaconState): BeaconBlock = # prev_root, # BeaconBlockBody() # ) - {.error: "Not implemented".} \ No newline at end of file + {.error: "Not implemented".} diff --git a/tests/official/test_fixture_bls.nim b/tests/official/test_fixture_bls.nim index c726054320..0f197d0f2a 100644 --- a/tests/official/test_fixture_bls.nim +++ b/tests/official/test_fixture_bls.nim @@ -25,10 +25,10 @@ var suite "Official - BLS tests": test "Parsing the official BLS tests": - blsPrivToPubTests = parseTestsBLSPrivToPub(TestFolder / TestsPath / "priv_to_pub" / "priv_to_pub.json") - blsSignMsgTests = parseTestsBLSSignMsg(TestFolder / TestsPath / "sign_msg" / "sign_msg.json") - blsAggSigTests = parseTestsBLSAggSig(TestFolder / TestsPath / "aggregate_sigs" / "aggregate_sigs.json") - blsAggPubKeyTests = parseTestsBLSAggPubKey(TestFolder / TestsPath / "aggregate_pubkeys" / "aggregate_pubkeys.json") + blsPrivToPubTests = parseTests(TestFolder / TestsPath / "priv_to_pub" / "priv_to_pub.json", BLSPrivToPub) + blsSignMsgTests = parseTests(TestFolder / TestsPath / "sign_msg" / "sign_msg.json", BLSSignMsg) + blsAggSigTests = parseTests(TestFolder / TestsPath / "aggregate_sigs" / "aggregate_sigs.json", BLSAggSig) + blsAggPubKeyTests = parseTests(TestFolder / TestsPath / "aggregate_pubkeys" / "aggregate_pubkeys.json", BLSAggPubKey) test "Private to public key conversion": for t in blsPrivToPubTests.test_cases: diff --git a/tests/official/test_fixture_shuffling.nim b/tests/official/test_fixture_shuffling.nim index 4633714906..594440e4e9 100644 --- a/tests/official/test_fixture_shuffling.nim +++ b/tests/official/test_fixture_shuffling.nim @@ -7,7 +7,7 @@ import # Standard library - ospaths, strutils, json, unittest, + ospaths, strutils, unittest, # Third parties # Beacon chain internals @@ -27,7 +27,7 @@ var shufflingTests: Tests[Shuffling] suite "Official - Shuffling tests [Preset: " & preset(): test "Parsing the official shuffling tests [Preset: " & preset(): - shufflingTests = parseTestsShuffling(TestFolder / TestsPath) + shufflingTests = parseTests(TestFolder / TestsPath, Shuffling) test "Shuffling a sequence of N validators" & preset(): for t in shufflingTests.test_cases: