First tilt at accelerating block production

This commit is contained in:
Michael Sproul
2022-03-28 17:28:10 +11:00
parent eb0324aa6b
commit c08e26803c
7 changed files with 193 additions and 26 deletions

View File

@@ -2873,21 +2873,19 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
metrics::inc_counter(&metrics::BLOCK_PRODUCTION_REQUESTS);
let _complete_timer = metrics::start_timer(&metrics::BLOCK_PRODUCTION_TIMES);
// Producing a block requires the tree hash cache, so clone a full state corresponding to
// the head from the snapshot cache. Unfortunately we can't move the snapshot out of the
// cache (which would be fast), because we need to re-process the block after it has been
// signed. If we miss the cache or we're producing a block that conflicts with the head,
// fall back to getting the head from `slot - 1`.
let state_load_timer = metrics::start_timer(&metrics::BLOCK_PRODUCTION_STATE_LOAD_TIMES);
let head_info = self
.head_info()
.map_err(BlockProductionError::UnableToGetHeadInfo)?;
let (state, state_root_opt) = if head_info.slot < slot {
let state = self
.state_at_slot(slot - 1, StateSkipConfig::WithStateRoots)
.map_err(|_| BlockProductionError::UnableToProduceAtSlot(slot))?;
(state, None)
let (state, state_root_opt) = if head_info.slot <= slot {
// Fetch the head state advanced through to `slot`, which should be present in the state
// cache thanks to the state advance timer.
let (state_root, state) = self
.store
.get_advanced_state(head_info.block_root, slot, head_info.state_root)
.map_err(BlockProductionError::FailedToLoadState)?
.ok_or(BlockProductionError::UnableToProduceAtSlot(slot))?;
(state, Some(state_root))
} else {
warn!(
self.log,

View File

@@ -1,6 +1,6 @@
use crate::{BeaconChain, BeaconChainError, BeaconChainTypes};
use eth2::lighthouse::{AttestationRewards, BlockReward, BlockRewardMeta};
use operation_pool::{AttMaxCover, MaxCover};
use operation_pool::{AttMaxCover, MaxCover, RewardCache};
use state_processing::per_block_processing::altair::sync_committee::compute_sync_aggregate_rewards;
use types::{BeaconBlockRef, BeaconState, EthSpec, Hash256, RelativeEpoch};
@@ -16,13 +16,15 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
}
let active_indices = state.get_cached_active_validator_indices(RelativeEpoch::Current)?;
let mut reward_cache = RewardCache::default();
reward_cache.update(state)?;
let total_active_balance = state.get_total_balance(active_indices, &self.spec)?;
let mut per_attestation_rewards = block
.body()
.attestations()
.iter()
.map(|att| {
AttMaxCover::new(att, state, total_active_balance, &self.spec)
AttMaxCover::new(att, state, &reward_cache, total_active_balance, &self.spec)
.ok_or(BeaconChainError::BlockRewardAttestationError)
})
.collect::<Result<Vec<_>, _>>()?;

View File

@@ -209,6 +209,7 @@ pub enum BlockProductionError {
TerminalPoWBlockLookupFailed(execution_layer::Error),
GetPayloadFailed(execution_layer::Error),
FailedToReadFinalizedBlock(store::Error),
FailedToLoadState(store::Error),
MissingFinalizedBlock(Hash256),
BlockTooLarge(usize),
}