block processing: v0.6.1 headers, randao, eth1 data

This commit is contained in:
Michael Sproul 2019-05-20 12:42:02 +10:00
parent aa01808a00
commit 55537078a1
No known key found for this signature in database
GPG Key ID: 77B1309D2E54E914
4 changed files with 23 additions and 20 deletions

View File

@ -3,7 +3,7 @@ mod macros;
pub mod common; pub mod common;
//pub mod get_genesis_state; //pub mod get_genesis_state;
//pub mod per_block_processing; pub mod per_block_processing;
pub mod per_epoch_processing; pub mod per_epoch_processing;
//pub mod per_slot_processing; //pub mod per_slot_processing;

View File

@ -86,7 +86,7 @@ fn per_block_processing_signature_optional<T: EthSpec>(
verify_block_signature(&state, &block, &spec)?; verify_block_signature(&state, &block, &spec)?;
} }
process_randao(&mut 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_proposer_slashings(&mut state, &block.body.proposer_slashings, spec)?;
process_attester_slashings(&mut state, &block.body.attester_slashings, spec)?; process_attester_slashings(&mut state, &block.body.attester_slashings, spec)?;
process_attestations(&mut state, &block.body.attestations, spec)?; process_attestations(&mut state, &block.body.attestations, spec)?;
@ -99,7 +99,7 @@ fn per_block_processing_signature_optional<T: EthSpec>(
/// Processes the block header. /// Processes the block header.
/// ///
/// Spec v0.5.1 /// Spec v0.6.1
pub fn process_block_header<T: EthSpec>( pub fn process_block_header<T: EthSpec>(
state: &mut BeaconState<T>, state: &mut BeaconState<T>,
block: &BeaconBlock, block: &BeaconBlock,
@ -119,12 +119,17 @@ pub fn process_block_header<T: EthSpec>(
state.latest_block_header = block.temporary_block_header(spec); 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(()) Ok(())
} }
/// Verifies the signature of a block. /// Verifies the signature of a block.
/// ///
/// Spec v0.5.1 /// Spec v0.6.1
pub fn verify_block_signature<T: EthSpec>( pub fn verify_block_signature<T: EthSpec>(
state: &BeaconState<T>, state: &BeaconState<T>,
block: &BeaconBlock, block: &BeaconBlock,
@ -135,7 +140,7 @@ pub fn verify_block_signature<T: EthSpec>(
let domain = spec.get_domain( let domain = spec.get_domain(
block.slot.epoch(spec.slots_per_epoch), block.slot.epoch(spec.slots_per_epoch),
Domain::BeaconBlock, Domain::BeaconProposer,
&state.fork, &state.fork,
); );
@ -152,7 +157,7 @@ pub fn verify_block_signature<T: EthSpec>(
/// Verifies the `randao_reveal` against the block's proposer pubkey and updates /// Verifies the `randao_reveal` against the block's proposer pubkey and updates
/// `state.latest_randao_mixes`. /// `state.latest_randao_mixes`.
/// ///
/// Spec v0.5.1 /// Spec v0.6.1
pub fn process_randao<T: EthSpec>( pub fn process_randao<T: EthSpec>(
state: &mut BeaconState<T>, state: &mut BeaconState<T>,
block: &BeaconBlock, block: &BeaconBlock,
@ -176,32 +181,29 @@ pub fn process_randao<T: EthSpec>(
); );
// Update the current epoch RANDAO mix. // 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(()) Ok(())
} }
/// Update the `state.eth1_data_votes` based upon the `eth1_data` provided. /// 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<T: EthSpec>( pub fn process_eth1_data<T: EthSpec>(
state: &mut BeaconState<T>, state: &mut BeaconState<T>,
eth1_data: &Eth1Data, eth1_data: &Eth1Data,
spec: &ChainSpec,
) -> Result<(), Error> { ) -> Result<(), Error> {
// Attempt to find a `Eth1DataVote` with matching `Eth1Data`. state.eth1_data_votes.push(eth1_data.clone());
let matching_eth1_vote_index = state
let num_votes = state
.eth1_data_votes .eth1_data_votes
.iter() .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 num_votes * 2 > spec.slots_per_eth1_voting_period {
if let Some(index) = matching_eth1_vote_index { state.latest_eth1_data = eth1_data.clone();
state.eth1_data_votes[index].vote_count += 1;
} else {
state.eth1_data_votes.push(Eth1DataVote {
eth1_data: eth1_data.clone(),
vote_count: 1,
});
} }
Ok(()) Ok(())

View File

@ -71,6 +71,7 @@ pub enum BlockInvalid {
state: Hash256, state: Hash256,
block: Hash256, block: Hash256,
}, },
ProposerSlashed(usize),
BadSignature, BadSignature,
BadRandaoSignature, BadRandaoSignature,
MaxAttestationsExceeded, MaxAttestationsExceeded,

View File

@ -478,7 +478,7 @@ impl<T: EthSpec> BeaconState<T> {
/// ///
/// See `Self::get_randao_mix`. /// 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> { pub fn update_randao_mix(&mut self, epoch: Epoch, signature: &Signature) -> Result<(), Error> {
let i = epoch.as_usize() % T::LatestRandaoMixesLength::to_usize(); let i = epoch.as_usize() % T::LatestRandaoMixesLength::to_usize();