Skip to content

Commit

Permalink
Introduce bitcoin mainnet chain spec (#33)
Browse files Browse the repository at this point in the history
* Build chain spec for bitcoin mainnet

```
./target/release/subcoin build-spec --chain=bitcoin-mainnet --disable-default-bootnode --raw > chain-spec-raw-bitcoin-mainnet.json
```

* Nits
  • Loading branch information
liuchengxu authored Aug 5, 2024
1 parent 1948535 commit 033aeaa
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 31 deletions.
30 changes: 30 additions & 0 deletions crates/subcoin-node/res/chain-spec-raw-bitcoin-mainnet.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions crates/subcoin-node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub enum Command {
#[command(subcommand)]
Blockchain(Blockchain),

/// Build a chain specification.
BuildSpec(sc_cli::BuildSpecCmd),

/// Validate blocks.
CheckBlock(Box<sc_cli::CheckBlockCmd>),

Expand Down Expand Up @@ -148,6 +151,10 @@ pub fn run() -> sc_cli::Result<()> {
Ok((cmd.run(client), task_manager))
})
}
Command::BuildSpec(cmd) => {
let runner = SubstrateCli.create_runner(&cmd)?;
runner.sync_run(|config| cmd.run(config.chain_spec, config.network))
}
Command::CheckBlock(cmd) => {
let runner = SubstrateCli.create_runner(&*cmd)?;
runner.async_run(|config| {
Expand Down
48 changes: 25 additions & 23 deletions crates/subcoin-node/src/cli/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@ use subcoin_network::PeerId;
/// Chain.
///
/// Currently only Bitcoin is supported, more chains may be supported in the future.
// TODO: This clippy warning will be fixed once more chains are supported.
#[allow(clippy::enum_variant_names)]
#[derive(Clone, Copy, Default, Debug, clap::ValueEnum)]
pub enum Chain {
/// Bitcoin mainnet.
#[default]
Bitcoin,
BitcoinMainnet,
/// Bitcoin testnet
BitcoinTestnet,
/// Bitcoin signet.
BitcoinSignet,
}

/// Bitcoin network type.
#[derive(Clone, Copy, Debug, clap::ValueEnum)]
pub enum Network {
/// Mainnet.
Mainnet,
/// Testnet.
Testnet,
/// Signet.
Signet,
impl Chain {
/// Returns the value of `id` in `SubstrateCli::load_spec(id)`.
fn chain_spec_id(&self) -> &'static str {
// Convert to kebab-case for consistency in CLI.
match self {
Self::BitcoinMainnet => "bitcoin-mainnet",
Self::BitcoinTestnet => "bitcoin-testnet",
Self::BitcoinSignet => "bitcoin-signet",
}
}
}

#[derive(Debug, Clone, Parser)]
Expand Down Expand Up @@ -71,14 +78,10 @@ pub enum BlockExecution {

#[derive(Debug, Clone, Parser)]
pub struct CommonParams {
/// Specify the chain network.
#[arg(long, value_name = "CHAIN", default_value = "bitcoin")]
/// Specify the chain.
#[arg(long, value_name = "CHAIN", default_value = "bitcoin-mainnet")]
pub chain: Chain,

/// Specify the chain network.
#[arg(long, value_name = "NETWORK", default_value = "mainnet")]
pub network: Network,

/// Specify the block execution strategy.
#[clap(long, value_enum, default_value_t = BlockExecution::RuntimeDisk)]
pub block_execution: BlockExecution,
Expand Down Expand Up @@ -116,7 +119,7 @@ impl CommonParams {
pub fn as_shared_params(&self) -> sc_cli::SharedParams {
// TODO: expose more flags?
sc_cli::SharedParams {
chain: Some(format!("{:?}", self.chain)),
chain: Some(self.chain.chain_spec_id().to_string()),
dev: false,
base_path: self.base_path.clone(),
log: self.log.clone(),
Expand All @@ -128,13 +131,12 @@ impl CommonParams {
}
}

/// Determines the Bitcoin network type based on the current chain and network settings.
#[allow(unused)]
/// Determines the Bitcoin network type based on the current chain setting.
pub fn bitcoin_network(&self) -> bitcoin::Network {
match (self.chain, self.network) {
(Chain::Bitcoin, Network::Mainnet) => bitcoin::Network::Bitcoin,
(Chain::Bitcoin, Network::Testnet) => bitcoin::Network::Testnet,
(Chain::Bitcoin, Network::Signet) => bitcoin::Network::Signet,
match self.chain {
Chain::BitcoinMainnet => bitcoin::Network::Bitcoin,
Chain::BitcoinTestnet => bitcoin::Network::Testnet,
Chain::BitcoinSignet => bitcoin::Network::Signet,
}
}

Expand Down
22 changes: 14 additions & 8 deletions crates/subcoin-node/src/substrate_cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use subcoin_service::ChainSpec;

const BITCOIN_MAINNET_CHAIN_SPEC: &str = include_str!("../res/chain-spec-raw-bitcoin-mainnet.json");

/// Fake CLI for satisfying the Substrate CLI interface.
///
/// Primarily for creating a Substrate runner.
Expand Down Expand Up @@ -29,13 +33,15 @@ impl sc_cli::SubstrateCli for SubstrateCli {
2024
}

fn load_spec(&self, _id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
// TODO: different chain spec for different bitcoin network.
// parse network from id
// The chain spec here does not impact the chain but only for showing the proper
// network type on explorer.
Ok(Box::new(subcoin_service::chain_spec::config(
bitcoin::Network::Bitcoin,
)?))
fn load_spec(&self, id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
let chain_spec = match id {
"bitcoin-mainnet" => ChainSpec::from_json_bytes(BITCOIN_MAINNET_CHAIN_SPEC.as_bytes())?,
"bitcoin-testnet" | "bitcoin-signet" => {
unimplemented!("Bitcoin testnet and signet are unsupported")
}
path => ChainSpec::from_json_file(std::path::PathBuf::from(path))?,
};

Ok(Box::new(chain_spec))
}
}

0 comments on commit 033aeaa

Please sign in to comment.