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

CLI options for frontier sqlite backend #855

Merged
merged 3 commits into from
Jun 25, 2024
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
39 changes: 38 additions & 1 deletion client/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
// You may obtain a copy of the License at the root of this project source code

use crate::custom_commands::VerifyProofSigSubCommand;
use crate::{cli_opt, custom_commands::VerifyProofSigSubCommand};
use clap::ArgAction;

#[allow(missing_docs)]
Expand All @@ -24,6 +24,27 @@ pub struct RunCmd {
#[clap(flatten)]
pub base: sc_cli::RunCmd,

/// Sets the frontier backend type (KeyValue or Sql)
#[arg(long, value_enum, ignore_case = true, default_value_t = cli_opt::FrontierBackendType::default())]
pub frontier_backend_type: cli_opt::FrontierBackendType,

// Sets the SQL backend's pool size.
#[arg(long, default_value = "100")]
pub frontier_sql_backend_pool_size: u32,

/// Sets the SQL backend's query timeout in number of VM ops.
#[arg(long, default_value = "10000000")]
pub frontier_sql_backend_num_ops_timeout: u32,

/// Sets the SQL backend's auxiliary thread limit.
#[arg(long, default_value = "4")]
pub frontier_sql_backend_thread_count: u32,

/// Sets the SQL backend's query timeout in number of VM ops.
/// Default value is 200MB.
#[arg(long, default_value = "209715200")]
pub frontier_sql_backend_cache_size: u64,

/// Maximum number of logs in a query (EVM).
#[clap(long, default_value = "10000")]
pub max_past_logs: u32,
Expand Down Expand Up @@ -52,6 +73,22 @@ pub struct RunCmd {
pub ethy_p2p: bool,
}

impl RunCmd {
pub fn new_rpc_config(&self) -> cli_opt::RpcConfig {
cli_opt::RpcConfig {
frontier_backend_config: match self.frontier_backend_type {
cli_opt::FrontierBackendType::KeyValue => cli_opt::FrontierBackendConfig::KeyValue,
cli_opt::FrontierBackendType::Sql => cli_opt::FrontierBackendConfig::Sql {
pool_size: self.frontier_sql_backend_pool_size,
num_ops_timeout: self.frontier_sql_backend_num_ops_timeout,
thread_count: self.frontier_sql_backend_thread_count,
cache_size: self.frontier_sql_backend_cache_size,
},
},
}
}
}

#[derive(Debug, clap::Parser)]
pub struct Cli {
#[clap(subcommand)]
Expand Down
41 changes: 41 additions & 0 deletions client/src/cli_opt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

/// Available frontier backend types.
#[derive(Debug, Copy, Clone, Default, clap::ValueEnum)]
pub enum FrontierBackendType {
/// Either RocksDb or ParityDb as per inherited from the global backend settings.
#[default]
KeyValue,
/// Sql database with custom log indexing.
Sql,
}

/// Defines the frontier backend configuration.
pub enum FrontierBackendConfig {
KeyValue,
Sql { pool_size: u32, num_ops_timeout: u32, thread_count: u32, cache_size: u64 },
}

impl Default for FrontierBackendConfig {
fn default() -> FrontierBackendConfig {
FrontierBackendConfig::KeyValue
}
}

pub struct RpcConfig {
pub frontier_backend_config: FrontierBackendConfig,
}
33 changes: 23 additions & 10 deletions client/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,37 @@ pub fn run() -> sc_cli::Result<()> {
},
Some(Subcommand::CheckBlock(cmd)) => {
let runner = cli.create_runner(cmd)?;
let rpc_config = cli.run.new_rpc_config();
runner.async_run(|config| {
let PartialComponents { client, task_manager, import_queue, .. } =
service::new_partial(&config, &cli)?;
service::new_partial(&config, &cli, &rpc_config)?;
Ok((cmd.run(client, import_queue), task_manager))
})
},
Some(Subcommand::ExportBlocks(cmd)) => {
let runner = cli.create_runner(cmd)?;
let rpc_config = cli.run.new_rpc_config();
runner.async_run(|config| {
let PartialComponents { client, task_manager, .. } =
service::new_partial(&config, &cli)?;
service::new_partial(&config, &cli, &rpc_config)?;
Ok((cmd.run(client, config.database), task_manager))
})
},
Some(Subcommand::ExportState(cmd)) => {
let runner = cli.create_runner(cmd)?;
let rpc_config = cli.run.new_rpc_config();
runner.async_run(|config| {
let PartialComponents { client, task_manager, .. } =
service::new_partial(&config, &cli)?;
service::new_partial(&config, &cli, &rpc_config)?;
Ok((cmd.run(client, config.chain_spec), task_manager))
})
},
Some(Subcommand::ImportBlocks(cmd)) => {
let runner = cli.create_runner(cmd)?;
let rpc_config = cli.run.new_rpc_config();
runner.async_run(|config| {
let PartialComponents { client, task_manager, import_queue, .. } =
service::new_partial(&config, &cli)?;
service::new_partial(&config, &cli, &rpc_config)?;
Ok((cmd.run(client, import_queue), task_manager))
})
},
Expand All @@ -107,9 +111,10 @@ pub fn run() -> sc_cli::Result<()> {
},
Some(Subcommand::Revert(cmd)) => {
let runner = cli.create_runner(cmd)?;
let rpc_config = cli.run.new_rpc_config();
runner.async_run(|config| {
let PartialComponents { client, task_manager, backend, .. } =
service::new_partial(&config, &cli)?;
service::new_partial(&config, &cli, &rpc_config)?;
let aux_revert = Box::new(|client, _, blocks| {
sc_consensus_grandpa::revert(client, blocks)?;
Ok(())
Expand Down Expand Up @@ -143,7 +148,9 @@ pub fn run() -> sc_cli::Result<()> {
cmd.run::<Block, ()>(config)
},
BenchmarkCmd::Block(cmd) => {
let PartialComponents { client, .. } = service::new_partial(&config, &cli)?;
let rpc_config = cli.run.new_rpc_config();
let PartialComponents { client, .. } =
service::new_partial(&config, &cli, &rpc_config)?;
cmd.run(client)
},
#[cfg(not(feature = "runtime-benchmarks"))]
Expand All @@ -153,15 +160,18 @@ pub fn run() -> sc_cli::Result<()> {
),
#[cfg(feature = "runtime-benchmarks")]
BenchmarkCmd::Storage(cmd) => {
let rpc_config = cli.run.new_rpc_config();
let PartialComponents { client, backend, .. } =
service::new_partial(&config, &cli)?;
service::new_partial(&config, &cli, &rpc_config)?;
let db = backend.expose_db();
let storage = backend.expose_storage();

cmd.run(config, client, db, storage)
},
BenchmarkCmd::Overhead(cmd) => {
let PartialComponents { client, .. } = service::new_partial(&config, &cli)?;
let rpc_config = cli.run.new_rpc_config();
let PartialComponents { client, .. } =
service::new_partial(&config, &cli, &rpc_config)?;
let ext_builder = RemarkBuilder::new(client.clone());

cmd.run(
Expand All @@ -173,7 +183,9 @@ pub fn run() -> sc_cli::Result<()> {
)
},
BenchmarkCmd::Extrinsic(cmd) => {
let PartialComponents { client, .. } = service::new_partial(&config, &cli)?;
let rpc_config = cli.run.new_rpc_config();
let PartialComponents { client, .. } =
service::new_partial(&config, &cli, &rpc_config)?;
// Register the *Remark* and *TKA* builders.
let alice: sp_core::ecdsa::Pair =
sp_core::ecdsa::Pair::from_string("//Alice", None).unwrap().into();
Expand Down Expand Up @@ -232,7 +244,8 @@ pub fn run() -> sc_cli::Result<()> {
None => {
let runner = cli.create_runner(&cli.run.base)?;
runner.run_node_until_exit(|config| async move {
service::new_full(config, &cli).map_err(sc_cli::Error::Service)
let rpc_config = cli.run.new_rpc_config();
service::new_full(config, &cli, &rpc_config).map_err(sc_cli::Error::Service)
})
},
}
Expand Down
1 change: 1 addition & 0 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod chain_spec;
mod service;
mod benchmarking;
mod cli;
mod cli_opt;
mod command;
mod consensus_data_providers;
mod custom_commands;
Expand Down
19 changes: 9 additions & 10 deletions client/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

use std::{collections::BTreeMap, sync::Arc};

use fp_rpc::EthereumRuntimeRPCApi;

use jsonrpsee::RpcModule;
// Substrate
use sc_client_api::{
Expand All @@ -44,7 +46,7 @@ use sp_blockchain::{
use sp_consensus::SelectChain;
use sp_consensus_babe::BabeApi;
use sp_keystore::KeystorePtr;
use sp_runtime::traits::BlakeTwo256;
use sp_runtime::traits::Block as BlockT;

// Frontier
use fc_rpc::{
Expand Down Expand Up @@ -136,16 +138,13 @@ pub struct FullDeps<C, P, A: ChainApi, BE, SC> {
pub eth_forced_parent_hashes: Option<BTreeMap<H256, H256>>,
}

pub fn overrides_handle<C, BE>(client: Arc<C>) -> Arc<OverrideHandle<Block>>
pub fn overrides_handle<B, C, BE>(client: Arc<C>) -> Arc<OverrideHandle<B>>
where
C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,
C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,
C: Send + Sync + 'static,
C::Api: sp_api::ApiExt<Block>
+ fp_rpc::EthereumRuntimeRPCApi<Block>
+ fp_rpc::ConvertTransactionRuntimeApi<Block>,
BE: Backend<Block> + 'static,
BE::State: StateBackend<BlakeTwo256>,
B: BlockT,
C: ProvideRuntimeApi<B>,
C::Api: EthereumRuntimeRPCApi<B>,
C: HeaderBackend<B> + StorageProvider<B, BE> + 'static,
BE: Backend<B> + 'static,
{
// NB: the following is used to redefine storage schema after certain blocks
// on live chains
Expand Down
85 changes: 62 additions & 23 deletions client/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
use sp_runtime::{offchain::OffchainStorage, traits::BlakeTwo256};
use std::{
collections::BTreeMap,
path::Path,
sync::{Arc, Mutex},
time::Duration,
};

use seed_primitives::{ethy::ETH_HTTP_URI, opaque::Block, XRP_HTTP_URI};
use seed_runtime::{self, RuntimeApi};

use crate::{cli::Cli, consensus_data_providers::BabeConsensusDataProvider};
use crate::{
cli::Cli,
cli_opt::{FrontierBackendConfig, RpcConfig},
consensus_data_providers::BabeConsensusDataProvider,
};

// Our native executor instance.
pub struct ExecutorDispatch;
Expand Down Expand Up @@ -90,6 +95,7 @@ pub fn frontier_database_dir(config: &Configuration, path: &str) -> std::path::P
pub fn open_frontier_backend<C, BE>(
client: Arc<C>,
config: &Configuration,
rpc_config: &RpcConfig,
) -> Result<fc_db::Backend<Block>, String>
where
C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,
Expand All @@ -99,32 +105,61 @@ where
BE: Backend<Block> + 'static,
BE::State: StateBackend<BlakeTwo256>,
{
// TODO - take cli arg for frontier-backend-type and create accordingly.
Ok(fc_db::Backend::KeyValue(fc_db::kv::Backend::<Block>::new(
client,
&fc_db::kv::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,
let frontier_backend = match rpc_config.frontier_backend_config {
FrontierBackendConfig::KeyValue =>
fc_db::Backend::KeyValue(fc_db::kv::Backend::<Block>::new(
client,
&fc_db::kv::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()
),
},
},
_ =>
return Err("Supported db sources: `rocksdb` | `paritydb` | `auto`".to_string()),
},
)?),
FrontierBackendConfig::Sql { pool_size, num_ops_timeout, thread_count, cache_size } => {
let overrides = crate::rpc::overrides_handle(client.clone());
let sqlite_db_path = frontier_database_dir(config, "sql");
std::fs::create_dir_all(&sqlite_db_path).expect("failed creating sql db directory");
let backend = futures::executor::block_on(fc_db::sql::Backend::new(
fc_db::sql::BackendConfig::Sqlite(fc_db::sql::SqliteBackendConfig {
path: Path::new("sqlite:///")
.join(sqlite_db_path)
.join("frontier.db3")
.to_str()
.expect("frontier sql path error"),
create_if_missing: true,
thread_count,
cache_size,
}),
pool_size,
std::num::NonZeroU32::new(num_ops_timeout),
overrides.clone(),
))
.unwrap_or_else(|err| panic!("failed creating sql backend: {:?}", err));
fc_db::Backend::Sql(backend)
},
)?))
};
Ok(frontier_backend)
}

pub fn new_partial(
config: &Configuration,
cli: &Cli,
rpc_config: &RpcConfig,
) -> Result<
sc_service::PartialComponents<
FullClient,
Expand Down Expand Up @@ -183,7 +218,7 @@ pub fn new_partial(
client.clone(),
);

let frontier_backend = open_frontier_backend(client.clone(), config)?;
let frontier_backend = open_frontier_backend(client.clone(), config, rpc_config)?;

let filter_pool: Option<FilterPool> = Some(Arc::new(Mutex::new(BTreeMap::new())));
let fee_history_cache: FeeHistoryCache = Arc::new(Mutex::new(BTreeMap::new()));
Expand Down Expand Up @@ -252,7 +287,11 @@ pub fn new_partial(
}

/// Builds a new service for a full client.
pub fn new_full(config: Configuration, cli: &Cli) -> Result<TaskManager, ServiceError> {
pub fn new_full(
config: Configuration,
cli: &Cli,
rpc_config: &RpcConfig,
) -> Result<TaskManager, ServiceError> {
let sc_service::PartialComponents {
client,
backend,
Expand All @@ -270,7 +309,7 @@ pub fn new_full(config: Configuration, cli: &Cli) -> Result<TaskManager, Service
(fee_history_cache, fee_history_cache_limit),
babe_worker_handle,
),
} = new_partial(&config, cli)?;
} = new_partial(&config, cli, rpc_config)?;

// Set eth http bridge config
// the config is stored into the offchain context where it can
Expand Down
Loading