Skip to content

Commit

Permalink
feat(bolt-sidecar): AccountStateCache with metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
thedevbirb committed Nov 27, 2024
1 parent 6a2cdc9 commit 7ecafa1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
36 changes: 36 additions & 0 deletions bolt-sidecar/src/state/account_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::ops::{Deref, DerefMut};

use alloy::primitives::Address;

use crate::{common::score_cache::ScoreCache, primitives::AccountState, telemetry::ApiMetrics};

const GET_SCORE: isize = 4;
const INSERT_SCORE: isize = 4;
const UPDATE_SCORE: isize = -1;

/// A scored cache for account states.
#[derive(Debug, Default)]
pub struct AccountStateCache(
pub ScoreCache<GET_SCORE, INSERT_SCORE, UPDATE_SCORE, Address, AccountState>,
);

impl Deref for AccountStateCache {
type Target = ScoreCache<GET_SCORE, INSERT_SCORE, UPDATE_SCORE, Address, AccountState>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for AccountStateCache {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl AccountStateCache {
/// Insert an account state into the cache, and update the metrics.
pub fn insert(&mut self, address: Address, account_state: AccountState) {
ApiMetrics::set_account_states(self.len());
self.0.insert(address, account_state);
}
}
11 changes: 3 additions & 8 deletions bolt-sidecar/src/state/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
telemetry::ApiMetrics,
};

use super::fetcher::StateFetcher;
use super::{account_state::AccountStateCache, fetcher::StateFetcher};

/// Possible commitment validation errors.
///
Expand Down Expand Up @@ -126,11 +126,6 @@ impl ValidationError {
}
}

const GET_SCORE: isize = 4;
const INSERT_SCORE: isize = 4;
const UPDATE_SCORE: isize = -1;
type AccountStatesCache = ScoreCache<GET_SCORE, INSERT_SCORE, UPDATE_SCORE, Address, AccountState>;

/// The minimal state of the execution layer at some block number (`head`).
/// This is the state that is needed to simulate commitments.
/// It contains per-address nonces and balances, as well as the minimum basefee.
Expand Down Expand Up @@ -161,7 +156,7 @@ pub struct ExecutionState<C> {
/// When a commitment request is made from an account its score is bumped of
/// [ACCOUNT_STATE_SCORE_BUMP], and when it updated it is decreased by
/// [ACCOUNT_STATE_UPDATE_PENALTY].
account_states: AccountStatesCache,
account_states: AccountStateCache,
/// The block templates by target SLOT NUMBER.
/// We have multiple block templates because in rare cases we might have multiple
/// proposal duties for a single lookahead.
Expand Down Expand Up @@ -226,7 +221,7 @@ impl<C: StateFetcher> ExecutionState<C> {
limits,
client,
slot: 0,
account_states: AccountStatesCache::with_max_len(num_accounts),
account_states: AccountStateCache(ScoreCache::with_max_len(num_accounts)),
block_templates: HashMap::new(),
// Load the default KZG settings
kzg_settings: EnvKzgSettings::default(),
Expand Down
4 changes: 4 additions & 0 deletions bolt-sidecar/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub use consensus::ConsensusState;
pub mod head_tracker;
pub use head_tracker::HeadTracker;

/// Module that defines the account state cache.
pub mod account_state;
pub use account_state::AccountStateCache;

/// The deadline for a which a commitment is considered valid.
#[derive(Debug)]
pub struct CommitmentDeadline {
Expand Down
7 changes: 7 additions & 0 deletions bolt-sidecar/src/telemetry/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const GROSS_TIP_REVENUE: &str = "bolt_sidecar_gross_tip_revenue";
// Gauges ------------------------------------------------------------------
/// Gauge for the latest slot number
const LATEST_HEAD: &str = "bolt_sidecar_latest_head";
/// Number of account states saved in cache.
const ACCOUNT_STATES: &str = "bolt_sidecar_account_states";

// Histograms --------------------------------------------------------------
/// Histogram for the total duration of HTTP requests in seconds.
Expand All @@ -52,6 +54,7 @@ impl ApiMetrics {

// Gauges
describe_gauge!(LATEST_HEAD, "Latest slot number");
describe_gauge!(ACCOUNT_STATES, "Number of account states saved in cache");

// Histograms
describe_histogram!(
Expand Down Expand Up @@ -119,6 +122,10 @@ impl ApiMetrics {
gauge!(LATEST_HEAD).set(slot);
}

pub fn set_account_states(count: usize) {
gauge!(ACCOUNT_STATES).set(count as f64);
}

/// Mixed ----------------------------------------------------------------
/// Observes the duration of an HTTP request by storing it in a histogram,
Expand Down

0 comments on commit 7ecafa1

Please sign in to comment.