mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 18:32:42 +00:00
Consensus context with proposer index caching (#3604)
## Issue Addressed Closes https://github.com/sigp/lighthouse/issues/2371 ## Proposed Changes Backport some changes from `tree-states` that remove duplicated calculations of the `proposer_index`. With this change the proposer index should be calculated only once for each block, and then plumbed through to every place it is required. ## Additional Info In future I hope to add more data to the consensus context that is cached on a per-epoch basis, like the effective balances of validators and the base rewards. There are some other changes to remove indexing in tests that were also useful for `tree-states` (the `tree-states` types don't implement `Index`).
This commit is contained in:
@@ -5,14 +5,17 @@ use crate::decode::{ssz_decode_file, ssz_decode_file_with, ssz_decode_state, yam
|
||||
use crate::testing_spec;
|
||||
use crate::type_name::TypeName;
|
||||
use serde_derive::Deserialize;
|
||||
use state_processing::per_block_processing::{
|
||||
errors::BlockProcessingError,
|
||||
process_block_header, process_execution_payload,
|
||||
process_operations::{
|
||||
altair, base, process_attester_slashings, process_deposits, process_exits,
|
||||
process_proposer_slashings,
|
||||
use state_processing::{
|
||||
per_block_processing::{
|
||||
errors::BlockProcessingError,
|
||||
process_block_header, process_execution_payload,
|
||||
process_operations::{
|
||||
altair, base, process_attester_slashings, process_deposits, process_exits,
|
||||
process_proposer_slashings,
|
||||
},
|
||||
process_sync_aggregate, VerifyBlockRoot, VerifySignatures,
|
||||
},
|
||||
process_sync_aggregate, VerifyBlockRoot, VerifySignatures,
|
||||
ConsensusContext,
|
||||
};
|
||||
use std::fmt::Debug;
|
||||
use std::path::Path;
|
||||
@@ -76,11 +79,16 @@ impl<E: EthSpec> Operation<E> for Attestation<E> {
|
||||
spec: &ChainSpec,
|
||||
_: &Operations<E, Self>,
|
||||
) -> Result<(), BlockProcessingError> {
|
||||
let proposer_index = state.get_beacon_proposer_index(state.slot(), spec)? as u64;
|
||||
let mut ctxt = ConsensusContext::new(state.slot());
|
||||
let proposer_index = ctxt.get_proposer_index(state, spec)?;
|
||||
match state {
|
||||
BeaconState::Base(_) => {
|
||||
base::process_attestations(state, &[self.clone()], VerifySignatures::True, spec)
|
||||
}
|
||||
BeaconState::Base(_) => base::process_attestations(
|
||||
state,
|
||||
&[self.clone()],
|
||||
VerifySignatures::True,
|
||||
&mut ctxt,
|
||||
spec,
|
||||
),
|
||||
BeaconState::Altair(_) | BeaconState::Merge(_) => altair::process_attestation(
|
||||
state,
|
||||
self,
|
||||
@@ -108,7 +116,14 @@ impl<E: EthSpec> Operation<E> for AttesterSlashing<E> {
|
||||
spec: &ChainSpec,
|
||||
_: &Operations<E, Self>,
|
||||
) -> Result<(), BlockProcessingError> {
|
||||
process_attester_slashings(state, &[self.clone()], VerifySignatures::True, spec)
|
||||
let mut ctxt = ConsensusContext::new(state.slot());
|
||||
process_attester_slashings(
|
||||
state,
|
||||
&[self.clone()],
|
||||
VerifySignatures::True,
|
||||
&mut ctxt,
|
||||
spec,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +162,14 @@ impl<E: EthSpec> Operation<E> for ProposerSlashing {
|
||||
spec: &ChainSpec,
|
||||
_: &Operations<E, Self>,
|
||||
) -> Result<(), BlockProcessingError> {
|
||||
process_proposer_slashings(state, &[self.clone()], VerifySignatures::True, spec)
|
||||
let mut ctxt = ConsensusContext::new(state.slot());
|
||||
process_proposer_slashings(
|
||||
state,
|
||||
&[self.clone()],
|
||||
VerifySignatures::True,
|
||||
&mut ctxt,
|
||||
spec,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,10 +211,12 @@ impl<E: EthSpec> Operation<E> for BeaconBlock<E> {
|
||||
spec: &ChainSpec,
|
||||
_: &Operations<E, Self>,
|
||||
) -> Result<(), BlockProcessingError> {
|
||||
let mut ctxt = ConsensusContext::new(state.slot());
|
||||
process_block_header(
|
||||
state,
|
||||
self.to_ref().temporary_block_header(),
|
||||
VerifyBlockRoot::True,
|
||||
&mut ctxt,
|
||||
spec,
|
||||
)?;
|
||||
Ok(())
|
||||
|
||||
@@ -5,7 +5,7 @@ use crate::decode::{ssz_decode_file_with, ssz_decode_state, yaml_decode_file};
|
||||
use serde_derive::Deserialize;
|
||||
use state_processing::{
|
||||
per_block_processing, per_slot_processing, BlockProcessingError, BlockSignatureStrategy,
|
||||
VerifyBlockRoot,
|
||||
ConsensusContext, VerifyBlockRoot,
|
||||
};
|
||||
use types::{BeaconState, EthSpec, ForkName, RelativeEpoch, SignedBeaconBlock};
|
||||
|
||||
@@ -91,26 +91,28 @@ impl<E: EthSpec> Case for SanityBlocks<E> {
|
||||
.build_committee_cache(RelativeEpoch::Current, spec)
|
||||
.unwrap();
|
||||
|
||||
let mut ctxt = ConsensusContext::new(indiv_state.slot());
|
||||
per_block_processing(
|
||||
&mut indiv_state,
|
||||
signed_block,
|
||||
None,
|
||||
BlockSignatureStrategy::VerifyIndividual,
|
||||
VerifyBlockRoot::True,
|
||||
&mut ctxt,
|
||||
spec,
|
||||
)?;
|
||||
|
||||
let mut ctxt = ConsensusContext::new(indiv_state.slot());
|
||||
per_block_processing(
|
||||
&mut bulk_state,
|
||||
signed_block,
|
||||
None,
|
||||
BlockSignatureStrategy::VerifyBulk,
|
||||
VerifyBlockRoot::True,
|
||||
&mut ctxt,
|
||||
spec,
|
||||
)?;
|
||||
|
||||
if block.state_root() == bulk_state.canonical_root()
|
||||
&& block.state_root() == indiv_state.canonical_root()
|
||||
if block.state_root() == bulk_state.update_tree_hash_cache().unwrap()
|
||||
&& block.state_root() == indiv_state.update_tree_hash_cache().unwrap()
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::decode::{ssz_decode_file_with, ssz_decode_state, yaml_decode_file};
|
||||
use serde_derive::Deserialize;
|
||||
use state_processing::{
|
||||
per_block_processing, state_advance::complete_state_advance, BlockSignatureStrategy,
|
||||
VerifyBlockRoot,
|
||||
ConsensusContext, VerifyBlockRoot,
|
||||
};
|
||||
use std::str::FromStr;
|
||||
use types::{BeaconState, Epoch, ForkName, SignedBeaconBlock};
|
||||
@@ -91,12 +91,13 @@ impl<E: EthSpec> Case for TransitionTest<E> {
|
||||
.map_err(|e| format!("Failed to advance: {:?}", e))?;
|
||||
|
||||
// Apply block.
|
||||
let mut ctxt = ConsensusContext::new(state.slot());
|
||||
per_block_processing(
|
||||
&mut state,
|
||||
block,
|
||||
None,
|
||||
BlockSignatureStrategy::VerifyBulk,
|
||||
VerifyBlockRoot::True,
|
||||
&mut ctxt,
|
||||
spec,
|
||||
)
|
||||
.map_err(|e| format!("Block processing failed: {:?}", e))?;
|
||||
|
||||
Reference in New Issue
Block a user