Remove the --unsafe-pruning CLI-argument (step 1) (#10995)

* sc-client-db: utils::open_database(...) — return OpenDbError so that the caller could tell the `OpenDbError::DoesNotExist` clearly

* sc-client-db: utils::open_database(..) — accept the `create: bool` argument

* sc-client-db: pruning — optional argument in the DatabaseSettings

* sc-state-db: Split `Error<E>` into separate `Error<E>` and `StateDbError`

* StateDb::open: choose the pruning-mode depending on the requested and stored values

* sc-state-db: test for different combinations of stored and requested pruning-modes

* CLI-argument: mark the unsafe-pruning as deprecated

* Fix tests

* tests: do not specify --pruning when running the substrate over the existing storage

* fix types for benches

* cargo fmt

* Check whether the pruning-mode and sync-mode are compatible

* cargo fmt

* parity-db: 0.3.11 -> 0.3.12

* sc-state-db: MetaDb::set_meta — a better doc-test

* cargo fmt

* make MetaDb read-only again!

* Remove the stray newline (and run the CI once again please)

* Last nitpicks

* A more comprehensive error message
This commit is contained in:
Roman Gafiyatullin
2022-05-06 13:07:44 +03:00
committed by GitHub
parent 994f8076b1
commit 729cba9d9e
23 changed files with 546 additions and 338 deletions
+62 -19
View File
@@ -296,8 +296,8 @@ pub struct DatabaseSettings {
pub state_cache_size: usize,
/// Ratio of cache size dedicated to child tries.
pub state_cache_child_ratio: Option<(usize, usize)>,
/// State pruning mode.
pub state_pruning: PruningMode,
/// Requested state pruning mode.
pub state_pruning: Option<PruningMode>,
/// Where to find the database.
pub source: DatabaseSource,
/// Block pruning mode.
@@ -341,7 +341,13 @@ pub enum DatabaseSource {
},
/// Use a custom already-open database.
Custom(Arc<dyn Database<DbHash>>),
Custom {
/// the handle to the custom storage
db: Arc<dyn Database<DbHash>>,
/// if set, the `create` flag will be required to open such datasource
require_create_flag: bool,
},
}
impl DatabaseSource {
@@ -354,7 +360,7 @@ impl DatabaseSource {
// I would think rocksdb, but later parity-db.
DatabaseSource::Auto { paritydb_path, .. } => Some(paritydb_path),
DatabaseSource::RocksDb { path, .. } | DatabaseSource::ParityDb { path } => Some(path),
DatabaseSource::Custom(..) => None,
DatabaseSource::Custom { .. } => None,
}
}
@@ -370,7 +376,7 @@ impl DatabaseSource {
*path = p.into();
true
},
DatabaseSource::Custom(..) => false,
DatabaseSource::Custom { .. } => false,
}
}
}
@@ -381,7 +387,7 @@ impl std::fmt::Display for DatabaseSource {
DatabaseSource::Auto { .. } => "Auto",
DatabaseSource::RocksDb { .. } => "RocksDb",
DatabaseSource::ParityDb { .. } => "ParityDb",
DatabaseSource::Custom(_) => "Custom",
DatabaseSource::Custom { .. } => "Custom",
};
write!(f, "{}", name)
}
@@ -416,7 +422,7 @@ struct PendingBlock<Block: BlockT> {
struct StateMetaDb<'a>(&'a dyn Database<DbHash>);
impl<'a> sc_state_db::MetaDb for StateMetaDb<'a> {
type Error = io::Error;
type Error = sp_database::error::DatabaseError;
fn get_meta(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
Ok(self.0.get(columns::STATE_META, key))
@@ -1009,9 +1015,23 @@ impl<Block: BlockT> Backend<Block> {
/// Create a new instance of database backend.
///
/// The pruning window is how old a block must be before the state is pruned.
pub fn new(config: DatabaseSettings, canonicalization_delay: u64) -> ClientResult<Self> {
let db = crate::utils::open_database::<Block>(&config, DatabaseType::Full)?;
Self::from_database(db as Arc<_>, canonicalization_delay, &config)
pub fn new(db_config: DatabaseSettings, canonicalization_delay: u64) -> ClientResult<Self> {
use utils::OpenDbError;
let db_source = &db_config.source;
let (needs_init, db) =
match crate::utils::open_database::<Block>(db_source, DatabaseType::Full, false) {
Ok(db) => (false, db),
Err(OpenDbError::DoesNotExist) => {
let db =
crate::utils::open_database::<Block>(db_source, DatabaseType::Full, true)?;
(true, db)
},
Err(as_is) => return Err(as_is.into()),
};
Self::from_database(db as Arc<_>, canonicalization_delay, &db_config, needs_init)
}
/// Create new memory-backed client backend for tests.
@@ -1028,8 +1048,8 @@ impl<Block: BlockT> Backend<Block> {
let db_setting = DatabaseSettings {
state_cache_size: 16777216,
state_cache_child_ratio: Some((50, 100)),
state_pruning: PruningMode::keep_blocks(keep_blocks),
source: DatabaseSource::Custom(db),
state_pruning: Some(PruningMode::keep_blocks(keep_blocks)),
source: DatabaseSource::Custom { db, require_create_flag: true },
keep_blocks: KeepBlocks::Some(keep_blocks),
};
@@ -1057,18 +1077,31 @@ impl<Block: BlockT> Backend<Block> {
db: Arc<dyn Database<DbHash>>,
canonicalization_delay: u64,
config: &DatabaseSettings,
should_init: bool,
) -> ClientResult<Self> {
let is_archive_pruning = config.state_pruning.is_archive();
let blockchain = BlockchainDb::new(db.clone())?;
let mut db_init_transaction = Transaction::new();
let requested_state_pruning = config.state_pruning.clone();
let state_meta_db = StateMetaDb(db.as_ref());
let map_e = sp_blockchain::Error::from_state_db;
let state_db: StateDb<_, _> = StateDb::new(
config.state_pruning.clone(),
let (state_db_init_commit_set, state_db) = StateDb::open(
&state_meta_db,
requested_state_pruning,
!db.supports_ref_counting(),
&StateMetaDb(&*db),
should_init,
)
.map_err(map_e)?;
apply_state_commit(&mut db_init_transaction, state_db_init_commit_set);
let state_pruning_used = state_db.pruning_mode();
let is_archive_pruning = state_pruning_used.is_archive();
let blockchain = BlockchainDb::new(db.clone())?;
let storage_db =
StorageDb { db: db.clone(), state_db, prefix_keys: !db.supports_ref_counting() };
let offchain_storage = offchain::LocalStorage::new(db.clone());
let backend = Backend {
@@ -1105,6 +1138,9 @@ impl<Block: BlockT> Backend<Block> {
with_state: true,
});
}
db.commit(db_init_transaction)?;
Ok(backend)
}
@@ -2251,6 +2287,13 @@ impl<Block: BlockT> sc_client_api::backend::Backend<Block> for Backend<Block> {
fn get_import_lock(&self) -> &RwLock<()> {
&*self.import_lock
}
fn requires_full_sync(&self) -> bool {
matches!(
self.storage.state_db.pruning_mode(),
PruningMode::ArchiveAll | PruningMode::ArchiveCanonical
)
}
}
impl<Block: BlockT> sc_client_api::backend::LocalBackend<Block> for Backend<Block> {}
@@ -2390,8 +2433,8 @@ pub(crate) mod tests {
DatabaseSettings {
state_cache_size: 16777216,
state_cache_child_ratio: Some((50, 100)),
state_pruning: PruningMode::keep_blocks(1),
source: DatabaseSource::Custom(backing),
state_pruning: Some(PruningMode::keep_blocks(1)),
source: DatabaseSource::Custom { db: backing, require_create_flag: false },
keep_blocks: KeepBlocks::All,
},
0,