2019-09-03 18:02:21 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-Present Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
2019-09-03 18:02:21 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2019-11-14 10:47:55 +00:00
|
|
|
{.used.}
|
|
|
|
|
2019-09-03 18:02:21 +00:00
|
|
|
import
|
|
|
|
# Standard library
|
2019-11-13 11:30:11 +00:00
|
|
|
os, unittest,
|
2019-09-03 18:02:21 +00:00
|
|
|
# Beacon chain internals
|
2019-12-16 18:08:50 +00:00
|
|
|
../../beacon_chain/spec/[crypto, datatypes],
|
2019-09-03 18:02:21 +00:00
|
|
|
../../beacon_chain/[ssz, state_transition, extras],
|
|
|
|
# Test utilities
|
|
|
|
../testutil,
|
|
|
|
./fixtures_utils,
|
2019-11-06 15:02:06 +00:00
|
|
|
../helpers/debug_state
|
2019-09-03 18:02:21 +00:00
|
|
|
|
2019-11-13 11:30:11 +00:00
|
|
|
const SanityBlocksDir = SszTestsDir/const_preset/"phase0"/"sanity"/"blocks"/"pyspec_tests"
|
2019-09-03 18:02:21 +00:00
|
|
|
|
|
|
|
template runValidTest(testName: string, identifier: untyped, num_blocks: int): untyped =
|
|
|
|
# We wrap the tests in a proc to avoid running out of globals
|
|
|
|
# in the future: Nim supports up to 3500 globals
|
|
|
|
# but unittest with the macro/templates put everything as globals
|
|
|
|
# https://github.com/nim-lang/Nim/issues/12084#issue-486866402
|
|
|
|
|
|
|
|
const testDir = SanityBlocksDir / astToStr(identifier)
|
|
|
|
|
|
|
|
proc `testImpl _ blck _ identifier`() =
|
2019-12-05 10:27:00 +00:00
|
|
|
timedTest "[Valid] " & testName & " (" & astToStr(identifier) & ")":
|
2019-09-03 18:02:21 +00:00
|
|
|
var stateRef, postRef: ref BeaconState
|
|
|
|
new stateRef
|
|
|
|
new postRef
|
|
|
|
stateRef[] = parseTest(testDir/"pre.ssz", SSZ, BeaconState)
|
|
|
|
postRef[] = parseTest(testDir/"post.ssz", SSZ, BeaconState)
|
|
|
|
|
|
|
|
for i in 0 ..< num_blocks:
|
2019-12-16 18:08:50 +00:00
|
|
|
let blck = parseTest(testDir/"blocks_" & $i & ".ssz", SSZ, SignedBeaconBlock)
|
2019-09-03 18:02:21 +00:00
|
|
|
|
|
|
|
# TODO: The EF is using invalid BLS keys so we can't verify them
|
2019-12-16 18:08:50 +00:00
|
|
|
let success = state_transition(stateRef[], blck.message, flags = {skipValidation})
|
2019-09-03 18:02:21 +00:00
|
|
|
doAssert success, "Failure when applying block " & $i
|
|
|
|
|
|
|
|
# Checks:
|
|
|
|
# check: stateRef.hash_tree_root() == postRef.hash_tree_root()
|
2019-11-14 19:03:08 +00:00
|
|
|
reportDiff(stateRef, postRef)
|
2019-09-03 18:02:21 +00:00
|
|
|
|
|
|
|
`testImpl _ blck _ identifier`()
|
|
|
|
|
|
|
|
suite "Official - Sanity - Blocks " & preset():
|
2019-12-05 10:27:00 +00:00
|
|
|
timedTest "[Invalid] Previous slot block transition (prev_slot_block_transition)":
|
2019-09-03 18:02:21 +00:00
|
|
|
const testDir = SanityBlocksDir/"prev_slot_block_transition"
|
|
|
|
var stateRef: ref BeaconState
|
|
|
|
new stateRef
|
|
|
|
stateRef[] = parseTest(testDir/"pre.ssz", SSZ, BeaconState)
|
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
let blck = parseTest(testDir/"blocks_0.ssz", SSZ, SignedBeaconBlock)
|
2019-09-03 18:02:21 +00:00
|
|
|
|
|
|
|
# Check that a block build for an old slot cannot be used for state transition
|
|
|
|
expect(AssertionError):
|
|
|
|
# assert in process_slots. This should not be triggered
|
|
|
|
# for blocks from block_pool/network
|
2019-12-16 18:08:50 +00:00
|
|
|
discard state_transition(stateRef[], blck.message, flags = {skipValidation})
|
2019-09-03 18:02:21 +00:00
|
|
|
|
|
|
|
runValidTest("Same slot block transition", same_slot_block_transition, 1)
|
|
|
|
runValidTest("Empty block transition", empty_block_transition, 1)
|
|
|
|
|
|
|
|
when false: # TODO: we need more granular skipValidation
|
2019-12-05 10:27:00 +00:00
|
|
|
timedTest "[Invalid] Invalid state root":
|
2019-09-03 18:02:21 +00:00
|
|
|
const testDir = SanityBlocksDir/"invalid_state_root"
|
|
|
|
var stateRef: ref BeaconState
|
|
|
|
new stateRef
|
|
|
|
stateRef[] = parseTest(testDir/"pre.ssz", SSZ, BeaconState)
|
|
|
|
|
|
|
|
let blck = parseTest(testDir/"blocks_0.ssz", SSZ, BeaconBlock)
|
|
|
|
|
|
|
|
expect(AssertionError):
|
2019-11-18 12:48:41 +00:00
|
|
|
discard state_transition(stateRef[], blck, flags = {skipValidation})
|
2019-09-03 18:02:21 +00:00
|
|
|
|
|
|
|
runValidTest("Skipped Slots", skipped_slots, 1)
|
2019-09-12 21:28:33 +00:00
|
|
|
runValidTest("Empty epoch transition", empty_epoch_transition, 1)
|
|
|
|
when const_preset=="minimal":
|
2019-11-01 08:46:50 +00:00
|
|
|
runValidTest("Empty epoch transition not finalizing", empty_epoch_transition_not_finalizing, 1)
|
2019-11-10 00:03:41 +00:00
|
|
|
|
2019-11-13 11:30:11 +00:00
|
|
|
when false:
|
|
|
|
# TODO investigate/fix after 0.9.0 transition broke this in mainnet and
|
|
|
|
# in 0.9.1 even minimal broke. For the latter at least, it differs only
|
|
|
|
# in latest_block_header.body_root, which is just a hash_tree_root() of
|
|
|
|
# the one block read by this test case. All balances agree. It's an SSZ
|
|
|
|
# or hashing issue.
|
2019-11-10 00:03:41 +00:00
|
|
|
runValidTest("Attester slashing", attester_slashing, 1)
|
2019-09-03 18:02:21 +00:00
|
|
|
runValidTest("Proposer slashing", proposer_slashing, 1)
|
|
|
|
|
|
|
|
# TODO: Expected deposit in block
|
|
|
|
|
2019-11-13 11:30:11 +00:00
|
|
|
runValidTest("Deposit in block", deposit_in_block, 1)
|
2019-09-03 18:02:21 +00:00
|
|
|
runValidTest("Deposit top up", deposit_top_up, 1)
|
|
|
|
|
2019-11-13 11:30:11 +00:00
|
|
|
when const_preset=="minimal":
|
|
|
|
# TODO this doesn't work on mainnet
|
2019-09-03 18:02:21 +00:00
|
|
|
runValidTest("Attestation", attestation, 2)
|
2019-09-12 21:28:33 +00:00
|
|
|
runValidTest("Voluntary exit", voluntary_exit, 2)
|
|
|
|
runValidTest("Balance-driven status transitions", balance_driven_status_transitions, 1)
|
2019-09-09 13:10:03 +00:00
|
|
|
runValidTest("Historical batch", historical_batch, 1)
|
2019-09-12 21:28:33 +00:00
|
|
|
when const_preset=="minimal":
|
2019-11-01 08:46:50 +00:00
|
|
|
runValidTest("ETH1 data votes consensus", eth1_data_votes_consensus, 17)
|
|
|
|
runValidTest("ETH1 data votes no consensus", eth1_data_votes_no_consensus, 16)
|