Implement shuffling test (pending typedesc and shuffling algo fix)

This commit is contained in:
Mamy André-Ratsimbazafy 2019-05-09 12:02:52 +02:00
parent 8cfbfdf565
commit ad9f643873
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
3 changed files with 74 additions and 8 deletions

View File

@ -12,14 +12,15 @@ export nimcrypto.toHex
type
# TODO: use ref object to avoid allocating
# so much on the stack - pending https://github.com/status-im/nim-json-serialization/issues/3
StateTest* = object
StateTests* = object
title*: string
summary*: string
test_suite*: string
fork*: string
test_cases*: seq[TestCase]
test_cases*: seq[StateTestCase]
TestConstants* = object
# TODO - 0.5.1 constants
SHARD_COUNT*: int
TARGET_COMMITTEE_SIZE*: int
MAX_BALANCE_CHURN_QUOTIENT*: int
@ -66,13 +67,30 @@ type
DOMAIN_VOLUNTARY_EXIT*: SignatureDomain
DOMAIN_TRANSFER*: SignatureDomain
TestCase* = object
StateTestCase* = object
name*: string
config*: TestConstants
verify_signatures*: bool
initial_state*: BeaconState
blocks*: seq[BeaconBlock]
expected_state*: BeaconState
ShufflingTests* = object
title*: string
summary*: string
forks_timeline*: string
forks*: seq[string]
config*: string
runner*: string
handler*: string
test_cases*: seq[ShufflingTestCase]
ShufflingTestCase* = object
seed*: Eth2Digest
count*: uint64
shuffled*: seq[ValidatorIndex]
Tests* = StateTests or ShufflingTests
# #######################
# Default init
@ -89,9 +107,27 @@ proc readValue*[N: static int](r: var JsonReader, a: var array[N, byte]) {.inlin
# if so export that to nim-eth
hexToByteArray(r.readValue(string), a)
proc parseStateTests*(jsonPath: string): StateTest =
proc readValue*(r: var JsonReader, a: var ValidatorIndex) {.inline.} =
a = r.readValue(uint32)
# TODO: cannot pass a typedesc
# proc parseTests*(jsonPath: string, T: typedesc[Tests]): T =
# # TODO: due to generic early symbol resolution
# # we cannot use a generic proc
# # Otherwise we get:
# # "Error: undeclared identifier: 'ReaderType'"
# # Templates, even untyped don't work
# try:
# result = Json.loadFile(jsonPath, T)
# except SerializationError as err:
# writeStackTrace()
# stderr.write "Json load issue for file \"", jsonPath, "\"\n"
# stderr.write err.formatMsg(jsonPath), "\n"
# quit 1
proc parseTests*(jsonPath: string): ShufflingTests =
try:
result = Json.loadFile(jsonPath, StateTest)
result = Json.loadFile(jsonPath, ShufflingTests)
except SerializationError as err:
writeStackTrace()
stderr.write "Json load issue for file \"", jsonPath, "\"\n"

View File

@ -0,0 +1,30 @@
# beacon_chain
# Copyright (c) 2018 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import
# Standard libs
ospaths, strutils, json, unittest,
# Third parties
# Beacon chain internals
../../beacon_chain/spec/validator,
# Test utilities
./fixtures_utils
const TestFolder = currentSourcePath.rsplit(DirSep, 1)[0]
const TestsPath = "fixtures" / "json_tests" / "shuffling" / "core" / "shuffling_full.json"
var shufflingTests: ShufflingTests
suite "Official - Shuffling tests":
test "Parsing the official shuffling tests":
shufflingTests = parseTests(TestFolder / TestsPath)
test "Shuffling a sequence of N validators":
for t in shufflingTests.test_cases:
let implResult = get_shuffled_seq(t.seed, t.count)
check: implResult == t.shuffled

View File

@ -14,17 +14,17 @@ import
../../beacon_chain/spec/[datatypes, crypto, digest, beaconstate],
../../beacon_chain/[ssz, state_transition],
# Test utilities
./state_test_utils
./fixtures_utils
const TestFolder = currentSourcePath.rsplit(DirSep, 1)[0]
const TestsPath = "fixtures" / "json_tests" / "state" / "sanity-check_default-config_100-vals.json"
var stateTests: StateTest
var stateTests: StateTests
suite "Official - State tests": # Initializing a beacon state from the deposits
# Source: https://github.com/ethereum/eth2.0-specs/blob/2baa242ac004b0475604c4c4ef4315e14f56c5c7/tests/phase0/test_sanity.py#L55-L460
test "Parsing the official state tests into Nimbus beacon types":
stateTests = parseStateTests(TestFolder / TestsPath)
stateTests = parseTests(TestFolder / TestsPath, StateTests) # TODO pending typedesc fix in fixture_utils.nim
doAssert $stateTests.test_cases[0].name == "test_empty_block_transition"
test "[For information - Non-blocking] Block root signing":