Add early attester cache (#2872)

## Issue Addressed

NA

## Proposed Changes

Introduces a cache to attestation to produce atop blocks which will become the head, but are not fully imported (e.g., not inserted into the database).

Whilst attesting to a block before it's imported is rather easy, if we're going to produce that attestation then we also need to be able to:

1. Verify that attestation.
1. Respond to RPC requests for the `beacon_block_root`.

Attestation verification (1) is *partially* covered. Since we prime the shuffling cache before we insert the block into the early attester cache, we should be fine for all typical use-cases. However, it is possible that the cache is washed out before we've managed to insert the state into the database and then attestation verification will fail with a "missing beacon state"-type error.

Providing the block via RPC (2) is also partially covered, since we'll check the database *and* the early attester cache when responding a blocks-by-root request. However, we'll still omit the block from blocks-by-range requests (until the block lands in the DB). I *think* this is fine, since there's no guarantee that we return all blocks for those responses.

Another important consideration is whether or not the *parent* of the early attester block is available in the databse. If it were not, we might fail to respond to blocks-by-root request that are iterating backwards to collect a chain of blocks. I argue that *we will always have the parent of the early attester block in the database.* This is because we are holding the fork-choice write-lock when inserting the block into the early attester cache and we do not drop that until the block is in the database.
This commit is contained in:
Paul Hauner
2022-01-11 01:35:55 +00:00
parent 65b1374b58
commit 02e2fd2fb8
9 changed files with 304 additions and 10 deletions

View File

@@ -75,7 +75,7 @@ impl From<BeaconChainError> for Error {
/// Stores the minimal amount of data required to compute the committee length for any committee at any
/// slot in a given `epoch`.
struct CommitteeLengths {
pub struct CommitteeLengths {
/// The `epoch` to which the lengths pertain.
epoch: Epoch,
/// The length of the shuffling in `self.epoch`.
@@ -84,7 +84,7 @@ struct CommitteeLengths {
impl CommitteeLengths {
/// Instantiate `Self` using `state.current_epoch()`.
fn new<T: EthSpec>(state: &BeaconState<T>, spec: &ChainSpec) -> Result<Self, Error> {
pub fn new<T: EthSpec>(state: &BeaconState<T>, spec: &ChainSpec) -> Result<Self, Error> {
let active_validator_indices_len = if let Ok(committee_cache) =
state.committee_cache(RelativeEpoch::Current)
{
@@ -101,8 +101,16 @@ impl CommitteeLengths {
})
}
/// Get the count of committees per each slot of `self.epoch`.
pub fn get_committee_count_per_slot<T: EthSpec>(
&self,
spec: &ChainSpec,
) -> Result<usize, Error> {
T::get_committee_count_per_slot(self.active_validator_indices_len, spec).map_err(Into::into)
}
/// Get the length of the committee at the given `slot` and `committee_index`.
fn get<T: EthSpec>(
pub fn get_committee_length<T: EthSpec>(
&self,
slot: Slot,
committee_index: CommitteeIndex,
@@ -120,8 +128,7 @@ impl CommitteeLengths {
}
let slots_per_epoch = slots_per_epoch as usize;
let committees_per_slot =
T::get_committee_count_per_slot(self.active_validator_indices_len, spec)?;
let committees_per_slot = self.get_committee_count_per_slot::<T>(spec)?;
let index_in_epoch = compute_committee_index_in_epoch(
slot,
slots_per_epoch,
@@ -172,7 +179,7 @@ impl AttesterCacheValue {
spec: &ChainSpec,
) -> Result<(JustifiedCheckpoint, CommitteeLength), Error> {
self.committee_lengths
.get::<T>(slot, committee_index, spec)
.get_committee_length::<T>(slot, committee_index, spec)
.map(|committee_length| (self.current_justified_checkpoint, committee_length))
}
}