Skip to main content

fedimintd/
lib.rs

1#![deny(clippy::pedantic)]
2#![allow(clippy::cast_possible_wrap)]
3#![allow(clippy::missing_errors_doc)]
4#![allow(clippy::missing_panics_doc)]
5#![allow(clippy::must_use_candidate)]
6#![allow(clippy::return_self_not_must_use)]
7#![allow(clippy::large_futures)]
8
9mod metrics;
10
11use std::convert::Infallible;
12use std::env;
13use std::fmt::Write as _;
14use std::net::SocketAddr;
15use std::path::PathBuf;
16use std::time::Duration;
17
18use anyhow::Context as _;
19use bitcoin::Network;
20use clap::builder::BoolishValueParser;
21use clap::{ArgGroup, CommandFactory, FromArgMatches, Parser};
22use fedimint_core::db::Database;
23use fedimint_core::envs::{
24    FM_IROH_DNS_ENV, FM_IROH_RELAY_ENV, FM_USE_UNKNOWN_MODULE_ENV, is_env_var_set,
25    is_running_in_test_env,
26};
27use fedimint_core::module::registry::ModuleRegistry;
28use fedimint_core::module::{ApiAuth, CORE_CONSENSUS_VERSION};
29use fedimint_core::rustls::install_crypto_provider;
30use fedimint_core::task::TaskGroup;
31use fedimint_core::timing;
32use fedimint_core::util::{FmtCompactAnyhow as _, SafeUrl, handle_version_hash_command};
33use fedimint_ln_server::LightningInit;
34use fedimint_logging::{LOG_CORE, LOG_SERVER, TracingSetup};
35use fedimint_meta_server::MetaInit;
36use fedimint_mint_server::MintInit;
37use fedimint_rocksdb::RocksDb;
38use fedimint_server::IrohNextApiSettings;
39use fedimint_server::config::ConfigGenSettings;
40use fedimint_server::config::io::{DB_FILE, PLAINTEXT_PASSWORD};
41use fedimint_server::core::ServerModuleInitRegistry;
42use fedimint_server::net::api::ApiSecrets;
43use fedimint_server_bitcoin_rpc::BitcoindClientWithFallback;
44use fedimint_server_bitcoin_rpc::bitcoind::BitcoindClient;
45use fedimint_server_bitcoin_rpc::esplora::EsploraClient;
46use fedimint_server_bitcoin_rpc::tracked::ServerBitcoinRpcTracked;
47use fedimint_server_core::ServerModuleInitRegistryExt;
48use fedimint_server_core::bitcoin_rpc::IServerBitcoinRpc;
49use fedimint_unknown_server::UnknownInit;
50use fedimint_wallet_server::WalletInit;
51use fedimintd_envs::{
52    FM_API_URL_ENV, FM_BIND_API_ENV, FM_BIND_API_NEXT_ENV, FM_BIND_METRICS_ENV, FM_BIND_P2P_ENV,
53    FM_BIND_TOKIO_CONSOLE_ENV, FM_BIND_UI_ENV, FM_BITCOIN_NETWORK_ENV, FM_BITCOIND_PASSWORD_ENV,
54    FM_BITCOIND_URL_ENV, FM_BITCOIND_URL_PASSWORD_FILE_ENV, FM_BITCOIND_USERNAME_ENV,
55    FM_DATA_DIR_ENV, FM_DB_CHECKPOINT_RETENTION_ENV, FM_DISABLE_META_MODULE_ENV,
56    FM_ENABLE_IROH_ENV, FM_ESPLORA_URL_ENV, FM_FORCE_API_SECRETS_ENV,
57    FM_IROH_API_MAX_CONNECTIONS_ENV, FM_IROH_API_MAX_REQUESTS_PER_CONNECTION_ENV,
58    FM_IROH_NEXT_ENABLE_ENV, FM_IROH_P2P_RELAY_ENV, FM_P2P_MAX_CONNECTION_AGE_SECS_ENV,
59    FM_P2P_URL_ENV, FM_PASSWORD_API_ENV, FM_PASSWORD_UI_ENV, FM_SESSION_TIMEOUT_SECS_ENV,
60};
61use futures::FutureExt as _;
62#[cfg(all(
63    not(feature = "jemalloc"),
64    not(any(target_env = "msvc", target_os = "ios", target_os = "android"))
65))]
66use tracing::warn;
67use tracing::{debug, error, info};
68
69use crate::metrics::APP_START_TS;
70
71/// Time we will wait before forcefully shutting down tasks
72const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(10);
73
74#[derive(Parser)]
75#[command(version)]
76#[command(
77    group(
78        ArgGroup::new("bitcoind_password_auth")
79           .args(["bitcoind_password", "bitcoind_url_password_file"])
80           .multiple(false)
81    ),
82    group(
83        ArgGroup::new("bitcoind_auth")
84            .args(["bitcoind_url"])
85            .requires("bitcoind_password_auth")
86            .requires_all(["bitcoind_username", "bitcoind_url"])
87    ),
88    group(
89        ArgGroup::new("bitcoin_rpc")
90            .required(true)
91            .multiple(true)
92            .args(["bitcoind_url", "esplora_url"])
93    )
94)]
95struct ServerOpts {
96    /// Path to folder containing federation config files
97    #[arg(long = "data-dir", env = FM_DATA_DIR_ENV)]
98    data_dir: PathBuf,
99
100    /// Password gating the guardian admin UI on bind-ui. Optional: falls
101    /// back to reading password.private from data-dir, and if neither is
102    /// present the UI is served without a login form. Only safe when
103    /// bind-ui stays on a trusted interface (the default 127.0.0.1).
104    #[arg(long, env = FM_PASSWORD_UI_ENV)]
105    password_ui: Option<String>,
106
107    /// Password gating admin RPCs on the public API (WebSocket on bind-api
108    /// and iroh). Optional and never falls back to disk: when unset, admin
109    /// RPCs return 401 unconditionally, since the public API is always
110    /// network-reachable and must be enabled explicitly.
111    #[arg(long, env = FM_PASSWORD_API_ENV)]
112    password_api: Option<String>,
113
114    /// The bitcoin network of the federation
115    #[arg(long, env = FM_BITCOIN_NETWORK_ENV, default_value = "regtest")]
116    bitcoin_network: Network,
117
118    /// The username to use when connecting to bitcoind
119    #[arg(long, env = FM_BITCOIND_USERNAME_ENV)]
120    bitcoind_username: Option<String>,
121
122    /// The password to use when connecting to bitcoind
123    #[arg(long, env = FM_BITCOIND_PASSWORD_ENV)]
124    bitcoind_password: Option<String>,
125
126    /// Bitcoind RPC URL, e.g. <http://127.0.0.1:8332>
127    /// This should not include authentication parameters, they should be
128    /// included in `FM_BITCOIND_USERNAME` and `FM_BITCOIND_PASSWORD`
129    #[arg(long, env = FM_BITCOIND_URL_ENV)]
130    bitcoind_url: Option<SafeUrl>,
131
132    /// If set, the password part of `--bitcoind-url` will be set/replaced with
133    /// the content of this file.
134    ///
135    /// This is useful for setups that provide secret material via ramdisk
136    /// e.g. SOPS, age, etc.
137    ///
138    /// Note this is not meant to handle bitcoind's cookie file.
139    #[arg(long, env = FM_BITCOIND_URL_PASSWORD_FILE_ENV)]
140    bitcoind_url_password_file: Option<PathBuf>,
141
142    /// Esplora HTTP base URL, e.g. <https://mempool.space/api>
143    #[arg(long, env = FM_ESPLORA_URL_ENV)]
144    esplora_url: Option<SafeUrl>,
145
146    /// Address we bind to for p2p consensus communication
147    ///
148    /// Should be `0.0.0.0:8173` most of the time, as p2p connectivity is public
149    /// and direct, and the port should be open it in the firewall.
150    #[arg(long, env = FM_BIND_P2P_ENV, default_value = "0.0.0.0:8173")]
151    bind_p2p: SocketAddr,
152
153    /// Address we bind to for the API
154    ///
155    /// Should be `0.0.0.0:8174` most of the time, as api connectivity is public
156    /// and direct, and the port should be open it in the firewall.
157    #[arg(long, env = FM_BIND_API_ENV, default_value = "0.0.0.0:8174")]
158    bind_api: SocketAddr,
159
160    /// Address we bind to for exposing the Web UI
161    ///
162    /// Built-in web UI is exposed as an HTTP port, and typically should
163    /// have TLS terminated by Nginx/Traefik/etc. and forwarded to the locally
164    /// bind port.
165    #[arg(long, env = FM_BIND_UI_ENV, default_value = "127.0.0.1:8175")]
166    bind_ui: SocketAddr,
167
168    /// Our external address for communicating with our peers
169    ///
170    /// `fedimint://<fqdn>:8173` for TCP/TLS p2p connectivity (legacy/standard).
171    ///
172    /// Ignored when Iroh stack is used. (newer/experimental)
173    #[arg(long, env = FM_P2P_URL_ENV)]
174    p2p_url: Option<SafeUrl>,
175
176    /// Our API address for clients to connect to us
177    ///
178    /// Typically `wss://<fqdn>/ws/` for TCP/TLS connectivity (legacy/standard)
179    ///
180    /// Ignored when Iroh stack is used. (newer/experimental)
181    #[arg(long, env = FM_API_URL_ENV)]
182    api_url: Option<SafeUrl>,
183
184    /// Whether to use the Iroh networking stack instead of the legacy
185    /// TCP/TLS/websocket stack. Defaults to Iroh in production and to the
186    /// legacy stack in test environments (`cargo test` / `devimint`). Only
187    /// consulted at DKG / config generation time; existing federations keep the
188    /// stack baked into their config. Pass `--enable-iroh true/false` (or
189    /// `FM_ENABLE_IROH=true/false`) to override.
190    #[arg(long, env = FM_ENABLE_IROH_ENV, value_parser = BoolishValueParser::new())]
191    enable_iroh: Option<bool>,
192
193    /// Optional URL of the Iroh DNS server
194    #[arg(long, env = FM_IROH_DNS_ENV, requires = "enable_iroh")]
195    iroh_dns: Option<SafeUrl>,
196
197    /// Optional URLs of the Iroh relays to use for registering
198    #[arg(long, env = FM_IROH_RELAY_ENV, requires = "enable_iroh", value_delimiter = ',')]
199    iroh_relays: Vec<SafeUrl>,
200
201    /// Optional Iroh 1.0 relay URLs to use for guardian P2P
202    #[arg(long, env = FM_IROH_P2P_RELAY_ENV, value_delimiter = ',')]
203    iroh_p2p_relays: Vec<SafeUrl>,
204
205    /// Number of checkpoints from the current session to retain on disk
206    #[arg(long, env = FM_DB_CHECKPOINT_RETENTION_ENV, default_value = "1")]
207    db_checkpoint_retention: u64,
208
209    /// Exit the process if a consensus session is not completed within this
210    /// many seconds, relying on the process supervisor to restart fedimintd.
211    /// Restarting guardians has been observed to resolve stuck sessions.
212    #[arg(long, env = FM_SESSION_TIMEOUT_SECS_ENV, default_value = "3600")]
213    session_timeout_secs: u64,
214
215    /// Drop a p2p connection to a peer once it exceeds this age in seconds,
216    /// relying on the reconnection logic to re-establish it. Unset by
217    /// default, in which case connections are never dropped due to their age.
218    #[arg(long, env = FM_P2P_MAX_CONNECTION_AGE_SECS_ENV)]
219    p2p_max_connection_age_secs: Option<u64>,
220
221    /// Enable tokio console logging
222    #[arg(long, env = FM_BIND_TOKIO_CONSOLE_ENV)]
223    bind_tokio_console: Option<SocketAddr>,
224
225    /// Enable jaeger for tokio console logging
226    #[arg(long, default_value = "false")]
227    with_jaeger: bool,
228
229    /// Enable prometheus metrics
230    #[arg(long, env = FM_BIND_METRICS_ENV, default_value = "127.0.0.1:8176")]
231    bind_metrics: Option<SocketAddr>,
232
233    /// Comma separated list of API secrets.
234    ///
235    /// Setting it will enforce API authentication and make the Federation
236    /// "private".
237    ///
238    /// The first secret in the list is the "active" one that the peer will use
239    /// itself to connect to other peers. Any further one is accepted by
240    /// this peer, e.g. for the purposes of smooth rotation of secret
241    /// between users.
242    ///
243    /// Note that the value provided here will override any other settings
244    /// that the user might want to set via UI at runtime, etc.
245    /// In the future, managing secrets might be possible via Admin UI
246    /// and defaults will be provided via `FM_DEFAULT_API_SECRETS`.
247    #[arg(long, env = FM_FORCE_API_SECRETS_ENV, default_value = "")]
248    force_api_secrets: ApiSecrets,
249
250    /// Maximum number of concurrent Iroh API connections
251    #[arg(long = "iroh-api-max-connections", env = FM_IROH_API_MAX_CONNECTIONS_ENV, default_value = "1000")]
252    iroh_api_max_connections: usize,
253
254    /// Maximum number of parallel requests per Iroh API connection
255    #[arg(long = "iroh-api-max-requests-per-connection", env = FM_IROH_API_MAX_REQUESTS_PER_CONNECTION_ENV, default_value = "50")]
256    iroh_api_max_requests_per_connection: usize,
257
258    /// Enable the transitional Iroh 1.0 API endpoint alongside Iroh 0.35.
259    ///
260    /// For a federation configured with the legacy Iroh API, this is a runtime
261    /// setting independent of the DKG-only `--enable-iroh` option. It is
262    /// enabled by default. Once the endpoint is advertised, it must remain
263    /// enabled.
264    #[arg(
265        long,
266        env = FM_IROH_NEXT_ENABLE_ENV,
267        default_value_t = true,
268        action = clap::ArgAction::Set,
269        num_args = 0..=1,
270        default_missing_value = "true",
271    )]
272    enable_iroh_next: bool,
273
274    /// Bind address for the transitional Iroh 1.0 API endpoint
275    #[arg(long, env = FM_BIND_API_NEXT_ENV)]
276    bind_api_next: Option<SocketAddr>,
277}
278
279impl ServerOpts {
280    pub async fn get_bitcoind_url_and_password(&self) -> anyhow::Result<(SafeUrl, String)> {
281        let url = self
282            .bitcoind_url
283            .clone()
284            .ok_or_else(|| anyhow::anyhow!("No bitcoind url set"))?;
285        if let Some(password_file) = self.bitcoind_url_password_file.as_ref() {
286            let password = tokio::fs::read_to_string(password_file)
287                .await
288                .context("Failed to read the password")?
289                .trim()
290                .to_owned();
291            Ok((url, password))
292        } else {
293            let password = self
294                .bitcoind_password
295                .clone()
296                .expect("FM_BITCOIND_URL is set but FM_BITCOIND_PASSWORD is not");
297            Ok((url, password))
298        }
299    }
300}
301
302/// Block the thread and run a Fedimintd server
303///
304/// # Arguments
305///
306/// * `module_init_registry` - The registry of available modules.
307///
308/// * `code_version_hash` - The git hash of the code that the `fedimintd` binary
309///   is being built from. This is used mostly for information purposes
310///   (`fedimintd version-hash`). See `fedimint-build` crate for easy way to
311///   obtain it.
312///
313/// * `code_version_vendor_suffix` - An optional suffix that will be appended to
314///   the internal fedimint release version, to distinguish binaries built by
315///   different vendors, usually with a different set of modules. The suffix is
316///   informational in setup/DKG: compatibility and consensus config generation
317///   use the normalized `x.y.z` release version.
318#[allow(clippy::too_many_lines)]
319pub async fn run(
320    module_init_registry: ServerModuleInitRegistry,
321    code_version_hash: &str,
322    code_version_vendor_suffix: Option<&str>,
323) -> anyhow::Result<Infallible> {
324    assert_eq!(
325        env!("FEDIMINT_BUILD_CODE_VERSION").len(),
326        code_version_hash.len(),
327        "version_hash must have an expected length"
328    );
329
330    handle_version_hash_command(code_version_hash);
331
332    let fedimint_version = env!("CARGO_PKG_VERSION");
333
334    APP_START_TS
335        .with_label_values(&[fedimint_version, code_version_hash])
336        .set(fedimint_core::time::duration_since_epoch().as_secs() as i64);
337
338    let server_opts = {
339        // Collect env vars from all registered modules and append them to the
340        // long-help text so operators can discover them via `fedimintd --help`.
341        let mut module_env_help = String::from("\nModule environment variables:\n");
342        for (_kind, module_init) in module_init_registry.iter() {
343            for doc in module_init.get_documented_env_vars() {
344                let _ = writeln!(module_env_help, "  {:40}  {}", doc.name, doc.description);
345            }
346        }
347        let matches = ServerOpts::command()
348            .after_long_help(module_env_help)
349            .get_matches();
350        ServerOpts::from_arg_matches(&matches)
351            .expect("clap arg matches must be valid after parsing")
352    };
353
354    let mut tracing_builder = TracingSetup::default();
355
356    tracing_builder
357        .tokio_console_bind(server_opts.bind_tokio_console)
358        .with_jaeger(server_opts.with_jaeger);
359
360    tracing_builder.init().unwrap();
361
362    info!("Starting fedimintd (version: {fedimint_version} version_hash: {code_version_hash})");
363
364    #[cfg(all(
365        not(feature = "jemalloc"),
366        not(any(target_env = "msvc", target_os = "ios", target_os = "android"))
367    ))]
368    warn!(
369        target: LOG_SERVER,
370        "fedimintd was built without the `jemalloc` feature. rocksdb is prone to memory \
371         fragmentation with the default allocator; consider rebuilding with `--features jemalloc`."
372    );
373
374    debug!(
375        target: LOG_SERVER,
376        core_consensus = %CORE_CONSENSUS_VERSION,
377        "Supported core consensus version",
378    );
379
380    let code_version_str = code_version_vendor_suffix.map_or_else(
381        || fedimint_version.to_string(),
382        |suffix| format!("{fedimint_version}+{suffix}"),
383    );
384
385    let timing_total_runtime = timing::TimeReporter::new("total-runtime").info();
386
387    let root_task_group = TaskGroup::new();
388
389    if let Some(bind_metrics) = server_opts.bind_metrics.as_ref() {
390        info!(
391            target: LOG_SERVER,
392            url = %format!("http://{}/metrics", bind_metrics),
393            "Initializing metrics server",
394        );
395        fedimint_metrics::spawn_api_server(*bind_metrics, root_task_group.clone()).await?;
396    }
397
398    let enable_iroh = server_opts.enable_iroh.unwrap_or(!is_running_in_test_env());
399    let iroh_next_api_settings = if server_opts.enable_iroh_next {
400        Some(IrohNextApiSettings::new(server_opts.bind_api_next))
401    } else {
402        None
403    };
404
405    let settings = ConfigGenSettings {
406        p2p_bind: server_opts.bind_p2p,
407        api_bind: server_opts.bind_api,
408        ui_bind: server_opts.bind_ui,
409        p2p_url: server_opts.p2p_url.clone(),
410        api_url: server_opts.api_url.clone(),
411        enable_iroh,
412        iroh_dns: server_opts.iroh_dns.clone(),
413        iroh_relays: server_opts.iroh_relays.clone(),
414        network: server_opts.bitcoin_network,
415        available_modules: module_init_registry.kinds(),
416        default_modules: module_init_registry.default_modules(),
417    };
418
419    let db = Database::new(
420        RocksDb::build(server_opts.data_dir.join(DB_FILE))
421            .open()
422            .await
423            .unwrap(),
424        ModuleRegistry::default(),
425    );
426
427    let dyn_server_bitcoin_rpc = match (
428        server_opts.bitcoind_url.as_ref(),
429        server_opts.esplora_url.as_ref(),
430    ) {
431        (Some(_), None) => {
432            let bitcoind_username = server_opts
433                .bitcoind_username
434                .clone()
435                .expect("FM_BITCOIND_URL is set but FM_BITCOIND_USERNAME is not");
436            let (bitcoind_url, bitcoind_password) = server_opts
437                .get_bitcoind_url_and_password()
438                .await
439                .expect("Failed to get bitcoind url");
440            BitcoindClient::new(bitcoind_username, bitcoind_password, &bitcoind_url)
441                .unwrap()
442                .into_dyn()
443        }
444        (None, Some(url)) => EsploraClient::new(url).unwrap().into_dyn(),
445        (Some(_), Some(esplora_url)) => {
446            let bitcoind_username = server_opts
447                .bitcoind_username
448                .clone()
449                .expect("FM_BITCOIND_URL is set but FM_BITCOIND_USERNAME is not");
450            let (bitcoind_url, bitcoind_password) = server_opts
451                .get_bitcoind_url_and_password()
452                .await
453                .expect("Failed to get bitcoind url");
454            BitcoindClientWithFallback::new(
455                bitcoind_username,
456                bitcoind_password,
457                &bitcoind_url,
458                esplora_url,
459            )
460            .unwrap()
461            .into_dyn()
462        }
463        _ => unreachable!("ArgGroup already enforced XOR relation"),
464    };
465    let dyn_server_bitcoin_rpc =
466        ServerBitcoinRpcTracked::new(dyn_server_bitcoin_rpc, "server").into_dyn();
467
468    root_task_group.install_kill_handler();
469
470    install_crypto_provider().await;
471
472    // The UI password falls back to the legacy password.private file on disk
473    // if its env var is not set, since the UI defaults to a loopback bind.
474    // The API password never falls back: the public admin API is always
475    // network-reachable, so it must be enabled explicitly via its env var or
476    // else admin RPCs return 401. Absence of a password for a given plane opts
477    // that plane into passwordless mode (UI) or disabled admin RPCs (API).
478    let password_file = std::fs::read_to_string(server_opts.data_dir.join(PLAINTEXT_PASSWORD))
479        .ok()
480        .map(|s| s.trim().to_owned());
481
482    let auth_ui = server_opts.password_ui.or(password_file).map(ApiAuth::new);
483    let auth_api = server_opts.password_api.map(ApiAuth::new);
484
485    let task_group = root_task_group.clone();
486    let code_version_hash = code_version_hash.to_string();
487    root_task_group.spawn_cancellable("main", async move {
488        fedimint_server::run_with_iroh_p2p_relays_and_next_api(
489            server_opts.data_dir,
490            auth_ui,
491            auth_api,
492            server_opts.force_api_secrets,
493            settings,
494            db,
495            code_version_str,
496            code_version_hash,
497            module_init_registry,
498            task_group,
499            dyn_server_bitcoin_rpc,
500            Box::new(fedimint_server_ui::setup::router),
501            Box::new(fedimint_server_ui::dashboard::router),
502            server_opts.db_checkpoint_retention,
503            Duration::from_secs(server_opts.session_timeout_secs),
504            server_opts
505                .p2p_max_connection_age_secs
506                .map(Duration::from_secs),
507            fedimint_server::ConnectionLimits::new(
508                server_opts.iroh_api_max_connections,
509                server_opts.iroh_api_max_requests_per_connection,
510            ),
511            server_opts.iroh_p2p_relays,
512            iroh_next_api_settings,
513        )
514        .await
515        .unwrap_or_else(|err| panic!("Main task returned error: {}", err.fmt_compact_anyhow()));
516    });
517
518    let shutdown_future = root_task_group
519        .make_handle()
520        .make_shutdown_rx()
521        .then(|()| async {
522            info!(target: LOG_CORE, "Shutdown called");
523        });
524
525    shutdown_future.await;
526
527    debug!(target: LOG_CORE, "Terminating main task");
528
529    if let Err(err) = root_task_group.join_all(Some(SHUTDOWN_TIMEOUT)).await {
530        error!(target: LOG_CORE, err = %err.fmt_compact_anyhow(), "Error while shutting down task group");
531    }
532
533    debug!(target: LOG_CORE, "Shutdown complete");
534
535    fedimint_logging::shutdown();
536
537    drop(timing_total_runtime);
538
539    std::process::exit(-1);
540}
541
542pub fn default_modules() -> ServerModuleInitRegistry {
543    let mut server_gens = ServerModuleInitRegistry::new();
544
545    server_gens.attach(MintInit);
546    server_gens.attach(fedimint_mintv2_server::MintInit);
547
548    server_gens.attach(WalletInit);
549    server_gens.attach(fedimint_walletv2_server::WalletInit);
550
551    server_gens.attach(LightningInit);
552    server_gens.attach(fedimint_lnv2_server::LightningInit);
553
554    if !is_env_var_set(FM_DISABLE_META_MODULE_ENV) {
555        server_gens.attach(MetaInit);
556    }
557
558    if is_env_var_set(FM_USE_UNKNOWN_MODULE_ENV) {
559        server_gens.attach(UnknownInit);
560    }
561
562    server_gens
563}
564
565#[cfg(test)]
566mod tests {
567    use super::*;
568
569    fn server_opts_args() -> Vec<&'static str> {
570        vec![
571            "fedimintd",
572            "--data-dir",
573            "/tmp/fedimintd-test",
574            "--bitcoind-url",
575            "http://127.0.0.1:18443",
576            "--bitcoind-username",
577            "user",
578            "--bitcoind-password",
579            "pass",
580        ]
581    }
582
583    fn parse_server_opts() -> ServerOpts {
584        ServerOpts::try_parse_from(server_opts_args()).expect("server opts should parse")
585    }
586
587    fn parse_server_opts_with_enable_iroh_env(value: &str) -> ServerOpts {
588        let previous = std::env::var_os(FM_ENABLE_IROH_ENV);
589        // This test does not spawn threads while mutating the process
590        // environment.
591        unsafe {
592            std::env::set_var(FM_ENABLE_IROH_ENV, value);
593        }
594
595        let opts = parse_server_opts();
596
597        // This test does not spawn threads while mutating the process
598        // environment.
599        unsafe {
600            if let Some(previous) = previous {
601                std::env::set_var(FM_ENABLE_IROH_ENV, previous);
602            } else {
603                std::env::remove_var(FM_ENABLE_IROH_ENV);
604            }
605        }
606
607        opts
608    }
609
610    #[test]
611    fn enable_iroh_env_accepts_numeric_booleans() {
612        assert_eq!(
613            parse_server_opts_with_enable_iroh_env("1").enable_iroh,
614            Some(true)
615        );
616        assert_eq!(
617            parse_server_opts_with_enable_iroh_env("0").enable_iroh,
618            Some(false)
619        );
620    }
621
622    #[test]
623    fn iroh_next_api_defaults_to_enabled_and_accepts_false() {
624        let command = ServerOpts::command();
625        let enable_iroh_next = command
626            .get_arguments()
627            .find(|arg| arg.get_id() == "enable_iroh_next")
628            .expect("enable-iroh-next argument exists");
629        assert_eq!(enable_iroh_next.get_default_values(), ["true"]);
630
631        let mut args = server_opts_args();
632        args.push("--enable-iroh-next=false");
633        let opts = ServerOpts::try_parse_from(args).expect("explicit false should parse");
634        assert!(!opts.enable_iroh_next);
635    }
636
637    #[test]
638    fn p2p_relay_does_not_require_enable_iroh_or_change_api_relays() {
639        let opts = ServerOpts::try_parse_from([
640            "fedimintd",
641            "--data-dir",
642            "/tmp/fedimintd-test",
643            "--bitcoind-url",
644            "http://127.0.0.1:18443",
645            "--bitcoind-username",
646            "user",
647            "--bitcoind-password",
648            "pass",
649            "--iroh-p2p-relays",
650            "https://relay.example.com/",
651        ])
652        .expect("P2P relay should parse independently");
653
654        assert!(opts.iroh_relays.is_empty());
655        assert_eq!(opts.iroh_p2p_relays.len(), 1);
656    }
657}