mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2025-01-10 21:11:22 +00:00
66eca1a882
## Proposed Changes This PR has two aims: to speed up attestation packing in the op pool, and to fix bugs in the verification of attester slashings, proposer slashings and voluntary exits. The changes are bundled into a single database schema upgrade (v12). Attestation packing is sped up by removing several inefficiencies: - No more recalculation of `attesting_indices` during packing. - No (unnecessary) examination of the `ParticipationFlags`: a bitfield suffices. See `RewardCache`. - No re-checking of attestation validity during packing: the `AttestationMap` provides attestations which are "correct by construction" (I have checked this using Hydra). - No SSZ re-serialization for the clunky `AttestationId` type (it can be removed in a future release). So far the speed-up seems to be roughly 2-10x, from 500ms down to 50-100ms. Verification of attester slashings, proposer slashings and voluntary exits is fixed by: - Tracking the `ForkVersion`s that were used to verify each message inside the `SigVerifiedOp`. This allows us to quickly re-verify that they match the head state's opinion of what the `ForkVersion` should be at the epoch(s) relevant to the message. - Storing the `SigVerifiedOp` on disk rather than the raw operation. This allows us to continue track the fork versions after a reboot. This is mostly contained in this commit 52bb1840ae5c4356a8fc3a51e5df23ed65ed2c7f. ## Additional Info The schema upgrade uses the justified state to re-verify attestations and compute `attesting_indices` for them. It will drop any attestations that fail to verify, by the logic that attestations are most valuable in the few slots after they're observed, and are probably stale and useless by the time a node restarts. Exits and proposer slashings and similarly re-verified to obtain `SigVerifiedOp`s. This PR contains a runtime killswitch `--paranoid-block-proposal` which opts out of all the optimisations in favour of closely verifying every included message. Although I'm quite sure that the optimisations are correct this flag could be useful in the event of an unforeseen emergency. Finally, you might notice that the `RewardCache` appears quite useless in its current form because it is only updated on the hot-path immediately before proposal. My hope is that in future we can shift calls to `RewardCache::update` into the background, e.g. while performing the state advance. It is also forward-looking to `tree-states` compatibility, where iterating and indexing `state.{previous,current}_epoch_participation` is expensive and needs to be minimised.
123 lines
4.3 KiB
Rust
123 lines
4.3 KiB
Rust
use crate::OpPoolError;
|
|
use bitvec::vec::BitVec;
|
|
use types::{BeaconState, BeaconStateError, Epoch, EthSpec, Hash256, ParticipationFlags};
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
struct Initialization {
|
|
current_epoch: Epoch,
|
|
latest_block_root: Hash256,
|
|
}
|
|
|
|
/// Cache to store pre-computed information for block proposal.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct RewardCache {
|
|
initialization: Option<Initialization>,
|
|
/// `BitVec` of validator indices which don't have default participation flags for the prev. epoch.
|
|
///
|
|
/// We choose to only track whether validators have *any* participation flag set because
|
|
/// it's impossible to include a new attestation which is better than the existing participation
|
|
/// UNLESS the validator makes a slashable attestation, and we assume that this is rare enough
|
|
/// that it's acceptable to be slightly sub-optimal in this case.
|
|
previous_epoch_participation: BitVec,
|
|
/// `BitVec` of validator indices which don't have default participation flags for the current epoch.
|
|
current_epoch_participation: BitVec,
|
|
}
|
|
|
|
impl RewardCache {
|
|
pub fn has_attested_in_epoch(
|
|
&self,
|
|
validator_index: u64,
|
|
epoch: Epoch,
|
|
) -> Result<bool, OpPoolError> {
|
|
if let Some(init) = &self.initialization {
|
|
if init.current_epoch == epoch {
|
|
Ok(*self
|
|
.current_epoch_participation
|
|
.get(validator_index as usize)
|
|
.ok_or(OpPoolError::RewardCacheOutOfBounds)?)
|
|
} else if init.current_epoch == epoch + 1 {
|
|
Ok(*self
|
|
.previous_epoch_participation
|
|
.get(validator_index as usize)
|
|
.ok_or(OpPoolError::RewardCacheOutOfBounds)?)
|
|
} else {
|
|
Err(OpPoolError::RewardCacheWrongEpoch)
|
|
}
|
|
} else {
|
|
Err(OpPoolError::RewardCacheWrongEpoch)
|
|
}
|
|
}
|
|
|
|
/// Return the root of the latest block applied to `state`.
|
|
///
|
|
/// For simplicity at genesis we return the zero hash, which will cause one unnecessary
|
|
/// re-calculation in `update`.
|
|
fn latest_block_root<E: EthSpec>(state: &BeaconState<E>) -> Result<Hash256, OpPoolError> {
|
|
if state.slot() == 0 {
|
|
Ok(Hash256::zero())
|
|
} else {
|
|
Ok(*state
|
|
.get_block_root(state.slot() - 1)
|
|
.map_err(OpPoolError::RewardCacheGetBlockRoot)?)
|
|
}
|
|
}
|
|
|
|
/// Update the cache.
|
|
pub fn update<E: EthSpec>(&mut self, state: &BeaconState<E>) -> Result<(), OpPoolError> {
|
|
if matches!(state, BeaconState::Base(_)) {
|
|
return Ok(());
|
|
}
|
|
|
|
let current_epoch = state.current_epoch();
|
|
let latest_block_root = Self::latest_block_root(state)?;
|
|
|
|
let new_init = Initialization {
|
|
current_epoch,
|
|
latest_block_root,
|
|
};
|
|
|
|
// The participation flags change every block, and will almost always need updating when
|
|
// this function is called at a new slot.
|
|
if self
|
|
.initialization
|
|
.as_ref()
|
|
.map_or(true, |init| *init != new_init)
|
|
{
|
|
self.update_previous_epoch_participation(state)
|
|
.map_err(OpPoolError::RewardCacheUpdatePrevEpoch)?;
|
|
self.update_current_epoch_participation(state)
|
|
.map_err(OpPoolError::RewardCacheUpdateCurrEpoch)?;
|
|
|
|
self.initialization = Some(new_init);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn update_previous_epoch_participation<E: EthSpec>(
|
|
&mut self,
|
|
state: &BeaconState<E>,
|
|
) -> Result<(), BeaconStateError> {
|
|
let default_participation = ParticipationFlags::default();
|
|
self.previous_epoch_participation = state
|
|
.previous_epoch_participation()?
|
|
.iter()
|
|
.map(|participation| *participation != default_participation)
|
|
.collect();
|
|
Ok(())
|
|
}
|
|
|
|
fn update_current_epoch_participation<E: EthSpec>(
|
|
&mut self,
|
|
state: &BeaconState<E>,
|
|
) -> Result<(), BeaconStateError> {
|
|
let default_participation = ParticipationFlags::default();
|
|
self.current_epoch_participation = state
|
|
.current_epoch_participation()?
|
|
.iter()
|
|
.map(|participation| *participation != default_participation)
|
|
.collect();
|
|
Ok(())
|
|
}
|
|
}
|