Implement tree states & hierarchical state DB

This commit is contained in:
Michael Sproul
2023-06-19 10:14:47 +10:00
parent 2bb62b7f7d
commit 23db089a7a
193 changed files with 6093 additions and 5925 deletions

View File

@@ -7,9 +7,7 @@ use beacon_chain::{
use eth2::types::{self as api_types};
use slot_clock::SlotClock;
use state_processing::state_advance::partial_state_advance;
use types::{
AttestationDuty, BeaconState, ChainSpec, CloneConfig, Epoch, EthSpec, Hash256, RelativeEpoch,
};
use types::{AttestationDuty, BeaconState, ChainSpec, Epoch, EthSpec, Hash256, RelativeEpoch};
/// The struct that is returned to the requesting HTTP client.
type ApiDuties = api_types::DutiesResponse<Vec<api_types::AttesterData>>;
@@ -93,8 +91,7 @@ fn compute_historic_attester_duties<T: BeaconChainTypes>(
if head.beacon_state.current_epoch() <= request_epoch {
Some((
head.beacon_state_root(),
head.beacon_state
.clone_with(CloneConfig::committee_caches_only()),
head.beacon_state.clone(),
execution_status.is_optimistic_or_invalid(),
))
} else {

View File

@@ -277,7 +277,7 @@ pub fn get_block_packing_efficiency<T: BeaconChainTypes>(
));
let pre_slot_hook =
|state: &mut BeaconState<T::EthSpec>| -> Result<(), PackingEfficiencyError> {
|_, state: &mut BeaconState<T::EthSpec>| -> Result<(), PackingEfficiencyError> {
// Add attestations to `available_attestations`.
handler.lock().add_attestations(state.slot())?;
Ok(())

View File

@@ -46,7 +46,6 @@ use slog::{crit, debug, error, info, warn, Logger};
use slot_clock::SlotClock;
use ssz::Encode;
pub use state_id::StateId;
use std::borrow::Cow;
use std::future::Future;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
@@ -613,7 +612,7 @@ pub fn serve<T: BeaconChainTypes>(
query.id.as_ref().map_or(true, |ids| {
ids.iter().any(|id| match id {
ValidatorId::PublicKey(pubkey) => {
&validator.pubkey == pubkey
validator.pubkey() == pubkey
}
ValidatorId::Index(param_index) => {
*param_index == *index as u64
@@ -673,7 +672,7 @@ pub fn serve<T: BeaconChainTypes>(
query.id.as_ref().map_or(true, |ids| {
ids.iter().any(|id| match id {
ValidatorId::PublicKey(pubkey) => {
&validator.pubkey == pubkey
validator.pubkey() == pubkey
}
ValidatorId::Index(param_index) => {
*param_index == *index as u64
@@ -731,9 +730,13 @@ pub fn serve<T: BeaconChainTypes>(
"Invalid validator ID".to_string(),
))
}))
.and(log_filter.clone())
.and(warp::path::end())
.and_then(
|state_id: StateId, chain: Arc<BeaconChain<T>>, validator_id: ValidatorId| {
|state_id: StateId,
chain: Arc<BeaconChain<T>>,
validator_id: ValidatorId,
log: Logger| {
blocking_json_task(move || {
let (data, execution_optimistic, finalized) = state_id
.map_state_and_execution_optimistic_and_finalized(
@@ -741,7 +744,23 @@ pub fn serve<T: BeaconChainTypes>(
|state, execution_optimistic, finalized| {
let index_opt = match &validator_id {
ValidatorId::PublicKey(pubkey) => {
state.validators().iter().position(|v| v.pubkey == *pubkey)
// Fast path: use the pubkey cache which is probably
// initialised at the head.
match state.get_validator_index_read_only(pubkey) {
Ok(result) => result,
Err(e) => {
// Slow path, fall back to iteration.
debug!(
log,
"Validator look-up cache miss";
"reason" => ?e,
);
state
.validators()
.iter()
.position(|v| v.pubkey() == pubkey)
}
}
}
ValidatorId::Index(index) => Some(*index as usize),
};
@@ -832,10 +851,10 @@ pub fn serve<T: BeaconChainTypes>(
None
};
let committee_cache = if let Some(ref shuffling) =
let committee_cache = if let Some(shuffling) =
maybe_cached_shuffling
{
Cow::Borrowed(&**shuffling)
shuffling
} else {
let possibly_built_cache =
match RelativeEpoch::from_epoch(current_epoch, epoch) {
@@ -846,14 +865,13 @@ pub fn serve<T: BeaconChainTypes>(
{
state
.committee_cache(relative_epoch)
.map(Cow::Borrowed)
.map(Arc::clone)
}
_ => CommitteeCache::initialized(
state,
epoch,
&chain.spec,
)
.map(Cow::Owned),
),
}
.map_err(|e| {
match e {
@@ -901,7 +919,7 @@ pub fn serve<T: BeaconChainTypes>(
{
cache_write.insert_committee_cache(
shuffling_id,
&*possibly_built_cache,
&possibly_built_cache,
);
}
}

View File

@@ -10,7 +10,7 @@ use safe_arith::SafeArith;
use slog::{debug, Logger};
use slot_clock::SlotClock;
use std::cmp::Ordering;
use types::{CloneConfig, Epoch, EthSpec, Hash256, Slot};
use types::{Epoch, EthSpec, Hash256, Slot};
/// The struct that is returned to the requesting HTTP client.
type ApiDuties = api_types::DutiesResponse<Vec<api_types::ProposerData>>;
@@ -192,8 +192,7 @@ fn compute_historic_proposer_duties<T: BeaconChainTypes>(
if head.beacon_state.current_epoch() <= epoch {
Some((
head.beacon_state_root(),
head.beacon_state
.clone_with(CloneConfig::committee_caches_only()),
head.beacon_state.clone(),
execution_status.is_optimistic_or_invalid(),
))
} else {

View File

@@ -163,10 +163,7 @@ impl StateId {
.head_and_execution_status()
.map_err(warp_utils::reject::beacon_chain_error)?;
return Ok((
cached_head
.snapshot
.beacon_state
.clone_with_only_committee_caches(),
cached_head.snapshot.beacon_state.clone(),
execution_status.is_optimistic_or_invalid(),
false,
));

View File

@@ -99,13 +99,13 @@ pub fn validator_inclusion_data<T: BeaconChainTypes>(
let summary = get_epoch_processing_summary(&mut state, &chain.spec)?;
Ok(Some(ValidatorInclusionData {
is_slashed: validator.slashed,
is_slashed: validator.slashed(),
is_withdrawable_in_current_epoch: validator.is_withdrawable_at(epoch),
is_active_unslashed_in_current_epoch: summary
.is_active_unslashed_in_current_epoch(validator_index),
is_active_unslashed_in_previous_epoch: summary
.is_active_unslashed_in_previous_epoch(validator_index),
current_epoch_effective_balance_gwei: validator.effective_balance,
current_epoch_effective_balance_gwei: validator.effective_balance(),
is_current_epoch_target_attester: summary
.is_current_epoch_target_attester(validator_index)
.map_err(convert_cache_error)?,