mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 04:01:51 +00:00
* Remove use of ethers_core::RlpStream
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core
* Remove old code
* Simplify keccak call
* Remove unused package
* Merge branch 'unstable' of https://github.com/ethDreamer/lighthouse into remove_use_of_ethers_core
* Merge branch 'unstable' into remove_use_of_ethers_core
* Run clippy
* Merge branch 'remove_use_of_ethers_core' of https://github.com/dospore/lighthouse into remove_use_of_ethers_core
* Check all cargo fmt
* migrate to alloy primitives init
* fix deps
* integrate alloy-primitives
* resolve dep issues
* more changes based on dep changes
* add TODOs
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core
* Revert lock
* Add BeaconBlocksByRange v3
* continue migration
* Revert "Add BeaconBlocksByRange v3"
This reverts commit e3ce7fc5ea.
* impl hash256 extended trait
* revert some uneeded diffs
* merge conflict resolved
* fix subnet id rshift calc
* rename to FixedBytesExtended
* debugging
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives
* fix failed test
* fixing more tests
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core
* introduce a shim to convert between the two u256 types
* move alloy to wrokspace
* align alloy versions
* update
* update web3signer test certs
* refactor
* resolve failing tests
* linting
* fix graffiti string test
* fmt
* fix ef test
* resolve merge conflicts
* remove udep and revert cert
* cargo patch
* cyclic dep
* fix build error
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives
* resolve conflicts, update deps
* merge unstable
* fmt
* fix deps
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives
* resolve merge conflicts
* resolve conflicts, make necessary changes
* Remove patch
* fmt
* remove file
* merge conflicts
* sneaking in a smol change
* bump versions
* Merge remote-tracking branch 'origin/unstable' into migrate-to-alloy-primitives
* Updates for peerDAS
* Update ethereum_hashing to prevent dupe
* updated alloy-consensus, removed TODOs
* cargo update
* endianess fix
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives
* fmt
* fix merge
* fix test
* fixed_bytes crate
* minor fixes
* convert u256 to i64
* panic free mixin to_low_u64_le
* from_str_radix
* computbe_subnet api and ensuring we use big-endian
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives
* fix test
* Simplify subnet_id test
* Simplify some more tests
* Add tests to fixed_bytes crate
* Merge branch 'unstable' into migrate-to-alloy-primitives
125 lines
4.4 KiB
Rust
125 lines
4.4 KiB
Rust
use crate::OpPoolError;
|
|
use bitvec::vec::BitVec;
|
|
use types::{
|
|
BeaconState, BeaconStateError, Epoch, EthSpec, FixedBytesExtended, 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(())
|
|
}
|
|
}
|