Skip to content
Closed
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
45 changes: 42 additions & 3 deletions beacon_node/network/src/sync/block_lookups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod single_block_lookup;
mod tests;

const FAILED_CHAINS_CACHE_EXPIRY_SECONDS: u64 = 60;
const COMPLETED_LOOKUP_CACHE_EXPIRY_SECONDS: u64 = 60;
pub const SINGLE_BLOCK_LOOKUP_MAX_ATTEMPTS: u8 = 4;

pub enum BlockComponent<E: EthSpec> {
Expand Down Expand Up @@ -61,10 +62,19 @@ enum Action {
Continue,
}

#[derive(Debug)]
enum LookupTrigger {
UnknownParent,
Attestation,
}

pub struct BlockLookups<T: BeaconChainTypes> {
/// A cache of failed chain lookups to prevent duplicate searches.
failed_chains: LRUTimeCache<Hash256>,

/// A cache of completed lookups to prevent duplicate searches.
completed_lookups: LRUTimeCache<Hash256>,

// TODO: Why not index lookups by block_root?
single_block_lookups: FnvHashMap<SingleLookupId, SingleBlockLookup<T>>,

Expand All @@ -78,6 +88,9 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
failed_chains: LRUTimeCache::new(Duration::from_secs(
FAILED_CHAINS_CACHE_EXPIRY_SECONDS,
)),
completed_lookups: LRUTimeCache::new(Duration::from_secs(
COMPLETED_LOOKUP_CACHE_EXPIRY_SECONDS,
)),
single_block_lookups: Default::default(),
log,
}
Expand Down Expand Up @@ -134,6 +147,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
block_root,
Some(block_component),
Some(parent_root),
LookupTrigger::UnknownParent,
&[peer_id],
cx,
);
Expand All @@ -148,7 +162,14 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
peer_source: &[PeerId],
cx: &mut SyncNetworkContext<T>,
) {
self.new_current_lookup(block_root, None, None, peer_source, cx);
self.new_current_lookup(
block_root,
None,
None,
LookupTrigger::Attestation,
peer_source,
cx,
);
}

/// A block or blob triggers the search of a parent.
Expand Down Expand Up @@ -204,20 +225,33 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
}

// `block_root_to_search` is a failed chain check happens inside new_current_lookup
self.new_current_lookup(block_root_to_search, None, None, peers, cx)
self.new_current_lookup(
block_root_to_search,
None,
None,
LookupTrigger::UnknownParent,
peers,
cx,
)
}

/// Searches for a single block hash. If the blocks parent is unknown, a chain of blocks is
/// constructed.
/// Returns true if the lookup is created or already exists
/// Returns true if the lookup is created, already exists, or was already completed.
fn new_current_lookup(
&mut self,
block_root: Hash256,
block_component: Option<BlockComponent<T::EthSpec>>,
awaiting_parent: Option<Hash256>,
lookup_trigger: LookupTrigger,
peers: &[PeerId],
cx: &mut SyncNetworkContext<T>,
) -> bool {
// If the lookup is complete, don't create a new one.
if self.completed_lookups.contains(&block_root) {
return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to have a debug! log here to know when this is firing

}

// If this block or it's parent is part of a known failed chain, ignore it.
if self.failed_chains.contains(&block_root) {
debug!(self.log, "Block is from a past failed chain. Dropping"; "block_root" => ?block_root);
Expand Down Expand Up @@ -266,6 +300,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
"{}", msg;
"peer_ids" => ?peers,
"block" => ?block_root,
"lookup_trigger" => ?lookup_trigger,
);
metrics::inc_counter(&metrics::SYNC_LOOKUP_CREATED);

Expand Down Expand Up @@ -607,6 +642,10 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
if let Some(lookup) = self.single_block_lookups.remove(&id) {
debug!(self.log, "Dropping completed lookup"; "block" => %lookup.block_root());
metrics::inc_counter(&metrics::SYNC_LOOKUP_COMPLETED);

// Cache the block root to prevent duplicate searches.
self.completed_lookups.insert(lookup.block_root());

// Block imported, continue the requests of pending child blocks
self.continue_child_lookups(lookup.block_root(), cx);
self.update_metrics();
Expand Down