nimbus-eth2/tests/spec_epoch_processing/test_process_crosslinks.nim

121 lines
4.2 KiB
Nim
Raw Normal View History

v0.8.1 tests refactor (#326) * Introduce new mocking proc to replace: - makeFakeValidatorPrivKey - hackPrivKey - getNextBeaconProposerIndex - addBlock - makeBlock * Add comments on datastructure unsynced with the spec * Add merkle tree constructor and initial mocking for deposits (missing merkle proofs) * [Mock] Implement sparse merkle tree and merkle proof builder * [Mocking] Genesis deposits * Add compact_committees_roots init + mock genesis state * [Tests] Add first deposit test using the new mocking procedures * [Tests -deposits] add at and over 32 ETH deposit tests * [Tests - deposits] Add test for validator top-up * [Tests -deposits] Mention the TODO to test for invalid conditions * [Tests] Add stub to test "is_valid_genesis_state" * [Merkle proofs] Implement round-trip checks * Deactivate roundtrips test * SSZ - use EF convention for hash_tree_root / hashTreeRoot * [Tests - Attestation] Attestation mocking + initial test * Add mocking + 3 new tests for valid attestations + mention future invalid attestation tests * Add crosslinks test (1 failing to attestations in block being duplicated in state transition) * Single attestation crosslink test - workaround https://github.com/status-im/nim-beacon-chain/issues/361 * Add test for failed crosslink penalty * Rebase fixes + add refactored tests to test suite * justif-finalization helpers first batch * Add 234 finalization tests * Fix justif test, Rule I 234 finalization does not happen with sufficient support. (Also unittest check template does not fail properly in some cases) * Add tests for all finalization rules * Properly delete nim-byteutils following https://github.com/status-im/nim-beacon-chain/commit/c91727e7e5f9c7a95a634055e94e9d3bcb6cc41a#diff-7c3613dba5171cb6027c67835dd3b9d4 * use digest helper for deposit root
2019-08-28 12:07:00 +00:00
# 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.
# process_crosslinks (state_transition_epoch.nim)
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.2/specs/core/0_beacon-chain.md#crosslinks
# ---------------------------------------------------------------
import
# Standard library
unittest,
# Specs
../../beacon_chain/spec/[beaconstate, datatypes, validator, helpers, state_transition_epoch],
# Internals
# Mock helpers
../mocking/[mock_genesis, mock_attestations, mock_state, mock_blocks],
./epoch_utils,
../testutil
suite "[Unit - Spec - Epoch processing] Crosslinks " & preset():
const NumValidators = uint64(8) * SLOTS_PER_EPOCH
let genesisState = initGenesisState(NumValidators)
doAssert genesisState.validators.len == int NumValidators
var state: BeaconState
template resetState: untyped =
deepCopy(state, genesisState)
test "No attestations":
resetState()
transitionEpochUntilCrosslinks(state)
for shard in 0 ..< SHARD_COUNT:
check state.previous_crosslinks[shard] == state.current_crosslinks[shard]
test "Single crosslink update from current epoch":
resetState()
nextEpoch(state)
var attestation = mockAttestation(state)
fillAggregateAttestation(state, attestation)
state.add(attestation, state.slot + MIN_ATTESTATION_INCLUSION_DELAY)
# TODO: all attestations are duplicated at the moment
# pending fix of https://github.com/status-im/nim-beacon-chain/issues/361
check: state.current_epoch_attestations.len == 2
# For sanity checks
let shard = attestation.data.crosslink.shard
let pre_crosslink = state.current_crosslinks[shard]
transitionEpochUntilCrosslinks(state)
check:
state.previous_crosslinks[shard] != state.current_crosslinks[shard]
pre_crosslink != state.current_crosslinks[shard]
test "Double late crosslink":
resetState()
if get_committee_count(state, get_current_epoch(state)) < SHARD_COUNT:
echo " [Warning] Skipping Double-late crosslink test: Committee.len < SHARD_COUNT for preset " & const_preset
else:
nextEpoch(state)
state.slot += 4
var attestation_1 = mockAttestation(state)
fillAggregateAttestation(state, attestation_1)
# Add attestation_1 to next epoch
nextEpoch(state)
state.add(attestation_1, state.slot + 1)
var attestation_2: Attestation
for _ in 0 ..< SLOTS_PER_EPOCH:
attestation_2 = mockAttestation(state)
if attestation_2.data.crosslink.shard == attestation_1.data.crosslink.shard:
signMockAttestation(state, attestation_2)
break
nextSlot(state)
applyEmptyBlock(state)
fillAggregateAttestation(state, attestation_2)
# Add attestation_2 in the next epoch after attestation_1 has already
# updated the relevant crosslink
nextEpoch(state)
state.add(attestation_2, state.slot + 1)
# TODO: all attestations are duplicated at the moment
# pending fix of https://github.com/status-im/nim-beacon-chain/issues/361
check: state.previous_epoch_attestations.len == 2
check: state.current_epoch_attestations.len == 0
var cache = get_empty_per_epoch_cache()
let crosslink_deltas = get_crosslink_deltas(state, cache)
transitionEpochUntilCrosslinks(state)
let shard = attestation_2.data.crosslink.shard
# ensure that the current crosslinks were not updated by the second attestation
check: state.previous_crosslinks[shard] == state.current_crosslinks[shard]
# ensure no reward, only penalties for the failed crosslink
for index in get_crosslink_committee(
state,
attestation_2.data.target.epoch,
attestation_2.data.crosslink.shard,
cache
):
check:
crosslink_deltas[0][index] == 0.Gwei
crosslink_deltas[1][index] > 0.Gwei