Skip to content

Commit

Permalink
Derive clap::ValueEnum under cli feature
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Jul 10, 2024
1 parent b569d15 commit ca75329
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 37 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/sc-consensus-nakamoto/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 }
clap = { workspace = true, optional = true }
codec = { workspace = true }
futures = { workspace = true }
hex-literal = { workspace = true }
Expand All @@ -25,3 +26,7 @@ sp-state-machine = { workspace = true }
subcoin-primitives = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }

[features]
default = []
cli = ["clap"]
18 changes: 1 addition & 17 deletions crates/sc-consensus-nakamoto/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use sc_client_api::{AuxStore, Backend, StorageProvider};
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::Block as BlockT;
use std::marker::PhantomData;
use std::str::FromStr;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use subcoin_primitives::runtime::bitcoin_block_subsidy;
use subcoin_primitives::{BackendExt, CoinStorageKey};

/// Represents the level of block verification.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
pub enum BlockVerification {
/// No verification performed.
None,
Expand All @@ -23,22 +23,6 @@ pub enum BlockVerification {
HeaderOnly,
}

impl FromStr for BlockVerification {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"none" => Ok(Self::None),
"full" => Ok(Self::Full),
"header-only" => Ok(Self::HeaderOnly),
_ => Err(format!(
"Invalid block verification value: {s}, \
possible values: [\"none\", \"full\", \"header-only\"]"
)),
}
}
}

/// Block verification error.
#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand Down
5 changes: 5 additions & 0 deletions crates/subcoin-network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license.workspace = true
async-trait = { workspace = true }
bitcoin = { workspace = true }
chrono = { workspace = true }
clap = { workspace = true, optional = true }
fastrand = { workspace = true }
futures = { workspace = true }
indexmap = { workspace = true }
Expand All @@ -35,3 +36,7 @@ tracing = { workspace = true }
sp-tracing = { workspace = true }
subcoin-service = { workspace = true }
subcoin-test-service = { workspace = true }

[features]
default = []
cli = ["clap"]
17 changes: 1 addition & 16 deletions crates/subcoin-network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ use serde::{Deserialize, Serialize};
use sp_runtime::traits::Block as BlockT;
use std::marker::PhantomData;
use std::net::{AddrParseError, SocketAddr, ToSocketAddrs};
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, AtomicU64};
use std::sync::Arc;
use tokio::net::TcpListener;
Expand Down Expand Up @@ -149,6 +148,7 @@ fn seednodes(network: BitcoinNetwork) -> Vec<&'static str> {

/// Represents the strategy for block syncing.
#[derive(Debug, Clone, Copy, Default)]
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
pub enum SyncStrategy {
/// Download the headers first, followed by the block bodies.
#[default]
Expand All @@ -157,21 +157,6 @@ pub enum SyncStrategy {
BlocksFirst,
}

impl FromStr for SyncStrategy {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"blocks-first" => Ok(Self::BlocksFirst),
"headers-first" => Ok(Self::HeadersFirst),
_ => Err(format!(
"Invalid sync strategy value: {s}, \
possible values: [\"blocks-first\", \"headers-first\"]"
)),
}
}
}

// Ignore the peer if it is not full with witness enabled as we only want to
// download from peers that can provide use full witness data for blocks.
fn validate_outbound_services(services: ServiceFlags) -> Result<(), Error> {
Expand Down
4 changes: 2 additions & 2 deletions crates/subcoin-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pallet-bitcoin = { workspace = true }
sc-cli = { workspace = true }
sc-client-api = { workspace = true }
sc-consensus = { workspace = true }
sc-consensus-nakamoto = { workspace = true }
sc-consensus-nakamoto = { workspace = true, features = ["cli"] }
sc-executor = { workspace = true }
sc-informant = { workspace = true }
sc-network = { workspace = true }
Expand All @@ -44,7 +44,7 @@ sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
subcoin-informant = { workspace = true }
subcoin-network = { workspace = true }
subcoin-network = { workspace = true, features = ["cli"] }
subcoin-primitives = { workspace = true }
subcoin-rpc = { workspace = true }
subcoin-runtime = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/subcoin-node/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use subcoin_network::SyncStrategy;
#[derive(Debug, Clone, Parser)]
pub struct Run {
/// Specify the major sync strategy.
#[clap(long, value_parser = clap::value_parser!(SyncStrategy))]
#[clap(long, default_value = "headers-first")]
pub sync_strategy: SyncStrategy,

/// Specify the block verification level.
#[clap(long, value_parser = clap::value_parser!(BlockVerification), default_value = "full")]
#[clap(long, default_value = "full")]
pub block_verification: BlockVerification,

/// Do not run the finalizer which will finalize the blocks on confirmation depth.
Expand Down

0 comments on commit ca75329

Please sign in to comment.