Implement ConsensusContext

This commit is contained in:
Michael Sproul
2022-02-17 16:40:15 +11:00
parent 1db0e32bfb
commit c88fcfed2b
10 changed files with 170 additions and 26 deletions

View File

@@ -70,7 +70,7 @@ use state_processing::{
per_block_processing::{errors::AttestationValidationError, is_merge_transition_complete},
per_slot_processing,
state_advance::{complete_state_advance, partial_state_advance},
BlockSignatureStrategy, SigVerifiedOp, VerifyBlockRoot,
BlockSignatureStrategy, ConsensusContext, SigVerifiedOp, VerifyBlockRoot,
};
use std::borrow::Cow;
use std::cmp::Ordering;
@@ -3096,12 +3096,13 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
}
let process_timer = metrics::start_timer(&metrics::BLOCK_PRODUCTION_PROCESS_TIMES);
let mut ctxt = ConsensusContext::new(block.slot()).set_proposer_index(proposer_index);
per_block_processing(
&mut state,
&block,
None,
BlockSignatureStrategy::VerifyRandao,
VerifyBlockRoot::True,
&mut ctxt,
&self.spec,
)?;
drop(process_timer);

View File

@@ -63,7 +63,8 @@ use state_processing::{
block_signature_verifier::{BlockSignatureVerifier, Error as BlockSignatureVerifierError},
per_block_processing, per_slot_processing,
state_advance::partial_state_advance,
BlockProcessingError, BlockSignatureStrategy, SlotProcessingError, VerifyBlockRoot,
BlockProcessingError, BlockSignatureStrategy, ConsensusContext, SlotProcessingError,
VerifyBlockRoot,
};
use std::borrow::Cow;
use std::fs;
@@ -488,10 +489,16 @@ pub fn signature_verify_chain_segment<T: BeaconChainTypes>(
let mut signature_verified_blocks = chain_segment
.into_iter()
.map(|(block_root, block)| SignatureVerifiedBlock {
block,
block_root,
parent: None,
.map(|(block_root, block)| {
// FIXME(sproul): safe to include proposer index here?
let consensus_context =
ConsensusContext::new(block.slot()).set_current_block_root(block_root);
SignatureVerifiedBlock {
block,
block_root,
parent: None,
consensus_context,
}
})
.collect::<Vec<_>>();
@@ -509,6 +516,7 @@ pub struct GossipVerifiedBlock<T: BeaconChainTypes> {
pub block: SignedBeaconBlock<T::EthSpec>,
pub block_root: Hash256,
parent: Option<PreProcessingSnapshot<T::EthSpec>>,
consensus_context: ConsensusContext<T::EthSpec>,
}
/// A wrapper around a `SignedBeaconBlock` that indicates that all signatures (except the deposit
@@ -517,6 +525,7 @@ pub struct SignatureVerifiedBlock<T: BeaconChainTypes> {
block: SignedBeaconBlock<T::EthSpec>,
block_root: Hash256,
parent: Option<PreProcessingSnapshot<T::EthSpec>>,
consensus_context: ConsensusContext<T::EthSpec>,
}
/// A wrapper around a `SignedBeaconBlock` that indicates that this block is fully verified and
@@ -780,10 +789,16 @@ impl<T: BeaconChainTypes> GossipVerifiedBlock<T> {
// Validate the block's execution_payload (if any).
validate_execution_payload_for_gossip(&parent_block, block.message(), chain)?;
// Having checked the proposer index and the block root we can cache them.
let consensus_context = ConsensusContext::new(block.slot())
.set_current_block_root(block_root)
.set_proposer_index(block.message().proposer_index());
Ok(Self {
block,
block_root,
parent,
consensus_context,
})
}
@@ -846,6 +861,8 @@ impl<T: BeaconChainTypes> SignatureVerifiedBlock<T> {
if signature_verifier.verify().is_ok() {
Ok(Self {
consensus_context: ConsensusContext::new(block.slot())
.set_current_block_root(block_root),
block,
block_root,
parent: Some(parent),
@@ -895,6 +912,7 @@ impl<T: BeaconChainTypes> SignatureVerifiedBlock<T> {
block,
block_root: from.block_root,
parent: Some(parent),
consensus_context: from.consensus_context,
})
} else {
Err(BlockError::InvalidSignature)
@@ -930,6 +948,7 @@ impl<T: BeaconChainTypes> IntoFullyVerifiedBlock<T> for SignatureVerifiedBlock<T
block,
self.block_root,
parent,
self.consensus_context,
chain,
)
.map_err(|e| BlockSlashInfo::SignatureValid(header, e))
@@ -972,6 +991,7 @@ impl<'a, T: BeaconChainTypes> FullyVerifiedBlock<'a, T> {
block: SignedBeaconBlock<T::EthSpec>,
block_root: Hash256,
parent: PreProcessingSnapshot<T::EthSpec>,
mut consensus_context: ConsensusContext<T::EthSpec>,
chain: &BeaconChain<T>,
) -> Result<Self, BlockError<T::EthSpec>> {
// Reject any block if its parent is not known to fork choice.
@@ -1172,10 +1192,10 @@ impl<'a, T: BeaconChainTypes> FullyVerifiedBlock<'a, T> {
if let Err(err) = per_block_processing(
&mut state,
&block,
Some(block_root),
// Signatures were verified earlier in this function.
BlockSignatureStrategy::NoVerification,
VerifyBlockRoot::True,
&mut consensus_context,
&chain.spec,
) {
match err {

View File

@@ -4,7 +4,8 @@ use itertools::process_results;
use slog::{info, warn, Logger};
use state_processing::state_advance::complete_state_advance;
use state_processing::{
per_block_processing, per_block_processing::BlockSignatureStrategy, VerifyBlockRoot,
per_block_processing, per_block_processing::BlockSignatureStrategy, ConsensusContext,
VerifyBlockRoot,
};
use std::sync::Arc;
use std::time::Duration;
@@ -158,12 +159,14 @@ pub fn reset_fork_choice_to_finalization<E: EthSpec, Hot: ItemStore<E>, Cold: It
complete_state_advance(&mut state, None, block.slot(), spec)
.map_err(|e| format!("State advance failed: {:?}", e))?;
let mut ctxt = ConsensusContext::new(block.slot())
.set_proposer_index(block.message().proposer_index());
per_block_processing(
&mut state,
&block,
None,
BlockSignatureStrategy::NoVerification,
VerifyBlockRoot::True,
&mut ctxt,
spec,
)
.map_err(|e| format!("Error replaying block: {:?}", e))?;