Do not run forced_canonicalization for archive nodes (#13051)

We don't canonicalize on archive nodes and thus `best_canonical` always returned `None`. So, the
moment such a node tried to force canonicalize, it was trapped in some endless loop.

This pr solves this by renaming `best_canonical` to `last_canonicalized` and also making the return
value more clear by introducing a custom enum `LastCanonicalized`.
This commit is contained in:
Bastian Köcher
2023-01-03 23:00:34 +01:00
committed by GitHub
parent 1db2dc5b36
commit 8508c0ed1f
2 changed files with 221 additions and 95 deletions
+30 -6
View File
@@ -283,6 +283,17 @@ fn to_meta_key<S: Codec>(suffix: &[u8], data: &S) -> Vec<u8> {
buffer
}
/// Status information about the last canonicalized block.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LastCanonicalized {
/// Not yet have canonicalized any block.
None,
/// The given block number is the last canonicalized block.
Block(u64),
/// No canonicalization is happening (pruning mode is archive all).
NotCanonicalizing,
}
pub struct StateDbSync<BlockHash: Hash, Key: Hash, D: MetaDb> {
mode: PruningMode,
non_canonical: NonCanonicalOverlay<BlockHash, Key>,
@@ -349,15 +360,28 @@ impl<BlockHash: Hash, Key: Hash, D: MetaDb> StateDbSync<BlockHash, Key, D> {
Ok(commit)
}
fn best_canonical(&self) -> Option<u64> {
self.non_canonical.last_canonicalized_block_number()
/// Returns the block number of the last canonicalized block.
fn last_canonicalized(&self) -> LastCanonicalized {
if self.mode == PruningMode::ArchiveAll {
LastCanonicalized::NotCanonicalizing
} else {
self.non_canonical
.last_canonicalized_block_number()
.map(LastCanonicalized::Block)
.unwrap_or_else(|| LastCanonicalized::None)
}
}
fn is_pruned(&self, hash: &BlockHash, number: u64) -> IsPruned {
match self.mode {
PruningMode::ArchiveAll => IsPruned::NotPruned,
PruningMode::ArchiveCanonical | PruningMode::Constrained(_) => {
if self.best_canonical().map(|c| number > c).unwrap_or(true) {
if self
.non_canonical
.last_canonicalized_block_number()
.map(|c| number > c)
.unwrap_or(true)
{
if self.non_canonical.have_block(hash) {
IsPruned::NotPruned
} else {
@@ -611,9 +635,9 @@ impl<BlockHash: Hash, Key: Hash, D: MetaDb> StateDb<BlockHash, Key, D> {
self.db.write().remove(hash)
}
/// Returns last finalized block number.
pub fn best_canonical(&self) -> Option<u64> {
return self.db.read().best_canonical()
/// Returns last canonicalized block.
pub fn last_canonicalized(&self) -> LastCanonicalized {
self.db.read().last_canonicalized()
}
/// Check if block is pruned away.