From acaa340b418f84bb1b4a295281dfc97a325a135b Mon Sep 17 00:00:00 2001 From: realbigsean Date: Sun, 20 Feb 2022 15:27:22 -0700 Subject: [PATCH] add new beacon state variant for shanghai --- beacon_node/beacon_chain/src/beacon_chain.rs | 24 +++++++++++ .../beacon_chain/src/blob_verification.rs | 1 + beacon_node/beacon_chain/src/lib.rs | 1 + beacon_node/store/src/partial_beacon_state.rs | 41 +++++++++++++++---- .../src/common/slash_validator.rs | 8 ++-- .../src/per_epoch_processing.rs | 4 +- consensus/types/src/beacon_state.rs | 19 +++++---- consensus/types/src/chain_spec.rs | 3 ++ .../ef_tests/src/cases/epoch_processing.rs | 22 +++++----- testing/ef_tests/src/cases/operations.rs | 18 ++++---- 10 files changed, 105 insertions(+), 36 deletions(-) create mode 100644 beacon_node/beacon_chain/src/blob_verification.rs diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 3eecc9a0d..5373cee20 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -3619,6 +3619,30 @@ impl BeaconChain { .ok_or(BlockProductionError::MissingExecutionPayload)?, }, }), + BeaconState::Shanghai(_) => { + let sync_aggregate = get_sync_aggregate()?; + let execution_payload = get_execution_payload(self, &state, proposer_index)?; + //FIXME(sean) get blobs + BeaconBlock::Shanghai(BeaconBlockShanghai { + slot, + proposer_index, + parent_root, + state_root: Hash256::zero(), + body: BeaconBlockBodyShanghai { + randao_reveal, + eth1_data, + graffiti, + proposer_slashings: proposer_slashings.into(), + attester_slashings: attester_slashings.into(), + attestations, + deposits, + voluntary_exits: voluntary_exits.into(), + sync_aggregate, + execution_payload, + blob_kzgs: VariableList::empty(), + }, + }) + } }; let block = SignedBeaconBlock::from_block( diff --git a/beacon_node/beacon_chain/src/blob_verification.rs b/beacon_node/beacon_chain/src/blob_verification.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/beacon_node/beacon_chain/src/blob_verification.rs @@ -0,0 +1 @@ + diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index fbcd8f7fb..84e15ead7 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -5,6 +5,7 @@ mod beacon_chain; mod beacon_fork_choice_store; pub mod beacon_proposer_cache; mod beacon_snapshot; +mod blob_verification; pub mod block_reward; mod block_times_cache; mod block_verification; diff --git a/beacon_node/store/src/partial_beacon_state.rs b/beacon_node/store/src/partial_beacon_state.rs index 010796afd..1e52e9856 100644 --- a/beacon_node/store/src/partial_beacon_state.rs +++ b/beacon_node/store/src/partial_beacon_state.rs @@ -14,7 +14,7 @@ use types::*; /// /// Utilises lazy-loading from separate storage for its vector fields. #[superstruct( - variants(Base, Altair, Merge), + variants(Base, Altair, Merge, Shanghai), variant_attributes(derive(Debug, PartialEq, Clone, Encode, Decode)) )] #[derive(Debug, PartialEq, Clone, Encode)] @@ -66,9 +66,9 @@ where pub current_epoch_attestations: VariableList, T::MaxPendingAttestations>, // Participation (Altair and later) - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Shanghai))] pub previous_epoch_participation: VariableList, - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Shanghai))] pub current_epoch_participation: VariableList, // Finality @@ -78,17 +78,17 @@ where pub finalized_checkpoint: Checkpoint, // Inactivity - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Shanghai))] pub inactivity_scores: VariableList, // Light-client sync committees - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Shanghai))] pub current_sync_committee: Arc>, - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Shanghai))] pub next_sync_committee: Arc>, // Execution - #[superstruct(only(Merge))] + #[superstruct(only(Merge, Shanghai))] pub latest_execution_payload_header: ExecutionPayloadHeader, } @@ -178,6 +178,20 @@ impl PartialBeaconState { latest_execution_payload_header ] ), + BeaconState::Shanghai(s) => impl_from_state_forgetful!( + s, + outer, + Shanghai, + PartialBeaconStateShanghai, + [ + previous_epoch_participation, + current_epoch_participation, + current_sync_committee, + next_sync_committee, + inactivity_scores, + latest_execution_payload_header + ] + ), } } @@ -365,6 +379,19 @@ impl TryInto> for PartialBeaconState { latest_execution_payload_header ] ), + PartialBeaconState::Shanghai(inner) => impl_try_into_beacon_state!( + inner, + Shanghai, + BeaconStateShanghai, + [ + previous_epoch_participation, + current_epoch_participation, + current_sync_committee, + next_sync_committee, + inactivity_scores, + latest_execution_payload_header + ] + ), }; Ok(state) } diff --git a/consensus/state_processing/src/common/slash_validator.rs b/consensus/state_processing/src/common/slash_validator.rs index e9d94a106..23bd35bf6 100644 --- a/consensus/state_processing/src/common/slash_validator.rs +++ b/consensus/state_processing/src/common/slash_validator.rs @@ -45,9 +45,11 @@ pub fn slash_validator( validator_effective_balance.safe_div(spec.whistleblower_reward_quotient)?; let proposer_reward = match state { BeaconState::Base(_) => whistleblower_reward.safe_div(spec.proposer_reward_quotient)?, - BeaconState::Altair(_) | BeaconState::Merge(_) => whistleblower_reward - .safe_mul(PROPOSER_WEIGHT)? - .safe_div(WEIGHT_DENOMINATOR)?, + BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => { + whistleblower_reward + .safe_mul(PROPOSER_WEIGHT)? + .safe_div(WEIGHT_DENOMINATOR)? + } }; // Ensure the whistleblower index is in the validator registry. diff --git a/consensus/state_processing/src/per_epoch_processing.rs b/consensus/state_processing/src/per_epoch_processing.rs index cb90c67b5..8074a32ed 100644 --- a/consensus/state_processing/src/per_epoch_processing.rs +++ b/consensus/state_processing/src/per_epoch_processing.rs @@ -37,7 +37,9 @@ pub fn process_epoch( match state { BeaconState::Base(_) => base::process_epoch(state, spec), - BeaconState::Altair(_) | BeaconState::Merge(_) => altair::process_epoch(state, spec), + BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => { + altair::process_epoch(state, spec) + } } } diff --git a/consensus/types/src/beacon_state.rs b/consensus/types/src/beacon_state.rs index a5d00cdf2..eb4be019f 100644 --- a/consensus/types/src/beacon_state.rs +++ b/consensus/types/src/beacon_state.rs @@ -172,7 +172,7 @@ impl From for Hash256 { /// The state of the `BeaconChain` at some slot. #[superstruct( - variants(Base, Altair, Merge), + variants(Base, Altair, Merge, Shanghai), variant_attributes( derive( Derivative, @@ -250,9 +250,9 @@ where pub current_epoch_attestations: VariableList, T::MaxPendingAttestations>, // Participation (Altair and later) - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Shanghai))] pub previous_epoch_participation: VariableList, - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Shanghai))] pub current_epoch_participation: VariableList, // Finality @@ -267,17 +267,17 @@ where // Inactivity #[serde(with = "ssz_types::serde_utils::quoted_u64_var_list")] - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Shanghai))] pub inactivity_scores: VariableList, // Light-client sync committees - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Shanghai))] pub current_sync_committee: Arc>, - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Shanghai))] pub next_sync_committee: Arc>, // Execution - #[superstruct(only(Merge))] + #[superstruct(only(Merge, Shanghai))] pub latest_execution_payload_header: ExecutionPayloadHeader, // Caching (not in the spec) @@ -389,6 +389,7 @@ impl BeaconState { BeaconState::Base { .. } => ForkName::Base, BeaconState::Altair { .. } => ForkName::Altair, BeaconState::Merge { .. } => ForkName::Merge, + BeaconState::Shanghai { .. } => ForkName::Shanghai, }; if fork_at_slot == object_fork { @@ -1102,6 +1103,7 @@ impl BeaconState { BeaconState::Base(state) => (&mut state.validators, &mut state.balances), BeaconState::Altair(state) => (&mut state.validators, &mut state.balances), BeaconState::Merge(state) => (&mut state.validators, &mut state.balances), + BeaconState::Shanghai(state) => (&mut state.validators, &mut state.balances), } } @@ -1298,12 +1300,14 @@ impl BeaconState { BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant), BeaconState::Altair(state) => Ok(&mut state.current_epoch_participation), BeaconState::Merge(state) => Ok(&mut state.current_epoch_participation), + BeaconState::Shanghai(state) => Ok(&mut state.current_epoch_participation), } } else if epoch == self.previous_epoch() { match self { BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant), BeaconState::Altair(state) => Ok(&mut state.previous_epoch_participation), BeaconState::Merge(state) => Ok(&mut state.previous_epoch_participation), + BeaconState::Shanghai(state) => Ok(&mut state.previous_epoch_participation), } } else { Err(BeaconStateError::EpochOutOfBounds) @@ -1608,6 +1612,7 @@ impl BeaconState { BeaconState::Base(inner) => BeaconState::Base(inner.clone()), BeaconState::Altair(inner) => BeaconState::Altair(inner.clone()), BeaconState::Merge(inner) => BeaconState::Merge(inner.clone()), + BeaconState::Shanghai(inner) => BeaconState::Shanghai(inner.clone()), }; if config.committee_caches { *res.committee_caches_mut() = self.committee_caches().clone(); diff --git a/consensus/types/src/chain_spec.rs b/consensus/types/src/chain_spec.rs index 8887061e4..4f62022fb 100644 --- a/consensus/types/src/chain_spec.rs +++ b/consensus/types/src/chain_spec.rs @@ -274,6 +274,7 @@ impl ChainSpec { BeaconState::Base(_) => self.inactivity_penalty_quotient, BeaconState::Altair(_) => self.inactivity_penalty_quotient_altair, BeaconState::Merge(_) => self.inactivity_penalty_quotient_bellatrix, + BeaconState::Shanghai(_) => self.inactivity_penalty_quotient_bellatrix, } } @@ -286,6 +287,7 @@ impl ChainSpec { BeaconState::Base(_) => self.proportional_slashing_multiplier, BeaconState::Altair(_) => self.proportional_slashing_multiplier_altair, BeaconState::Merge(_) => self.proportional_slashing_multiplier_bellatrix, + BeaconState::Shanghai(_) => self.proportional_slashing_multiplier_bellatrix, } } @@ -298,6 +300,7 @@ impl ChainSpec { BeaconState::Base(_) => self.min_slashing_penalty_quotient, BeaconState::Altair(_) => self.min_slashing_penalty_quotient_altair, BeaconState::Merge(_) => self.min_slashing_penalty_quotient_bellatrix, + BeaconState::Shanghai(_) => self.min_slashing_penalty_quotient_bellatrix, } } diff --git a/testing/ef_tests/src/cases/epoch_processing.rs b/testing/ef_tests/src/cases/epoch_processing.rs index 9163b551a..04470c73c 100644 --- a/testing/ef_tests/src/cases/epoch_processing.rs +++ b/testing/ef_tests/src/cases/epoch_processing.rs @@ -97,7 +97,7 @@ impl EpochTransition for JustificationAndFinalization { justification_and_finalization_state.apply_changes_to_state(state); Ok(()) } - BeaconState::Altair(_) | BeaconState::Merge(_) => { + BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => { let justification_and_finalization_state = altair::process_justification_and_finalization( state, @@ -118,7 +118,7 @@ impl EpochTransition for RewardsAndPenalties { validator_statuses.process_attestations(state)?; base::process_rewards_and_penalties(state, &mut validator_statuses, spec) } - BeaconState::Altair(_) | BeaconState::Merge(_) => { + BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => { altair::process_rewards_and_penalties( state, &altair::ParticipationCache::new(state, spec).unwrap(), @@ -147,7 +147,7 @@ impl EpochTransition for Slashings { spec, )?; } - BeaconState::Altair(_) | BeaconState::Merge(_) => { + BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => { process_slashings( state, altair::ParticipationCache::new(state, spec) @@ -205,7 +205,7 @@ impl EpochTransition for SyncCommitteeUpdates { fn run(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), EpochProcessingError> { match state { BeaconState::Base(_) => Ok(()), - BeaconState::Altair(_) | BeaconState::Merge(_) => { + BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => { altair::process_sync_committee_updates(state, spec) } } @@ -216,11 +216,13 @@ impl EpochTransition for InactivityUpdates { fn run(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), EpochProcessingError> { match state { BeaconState::Base(_) => Ok(()), - BeaconState::Altair(_) | BeaconState::Merge(_) => altair::process_inactivity_updates( - state, - &altair::ParticipationCache::new(state, spec).unwrap(), - spec, - ), + BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => { + altair::process_inactivity_updates( + state, + &altair::ParticipationCache::new(state, spec).unwrap(), + spec, + ) + } } } } @@ -229,7 +231,7 @@ impl EpochTransition for ParticipationFlagUpdates { fn run(state: &mut BeaconState, _: &ChainSpec) -> Result<(), EpochProcessingError> { match state { BeaconState::Base(_) => Ok(()), - BeaconState::Altair(_) | BeaconState::Merge(_) => { + BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => { altair::process_participation_flag_updates(state) } } diff --git a/testing/ef_tests/src/cases/operations.rs b/testing/ef_tests/src/cases/operations.rs index 798dae083..bfa63bfe6 100644 --- a/testing/ef_tests/src/cases/operations.rs +++ b/testing/ef_tests/src/cases/operations.rs @@ -81,14 +81,16 @@ impl Operation for Attestation { BeaconState::Base(_) => { base::process_attestations(state, &[self.clone()], VerifySignatures::True, spec) } - BeaconState::Altair(_) | BeaconState::Merge(_) => altair::process_attestation( - state, - self, - 0, - proposer_index, - VerifySignatures::True, - spec, - ), + BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => { + altair::process_attestation( + state, + self, + 0, + proposer_index, + VerifySignatures::True, + spec, + ) + } } } }