switch too-strict/against-spec operations processing to use spec-based process_operations

This commit is contained in:
Dustin Brody 2020-05-22 17:52:07 +02:00 committed by tersec
parent f06df1cea6
commit 95ee0218db
1 changed files with 37 additions and 101 deletions

View File

@ -196,21 +196,6 @@ proc process_proposer_slashing*(
true 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 # 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( func is_slashable_attestation_data(
data_1: AttestationData, data_2: AttestationData): bool = data_1: AttestationData, data_2: AttestationData): bool =
@ -261,63 +246,6 @@ proc process_attester_slashing*(
return false return false
return true 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 # https://github.com/ethereum/eth2.0-specs/blob/v0.9.4/specs/core/0_beacon-chain.md#voluntary-exits
proc process_voluntary_exit*( proc process_voluntary_exit*(
state: var BeaconState, state: var BeaconState,
@ -379,14 +307,42 @@ proc process_voluntary_exit*(
true true
proc processVoluntaryExits(state: var BeaconState, blck: BeaconBlock, flags: UpdateFlags): bool {.nbench.}= # https://github.com/ethereum/eth2.0-specs/blob/v0.11.3/specs/phase0/beacon-chain.md#operations
if len(blck.body.voluntary_exits) > MAX_VOLUNTARY_EXITS: proc process_operations(state: var BeaconState, body: BeaconBlockBody,
notice "[Block processing - Voluntary Exit]: too many exits!" 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 return false
for exit in blck.body.voluntary_exits:
if not process_voluntary_exit(state, exit, flags): proc for_ops(state: var BeaconState, operations: auto, fn: auto, flags: auto,
return false stateCache: var auto) =
return true 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 # https://github.com/ethereum/eth2.0-specs/blob/v0.11.3/specs/phase0/beacon-chain.md#block-processing
proc process_block*( proc process_block*(
@ -421,28 +377,8 @@ proc process_block*(
return false return false
process_eth1_data(state, blck.body) process_eth1_data(state, blck.body)
if not process_operations(state, blck.body, flags, stateCache):
# TODO, everything below is now in process_operations # One could combine this and the default-true, but that's a bit implicit
# 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)
return false return false
true true