nimbus-eth2/tests/official/test_fixture_state.nim
Dustin Brody 6a47ca3c46
More 0.6.1 updates (#274)
* implement get_churn_limit from 0.6.1; update initiate_validator_exit and get_previous_epoch to 0.6.1; remove obsoleted/non-spec reduce_balance, replaced by decrease_balance; mark BeaconBlock as 0.6.1

* rename get_block_root to get_block_root_at_slot and introduce epoch-oriented get_block_root; implement get_attestation_slot, get_matching_source_attestations, get_matching_target_attestations, get_matching_head_attestations, 0.6.1 get_attesting_indices, get_unslashed_attesting_indices, get_attestation_deltas, process_rewards_and_penalties; rm get_inactivity_penalty

* update Validator and processVoluntaryExits to 0.6.1; rm obsolete inclusion_slots/inclusion_distances

* rm removed activate_validator; mark DepositData, misc values, Gwei values, Randao processing, state caching as 0.6.1; update GENESIS_SLOT to 64, since it doesn't quite yet work at 0

* mark BeaconBlockHeader as 0.6.1; update BeaconBlockBody to 0.6.1; rename WHISTLEBLOWER_REWARD_QUOTIENT to WHISTLEBLOWING_REWARD_QUOTIENT; update slash_validator to 0.6.1

* implement 0.6.2 is_slashable_validator; update processProposerSlashings to 0.6.2; mark state caching as 0.6.2

* mark get_total_active_balance and process_slashings as 0.6.2; update get_base_reward to 0.6.2

* rm prepare_validator_for_withdrawal, process_exit_queue; mark mainnet misc constants as 0.6.2; update process block header to 0.6.2

* address mratsim's code review comment
2019-05-29 10:08:56 +00:00

114 lines
5.1 KiB
Nim

# 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, strformat,
# Third parties
byteutils,
# Beacon chain internals
../../beacon_chain/spec/[datatypes, crypto, digest, beaconstate],
../../beacon_chain/[ssz, state_transition],
# Test utilities
./fixtures_utils
const TestFolder = currentSourcePath.rsplit(DirSep, 1)[0]
const TestsPath = "fixtures" / "json_tests" / "state" / "sanity-check_default-config_100-vals.json"
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 = parseTests(TestFolder / TestsPath, StateTests)
doAssert $stateTests.test_cases[0].name == "test_empty_block_transition"
test "[For information - Non-blocking] Block root signing":
# TODO: Currently we are unable to use the official EF tests:
# - The provided zero signature "0x0000..." is an invalid compressed BLS signature
# - Block headers are using that signature
# - Block processing checks that block.previous_block_root == signing_root(state.latest_block_header)
# -> Changing EF provided previous_block_root would render the block transition tests meaningless
# -> Changing the signature to a valid "0xc000..." makes all hashes/signing_root wrong ...
#
# So we only test that block header signing in Nimbus matches block header signing from the EF
# And we can't deserialize from the raw YAML/JSON to avoid sanity checks on the signature
# TODO: Move that in an actual SSZ test suite
block: # sanity-check_default-config_100-vals.yaml - test "test_empty_block_transition"
let header = BeaconBlockHeader(
slot: Slot(4294967296),
previous_block_root: ZERO_HASH,
state_root: ZERO_HASH,
block_body_root: Eth2Digest(data:
hexToByteArray[32]("0x13f2001ff0ee4a528b3c43f63d70a997aefca990ed8eada2223ee6ec3807f7cc")
),
signature: ValidatorSig()
)
let previous_block_root = Eth2Digest(data:
hexToByteArray[32]("0x1179346f489d8be1731377cb199af5cc61faa38353e2d67e096bed182677062a")
)
echo " Expected previous block root: 0x", previous_block_root
echo " Computed header signed root: 0x", signing_root(header)
test "[For information] Print list of official tests to implement":
for i, test in stateTests.test_cases:
echo &" Test #{i:03}: {test.name}"
# test "Empty block transition":
# # TODO - assert that the constants match
# var state: BeaconState
# doAssert stateTests.test_cases[0].name == "test_empty_block_transition"
# template tcase(): untyped {.dirty.} =
# # Alias
# stateTests.test_cases[0]
# deepCopy(state, tcase.initial_state)
# # Use the provided empty block
# # Alternatively, generate one with `build_empty_block_for_next_slot`
# let blck = tcase.blocks[0]
# debugEcho blck.previous_block_root
# let ok = updateState(state, blck, flags = {})
# check:
# ok
# tcase.expected_state.eth1_data_votes.len == state.eth1_data_votes.len + 1
# get_block_root_at_slot(tcase.expected_state, state.slot) == blck.previous_block_root
suite "[For information - non-blocking] Extra state tests":
var initialState: BeaconState
test "Initializing from scratch a new beacon chain with the same constants and deposit configuration as official state test 0":
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,
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 "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()