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

fix: only use tags if they have been set #10150

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ where
let last_canonical_hashes =
externals.fetch_latest_canonical_hashes(config.num_of_canonical_hashes() as usize)?;

let last_finalized_block_number = externals.fetch_latest_finalized_block_number()?;
// If we haven't written the finalized block, assume it's zero
let last_finalized_block_number =
externals.fetch_latest_finalized_block_number()?.unwrap_or_default();

Ok(Self {
externals,
Expand Down
4 changes: 3 additions & 1 deletion crates/blockchain-tree/src/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ impl<DB: Database, E> TreeExternals<DB, E> {
Ok(hashes)
}

pub(crate) fn fetch_latest_finalized_block_number(&self) -> ProviderResult<BlockNumber> {
pub(crate) fn fetch_latest_finalized_block_number(
&self,
) -> ProviderResult<Option<BlockNumber>> {
self.provider_factory.provider()?.last_finalized_block_number()
}

Expand Down
6 changes: 2 additions & 4 deletions crates/chain-state/src/chain_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl ChainInfoTracker {
/// Create a new chain info container for the given canonical head and finalized header if it
/// exists.
pub fn new(head: SealedHeader, finalized: Option<SealedHeader>) -> Self {
let (finalized_block, _) = watch::channel(finalized.clone());
let (safe_block, _) = watch::channel(finalized);
let (finalized_block, _) = watch::channel(finalized);
let (safe_block, _) = watch::channel(None);

Self {
inner: Arc::new(ChainInfoInner {
Expand Down Expand Up @@ -88,14 +88,12 @@ impl ChainInfoTracker {
}

/// Returns the safe header of the chain.
#[allow(dead_code)]
pub fn get_safe_num_hash(&self) -> Option<BlockNumHash> {
let h = self.inner.safe_block.borrow();
h.as_ref().map(|h| h.num_hash())
}

/// Returns the finalized header of the chain.
#[allow(dead_code)]
pub fn get_finalized_num_hash(&self) -> Option<BlockNumHash> {
let h = self.inner.finalized_block.borrow();
h.as_ref().map(|h| h.num_hash())
Expand Down
4 changes: 3 additions & 1 deletion crates/cli/commands/src/stage/unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ impl Command {
let last_saved_finalized_block_number = provider.last_finalized_block_number()?;
let range_min =
range.clone().min().ok_or(eyre::eyre!("Could not fetch lower range end"))?;
if range_min < last_saved_finalized_block_number {
if last_saved_finalized_block_number.is_none() ||
Some(range_min) < last_saved_finalized_block_number
{
provider.save_finalized_block_number(BlockNumber::from(range_min))?;
}

Expand Down
7 changes: 6 additions & 1 deletion crates/stages/api/src/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,12 @@ where
// update finalized block if needed
let last_saved_finalized_block_number =
provider_rw.last_finalized_block_number()?;
if checkpoint.block_number < last_saved_finalized_block_number {

// If None, that means the finalized block is not written so we should
// always save in that case
if last_saved_finalized_block_number.is_none() ||
Some(checkpoint.block_number) < last_saved_finalized_block_number
{
provider_rw.save_finalized_block_number(BlockNumber::from(
checkpoint.block_number,
))?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ where
latest: SealedHeader,
) -> ProviderResult<Self> {
let provider = database.provider()?;
let finalized_block_number = provider.last_finalized_block_number()?;
let finalized_header = provider.sealed_header(finalized_block_number)?;
let finalized_header = provider
.last_finalized_block_number()?
.map(|num| provider.sealed_header(num))
.transpose()?
.flatten();
Ok(Self {
database,
canonical_in_memory_state: CanonicalInMemoryState::with_head(latest, finalized_header),
Expand Down
8 changes: 3 additions & 5 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3663,18 +3663,16 @@ impl<TX: DbTx> StatsReader for DatabaseProvider<TX> {
}

impl<TX: DbTx> FinalizedBlockReader for DatabaseProvider<TX> {
fn last_finalized_block_number(&self) -> ProviderResult<BlockNumber> {
fn last_finalized_block_number(&self) -> ProviderResult<Option<BlockNumber>> {
let mut finalized_blocks = self
.tx
.cursor_read::<tables::ChainState>()?
.walk(Some(tables::ChainStateKey::LastFinalizedBlock))?
.take(1)
.collect::<Result<BTreeMap<tables::ChainStateKey, BlockNumber>, _>>()?;

let last_finalized_block_number = finalized_blocks
.pop_first()
.unwrap_or((tables::ChainStateKey::LastFinalizedBlock, 0_u64));
Ok(last_finalized_block_number.1)
let last_finalized_block_number = finalized_blocks.pop_first().map(|pair| pair.1);
Ok(last_finalized_block_number)
}
}

Expand Down
7 changes: 5 additions & 2 deletions crates/storage/provider/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ where
.header_by_number(best.best_number)?
.ok_or(ProviderError::HeaderNotFound(best.best_number.into()))?;

let finalized_block_number = provider.last_finalized_block_number()?;
let finalized_header = provider.sealed_header(finalized_block_number)?;
let finalized_header = provider
.last_finalized_block_number()?
.map(|num| provider.sealed_header(num))
.transpose()?
.flatten();

Ok(Self::with_blocks(database, tree, latest_header.seal(best.best_hash), finalized_header))
}
Expand Down
4 changes: 3 additions & 1 deletion crates/storage/provider/src/traits/finalized_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use reth_primitives::BlockNumber;
/// Functionality to read the last known finalized block from the database.
pub trait FinalizedBlockReader: Send + Sync {
/// Returns the last finalized block number.
fn last_finalized_block_number(&self) -> ProviderResult<BlockNumber>;
///
/// If no finalized block has been written yet, this returns `None`.
fn last_finalized_block_number(&self) -> ProviderResult<Option<BlockNumber>>;
}

/// Functionality to write the last known finalized block to the database.
Expand Down
Loading