Skip to main content

dfx_core/config/model/
bitcoin_adapter.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3use std::net::{IpAddr, Ipv4Addr, SocketAddr};
4use std::path::PathBuf;
5use std::str::FromStr;
6
7const BITCOIND_REGTEST_DEFAULT_PORT: u16 = 18444;
8
9pub fn default_nodes() -> Vec<SocketAddr> {
10    vec![SocketAddr::new(
11        IpAddr::V4(Ipv4Addr::LOCALHOST),
12        BITCOIND_REGTEST_DEFAULT_PORT,
13    )]
14}
15
16// These definitions come from https://gitlab.com/dfinity-lab/public/ic/-/blob/master/rs/bitcoin/adapter/src/config.rs
17#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Default)]
18/// The source of the unix domain socket to be used for inter-process
19/// communication.
20pub enum IncomingSource {
21    /// We use systemd's created socket.
22    #[default]
23    Systemd,
24    /// We use the corresponing path as socket.
25    Path(PathBuf),
26}
27
28/// Represents the log level of the bitcoin adapter.
29#[derive(Clone, Debug, Serialize, Deserialize, Copy, PartialEq, Eq, JsonSchema, Default)]
30#[serde(rename_all = "snake_case")]
31pub enum BitcoinAdapterLogLevel {
32    Critical,
33    Error,
34    Warning,
35    #[default]
36    Info,
37    Debug,
38    Trace,
39}
40
41impl FromStr for BitcoinAdapterLogLevel {
42    type Err = String;
43
44    fn from_str(input: &str) -> Result<BitcoinAdapterLogLevel, Self::Err> {
45        match input {
46            "critical" => Ok(BitcoinAdapterLogLevel::Critical),
47            "error" => Ok(BitcoinAdapterLogLevel::Error),
48            "warning" => Ok(BitcoinAdapterLogLevel::Warning),
49            "info" => Ok(BitcoinAdapterLogLevel::Info),
50            "debug" => Ok(BitcoinAdapterLogLevel::Debug),
51            "trace" => Ok(BitcoinAdapterLogLevel::Trace),
52            other => Err(format!("Unknown log level: {other}")),
53        }
54    }
55}
56
57#[derive(Clone, Debug, Deserialize, Serialize)]
58pub struct LoggerConfig {
59    level: BitcoinAdapterLogLevel,
60}
61
62/// This struct contains configuration options for the BTC Adapter.
63#[derive(Clone, Debug, Deserialize, Serialize)]
64pub struct Config {
65    /// The type of Bitcoin network we plan to communicate to (e.g. "mainnet", "testnet", "regtest", etc.).
66    pub network: String,
67    /// Addresses of nodes to connect to (in case discovery from seeds is not possible/sufficient)
68    #[serde(default)]
69    pub nodes: Vec<SocketAddr>,
70    /// Specifies which unix domain socket should be used for serving incoming requests.
71    #[serde(default)]
72    pub incoming_source: IncomingSource,
73
74    pub logger: LoggerConfig,
75}
76
77impl Config {
78    pub fn new(
79        nodes: Vec<SocketAddr>,
80        uds_path: PathBuf,
81        log_level: BitcoinAdapterLogLevel,
82    ) -> Config {
83        Config {
84            network: String::from("regtest"),
85            nodes,
86            incoming_source: IncomingSource::Path(uds_path),
87            logger: LoggerConfig { level: log_level },
88        }
89    }
90
91    pub fn get_socket_path(&self) -> Option<PathBuf> {
92        match &self.incoming_source {
93            IncomingSource::Systemd => None,
94            IncomingSource::Path(path) => Some(path.clone()),
95        }
96    }
97}