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:
Michael Sproul
2022-10-15 22:25:54 +00:00
parent e4cbdc1c77
commit 59ec6b71b8
21 changed files with 388 additions and 100 deletions

View File

@@ -1,9 +1,13 @@
use crate::common::{decrease_balance, increase_balance, initiate_validator_exit};
use crate::{
common::{decrease_balance, increase_balance, initiate_validator_exit},
per_block_processing::errors::BlockProcessingError,
ConsensusContext,
};
use safe_arith::SafeArith;
use std::cmp;
use types::{
consts::altair::{PROPOSER_WEIGHT, WEIGHT_DENOMINATOR},
BeaconStateError as Error, *,
*,
};
/// Slash the validator with index `slashed_index`.
@@ -11,8 +15,9 @@ pub fn slash_validator<T: EthSpec>(
state: &mut BeaconState<T>,
slashed_index: usize,
opt_whistleblower_index: Option<usize>,
ctxt: &mut ConsensusContext<T>,
spec: &ChainSpec,
) -> Result<(), Error> {
) -> Result<(), BlockProcessingError> {
let epoch = state.current_epoch();
initiate_validator_exit(state, slashed_index, spec)?;
@@ -39,7 +44,7 @@ pub fn slash_validator<T: EthSpec>(
)?;
// Apply proposer and whistleblower rewards
let proposer_index = state.get_beacon_proposer_index(state.slot(), spec)?;
let proposer_index = ctxt.get_proposer_index(state, spec)? as usize;
let whistleblower_index = opt_whistleblower_index.unwrap_or(proposer_index);
let whistleblower_reward =
validator_effective_balance.safe_div(spec.whistleblower_reward_quotient)?;
@@ -52,7 +57,7 @@ pub fn slash_validator<T: EthSpec>(
// Ensure the whistleblower index is in the validator registry.
if state.validators().get(whistleblower_index).is_none() {
return Err(BeaconStateError::UnknownValidator(whistleblower_index));
return Err(BeaconStateError::UnknownValidator(whistleblower_index).into());
}
increase_balance(state, proposer_index, proposer_reward)?;