add new beacon state variant for shanghai

This commit is contained in:
realbigsean 2022-02-20 15:27:22 -07:00
parent 203418ffc9
commit acaa340b41
No known key found for this signature in database
GPG Key ID: B372B64D866BF8CC
10 changed files with 105 additions and 36 deletions

View File

@ -3619,6 +3619,30 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.ok_or(BlockProductionError::MissingExecutionPayload)?, .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( let block = SignedBeaconBlock::from_block(

View File

@ -0,0 +1 @@

View File

@ -5,6 +5,7 @@ mod beacon_chain;
mod beacon_fork_choice_store; mod beacon_fork_choice_store;
pub mod beacon_proposer_cache; pub mod beacon_proposer_cache;
mod beacon_snapshot; mod beacon_snapshot;
mod blob_verification;
pub mod block_reward; pub mod block_reward;
mod block_times_cache; mod block_times_cache;
mod block_verification; mod block_verification;

View File

@ -14,7 +14,7 @@ use types::*;
/// ///
/// Utilises lazy-loading from separate storage for its vector fields. /// Utilises lazy-loading from separate storage for its vector fields.
#[superstruct( #[superstruct(
variants(Base, Altair, Merge), variants(Base, Altair, Merge, Shanghai),
variant_attributes(derive(Debug, PartialEq, Clone, Encode, Decode)) variant_attributes(derive(Debug, PartialEq, Clone, Encode, Decode))
)] )]
#[derive(Debug, PartialEq, Clone, Encode)] #[derive(Debug, PartialEq, Clone, Encode)]
@ -66,9 +66,9 @@ where
pub current_epoch_attestations: VariableList<PendingAttestation<T>, T::MaxPendingAttestations>, pub current_epoch_attestations: VariableList<PendingAttestation<T>, T::MaxPendingAttestations>,
// Participation (Altair and later) // Participation (Altair and later)
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Shanghai))]
pub previous_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>, pub previous_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>,
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Shanghai))]
pub current_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>, pub current_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>,
// Finality // Finality
@ -78,17 +78,17 @@ where
pub finalized_checkpoint: Checkpoint, pub finalized_checkpoint: Checkpoint,
// Inactivity // Inactivity
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Shanghai))]
pub inactivity_scores: VariableList<u64, T::ValidatorRegistryLimit>, pub inactivity_scores: VariableList<u64, T::ValidatorRegistryLimit>,
// Light-client sync committees // Light-client sync committees
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Shanghai))]
pub current_sync_committee: Arc<SyncCommittee<T>>, pub current_sync_committee: Arc<SyncCommittee<T>>,
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Shanghai))]
pub next_sync_committee: Arc<SyncCommittee<T>>, pub next_sync_committee: Arc<SyncCommittee<T>>,
// Execution // Execution
#[superstruct(only(Merge))] #[superstruct(only(Merge, Shanghai))]
pub latest_execution_payload_header: ExecutionPayloadHeader<T>, pub latest_execution_payload_header: ExecutionPayloadHeader<T>,
} }
@ -178,6 +178,20 @@ impl<T: EthSpec> PartialBeaconState<T> {
latest_execution_payload_header 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<E: EthSpec> TryInto<BeaconState<E>> for PartialBeaconState<E> {
latest_execution_payload_header 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) Ok(state)
} }

View File

@ -45,9 +45,11 @@ pub fn slash_validator<T: EthSpec>(
validator_effective_balance.safe_div(spec.whistleblower_reward_quotient)?; validator_effective_balance.safe_div(spec.whistleblower_reward_quotient)?;
let proposer_reward = match state { let proposer_reward = match state {
BeaconState::Base(_) => whistleblower_reward.safe_div(spec.proposer_reward_quotient)?, BeaconState::Base(_) => whistleblower_reward.safe_div(spec.proposer_reward_quotient)?,
BeaconState::Altair(_) | BeaconState::Merge(_) => whistleblower_reward BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => {
.safe_mul(PROPOSER_WEIGHT)? whistleblower_reward
.safe_div(WEIGHT_DENOMINATOR)?, .safe_mul(PROPOSER_WEIGHT)?
.safe_div(WEIGHT_DENOMINATOR)?
}
}; };
// Ensure the whistleblower index is in the validator registry. // Ensure the whistleblower index is in the validator registry.

View File

@ -37,7 +37,9 @@ pub fn process_epoch<T: EthSpec>(
match state { match state {
BeaconState::Base(_) => base::process_epoch(state, spec), 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)
}
} }
} }

View File

