From 39db5800f1244f0df9255a78d641570170e70585 Mon Sep 17 00:00:00 2001 From: Qinxuan Chen Date: Mon, 6 Jun 2022 15:49:09 +0800 Subject: [PATCH] Move `open_frontier_backend` to `fc_db` (#711) * Move open_frontier_backend to fc_db Signed-off-by: koushiro * Some nits Signed-off-by: koushiro --- Cargo.toml | 2 +- client/db/src/lib.rs | 33 +++++++++++++++++++- template/node/src/command.rs | 8 +++-- template/node/src/service.rs | 58 +++++++++++------------------------- 4 files changed, 55 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e656a722903e5..dbe74ae03476e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,4 +26,4 @@ members = [ "template/node", "template/runtime", ] -resolver = "2" \ No newline at end of file +resolver = "2" diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index 58bdb1ac92075..3caa87d382ca7 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -20,7 +20,11 @@ mod parity_db_adapter; mod utils; -use std::{marker::PhantomData, sync::Arc}; +use std::{ + marker::PhantomData, + path::{Path, PathBuf}, + sync::Arc, +}; use codec::{Decode, Encode}; use fp_storage::{EthereumStorageSchema, PALLET_ETHEREUM_SCHEMA_CACHE}; @@ -58,7 +62,34 @@ pub struct Backend { mapping: Arc>, } +/// Returns the frontier database directory. +pub fn frontier_database_dir(db_config_dir: &Path, db_path: &str) -> PathBuf { + db_config_dir.join("frontier").join(db_path) +} + impl Backend { + pub fn open(database: &DatabaseSource, db_config_dir: &Path) -> Result { + Self::new(&DatabaseSettings { + source: match database { + DatabaseSource::RocksDb { .. } => DatabaseSource::RocksDb { + path: frontier_database_dir(db_config_dir, "db"), + cache_size: 0, + }, + DatabaseSource::ParityDb { .. } => DatabaseSource::ParityDb { + path: frontier_database_dir(db_config_dir, "paritydb"), + }, + DatabaseSource::Auto { .. } => DatabaseSource::Auto { + rocksdb_path: frontier_database_dir(db_config_dir, "db"), + paritydb_path: frontier_database_dir(db_config_dir, "paritydb"), + cache_size: 0, + }, + _ => { + return Err("Supported db sources: `rocksdb` | `paritydb` | `auto`".to_string()) + } + }, + }) + } + pub fn new(config: &DatabaseSettings) -> Result { let db = utils::open_database(config)?; diff --git a/template/node/src/command.rs b/template/node/src/command.rs index 6eb6b07beaa31..6e01360d4c832 100644 --- a/template/node/src/command.rs +++ b/template/node/src/command.rs @@ -18,6 +18,7 @@ use std::sync::Arc; use clap::Parser; +use fc_db::frontier_database_dir; use frame_benchmarking_cli::BenchmarkCmd; use frontier_template_runtime::Block; use sc_cli::{ChainSpec, RuntimeVersion, SubstrateCli}; @@ -27,7 +28,7 @@ use crate::{ chain_spec, cli::{Cli, Subcommand}, command_helper::{inherent_benchmark_data, BenchmarkExtrinsicBuilder}, - service::{self, frontier_database_dir}, + service::{self, db_config_dir}, }; impl SubstrateCli for Cli { @@ -130,13 +131,14 @@ pub fn run() -> sc_cli::Result<()> { let runner = cli.create_runner(cmd)?; runner.sync_run(|config| { // Remove Frontier offchain db + let db_config_dir = db_config_dir(&config); let frontier_database_config = match config.database { DatabaseSource::RocksDb { .. } => DatabaseSource::RocksDb { - path: frontier_database_dir(&config, "db"), + path: frontier_database_dir(&db_config_dir, "db"), cache_size: 0, }, DatabaseSource::ParityDb { .. } => DatabaseSource::ParityDb { - path: frontier_database_dir(&config, "paritydb"), + path: frontier_database_dir(&db_config_dir, "paritydb"), }, _ => { return Err(format!("Cannot purge `{:?}` database", config.database).into()) diff --git a/template/node/src/service.rs b/template/node/src/service.rs index 64a835e3dc3cf..35dcbdee30e50 100644 --- a/template/node/src/service.rs +++ b/template/node/src/service.rs @@ -2,6 +2,7 @@ use std::{ collections::BTreeMap, + path::PathBuf, sync::{Arc, Mutex}, time::Duration, }; @@ -17,7 +18,7 @@ use sc_telemetry::{Telemetry, TelemetryWorker}; use sp_core::U256; // Frontier use fc_consensus::FrontierBlockImport; -use fc_db::DatabaseSource; +use fc_db::Backend as FrontierBackend; use fc_mapping_sync::{MappingSyncWorker, SyncStrategy}; use fc_rpc::{EthTask, OverrideHandle}; use fc_rpc_core::types::{FeeHistoryCache, FeeHistoryCacheLimit, FilterPool}; @@ -69,40 +70,15 @@ pub type ConsensusResult = ( Sealing, ); -pub fn frontier_database_dir(config: &Configuration, path: &str) -> std::path::PathBuf { - let config_dir = config +pub(crate) fn db_config_dir(config: &Configuration) -> PathBuf { + config .base_path .as_ref() .map(|base_path| base_path.config_dir(config.chain_spec.id())) .unwrap_or_else(|| { - BasePath::from_project("", "", &crate::cli::Cli::executable_name()) + BasePath::from_project("", "", &Cli::executable_name()) .config_dir(config.chain_spec.id()) - }); - config_dir.join("frontier").join(path) -} - -pub fn open_frontier_backend(config: &Configuration) -> Result>, String> { - Ok(Arc::new(fc_db::Backend::::new( - &fc_db::DatabaseSettings { - source: match config.database { - DatabaseSource::RocksDb { .. } => DatabaseSource::RocksDb { - path: frontier_database_dir(config, "db"), - cache_size: 0, - }, - DatabaseSource::ParityDb { .. } => DatabaseSource::ParityDb { - path: frontier_database_dir(config, "paritydb"), - }, - DatabaseSource::Auto { .. } => DatabaseSource::Auto { - rocksdb_path: frontier_database_dir(config, "db"), - paritydb_path: frontier_database_dir(config, "paritydb"), - cache_size: 0, - }, - _ => { - return Err("Supported db sources: `rocksdb` | `paritydb` | `auto`".to_string()) - } - }, - }, - )?)) + }) } pub fn new_partial( @@ -118,7 +94,7 @@ pub fn new_partial( ( Option, ConsensusResult, - Arc>, + Arc>, Option, (FeeHistoryCache, FeeHistoryCacheLimit), ), @@ -174,7 +150,10 @@ pub fn new_partial( client.clone(), ); - let frontier_backend = open_frontier_backend(config)?; + let frontier_backend = Arc::new(FrontierBackend::open( + &config.database, + &db_config_dir(config), + )?); let filter_pool: Option = Some(Arc::new(Mutex::new(BTreeMap::new()))); let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new())); let fee_history_cache_limit: FeeHistoryCacheLimit = cli.run.fee_history_limit; @@ -286,7 +265,6 @@ fn remote_keystore(_url: &str) -> Result, &'static str> { #[cfg(feature = "aura")] pub fn new_full(mut config: Configuration, cli: &Cli) -> Result { use sc_client_api::{BlockBackend, ExecutorProvider}; - use sc_network::warp_request_handler::WarpSyncProvider; use sp_consensus_aura::sr25519::AuthorityPair as AuraPair; // Use ethereum style for subscription ids @@ -337,12 +315,10 @@ pub fn new_full(mut config: Configuration, cli: &Cli) -> Result>> = Some(Arc::new( - sc_finality_grandpa::warp_proof::NetworkProvider::new( - backend.clone(), - consensus_result.1.shared_authority_set().clone(), - Vec::default(), - ), + let warp_sync = Arc::new(sc_finality_grandpa::warp_proof::NetworkProvider::new( + backend.clone(), + consensus_result.1.shared_authority_set().clone(), + Vec::default(), )); let (network, system_rpc_tx, network_starter) = @@ -353,7 +329,7 @@ pub fn new_full(mut config: Configuration, cli: &Cli) -> Result, backend: Arc, - frontier_backend: Arc>, + frontier_backend: Arc>, filter_pool: Option, overrides: Arc>, fee_history_cache: FeeHistoryCache,