Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EIP-7251 MaxEB PoC #9

Draft
wants to merge 5 commits into
base: unstable
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion account_manager/src/validator/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub fn cli_run<T: EthSpec>(
};

let deposit_gwei = clap_utils::parse_optional(matches, DEPOSIT_GWEI_FLAG)?
.unwrap_or(spec.max_effective_balance);
.unwrap_or(spec.min_activation_balance);
let count: Option<usize> = clap_utils::parse_optional(matches, COUNT_FLAG)?;
let at_most: Option<usize> = clap_utils::parse_optional(matches, AT_MOST_FLAG)?;

Expand Down
17 changes: 12 additions & 5 deletions beacon_node/beacon_chain/src/attestation_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use store::consts::altair::{
};
use types::consts::altair::WEIGHT_DENOMINATOR;

use types::{BeaconState, Epoch, EthSpec};
use types::{BeaconState, Epoch, EthSpec, ForkName};

use eth2::types::ValidatorId;
use state_processing::common::base::get_base_reward_from_effective_balance;
Expand Down Expand Up @@ -166,7 +166,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let base_reward_per_increment =
BaseRewardPerIncrement::new(total_active_balance, spec)?;

for effective_balance_eth in 1..=self.max_effective_balance_increment_steps()? {
for effective_balance_eth in
1..=self.max_effective_balance_increment_steps(state.fork_name_unchecked())?
{
let effective_balance =
effective_balance_eth.safe_mul(spec.effective_balance_increment)?;
let base_reward =
Expand Down Expand Up @@ -293,10 +295,13 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
})
}

