From 95ee0218db2641a39d607179de4caca58b6e784b Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Fri, 22 May 2020 17:52:07 +0200 Subject: [PATCH] switch too-strict/against-spec operations processing to use spec-based process_operations --- beacon_chain/spec/state_transition_block.nim | 138 +++++-------------- 1 file changed, 37 insertions(+), 101 deletions(-) diff --git a/beacon_chain/spec/state_transition_block.nim b/beacon_chain/spec/state_transition_block.nim index f0004c966..be21ebb69 100644 --- a/beacon_chain/spec/state_transition_block.nim +++ b/beacon_chain/spec/state_transition_block.nim @@ -196,21 +196,6 @@ proc process_proposer_slashing*( true -proc processProposerSlashings( - state: var BeaconState, blck: BeaconBlock, flags: UpdateFlags, - stateCache: var StateCache): bool {.nbench.}= - if len(blck.body.proposer_slashings) > MAX_PROPOSER_SLASHINGS: - notice "PropSlash: too many!", - proposer_slashings = len(blck.body.proposer_slashings) - return false - - for proposer_slashing in blck.body.proposer_slashings: - if not process_proposer_slashing( - state, proposer_slashing, flags, stateCache): - return false - - true - # https://github.com/ethereum/eth2.0-specs/blob/v0.11.3/specs/phase0/beacon-chain.md#is_slashable_attestation_data func is_slashable_attestation_data( data_1: AttestationData, data_2: AttestationData): bool = @@ -261,63 +246,6 @@ proc process_attester_slashing*( return false return true -# https://github.com/ethereum/eth2.0-specs/blob/v0.10.1/specs/phase0/beacon-chain.md#attester-slashings -proc processAttesterSlashings(state: var BeaconState, blck: BeaconBlock, - flags: UpdateFlags, stateCache: var StateCache): bool {.nbench.}= - # Process ``AttesterSlashing`` operation. - if len(blck.body.attester_slashings) > MAX_ATTESTER_SLASHINGS: - notice "Attester slashing: too many!" - return false - - for attester_slashing in blck.body.attester_slashings: - if not process_attester_slashing(state, attester_slashing, {}, stateCache): - return false - return true - -# https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#attestations -proc processAttestations( - state: var BeaconState, blck: BeaconBlock, flags: UpdateFlags, - stateCache: var StateCache): bool {.nbench.}= - ## Each block includes a number of attestations that the proposer chose. Each - ## attestation represents an update to a specific shard and is signed by a - ## committee of validators. - ## Here we make sanity checks for each attestation and it to the state - most - ## updates will happen at the epoch boundary where state updates happen in - ## bulk. - if blck.body.attestations.len > MAX_ATTESTATIONS: - notice "Attestation: too many!", attestations = blck.body.attestations.len - return false - - trace "in processAttestations, not processed attestations", - attestations_len = blck.body.attestations.len() - - if not blck.body.attestations.allIt(process_attestation(state, it, flags, stateCache)): - return false - - true - -# https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#deposits -proc processDeposits(state: var BeaconState, blck: BeaconBlock, - flags: UpdateFlags): bool {.nbench.} = - let - num_deposits = len(blck.body.deposits).int64 - req_deposits = min(MAX_DEPOSITS, - state.eth1_data.deposit_count.int64 - state.eth1_deposit_index.int64) - if not (num_deposits == req_deposits): - notice "processDeposits: incorrect number of deposits", - num_deposits = num_deposits, - req_deposits = req_deposits, - deposit_count = state.eth1_data.deposit_count, - deposit_index = state.eth1_deposit_index - return false - - for deposit in blck.body.deposits: - if not process_deposit(state, deposit, flags): - notice "processDeposits: deposit invalid" - return false - - true - # https://github.com/ethereum/eth2.0-specs/blob/v0.9.4/specs/core/0_beacon-chain.md#voluntary-exits proc process_voluntary_exit*( state: var BeaconState, @@ -379,14 +307,42 @@ proc process_voluntary_exit*( true -proc processVoluntaryExits(state: var BeaconState, blck: BeaconBlock, flags: UpdateFlags): bool {.nbench.}= - if len(blck.body.voluntary_exits) > MAX_VOLUNTARY_EXITS: - notice "[Block processing - Voluntary Exit]: too many exits!" +# https://github.com/ethereum/eth2.0-specs/blob/v0.11.3/specs/phase0/beacon-chain.md#operations +proc process_operations(state: var BeaconState, body: BeaconBlockBody, + flags: UpdateFlags, stateCache: var StateCache): bool {.nbench.} = + # Verify that outstanding deposits are processed up to the maximum number of + # deposits + let + num_deposits = len(body.deposits).int64 + req_deposits = min(MAX_DEPOSITS, + state.eth1_data.deposit_count.int64 - state.eth1_deposit_index.int64) + if not (num_deposits == req_deposits): + notice "processOperations: incorrect number of deposits", + num_deposits = num_deposits, + req_deposits = req_deposits, + deposit_count = state.eth1_data.deposit_count, + deposit_index = state.eth1_deposit_index return false - for exit in blck.body.voluntary_exits: - if not process_voluntary_exit(state, exit, flags): - return false - return true + + proc for_ops(state: var BeaconState, operations: auto, fn: auto, flags: auto, + stateCache: var auto) = + for operation in operations: + discard fn(state, operation, flags, stateCache) + + proc for_ops( + state: var BeaconState, operations: auto, fn: auto, flags: auto) = + for operation in operations: + discard fn(state, operation, flags) + + for_ops(state, body.proposer_slashings, process_proposer_slashing, flags, + stateCache) + for_ops(state, body.attester_slashings, process_attester_slashing, flags, + stateCache) + for_ops(state, body.attestations, process_attestation, flags, stateCache) + for_ops(state, body.deposits, process_deposit, flags) + for_ops(state, body.voluntary_exits, process_voluntary_exit, flags) + + true # https://github.com/ethereum/eth2.0-specs/blob/v0.11.3/specs/phase0/beacon-chain.md#block-processing proc process_block*( @@ -421,28 +377,8 @@ proc process_block*( return false process_eth1_data(state, blck.body) - - # TODO, everything below is now in process_operations - # and implementation is per element instead of the whole seq - # https://github.com/ethereum/eth2.0-specs/blob/v0.11.3/specs/phase0/beacon-chain.md#operations - if not processProposerSlashings(state, blck, flags, stateCache): - debug "[Block processing] Proposer slashing failure", slot = shortLog(state.slot) - return false - - if not processAttesterSlashings(state, blck, flags, stateCache): - debug "[Block processing] Attester slashing failure", slot = shortLog(state.slot) - return false - - if not processAttestations(state, blck, flags, stateCache): - debug "[Block processing] Attestation processing failure", slot = shortLog(state.slot) - return false - - if not processDeposits(state, blck, flags): - debug "[Block processing] Deposit processing failure", slot = shortLog(state.slot) - return false - - if not processVoluntaryExits(state, blck, flags): - debug "[Block processing - Voluntary Exit] Exit processing failure", slot = shortLog(state.slot) + if not process_operations(state, blck.body, flags, stateCache): + # One could combine this and the default-true, but that's a bit implicit return false true