Skip to content

Commit 11ebcf8

Browse files
bomanapsArtiomTr
authored andcommitted
address review comment on pruned blocks
1 parent 383c033 commit 11ebcf8

4 files changed

Lines changed: 19 additions & 17 deletions

File tree

lean_client/fork_choice/src/handlers.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use metrics::METRICS;
77
use ssz::{H256, SszHash};
88
use tracing::warn;
99

10-
use crate::store::{INTERVALS_PER_SLOT, MILLIS_PER_INTERVAL, Store, tick_interval, update_head};
10+
use crate::store::{
11+
INTERVALS_PER_SLOT, MILLIS_PER_INTERVAL, STATE_PRUNE_BUFFER, Store, tick_interval, update_head,
12+
};
1113

1214
#[inline]
1315
pub fn on_tick(store: &mut Store, time_millis: u64, has_proposal: bool) {
@@ -463,11 +465,8 @@ fn process_block_internal(
463465
"Block processed - new state info"
464466
);
465467

466-
// Store block and state, store the plain Block (not SignedBlockWithAttestation)
467468
store.blocks.insert(block_root, block.clone());
468469
store.states.insert(block_root, new_state.clone());
469-
// Also store signed block for serving BlocksByRoot requests (checkpoint sync backfill)
470-
store.signed_blocks.insert(block_root, signed_block.clone());
471470

472471
// Retry attestations that arrived before this block was known.
473472
// Drain the queue for this root and re-process each attestation.
@@ -525,6 +524,13 @@ fn process_block_internal(
525524
};
526525
metrics.lean_latest_finalized_slot.set(slot);
527526
});
527+
528+
let keep_from = store
529+
.latest_finalized
530+
.slot
531+
.0
532+
.saturating_sub(STATE_PRUNE_BUFFER);
533+
store.states.retain(|_, state| state.slot.0 >= keep_from);
528534
}
529535

530536
if !justified_updated && !finalized_updated {

lean_client/fork_choice/src/store.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,10 @@ pub struct Store {
6363
pub latest_new_aggregated_payloads: HashMap<SignatureKey, Vec<AggregatedSignatureProof>>,
6464

6565
/// Attestation data indexed by hash (data_root).
66-
/// Used to look up the exact attestation data that was signed,
67-
/// matching ream's attestation_data_by_root_provider design.
66+
/// Used to look up the exact attestation data that was signed when
67+
/// processing aggregated payloads for safe target computation.
6868
pub attestation_data_by_root: HashMap<H256, AttestationData>,
6969

70-
/// Signed blocks indexed by block root.
71-
/// Used to serve BlocksByRoot requests to peers for checkpoint sync backfill.
72-
pub signed_blocks: HashMap<H256, SignedBlockWithAttestation>,
73-
7470
/// Gossip attestations waiting for referenced blocks to arrive.
7571
/// Keyed by the missing block root. Drained when that block is processed.
7672
pub pending_attestations: HashMap<H256, Vec<SignedAttestation>>,
@@ -86,6 +82,13 @@ pub struct Store {
8682

8783
const JUSTIFICATION_LOOKBACK_SLOTS: u64 = 3;
8884

85+
/// Number of slots before the finalized slot for which states are retained.
86+
/// States older than (finalized_slot - STATE_PRUNE_BUFFER) are pruned after
87+
/// each finalization advance. The buffer covers late-arriving blocks and rapid
88+
/// finalization jumps without risk of evicting a parent state still needed
89+
/// for an in-flight state transition.
90+
pub const STATE_PRUNE_BUFFER: u64 = 128;
91+
8992
impl Store {
9093
pub fn produce_attestation_data(&self, slot: Slot) -> Result<AttestationData> {
9194
let head_checkpoint = Checkpoint {
@@ -226,7 +229,6 @@ pub fn get_forkchoice_store(
226229
latest_known_aggregated_payloads: HashMap::new(),
227230
latest_new_aggregated_payloads: HashMap::new(),
228231
attestation_data_by_root: HashMap::new(),
229-
signed_blocks: [(block_root, anchor_block)].into(),
230232
pending_attestations: HashMap::new(),
231233
pending_aggregated_attestations: HashMap::new(),
232234
pending_fetch_roots: HashSet::new(),

lean_client/networking/src/network/service.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -771,9 +771,6 @@ where
771771
let root = block.message.block.hash_tree_root();
772772
provider.insert(root, block.clone());
773773
}
774-
// Prune finalized blocks — they can never be processed.
775-
let finalized_slot = self.status_provider.read().finalized.slot.0;
776-
provider.retain(|_, b| b.message.block.slot.0 > finalized_slot);
777774
// Hard cap: evict lowest-slot blocks if still over limit.
778775
if provider.len() > MAX_BLOCK_CACHE_SIZE {
779776
let to_remove = provider.len() - MAX_BLOCK_CACHE_SIZE;

lean_client/src/main.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,9 +877,6 @@ async fn main() -> Result<()> {
877877
{
878878
let mut provider = signed_block_provider.write();
879879
provider.insert(block_root, signed_block_with_attestation.clone());
880-
// Prune finalized blocks — they can never be processed.
881-
let finalized_slot = status_provider.read().finalized.slot.0;
882-
provider.retain(|_, b| b.message.block.slot.0 > finalized_slot);
883880
// Hard cap: evict lowest-slot blocks if still over limit.
884881
if provider.len() > MAX_BLOCK_CACHE_SIZE {
885882
let to_remove = provider.len() - MAX_BLOCK_CACHE_SIZE;

0 commit comments

Comments
 (0)