Official state tests - parsing and loading beacon state (#227)

* initial commit of official state tests

* sanity check fixture

* Parsing official state test is mostly working
(Except BLS signature)

* Successfully load state test

* Use json-serialization instead of json and display deserialized and from scratch beacon state hashes

* Add official state test as a smoke parsing test
This commit is contained in:
Mamy Ratsimbazafy 2019-04-02 16:50:24 +02:00 committed by GitHub
parent ff7adcefbd
commit ad133a0222
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 44155 additions and 1 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "tests/official/eth2.0-tests"]
path = tests/official/eth2.0-tests
url = https://github.com/ethereum/eth2.0-tests

View File

@ -16,4 +16,5 @@ import
./test_ssz,
./test_state_transition,
./test_sync_protocol,
./test_validator
./test_validator,
./official/test_fixture_state

18
tests/official/README.md Normal file
View File

@ -0,0 +1,18 @@
Note on serialization hacks:
### FAR_FUTURE_SLOT (18446744073709551615)
The FAR_FUTURE_SLOT (18446744073709551615) has been rewritten as a string **in the YAML file**
as it's 2^64-1 and Nim by default try to parse it into a int64 (which can represents up to 2^63-1).
The YAML file is then converted to JSON for easy input to the json serialization/deserialization
with beacon chain type support.
"18446744073709551615" is then replaced again by uint64 18446744073709551615.
### Compressed signature
In `latest_block_header` field, the signatures and randao_reveals are
`"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"`
but that is not a valid compressed BLS signature, the zero signature should be:
`"0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"`

@ -0,0 +1 @@
Subproject commit 3ec28295b0c8365f0ec7ad79cfe933755021ee1b

View File

