Skip to content

Commit

Permalink
Integrate storage monitor (paritytech#5)
Browse files Browse the repository at this point in the history
* Integrate storage monitor

* Fix clippy
  • Loading branch information
liuchengxu authored Jul 7, 2024
1 parent 5a8c608 commit a8f5ccb
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 16 deletions.
45 changes: 36 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ sc-network-sync = { git = "https://github.com/liuchengxu/polkadot-sdk", branch =
sc-rpc = { git = "https://github.com/liuchengxu/polkadot-sdk", branch = "subcoin" }
# Disable the default `rocksdb` feature
sc-service = { git = "https://github.com/liuchengxu/polkadot-sdk", branch = "subcoin", default-features = false }
sc-storage-monitor = { git = "https://github.com/liuchengxu/polkadot-sdk", branch = "subcoin" }
sc-sysinfo = { git = "https://github.com/liuchengxu/polkadot-sdk", branch = "subcoin" }
sc-telemetry = { git = "https://github.com/liuchengxu/polkadot-sdk", branch = "subcoin" }
sc-transaction-pool = { git = "https://github.com/liuchengxu/polkadot-sdk", branch = "subcoin" }
sc-transaction-pool-api = { git = "https://github.com/liuchengxu/polkadot-sdk", branch = "subcoin" }
Expand Down
1 change: 1 addition & 0 deletions crates/subcoin-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ sc-network = { workspace = true }
sc-network-sync = { workspace = true }
sc-rpc = { workspace = true }
sc-service = { workspace = true }
sc-storage-monitor = { workspace = true }
sc-telemetry = { workspace = true }
sc-transaction-pool = { workspace = true }
sc-transaction-pool-api = { workspace = true }
Expand Down
22 changes: 21 additions & 1 deletion crates/subcoin-node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,29 @@ pub enum Command {
pub struct Cli {
#[command(subcommand)]
pub command: Command,

/// Disable automatic hardware benchmarks.
///
/// By default these benchmarks are automatically ran at startup and measure
/// the CPU speed, the memory bandwidth and the disk speed.
///
/// The results are then printed out in the logs, and also sent as part of
/// telemetry, if telemetry is enabled.
#[arg(long)]
pub no_hardware_benchmarks: bool,

#[allow(missing_docs)]
#[clap(flatten)]
pub storage_monitor: sc_storage_monitor::StorageMonitorParams,
}

/// Parse and run command line arguments
pub fn run() -> sc_cli::Result<()> {
let Cli { command } = Cli::parse();
let Cli {
command,
no_hardware_benchmarks,
storage_monitor,
} = Cli::parse();

match command {
Command::ImportBlocks(cmd) => {
Expand All @@ -59,6 +77,8 @@ pub fn run() -> sc_cli::Result<()> {
} = subcoin_service::new_node(subcoin_service::SubcoinConfiguration {
network: bitcoin::Network::Bitcoin,
config: &config,
no_hardware_benchmarks,
storage_monitor,
})?;
task_manager.spawn_handle().spawn("finalizer", None, {
let client = client.clone();
Expand Down
3 changes: 3 additions & 0 deletions crates/subcoin-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license.workspace = true
[dependencies]
async-trait = { workspace = true }
bitcoin = { workspace = true }
frame-benchmarking-cli = { workspace = true }
frame-system = { workspace = true }
futures = { workspace = true }
jsonrpsee = { workspace = true }
Expand All @@ -21,6 +22,8 @@ sc-network = { workspace = true }
sc-rpc = { workspace = true }
# `test-helpers` for the exposed Client type.
sc-service = { workspace = true, features = ["test-helpers"], default-features = false }
sc-storage-monitor = { workspace = true }
sc-sysinfo = { workspace = true }
sc-telemetry = { workspace = true }
sc-transaction-pool = { workspace = true }
sc-utils = { workspace = true }
Expand Down
68 changes: 62 additions & 6 deletions crates/subcoin-service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//! Subcoin service implementation. Specialized wrapper over substrate service.
#![allow(deprecated)]

pub mod chain_spec;
mod genesis_block_builder;
mod transaction_adapter;

use bitcoin::hashes::Hash;
use frame_benchmarking_cli::SUBSTRATE_REFERENCE_HARDWARE;
use futures::StreamExt;
use genesis_block_builder::GenesisBlockBuilder;
use sc_client_api::{AuxStore, BlockchainEvents, Finalizer, HeaderBackend};
Expand All @@ -21,6 +24,7 @@ use sp_core::Encode;
use sp_keystore::KeystorePtr;
use sp_runtime::traits::{Block as BlockT, CheckedSub};
use std::ops::Deref;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use subcoin_runtime::interface::OpaqueBlock as Block;
Expand Down Expand Up @@ -135,7 +139,7 @@ async fn build_system_rpc_future<Block, Client>(
}
}

/// Bitcoin node components.
/// Subcoin node components.
pub struct NodeComponents {
/// Client.
pub client: Arc<FullClient>,
Expand All @@ -149,10 +153,12 @@ pub struct NodeComponents {
pub system_rpc_tx: TracingUnboundedSender<sc_rpc::system::Request<Block>>,
}

/// Substrate configuration plus some subcoin specific configs.
/// Subcoin node configuration.
pub struct SubcoinConfiguration<'a> {
pub network: bitcoin::Network,
pub config: &'a Configuration,
pub no_hardware_benchmarks: bool,
pub storage_monitor: sc_storage_monitor::StorageMonitorParams,
}

impl<'a> Deref for SubcoinConfiguration<'a> {
Expand Down Expand Up @@ -183,6 +189,13 @@ fn initialize_genesis_block_hash_mapping<Block: BlockT, Client: HeaderBackend<Bl

/// Creates a new subcoin node.
pub fn new_node(config: SubcoinConfiguration) -> Result<NodeComponents, ServiceError> {
let SubcoinConfiguration {
network: bitcoin_network,
config,
no_hardware_benchmarks,
storage_monitor,
} = config;

let telemetry = config
.telemetry_endpoints
.clone()
Expand All @@ -195,12 +208,10 @@ pub fn new_node(config: SubcoinConfiguration) -> Result<NodeComponents, ServiceE
.transpose()?;

// TODO: maintain the native executor on our own since it's deprecated upstream
let executor = sc_service::new_native_or_wasm_executor(&config);
let executor = sc_service::new_native_or_wasm_executor(config);

let backend = sc_service::new_db_backend(config.db_config())?;

let bitcoin_network = config.network;

let genesis_block_builder = GenesisBlockBuilder::<_, _, _, TransactionAdapter>::new(
bitcoin_network,
config.chain_spec.as_storage_builder(),
Expand All @@ -211,7 +222,7 @@ pub fn new_node(config: SubcoinConfiguration) -> Result<NodeComponents, ServiceE

let (client, backend, _keystore_container, task_manager) =
sc_service::new_full_parts_with_genesis_builder::<Block, RuntimeApi, _, _>(
config.deref(),
config,
telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),
executor.clone(),
backend,
Expand All @@ -224,6 +235,13 @@ pub fn new_node(config: SubcoinConfiguration) -> Result<NodeComponents, ServiceE

let client = Arc::new(client);

let mut telemetry = telemetry.map(|(worker, telemetry)| {
task_manager
.spawn_handle()
.spawn("telemetry", None, worker.run());
telemetry
});

let has_bootnodes = false;

let spawner = task_manager.spawn_handle();
Expand All @@ -239,6 +257,44 @@ pub fn new_node(config: SubcoinConfiguration) -> Result<NodeComponents, ServiceE
),
);

let database_path = config.database.path().map(Path::to_path_buf);
let maybe_hwbench = (!no_hardware_benchmarks)
.then_some(database_path.as_ref().map(|db_path| {
let _ = std::fs::create_dir_all(db_path);
sc_sysinfo::gather_hwbench(Some(db_path))
}))
.flatten();

if let Some(hwbench) = maybe_hwbench {
sc_sysinfo::print_hwbench(&hwbench);
match SUBSTRATE_REFERENCE_HARDWARE.check_hardware(&hwbench) {
Err(err) if config.role.is_authority() => {
tracing::warn!(
"⚠️ The hardware does not meet the minimal requirements {err} for role 'Authority'.",
);
}
_ => {}
}

if let Some(ref mut telemetry) = telemetry {
let telemetry_handle = telemetry.handle();
task_manager.spawn_handle().spawn(
"telemetry_hwbench",
None,
sc_sysinfo::initialize_hwbench_telemetry(telemetry_handle, hwbench),
);
}
}

if let Some(database_path) = database_path {
sc_storage_monitor::StorageMonitorService::try_spawn(
storage_monitor,
database_path,
&task_manager.spawn_essential_handle(),
)
.map_err(|e| ServiceError::Application(e.into()))?;
}

Ok(NodeComponents {
client,
backend,
Expand Down

0 comments on commit a8f5ccb

Please sign in to comment.