@ -172,7 +172,7 @@ impl From<BeaconStateHash> for Hash256 {
/// The state of the `BeaconChain` at some slot. /// The state of the `BeaconChain` at some slot.
#[superstruct( #[superstruct(
variants(Base, Altair, Merge), variants(Base, Altair, Merge, Shanghai),
variant_attributes( variant_attributes(
derive( derive(
Derivative, Derivative,
@ -250,9 +250,9 @@ where
pub current_epoch_attestations: VariableList<PendingAttestation<T>, T::MaxPendingAttestations>, pub current_epoch_attestations: VariableList<PendingAttestation<T>, T::MaxPendingAttestations>,
// Participation (Altair and later) // Participation (Altair and later)
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Shanghai))]
pub previous_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>, pub previous_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>,
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Shanghai))]
pub current_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>, pub current_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>,
// Finality // Finality
@ -267,17 +267,17 @@ where
// Inactivity // Inactivity
#[serde(with = "ssz_types::serde_utils::quoted_u64_var_list")] #[serde(with = "ssz_types::serde_utils::quoted_u64_var_list")]
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Shanghai))]
pub inactivity_scores: VariableList<u64, T::ValidatorRegistryLimit>, pub inactivity_scores: VariableList<u64, T::ValidatorRegistryLimit>,
// Light-client sync committees // Light-client sync committees
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Shanghai))]
pub current_sync_committee: Arc<SyncCommittee<T>>, pub current_sync_committee: Arc<SyncCommittee<T>>,
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Shanghai))]
pub next_sync_committee: Arc<SyncCommittee<T>>, pub next_sync_committee: Arc<SyncCommittee<T>>,
// Execution // Execution
#[superstruct(only(Merge))] #[superstruct(only(Merge, Shanghai))]
pub latest_execution_payload_header: ExecutionPayloadHeader<T>, pub latest_execution_payload_header: ExecutionPayloadHeader<T>,
// Caching (not in the spec) // Caching (not in the spec)
@ -389,6 +389,7 @@ impl<T: EthSpec> BeaconState<T> {
BeaconState::Base { .. } => ForkName::Base, BeaconState::Base { .. } => ForkName::Base,
BeaconState::Altair { .. } => ForkName::Altair, BeaconState::Altair { .. } => ForkName::Altair,
BeaconState::Merge { .. } => ForkName::Merge, BeaconState::Merge { .. } => ForkName::Merge,
BeaconState::Shanghai { .. } => ForkName::Shanghai,
}; };
if fork_at_slot == object_fork { if fork_at_slot == object_fork {
@ -1102,6 +1103,7 @@ impl<T: EthSpec> BeaconState<T> {
BeaconState::Base(state) => (&mut state.validators, &mut state.balances), BeaconState::Base(state) => (&mut state.validators, &mut state.balances),
BeaconState::Altair(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::Merge(state) => (&mut state.validators, &mut state.balances),
BeaconState::Shanghai(state) => (&mut state.validators, &mut state.balances),
} }
} }
@ -1298,12 +1300,14 @@ impl<T: EthSpec> BeaconState<T> {
BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant), BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant),
BeaconState::Altair(state) => Ok(&mut state.current_epoch_participation), BeaconState::Altair(state) => Ok(&mut state.current_epoch_participation),
BeaconState::Merge(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() { } else if epoch == self.previous_epoch() {
match self { match self {
BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant), BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant),
BeaconState::Altair(state) => Ok(&mut state.previous_epoch_participation), BeaconState::Altair(state) => Ok(&mut state.previous_epoch_participation),
BeaconState::Merge(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 { } else {
Err(BeaconStateError::EpochOutOfBounds) Err(BeaconStateError::EpochOutOfBounds)
@ -1608,6 +1612,7 @@ impl<T: EthSpec> BeaconState<T> {
BeaconState::Base(inner) => BeaconState::Base(inner.clone()), BeaconState::Base(inner) => BeaconState::Base(inner.clone()),
BeaconState::Altair(inner) => BeaconState::Altair(inner.clone()), BeaconState::Altair(inner) => BeaconState::Altair(inner.clone()),
BeaconState::Merge(inner) => BeaconState::Merge(inner.clone()), BeaconState::Merge(inner) => BeaconState::Merge(inner.clone()),
BeaconState::Shanghai(inner) => BeaconState::Shanghai(inner.clone()),
}; };
if config.committee_caches { if config.committee_caches {
*res.committee_caches_mut() = self.committee_caches().clone(); *res.committee_caches_mut() = self.committee_caches().clone();

View File

@ -274,6 +274,7 @@ impl ChainSpec {
BeaconState::Base(_) => self.inactivity_penalty_quotient, BeaconState::Base(_) => self.inactivity_penalty_quotient,
BeaconState::Altair(_) => self.inactivity_penalty_quotient_altair, BeaconState::Altair(_) => self.inactivity_penalty_quotient_altair,
BeaconState::Merge(_) => self.inactivity_penalty_quotient_bellatrix, 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::Base(_) => self.proportional_slashing_multiplier,
BeaconState::Altair(_) => self.proportional_slashing_multiplier_altair, BeaconState::Altair(_) => self.proportional_slashing_multiplier_altair,
BeaconState::Merge(_) => self.proportional_slashing_multiplier_bellatrix, 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::Base(_) => self.min_slashing_penalty_quotient,
BeaconState::Altair(_) => self.min_slashing_penalty_quotient_altair, BeaconState::Altair(_) => self.min_slashing_penalty_quotient_altair,
BeaconState::Merge(_) => self.min_slashing_penalty_quotient_bellatrix, BeaconState::Merge(_) => self.min_slashing_penalty_quotient_bellatrix,
BeaconState::Shanghai(_) => self.min_slashing_penalty_quotient_bellatrix,
} }
} }

View File

@ -97,7 +97,7 @@ impl<E: EthSpec> EpochTransition<E> for JustificationAndFinalization {
justification_and_finalization_state.apply_changes_to_state(state); justification_and_finalization_state.apply_changes_to_state(state);
Ok(()) Ok(())
} }
BeaconState::Altair(_) | BeaconState::Merge(_) => { BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => {
let justification_and_finalization_state = let justification_and_finalization_state =
altair::process_justification_and_finalization( altair::process_justification_and_finalization(
state, state,
@ -118,7 +118,7 @@ impl<E: EthSpec> EpochTransition<E> for RewardsAndPenalties {
validator_statuses.process_attestations(state)?; validator_statuses.process_attestations(state)?;
base::process_rewards_and_penalties(state, &mut validator_statuses, spec) 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( altair::process_rewards_and_penalties(
state, state,
&altair::ParticipationCache::new(state, spec).unwrap(), &altair::ParticipationCache::new(state, spec).unwrap(),
@ -147,7 +147,7 @@ impl<E: EthSpec> EpochTransition<E> for Slashings {
spec, spec,
)?; )?;
} }
BeaconState::Altair(_) | BeaconState::Merge(_) => { BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => {
process_slashings( process_slashings(
state, state,
altair::ParticipationCache::new(state, spec) altair::ParticipationCache::new(state, spec)
@ -205,7 +205,7 @@ impl<E: EthSpec> EpochTransition<E> for SyncCommitteeUpdates {
fn run(state: &mut BeaconState<E>, spec: &ChainSpec) -> Result<(), EpochProcessingError> { fn run(state: &mut BeaconState<E>, spec: &ChainSpec) -> Result<(), EpochProcessingError> {
match state { match state {
BeaconState::Base(_) => Ok(()), BeaconState::Base(_) => Ok(()),
BeaconState::Altair(_) | BeaconState::Merge(_) => { BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => {
altair::process_sync_committee_updates(state, spec) altair::process_sync_committee_updates(state, spec)
} }
} }
@ -216,11 +216,13 @@ impl<E: EthSpec> EpochTransition<E> for InactivityUpdates {
fn run(state: &mut BeaconState<E>, spec: &ChainSpec) -> Result<(), EpochProcessingError> { fn run(state: &mut BeaconState<E>, spec: &ChainSpec) -> Result<(), EpochProcessingError> {
match state { match state {
BeaconState::Base(_) => Ok(()), BeaconState::Base(_) => Ok(()),
BeaconState::Altair(_) | BeaconState::Merge(_) => altair::process_inactivity_updates( BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => {
state, altair::process_inactivity_updates(
&altair::ParticipationCache::new(state, spec).unwrap(), state,
spec, &altair::ParticipationCache::new(state, spec).unwrap(),
), spec,
)
}
} }
} }
} }
@ -229,7 +231,7 @@ impl<E: EthSpec> EpochTransition<E> for ParticipationFlagUpdates {
fn run(state: &mut BeaconState<E>, _: &ChainSpec) -> Result<(), EpochProcessingError> { fn run(state: &mut BeaconState<E>, _: &ChainSpec) -> Result<(), EpochProcessingError> {
match state { match state {
BeaconState::Base(_) => Ok(()), BeaconState::Base(_) => Ok(()),
BeaconState::Altair(_) | BeaconState::Merge(_) => { BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => {
altair::process_participation_flag_updates(state) altair::process_participation_flag_updates(state)
} }
} }

View File

@ -81,14 +81,16 @@ impl<E: EthSpec> Operation<E> for Attestation<E> {
BeaconState::Base(_) => { BeaconState::Base(_) => {
base::process_attestations(state, &[self.clone()], VerifySignatures::True, spec) base::process_attestations(state, &[self.clone()], VerifySignatures::True, spec)
} }
BeaconState::Altair(_) | BeaconState::Merge(_) => altair::process_attestation( BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Shanghai(_) => {
state, altair::process_attestation(
self, state,
0, self,
proposer_index, 0,
VerifySignatures::True, proposer_index,
spec, VerifySignatures::True,
), spec,
)
}
} }
} }
} }