Skip to content
35 changes: 11 additions & 24 deletions crates/net/p2p/src/req_resp/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;

use ethlambda_storage::Store;
use libp2p::{PeerId, request_response};
Expand Down Expand Up @@ -269,31 +269,18 @@ fn canonical_blocks_by_range(store: &Store, start_slot: u64, count: u64) -> Vec<
return Vec::new();
};

let mut roots_by_slot = HashMap::new();
let mut current_root = store.head().expect("head block exists");

while !current_root.is_zero() {
let Ok(Some(header)) = store.get_block_header(&current_root) else {
break;
};

if header.slot < start_slot {
break;
}

if header.slot <= end_slot {
roots_by_slot.insert(header.slot, current_root);
match store.get_signed_blocks_by_slot_range(start_slot, end_slot) {
Ok(blocks) => blocks,
Err(err) => {
warn!(
start_slot,
end_slot,
?err,
"Failed to get signed blocks by slot range"
);
Vec::new()
}

current_root = header.parent_root;
}

(start_slot..=end_slot)
.filter_map(|slot| {
let root = roots_by_slot.get(&slot)?;
store.get_signed_block(root).ok().flatten()
})
.collect()
}

async fn handle_blocks_by_root_response(
Expand Down
6 changes: 5 additions & 1 deletion crates/storage/src/api/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum Table {
/// finalized boundary are pruned (`prune_old_block_signatures`), while
/// headers and bodies are kept forever.
BlockSignatures,
/// Canonical block index: slot -> block root
BlockRoots,
/// State storage: H256 -> State
///
/// Holds full-state snapshots only: the bootstrap anchor plus one anchor
Expand All @@ -35,10 +37,11 @@ pub enum Table {
}

/// All table variants.
pub const ALL_TABLES: [Table; 7] = [
pub const ALL_TABLES: [Table; 8] = [
Table::BlockHeaders,
Table::BlockBodies,
Table::BlockSignatures,
Table::BlockRoots,
Table::States,
Table::StateDiffs,
Table::Metadata,
Expand All @@ -52,6 +55,7 @@ impl Table {
Table::BlockHeaders => "block_headers",
Table::BlockBodies => "block_bodies",
Table::BlockSignatures => "block_signatures",
Table::BlockRoots => "block_roots",
Table::States => "states",
Table::StateDiffs => "state_diffs",
Table::Metadata => "metadata",
Expand Down
Loading