diff --git a/eth2/state_processing/src/lib.rs b/eth2/state_processing/src/lib.rs index 8b386ddbc9..547df01473 100644 --- a/eth2/state_processing/src/lib.rs +++ b/eth2/state_processing/src/lib.rs @@ -3,7 +3,7 @@ mod macros; pub mod common; //pub mod get_genesis_state; -//pub mod per_block_processing; +pub mod per_block_processing; pub mod per_epoch_processing; //pub mod per_slot_processing; diff --git a/eth2/state_processing/src/per_block_processing.rs b/eth2/state_processing/src/per_block_processing.rs index db73c96187..7b3c51295b 100644 --- a/eth2/state_processing/src/per_block_processing.rs +++ b/eth2/state_processing/src/per_block_processing.rs @@ -86,7 +86,7 @@ fn per_block_processing_signature_optional( verify_block_signature(&state, &block, &spec)?; } process_randao(&mut state, &block, &spec)?; - process_eth1_data(&mut state, &block.body.eth1_data)?; + process_eth1_data(&mut state, &block.body.eth1_data, spec)?; process_proposer_slashings(&mut state, &block.body.proposer_slashings, spec)?; process_attester_slashings(&mut state, &block.body.attester_slashings, spec)?; process_attestations(&mut state, &block.body.attestations, spec)?; @@ -99,7 +99,7 @@ fn per_block_processing_signature_optional( /// Processes the block header. /// -/// Spec v0.5.1 +/// Spec v0.6.1 pub fn process_block_header( state: &mut BeaconState, block: &BeaconBlock, @@ -119,12 +119,17 @@ pub fn process_block_header( state.latest_block_header = block.temporary_block_header(spec); + // Verify proposer is not slashed + let proposer_idx = state.get_beacon_proposer_index(block.slot, RelativeEpoch::Current, spec)?; + let proposer = &state.validator_registry[proposer_idx]; + verify!(!proposer.slashed, Invalid::ProposerSlashed(proposer_idx)); + Ok(()) } /// Verifies the signature of a block. /// -/// Spec v0.5.1 +/// Spec v0.6.1 pub fn verify_block_signature( state: &BeaconState, block: &BeaconBlock, @@ -135,7 +140,7 @@ pub fn verify_block_signature( let domain = spec.get_domain( block.slot.epoch(spec.slots_per_epoch), - Domain::BeaconBlock, + Domain::BeaconProposer, &state.fork, ); @@ -152,7 +157,7 @@ pub fn verify_block_signature( /// Verifies the `randao_reveal` against the block's proposer pubkey and updates /// `state.latest_randao_mixes`. /// -/// Spec v0.5.1 +/// Spec v0.6.1 pub fn process_randao( state: &mut BeaconState, block: &BeaconBlock, @@ -176,32 +181,29 @@ pub fn process_randao( ); // Update the current epoch RANDAO mix. - state.update_randao_mix(state.current_epoch(), &block.body.randao_reveal, spec)?; + state.update_randao_mix(state.current_epoch(), &block.body.randao_reveal)?; Ok(()) } /// Update the `state.eth1_data_votes` based upon the `eth1_data` provided. /// -/// Spec v0.5.1 +/// Spec v0.6.1 pub fn process_eth1_data( state: &mut BeaconState, eth1_data: &Eth1Data, + spec: &ChainSpec, ) -> Result<(), Error> { - // Attempt to find a `Eth1DataVote` with matching `Eth1Data`. - let matching_eth1_vote_index = state + state.eth1_data_votes.push(eth1_data.clone()); + + let num_votes = state .eth1_data_votes .iter() - .position(|vote| vote.eth1_data == *eth1_data); + .filter(|vote| *vote == eth1_data) + .count() as u64; - // If a vote exists, increment it's `vote_count`. Otherwise, create a new `Eth1DataVote`. - if let Some(index) = matching_eth1_vote_index { - state.eth1_data_votes[index].vote_count += 1; - } else { - state.eth1_data_votes.push(Eth1DataVote { - eth1_data: eth1_data.clone(), - vote_count: 1, - }); + if num_votes * 2 > spec.slots_per_eth1_voting_period { + state.latest_eth1_data = eth1_data.clone(); } Ok(()) diff --git a/eth2/state_processing/src/per_block_processing/errors.rs b/eth2/state_processing/src/per_block_processing/errors.rs index 6c21d37a57..902d525027 100644 --- a/eth2/state_processing/src/per_block_processing/errors.rs +++ b/eth2/state_processing/src/per_block_processing/errors.rs @@ -71,6 +71,7 @@ pub enum BlockInvalid { state: Hash256, block: Hash256, }, + ProposerSlashed(usize), BadSignature, BadRandaoSignature, MaxAttestationsExceeded, diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index d88e1c6eba..9d9e713e84 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -478,7 +478,7 @@ impl BeaconState { /// /// See `Self::get_randao_mix`. /// - /// Spec v0.5.1 + /// Spec v0.6.1 pub fn update_randao_mix(&mut self, epoch: Epoch, signature: &Signature) -> Result<(), Error> { let i = epoch.as_usize() % T::LatestRandaoMixesLength::to_usize();