fn max_effective_balance_increment_steps(&self) -> Result<u64, BeaconChainError> {
fn max_effective_balance_increment_steps(
&self,
fork: ForkName,
) -> Result<u64, BeaconChainError> {
let spec = &self.spec;
let max_steps = spec
.max_effective_balance
.max_effective_balance(fork)
.safe_div(spec.effective_balance_increment)?;
Ok(max_steps)
}
Expand Down Expand Up @@ -340,7 +345,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {

let mut ideal_attestation_rewards_list = Vec::new();

for effective_balance_step in 1..=self.max_effective_balance_increment_steps()? {
for effective_balance_step in
1..=self.max_effective_balance_increment_steps(state.fork_name_unchecked())?
{
let effective_balance =
effective_balance_step.safe_mul(spec.effective_balance_increment)?;
let base_reward = get_base_reward_from_effective_balance::<T::EthSpec>(
Expand Down
35 changes: 34 additions & 1 deletion beacon_node/beacon_chain/src/attestation_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,19 @@ impl<'a, T: BeaconChainTypes> IndexedAggregatedAttestation<'a, T> {
Err(e) => return Err(SignatureNotChecked(&signed_aggregate.message.aggregate, e)),
};

let relevant_epoch = signed_aggregate.message.epoch().saturating_sub(1u64);
let aggregator = signed_aggregate.message.aggregator_index as usize;

chain
.effective_balances_cache
.load_epoch(chain, relevant_epoch)
.map_err(|e| {
SignatureNotChecked(
&signed_aggregate.message.aggregate,
Error::BeaconChainError(e),
)
})?;

let indexed_attestation =
match map_attestation_committee(chain, attestation, |(committee, _)| {
// Note: this clones the signature which is known to be a relatively slow operation.
Expand All @@ -547,8 +560,28 @@ impl<'a, T: BeaconChainTypes> IndexedAggregatedAttestation<'a, T> {
let selection_proof =
SelectionProof::from(signed_aggregate.message.selection_proof.clone());

let factor = if chain
.spec
.fork_name_at_slot::<T::EthSpec>(signed_aggregate.message.aggregate.data.slot)
>= ForkName::Deneb
{
let total_committee_balance = chain
.effective_balances_cache
.get_committee_balance::<T::EthSpec>(&committee)
.map_err(|e| Error::BeaconChainError(e))?;
let aggregator_balance = chain
.effective_balances_cache
.get_effective_balance(relevant_epoch, aggregator)
.map_err(|e| Error::BeaconChainError(e))?;

// TODO: is it just standard integer division?
(total_committee_balance / aggregator_balance) as usize
} else {
committee.committee.len()
};

if !selection_proof
.is_aggregator(committee.committee.len(), &chain.spec)
.is_aggregator(factor, &chain.spec)
.map_err(|e| Error::BeaconChainError(e.into()))?
{
return Err(Error::InvalidSelectionProof { aggregator_index });
Expand Down
3 changes: 3 additions & 0 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::data_availability_checker::{
Availability, AvailabilityCheckError, AvailableBlock, DataAvailabilityChecker,
};
use crate::early_attester_cache::EarlyAttesterCache;
use crate::effective_balances_cache::EffectiveBalancesCache;
use crate::errors::{BeaconChainError as Error, BlockProductionError};
use crate::eth1_chain::{Eth1Chain, Eth1ChainBackend};
use crate::eth1_finalization_cache::{Eth1FinalizationCache, Eth1FinalizationData};
Expand Down Expand Up @@ -426,6 +427,8 @@ pub struct BeaconChain<T: BeaconChainTypes> {
pub latest_seen_optimistic_update: Mutex<Option<LightClientOptimisticUpdate<T::EthSpec>>>,
/// Provides information from the Ethereum 1 (PoW) chain.
pub eth1_chain: Option<Eth1Chain<T::Eth1Chain, T::EthSpec>>,
/// Caches effective balances for attestation aggregator calculation
pub effective_balances_cache: EffectiveBalancesCache,
/// Interfaces with the execution client.
pub execution_layer: Option<ExecutionLayer<T::EthSpec>>,
/// Stores information about the canonical head and finalized/justified checkpoints of the
Expand Down
5 changes: 3 additions & 2 deletions beacon_node/beacon_chain/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ where
latest_seen_finality_update: <_>::default(),
latest_seen_optimistic_update: <_>::default(),
eth1_chain: self.eth1_chain,
effective_balances_cache: <_>::default(),
execution_layer: self.execution_layer,
genesis_validators_root,
genesis_time,
Expand Down Expand Up @@ -1225,8 +1226,8 @@ mod test {

for b in state.balances() {
assert_eq!(
*b, spec.max_effective_balance,
"validator balances should be max effective balance"
*b, spec.min_activation_balance,
"validator balances should be min activation balance"
);
}

Expand Down
179 changes: 179 additions & 0 deletions beacon_node/beacon_chain/src/effective_balances_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
use crate::{BeaconChain, BeaconChainError, BeaconChainTypes};
use parking_lot::{RwLock, RwLockUpgradableReadGuard};
use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;
use types::{BeaconCommittee, BeaconState, Epoch, EthSpec, ForkName, Slot};

// Max number of epochs this cache will store data for
pub const EFFECTIVE_BALANCE_CACHE_SIZE: usize = 8;

#[derive(Debug)]
pub enum Error {
StateUnavailable(Epoch),
CacheMiss(Epoch),
ValidatorIndexUnknown(usize),
EpochOutOfBounds(Epoch),
}

#[derive(Clone)]
pub struct EffectiveBalancesCache {
pub effective_balances: Arc<RwLock<BTreeMap<Epoch, Arc<Vec<u64>>>>>,
pub committee_balances: Arc<RwLock<BTreeMap<Epoch, HashMap<(Slot, u64), u64>>>>,
}

impl Default for EffectiveBalancesCache {
fn default() -> Self {
Self {
effective_balances: Arc::new(RwLock::new(BTreeMap::new())),
committee_balances: Arc::new(RwLock::new(BTreeMap::new())),
}
}
}

impl EffectiveBalancesCache {
pub fn new() -> Self {
Self::default()
}

fn load_balances<E: EthSpec>(&self, state: &BeaconState<E>) {
let state_epoch = state.current_epoch();
let read_lock = self.effective_balances.upgradable_read();
if read_lock.contains_key(&state_epoch) {
return;
}

let mut write_lock = RwLockUpgradableReadGuard::upgrade(read_lock);

// remove the oldest epoch if the cache is full
while write_lock.len() >= EFFECTIVE_BALANCE_CACHE_SIZE {
write_lock.pop_first();
}

let validators = state.validators();
let mut balances = Vec::with_capacity(validators.len());
for validator in validators {
balances.push(validator.effective_balance);
}

write_lock.insert(state_epoch, Arc::new(balances));
}

pub fn load_epoch<T: BeaconChainTypes>(
&self,
chain: &BeaconChain<T>,
epoch: Epoch,
) -> Result<(), BeaconChainError> {
if chain.spec.fork_name_at_epoch(epoch.saturating_add(1u64)) <= ForkName::Deneb {
// the cache isn't necessary before electra
return Ok(());
}
// ensure requested epoch is within range
let current_epoch = chain.head().snapshot.beacon_state.current_epoch();
if epoch.saturating_add(EFFECTIVE_BALANCE_CACHE_SIZE as u64) < current_epoch
|| epoch > current_epoch
{
return Err(Error::EpochOutOfBounds(epoch).into());
}

// quick check to see if the epoch is already loaded before loading a state from disk
let read_lock = self.effective_balances.read();
if read_lock.contains_key(&epoch) {
return Ok(());
}
// loading the state from disk is slow so we don't want to be holding the lock
drop(read_lock);

if epoch == current_epoch {
self.load_balances(&chain.head().snapshot.beacon_state);
return Ok(());
}

// load state from disk
let state_slot = epoch.start_slot(T::EthSpec::slots_per_epoch());
let state_root = *chain
.head()
.snapshot
.beacon_state
.get_state_root(state_slot)?;

let state = chain
.get_state(&state_root, Some(state_slot))?
.ok_or(Error::StateUnavailable(epoch))?;

self.load_balances(&state);

Ok(())
}

// will return the committee balance if the epoch is already loaded
pub fn get_committee_balance<E: EthSpec>(
&self,
beacon_committee: &BeaconCommittee,
) -> Result<u64, BeaconChainError> {
// We need the previous epoch to calculate the committee balance
let balances_epoch = beacon_committee
.slot
.epoch(E::slots_per_epoch())
.saturating_sub(1u64);

// see if the value is cached in the committee_balances
let read_lock = self.committee_balances.read();
if let Some(committee_balance) = read_lock
.get(&balances_epoch)
.and_then(|map| map.get(&(beacon_committee.slot, beacon_committee.index)))
.cloned()
{
return Ok(committee_balance);
}
drop(read_lock);

// grab the epoch balances to calculate the committee balance
let read_lock = self.effective_balances.read();
let effective_balances = read_lock
.get(&balances_epoch)
.ok_or(Error::CacheMiss(balances_epoch))?
// this clone is cheap because of the Arc
.clone();
drop(read_lock);

let mut committee_balance = 0;
for index in beacon_committee.committee {
let balance = effective_balances
.get(*index)
.ok_or(Error::ValidatorIndexUnknown(*index))?;
committee_balance += balance;
}

// cache the committee balance
let mut write_lock = self.committee_balances.write();

write_lock
.entry(balances_epoch)
.or_insert_with(HashMap::new)
.insert(
(beacon_committee.slot, beacon_committee.index),
committee_balance,
);

// remove the oldest epoch if the cache is full
while write_lock.len() > EFFECTIVE_BALANCE_CACHE_SIZE {
write_lock.pop_first();
}

Ok(committee_balance)
}

pub fn get_effective_balance(
&self,
epoch: Epoch,
validator_index: usize,
) -> Result<u64, BeaconChainError> {
self.effective_balances
.read()
.get(&epoch)
.ok_or(Error::CacheMiss(epoch))?
.get(validator_index)
.cloned()
.ok_or(Error::ValidatorIndexUnknown(validator_index).into())
}
}
3 changes: 3 additions & 0 deletions beacon_node/beacon_chain/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::beacon_block_streamer::Error as BlockStreamerError;
use crate::beacon_chain::ForkChoiceError;
use crate::beacon_fork_choice_store::Error as ForkChoiceStoreError;
use crate::data_availability_checker::AvailabilityCheckError;
use crate::effective_balances_cache::Error as EffectiveBalancesCacheError;
use crate::eth1_chain::Error as Eth1ChainError;
use crate::historical_blocks::HistoricalBlockError;
use crate::migrate::PruningError;
Expand Down Expand Up @@ -223,6 +224,7 @@ pub enum BeaconChainError {
AvailabilityCheckError(AvailabilityCheckError),
LightClientError(LightClientError),
UnsupportedFork,
EffectiveBalancesCacheError(EffectiveBalancesCacheError),
}

easy_from_to!(SlotProcessingError, BeaconChainError);
Expand Down Expand Up @@ -250,6 +252,7 @@ easy_from_to!(StateAdvanceError, BeaconChainError);
easy_from_to!(BlockReplayError, BeaconChainError);
easy_from_to!(InconsistentFork, BeaconChainError);
easy_from_to!(AvailabilityCheckError, BeaconChainError);
easy_from_to!(EffectiveBalancesCacheError, BeaconChainError);

#[derive(Debug)]
pub enum BlockProductionError {
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/eth1_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ mod test {
let mut deposit = DepositData {
pubkey: keypair.pk.into(),
withdrawal_credentials: Hash256::zero(),
amount: spec.max_effective_balance,
amount: spec.min_activation_balance,
signature: Signature::empty().into(),
};

Expand Down
2 changes: 2 additions & 0 deletions beacon_node/beacon_chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod chain_config;
pub mod data_availability_checker;
pub mod deneb_readiness;
mod early_attester_cache;
pub mod effective_balances_cache;
mod errors;
pub mod eth1_chain;
mod eth1_finalization_cache;
Expand Down Expand Up @@ -79,6 +80,7 @@ pub use block_verification::{
pub use block_verification_types::AvailabilityPendingExecutedBlock;
pub use block_verification_types::ExecutedBlock;
pub use canonical_head::{CachedHead, CanonicalHead, CanonicalHeadRwLock};
pub use effective_balances_cache::EffectiveBalancesCache;
pub use eth1_chain::{Eth1Chain, Eth1ChainBackend};
pub use events::ServerSentEventHandler;
pub use execution_layer::EngineState;
Expand Down
Loading