From c0fb998fdc68d3197cd882621ee22a71926ab233 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Tue, 28 Jan 2020 22:24:45 +0100 Subject: [PATCH] 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 45a46cabbf..e5e14bbec1 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 b980cab080..361caf8b74 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 a9de1ecb59..26f81a8c10 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 e067e34a7d..566ac7e066 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)