Skip to content

Commit 3f14a08

Browse files
authored
chore: move out the CLI module from the library (dfinity#3169)
CLIs should belong to the binary target
1 parent 2453254 commit 3f14a08

File tree

10 files changed

+39
-42
lines changed

10 files changed

+39
-42
lines changed

rs/bitcoin/adapter/BUILD.bazel

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test", "rust_test_suite")
22
load("//bazel:defs.bzl", "rust_bench")
33

4-
package(default_visibility = ["//visibility:private"])
5-
64
DEPENDENCIES = [
75
# Keep sorted.
86
"//rs/bitcoin/service",
@@ -12,7 +10,6 @@ DEPENDENCIES = [
1210
"//rs/monitoring/logger",
1311
"//rs/monitoring/metrics",
1412
"@crate_index//:bitcoin",
15-
"@crate_index//:clap",
1613
"@crate_index//:futures",
1714
"@crate_index//:hashlink",
1815
"@crate_index//:hex",
@@ -50,39 +47,45 @@ DEV_DEPENDENCIES = [
5047

5148
MACRO_DEV_DEPENDENCIES = []
5249

53-
ALIASES = {}
54-
5550
rust_library(
5651
name = "adapter",
57-
srcs = glob(["src/**"]),
58-
aliases = ALIASES,
52+
srcs = glob(
53+
["src/**"],
54+
exclude = [
55+
"src/main.rs",
56+
"src/cli.rs",
57+
"src/stress_test.rs",
58+
],
59+
),
5960
crate_name = "ic_btc_adapter",
6061
proc_macro_deps = MACRO_DEPENDENCIES,
61-
version = "0.1.0",
6262
visibility = ["//rs/pocket_ic_server:__subpackages__"],
6363
deps = DEPENDENCIES,
6464
)
6565

6666
rust_binary(
6767
name = "ic-btc-adapter",
68-
srcs = ["src/main.rs"],
69-
aliases = ALIASES,
68+
srcs = [
69+
"src/cli.rs",
70+
"src/main.rs",
71+
],
7072
proc_macro_deps = MACRO_DEPENDENCIES,
7173
visibility = ["//rs:release-pkg"],
7274
deps = DEPENDENCIES + [
7375
":adapter",
7476
"//rs/monitoring/adapter_metrics/server",
77+
"@crate_index//:clap",
7578
],
7679
)
7780

7881
rust_binary(
7982
name = "adapter-stress-test",
8083
srcs = ["src/stress_test.rs"],
81-
aliases = ALIASES,
8284
proc_macro_deps = MACRO_DEPENDENCIES,
8385
deps = DEPENDENCIES + [
8486
":adapter",
8587
"//rs/monitoring/adapter_metrics/server",
88+
"@crate_index//:clap",
8689
"@crate_index//:hyper-util",
8790
],
8891
)
@@ -102,7 +105,6 @@ rust_test_suite(
102105
name = "adapter_integration",
103106
timeout = "long",
104107
srcs = glob(["tests/**/*.rs"]),
105-
aliases = ALIASES,
106108
data = [
107109
# Keep sorted.
108110
"//:bitcoind",
@@ -127,7 +129,6 @@ rust_bench(
127129
name = "e2e_bench",
128130
testonly = True,
129131
srcs = ["benches/e2e.rs"],
130-
aliases = ALIASES,
131132
proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
132133
deps = DEPENDENCIES + DEV_DEPENDENCIES + [":adapter"],
133134
)

rs/bitcoin/adapter/benches/e2e.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use bitcoin::{
22
block::Header as BlockHeader, blockdata::constants::genesis_block, BlockHash, Network,
33
};
44
use criterion::{criterion_group, criterion_main, Criterion};
5-
use ic_btc_adapter::config::Config;
6-
use ic_btc_adapter::config::IncomingSource;
7-
use ic_btc_adapter::start_server;
5+
use ic_btc_adapter::{start_server, Config, IncomingSource};
86
use ic_btc_adapter_client::setup_bitcoin_adapter_clients;
97
use ic_btc_adapter_test_utils::generate_headers;
108
use ic_btc_replica_types::BitcoinAdapterRequestWrapper;

rs/bitcoin/adapter/src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A parser for the command line flags and configuration file.
2-
use crate::config::{address_limits, Config};
32
use clap::Parser;
43
use http::Uri;
4+
use ic_btc_adapter::{address_limits, Config};
55
use std::{fs::File, io, path::PathBuf};
66
use thiserror::Error;
77

@@ -54,7 +54,7 @@ impl Cli {
5454
#[cfg(test)]
5555
pub mod test {
5656
use super::*;
57-
use crate::config::IncomingSource;
57+
use crate::IncomingSource;
5858
use bitcoin::Network;
5959
use std::io::Write;
6060
use std::path::PathBuf;

rs/bitcoin/adapter/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn default_idle_seconds() -> u64 {
5858

5959
/// This function is used to get the address limits for the `AddressBook`
6060
/// based on the provided `Network`.
61-
pub(crate) fn address_limits(network: Network) -> (usize, usize) {
61+
pub fn address_limits(network: Network) -> (usize, usize) {
6262
match network {
6363
Network::Bitcoin => (500, 2000),
6464
Network::Testnet => (100, 1000),

rs/bitcoin/adapter/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ mod addressbook;
2525
mod blockchainmanager;
2626
/// This module contains the data structure for storing the current state of the Bitcoin ledger
2727
mod blockchainstate;
28-
/// This module contains command line arguments parser.
29-
pub mod cli;
3028
/// This module contains constants and types that are shared by many modules.
3129
mod common;
3230
/// This module contains the basic configuration struct used to start up an
@@ -53,6 +51,8 @@ mod transaction_store;
5351
// malicious fork can be prioritized by a DFS, thus potentially ignoring honest forks).
5452
mod get_successors_handler;
5553

54+
pub use config::{address_limits, Config, IncomingSource};
55+
5656
use crate::{
5757
blockchainstate::BlockchainState, get_successors_handler::GetSuccessorsHandler,
5858
router::start_main_event_loop, rpc_server::start_grpc_server, stream::StreamEvent,

rs/bitcoin/adapter/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
use clap::Parser;
22
use ic_adapter_metrics_server::start_metrics_grpc;
3-
use ic_btc_adapter::config::IncomingSource;
4-
use ic_btc_adapter::{cli::Cli, start_server};
3+
use ic_btc_adapter::{start_server, IncomingSource};
54
use ic_http_endpoints_async_utils::abort_on_panic;
65
use ic_http_endpoints_async_utils::incoming_from_nth_systemd_socket;
76
use ic_http_endpoints_async_utils::shutdown_signal;
87
use ic_logger::{info, new_replica_logger_from_config};
98
use ic_metrics::MetricsRegistry;
109
use serde_json::to_string_pretty;
1110

11+
mod cli;
12+
1213
#[tokio::main]
1314
pub async fn main() {
1415
// We abort the whole program with a core dump if a single thread panics.
1516
// This way we can capture all the context if a critical error
1617
// happens.
1718
abort_on_panic();
1819

19-
let cli = Cli::parse();
20+
let cli = cli::Cli::parse();
2021
let config = match cli.get_config() {
2122
Ok(config) => config,
2223
Err(err) => {

rs/bitcoin/adapter/src/rpc_server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::{
22
blockchainstate::BlockchainState,
3-
config::{Config, IncomingSource},
43
get_successors_handler::{GetSuccessorsRequest, GetSuccessorsResponse},
54
metrics::{ServiceMetrics, LABEL_GET_SUCCESSOR, LABEL_SEND_TRANSACTION},
6-
BlockchainManagerRequest, GetSuccessorsHandler, TransactionManagerRequest,
5+
BlockchainManagerRequest, Config, GetSuccessorsHandler, IncomingSource,
6+
TransactionManagerRequest,
77
};
88
use bitcoin::{consensus::Encodable, hashes::Hash, BlockHash};
99
use ic_btc_service::{

rs/bitcoin/adapter/src/stress_test.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{convert::TryFrom, path::PathBuf, time::Duration};
22

3+
use bitcoin::Network;
34
use bitcoin::{blockdata::constants::genesis_block, consensus::Decodable, Block, BlockHash};
45
use clap::Parser;
56
use ic_btc_service::{
@@ -13,8 +14,6 @@ use tokio::{
1314
use tonic::transport::{Channel, Endpoint, Uri};
1415
use tower::service_fn;
1516

16-
use ic_btc_adapter::{cli::Cli, config::IncomingSource};
17-
1817
async fn setup_channel(uds_path: PathBuf) -> Channel {
1918
Endpoint::try_from("http://[::]:50051")
2019
.expect("failed to make endpoint")
@@ -36,23 +35,26 @@ async fn setup_client(uds_path: PathBuf) -> BtcServiceClient<Channel> {
3635
BtcServiceClient::new(channel)
3736
}
3837

38+
/// This struct is use to provide a command line interface to the adapter.
39+
#[derive(Parser)]
40+
#[clap(version = "0.0.0", author = "DFINITY team <[email protected]>")]
41+
pub struct Cli {
42+
/// This field contains the path to the config file.
43+
pub network: Network,
44+
pub uds_path: PathBuf,
45+
}
46+
3947
#[tokio::main]
4048
async fn main() {
4149
let cli = Cli::parse();
42-
let config = cli.get_config().expect("Error while reading config file.");
43-
let uds_path = if let IncomingSource::Path(uds_path) = config.incoming_source {
44-
uds_path
45-
} else {
46-
panic!("Cannot use systemd as a incoming source.");
47-
};
4850
let interval_sleep_ms = Duration::from_millis(1000);
4951
let request_timeout_ms = Duration::from_millis(50);
5052

51-
let block_0 = genesis_block(config.network);
53+
let block_0 = genesis_block(cli.network);
5254
let mut total_processed_block_hashes: usize = 0;
5355
let mut processed_block_hashes: Vec<BlockHash> = vec![];
5456
let mut current_anchor = block_0.block_hash();
55-
let mut rpc_client = setup_client(uds_path).await;
57+
let mut rpc_client = setup_client(cli.uds_path).await;
5658
let total_timer = Instant::now();
5759

5860
loop {

rs/bitcoin/adapter/tests/adapter_test.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use bitcoin::{consensus::encode::deserialize, Address, Amount, Block, BlockHash};
22
use bitcoincore_rpc::{bitcoincore_rpc_json::CreateRawTransactionInput, Auth, Client, RpcApi};
33
use bitcoind::{BitcoinD, Conf, P2P};
4-
use ic_btc_adapter::{
5-
config::{Config, IncomingSource},
6-
start_server,
7-
};
4+
use ic_btc_adapter::{start_server, Config, IncomingSource};
85
use ic_btc_adapter_client::setup_bitcoin_adapter_clients;
96
use ic_btc_interface::Network;
107
use ic_btc_replica_types::{

rs/https_outcalls/adapter/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test", "rust_test_suite")
22

3-
package(default_visibility = ["//visibility:private"])
4-
53
DEPENDENCIES = [
64
# Keep sorted.
75
"//rs/config",

0 commit comments

Comments
 (0)