Skip to content
Open
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
3 changes: 3 additions & 0 deletions fork_choice_control/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,9 @@ fn initialize_preprocessed_state_cache<P: Preset>(
accessors::get_or_init_total_active_balance(state, false);
accessors::get_or_init_validator_indices(state, false);

if let Some(post_gloas_state) = state.post_gloas() {
accessors::get_or_init_builder_indices(post_gloas_state, false);
}
Ok(())
}

Expand Down
42 changes: 40 additions & 2 deletions helper_functions/src/accessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,44 @@ pub fn get_or_init_validator_indices<P: Preset>(
})
}

pub fn builder_public_key<P: Preset>(
state: &(impl PostGloasBeaconState<P> + ?Sized),
builder_index: BuilderIndex,
) -> Result<&PublicKeyBytes> {
Ok(&state.builders().get(builder_index)?.pubkey)
}

#[must_use]
pub fn builder_index_of_public_key<P: Preset>(
state: &(impl PostGloasBeaconState<P> + ?Sized),
public_key: &PublicKeyBytes,
) -> Option<BuilderIndex> {
get_or_init_builder_indices(state, true)
.get(public_key)
.copied()
}

pub fn get_or_init_builder_indices<P: Preset>(
state: &(impl PostGloasBeaconState<P> + ?Sized),
report_cache_miss: bool,
) -> &HashMap<PublicKeyBytes, BuilderIndex> {
state.cache().builder_indices.get_or_init(|| {
if report_cache_miss {
#[cfg(feature = "metrics")]
if let Some(metrics) = METRICS.get() {
metrics.builder_indices_init_count.inc();
}
}

state
.builders()
.into_iter()
.map(|builder| builder.pubkey)
.zip(0..)
.collect()
})
}

pub fn get_active_validator_indices<P: Preset>(
state: &impl BeaconState<P>,
relative_epoch: RelativeEpoch,
Expand Down Expand Up @@ -1176,8 +1214,8 @@ pub fn get_builder_payment_quorum_threshold<P: Preset>(state: &impl BeaconState<
}

pub fn get_active_builder_indices<P: Preset>(
state: &(impl PostGloasBeaconState<P> + ?Sized),
) -> impl Iterator<Item = ValidatorIndex> + '_ {
state: &dyn PostGloasBeaconState<P>,
) -> impl Iterator<Item = BuilderIndex> + '_ {
(0..)
.zip(state.builders())
.filter(move |(_, builder)| {
Expand Down
14 changes: 7 additions & 7 deletions http_api/src/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3672,13 +3672,13 @@ pub async fn validator_execution_payload_bid<P: Preset, W: Wait>(
return Err(Error::InvalidSlot(slot));
};

// TODO(gloas): check builder exist with `builder_indices` cache in beacon state
let _ = beacon_state
.post_gloas()
.ok_or(Error::StatePreGloas)?
.builders()
.get(builder_index)
.map_err(|_| Error::InvalidBuilderIndex(builder_index))?;
let Some(gloas_state) = beacon_state.post_gloas() else {
return Err(Error::StatePreGloas);
};

if !accessors::get_active_builder_indices(gloas_state).contains(&builder_index) {
return Err(Error::InvalidBuilderIndex(builder_index));
}

let version = beacon_state.phase();
let signed_bid = controller
Expand Down
19 changes: 19 additions & 0 deletions prometheus_metrics/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ pub struct Metrics {
pub beacon_proposer_index_init_count: IntCounter,
pub total_active_balance_init_count: IntCounter,
pub validator_indices_init_count: IntCounter,
pub builder_indices_init_count: IntCounter,

// Transition function metrics
pub blinded_block_transition_times: Histogram,
Expand All @@ -197,6 +198,7 @@ pub struct Metrics {
beacon_current_justified_epoch: IntGauge,
beacon_previous_justified_epoch: IntGauge,
beacon_current_active_validators: IntGauge,
beacon_current_active_builders: IntGauge,

pub beacon_reorgs_total: IntCounter,

Expand Down Expand Up @@ -847,6 +849,11 @@ impl Metrics {
"Validator indices cache init count",
)?,

builder_indices_init_count: IntCounter::new(
"BUILDER_INDICES_INIT_COUNT",
"Builder indices init count",
)?,

// Transition function metrics
blinded_block_transition_times: Histogram::with_opts(histogram_opts!(
"BLINDED_BLOCK_TRANSITION_TIMES",
Expand Down Expand Up @@ -891,6 +898,11 @@ impl Metrics {
"Current total active validators",
)?,

beacon_current_active_builders: IntGauge::new(
"beacon_current_active_builders",
"Current total active builders",
)?,

beacon_reorgs_total: IntCounter::new(
"beacon_reorgs_total",
"Total number of chain reorganizations",
Expand Down Expand Up @@ -1177,6 +1189,7 @@ impl Metrics {
default_registry.register(Box::new(self.beacon_proposer_index_init_count.clone()))?;
default_registry.register(Box::new(self.total_active_balance_init_count.clone()))?;
default_registry.register(Box::new(self.validator_indices_init_count.clone()))?;
default_registry.register(Box::new(self.builder_indices_init_count.clone()))?;
default_registry.register(Box::new(self.blinded_block_transition_times.clone()))?;
default_registry.register(Box::new(self.block_transition_times.clone()))?;
default_registry.register(Box::new(self.epoch_processing_times.clone()))?;
Expand All @@ -1186,6 +1199,7 @@ impl Metrics {
default_registry.register(Box::new(self.beacon_current_justified_epoch.clone()))?;
default_registry.register(Box::new(self.beacon_previous_justified_epoch.clone()))?;
default_registry.register(Box::new(self.beacon_current_active_validators.clone()))?;
default_registry.register(Box::new(self.beacon_current_active_builders.clone()))?;
default_registry.register(Box::new(self.beacon_reorgs_total.clone()))?;
default_registry.register(Box::new(self.beacon_processed_deposits_total.clone()))?;
default_registry.register(Box::new(
Expand Down Expand Up @@ -1514,6 +1528,11 @@ impl Metrics {
.set(validator_count as i64);
}

pub fn set_beacon_current_active_builders(&self, builder_count: usize) {
self.beacon_current_active_builders
.set(builder_count as i64);
}

pub fn set_beacon_processed_deposits_total(&self, total_deposits: u64) {
self.beacon_processed_deposits_total
.set(total_deposits as i64);
Expand Down
4 changes: 3 additions & 1 deletion types/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use once_cell::sync::OnceCell;
use std::collections::HashMap;

use crate::{
altair::primitives::NonZeroGwei, nonstandard::RelativeEpoch, phase0::primitives::ValidatorIndex,
altair::primitives::NonZeroGwei, gloas::primitives::BuilderIndex, nonstandard::RelativeEpoch,
phase0::primitives::ValidatorIndex,
};

// Possible optimization: cache all proposer indices in an epoch.
Expand All @@ -34,6 +35,7 @@ pub struct Cache {
pub active_validator_indices_shuffled: EnumMap<RelativeEpoch, OnceCell<PackedIndices>>,
pub total_active_balance: EnumMap<RelativeEpoch, OnceCell<NonZeroGwei>>,
pub validator_indices: OnceCell<HashMap<PublicKeyBytes, ValidatorIndex>>,
pub builder_indices: OnceCell<HashMap<PublicKeyBytes, BuilderIndex>>,
}

impl Cache {
Expand Down
Loading