Merge in staging, update validator store/cache

Merge remote-tracking branch 'origin/staging' into tree-states
This commit is contained in:
Michael Sproul
2022-11-30 17:27:55 +11:00
15 changed files with 747 additions and 439 deletions

View File

@@ -63,14 +63,39 @@ impl<T: EthSpec> ConsensusContext<T> {
self
}
// FIXME(sproul): extra safety checks?
/// Strict method for fetching the proposer index.
///
/// Gets the proposer index for `self.slot` while ensuring that it matches `state.slot()`. This
/// method should be used in block processing and almost everywhere the proposer index is
/// required. If the slot check is too restrictive, see `get_proposer_index_from_epoch_state`.
pub fn get_proposer_index(
&mut self,
state: &BeaconState<T>,
spec: &ChainSpec,
) -> Result<u64, ContextError> {
self.check_epoch(state.current_epoch())?;
self.check_slot(state.slot())?;
self.get_proposer_index_no_checks(state, spec)
}
/// More liberal method for fetching the proposer index.
///
/// Fetches the proposer index for `self.slot` but does not require the state to be from an
/// exactly matching slot (merely a matching epoch). This is useful in batch verification where
/// we want to extract the proposer index from a single state for every slot in the epoch.
pub fn get_proposer_index_from_epoch_state(
&mut self,
state: &BeaconState<T>,
spec: &ChainSpec,
) -> Result<u64, ContextError> {
self.check_epoch(state.current_epoch())?;
self.get_proposer_index_no_checks(state, spec)
}
fn get_proposer_index_no_checks(
&mut self,
state: &BeaconState<T>,
spec: &ChainSpec,
) -> Result<u64, ContextError> {
if let Some(proposer_index) = self.proposer_index {
return Ok(proposer_index);
}

View File

@@ -336,6 +336,7 @@ pub fn get_new_eth1_data<T: EthSpec>(
/// https://github.com/ethereum/consensus-specs/blob/v1.1.5/specs/merge/beacon-chain.md#process_execution_payload
pub fn partially_verify_execution_payload<T: EthSpec, Payload: ExecPayload<T>>(
state: &BeaconState<T>,
block_slot: Slot,
payload: &Payload,
spec: &ChainSpec,
) -> Result<(), BlockProcessingError> {
@@ -356,7 +357,7 @@ pub fn partially_verify_execution_payload<T: EthSpec, Payload: ExecPayload<T>>(
}
);
let timestamp = compute_timestamp_at_slot(state, spec)?;
let timestamp = compute_timestamp_at_slot(state, block_slot, spec)?;
block_verify!(
payload.timestamp() == timestamp,
BlockProcessingError::ExecutionInvalidTimestamp {
@@ -380,7 +381,7 @@ pub fn process_execution_payload<T: EthSpec, Payload: ExecPayload<T>>(
payload: &Payload,
spec: &ChainSpec,
) -> Result<(), BlockProcessingError> {
partially_verify_execution_payload(state, payload, spec)?;
partially_verify_execution_payload(state, state.slot(), payload, spec)?;
*state.latest_execution_payload_header_mut()? = payload.to_execution_payload_header();
@@ -417,9 +418,10 @@ pub fn is_execution_enabled<T: EthSpec, Payload: ExecPayload<T>>(
/// https://github.com/ethereum/consensus-specs/blob/dev/specs/merge/beacon-chain.md#compute_timestamp_at_slot
pub fn compute_timestamp_at_slot<T: EthSpec>(
state: &BeaconState<T>,
block_slot: Slot,
spec: &ChainSpec,
) -> Result<u64, ArithError> {
let slots_since_genesis = state.slot().as_u64().safe_sub(spec.genesis_slot.as_u64())?;
let slots_since_genesis = block_slot.as_u64().safe_sub(spec.genesis_slot.as_u64())?;
slots_since_genesis
.safe_mul(spec.seconds_per_slot)
.and_then(|since_genesis| state.genesis_time().safe_add(since_genesis))

View File

@@ -144,7 +144,8 @@ where
ctxt: &mut ConsensusContext<T>,
) -> Result<()> {
let block_root = Some(ctxt.get_current_block_root(block)?);
let verified_proposer_index = Some(ctxt.get_proposer_index(self.state, self.spec)?);
let verified_proposer_index =
Some(ctxt.get_proposer_index_from_epoch_state(self.state, self.spec)?);
self.include_block_proposal(block, block_root, verified_proposer_index)?;
self.include_all_signatures_except_proposal(block, ctxt)?;
@@ -159,7 +160,8 @@ where
block: &'a SignedBeaconBlock<T, Payload>,
ctxt: &mut ConsensusContext<T>,
) -> Result<()> {
let verified_proposer_index = Some(ctxt.get_proposer_index(self.state, self.spec)?);
let verified_proposer_index =
Some(ctxt.get_proposer_index_from_epoch_state(self.state, self.spec)?);
self.include_randao_reveal(block, verified_proposer_index)?;
self.include_proposer_slashings(block)?;
self.include_attester_slashings(block)?;
@@ -281,7 +283,7 @@ where
.attestations()
.iter()
.try_for_each(|attestation| {
let indexed_attestation = ctxt.get_indexed_attestation(&self.state, attestation)?;
let indexed_attestation = ctxt.get_indexed_attestation(self.state, attestation)?;
self.sets.push(indexed_attestation_signature_set(
self.state,