update process_deposit() to actually check is_valid_merkle_branch() unless skipValidation specified

This commit is contained in:
Dustin Brody 2020-01-28 22:24:45 +01:00 committed by tersec
parent 659c24a476
commit 45dd12cf3f
4 changed files with 18 additions and 23 deletions

View File

@ -47,27 +47,20 @@ func decrease_balance*(
else: else:
state.balances[index] - delta state.balances[index] - delta
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#deposits # https://github.com/ethereum/eth2.0-specs/blob/v0.9.4/specs/core/0_beacon-chain.md#deposits
func process_deposit*( proc process_deposit*(
state: var BeaconState, deposit: Deposit, flags: UpdateFlags = {}): bool {.nbench.}= state: var BeaconState, deposit: Deposit, flags: UpdateFlags = {}): bool {.nbench.}=
# Process an Eth1 deposit, registering a validator or increasing its balance. # Process an Eth1 deposit, registering a validator or increasing its balance.
# Verify the Merkle branch # Verify the Merkle branch
# TODO enable this check, but don't use doAssert if skipValidation notin flags and not is_valid_merkle_branch(
if not is_valid_merkle_branch( hash_tree_root(deposit.data),
hash_tree_root(deposit.getDepositMessage), deposit.proof,
deposit.proof, DEPOSIT_CONTRACT_TREE_DEPTH + 1,
DEPOSIT_CONTRACT_TREE_DEPTH, state.eth1_deposit_index,
state.eth1_deposit_index,
state.eth1_data.deposit_root, state.eth1_data.deposit_root,
): ):
## TODO: a notice-like mechanism which works in a func return false
## 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
# Deposits must be processed in order # Deposits must be processed in order
state.eth1_deposit_index += 1 state.eth1_deposit_index += 1

View File

@ -1,5 +1,5 @@
# beacon_chain # beacon_chain
# Copyright (c) 2019 Status Research & Development GmbH # Copyright (c) 2019-2020 Status Research & Development GmbH
# Licensed and distributed under either of # Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * 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). # * 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 let
genesisState = 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) genesisBlock = get_initial_beacon_block(genesisState)
echo "Starting simulation..." echo "Starting simulation..."

View File

@ -1,5 +1,5 @@
# beacon_chain # beacon_chain
# Copyright (c) 2018 Status Research & Development GmbH # Copyright (c) 2018-2020 Status Research & Development GmbH
# Licensed and distributed under either of # Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * 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). # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -10,11 +10,12 @@
import import
times, unittest, times, unittest,
./testutil, ./testblockutil, ./testutil, ./testblockutil,
../beacon_chain/spec/[beaconstate, datatypes, digest] ../beacon_chain/spec/[beaconstate, datatypes, digest],
../beacon_chain/extras
suite "Beacon state" & preset(): suite "Beacon state" & preset():
timedTest "Smoke test initialize_beacon_state_from_eth1" & preset(): timedTest "Smoke test initialize_beacon_state_from_eth1" & preset():
let state = initialize_beacon_state_from_eth1( let state = initialize_beacon_state_from_eth1(
Eth2Digest(), 0, Eth2Digest(), 0,
makeInitialDeposits(SLOTS_PER_EPOCH, {}), {}) makeInitialDeposits(SLOTS_PER_EPOCH, {}), {skipValidation})
check: state.validators.len == SLOTS_PER_EPOCH check: state.validators.len == SLOTS_PER_EPOCH

View File

@ -1,5 +1,5 @@
# beacon_chain # beacon_chain
# Copyright (c) 2018 Status Research & Development GmbH # Copyright (c) 2018-2020 Status Research & Development GmbH
# Licensed and distributed under either of # Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * 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). # * 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, unittest,
./testutil, ./testblockutil, ./testutil, ./testblockutil,
../beacon_chain/spec/[beaconstate, datatypes, digest, validator], ../beacon_chain/spec/[beaconstate, datatypes, digest, validator],
../beacon_chain/[state_transition, ssz] ../beacon_chain/[extras, state_transition, ssz]
suite "Block processing" & preset(): suite "Block processing" & preset():
## For now just test that we can compile and execute block processing with ## 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 # TODO bls verification is a bit of a bottleneck here
genesisState = initialize_beacon_state_from_eth1( genesisState = initialize_beacon_state_from_eth1(
Eth2Digest(), 0, Eth2Digest(), 0,
makeInitialDeposits(), {}) makeInitialDeposits(), {skipValidation})
genesisBlock = get_initial_beacon_block(genesisState) genesisBlock = get_initial_beacon_block(genesisState)
genesisRoot = hash_tree_root(genesisBlock.message) genesisRoot = hash_tree_root(genesisBlock.message)