Skip to content

Commit

Permalink
fix: persist finalized block (paradigmxyz#11623)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored and reymom committed Oct 15, 2024
1 parent 7ed900b commit 7b74cb4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
17 changes: 16 additions & 1 deletion crates/engine/tree/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use reth_chain_state::ExecutedBlock;
use reth_errors::ProviderError;
use reth_provider::{
providers::ProviderNodeTypes, writer::UnifiedStorageWriter, BlockHashReader,
DatabaseProviderFactory, ProviderFactory, StaticFileProviderFactory,
DatabaseProviderFactory, FinalizedBlockWriter, ProviderFactory, StaticFileProviderFactory,
};
use reth_prune::{PrunerError, PrunerOutput, PrunerWithFactory};
use reth_stages_api::{MetricEvent, MetricEventsSender};
Expand Down Expand Up @@ -92,6 +92,10 @@ impl<N: ProviderNodeTypes> PersistenceService<N> {
// we ignore the error because the caller may or may not care about the result
let _ = sender.send(res);
}
PersistenceAction::SaveFinalizedBlock(finalized_block) => self
.provider
.database_provider_rw()?
.save_finalized_block_number(finalized_block)?,
}
}
Ok(())
Expand Down Expand Up @@ -168,6 +172,9 @@ pub enum PersistenceAction {
/// Prune associated block data before the given block number, according to already-configured
/// prune modes.
PruneBefore(u64, oneshot::Sender<PrunerOutput>),

/// Update the persisted finalized block on disk
SaveFinalizedBlock(u64),
}

/// A handle to the persistence service
Expand Down Expand Up @@ -235,6 +242,14 @@ impl PersistenceHandle {
self.send_action(PersistenceAction::SaveBlocks(blocks, tx))
}

/// Persists the finalized block number on disk.
pub fn save_finalized_block_number(
&self,
finalized_block: u64,
) -> Result<(), SendError<PersistenceAction>> {
self.send_action(PersistenceAction::SaveFinalizedBlock(finalized_block))
}

/// Tells the persistence service to remove blocks above a certain block number. The removed
/// blocks are returned by the service.
///
Expand Down
9 changes: 8 additions & 1 deletion crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2358,7 +2358,14 @@ where
return Err(OnForkChoiceUpdated::invalid_state())
}
Ok(Some(finalized)) => {
self.canonical_in_memory_state.set_finalized(finalized);
if Some(finalized.num_hash()) !=
self.canonical_in_memory_state.get_finalized_num_hash()
{
// we're also persisting the finalized block on disk so we can reload it on
// restart this is required by optimism which queries the finalized block: <https://github.com/ethereum-optimism/optimism/blob/c383eb880f307caa3ca41010ec10f30f08396b2e/op-node/rollup/sync/start.go#L65-L65>
let _ = self.persistence.save_finalized_block_number(finalized.number);
self.canonical_in_memory_state.set_finalized(finalized);
}
}
Err(err) => {
error!(target: "engine::tree", %err, "Failed to fetch finalized block header");
Expand Down

0 comments on commit 7b74cb4

Please sign in to comment.