Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Jun 13, 2019
1 parent 22f518a commit 5b4fff3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
47 changes: 21 additions & 26 deletions tests/official/fixtures_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ 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],
../../beacon_chain/ssz,
# 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
Expand All @@ -20,7 +27,7 @@ type
test_suite*: string
fork*: string
test_cases*: seq[StateTestCase]

TestConstants* = object
# TODO - 0.5.1 constants
SHARD_COUNT*: int
Expand Down Expand Up @@ -110,7 +117,7 @@ type

Domain = distinct uint64
## Domains have custom hex serialization

BLSPrivToPub* = object
input*: ValidatorPrivKey
output*: ValidatorPubKey
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -216,4 +211,4 @@ proc build_empty_block_for_next_slot*(state: BeaconState): BeaconBlock =
# prev_root,
# BeaconBlockBody()
# )
{.error: "Not implemented".}
{.error: "Not implemented".}
8 changes: 4 additions & 4 deletions tests/official/test_fixture_bls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/official/test_fixture_shuffling.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import
# Standard library
ospaths, strutils, json, unittest,
ospaths, strutils, unittest,
# Third parties

# Beacon chain internals
Expand All @@ -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:
Expand Down

0 comments on commit 5b4fff3

Please sign in to comment.