Allow passing a custom database when creating the Service (#3957)

* Put the DB configuration in an enum

* Allow passing a custom database to client-db

* Clean-ups in client-db

* Fix client tests

* Fix service tests

* Hopefully fix tests for good this time 😩

* Address review
This commit is contained in:
Pierre Krieger
2019-10-30 16:50:08 +01:00
committed by Bastian Köcher
parent 14e4cf9155
commit 2c2bba64a0
10 changed files with 145 additions and 98 deletions
+45 -28
View File
@@ -17,7 +17,7 @@
use crate::{Service, NetworkStatus, NetworkState, error::{self, Error}, DEFAULT_PROTOCOL_ID};
use crate::{SpawnTaskHandle, start_rpc_servers, build_network_future, TransactionPoolAdapter};
use crate::status_sinks;
use crate::config::Configuration;
use crate::config::{Configuration, DatabaseConfig};
use client::{
BlockchainEvents, Client, runtime_api,
backend::RemoteBackend, light::blockchain::RemoteBlockchain,
@@ -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,
state_cache_size: config.state_cache_size,
state_cache_child_ratio:
config.state_cache_child_ratio.map(|v| (v, 100)),
path: config.database_path.clone(),
pruning: config.pruning.clone(),
};
let executor = NativeExecutor::<TExecDisp>::new(
config.wasm_method,
config.default_heap_pages,
@@ -177,14 +168,32 @@ where TGen: RuntimeGenesis, TCSExt: Extension {
.cloned()
.unwrap_or_default();
let (client, backend) = client_db::new_client(
db_settings,
executor,
&config.chain_spec,
fork_blocks,
config.execution_strategies.clone(),
Some(keystore.clone()),
)?;
let (client, backend) = {
let db_config = client_db::DatabaseSettings {
state_cache_size: config.state_cache_size,
state_cache_child_ratio:
config.state_cache_child_ratio.map(|v| (v, 100)),
pruning: config.pruning.clone(),
source: match &config.database {
DatabaseConfig::Path { path, cache_size } =>
client_db::DatabaseSettingsSrc::Path {
path: path.clone(),
cache_size: cache_size.clone().map(|u| u as usize),
},
DatabaseConfig::Custom(db) =>
client_db::DatabaseSettingsSrc::Custom(db.clone()),
},
};
client_db::new_client(
db_config,
executor,
&config.chain_spec,
fork_blocks,
config.execution_strategies.clone(),
Some(keystore.clone()),
)?
};
let client = Arc::new(client);
@@ -229,21 +238,29 @@ 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: config.database_cache_size.map(|u| u as usize),
state_cache_size: config.state_cache_size,
state_cache_child_ratio:
config.state_cache_child_ratio.map(|v| (v, 100)),
path: config.database_path.clone(),
pruning: config.pruning.clone(),
};
let executor = NativeExecutor::<TExecDisp>::new(
config.wasm_method,
config.default_heap_pages,
);
let db_storage = client_db::light::LightStorage::new(db_settings)?;
let db_storage = {
let db_settings = client_db::DatabaseSettings {
state_cache_size: config.state_cache_size,
state_cache_child_ratio:
config.state_cache_child_ratio.map(|v| (v, 100)),
pruning: config.pruning.clone(),
source: match &config.database {
DatabaseConfig::Path { path, cache_size } =>
client_db::DatabaseSettingsSrc::Path {
path: path.clone(),
cache_size: cache_size.clone().map(|u| u as usize),
},
DatabaseConfig::Custom(db) =>
client_db::DatabaseSettingsSrc::Custom(db.clone()),
},
};
client_db::light::LightStorage::new(db_settings)?
};
let light_blockchain = client::light::new_light_blockchain(db_storage);
let fetch_checker = Arc::new(client::light::new_fetch_checker(light_blockchain.clone(), executor.clone()));
let fetcher = Arc::new(network::OnDemand::new(fetch_checker));
+23 -8
View File
@@ -17,11 +17,11 @@
//! Service configuration.
pub use client::ExecutionStrategies;
pub use client_db::PruningMode;
pub use client_db::{kvdb::KeyValueDB, PruningMode};
pub use network::config::{ExtTransport, NetworkConfiguration, Roles};
pub use substrate_executor::WasmExecutionMethod;
use std::{path::PathBuf, net::SocketAddr};
use std::{path::PathBuf, net::SocketAddr, sync::Arc};
use transaction_pool;
use chain_spec::{ChainSpec, RuntimeGenesis, Extension, NoExtension};
use primitives::crypto::Protected;
@@ -45,10 +45,8 @@ pub struct Configuration<C, G, E = NoExtension> {
pub network: NetworkConfiguration,
/// Path to key files.
pub keystore_path: PathBuf,
/// Path to the database.
pub database_path: PathBuf,
/// Cache Size for internal database in MiB
pub database_cache_size: Option<u32>,
/// Configuration for the database.
pub database: DatabaseConfig,
/// Size of internal state cache in Bytes
pub state_cache_size: usize,
/// Size in percent of cache size dedicated to child tries
@@ -100,6 +98,21 @@ pub struct Configuration<C, G, E = NoExtension> {
pub dev_key_seed: Option<String>,
}
/// Configuration of the database of the client.
#[derive(Clone)]
pub enum DatabaseConfig {
/// Database file at a specific path. Recommended for most uses.
Path {
/// Path to the database.
path: PathBuf,
/// Cache Size for internal database in MiB
cache_size: Option<u32>,
},
/// A custom implementation of an already-open database.
Custom(Arc<dyn KeyValueDB>),
}
impl<C, G, E> Configuration<C, G, E> where
C: Default,
G: RuntimeGenesis,
@@ -117,8 +130,10 @@ impl<C, G, E> Configuration<C, G, E> where
transaction_pool: Default::default(),
network: Default::default(),
keystore_path: Default::default(),
database_path: Default::default(),
database_cache_size: Default::default(),
database: DatabaseConfig::Path {
path: Default::default(),
cache_size: Default::default(),
},
state_cache_size: Default::default(),
state_cache_child_ratio: Default::default(),
custom: Default::default(),