Plumb reward cache through reward calc

This commit is contained in:
Michael Sproul
2022-07-18 16:02:42 +10:00
parent 830f8032f0
commit 092d228078
7 changed files with 28 additions and 13 deletions

View File

@@ -3280,7 +3280,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
metrics::start_timer(&metrics::BLOCK_PRODUCTION_UNAGGREGATED_TIMES); metrics::start_timer(&metrics::BLOCK_PRODUCTION_UNAGGREGATED_TIMES);
for attestation in self.naive_aggregation_pool.read().iter() { for attestation in self.naive_aggregation_pool.read().iter() {
let import = |attestation: &Attestation<T::EthSpec>| { let import = |attestation: &Attestation<T::EthSpec>| {
let attesting_indices = get_attesting_indices_from_state(&state, &attestation)?; let attesting_indices = get_attesting_indices_from_state(&state, attestation)?;
self.op_pool self.op_pool
.insert_attestation(attestation.clone(), attesting_indices) .insert_attestation(attestation.clone(), attesting_indices)
}; };
@@ -3342,8 +3342,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
"err" => ?e, "err" => ?e,
"block_slot" => state.slot(), "block_slot" => state.slot(),
); );
assert!(false); panic!("Attempted to include an invalid attestation");
false // false
} else { } else {
true true
} }

View File

