Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make rocksdb and paritydb optional #1088

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ sqlx = "0.7.0-alpha.3"
sc-basic-authorship = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-block-builder = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-chain-spec = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-cli = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-cli = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sc-client-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-client-db = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-consensus = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
Expand All @@ -75,7 +75,7 @@ sc-network-common = { version = "0.10.0-dev", git = "https://github.com/parityte
sc-network-sync = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-rpc = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-rpc-api = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-service = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-service = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sc-telemetry = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-transaction-pool = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sc-transaction-pool-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
Expand Down
3 changes: 2 additions & 1 deletion client/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ scale-codec = { package = "parity-scale-codec", workspace = true }
tempfile = "3.3.0"
# Substrate
sc-block-builder = { workspace = true }
sc-client-db = { workspace = true }
sc-client-db = { workspace = true, features = ["rocksdb"] }
sp-consensus = { workspace = true }
sp-io = { workspace = true }
substrate-test-runtime-client = { workspace = true }
# Frontier
fc-db = { workspace = true, features = ["rocksdb"] }
frontier-template-runtime = { workspace = true, features = ["default"] }
12 changes: 9 additions & 3 deletions client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ log = "0.4.17"
parity-db = { workspace = true, optional = true }
parking_lot = "0.12.1"
scale-codec = { package = "parity-scale-codec", workspace = true }
smallvec = "1.10"
smallvec = { version = "1.10", optional = true }
sqlx = { workspace = true, features = ["runtime-tokio-native-tls", "sqlite"] }
tokio = { version = "1.19", features = ["macros", "sync"] }
# Substrate
sc-client-api = { workspace = true }
sc-client-db = { workspace = true, features = ["rocksdb"] }
sc-client-db = { workspace = true }
sp-api = { workspace = true }
sp-blockchain = { workspace = true }
sp-core = { workspace = true }
Expand All @@ -38,7 +38,13 @@ fp-rpc = { workspace = true, features = ["default"] }
fp-storage = { workspace = true, features = ["default"] }

[features]
default = ["kvdb-rocksdb", "parity-db"]
default = ["parity-db"]
parity-db = ["dep:parity-db"]
rocksdb = [
"kvdb-rocksdb",
"sc-client-db/rocksdb",
"smallvec",
]

[dev-dependencies]
maplit = "1.0.2"
Expand Down
2 changes: 2 additions & 0 deletions client/db/src/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ impl<Block: BlockT> Backend<Block> {
client,
&DatabaseSettings {
source: match database {
#[cfg(feature = "rocksdb")]
DatabaseSource::RocksDb { .. } => DatabaseSource::RocksDb {
path: frontier_database_dir(db_config_dir, "db"),
cache_size: 0,
},
#[cfg(feature = "parity-db")]
DatabaseSource::ParityDb { .. } => DatabaseSource::ParityDb {
path: frontier_database_dir(db_config_dir, "paritydb"),
},
Expand Down
34 changes: 21 additions & 13 deletions client/db/src/kv/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,11 @@ pub(crate) fn upgrade_db<Block: BlockT, C: HeaderBackend<Block>>(
match db_version {
0 => return Err(UpgradeError::UnsupportedVersion(db_version)),
1 => {
let summary = match source {
DatabaseSource::ParityDb { .. } => {
migrate_1_to_2_parity_db::<Block, C>(client, db_path)?
}
DatabaseSource::RocksDb { .. } => {
migrate_1_to_2_rocks_db::<Block, C>(client, db_path)?
}
let summary: UpgradeVersion1To2Summary = match source {
#[cfg(feature = "parity-db")]
DatabaseSource::ParityDb { .. } => migrate_1_to_2_parity_db::<Block, C>(client, db_path)?,
#[cfg(feature = "rocksdb")]
DatabaseSource::RocksDb { .. } => migrate_1_to_2_rocks_db::<Block, C>(client, db_path)?,
_ => panic!("DatabaseSource required for upgrade ParityDb | RocksDb"),
};
if !summary.error.is_empty() {
Expand Down Expand Up @@ -165,6 +163,7 @@ fn version_file_path(path: &Path) -> PathBuf {
/// Migration from version1 to version2:
/// - The format of the Ethereum<>Substrate block mapping changed to support equivocation.
/// - Migrating schema from One-to-one to One-to-many (EthHash: Vec<SubstrateHash>) relationship.
#[cfg(feature = "rocksdb")]
pub(crate) fn migrate_1_to_2_rocks_db<Block: BlockT, C: HeaderBackend<Block>>(
client: Arc<C>,
db_path: &Path,
Expand Down Expand Up @@ -246,6 +245,7 @@ pub(crate) fn migrate_1_to_2_rocks_db<Block: BlockT, C: HeaderBackend<Block>>(
Ok(res)
}

#[cfg(feature = "parity-db")]
pub(crate) fn migrate_1_to_2_parity_db<Block: BlockT, C: HeaderBackend<Block>>(
client: Arc<C>,
db_path: &Path,
Expand Down Expand Up @@ -333,6 +333,7 @@ mod tests {
sync::Arc,
};

use crate::kv::DatabaseSettings;
use scale_codec::Encode;
use sp_blockchain::HeaderBackend;
use sp_core::H256;
Expand All @@ -352,23 +353,29 @@ mod tests {
Ok(Arc::new(crate::kv::Backend::<Block>::new(client, setting)?))
}

#[cfg_attr(not(any(feature = "rocksdb", feature = "parity-db")), ignore)]
#[test]
fn upgrade_1_to_2_works() {
let tmp_1 = tempdir().expect("create a temporary directory");
let tmp_2 = tempdir().expect("create a temporary directory");

let settings = vec![
let settings: Vec<DatabaseSettings> = vec![
// Rocks db
#[cfg(feature = "rocksdb")]
crate::kv::DatabaseSettings {
source: sc_client_db::DatabaseSource::RocksDb {
path: tmp_1.path().to_owned(),
path: tempdir()
.expect("create a temporary directory")
.path()
.to_owned(),
cache_size: 0,
},
},
// Parity db
#[cfg(feature = "parity-db")]
crate::kv::DatabaseSettings {
source: sc_client_db::DatabaseSource::ParityDb {
path: tmp_2.path().to_owned(),
path: tempdir()
.expect("create a temporary directory")
.path()
.to_owned(),
},
},
];
Expand Down Expand Up @@ -495,6 +502,7 @@ mod tests {
}
}

#[cfg(feature = "rocksdb")]
#[test]
fn create_db_with_current_version_works() {
let tmp = tempdir().expect("create a temporary directory");
Expand Down
10 changes: 5 additions & 5 deletions client/db/src/kv/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pub fn open_database<Block: BlockT, C: HeaderBackend<Block>>(
config: &DatabaseSettings,
) -> Result<Arc<dyn Database<DbHash>>, String> {
let db: Arc<dyn Database<DbHash>> = match &config.source {
DatabaseSource::ParityDb { path } => {
open_parity_db::<Block, C>(client, path, &config.source)?
}
#[cfg(feature = "parity-db")]
DatabaseSource::ParityDb { path } => open_parity_db::<Block, C>(client, path, &config.source)?,
#[cfg(feature = "rocksdb")]
DatabaseSource::RocksDb { path, .. } => {
open_kvdb_rocksdb::<Block, C>(client, path, true, &config.source)?
}
Expand All @@ -51,7 +51,7 @@ pub fn open_database<Block: BlockT, C: HeaderBackend<Block>>(
Ok(db)
}

#[cfg(feature = "kvdb-rocksdb")]
#[cfg(feature = "rocksdb")]
fn open_kvdb_rocksdb<Block: BlockT, C: HeaderBackend<Block>>(
client: Arc<C>,
path: &Path,
Expand All @@ -75,7 +75,7 @@ fn open_kvdb_rocksdb<Block: BlockT, C: HeaderBackend<Block>>(
return Ok(sp_database::as_database(db));
}

#[cfg(not(feature = "kvdb-rocksdb"))]
#[cfg(not(feature = "rocksdb"))]
fn open_kvdb_rocksdb<Block: BlockT, C: HeaderBackend<Block>>(
_client: Arc<C>,
_path: &Path,
Expand Down
2 changes: 1 addition & 1 deletion template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ sc-network-common = { workspace = true }
sc-network-sync = { workspace = true }
sc-rpc = { workspace = true }
sc-rpc-api = { workspace = true }
sc-service = { workspace = true }
sc-service = { workspace = true, features = ["default"] }
sc-telemetry = { workspace = true }
sc-transaction-pool = { workspace = true }
sc-transaction-pool-api = { workspace = true }
Expand Down