From 45dd12cf3fcfaa1ca155c2738f0d39e783d9d553 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Tue, 28 Jan 2020 22:24:45 +0100 Subject: [PATCH 1/3] update process_deposit() to actually check is_valid_merkle_branch() unless skipValidation specified --- beacon_chain/spec/beaconstate.nim | 23 ++++++++--------------- research/state_sim.nim | 5 +++-- tests/test_beaconstate.nim | 7 ++++--- tests/test_state_transition.nim | 6 +++--- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 45a46cabb..e5e14bbec 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -47,27 +47,20 @@ func decrease_balance*( else: state.balances[index] - delta -# https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#deposits -func process_deposit*( +# https://github.com/ethereum/eth2.0-specs/blob/v0.9.4/specs/core/0_beacon-chain.md#deposits +proc process_deposit*( state: var BeaconState, deposit: Deposit, flags: UpdateFlags = {}): bool {.nbench.}= # Process an Eth1 deposit, registering a validator or increasing its balance. # Verify the Merkle branch - # TODO enable this check, but don't use doAssert - if not is_valid_merkle_branch( - hash_tree_root(deposit.getDepositMessage), - deposit.proof, - DEPOSIT_CONTRACT_TREE_DEPTH, - state.eth1_deposit_index, + if skipValidation notin flags and not is_valid_merkle_branch( + hash_tree_root(deposit.data), + deposit.proof, + DEPOSIT_CONTRACT_TREE_DEPTH + 1, + state.eth1_deposit_index, state.eth1_data.deposit_root, ): - ## TODO: a notice-like mechanism which works in a func - ## here and elsewhere, one minimal approach is a check-if-true - ## and return false iff so. - ## obviously, better/more principled ones exist, but - ## generally require broader rearchitecting, and this is what - ## mostly happens now, just haphazardly. - discard + return false # Deposits must be processed in order state.eth1_deposit_index += 1 diff --git a/research/state_sim.nim b/research/state_sim.nim index b980cab08..361caf8b7 100644 --- a/research/state_sim.nim +++ b/research/state_sim.nim @@ -1,5 +1,5 @@ # beacon_chain -# Copyright (c) 2019 Status Research & Development GmbH +# Copyright (c) 2019-2020 Status Research & Development GmbH # Licensed and distributed under either of # * 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). @@ -78,7 +78,8 @@ cli do(slots = SLOTS_PER_EPOCH * 6, let genesisState = - initialize_beacon_state_from_eth1(Eth2Digest(), 0, deposits, flags) + initialize_beacon_state_from_eth1( + Eth2Digest(), 0, deposits, {skipValidation}) genesisBlock = get_initial_beacon_block(genesisState) echo "Starting simulation..." diff --git a/tests/test_beaconstate.nim b/tests/test_beaconstate.nim index a9de1ecb5..26f81a8c1 100644 --- a/tests/test_beaconstate.nim +++ b/tests/test_beaconstate.nim @@ -1,5 +1,5 @@ # beacon_chain -# Copyright (c) 2018 Status Research & Development GmbH +# Copyright (c) 2018-2020 Status Research & Development GmbH # Licensed and distributed under either of # * 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). @@ -10,11 +10,12 @@ import times, unittest, ./testutil, ./testblockutil, - ../beacon_chain/spec/[beaconstate, datatypes, digest] + ../beacon_chain/spec/[beaconstate, datatypes, digest], + ../beacon_chain/extras suite "Beacon state" & preset(): timedTest "Smoke test initialize_beacon_state_from_eth1" & preset(): let state = initialize_beacon_state_from_eth1( Eth2Digest(), 0, - makeInitialDeposits(SLOTS_PER_EPOCH, {}), {}) + makeInitialDeposits(SLOTS_PER_EPOCH, {}), {skipValidation}) check: state.validators.len == SLOTS_PER_EPOCH diff --git a/tests/test_state_transition.nim b/tests/test_state_transition.nim index e067e34a7..566ac7e06 100644 --- a/tests/test_state_transition.nim +++ b/tests/test_state_transition.nim @@ -1,5 +1,5 @@ # beacon_chain -# Copyright (c) 2018 Status Research & Development GmbH +# Copyright (c) 2018-2020 Status Research & Development GmbH # Licensed and distributed under either of # * 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). @@ -11,7 +11,7 @@ import unittest, ./testutil, ./testblockutil, ../beacon_chain/spec/[beaconstate, datatypes, digest, validator], - ../beacon_chain/[state_transition, ssz] + ../beacon_chain/[extras, state_transition, ssz] suite "Block processing" & preset(): ## For now just test that we can compile and execute block processing with @@ -22,7 +22,7 @@ suite "Block processing" & preset(): # TODO bls verification is a bit of a bottleneck here genesisState = initialize_beacon_state_from_eth1( Eth2Digest(), 0, - makeInitialDeposits(), {}) + makeInitialDeposits(), {skipValidation}) genesisBlock = get_initial_beacon_block(genesisState) genesisRoot = hash_tree_root(genesisBlock.message) From 9748b2606ecd6f1c80567b6aa5b0626f4c5706f6 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Thu, 30 Jan 2020 11:54:15 +0100 Subject: [PATCH 2/3] update attestations and voluntary exit operations test to 0.10.1 --- .../test_fixture_operations_attestations.nim | 29 ++++++++++++------- ...test_fixture_operations_voluntary_exit.nim | 10 ++++--- tests/official/test_fixture_sanity_slots.nim | 2 ++ 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/tests/official/test_fixture_operations_attestations.nim b/tests/official/test_fixture_operations_attestations.nim index 4127c99b0..8a9d6ff6e 100644 --- a/tests/official/test_fixture_operations_attestations.nim +++ b/tests/official/test_fixture_operations_attestations.nim @@ -66,18 +66,27 @@ template runTest(testName: string, identifier: untyped) = `testImpl _ operations_attestations _ identifier`() suite "Official - Operations - Attestations " & preset(): - runTest("success", success) - runTest("success previous epoch", success_previous_epoch) - runTest("invalid attestation signature", invalid_attestation_signature) - runTest("before inclusion delay", before_inclusion_delay) + # https://github.com/ethereum/eth2.0-spec-tests/tree/v0.10.1/tests/minimal/phase0/operations/attestation/pyspec_tests + # https://github.com/ethereum/eth2.0-spec-tests/tree/v0.10.1/tests/mainnet/phase0/operations/attestation/pyspec_tests runTest("after_epoch_slots", after_epoch_slots) + runTest("bad source root", bad_source_root) + runTest("before inclusion delay", before_inclusion_delay) + runTest("empty aggregation bits", empty_aggregation_bits) + runTest("future target epoch", future_target_epoch) + runTest("invalid attestation signature", invalid_attestation_signature) + runTest("invalid current source root", invalid_current_source_root) + runTest("invalid index", invalid_index) + runTest("mismatched target and slot", mismatched_target_and_slot) + runTest("new source epoch", new_source_epoch) runTest("old source epoch", old_source_epoch) runTest("old target epoch", old_target_epoch) - runTest("future target epoch", future_target_epoch) - runTest("new source epoch", new_source_epoch) runTest("source root is target root", source_root_is_target_root) - runTest("invalid current source root", invalid_current_source_root) - runTest("bad source root", bad_source_root) - runTest("empty aggregation bits", empty_aggregation_bits) - runTest("too many aggregation bits", too_many_aggregation_bits) + runTest("success", success) + runTest("success multi-proposer index interations", + success_multi_proposer_index_iterations) + runTest("success previous epoch", success_previous_epoch) runTest("too few aggregation bits", too_few_aggregation_bits) + runTest("too many aggregation bits", too_many_aggregation_bits) + runTest("wrong index for committee signature", + wrong_index_for_committee_signature) + runTest("wrong index for slot", wrong_index_for_slot) diff --git a/tests/official/test_fixture_operations_voluntary_exit.nim b/tests/official/test_fixture_operations_voluntary_exit.nim index 00521f631..943921e6d 100644 --- a/tests/official/test_fixture_operations_voluntary_exit.nim +++ b/tests/official/test_fixture_operations_voluntary_exit.nim @@ -64,16 +64,18 @@ template runTest(identifier: untyped) = `testImpl _ voluntary_exit _ identifier`() suite "Official - Operations - Voluntary exit " & preset(): + # https://github.com/ethereum/eth2.0-spec-tests/tree/v0.10.1/tests/minimal/phase0/operations/voluntary_exit/pyspec_tests + # https://github.com/ethereum/eth2.0-spec-tests/tree/v0.10.1/tests/mainnet/phase0/operations/voluntary_exit/pyspec_tests runTest(success) when false: # TODO not sure how this particularly could falsely succeed runTest(invalid_signature) + runTest(validator_invalid_validator_index) + runTest(validator_already_exited) runTest(success_exit_queue) runTest(validator_exit_in_future) - runTest(validator_invalid_validator_index) - runTest(validator_not_active) - runTest(validator_already_exited) + runTest(default_exit_epoch_subsequent_exit) runTest(validator_not_active_long_enough) - + runTest(validator_not_active) diff --git a/tests/official/test_fixture_sanity_slots.nim b/tests/official/test_fixture_sanity_slots.nim index 8c6f69369..a20479750 100644 --- a/tests/official/test_fixture_sanity_slots.nim +++ b/tests/official/test_fixture_sanity_slots.nim @@ -47,6 +47,8 @@ template runTest(testName: string, identifier: untyped, num_slots: uint64): unty # --------------------------------------------------------------- suite "Official - Sanity - Slots " & preset(): + # https://github.com/ethereum/eth2.0-spec-tests/tree/v0.10.1/tests/minimal/phase0/sanity/slots/pyspec_tests + # https://github.com/ethereum/eth2.0-spec-tests/tree/v0.10.1/tests/mainnet/phase0/sanity/slots/pyspec_tests runTest("Advance 1 slot", slots_1, 1) runTest("Advance 2 slots", slots_2, 2) runTest("Advance an empty epoch", empty_epoch, SLOTS_PER_EPOCH) From 2591be8796d3f09f2ead643120a0fe0d4096c0aa Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Thu, 30 Jan 2020 13:40:08 +0100 Subject: [PATCH 3/3] re-organize/shuffle proposer_slashing operations test runner for easy consistency-with-alphabetical-GitHub checking --- .../test_fixture_operations_proposer_slashings.nim | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/official/test_fixture_operations_proposer_slashings.nim b/tests/official/test_fixture_operations_proposer_slashings.nim index f5e1b0409..50375bc1f 100644 --- a/tests/official/test_fixture_operations_proposer_slashings.nim +++ b/tests/official/test_fixture_operations_proposer_slashings.nim @@ -66,13 +66,15 @@ template runTest(identifier: untyped) = `testImpl_proposer_slashing _ identifier`() suite "Official - Operations - Proposer slashing " & preset(): - runTest(success) - runTest(invalid_sig_1) - runTest(invalid_sig_2) - runTest(invalid_sig_1_and_2) - runTest(invalid_proposer_index) + # https://github.com/ethereum/eth2.0-spec-tests/tree/v0.10.1/tests/minimal/phase0/operations/proposer_slashing/pyspec_tests + # https://github.com/ethereum/eth2.0-spec-tests/tree/v0.10.1/tests/mainnet/phase0/operations/proposer_slashing/pyspec_tests runTest(epochs_are_different) runTest(headers_are_same) + runTest(invalid_proposer_index) + runTest(invalid_sig_1) + runTest(invalid_sig_1_and_2) + runTest(invalid_sig_2) runTest(proposer_is_not_activated) runTest(proposer_is_slashed) runTest(proposer_is_withdrawn) + runTest(success)