@@ -13,14 +13,13 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
block: BeaconBlockRef<'_, T::EthSpec, Payload>, block: BeaconBlockRef<'_, T::EthSpec, Payload>,
block_root: Hash256, block_root: Hash256,
state: &BeaconState<T::EthSpec>, state: &BeaconState<T::EthSpec>,
reward_cache: &mut RewardCache,
include_attestations: bool, include_attestations: bool,
) -> Result<BlockReward, BeaconChainError> { ) -> Result<BlockReward, BeaconChainError> {
if block.slot() != state.slot() { if block.slot() != state.slot() {
return Err(BeaconChainError::BlockRewardSlotError); return Err(BeaconChainError::BlockRewardSlotError);
} }
// FIXME(sproul): pass this in
let mut reward_cache = RewardCache::default();
reward_cache.update(state)?; reward_cache.update(state)?;
let total_active_balance = state.get_total_active_balance()?; let total_active_balance = state.get_total_active_balance()?;
@@ -41,7 +40,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
AttMaxCover::new( AttMaxCover::new(
att.as_ref(), att.as_ref(),
state, state,
&reward_cache, reward_cache,
total_active_balance, total_active_balance,
&self.spec, &self.spec,
) )

View File

@@ -1278,8 +1278,14 @@ impl<T: BeaconChainTypes> ExecutionPendingBlock<T> {
*/ */
if let Some(ref event_handler) = chain.event_handler { if let Some(ref event_handler) = chain.event_handler {
if event_handler.has_block_reward_subscribers() { if event_handler.has_block_reward_subscribers() {
let block_reward = let mut reward_cache = Default::default();
chain.compute_block_reward(block.message(), block_root, &state, true)?; let block_reward = chain.compute_block_reward(
block.message(),
block_root,
&state,
&mut reward_cache,
true,
)?;
event_handler.register(EventKind::BlockReward(block_reward)); event_handler.register(EventKind::BlockReward(block_reward));
} }
} }

View File

@@ -52,6 +52,7 @@ pub fn get_block_rewards<T: BeaconChainTypes>(
.build_all_caches(&chain.spec) .build_all_caches(&chain.spec)
.map_err(beacon_state_error)?; .map_err(beacon_state_error)?;
let mut reward_cache = Default::default();
let mut block_rewards = Vec::with_capacity(blocks.len()); let mut block_rewards = Vec::with_capacity(blocks.len());
let block_replayer = BlockReplayer::new(state, &chain.spec) let block_replayer = BlockReplayer::new(state, &chain.spec)
@@ -63,6 +64,7 @@ pub fn get_block_rewards<T: BeaconChainTypes>(
block.message(), block.message(),
block.canonical_root(), block.canonical_root(),
state, state,
&mut reward_cache,
query.include_attestations, query.include_attestations,
)?; )?;
block_rewards.push(block_reward); block_rewards.push(block_reward);
@@ -100,6 +102,7 @@ pub fn compute_block_rewards<T: BeaconChainTypes>(
) -> Result<Vec<BlockReward>, warp::Rejection> { ) -> Result<Vec<BlockReward>, warp::Rejection> {
let mut block_rewards = Vec::with_capacity(blocks.len()); let mut block_rewards = Vec::with_capacity(blocks.len());
let mut state_cache = LruCache::new(STATE_CACHE_SIZE); let mut state_cache = LruCache::new(STATE_CACHE_SIZE);
let mut reward_cache = Default::default();
for block in blocks { for block in blocks {
let parent_root = block.parent_root(); let parent_root = block.parent_root();
@@ -170,7 +173,13 @@ pub fn compute_block_rewards<T: BeaconChainTypes>(
// Compute block reward. // Compute block reward.
let block_reward = chain let block_reward = chain
.compute_block_reward(block.to_ref(), block.canonical_root(), state, true) .compute_block_reward(
block.to_ref(),
block.canonical_root(),
state,
&mut reward_cache,
true,
)
.map_err(beacon_chain_error)?; .map_err(beacon_chain_error)?;
block_rewards.push(block_reward); block_rewards.push(block_reward);
} }

View File

@@ -3,7 +3,7 @@ use itertools::Itertools;
use std::collections::HashMap; use std::collections::HashMap;
use types::{ use types::{
AggregateSignature, Attestation, AttestationData, BeaconState, BitList, Checkpoint, Epoch, AggregateSignature, Attestation, AttestationData, BeaconState, BitList, Checkpoint, Epoch,
EthSpec, Hash256, IndexedAttestation, Slot, EthSpec, Hash256, Slot,
}; };
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
@@ -188,7 +188,7 @@ impl<T: EthSpec> AttestationMap<T> {
} }
/// Iterate all attestations in the map. /// Iterate all attestations in the map.
pub fn iter<'a>(&'a self) -> impl Iterator<Item = AttestationRef<'a, T>> + 'a { pub fn iter(&self) -> impl Iterator<Item = AttestationRef<T>> {
self.checkpoint_map self.checkpoint_map
.iter() .iter()
.flat_map(|(checkpoint_key, attestation_map)| attestation_map.iter(checkpoint_key)) .flat_map(|(checkpoint_key, attestation_map)| attestation_map.iter(checkpoint_key))

View File

@@ -30,7 +30,7 @@ use std::ptr;
use types::{ use types::{
sync_aggregate::Error as SyncAggregateError, typenum::Unsigned, Attestation, AttestationData, sync_aggregate::Error as SyncAggregateError, typenum::Unsigned, Attestation, AttestationData,
AttesterSlashing, BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, Fork, ForkVersion, AttesterSlashing, BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, Fork, ForkVersion,
Hash256, ProposerSlashing, SignedVoluntaryExit, Slot, SyncAggregate, SyncCommitteeContribution, ProposerSlashing, SignedVoluntaryExit, Slot, SyncAggregate, SyncCommitteeContribution,
Validator, Validator,
}; };
@@ -206,6 +206,7 @@ impl<T: EthSpec> OperationPool<T> {
} }
/// Return all valid attestations for the given epoch, for use in max cover. /// Return all valid attestations for the given epoch, for use in max cover.
#[allow(clippy::too_many_arguments)]
fn get_valid_attestations_for_epoch<'a>( fn get_valid_attestations_for_epoch<'a>(
&'a self, &'a self,
checkpoint_key: &'a CheckpointKey, checkpoint_key: &'a CheckpointKey,

View File

@@ -28,5 +28,5 @@ pub fn get_attesting_indices_from_state<T: EthSpec>(
att: &Attestation<T>, att: &Attestation<T>,
) -> Result<Vec<u64>, BeaconStateError> { ) -> Result<Vec<u64>, BeaconStateError> {
let committee = state.get_beacon_committee(att.data.slot, att.data.index)?; let committee = state.get_beacon_committee(att.data.slot, att.data.index)?;
get_attesting_indices::<T>(&committee.committee, &att.aggregation_bits) get_attesting_indices::<T>(committee.committee, &att.aggregation_bits)
} }