@ -0,0 +1,153 @@
# 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 lib
json, streams, strutils,
# Dependencies
yaml.tojson,
# Status libs
blscurve, nimcrypto, byteutils,
eth/common, serialization, json_serialization,
# Beacon chain internals
../../beacon_chain/spec/[datatypes, crypto, digest]
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
title*: string
summary*: string
test_suite*: string
fork*: string
test_cases*: seq[TestCase]
TestConstants* = object
SHARD_COUNT*: int
TARGET_COMMITTEE_SIZE*: int
MAX_BALANCE_CHURN_QUOTIENT*: int
MAX_INDICES_PER_SLASHABLE_VOTE*: int
MAX_EXIT_DEQUEUES_PER_EPOCH*: int
SHUFFLE_ROUND_COUNT*: int
DEPOSIT_CONTRACT_TREE_DEPTH*: int
MIN_DEPOSIT_AMOUNT*: uint64
MAX_DEPOSIT_AMOUNT*: uint64
FORK_CHOICE_BALANCE_INCREMENT*: uint64
EJECTION_BALANCE*: uint64
GENESIS_FORK_VERSION*: uint32
GENESIS_SLOT*: Slot
GENESIS_EPOCH*: Epoch
GENESIS_START_SHARD*: uint64
BLS_WITHDRAWAL_PREFIX_BYTE*: array[1, byte]
SECONDS_PER_SLOT*: uint64
MIN_ATTESTATION_INCLUSION_DELAY*: uint64
SLOTS_PER_EPOCH*: int
MIN_SEED_LOOKAHEAD*: int
ACTIVATION_EXIT_DELAY*: int
EPOCHS_PER_ETH1_VOTING_PERIOD*: uint64
SLOTS_PER_HISTORICAL_ROOT*: int
MIN_VALIDATOR_WITHDRAWABILITY_DELAY*: uint64
PERSISTENT_COMMITTEE_PERIOD*: uint64
LATEST_RANDAO_MIXES_LENGTH*: int
LATEST_ACTIVE_INDEX_ROOTS_LENGTH*: int
LATEST_SLASHED_EXIT_LENGTH*: int
BASE_REWARD_QUOTIENT*: uint64
WHISTLEBLOWER_REWARD_QUOTIENT*: uint64
ATTESTATION_INCLUSION_REWARD_QUOTIENT*: uint64
INACTIVITY_PENALTY_QUOTIENT*: uint64
MIN_PENALTY_QUOTIENT*: int
MAX_PROPOSER_SLASHINGS*: int
MAX_ATTESTER_SLASHINGS*: int
MAX_ATTESTATIONS*: int
MAX_DEPOSITS*: int
MAX_VOLUNTARY_EXITS*: int
MAX_TRANSFERS*: int
DOMAIN_BEACON_BLOCK*: SignatureDomain
DOMAIN_RANDAO*: SignatureDomain
DOMAIN_ATTESTATION*: SignatureDomain
DOMAIN_DEPOSIT*: SignatureDomain
DOMAIN_VOLUNTARY_EXIT*: SignatureDomain
DOMAIN_TRANSFER*: SignatureDomain
TestCase* = object
name*: string
config*: TestConstants
verify_signatures*: bool
initial_state*: BeaconState
blocks*: seq[BeaconBlock]
expected_state*: ExpectedState
ExpectedState* = object
## TODO what is this?
slot*: Slot
# #######################
# Default init
proc default*(T: typedesc): T = discard
# #######################
# JSON deserialization
proc readValue*[N: static int](r: var JsonReader, a: var array[N, byte]) {.inline.} =
# Needed for;
# - BLS_WITHDRAWAL_PREFIX_BYTE
# - FOrk datatypes
# TODO: are all bytes and bytearray serialized as hex?
# if so export that to nim-eth
hexToByteArray(r.readValue(string), a)
proc parseStateTests*(jsonPath: string): StateTest =
try:
result = Json.loadFile(jsonPath, StateTest)
except SerializationError as err:
writeStackTrace()
stderr.write "Json load issue for file \"", jsonPath, "\"\n"
stderr.write err.formatMsg(jsonPath), "\n"
quit 1
# #######################
# Yaml to JSON conversion
proc yamlToJson*(file: string): seq[JsonNode] =
try:
let fs = openFileStream(file)
defer: fs.close()
result = fs.loadToJson()
except IOError:
echo "Exception when reading file: " & file
raise
except OverflowError:
echo "Overflow exception when parsing. Did you stringify 18446744073709551615 (-1)?"
raise
when isMainModule:
# Do not forget to stringify FAR_EPOCH_SLOT = 18446744073709551615 (-1) in the YAML file
# And unstringify it in the produced JSON file
import os, typetraits
const
DefaultYML = "tests/official/sanity-check_default-config_100-vals-first_test.yaml"
DefaultOutputPath = "tests/official/sanity-check_default-config_100-vals-first_test.json"
var fileName, outputPath: string
if paramCount() == 0:
fileName = DefaultYML
outputPath = DefaultOutputPath
elif paramCount() == 1:
fileName = paramStr(1)
outputPath = DefaultOutputPath
elif paramCount() >= 2:
fileName = paramStr(1)
outputPath = paramStr(2)
let jsonString = $DefaultYML.yamlToJson[0]
DefaultOutputPath.writeFile jsonString

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,53 @@
# 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 lib
ospaths, strutils, json, unittest,
# Beacon chain internals
../../beacon_chain/spec/[datatypes, crypto, digest, beaconstate],
../../beacon_chain/ssz,
# Test utilities
./fixtures_utils
const TestFolder = currentSourcePath.rsplit(DirSep, 1)[0]
const TestsPath = "/sanity-check_default-config_100-vals-first_test.json"
suite "Official - State tests": # Initializing a beacon state from the deposits
var stateTests: StateTest
test "Parsing the official state tests into Nimbus beacon types":
stateTests = parseStateTests(TestFolder & TestsPath)
doAssert $stateTests.test_cases[0].name == "test_empty_block_transition"
var initialState: BeaconState
test "Initializing from scratch a new beacon chain with the same constants and deposit configuration as official state":
var deposits: seq[Deposit]
var index = 0'u64
for v in stateTests.test_cases[0].initial_state.validator_registry:
deposits.add Deposit(
proof: default(array[DEPOSIT_CONTRACT_TREE_DEPTH, Eth2Digest]),
index: index,
deposit_data: DepositData(
amount: 32000000000'u64, # TODO: read that from validator_balances
timestamp: 0'u64, # TODO: not initialized in test
deposit_input: DepositInput(
pubkey: v.pubkey,
withdrawal_credentials: v.withdrawal_credentials,
proof_of_possession: default(ValidatorSig) # TODO: not initialized in test
)
)
)
initialState = get_genesis_beacon_state(
genesis_validator_deposits = deposits,
genesis_time = 0,
genesis_eth1_data = Eth1Data()
)
test "[For information] Comparing state hashes":
# TODO - Add official hashes when available
# TODO - Make that a blocking test requirement
echo "Deserialized state hash: 0x" & $stateTests.test_cases[0].initial_state.hash_tree_root()
echo "From-scratch state hash: 0x" & $initialState.hash_tree_root()