Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support make canonical on demand #9867

Merged
merged 2 commits into from
Jul 29, 2024
Merged
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
41 changes: 27 additions & 14 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ impl TreeState {
/// Returns the new chain for the given head.
///
/// This also handles reorgs.
///
/// Note: This does not update the tracked state and instead returns the new chain based on the
/// given head.
fn on_new_head(&self, new_head: B256) -> Option<NewCanonicalChain> {
let mut new_chain = Vec::new();
let mut current_hash = new_head;
Expand Down Expand Up @@ -630,8 +633,10 @@ where
/// Attempts to make the given target canonical.
///
/// This will update the tracked canonical in memory state and do the necessary housekeeping.
const fn make_canonical(&self, target: B256) {
// TODO: implement state updates and shift canonical state
fn make_canonical(&mut self, target: B256) {
if let Some(chain_update) = self.state.tree_state.on_new_head(target) {
self.on_canonical_chain_update(chain_update);
}
}

/// Convenience function to handle an optional tree event.
Expand Down Expand Up @@ -1018,6 +1023,25 @@ where
None
}

/// Invoked when we the canonical chain has been updated.
///
/// This is invoked on a valid forkchoice update, or if we can make the target block canonical.
fn on_canonical_chain_update(&mut self, chain_update: NewCanonicalChain) {
trace!(target: "engine", new_blocks = %chain_update.new_block_count(), reorged_blocks = %chain_update.reorged_block_count() ,"applying new chain update");
// update the tracked canonical head
self.state.tree_state.set_canonical_head(chain_update.tip().num_hash());

let tip = chain_update.tip().header.clone();
let notification = chain_update.to_chain_notification();

// update the tracked in-memory state with the new chain
self.canonical_in_memory_state.update_chain(chain_update);
self.canonical_in_memory_state.set_canonical_head(tip);

// sends an event to all active listeners about the new canonical chain
self.canonical_in_memory_state.notify_canon_state(notification);
}

/// This handles downloaded blocks that are shown to be disconnected from the canonical chain.
///
/// This mainly compares the missing parent of the downloaded block with the current canonical
Expand Down Expand Up @@ -1502,19 +1526,8 @@ where

// 2. ensure we can apply a new chain update for the head block
if let Some(chain_update) = self.state.tree_state.on_new_head(state.head_block_hash) {
trace!(target: "engine", new_blocks = %chain_update.new_block_count(), reorged_blocks = %chain_update.reorged_block_count() ,"applying new chain update");
// update the tracked canonical head
self.state.tree_state.set_canonical_head(chain_update.tip().num_hash());

let tip = chain_update.tip().header.clone();
let notification = chain_update.to_chain_notification();

// update the tracked in-memory state with the new chain
self.canonical_in_memory_state.update_chain(chain_update);
self.canonical_in_memory_state.set_canonical_head(tip.clone());

// sends an event to all active listeners about the new canonical chain
self.canonical_in_memory_state.notify_canon_state(notification);
self.on_canonical_chain_update(chain_update);

// update the safe and finalized blocks and ensure their values are valid, but only
// after the head block is made canonical
Expand Down
Loading