Allow blacklisting blocks from being finalized again after block revert (#6301)

* Allow blacklisting blocks from being finalized again after block revert

* Use BlockRules for storing unfinalized and add have_state_at in revert

* Move finalization_check in finalize_block upward

* Directly mark finalization blacklist as badblocks

* Remove obselete comment
This commit is contained in:
Wei Tang
2020-07-31 14:32:13 +02:00
committed by GitHub
parent 7db19db948
commit 3c5cbb00aa
9 changed files with 105 additions and 50 deletions
@@ -34,10 +34,10 @@ where
let reverted = backend.revert(blocks, false)?;
let info = client.usage_info().chain;
if reverted.is_zero() {
if reverted.0.is_zero() {
info!("There aren't any non-finalized blocks to revert.");
} else {
info!("Reverted {} blocks. Best: #{} ({})", reverted, info.best_number, info.best_hash);
info!("Reverted {} blocks. Best: #{} ({})", reverted.0, info.best_number, info.best_hash);
}
Ok(())
}
@@ -30,7 +30,7 @@ use sc_client_api::{ForkBlocks, BadBlocks};
pub enum LookupResult<B: BlockT> {
/// Specification rules do not contain any special rules about this block
NotSpecial,
/// The bock is known to be bad and should not be imported
/// The block is known to be bad and should not be imported
KnownBad,
/// There is a specified canonical block hash for the given height
Expected(B::Hash)
@@ -57,6 +57,11 @@ impl<B: BlockT> BlockRules<B> {
}
}
/// Mark a new block as bad.
pub fn mark_bad(&mut self, hash: B::Hash) {
self.bad.insert(hash);
}
/// Check if there's any rule affecting the given block.
pub fn lookup(&self, number: NumberFor<B>, hash: &B::Hash) -> LookupResult<B> {
if let Some(hash_for_height) = self.forks.get(&number) {
@@ -66,7 +71,7 @@ impl<B: BlockT> BlockRules<B> {
}
if self.bad.contains(hash) {
return LookupResult::KnownBad;
return LookupResult::KnownBad
}
LookupResult::NotSpecial
+22 -11
View File
@@ -1054,20 +1054,31 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
/// reverted past the last finalized block. Returns the number of blocks
/// that were successfully reverted.
pub fn revert(&self, n: NumberFor<Block>) -> sp_blockchain::Result<NumberFor<Block>> {
Ok(self.backend.revert(n, false)?)
let (number, _) = self.backend.revert(n, false)?;
Ok(number)
}
/// Attempts to revert the chain by `n` blocks disregarding finality. This
/// method will revert any finalized blocks as requested and can potentially
/// leave the node in an inconsistent state. Other modules in the system that
/// persist data and that rely on finality (e.g. consensus parts) will be
/// unaffected by the revert. Use this method with caution and making sure
/// that no other data needs to be reverted for consistency aside from the
/// block data.
/// Attempts to revert the chain by `n` blocks disregarding finality. This method will revert
/// any finalized blocks as requested and can potentially leave the node in an inconsistent
/// state. Other modules in the system that persist data and that rely on finality
/// (e.g. consensus parts) will be unaffected by the revert. Use this method with caution and
/// making sure that no other data needs to be reverted for consistency aside from the block
/// data. If `blacklist` is set to true, will also blacklist reverted blocks from finalizing
/// again. The blacklist is reset upon client restart.
///
/// Returns the number of blocks that were successfully reverted.
pub fn unsafe_revert(&self, n: NumberFor<Block>) -> sp_blockchain::Result<NumberFor<Block>> {
Ok(self.backend.revert(n, true)?)
pub fn unsafe_revert(
&mut self,
n: NumberFor<Block>,
blacklist: bool,
) -> sp_blockchain::Result<NumberFor<Block>> {
let (number, reverted) = self.backend.revert(n, true)?;
if blacklist {
for b in reverted {
self.block_rules.mark_bad(b);
}
}
Ok(number)
}
/// Get blockchain info.
@@ -1921,7 +1932,7 @@ impl<B, E, Block, RA> BlockBackend<Block> for Client<B, E, Block, RA>
fn block_hash(&self, number: NumberFor<Block>) -> sp_blockchain::Result<Option<Block::Hash>> {
self.backend.blockchain().hash(number)
}
}
}
impl<B, E, Block, RA> backend::AuxStore for Client<B, E, Block, RA>