Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Allow passing a custom database when creating the Service #3957

Merged
merged 7 commits into from
Oct 30, 2019

Conversation

tomaka
Copy link
Contributor

@tomaka tomaka commented Oct 29, 2019

Relevant for #2416

Right now the substrate-client-db crate has a kvdb-rocksdb compile-time feature. When enabled (which is the case by default), we use RocksDB. When disabled (which is the case for the in-browser experiments), we print a warning and use the memorydb instead.

This pull request modifies this mechanic.

In both substrate_service::config::Configuration and substrate_client_db::DatabaseSettings it is now possible to pass either a path to a database, in which case it will be opened using RocksDB, or a custom implementation of the kvdb::KeyvalueDB trait. The in-browser node would manually open a database (using the browser's local-storage) and pass it at initialization.

The kvdb-rocksdb feature still exists in substrate_client_db. If it is disabled, we return an error at initialization in the case of a path to a database. This is safer than just printing a warning. Another option could be to simply remove at compile-time the option to pass a path, rather than producing a runtime error, but from my experience putting items behind Cargo features usually becomes a source of annoying compilation errors.

Thanks to this change, I also slightly cleaned up substrate_client_db::Backend::new_test.

@tomaka tomaka added the A0-please_review Pull request needs code review. label Oct 29, 2019
@tomaka tomaka requested a review from arkpar October 29, 2019 16:00
@@ -157,15 +157,6 @@ where TGen: RuntimeGenesis, TCSExt: Extension {
>, Error> {
let keystore = Keystore::open(config.keystore_path.clone(), config.keystore_password.clone())?;

let db_settings = client_db::DatabaseSettings {
cache_size: None,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this was surprisingly None rather than config.cache_size.
Apparently this was not intended, so I fixed it.

cc @svyatonik

@@ -32,6 +32,7 @@ header-metadata = { package = "substrate-header-metadata", path = "header-metada
[dev-dependencies]
env_logger = "0.7.0"
tempfile = "3.1.0"
client-db = { package = "substrate-client-db", path = "./db", features = ["kvdb-rocksdb"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed? test-client already pulls in client_db

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this in order to force the kvdb-rocksdb feature, otherwise the test fails.
Alternatively, I could maybe change the test to use an in-memory database.

let db_path = match config.database {
ConfigurationDb::Path { path, .. } => path,
_ => {
println!("Cannot purge custom database implementation");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use error! for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for println! because this is a CLI function, and the rest of the function also uses println! several times.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally errors should be printed to stderr.

@@ -96,6 +94,23 @@ pub struct Configuration<C, G, E = NoExtension> {
pub dev_key_seed: Option<String>,
}

/// Configuration of the database of the client.
#[derive(Clone)]
pub enum ConfigurationDb {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DbConfiguration or even DatabaseConfig would match our coding guide better

@arkpar
Copy link
Member

arkpar commented Oct 29, 2019

LG in general, minor grumbles

cache_size: Option<usize>,
},

/// Use a custom already-open database. Recommended only for testing purposes, or in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor minor nitpick. Could you just remove the everything after the first sentence?

This is exactly what I need for Cumulus and this does not fit into one of these categories ;)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants