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);
for attestation in self.naive_aggregation_pool.read().iter() {
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
.insert_attestation(attestation.clone(), attesting_indices)
};
@@ -3342,8 +3342,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
"err" => ?e,
"block_slot" => state.slot(),
);
assert!(false);
false
panic!("Attempted to include an invalid attestation");
// false
} else {
true
}

View File

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

View File

@@ -1278,8 +1278,14 @@ impl<T: BeaconChainTypes> ExecutionPendingBlock<T> {
*/
if let Some(ref event_handler) = chain.event_handler {
if event_handler.has_block_reward_subscribers() {
let block_reward =
chain.compute_block_reward(block.message(), block_root, &state, true)?;
let mut reward_cache = Default::default();
let block_reward = chain.compute_block_reward(
block.message(),
block_root,
&state,
&mut reward_cache,
true,
)?;
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)
.map_err(beacon_state_error)?;
let mut reward_cache = Default::default();
let mut block_rewards = Vec::with_capacity(blocks.len());
let block_replayer = BlockReplayer::new(state, &chain.spec)
@@ -63,6 +64,7 @@ pub fn get_block_rewards<T: BeaconChainTypes>(
block.message(),
block.canonical_root(),
state,
&mut reward_cache,
query.include_attestations,
)?;
block_rewards.push(block_reward);
@@ -100,6 +102,7 @@ pub fn compute_block_rewards<T: BeaconChainTypes>(
) -> Result<Vec<BlockReward>, warp::Rejection> {
let mut block_rewards = Vec::with_capacity(blocks.len());
let mut state_cache = LruCache::new(STATE_CACHE_SIZE);
let mut reward_cache = Default::default();
for block in blocks {
let parent_root = block.parent_root();
@@ -170,7 +173,13 @@ pub fn compute_block_rewards<T: BeaconChainTypes>(
// Compute block reward.
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)?;
block_rewards.push(block_reward);
}

View File

@@ -3,7 +3,7 @@ use itertools::Itertools;
use std::collections::HashMap;
use types::{
AggregateSignature, Attestation, AttestationData, BeaconState, BitList, Checkpoint, Epoch,
EthSpec, Hash256, IndexedAttestation, Slot,
EthSpec, Hash256, Slot,
};
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
@@ -188,7 +188,7 @@ impl<T: EthSpec> AttestationMap<T> {
}
/// 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
.iter()
.flat_map(|(checkpoint_key, attestation_map)| attestation_map.iter(checkpoint_key))

View File

@@ -30,7 +30,7 @@ use std::ptr;
use types::{
sync_aggregate::Error as SyncAggregateError, typenum::Unsigned, Attestation, AttestationData,
AttesterSlashing, BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, Fork, ForkVersion,
Hash256, ProposerSlashing, SignedVoluntaryExit, Slot, SyncAggregate, SyncCommitteeContribution,
ProposerSlashing, SignedVoluntaryExit, Slot, SyncAggregate, SyncCommitteeContribution,
Validator,
};
@@ -206,6 +206,7 @@ impl<T: EthSpec> OperationPool<T> {
}
/// 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>(
&'a self,
checkpoint_key: &'a CheckpointKey,

View File

@@ -28,5 +28,5 @@ pub fn get_attesting_indices_from_state<T: EthSpec>(
att: &Attestation<T>,
) -> Result<Vec<u64>, BeaconStateError> {
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)
}