mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-08 01:05:47 +00:00
Implement ConsensusContext
This commit is contained in:
@@ -70,7 +70,7 @@ use state_processing::{
|
|||||||
per_block_processing::{errors::AttestationValidationError, is_merge_transition_complete},
|
per_block_processing::{errors::AttestationValidationError, is_merge_transition_complete},
|
||||||
per_slot_processing,
|
per_slot_processing,
|
||||||
state_advance::{complete_state_advance, partial_state_advance},
|
state_advance::{complete_state_advance, partial_state_advance},
|
||||||
BlockSignatureStrategy, SigVerifiedOp, VerifyBlockRoot,
|
BlockSignatureStrategy, ConsensusContext, SigVerifiedOp, VerifyBlockRoot,
|
||||||
};
|
};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::cmp::Ordering;
|
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 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(
|
per_block_processing(
|
||||||
&mut state,
|
&mut state,
|
||||||
&block,
|
&block,
|
||||||
None,
|
|
||||||
BlockSignatureStrategy::VerifyRandao,
|
BlockSignatureStrategy::VerifyRandao,
|
||||||
VerifyBlockRoot::True,
|
VerifyBlockRoot::True,
|
||||||
|
&mut ctxt,
|
||||||
&self.spec,
|
&self.spec,
|
||||||
)?;
|
)?;
|
||||||
drop(process_timer);
|
drop(process_timer);
|
||||||
|
|||||||
@@ -63,7 +63,8 @@ use state_processing::{
|
|||||||
block_signature_verifier::{BlockSignatureVerifier, Error as BlockSignatureVerifierError},
|
block_signature_verifier::{BlockSignatureVerifier, Error as BlockSignatureVerifierError},
|
||||||
per_block_processing, per_slot_processing,
|
per_block_processing, per_slot_processing,
|
||||||
state_advance::partial_state_advance,
|
state_advance::partial_state_advance,
|
||||||
BlockProcessingError, BlockSignatureStrategy, SlotProcessingError, VerifyBlockRoot,
|
BlockProcessingError, BlockSignatureStrategy, ConsensusContext, SlotProcessingError,
|
||||||
|
VerifyBlockRoot,
|
||||||
};
|
};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@@ -488,10 +489,16 @@ pub fn signature_verify_chain_segment<T: BeaconChainTypes>(
|
|||||||
|
|
||||||
let mut signature_verified_blocks = chain_segment
|
let mut signature_verified_blocks = chain_segment
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(block_root, block)| SignatureVerifiedBlock {
|
.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,
|
||||||
block_root,
|
block_root,
|
||||||
parent: None,
|
parent: None,
|
||||||
|
consensus_context,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
@@ -509,6 +516,7 @@ pub struct GossipVerifiedBlock<T: BeaconChainTypes> {
|
|||||||
pub block: SignedBeaconBlock<T::EthSpec>,
|
pub block: SignedBeaconBlock<T::EthSpec>,
|
||||||
pub block_root: Hash256,
|
pub block_root: Hash256,
|
||||||
parent: Option<PreProcessingSnapshot<T::EthSpec>>,
|
parent: Option<PreProcessingSnapshot<T::EthSpec>>,
|
||||||
|
consensus_context: ConsensusContext<T::EthSpec>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A wrapper around a `SignedBeaconBlock` that indicates that all signatures (except the deposit
|
/// 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: SignedBeaconBlock<T::EthSpec>,
|
||||||
block_root: Hash256,
|
block_root: Hash256,
|
||||||
parent: Option<PreProcessingSnapshot<T::EthSpec>>,
|
parent: Option<PreProcessingSnapshot<T::EthSpec>>,
|
||||||
|
consensus_context: ConsensusContext<T::EthSpec>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A wrapper around a `SignedBeaconBlock` that indicates that this block is fully verified and
|
/// 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 the block's execution_payload (if any).
|
||||||
validate_execution_payload_for_gossip(&parent_block, block.message(), chain)?;
|
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 {
|
Ok(Self {
|
||||||
block,
|
block,
|
||||||
block_root,
|
block_root,
|
||||||
parent,
|
parent,
|
||||||
|
consensus_context,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -846,6 +861,8 @@ impl<T: BeaconChainTypes> SignatureVerifiedBlock<T> {
|
|||||||
|
|
||||||
if signature_verifier.verify().is_ok() {
|
if signature_verifier.verify().is_ok() {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
consensus_context: ConsensusContext::new(block.slot())
|
||||||
|
.set_current_block_root(block_root),
|
||||||
block,
|
block,
|
||||||
block_root,
|
block_root,
|
||||||
parent: Some(parent),
|
parent: Some(parent),
|
||||||
@@ -895,6 +912,7 @@ impl<T: BeaconChainTypes> SignatureVerifiedBlock<T> {
|
|||||||
block,
|
block,
|
||||||
block_root: from.block_root,
|
block_root: from.block_root,
|
||||||
parent: Some(parent),
|
parent: Some(parent),
|
||||||
|
consensus_context: from.consensus_context,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(BlockError::InvalidSignature)
|
Err(BlockError::InvalidSignature)
|
||||||
@@ -930,6 +948,7 @@ impl<T: BeaconChainTypes> IntoFullyVerifiedBlock<T> for SignatureVerifiedBlock<T
|
|||||||
block,
|
block,
|
||||||
self.block_root,
|
self.block_root,
|
||||||
parent,
|
parent,
|
||||||
|
self.consensus_context,
|
||||||
chain,
|
chain,
|
||||||
)
|
)
|
||||||
.map_err(|e| BlockSlashInfo::SignatureValid(header, e))
|
.map_err(|e| BlockSlashInfo::SignatureValid(header, e))
|
||||||
@@ -972,6 +991,7 @@ impl<'a, T: BeaconChainTypes> FullyVerifiedBlock<'a, T> {
|
|||||||
block: SignedBeaconBlock<T::EthSpec>,
|
block: SignedBeaconBlock<T::EthSpec>,
|
||||||
block_root: Hash256,
|
block_root: Hash256,
|
||||||
parent: PreProcessingSnapshot<T::EthSpec>,
|
parent: PreProcessingSnapshot<T::EthSpec>,
|
||||||
|
mut consensus_context: ConsensusContext<T::EthSpec>,
|
||||||
chain: &BeaconChain<T>,
|
chain: &BeaconChain<T>,
|
||||||
) -> Result<Self, BlockError<T::EthSpec>> {
|
) -> Result<Self, BlockError<T::EthSpec>> {
|
||||||
// Reject any block if its parent is not known to fork choice.
|
// 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(
|
if let Err(err) = per_block_processing(
|
||||||
&mut state,
|
&mut state,
|
||||||
&block,
|
&block,
|
||||||
Some(block_root),
|
|
||||||
// Signatures were verified earlier in this function.
|
// Signatures were verified earlier in this function.
|
||||||
BlockSignatureStrategy::NoVerification,
|
BlockSignatureStrategy::NoVerification,
|
||||||
VerifyBlockRoot::True,
|
VerifyBlockRoot::True,
|
||||||
|
&mut consensus_context,
|
||||||
&chain.spec,
|
&chain.spec,
|
||||||
) {
|
) {
|
||||||
match err {
|
match err {
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ use itertools::process_results;
|
|||||||
use slog::{info, warn, Logger};
|
use slog::{info, warn, Logger};
|
||||||
use state_processing::state_advance::complete_state_advance;
|
use state_processing::state_advance::complete_state_advance;
|
||||||
use state_processing::{
|
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::sync::Arc;
|
||||||
use std::time::Duration;
|
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)
|
complete_state_advance(&mut state, None, block.slot(), spec)
|
||||||
.map_err(|e| format!("State advance failed: {:?}", e))?;
|
.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(
|
per_block_processing(
|
||||||
&mut state,
|
&mut state,
|
||||||
&block,
|
&block,
|
||||||
None,
|
|
||||||
BlockSignatureStrategy::NoVerification,
|
BlockSignatureStrategy::NoVerification,
|
||||||
VerifyBlockRoot::True,
|
VerifyBlockRoot::True,
|
||||||
|
&mut ctxt,
|
||||||
spec,
|
spec,
|
||||||
)
|
)
|
||||||
.map_err(|e| format!("Error replaying block: {:?}", e))?;
|
.map_err(|e| format!("Error replaying block: {:?}", e))?;
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ use crate::{Error, ItemStore, KeyValueStore};
|
|||||||
use itertools::{process_results, Itertools};
|
use itertools::{process_results, Itertools};
|
||||||
use slog::info;
|
use slog::info;
|
||||||
use state_processing::{
|
use state_processing::{
|
||||||
per_block_processing, per_slot_processing, BlockSignatureStrategy, VerifyBlockRoot,
|
per_block_processing, per_slot_processing, BlockSignatureStrategy, ConsensusContext,
|
||||||
|
VerifyBlockRoot,
|
||||||
};
|
};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use types::{EthSpec, Hash256};
|
use types::{EthSpec, Hash256};
|
||||||
@@ -87,12 +88,16 @@ where
|
|||||||
|
|
||||||
// Apply block.
|
// Apply block.
|
||||||
if let Some(block) = block {
|
if let Some(block) = block {
|
||||||
|
let mut ctxt = ConsensusContext::new(block.slot())
|
||||||
|
.set_current_block_root(block_root)
|
||||||
|
.set_proposer_index(block.message().proposer_index());
|
||||||
|
|
||||||
per_block_processing(
|
per_block_processing(
|
||||||
&mut state,
|
&mut state,
|
||||||
&block,
|
&block,
|
||||||
Some(block_root),
|
|
||||||
BlockSignatureStrategy::NoVerification,
|
BlockSignatureStrategy::NoVerification,
|
||||||
VerifyBlockRoot::True,
|
VerifyBlockRoot::True,
|
||||||
|
&mut ctxt,
|
||||||
&self.spec,
|
&self.spec,
|
||||||
)
|
)
|
||||||
.map_err(HotColdDBError::BlockReplayBlockError)?;
|
.map_err(HotColdDBError::BlockReplayBlockError)?;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
per_block_processing, per_epoch_processing::EpochProcessingSummary, per_slot_processing,
|
per_block_processing, per_epoch_processing::EpochProcessingSummary, per_slot_processing,
|
||||||
BlockProcessingError, BlockSignatureStrategy, SlotProcessingError, VerifyBlockRoot,
|
BlockProcessingError, BlockSignatureStrategy, ConsensusContext, SlotProcessingError,
|
||||||
|
VerifyBlockRoot,
|
||||||
};
|
};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use types::{BeaconState, ChainSpec, EthSpec, Hash256, SignedBeaconBlock, Slot};
|
use types::{BeaconState, ChainSpec, EthSpec, Hash256, SignedBeaconBlock, Slot};
|
||||||
@@ -226,12 +227,13 @@ where
|
|||||||
VerifyBlockRoot::False
|
VerifyBlockRoot::False
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
let mut ctxt = ConsensusContext::new(block.slot());
|
||||||
per_block_processing(
|
per_block_processing(
|
||||||
&mut self.state,
|
&mut self.state,
|
||||||
block,
|
block,
|
||||||
None,
|
|
||||||
self.block_sig_strategy,
|
self.block_sig_strategy,
|
||||||
verify_block_root,
|
verify_block_root,
|
||||||
|
&mut ctxt,
|
||||||
self.spec,
|
self.spec,
|
||||||
)
|
)
|
||||||
.map_err(BlockReplayError::from)?;
|
.map_err(BlockReplayError::from)?;
|
||||||
|
|||||||
89
consensus/state_processing/src/consensus_context.rs
Normal file
89
consensus/state_processing/src/consensus_context.rs
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
use std::marker::PhantomData;
|
||||||
|
use tree_hash::TreeHash;
|
||||||
|
use types::{BeaconState, BeaconStateError, ChainSpec, EthSpec, Hash256, SignedBeaconBlock, Slot};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ConsensusContext<T: EthSpec> {
|
||||||
|
/// Slot to act as an identifier/safeguard
|
||||||
|
slot: Slot,
|
||||||
|
/// Proposer index of the block at `slot`.
|
||||||
|
proposer_index: Option<u64>,
|
||||||
|
/// Block root of the block at `slot`.
|
||||||
|
current_block_root: Option<Hash256>,
|
||||||
|
_phantom: PhantomData<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub enum ContextError {
|
||||||
|
BeaconState(BeaconStateError),
|
||||||
|
SlotMismatch { slot: Slot, expected: Slot },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<BeaconStateError> for ContextError {
|
||||||
|
fn from(e: BeaconStateError) -> Self {
|
||||||
|
Self::BeaconState(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: EthSpec> ConsensusContext<T> {
|
||||||
|
pub fn new(slot: Slot) -> Self {
|
||||||
|
Self {
|
||||||
|
slot,
|
||||||
|
proposer_index: None,
|
||||||
|
current_block_root: None,
|
||||||
|
_phantom: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_proposer_index(mut self, proposer_index: u64) -> Self {
|
||||||
|
self.proposer_index = Some(proposer_index);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_proposer_index(
|
||||||
|
&mut self,
|
||||||
|
state: &BeaconState<T>,
|
||||||
|
spec: &ChainSpec,
|
||||||
|
) -> Result<u64, ContextError> {
|
||||||
|
self.check_slot(state.slot())?;
|
||||||
|
|
||||||
|
if let Some(proposer_index) = self.proposer_index {
|
||||||
|
return Ok(proposer_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
let proposer_index = state.get_beacon_proposer_index(self.slot, spec)? as u64;
|
||||||
|
self.proposer_index = Some(proposer_index);
|
||||||
|
Ok(proposer_index)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_current_block_root(mut self, block_root: Hash256) -> Self {
|
||||||
|
self.current_block_root = Some(block_root);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_current_block_root(
|
||||||
|
&mut self,
|
||||||
|
block: &SignedBeaconBlock<T>,
|
||||||
|
) -> Result<Hash256, ContextError> {
|
||||||
|
self.check_slot(block.slot())?;
|
||||||
|
|
||||||
|
if let Some(current_block_root) = self.current_block_root {
|
||||||
|
return Ok(current_block_root);
|
||||||
|
}
|
||||||
|
|
||||||
|
let current_block_root = block.tree_hash_root();
|
||||||
|
self.current_block_root = Some(current_block_root);
|
||||||
|
Ok(current_block_root)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_slot(&self, slot: Slot) -> Result<(), ContextError> {
|
||||||
|
if slot == self.slot {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(ContextError::SlotMismatch {
|
||||||
|
slot,
|
||||||
|
expected: self.slot,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,6 +18,7 @@ mod metrics;
|
|||||||
|
|
||||||
pub mod block_replayer;
|
pub mod block_replayer;
|
||||||
pub mod common;
|
pub mod common;
|
||||||
|
pub mod consensus_context;
|
||||||
pub mod genesis;
|
pub mod genesis;
|
||||||
pub mod per_block_processing;
|
pub mod per_block_processing;
|
||||||
pub mod per_epoch_processing;
|
pub mod per_epoch_processing;
|
||||||
@@ -27,6 +28,7 @@ pub mod upgrade;
|
|||||||
pub mod verify_operation;
|
pub mod verify_operation;
|
||||||
|
|
||||||
pub use block_replayer::{BlockReplayError, BlockReplayer};
|
pub use block_replayer::{BlockReplayError, BlockReplayer};
|
||||||
|
pub use consensus_context::{ConsensusContext, ContextError};
|
||||||
pub use genesis::{
|
pub use genesis::{
|
||||||
eth2_genesis_time, initialize_beacon_state_from_eth1, is_valid_genesis_state,
|
eth2_genesis_time, initialize_beacon_state_from_eth1, is_valid_genesis_state,
|
||||||
process_activations,
|
process_activations,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use crate::consensus_context::ConsensusContext;
|
||||||
use errors::{BlockOperationError, BlockProcessingError, HeaderInvalid};
|
use errors::{BlockOperationError, BlockProcessingError, HeaderInvalid};
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use safe_arith::{ArithError, SafeArith};
|
use safe_arith::{ArithError, SafeArith};
|
||||||
@@ -90,9 +91,9 @@ pub enum VerifyBlockRoot {
|
|||||||
pub fn per_block_processing<T: EthSpec>(
|
pub fn per_block_processing<T: EthSpec>(
|
||||||
state: &mut BeaconState<T>,
|
state: &mut BeaconState<T>,
|
||||||
signed_block: &SignedBeaconBlock<T>,
|
signed_block: &SignedBeaconBlock<T>,
|
||||||
block_root: Option<Hash256>,
|
|
||||||
block_signature_strategy: BlockSignatureStrategy,
|
block_signature_strategy: BlockSignatureStrategy,
|
||||||
verify_block_root: VerifyBlockRoot,
|
verify_block_root: VerifyBlockRoot,
|
||||||
|
ctxt: &mut ConsensusContext<T>,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<(), BlockProcessingError> {
|
) -> Result<(), BlockProcessingError> {
|
||||||
let block = signed_block.message();
|
let block = signed_block.message();
|
||||||
@@ -110,6 +111,7 @@ pub fn per_block_processing<T: EthSpec>(
|
|||||||
let verify_signatures = match block_signature_strategy {
|
let verify_signatures = match block_signature_strategy {
|
||||||
BlockSignatureStrategy::VerifyBulk => {
|
BlockSignatureStrategy::VerifyBulk => {
|
||||||
// Verify all signatures in the block at once.
|
// Verify all signatures in the block at once.
|
||||||
|
let block_root = Some(ctxt.get_current_block_root(signed_block)?);
|
||||||
block_verify!(
|
block_verify!(
|
||||||
BlockSignatureVerifier::verify_entire_block(
|
BlockSignatureVerifier::verify_entire_block(
|
||||||
state,
|
state,
|
||||||
@@ -129,10 +131,10 @@ pub fn per_block_processing<T: EthSpec>(
|
|||||||
BlockSignatureStrategy::VerifyRandao => VerifySignatures::False,
|
BlockSignatureStrategy::VerifyRandao => VerifySignatures::False,
|
||||||
};
|
};
|
||||||
|
|
||||||
let proposer_index = process_block_header(state, block, verify_block_root, spec)?;
|
let proposer_index = process_block_header(state, block, verify_block_root, ctxt, spec)?;
|
||||||
|
|
||||||
if verify_signatures.is_true() {
|
if verify_signatures.is_true() {
|
||||||
verify_block_signature(state, signed_block, block_root, spec)?;
|
verify_block_signature(state, signed_block, ctxt, spec)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let verify_randao = if let BlockSignatureStrategy::VerifyRandao = block_signature_strategy {
|
let verify_randao = if let BlockSignatureStrategy::VerifyRandao = block_signature_strategy {
|
||||||
@@ -174,6 +176,7 @@ pub fn process_block_header<T: EthSpec>(
|
|||||||
state: &mut BeaconState<T>,
|
state: &mut BeaconState<T>,
|
||||||
block: BeaconBlockRef<'_, T>,
|
block: BeaconBlockRef<'_, T>,
|
||||||
verify_block_root: VerifyBlockRoot,
|
verify_block_root: VerifyBlockRoot,
|
||||||
|
ctxt: &mut ConsensusContext<T>,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<u64, BlockOperationError<HeaderInvalid>> {
|
) -> Result<u64, BlockOperationError<HeaderInvalid>> {
|
||||||
// Verify that the slots match
|
// Verify that the slots match
|
||||||
@@ -192,8 +195,8 @@ pub fn process_block_header<T: EthSpec>(
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Verify that proposer index is the correct index
|
// Verify that proposer index is the correct index
|
||||||
let proposer_index = block.proposer_index() as usize;
|
let proposer_index = block.proposer_index();
|
||||||
let state_proposer_index = state.get_beacon_proposer_index(block.slot(), spec)?;
|
let state_proposer_index = ctxt.get_proposer_index(state, spec)?;
|
||||||
verify!(
|
verify!(
|
||||||
proposer_index == state_proposer_index,
|
proposer_index == state_proposer_index,
|
||||||
HeaderInvalid::ProposerIndexMismatch {
|
HeaderInvalid::ProposerIndexMismatch {
|
||||||
@@ -217,11 +220,11 @@ pub fn process_block_header<T: EthSpec>(
|
|||||||
|
|
||||||
// Verify proposer is not slashed
|
// Verify proposer is not slashed
|
||||||
verify!(
|
verify!(
|
||||||
!state.get_validator(proposer_index)?.slashed,
|
!state.get_validator(proposer_index as usize)?.slashed,
|
||||||
HeaderInvalid::ProposerSlashed(proposer_index)
|
HeaderInvalid::ProposerSlashed(proposer_index)
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(block.proposer_index())
|
Ok(proposer_index)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Verifies the signature of a block.
|
/// Verifies the signature of a block.
|
||||||
@@ -230,9 +233,10 @@ pub fn process_block_header<T: EthSpec>(
|
|||||||
pub fn verify_block_signature<T: EthSpec>(
|
pub fn verify_block_signature<T: EthSpec>(
|
||||||
state: &BeaconState<T>,
|
state: &BeaconState<T>,
|
||||||
block: &SignedBeaconBlock<T>,
|
block: &SignedBeaconBlock<T>,
|
||||||
block_root: Option<Hash256>,
|
ctxt: &mut ConsensusContext<T>,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<(), BlockOperationError<HeaderInvalid>> {
|
) -> Result<(), BlockOperationError<HeaderInvalid>> {
|
||||||
|
let block_root = Some(ctxt.get_current_block_root(block)?);
|
||||||
verify!(
|
verify!(
|
||||||
block_proposal_signature_set(
|
block_proposal_signature_set(
|
||||||
state,
|
state,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use super::signature_sets::Error as SignatureSetError;
|
use super::signature_sets::Error as SignatureSetError;
|
||||||
|
use crate::ContextError;
|
||||||
use merkle_proof::MerkleTreeError;
|
use merkle_proof::MerkleTreeError;
|
||||||
use safe_arith::ArithError;
|
use safe_arith::ArithError;
|
||||||
use types::*;
|
use types::*;
|
||||||
@@ -70,6 +71,7 @@ pub enum BlockProcessingError {
|
|||||||
found: u64,
|
found: u64,
|
||||||
},
|
},
|
||||||
ExecutionInvalid,
|
ExecutionInvalid,
|
||||||
|
ConsensusContext(ContextError),
|
||||||
#[cfg(feature = "milhouse")]
|
#[cfg(feature = "milhouse")]
|
||||||
MilhouseError(milhouse::Error),
|
MilhouseError(milhouse::Error),
|
||||||
}
|
}
|
||||||
@@ -104,6 +106,12 @@ impl From<SyncAggregateInvalid> for BlockProcessingError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<ContextError> for BlockProcessingError {
|
||||||
|
fn from(e: ContextError) -> Self {
|
||||||
|
BlockProcessingError::ConsensusContext(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "milhouse")]
|
#[cfg(feature = "milhouse")]
|
||||||
impl From<milhouse::Error> for BlockProcessingError {
|
impl From<milhouse::Error> for BlockProcessingError {
|
||||||
fn from(e: milhouse::Error) -> Self {
|
fn from(e: milhouse::Error) -> Self {
|
||||||
@@ -118,6 +126,7 @@ impl From<BlockOperationError<HeaderInvalid>> for BlockProcessingError {
|
|||||||
BlockOperationError::BeaconStateError(e) => BlockProcessingError::BeaconStateError(e),
|
BlockOperationError::BeaconStateError(e) => BlockProcessingError::BeaconStateError(e),
|
||||||
BlockOperationError::SignatureSetError(e) => BlockProcessingError::SignatureSetError(e),
|
BlockOperationError::SignatureSetError(e) => BlockProcessingError::SignatureSetError(e),
|
||||||
BlockOperationError::SszTypesError(e) => BlockProcessingError::SszTypesError(e),
|
BlockOperationError::SszTypesError(e) => BlockProcessingError::SszTypesError(e),
|
||||||
|
BlockOperationError::ConsensusContext(e) => BlockProcessingError::ConsensusContext(e),
|
||||||
BlockOperationError::ArithError(e) => BlockProcessingError::ArithError(e),
|
BlockOperationError::ArithError(e) => BlockProcessingError::ArithError(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,6 +154,7 @@ macro_rules! impl_into_block_processing_error_with_index {
|
|||||||
BlockOperationError::BeaconStateError(e) => BlockProcessingError::BeaconStateError(e),
|
BlockOperationError::BeaconStateError(e) => BlockProcessingError::BeaconStateError(e),
|
||||||
BlockOperationError::SignatureSetError(e) => BlockProcessingError::SignatureSetError(e),
|
BlockOperationError::SignatureSetError(e) => BlockProcessingError::SignatureSetError(e),
|
||||||
BlockOperationError::SszTypesError(e) => BlockProcessingError::SszTypesError(e),
|
BlockOperationError::SszTypesError(e) => BlockProcessingError::SszTypesError(e),
|
||||||
|
BlockOperationError::ConsensusContext(e) => BlockProcessingError::ConsensusContext(e),
|
||||||
BlockOperationError::ArithError(e) => BlockProcessingError::ArithError(e),
|
BlockOperationError::ArithError(e) => BlockProcessingError::ArithError(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -176,6 +186,7 @@ pub enum BlockOperationError<T> {
|
|||||||
BeaconStateError(BeaconStateError),
|
BeaconStateError(BeaconStateError),
|
||||||
SignatureSetError(SignatureSetError),
|
SignatureSetError(SignatureSetError),
|
||||||
SszTypesError(ssz_types::Error),
|
SszTypesError(ssz_types::Error),
|
||||||
|
ConsensusContext(ContextError),
|
||||||
ArithError(ArithError),
|
ArithError(ArithError),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,6 +219,12 @@ impl<T> From<ArithError> for BlockOperationError<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> From<ContextError> for BlockOperationError<T> {
|
||||||
|
fn from(e: ContextError) -> Self {
|
||||||
|
BlockOperationError::ConsensusContext(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub enum HeaderInvalid {
|
pub enum HeaderInvalid {
|
||||||
ProposalSignatureInvalid,
|
ProposalSignatureInvalid,
|
||||||
@@ -217,14 +234,14 @@ pub enum HeaderInvalid {
|
|||||||
block_slot: Slot,
|
block_slot: Slot,
|
||||||
},
|
},
|
||||||
ProposerIndexMismatch {
|
ProposerIndexMismatch {
|
||||||
block_proposer_index: usize,
|
block_proposer_index: u64,
|
||||||
state_proposer_index: usize,
|
state_proposer_index: u64,
|
||||||
},
|
},
|
||||||
ParentBlockRootMismatch {
|
ParentBlockRootMismatch {
|
||||||
state: Hash256,
|
state: Hash256,
|
||||||
block: Hash256,
|
block: Hash256,
|
||||||
},
|
},
|
||||||
ProposerSlashed(usize),
|
ProposerSlashed(u64),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
@@ -319,6 +336,7 @@ impl From<BlockOperationError<IndexedAttestationInvalid>>
|
|||||||
BlockOperationError::BeaconStateError(e) => BlockOperationError::BeaconStateError(e),
|
BlockOperationError::BeaconStateError(e) => BlockOperationError::BeaconStateError(e),
|
||||||
BlockOperationError::SignatureSetError(e) => BlockOperationError::SignatureSetError(e),
|
BlockOperationError::SignatureSetError(e) => BlockOperationError::SignatureSetError(e),
|
||||||
BlockOperationError::SszTypesError(e) => BlockOperationError::SszTypesError(e),
|
BlockOperationError::SszTypesError(e) => BlockOperationError::SszTypesError(e),
|
||||||
|
BlockOperationError::ConsensusContext(e) => BlockOperationError::ConsensusContext(e),
|
||||||
BlockOperationError::ArithError(e) => BlockOperationError::ArithError(e),
|
BlockOperationError::ArithError(e) => BlockOperationError::ArithError(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ fn do_transition<T: EthSpec>(
|
|||||||
&mut pre_state,
|
&mut pre_state,
|
||||||
&block,
|
&block,
|
||||||
None,
|
None,
|
||||||
BlockSignatureStrategy::VerifyBulk,
|
BlockSignatureStrategy::NoVerification,
|
||||||
VerifyBlockRoot::True,
|
VerifyBlockRoot::True,
|
||||||
spec,
|
spec,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user