Skip to content

Commit

Permalink
Finalize blocks with enough confirmations
Browse files Browse the repository at this point in the history
It's observed that the memory usage could be extremely high without the
finalization, when the chain grows to 220000+. Concretely, the culprit
of the high memory usage is creating `NonCanonicalOverlay`.

There are also a few other improvements to import-blocks command.
  • Loading branch information
liuchengxu committed Jul 5, 2024
1 parent 2b4e5f4 commit 8b805e2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
6 changes: 4 additions & 2 deletions crates/sc-consensus-nakamoto/src/block_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,11 @@ where

/// Result of the operation of importing a Bitcoin block.
///
/// Same semantic with [`sc_consensus::ImportResult`] for including more information
/// in the Imported variant.
/// Same semantic with [`sc_consensus::ImportResult`] with additional information
/// in the [`ImportStatus::Imported`] variant.
#[derive(Debug, Clone)]
pub enum ImportStatus {
/// Block was imported successfully.
Imported {
block_number: u32,
block_hash: BlockHash,
Expand All @@ -287,6 +288,7 @@ pub enum ImportStatus {
}

impl ImportStatus {
/// Returns `true` if the import status is [`Self::UnknownParent`].
pub fn is_unknown_parent(&self) -> bool {
matches!(self, Self::UnknownParent)
}
Expand Down
13 changes: 13 additions & 0 deletions crates/subcoin-node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ pub fn run() -> sc_cli::Result<()> {
network: bitcoin::Network::Bitcoin,
config: &config,
})?;
task_manager.spawn_handle().spawn("finalizer", None, {
let client = client.clone();
let spawn_handle = task_manager.spawn_handle();
let confirmation_depth = 6u32;
// TODO: proper value
let is_major_syncing = std::sync::Arc::new(true.into());
subcoin_service::finalize_confirmed_blocks(
client,
spawn_handle,
confirmation_depth,
is_major_syncing,
)
});
Ok((import_blocks_cmd.run(client, data_dir), task_manager))
})
}
Expand Down
31 changes: 22 additions & 9 deletions crates/subcoin-node/src/commands/import_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@ use subcoin_service::FullClient;
/// Import Bitcoin blocks from bitcoind database.
#[derive(clap::Parser, Debug, Clone)]
pub struct ImportBlocks {
/// Path of the bitcoind database.
/// Path to the bitcoind database.
///
/// Value of the `-data-dir` argument in the bitcoind program.
/// This corresponds to the value of the `-data-dir` argument in the bitcoind program.
#[clap(index = 1, value_parser)]
pub data_dir: PathBuf,

/// Specify the block number of last block to import.
/// Number of blocks to import.
///
/// The default value is the highest block in the database.
/// The process will stop after importing the specified number of blocks.
pub block_count: Option<usize>,

/// Block number of last block to import.
///
/// The default value is to the highest block in the database.
#[clap(long)]
pub to: Option<usize>,
pub end_block: Option<usize>,

/// Whether to execute the transactions in the block.
/// Whether to execute the transactions within the blocks.
#[clap(long, default_value_t = true)]
pub execute_block: bool,
pub execute_transactions: bool,

#[allow(missing_docs)]
#[clap(flatten)]
Expand All @@ -46,6 +51,7 @@ pub struct ImportBlocks {
pub struct ImportBlocksCmd {
shared_params: SharedParams,
import_params: ImportParams,
block_count: Option<usize>,
to: Option<usize>,
execute_block: bool,
}
Expand All @@ -58,8 +64,9 @@ impl ImportBlocksCmd {
Self {
shared_params,
import_params,
to: cmd.to,
execute_block: cmd.execute_block,
block_count: cmd.block_count,
to: cmd.end_block,
execute_block: cmd.execute_transactions,
}
}

Expand Down Expand Up @@ -142,6 +149,12 @@ impl ImportBlocksCmd {
}

total_imported += 1;

if let Some(block_count) = self.block_count {
if total_imported == block_count {
break;
}
}
}

tracing::info!("Imported {total_imported} blocks successfully");
Expand Down

0 comments on commit 8b805e2

Please sign in to comment.