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

Finalize blocks on confirmation depth #4

Merged
merged 3 commits into from
Jul 5, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ target/

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

*.log
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
55 changes: 39 additions & 16 deletions crates/subcoin-node/src/commands/import_blocks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::cli::params::CommonParams;
use bitcoin_explorer::BitcoinDB;
use sc_cli::{NodeKeyParams, SharedParams};
use sc_cli::{ImportParams, NodeKeyParams, SharedParams};
use sc_client_api::HeaderBackend;
use sc_consensus_nakamoto::{
BitcoinBlockImport, BitcoinBlockImporter, BlockVerification, ImportConfig,
Expand All @@ -17,21 +17,30 @@ 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.
#[clap(long)]
pub to: Option<usize>,
/// The process will stop after importing the specified number of blocks.
pub block_count: Option<usize>,

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

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

#[allow(missing_docs)]
#[clap(flatten)]
pub import_params: ImportParams,

#[allow(missing_docs)]
#[clap(flatten)]
Expand All @@ -41,6 +50,8 @@ pub struct ImportBlocks {
/// Custom version of [`sc_cli::ImportBlocksCmd`].
pub struct ImportBlocksCmd {
shared_params: SharedParams,
import_params: ImportParams,
block_count: Option<usize>,
to: Option<usize>,
execute_block: bool,
}
Expand All @@ -49,10 +60,13 @@ impl ImportBlocksCmd {
/// Constructs a new instance of [`ImportBlocksCmd`].
pub fn new(cmd: &ImportBlocks) -> Self {
let shared_params = cmd.common_params.as_shared_params();
let import_params = cmd.import_params.clone();
Self {
shared_params,
to: cmd.to,
execute_block: cmd.execute_block,
import_params,
block_count: cmd.block_count,
to: cmd.end_block,
execute_block: cmd.execute_transactions,
}
}

Expand Down Expand Up @@ -106,18 +120,17 @@ impl ImportBlocksCmd {
if total_imported > 0 {
let info = client.info();

let best_number = info.best_number;
let substrate_block_hash = info.best_hash;

let bitcoin_block_hash =
BackendExt::<OpaqueBlock>::bitcoin_block_hash_for(&client, info.best_hash)
.unwrap_or_else(|| {
panic!(
"bitcoin block hash for substrate#{},{} is missing",
info.best_number, info.best_hash
"Bitcoin block hash for substrate#{best_number},{substrate_block_hash} is missing",
)
});

let best_number = info.best_number;
let substrate_block_hash = info.best_hash;

let speed = speed::<OpaqueBlock>(best_number, last_number, last_update);

tracing::info!(
Expand All @@ -136,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 Expand Up @@ -194,6 +213,10 @@ impl sc_cli::CliConfiguration for ImportBlocksCmd {
&self.shared_params
}

fn import_params(&self) -> Option<&ImportParams> {
Some(&self.import_params)
}

fn node_key_params(&self) -> Option<&NodeKeyParams> {
None
}
Expand Down