You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Skip unnecessary payload deserialization, batch attestation inserts, unify validator_indices, and clean up time constants (#174)
## Summary
Follow-up cleanup from reviewing the devnet 3 merge (#107). Four
targeted optimizations and deduplication fixes — no behavior changes.
- Skip payload deserialization in `update_safe_target` (runs every slot)
- Batch `insert_attestation_data_by_root` commits in `on_block_core`
(runs every block)
- Unify duplicated `aggregation_bits_to_validator_indices` into shared
`validator_indices()`
- Derive `MILLISECONDS_PER_SLOT` from base constants, remove redundant
`SECONDS_PER_SLOT`
## Changes in detail
### 1. Key-only iterators for `update_safe_target`
(`crates/storage/src/store.rs`, `crates/blockchain/src/store.rs`)
`update_safe_target` calls `iter_known_aggregated_payloads()` and
`iter_new_aggregated_payloads()`, which deserialize
`Vec<StoredAggregatedPayload>` per entry (each containing multi-KB
`AggregatedSignatureProof` objects). It then immediately discards the
values and passes only the keys to `extract_latest_attestations`.
**Fix:** Added `iter_known_aggregated_payload_keys()` and
`iter_new_aggregated_payload_keys()` backed by a shared
`iter_aggregated_payload_keys(table)` helper that decodes only the key
bytes, skipping value deserialization entirely. `update_safe_target` now
uses these through a `HashSet` for deduplication.
This runs every slot (every 4 seconds) and scales with validator count.
### 2. Batch attestation data inserts in `on_block_core`
(`crates/storage/src/store.rs`, `crates/blockchain/src/store.rs`)
`on_block_core` called `insert_attestation_data_by_root` once per block
body attestation plus once for the proposer attestation. Each call
opened a separate write batch and committed — N+1 round-trips per block.
**Fix:** Added `insert_attestation_data_by_root_batch` that writes all
entries in a single batch-commit. `on_block_core` now collects all
attestation data entries (body + proposer) and inserts them in one go.
The existing `insert_attestation_data_by_root` is kept for the
single-entry callers (`on_gossip_attestation`,
`on_gossip_aggregated_attestation`).
### 3. Unify `aggregation_bits_to_validator_indices`
(`crates/common/types/src/attestation.rs`,
`crates/common/types/src/block.rs`, `crates/blockchain/src/store.rs`)
`store.rs` had a free function
`aggregation_bits_to_validator_indices(&AggregationBits) -> Vec<u64>`
that was structurally identical to
`AggregatedSignatureProof::participant_indices()` in `block.rs`. Both
iterate a bitfield and collect set-bit indices.
**Fix:** Added `validator_indices(&AggregationBits) -> impl
Iterator<Item = u64>` in `ethlambda_types::attestation` (where
`AggregationBits` is defined). `participant_indices()` now delegates to
it. The local function in `store.rs` is removed; all 5 call sites use
the shared function.
### 4. Derive `MILLISECONDS_PER_SLOT` and remove `SECONDS_PER_SLOT`
(`crates/blockchain/src/lib.rs`, test files)
Three independent time constants were defined:
```rust
pub const SECONDS_PER_SLOT: u64 = 4;
pub const MILLISECONDS_PER_SLOT: u64 = 4_000;
pub const MILLISECONDS_PER_INTERVAL: u64 = 800;
pub const INTERVALS_PER_SLOT: u64 = 5;
```
`SECONDS_PER_SLOT` was only used in two test files and was redundant.
`MILLISECONDS_PER_SLOT` was independently defined rather than derived,
creating a consistency risk if either base constant changes.
**Fix:** Removed `SECONDS_PER_SLOT`. Made `MILLISECONDS_PER_SLOT =
MILLISECONDS_PER_INTERVAL * INTERVALS_PER_SLOT`. Updated test callers
(`forkchoice_spectests.rs`, `signature_spectests.rs`) to use
`genesis_time * 1000 + slot * MILLISECONDS_PER_SLOT`, matching the
production arithmetic in `get_proposal_head`.
## How to test
All existing tests cover these changes — no new behavior was introduced.
```bash
cargo test --workspace --release
cargo clippy --workspace --tests -- -D warnings
cargo fmt --all -- --check
```
All 102 tests pass.
0 commit comments