-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Remove discarded blocks and states from database by default #11983
Changes from 12 commits
bc5ba8e
d762b63
e425634
70b21b8
124f586
e2c5674
7bff3cb
da0c426
c519937
3f03c82
940d9db
458e078
a228701
6ae25a1
54a5a2b
9e3e8eb
ac58113
4c3810c
f4ea09b
2b0c25c
6cbc62e
df5ffec
cb62c23
79afe8b
3dff0f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,7 @@ pub struct PruningParams { | |
| pub state_pruning: Option<String>, | ||
| /// Specify the number of finalized blocks to keep in the database. | ||
| /// | ||
| /// Default is to keep all blocks. | ||
| /// Default is to keep all of finalized blocks. | ||
|
||
| /// | ||
| /// NOTE: only finalized blocks are subject for removal! | ||
| #[clap(alias = "keep-blocks", long, value_name = "COUNT")] | ||
|
|
@@ -46,6 +46,7 @@ impl PruningParams { | |
| .as_ref() | ||
| .map(|s| match s.as_str() { | ||
| "archive" => Ok(PruningMode::ArchiveAll), | ||
| "final" => Ok(PruningMode::ArchiveCanonical), | ||
|
||
| bc => bc | ||
| .parse() | ||
| .map_err(|_| error::Error::Input("Invalid pruning mode specified".to_string())) | ||
|
|
@@ -57,8 +58,9 @@ impl PruningParams { | |
| /// Get the block pruning value from the parameters | ||
| pub fn blocks_pruning(&self) -> error::Result<BlocksPruning> { | ||
| Ok(match self.blocks_pruning { | ||
| Some(0) => BlocksPruning::KeepAll, | ||
|
||
| Some(n) => BlocksPruning::Some(n), | ||
| None => BlocksPruning::All, | ||
| None => BlocksPruning::KeepFinalized, | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -320,10 +320,12 @@ pub struct DatabaseSettings { | |
| } | ||
|
|
||
| /// Block pruning settings. | ||
| #[derive(Debug, Clone, Copy)] | ||
| #[derive(Debug, Clone, Copy, PartialEq)] | ||
| pub enum BlocksPruning { | ||
| /// Keep full block history. | ||
| All, | ||
| /// Keep full block history, including Non-Finalized blocks. | ||
hzy1919 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| KeepAll, | ||
| /// Keep full finalized block history. | ||
| KeepFinalized, | ||
| /// Keep N recent finalized blocks. | ||
| Some(u32), | ||
| } | ||
|
|
@@ -1707,32 +1709,47 @@ impl<Block: BlockT> Backend<Block> { | |
| finalized: NumberFor<Block>, | ||
| displaced: &FinalizationOutcome<Block::Hash, NumberFor<Block>>, | ||
| ) -> ClientResult<()> { | ||
| if let BlocksPruning::Some(blocks_pruning) = self.blocks_pruning { | ||
| // Always keep the last finalized block | ||
| let keep = std::cmp::max(blocks_pruning, 1); | ||
| if finalized >= keep.into() { | ||
| let number = finalized.saturating_sub(keep.into()); | ||
| self.prune_block(transaction, BlockId::<Block>::number(number))?; | ||
| } | ||
| match self.blocks_pruning { | ||
| BlocksPruning::KeepAll => {}, | ||
| BlocksPruning::Some(blocks_pruning) => { | ||
| // Always keep the last finalized block | ||
| let keep = std::cmp::max(blocks_pruning, 1); | ||
| if finalized >= keep.into() { | ||
| let number = finalized.saturating_sub(keep.into()); | ||
| self.prune_block(transaction, BlockId::<Block>::number(number))?; | ||
| } | ||
| self.prune_displaced_branches(transaction, finalized, displaced)?; | ||
| }, | ||
| BlocksPruning::KeepFinalized => { | ||
| self.prune_displaced_branches(transaction, finalized, displaced)?; | ||
| }, | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| // Also discard all blocks from displaced branches | ||
| for h in displaced.leaves() { | ||
| let mut number = finalized; | ||
| let mut hash = *h; | ||
| // Follow displaced chains back until we reach a finalized block. | ||
| // Since leaves are discarded due to finality, they can't have parents | ||
| // that are canonical, but not yet finalized. So we stop deleting as soon as | ||
| // we reach canonical chain. | ||
| while self.blockchain.hash(number)? != Some(hash) { | ||
| let id = BlockId::<Block>::hash(hash); | ||
| match self.blockchain.header(id)? { | ||
| Some(header) => { | ||
| self.prune_block(transaction, id)?; | ||
| number = header.number().saturating_sub(One::one()); | ||
| hash = *header.parent_hash(); | ||
| }, | ||
| None => break, | ||
| } | ||
| fn prune_displaced_branches( | ||
| &self, | ||
| transaction: &mut Transaction<DbHash>, | ||
| finalized: NumberFor<Block>, | ||
| displaced: &FinalizationOutcome<Block::Hash, NumberFor<Block>>, | ||
| ) -> ClientResult<()> { | ||
| // Discard all blocks from displaced branches | ||
| for h in displaced.leaves() { | ||
| let mut number = finalized; | ||
| let mut hash = *h; | ||
| // Follow displaced chains back until we reach a finalized block. | ||
| // Since leaves are discarded due to finality, they can't have parents | ||
| // that are canonical, but not yet finalized. So we stop deleting as soon as | ||
| // we reach canonical chain. | ||
| while self.blockchain.hash(number)? != Some(hash) { | ||
| let id = BlockId::<Block>::hash(hash); | ||
| match self.blockchain.header(id)? { | ||
| Some(header) => { | ||
| self.prune_block(transaction, id)?; | ||
| number = header.number().saturating_sub(One::one()); | ||
| hash = *header.parent_hash(); | ||
| }, | ||
| None => break, | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1752,6 +1769,13 @@ impl<Block: BlockT> Backend<Block> { | |
| columns::BODY, | ||
| id, | ||
| )?; | ||
| utils::remove_from_db( | ||
| transaction, | ||
| &*self.storage.db, | ||
| columns::KEY_LOOKUP, | ||
| columns::JUSTIFICATIONS, | ||
| id, | ||
| )?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be worth mentioning in release note that justification are now pruned with block (additionally to the new parameters). |
||
| if let Some(index) = | ||
| read_db(&*self.storage.db, columns::KEY_LOOKUP, columns::BODY_INDEX, id)? | ||
| { | ||
|
|
@@ -2506,7 +2530,7 @@ pub(crate) mod tests { | |
| trie_cache_maximum_size: Some(16 * 1024 * 1024), | ||
| state_pruning: Some(PruningMode::blocks_pruning(1)), | ||
| source: DatabaseSource::Custom { db: backing, require_create_flag: false }, | ||
| blocks_pruning: BlocksPruning::All, | ||
| blocks_pruning: BlocksPruning::KeepFinalized, | ||
| }, | ||
| 0, | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc for this param should be updated with the new
finaloption.