From bc5ba8e42aec0122411b338d1f3fb9627ea0b070 Mon Sep 17 00:00:00 2001 From: Huang ZY Date: Fri, 5 Aug 2022 17:25:05 +0800 Subject: [PATCH 01/14] 1.Add pruning param "canonical" in sc-cli. 2.Make PruningMode's default value to ArchiveCanonical. --- client/cli/src/params/pruning_params.rs | 1 + client/state-db/src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/client/cli/src/params/pruning_params.rs b/client/cli/src/params/pruning_params.rs index 9d471224cc59..7e4770b71a33 100644 --- a/client/cli/src/params/pruning_params.rs +++ b/client/cli/src/params/pruning_params.rs @@ -46,6 +46,7 @@ impl PruningParams { .as_ref() .map(|s| match s.as_str() { "archive" => Ok(PruningMode::ArchiveAll), + "canonical" => Ok(PruningMode::ArchiveCanonical), bc => bc .parse() .map_err(|_| error::Error::Input("Invalid pruning mode specified".to_string())) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index d5cca9a34218..002e838c68e4 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -260,7 +260,7 @@ impl PruningMode { impl Default for PruningMode { fn default() -> Self { - PruningMode::Constrained(Default::default()) + PruningMode::ArchiveCanonical } } @@ -835,7 +835,7 @@ mod tests { #[test] fn pruning_mode_compatibility() { for (created, reopened, expected) in [ - (None, None, Ok(PruningMode::keep_blocks(256))), + (None, None, Ok(PruningMode::ArchiveCanonical)), (None, Some(PruningMode::keep_blocks(256)), Ok(PruningMode::keep_blocks(256))), (None, Some(PruningMode::keep_blocks(128)), Ok(PruningMode::keep_blocks(128))), (None, Some(PruningMode::keep_blocks(512)), Ok(PruningMode::keep_blocks(512))), From e4256346690ca63fda3c562870d9372cadf48178 Mon Sep 17 00:00:00 2001 From: Huang ZY Date: Mon, 8 Aug 2022 17:00:38 +0800 Subject: [PATCH 02/14] Update tests in sc-state-db. --- client/state-db/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 002e838c68e4..69827f83836f 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -836,11 +836,11 @@ mod tests { fn pruning_mode_compatibility() { for (created, reopened, expected) in [ (None, None, Ok(PruningMode::ArchiveCanonical)), - (None, Some(PruningMode::keep_blocks(256)), Ok(PruningMode::keep_blocks(256))), - (None, Some(PruningMode::keep_blocks(128)), Ok(PruningMode::keep_blocks(128))), - (None, Some(PruningMode::keep_blocks(512)), Ok(PruningMode::keep_blocks(512))), + (None, Some(PruningMode::keep_blocks(256)), Err(())), + (None, Some(PruningMode::keep_blocks(128)), Err(())), + (None, Some(PruningMode::keep_blocks(512)), Err(())), (None, Some(PruningMode::ArchiveAll), Err(())), - (None, Some(PruningMode::ArchiveCanonical), Err(())), + (None, Some(PruningMode::ArchiveCanonical), Ok(PruningMode::ArchiveCanonical)), (Some(PruningMode::keep_blocks(256)), None, Ok(PruningMode::keep_blocks(256))), ( Some(PruningMode::keep_blocks(256)), From e2c567491e1e7c8c1d9621dd3bdc81569c11cea9 Mon Sep 17 00:00:00 2001 From: Huang ZY Date: Mon, 8 Aug 2022 18:30:25 +0800 Subject: [PATCH 03/14] Update tests in sc-state-db. --- client/state-db/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index fd0e5ad52ecc..880f6650fe54 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -836,12 +836,12 @@ mod tests { fn pruning_mode_compatibility() { for (created, reopened, expected) in [ (None, None, Ok(PruningMode::ArchiveCanonical)), - (None, Some(PruningMode::keep_blocks(256)), Err(())), - (None, Some(PruningMode::keep_blocks(128)), Err(())), - (None, Some(PruningMode::keep_blocks(512)), Err(())), + (None, Some(PruningMode::blocks_pruning(256)), Err(())), + (None, Some(PruningMode::blocks_pruning(128)), Err(())), + (None, Some(PruningMode::blocks_pruning(512)), Err(())), (None, Some(PruningMode::ArchiveAll), Err(())), (None, Some(PruningMode::ArchiveCanonical), Ok(PruningMode::ArchiveCanonical)), - (Some(PruningMode::keep_blocks(256)), None, Ok(PruningMode::keep_blocks(256))), + (Some(PruningMode::blocks_pruning(256)), None, Ok(PruningMode::blocks_pruning(256))), ( Some(PruningMode::blocks_pruning(256)), Some(PruningMode::blocks_pruning(256)), From 3f03c824855b10429009fcea385661b86546fe3a Mon Sep 17 00:00:00 2001 From: Huang ZY Date: Fri, 9 Sep 2022 14:59:22 +0800 Subject: [PATCH 04/14] 1.Add a new value `AllWithNonFinalized` in `enum BlocksPruning` which Corresponds to `blocks_pruning 0` in CLI . 2.Change value `All` to `AllFinalized` in `enum BlocksPruning` and make it to keep full finalized block history. --- bin/node/cli/benches/block_production.rs | 2 +- bin/node/cli/benches/transaction_pool.rs | 2 +- bin/node/testing/src/bench.rs | 2 +- client/cli/src/config.rs | 4 ++-- client/cli/src/params/pruning_params.rs | 7 ++++--- client/db/benches/state_access.rs | 2 +- client/db/src/lib.rs | 26 +++++++++++++++--------- client/service/test/src/client/mod.rs | 4 ++-- client/service/test/src/lib.rs | 2 +- client/state-db/src/lib.rs | 12 +++++------ 10 files changed, 35 insertions(+), 28 deletions(-) diff --git a/bin/node/cli/benches/block_production.rs b/bin/node/cli/benches/block_production.rs index c0f3b96e093c..bdf4838e65c0 100644 --- a/bin/node/cli/benches/block_production.rs +++ b/bin/node/cli/benches/block_production.rs @@ -74,7 +74,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase { database: DatabaseSource::RocksDb { path: root.join("db"), cache_size: 128 }, trie_cache_maximum_size: Some(64 * 1024 * 1024), state_pruning: Some(PruningMode::ArchiveAll), - blocks_pruning: BlocksPruning::All, + blocks_pruning: BlocksPruning::AllWithNonFinalized, chain_spec: spec, wasm_method: WasmExecutionMethod::Compiled { instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite, diff --git a/bin/node/cli/benches/transaction_pool.rs b/bin/node/cli/benches/transaction_pool.rs index e6084fba8242..cdd06c7d16b4 100644 --- a/bin/node/cli/benches/transaction_pool.rs +++ b/bin/node/cli/benches/transaction_pool.rs @@ -68,7 +68,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase { database: DatabaseSource::RocksDb { path: root.join("db"), cache_size: 128 }, trie_cache_maximum_size: Some(64 * 1024 * 1024), state_pruning: Some(PruningMode::ArchiveAll), - blocks_pruning: BlocksPruning::All, + blocks_pruning: BlocksPruning::AllWithNonFinalized, chain_spec: spec, wasm_method: WasmExecutionMethod::Interpreted, // NOTE: we enforce the use of the native runtime to make the errors more debuggable diff --git a/bin/node/testing/src/bench.rs b/bin/node/testing/src/bench.rs index 534f0a4f0973..9cd2353f13b4 100644 --- a/bin/node/testing/src/bench.rs +++ b/bin/node/testing/src/bench.rs @@ -391,7 +391,7 @@ impl BenchDb { trie_cache_maximum_size: Some(16 * 1024 * 1024), state_pruning: Some(PruningMode::ArchiveAll), source: database_type.into_settings(dir.into()), - blocks_pruning: sc_client_db::BlocksPruning::All, + blocks_pruning: sc_client_db::BlocksPruning::AllWithNonFinalized, }; let task_executor = TaskExecutor::new(); diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index bc5941914de8..27c2728a341e 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -251,11 +251,11 @@ pub trait CliConfiguration: Sized { /// Get the block pruning mode. /// /// By default this is retrieved from `block_pruning` if it is available. Otherwise its - /// `BlocksPruning::All`. + /// `BlocksPruning::AllFinalized`. fn blocks_pruning(&self) -> Result { self.pruning_params() .map(|x| x.blocks_pruning()) - .unwrap_or_else(|| Ok(BlocksPruning::All)) + .unwrap_or_else(|| Ok(BlocksPruning::AllFinalized)) } /// Get the chain ID (string). diff --git a/client/cli/src/params/pruning_params.rs b/client/cli/src/params/pruning_params.rs index e077e78245ae..4400f4605b30 100644 --- a/client/cli/src/params/pruning_params.rs +++ b/client/cli/src/params/pruning_params.rs @@ -32,7 +32,7 @@ pub struct PruningParams { pub state_pruning: Option, /// 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,7 +46,7 @@ impl PruningParams { .as_ref() .map(|s| match s.as_str() { "archive" => Ok(PruningMode::ArchiveAll), - "canonical" => Ok(PruningMode::ArchiveCanonical), + "final" => Ok(PruningMode::ArchiveCanonical), bc => bc .parse() .map_err(|_| error::Error::Input("Invalid pruning mode specified".to_string())) @@ -58,8 +58,9 @@ impl PruningParams { /// Get the block pruning value from the parameters pub fn blocks_pruning(&self) -> error::Result { Ok(match self.blocks_pruning { + Some(0) => BlocksPruning::AllWithNonFinalized, Some(n) => BlocksPruning::Some(n), - None => BlocksPruning::All, + None => BlocksPruning::AllFinalized, }) } } diff --git a/client/db/benches/state_access.rs b/client/db/benches/state_access.rs index 78aed7858e34..3f5244865452 100644 --- a/client/db/benches/state_access.rs +++ b/client/db/benches/state_access.rs @@ -122,7 +122,7 @@ fn create_backend(config: BenchmarkConfig, temp_dir: &TempDir) -> Backend trie_cache_maximum_size, state_pruning: Some(PruningMode::ArchiveAll), source: DatabaseSource::ParityDb { path }, - blocks_pruning: BlocksPruning::All, + blocks_pruning: BlocksPruning::AllWithNonFinalized, }; Backend::new(settings, 100).expect("Creates backend") diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index 79ef7e9b6625..8a985abb3f96 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -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. + AllWithNonFinalized, + /// Keep full finalized block history. + AllFinalized, /// Keep N recent finalized blocks. Some(u32), } @@ -1707,12 +1709,16 @@ impl Backend { finalized: NumberFor, displaced: &FinalizationOutcome>, ) -> 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::::number(number))?; + if BlocksPruning::AllWithNonFinalized == self.blocks_pruning { + // Do nothing. + } else { + 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::::number(number))?; + } } // Also discard all blocks from displaced branches @@ -2506,7 +2512,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::AllFinalized, }, 0, ) diff --git a/client/service/test/src/client/mod.rs b/client/service/test/src/client/mod.rs index f02b1321d292..23b3b210d160 100644 --- a/client/service/test/src/client/mod.rs +++ b/client/service/test/src/client/mod.rs @@ -1199,7 +1199,7 @@ fn doesnt_import_blocks_that_revert_finality() { DatabaseSettings { trie_cache_maximum_size: Some(1 << 20), state_pruning: Some(PruningMode::ArchiveAll), - blocks_pruning: BlocksPruning::All, + blocks_pruning: BlocksPruning::AllWithNonFinalized, source: DatabaseSource::RocksDb { path: tmp.path().into(), cache_size: 1024 }, }, u64::MAX, @@ -1425,7 +1425,7 @@ fn returns_status_for_pruned_blocks() { DatabaseSettings { trie_cache_maximum_size: Some(1 << 20), state_pruning: Some(PruningMode::blocks_pruning(1)), - blocks_pruning: BlocksPruning::All, + blocks_pruning: BlocksPruning::AllFinalized, source: DatabaseSource::RocksDb { path: tmp.path().into(), cache_size: 1024 }, }, u64::MAX, diff --git a/client/service/test/src/lib.rs b/client/service/test/src/lib.rs index 11c1cbaf7afb..3be5b7360750 100644 --- a/client/service/test/src/lib.rs +++ b/client/service/test/src/lib.rs @@ -237,7 +237,7 @@ fn node_config< database: DatabaseSource::RocksDb { path: root.join("db"), cache_size: 128 }, trie_cache_maximum_size: Some(16 * 1024 * 1024), state_pruning: Default::default(), - blocks_pruning: BlocksPruning::All, + blocks_pruning: BlocksPruning::AllFinalized, chain_spec: Box::new((*spec).clone()), wasm_method: sc_service::config::WasmExecutionMethod::Interpreted, wasm_runtime_overrides: Default::default(), diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 4111385a8f34..f21b707a489f 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -270,7 +270,7 @@ impl PruningMode { impl Default for PruningMode { fn default() -> Self { - PruningMode::ArchiveCanonical + PruningMode::Constrained(Default::default()) } } @@ -924,12 +924,12 @@ mod tests { #[test] fn pruning_mode_compatibility() { for (created, reopened, expected) in [ - (None, None, Ok(PruningMode::ArchiveCanonical)), - (None, Some(PruningMode::blocks_pruning(256)), Err(())), - (None, Some(PruningMode::blocks_pruning(128)), Err(())), - (None, Some(PruningMode::blocks_pruning(512)), Err(())), + (None, None, Ok(PruningMode::blocks_pruning(256))), + (None, Some(PruningMode::blocks_pruning(256)), Ok(PruningMode::blocks_pruning(256))), + (None, Some(PruningMode::blocks_pruning(128)), Ok(PruningMode::blocks_pruning(128))), + (None, Some(PruningMode::blocks_pruning(512)), Ok(PruningMode::blocks_pruning(512))), (None, Some(PruningMode::ArchiveAll), Err(())), - (None, Some(PruningMode::ArchiveCanonical), Ok(PruningMode::ArchiveCanonical)), + (None, Some(PruningMode::ArchiveCanonical), Err(())), (Some(PruningMode::blocks_pruning(256)), None, Ok(PruningMode::blocks_pruning(256))), ( Some(PruningMode::blocks_pruning(256)), From 458e078b5f40cfb0b8a9845c17bff368d89d291e Mon Sep 17 00:00:00 2001 From: Huang ZY Date: Sat, 10 Sep 2022 16:32:10 +0800 Subject: [PATCH 05/14] Make some corresponding adjustments based on the content in the conversation. --- bin/node/cli/benches/block_production.rs | 2 +- bin/node/cli/benches/transaction_pool.rs | 2 +- bin/node/testing/src/bench.rs | 2 +- client/cli/src/config.rs | 4 +- client/cli/src/params/pruning_params.rs | 4 +- client/db/benches/state_access.rs | 2 +- client/db/src/lib.rs | 70 +++++++++++++++--------- client/service/test/src/client/mod.rs | 4 +- client/service/test/src/lib.rs | 2 +- 9 files changed, 55 insertions(+), 37 deletions(-) diff --git a/bin/node/cli/benches/block_production.rs b/bin/node/cli/benches/block_production.rs index bdf4838e65c0..ab4156a9f51c 100644 --- a/bin/node/cli/benches/block_production.rs +++ b/bin/node/cli/benches/block_production.rs @@ -74,7 +74,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase { database: DatabaseSource::RocksDb { path: root.join("db"), cache_size: 128 }, trie_cache_maximum_size: Some(64 * 1024 * 1024), state_pruning: Some(PruningMode::ArchiveAll), - blocks_pruning: BlocksPruning::AllWithNonFinalized, + blocks_pruning: BlocksPruning::KeepAll, chain_spec: spec, wasm_method: WasmExecutionMethod::Compiled { instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite, diff --git a/bin/node/cli/benches/transaction_pool.rs b/bin/node/cli/benches/transaction_pool.rs index cdd06c7d16b4..a8839642ddc2 100644 --- a/bin/node/cli/benches/transaction_pool.rs +++ b/bin/node/cli/benches/transaction_pool.rs @@ -68,7 +68,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase { database: DatabaseSource::RocksDb { path: root.join("db"), cache_size: 128 }, trie_cache_maximum_size: Some(64 * 1024 * 1024), state_pruning: Some(PruningMode::ArchiveAll), - blocks_pruning: BlocksPruning::AllWithNonFinalized, + blocks_pruning: BlocksPruning::KeepAll, chain_spec: spec, wasm_method: WasmExecutionMethod::Interpreted, // NOTE: we enforce the use of the native runtime to make the errors more debuggable diff --git a/bin/node/testing/src/bench.rs b/bin/node/testing/src/bench.rs index 9cd2353f13b4..58e9e2ffd2b3 100644 --- a/bin/node/testing/src/bench.rs +++ b/bin/node/testing/src/bench.rs @@ -391,7 +391,7 @@ impl BenchDb { trie_cache_maximum_size: Some(16 * 1024 * 1024), state_pruning: Some(PruningMode::ArchiveAll), source: database_type.into_settings(dir.into()), - blocks_pruning: sc_client_db::BlocksPruning::AllWithNonFinalized, + blocks_pruning: sc_client_db::BlocksPruning::KeepAll, }; let task_executor = TaskExecutor::new(); diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index 27c2728a341e..fad2ec7bc4a9 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -251,11 +251,11 @@ pub trait CliConfiguration: Sized { /// Get the block pruning mode. /// /// By default this is retrieved from `block_pruning` if it is available. Otherwise its - /// `BlocksPruning::AllFinalized`. + /// `BlocksPruning::KeepFinalized`. fn blocks_pruning(&self) -> Result { self.pruning_params() .map(|x| x.blocks_pruning()) - .unwrap_or_else(|| Ok(BlocksPruning::AllFinalized)) + .unwrap_or_else(|| Ok(BlocksPruning::KeepFinalized)) } /// Get the chain ID (string). diff --git a/client/cli/src/params/pruning_params.rs b/client/cli/src/params/pruning_params.rs index 4400f4605b30..4cd6aab929a7 100644 --- a/client/cli/src/params/pruning_params.rs +++ b/client/cli/src/params/pruning_params.rs @@ -58,9 +58,9 @@ impl PruningParams { /// Get the block pruning value from the parameters pub fn blocks_pruning(&self) -> error::Result { Ok(match self.blocks_pruning { - Some(0) => BlocksPruning::AllWithNonFinalized, + Some(0) => BlocksPruning::KeepAll, Some(n) => BlocksPruning::Some(n), - None => BlocksPruning::AllFinalized, + None => BlocksPruning::KeepFinalized, }) } } diff --git a/client/db/benches/state_access.rs b/client/db/benches/state_access.rs index 3f5244865452..714dda82d61b 100644 --- a/client/db/benches/state_access.rs +++ b/client/db/benches/state_access.rs @@ -122,7 +122,7 @@ fn create_backend(config: BenchmarkConfig, temp_dir: &TempDir) -> Backend trie_cache_maximum_size, state_pruning: Some(PruningMode::ArchiveAll), source: DatabaseSource::ParityDb { path }, - blocks_pruning: BlocksPruning::AllWithNonFinalized, + blocks_pruning: BlocksPruning::KeepAll, }; Backend::new(settings, 100).expect("Creates backend") diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index 8a985abb3f96..af2833faf9d4 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -323,9 +323,9 @@ pub struct DatabaseSettings { #[derive(Debug, Clone, Copy, PartialEq)] pub enum BlocksPruning { /// Keep full block history, including Non-Finalized blocks. - AllWithNonFinalized, + KeepAll, /// Keep full finalized block history. - AllFinalized, + KeepFinalized, /// Keep N recent finalized blocks. Some(u32), } @@ -1709,36 +1709,47 @@ impl Backend { finalized: NumberFor, displaced: &FinalizationOutcome>, ) -> ClientResult<()> { - if BlocksPruning::AllWithNonFinalized == self.blocks_pruning { - // Do nothing. - } else { - if let BlocksPruning::Some(blocks_pruning) = self.blocks_pruning { + 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::::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::::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, + finalized: NumberFor, + displaced: &FinalizationOutcome>, + ) -> 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::::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, } } } @@ -1758,6 +1769,13 @@ impl Backend { columns::BODY, id, )?; + utils::remove_from_db( + transaction, + &*self.storage.db, + columns::KEY_LOOKUP, + columns::JUSTIFICATIONS, + id, + )?; if let Some(index) = read_db(&*self.storage.db, columns::KEY_LOOKUP, columns::BODY_INDEX, id)? { @@ -2512,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::AllFinalized, + blocks_pruning: BlocksPruning::KeepFinalized, }, 0, ) diff --git a/client/service/test/src/client/mod.rs b/client/service/test/src/client/mod.rs index 23b3b210d160..f5c418182a43 100644 --- a/client/service/test/src/client/mod.rs +++ b/client/service/test/src/client/mod.rs @@ -1199,7 +1199,7 @@ fn doesnt_import_blocks_that_revert_finality() { DatabaseSettings { trie_cache_maximum_size: Some(1 << 20), state_pruning: Some(PruningMode::ArchiveAll), - blocks_pruning: BlocksPruning::AllWithNonFinalized, + blocks_pruning: BlocksPruning::KeepAll, source: DatabaseSource::RocksDb { path: tmp.path().into(), cache_size: 1024 }, }, u64::MAX, @@ -1425,7 +1425,7 @@ fn returns_status_for_pruned_blocks() { DatabaseSettings { trie_cache_maximum_size: Some(1 << 20), state_pruning: Some(PruningMode::blocks_pruning(1)), - blocks_pruning: BlocksPruning::AllFinalized, + blocks_pruning: BlocksPruning::KeepFinalized, source: DatabaseSource::RocksDb { path: tmp.path().into(), cache_size: 1024 }, }, u64::MAX, diff --git a/client/service/test/src/lib.rs b/client/service/test/src/lib.rs index 3be5b7360750..23245d46cba1 100644 --- a/client/service/test/src/lib.rs +++ b/client/service/test/src/lib.rs @@ -237,7 +237,7 @@ fn node_config< database: DatabaseSource::RocksDb { path: root.join("db"), cache_size: 128 }, trie_cache_maximum_size: Some(16 * 1024 * 1024), state_pruning: Default::default(), - blocks_pruning: BlocksPruning::AllFinalized, + blocks_pruning: BlocksPruning::KeepFinalized, chain_spec: Box::new((*spec).clone()), wasm_method: sc_service::config::WasmExecutionMethod::Interpreted, wasm_runtime_overrides: Default::default(), From a2287012d0350c39624cf27e8bc47388e01ff87f Mon Sep 17 00:00:00 2001 From: ZhiYong Date: Wed, 14 Sep 2022 10:52:27 +0800 Subject: [PATCH 06/14] Update client/db/src/lib.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/db/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index af2833faf9d4..5e4ea2b149f2 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -322,7 +322,7 @@ pub struct DatabaseSettings { /// Block pruning settings. #[derive(Debug, Clone, Copy, PartialEq)] pub enum BlocksPruning { - /// Keep full block history, including Non-Finalized blocks. + /// Keep full block history, of every block that was ever imported. KeepAll, /// Keep full finalized block history. KeepFinalized, From 6ae25a1b8dc1e5ff1af62090185952c4acadeeac Mon Sep 17 00:00:00 2001 From: Huang ZY Date: Wed, 14 Sep 2022 19:00:09 +0800 Subject: [PATCH 07/14] Apply suggestions from code review. --- client/cli/src/params/pruning_params.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/cli/src/params/pruning_params.rs b/client/cli/src/params/pruning_params.rs index 4cd6aab929a7..c87113df3124 100644 --- a/client/cli/src/params/pruning_params.rs +++ b/client/cli/src/params/pruning_params.rs @@ -33,6 +33,7 @@ pub struct PruningParams { /// Specify the number of finalized blocks to keep in the database. /// /// Default is to keep all of finalized blocks. + /// 0 keep all blocks. /// /// NOTE: only finalized blocks are subject for removal! #[clap(alias = "keep-blocks", long, value_name = "COUNT")] @@ -46,7 +47,7 @@ impl PruningParams { .as_ref() .map(|s| match s.as_str() { "archive" => Ok(PruningMode::ArchiveAll), - "final" => Ok(PruningMode::ArchiveCanonical), + "archive-canonical" => Ok(PruningMode::ArchiveCanonical), bc => bc .parse() .map_err(|_| error::Error::Input("Invalid pruning mode specified".to_string())) From 9e3e8eb65cda85c78cf6d7d2e6adac8dfd681e1a Mon Sep 17 00:00:00 2001 From: Huang ZY Date: Thu, 22 Sep 2022 00:49:44 +0800 Subject: [PATCH 08/14] 1.Change `blocks_pruning` to be like `state_pruning` . --- client/cli/src/params/pruning_params.rs | 22 +++-- client/db/src/lib.rs | 123 ++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 7 deletions(-) diff --git a/client/cli/src/params/pruning_params.rs b/client/cli/src/params/pruning_params.rs index c87113df3124..036e35e76786 100644 --- a/client/cli/src/params/pruning_params.rs +++ b/client/cli/src/params/pruning_params.rs @@ -37,7 +37,7 @@ pub struct PruningParams { /// /// NOTE: only finalized blocks are subject for removal! #[clap(alias = "keep-blocks", long, value_name = "COUNT")] - pub blocks_pruning: Option, + pub blocks_pruning: Option, } impl PruningParams { @@ -50,7 +50,7 @@ impl PruningParams { "archive-canonical" => Ok(PruningMode::ArchiveCanonical), bc => bc .parse() - .map_err(|_| error::Error::Input("Invalid pruning mode specified".to_string())) + .map_err(|_| error::Error::Input("Invalid state pruning mode specified".to_string())) .map(PruningMode::blocks_pruning), }) .transpose() @@ -58,10 +58,18 @@ impl PruningParams { /// Get the block pruning value from the parameters pub fn blocks_pruning(&self) -> error::Result { - Ok(match self.blocks_pruning { - Some(0) => BlocksPruning::KeepAll, - Some(n) => BlocksPruning::Some(n), - None => BlocksPruning::KeepFinalized, - }) + match self.blocks_pruning.as_ref() { + Some(bp) => { + match bp.as_str() { + "archive" => Ok(BlocksPruning::KeepAll), + "archive-canonical" => Ok(BlocksPruning::KeepFinalized), + bc => bc + .parse() + .map_err(|_| error::Error::Input("Invalid blocks pruning mode specified".to_string())) + .map(|n| BlocksPruning::Some(n)), + } + }, + None => Ok(BlocksPruning::KeepFinalized), + } } } diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index 5e4ea2b149f2..12ce86dc0450 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -1080,6 +1080,24 @@ impl Backend { Self::new(db_setting, canonicalization_delay).expect("failed to create test-db") } + #[cfg(any(test, feature = "test-helpers"))] + pub fn new_test_with_tx_storage_2(blocks_pruning: BlocksPruning, canonicalization_delay: u64) -> Self { + let db = kvdb_memorydb::create(crate::utils::NUM_COLUMNS); + let db = sp_database::as_database(db); + let state_pruning = match blocks_pruning { + BlocksPruning::KeepAll => PruningMode::ArchiveAll , + BlocksPruning::KeepFinalized => PruningMode::ArchiveCanonical , + BlocksPruning::Some(n) => PruningMode::blocks_pruning(n) , + }; + let db_setting = DatabaseSettings { + trie_cache_maximum_size: Some(16 * 1024 * 1024), + state_pruning: Some(state_pruning), + source: DatabaseSource::Custom { db, require_create_flag: true }, + blocks_pruning: blocks_pruning, + }; + + Self::new(db_setting, canonicalization_delay).expect("failed to create test-db") + } /// Expose the Database that is used by this backend. /// The second argument is the Column that stores the State. @@ -3233,6 +3251,111 @@ pub(crate) mod tests { assert_eq!(Some(vec![3.into()]), bc.body(BlockId::hash(blocks[3])).unwrap()); assert_eq!(Some(vec![4.into()]), bc.body(BlockId::hash(blocks[4])).unwrap()); } + + #[test] + fn prune_blocks_on_finalize_in_keep_all() { + let backend = Backend::::new_test_with_tx_storage_2(BlocksPruning::KeepAll, 0); + let mut blocks = Vec::new(); + let mut prev_hash = Default::default(); + for i in 0..5 { + let hash = insert_block( + &backend, + i, + prev_hash, + None, + Default::default(), + vec![i.into()], + None, + ) + .unwrap(); + blocks.push(hash); + prev_hash = hash; + } + + let mut op = backend.begin_operation().unwrap(); + backend.begin_state_operation(&mut op, BlockId::Hash(blocks[4])).unwrap(); + for i in 1..3 { + op.mark_finalized(BlockId::Hash(blocks[i]), None).unwrap(); + } + backend.commit_operation(op).unwrap(); + + let bc = backend.blockchain(); + assert_eq!(Some(vec![0.into()]), bc.body(BlockId::hash(blocks[0])).unwrap()); + assert_eq!(Some(vec![1.into()]), bc.body(BlockId::hash(blocks[1])).unwrap()); + assert_eq!(Some(vec![2.into()]), bc.body(BlockId::hash(blocks[2])).unwrap()); + assert_eq!(Some(vec![3.into()]), bc.body(BlockId::hash(blocks[3])).unwrap()); + assert_eq!(Some(vec![4.into()]), bc.body(BlockId::hash(blocks[4])).unwrap()); + } + + #[test] + fn prune_blocks_on_finalize_with_fork_in_keep_all() { + let backend = Backend::::new_test_with_tx_storage_2(BlocksPruning::KeepAll, 10); + let mut blocks = Vec::new(); + let mut prev_hash = Default::default(); + for i in 0..5 { + let hash = insert_block( + &backend, + i, + prev_hash, + None, + Default::default(), + vec![i.into()], + None, + ) + .unwrap(); + blocks.push(hash); + prev_hash = hash; + } + + // insert a fork at block 2 + let fork_hash_root = insert_block( + &backend, + 2, + blocks[1], + None, + sp_core::H256::random(), + vec![2.into()], + None, + ) + .unwrap(); + insert_block( + &backend, + 3, + fork_hash_root, + None, + H256::random(), + vec![3.into(), 11.into()], + None, + ) + .unwrap(); + + let mut op = backend.begin_operation().unwrap(); + backend.begin_state_operation(&mut op, BlockId::Hash(blocks[4])).unwrap(); + op.mark_head(BlockId::Hash(blocks[4])).unwrap(); + backend.commit_operation(op).unwrap(); + + let bc = backend.blockchain(); + assert_eq!(Some(vec![2.into()]), bc.body(BlockId::hash(fork_hash_root)).unwrap()); + + for i in 1..5 { + let mut op = backend.begin_operation().unwrap(); + backend.begin_state_operation(&mut op, BlockId::Hash(blocks[i])).unwrap(); + op.mark_finalized(BlockId::Hash(blocks[i]), None).unwrap(); + backend.commit_operation(op).unwrap(); + } + + assert_eq!(Some(vec![0.into()]), bc.body(BlockId::hash(blocks[0])).unwrap()); + assert_eq!(Some(vec![1.into()]), bc.body(BlockId::hash(blocks[1])).unwrap()); + assert_eq!(Some(vec![2.into()]), bc.body(BlockId::hash(blocks[2])).unwrap()); + assert_eq!(Some(vec![3.into()]), bc.body(BlockId::hash(blocks[3])).unwrap()); + assert_eq!(Some(vec![4.into()]), bc.body(BlockId::hash(blocks[4])).unwrap()); + + assert_eq!(Some(vec![2.into()]), bc.body(BlockId::hash(fork_hash_root)).unwrap()); + assert_eq!(bc.info().best_number, 4); + for i in 0..5 { + assert!(bc.hash(i).unwrap().is_some()); + } + } #[test] fn prune_blocks_on_finalize_with_fork() { From 4c3810c901b986095ca899bf7b99ec07b0f1ae1c Mon Sep 17 00:00:00 2001 From: Huang ZY Date: Sun, 25 Sep 2022 00:03:23 +0800 Subject: [PATCH 09/14] Fmt and add some doc. --- client/cli/src/params/pruning_params.rs | 22 ++++++++++++---------- client/db/src/lib.rs | 20 +++++++++++++------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/client/cli/src/params/pruning_params.rs b/client/cli/src/params/pruning_params.rs index 036e35e76786..0d33b0ad220d 100644 --- a/client/cli/src/params/pruning_params.rs +++ b/client/cli/src/params/pruning_params.rs @@ -50,7 +50,9 @@ impl PruningParams { "archive-canonical" => Ok(PruningMode::ArchiveCanonical), bc => bc .parse() - .map_err(|_| error::Error::Input("Invalid state pruning mode specified".to_string())) + .map_err(|_| { + error::Error::Input("Invalid state pruning mode specified".to_string()) + }) .map(PruningMode::blocks_pruning), }) .transpose() @@ -59,15 +61,15 @@ impl PruningParams { /// Get the block pruning value from the parameters pub fn blocks_pruning(&self) -> error::Result { match self.blocks_pruning.as_ref() { - Some(bp) => { - match bp.as_str() { - "archive" => Ok(BlocksPruning::KeepAll), - "archive-canonical" => Ok(BlocksPruning::KeepFinalized), - bc => bc - .parse() - .map_err(|_| error::Error::Input("Invalid blocks pruning mode specified".to_string())) - .map(|n| BlocksPruning::Some(n)), - } + Some(bp) => match bp.as_str() { + "archive" => Ok(BlocksPruning::KeepAll), + "archive-canonical" => Ok(BlocksPruning::KeepFinalized), + bc => bc + .parse() + .map_err(|_| { + error::Error::Input("Invalid blocks pruning mode specified".to_string()) + }) + .map(|n| BlocksPruning::Some(n)), }, None => Ok(BlocksPruning::KeepFinalized), } diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index 12ce86dc0450..aaade7f64bd7 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -1080,20 +1080,26 @@ impl Backend { Self::new(db_setting, canonicalization_delay).expect("failed to create test-db") } + + /// Same function as `new_test_with_tx_storage`,just change the type of `blocks_pruning` to + /// `BlocksPruning`. #[cfg(any(test, feature = "test-helpers"))] - pub fn new_test_with_tx_storage_2(blocks_pruning: BlocksPruning, canonicalization_delay: u64) -> Self { + pub fn new_test_with_tx_storage_2( + blocks_pruning: BlocksPruning, + canonicalization_delay: u64, + ) -> Self { let db = kvdb_memorydb::create(crate::utils::NUM_COLUMNS); let db = sp_database::as_database(db); let state_pruning = match blocks_pruning { - BlocksPruning::KeepAll => PruningMode::ArchiveAll , - BlocksPruning::KeepFinalized => PruningMode::ArchiveCanonical , - BlocksPruning::Some(n) => PruningMode::blocks_pruning(n) , + BlocksPruning::KeepAll => PruningMode::ArchiveAll, + BlocksPruning::KeepFinalized => PruningMode::ArchiveCanonical, + BlocksPruning::Some(n) => PruningMode::blocks_pruning(n), }; let db_setting = DatabaseSettings { trie_cache_maximum_size: Some(16 * 1024 * 1024), state_pruning: Some(state_pruning), source: DatabaseSource::Custom { db, require_create_flag: true }, - blocks_pruning: blocks_pruning, + blocks_pruning, }; Self::new(db_setting, canonicalization_delay).expect("failed to create test-db") @@ -3251,7 +3257,7 @@ pub(crate) mod tests { assert_eq!(Some(vec![3.into()]), bc.body(BlockId::hash(blocks[3])).unwrap()); assert_eq!(Some(vec![4.into()]), bc.body(BlockId::hash(blocks[4])).unwrap()); } - + #[test] fn prune_blocks_on_finalize_in_keep_all() { let backend = Backend::::new_test_with_tx_storage_2(BlocksPruning::KeepAll, 0); @@ -3278,7 +3284,7 @@ pub(crate) mod tests { op.mark_finalized(BlockId::Hash(blocks[i]), None).unwrap(); } backend.commit_operation(op).unwrap(); - + let bc = backend.blockchain(); assert_eq!(Some(vec![0.into()]), bc.body(BlockId::hash(blocks[0])).unwrap()); assert_eq!(Some(vec![1.into()]), bc.body(BlockId::hash(blocks[1])).unwrap()); From 2b0c25c487aaead8b3b4f85564ea64a9d05430d0 Mon Sep 17 00:00:00 2001 From: ZhiYong Date: Sun, 25 Sep 2022 21:18:57 +0800 Subject: [PATCH 10/14] Update client/cli/src/params/pruning_params.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/cli/src/params/pruning_params.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/cli/src/params/pruning_params.rs b/client/cli/src/params/pruning_params.rs index 0d33b0ad220d..1b7695e4d7b1 100644 --- a/client/cli/src/params/pruning_params.rs +++ b/client/cli/src/params/pruning_params.rs @@ -32,7 +32,7 @@ pub struct PruningParams { pub state_pruning: Option, /// Specify the number of finalized blocks to keep in the database. /// - /// Default is to keep all of finalized blocks. + /// Default is to keep all finalized blocks. /// 0 keep all blocks. /// /// NOTE: only finalized blocks are subject for removal! From 6cbc62ee22ef67c79aefacda1a1cdf2abf510ea0 Mon Sep 17 00:00:00 2001 From: ZhiYong Date: Sun, 25 Sep 2022 21:20:30 +0800 Subject: [PATCH 11/14] Update client/cli/src/params/pruning_params.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/cli/src/params/pruning_params.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/cli/src/params/pruning_params.rs b/client/cli/src/params/pruning_params.rs index 1b7695e4d7b1..d169d0e57aea 100644 --- a/client/cli/src/params/pruning_params.rs +++ b/client/cli/src/params/pruning_params.rs @@ -69,7 +69,7 @@ impl PruningParams { .map_err(|_| { error::Error::Input("Invalid blocks pruning mode specified".to_string()) }) - .map(|n| BlocksPruning::Some(n)), + .map(BlocksPruning::Some), }, None => Ok(BlocksPruning::KeepFinalized), } From df5ffecf0dd576980f05920c0317929e6b1f3751 Mon Sep 17 00:00:00 2001 From: Huang ZY Date: Sun, 25 Sep 2022 22:32:14 +0800 Subject: [PATCH 12/14] Update doc. --- client/cli/src/params/pruning_params.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/cli/src/params/pruning_params.rs b/client/cli/src/params/pruning_params.rs index d169d0e57aea..b764e4722e94 100644 --- a/client/cli/src/params/pruning_params.rs +++ b/client/cli/src/params/pruning_params.rs @@ -30,10 +30,12 @@ pub struct PruningParams { /// or for all of the canonical blocks (i.e 'archive-canonical'). #[clap(alias = "pruning", long, value_name = "PRUNING_MODE")] pub state_pruning: Option, - /// Specify the number of finalized blocks to keep in the database. + /// Specify the blocks pruning mode, a number of blocks to keep or 'archive'. /// /// Default is to keep all finalized blocks. - /// 0 keep all blocks. + /// otherwise, all blocks can be kept (i.e 'archive'), + /// or for all canonical blocks (i.e 'archive-canonical'), + /// or for the last N blocks (i.e a number). /// /// NOTE: only finalized blocks are subject for removal! #[clap(alias = "keep-blocks", long, value_name = "COUNT")] From cb62c23aa5744981ffaccbcdfffd0d1418c96bf6 Mon Sep 17 00:00:00 2001 From: Huang ZY Date: Sun, 25 Sep 2022 22:57:09 +0800 Subject: [PATCH 13/14] Change `new_test_with_tx_storage` to take `BlocksPruning`. --- client/db/src/lib.rs | 36 ++++++++++-------------------------- test-utils/client/src/lib.rs | 4 ++-- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index aaade7f64bd7..32c4c9ef85ed 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -1063,28 +1063,12 @@ impl Backend { /// Create new memory-backed client backend for tests. #[cfg(any(test, feature = "test-helpers"))] pub fn new_test(blocks_pruning: u32, canonicalization_delay: u64) -> Self { - Self::new_test_with_tx_storage(blocks_pruning, canonicalization_delay) + Self::new_test_with_tx_storage(BlocksPruning::Some(blocks_pruning), canonicalization_delay) } /// Create new memory-backed client backend for tests. #[cfg(any(test, feature = "test-helpers"))] - pub fn new_test_with_tx_storage(blocks_pruning: u32, canonicalization_delay: u64) -> Self { - let db = kvdb_memorydb::create(crate::utils::NUM_COLUMNS); - let db = sp_database::as_database(db); - let db_setting = DatabaseSettings { - trie_cache_maximum_size: Some(16 * 1024 * 1024), - state_pruning: Some(PruningMode::blocks_pruning(blocks_pruning)), - source: DatabaseSource::Custom { db, require_create_flag: true }, - blocks_pruning: BlocksPruning::Some(blocks_pruning), - }; - - Self::new(db_setting, canonicalization_delay).expect("failed to create test-db") - } - - /// Same function as `new_test_with_tx_storage`,just change the type of `blocks_pruning` to - /// `BlocksPruning`. - #[cfg(any(test, feature = "test-helpers"))] - pub fn new_test_with_tx_storage_2( + pub fn new_test_with_tx_storage( blocks_pruning: BlocksPruning, canonicalization_delay: u64, ) -> Self { @@ -3224,7 +3208,7 @@ pub(crate) mod tests { #[test] fn prune_blocks_on_finalize() { - let backend = Backend::::new_test_with_tx_storage(2, 0); + let backend = Backend::::new_test_with_tx_storage(BlocksPruning::Some(2), 0); let mut blocks = Vec::new(); let mut prev_hash = Default::default(); for i in 0..5 { @@ -3260,7 +3244,7 @@ pub(crate) mod tests { #[test] fn prune_blocks_on_finalize_in_keep_all() { - let backend = Backend::::new_test_with_tx_storage_2(BlocksPruning::KeepAll, 0); + let backend = Backend::::new_test_with_tx_storage(BlocksPruning::KeepAll, 0); let mut blocks = Vec::new(); let mut prev_hash = Default::default(); for i in 0..5 { @@ -3295,7 +3279,7 @@ pub(crate) mod tests { #[test] fn prune_blocks_on_finalize_with_fork_in_keep_all() { - let backend = Backend::::new_test_with_tx_storage_2(BlocksPruning::KeepAll, 10); + let backend = Backend::::new_test_with_tx_storage(BlocksPruning::KeepAll, 10); let mut blocks = Vec::new(); let mut prev_hash = Default::default(); for i in 0..5 { @@ -3365,7 +3349,7 @@ pub(crate) mod tests { #[test] fn prune_blocks_on_finalize_with_fork() { - let backend = Backend::::new_test_with_tx_storage(2, 10); + let backend = Backend::::new_test_with_tx_storage(BlocksPruning::Some(2), 10); let mut blocks = Vec::new(); let mut prev_hash = Default::default(); for i in 0..5 { @@ -3426,7 +3410,7 @@ pub(crate) mod tests { #[test] fn indexed_data_block_body() { - let backend = Backend::::new_test_with_tx_storage(1, 10); + let backend = Backend::::new_test_with_tx_storage(BlocksPruning::Some(1), 10); let x0 = ExtrinsicWrapper::from(0u64).encode(); let x1 = ExtrinsicWrapper::from(1u64).encode(); @@ -3468,7 +3452,7 @@ pub(crate) mod tests { #[test] fn index_invalid_size() { - let backend = Backend::::new_test_with_tx_storage(1, 10); + let backend = Backend::::new_test_with_tx_storage(BlocksPruning::Some(1), 10); let x0 = ExtrinsicWrapper::from(0u64).encode(); let x1 = ExtrinsicWrapper::from(1u64).encode(); @@ -3503,7 +3487,7 @@ pub(crate) mod tests { #[test] fn renew_transaction_storage() { - let backend = Backend::::new_test_with_tx_storage(2, 10); + let backend = Backend::::new_test_with_tx_storage(BlocksPruning::Some(2), 10); let mut blocks = Vec::new(); let mut prev_hash = Default::default(); let x1 = ExtrinsicWrapper::from(0u64).encode(); @@ -3550,7 +3534,7 @@ pub(crate) mod tests { #[test] fn remove_leaf_block_works() { - let backend = Backend::::new_test_with_tx_storage(2, 10); + let backend = Backend::::new_test_with_tx_storage(BlocksPruning::Some(2), 10); let mut blocks = Vec::new(); let mut prev_hash = Default::default(); for i in 0..2 { diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index be4549c9957c..148da063b40a 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -26,7 +26,7 @@ pub use sc_client_api::{ execution_extensions::{ExecutionExtensions, ExecutionStrategies}, BadBlocks, ForkBlocks, }; -pub use sc_client_db::{self, Backend}; +pub use sc_client_db::{self, Backend, BlocksPruning}; pub use sc_executor::{self, NativeElseWasmExecutor, WasmExecutionMethod}; pub use sc_service::{client, RpcHandlers}; pub use sp_consensus; @@ -102,7 +102,7 @@ impl /// Create new `TestClientBuilder` with default backend and storage chain mode pub fn with_tx_storage(blocks_pruning: u32) -> Self { - let backend = Arc::new(Backend::new_test_with_tx_storage(blocks_pruning, 0)); + let backend = Arc::new(Backend::new_test_with_tx_storage(BlocksPruning::Some(blocks_pruning), 0)); Self::with_backend(backend) } } From 3dff0f00b6eefca12233fbc3580efc85b00b03a3 Mon Sep 17 00:00:00 2001 From: Huang ZY Date: Sun, 25 Sep 2022 23:01:57 +0800 Subject: [PATCH 14/14] Fmt --- test-utils/client/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index 148da063b40a..d3e71f0ad28d 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -102,7 +102,8 @@ impl /// Create new `TestClientBuilder` with default backend and storage chain mode pub fn with_tx_storage(blocks_pruning: u32) -> Self { - let backend = Arc::new(Backend::new_test_with_tx_storage(BlocksPruning::Some(blocks_pruning), 0)); + let backend = + Arc::new(Backend::new_test_with_tx_storage(BlocksPruning::Some(blocks_pruning), 0)); Self::with_backend(backend) } }