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
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.

7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: help fmt lint docker-build shadow-build shadow-docker-build run-devnet test docs docs-deps docs-serve
.PHONY: help fmt lint bench docker-build shadow-build shadow-docker-build run-devnet test docs docs-deps docs-serve

help: ## πŸ“š Show help for each of the Makefile recipes
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Expand All @@ -14,6 +14,11 @@ test: leanSpec/fixtures ## πŸ§ͺ Run all tests
# signature verification/aggregation, without paying for LTO on every rebuild
cargo test --workspace --profile release-fast

BENCH_ARGS ?= synthetic --mock-crypto

bench: ## 🏁 Benchmark block building offline (override BENCH_ARGS to customize)
cargo run --release --bin ethlambda -- benchmark $(BENCH_ARGS)

GIT_COMMIT=$(shell git rev-parse HEAD)
GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
DOCKER_TAG?=local
Expand Down
1 change: 1 addition & 0 deletions bin/ethlambda/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ shadow-integration = ["ethlambda-crypto/shadow-integration"]
[dependencies]
ethlambda-blockchain.workspace = true
ethlambda-crypto.workspace = true
ethlambda-metrics.workspace = true
ethlambda-network-api.workspace = true
ethlambda-p2p.workspace = true
ethlambda-types.workspace = true
Expand Down
163 changes: 163 additions & 0 deletions bin/ethlambda/src/benchmark/corpus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//! Synthetic benchmark corpus: deterministic validators, a genesis store, and
//! per-slot attestation-pool seeding.

use std::sync::Arc;

use ethlambda_blockchain::store::produce_attestation_data;
use ethlambda_storage::{Store, backend::InMemoryBackend};
use ethlambda_types::{
attestation::{AggregationBits, HashedAttestationData},
block::SingleMessageAggregate,
state::{State, Validator, ValidatorPubkeyBytes},
};

/// Fixed genesis time for synthetic runs. The harness derives every tick
/// timestamp from slot numbers relative to this value and never reads the wall
/// clock, so runs are reproducible at any time of day.
const GENESIS_TIME: u64 = 1_700_000_000;

pub(crate) struct SyntheticCorpus {
num_validators: u64,
proofs_per_data: u64,
}

impl SyntheticCorpus {
pub(crate) fn new(num_validators: u64, proofs_per_data: u64) -> Self {
Self {
num_validators,
proofs_per_data,
}
}

/// Build a genesis store over an in-memory backend with `num_validators`
/// seed-derived validators.
///
/// Pubkeys are deterministic placeholder bytes: in mock-crypto mode no code
/// path decodes them (signature verification is skipped and best-proof
/// compaction never resolves pubkeys).
pub(crate) fn genesis_store(&self, seed: u64) -> Store {
let mut rng_state = seed;
let validators = (0..self.num_validators)
.map(|index| Validator {
attestation_pubkey: synthetic_pubkey(&mut rng_state),
proposal_pubkey: synthetic_pubkey(&mut rng_state),
index,
})
.collect();
let genesis_state = State::from_genesis(GENESIS_TIME, validators);
Store::from_anchor_state(Arc::new(InMemoryBackend::new()), genesis_state)
}

/// Seed the pending ("new") pool with the full validator set's attestations
/// for `attestation_slot`, split into `proofs_per_data` disjoint aggregates.
///
/// Mirrors what committee aggregators gossip during a slot: several
/// aggregates for the same `AttestationData`, each covering a validator
/// subset. The proposal tick then promotes them to the known pool, exactly
/// as on a live node. Entries are inserted in a fixed order because pool
/// insertion order pins within-entry proof choice during selection.
///
/// Returns the total number of pool entries the next build will see, across
/// both pools.
pub(crate) fn seed_pool(
&self,
store: &mut Store,
attestation_slot: u64,
) -> eyre::Result<usize> {
let data = produce_attestation_data(store, attestation_slot);
let entries = participant_groups(self.num_validators, self.proofs_per_data)
.into_iter()
.map(|participants| {
(
HashedAttestationData::new(data.clone()),
SingleMessageAggregate::empty(participants),
)
})
.collect();
store.insert_new_aggregated_payloads_batch(entries);

// The pending pool evicts whole data-root entries FIFO once its proof
// cap is exceeded, so an over-cap batch seeds nothing and every
// measured block would come out empty.
let pending = store.new_aggregated_payloads_count();
eyre::ensure!(
pending > 0,
"attestations seeded for slot {attestation_slot} were evicted from the pending pool; \
the measured workload would not match the requested parameters"
);
Ok(pending + store.known_aggregated_payloads_count())
}
}

/// Partition validators 0..num_validators into `groups` disjoint bitfields,
/// assigning validator `i` to group `i % groups`. Every group is non-empty
/// (groups is capped at the validator count) and the union covers every
/// validator exactly once.
fn participant_groups(num_validators: u64, groups: u64) -> Vec<AggregationBits> {
let groups = groups.clamp(1, num_validators);
(0..groups)
.map(|group| {
let mut bits = AggregationBits::with_length(num_validators as usize)
.expect("validator count is within the bitlist limit");
for index in (group..num_validators).step_by(groups as usize) {
bits.set(index as usize, true)
.expect("index is within the bitlist length");
}
bits
})
.collect()
}

/// splitmix64: tiny deterministic generator for placeholder pubkey bytes,
/// avoiding a rand dependency.
fn splitmix64(state: &mut u64) -> u64 {
*state = state.wrapping_add(0x9e37_79b9_7f4a_7c15);
let mut z = *state;
z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
z ^ (z >> 31)
}

fn synthetic_pubkey(rng_state: &mut u64) -> ValidatorPubkeyBytes {
let mut bytes = [0u8; 52];
for chunk in bytes.chunks_mut(8) {
let word = splitmix64(rng_state).to_le_bytes();
chunk.copy_from_slice(&word[..chunk.len()]);
}
bytes
}

#[cfg(test)]
mod tests {
use super::*;
use ethlambda_types::attestation::validator_indices;

#[test]
fn participant_groups_partition_all_validators() {
for (validators, groups) in [(8u64, 2u64), (8, 3), (5, 8), (1, 1), (4096, 4)] {
let partition = participant_groups(validators, groups);
assert_eq!(partition.len() as u64, groups.min(validators));
let mut seen = vec![0u32; validators as usize];
for bits in &partition {
let indices: Vec<u64> = validator_indices(bits).collect();
assert!(!indices.is_empty(), "every group must be non-empty");
for index in indices {
seen[index as usize] += 1;
}
}
assert!(
seen.iter().all(|&count| count == 1),
"every validator must appear in exactly one group: {seen:?}"
);
}
}

#[test]
fn synthetic_pubkeys_are_deterministic() {
let mut a = 42u64;
let mut b = 42u64;
assert_eq!(synthetic_pubkey(&mut a), synthetic_pubkey(&mut b));
let mut c = 43u64;
assert_ne!(synthetic_pubkey(&mut a), synthetic_pubkey(&mut c));
}
}
Loading