Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

520 changes: 480 additions & 40 deletions crates/blockchain/state_transition/src/beacon/fork_choice.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -715,15 +715,34 @@ fn check_head_against_reference(store: &Store, config: &Config) -> Result<(), St
}
}
};
if walked == reference {
Ok(())
} else {
Err(format!(
if walked != reference {
return Err(format!(
"the bottom-up walk chose 0x{} ({:?}) but the reference chose 0x{} ({:?})",
hex::encode(walked.root.0),
walked.payload_status,
hex::encode(reference.root.0),
reference.payload_status,
));
}
check_cached_weights(store, config, ForkRules::of(fork))
}

/// Checks that the weights the walk reads from the store's cache equal the
/// tally they replaced ([`fork_choice::compute_node_weights`]), node for node:
/// at every `head` check of every fixture, the cache has followed each vote,
/// block and equivocation the fixture fed the store.
fn check_cached_weights(store: &Store, config: &Config, rules: ForkRules) -> Result<(), String> {
let index = store.block_index();
let committees = store.committee_cache();
let cached = fork_choice::cached_node_weights(store, &index, config, &committees, rules)
.map_err(|err| format!("cached_node_weights: {err:?}"))?;
let tallied = fork_choice::compute_node_weights(store, &index, config, &committees, rules)
.map_err(|err| format!("compute_node_weights: {err:?}"))?;
if cached == tallied {
Ok(())
} else {
Err(format!(
"the cached weights {cached:?} differ from the tally {tallied:?}"
))
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ libssz-types.workspace = true
lru.workspace = true
xdelta3.workspace = true

# The cached fork-choice weights look a block root up once per vote batch, and
# once per latest message on a rebuild; std's SipHash was one of the largest
# costs of the head computation's tally. Already in the lockfile through other
# crates, so this adds no new version.
foldhash = "0.2.0"

[dev-dependencies]
tempfile = "3"
proptest = "1"
Expand Down
2 changes: 2 additions & 0 deletions crates/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod error;
mod liveness;
mod metrics;
pub mod pools;
mod proto_array;
mod state_codec;
mod state_diff;
mod state_writer;
Expand All @@ -21,6 +22,7 @@ pub use committee_cache::{CommitteeCache, Lookup, ShufflingKey};
/// callers can match on it (e.g. to distinguish [`Error::DbVersionMismatch`]).
pub use error::Error;
pub use liveness::ObservedLiveness;
pub use proto_array::{ProtoArrayError, WeightRules};
// `CacheKey` lives in `state_writer` (beside the `StateCache` it keys), not
// `store`; re-exported here so the public path (`ethlambda_storage::CacheKey`)
// is unaffected by which module owns it.
Expand Down
32 changes: 32 additions & 0 deletions crates/storage/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,35 @@ pub(crate) fn inc_state_cache_lookups(method: StateCacheMethod, key: &CacheKey,
.with_label_values(&[method.label(), kind, result])
.inc();
}

static LEAN_BEACON_PROTO_ARRAY_REBUILDS_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
register_int_counter_vec!(
"lean_beacon_proto_array_rebuilds_total",
"Full rebuilds of the cached beacon fork-choice weights, by what made the cache stale",
&["reason"]
)
.unwrap()
});

static LEAN_BEACON_PROTO_ARRAY_REBUILD_SECONDS: LazyLock<Histogram> = LazyLock::new(|| {
register_histogram!(
"lean_beacon_proto_array_rebuild_seconds",
"Time to rebuild the cached beacon fork-choice weights from every latest message",
vec![
0.0005, 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5
]
)
.unwrap()
});

/// Count one rebuild of the cached fork-choice weights, by `reason`.
pub(crate) fn inc_proto_array_rebuilds(reason: &str) {
LEAN_BEACON_PROTO_ARRAY_REBUILDS_TOTAL
.with_label_values(&[reason])
.inc();
}

/// Time one rebuild of the cached fork-choice weights; the guard records on drop.
pub(crate) fn time_proto_array_rebuild() -> TimingGuard {
TimingGuard::new(&LEAN_BEACON_PROTO_ARRAY_REBUILD_SECONDS)
}
Loading
Loading