gateway_tests/
main.rs

1#![deny(clippy::pedantic)]
2
3use std::collections::BTreeMap;
4use std::fs::{remove_dir_all, remove_file};
5use std::ops::ControlFlow;
6use std::path::PathBuf;
7use std::str::FromStr;
8use std::time::Duration;
9use std::{env, ffi};
10
11use clap::{Parser, Subcommand};
12use devimint::cli::cleanup_on_exit;
13use devimint::envs::FM_DATA_DIR_ENV;
14use devimint::external::{Bitcoind, Esplora};
15use devimint::federation::Federation;
16use devimint::util::{ProcessManager, almost_equal, poll, poll_with_timeout};
17use devimint::version_constants::{VERSION_0_7_0_ALPHA, VERSION_0_8_0_ALPHA};
18use devimint::{Gatewayd, LightningNode, cli, cmd, util};
19use fedimint_core::config::FederationId;
20use fedimint_core::time::now;
21use fedimint_core::{Amount, BitcoinAmountOrAll, bitcoin, default_esplora_server};
22use fedimint_gateway_common::{
23    FederationInfo, GatewayBalances, GatewayFedConfig, PaymentDetails, PaymentKind, PaymentStatus,
24};
25use fedimint_logging::LOG_TEST;
26use fedimint_testing_core::node_type::LightningNodeType;
27use itertools::Itertools;
28use tracing::info;
29
30#[derive(Parser)]
31struct GatewayTestOpts {
32    #[clap(subcommand)]
33    test: GatewayTest,
34}
35
36#[derive(Debug, Clone, Subcommand)]
37enum GatewayTest {
38    ConfigTest {
39        #[arg(long = "gw-type")]
40        gateway_type: LightningNodeType,
41    },
42    GatewaydMnemonic {
43        #[arg(long)]
44        old_gatewayd_path: PathBuf,
45        #[arg(long)]
46        new_gatewayd_path: PathBuf,
47        #[arg(long)]
48        old_gateway_cli_path: PathBuf,
49        #[arg(long)]
50        new_gateway_cli_path: PathBuf,
51    },
52    BackupRestoreTest,
53    LiquidityTest,
54    EsploraTest,
55}
56
57#[tokio::main]
58async fn main() -> anyhow::Result<()> {
59    let opts = GatewayTestOpts::parse();
60    match opts.test {
61        GatewayTest::ConfigTest { gateway_type } => Box::pin(config_test(gateway_type)).await,
62        GatewayTest::GatewaydMnemonic {
63            old_gatewayd_path,
64            new_gatewayd_path,
65            old_gateway_cli_path,
66            new_gateway_cli_path,
67        } => {
68            mnemonic_upgrade_test(
69                old_gatewayd_path,
70                new_gatewayd_path,
71                old_gateway_cli_path,
72                new_gateway_cli_path,
73            )
74            .await
75        }
76        GatewayTest::BackupRestoreTest => Box::pin(backup_restore_test()).await,
77        GatewayTest::LiquidityTest => Box::pin(liquidity_test()).await,
78        GatewayTest::EsploraTest => esplora_test().await,
79    }
80}
81
82async fn backup_restore_test() -> anyhow::Result<()> {
83    Box::pin(
84        devimint::run_devfed_test().call(|dev_fed, process_mgr| async move {
85            let gw = if devimint::util::supports_lnv2() {
86                dev_fed.gw_ldk_connected().await?
87            } else {
88                dev_fed.gw_lnd_registered().await?
89            };
90
91            let fed = dev_fed.fed().await?;
92            fed.pegin_gateways(10_000_000, vec![gw]).await?;
93
94            let mnemonic = gw.get_mnemonic().await?.mnemonic;
95
96            // Recover without a backup
97            info!(target: LOG_TEST, "Wiping gateway and recovering without a backup...");
98            let ln = gw.ln.clone();
99            let new_gw = stop_and_recover_gateway(
100                process_mgr.clone(),
101                mnemonic.clone(),
102                gw.to_owned(),
103                ln.clone(),
104                fed,
105            )
106            .await?;
107
108            // Recover with a backup
109            info!(target: LOG_TEST, "Wiping gateway and recovering with a backup...");
110            info!(target: LOG_TEST, "Creating backup...");
111            new_gw.backup_to_fed(fed).await?;
112            stop_and_recover_gateway(process_mgr, mnemonic, new_gw, ln, fed).await?;
113
114            info!(target: LOG_TEST, "backup_restore_test successful");
115            Ok(())
116        }),
117    )
118    .await
119}
120
121async fn stop_and_recover_gateway(
122    process_mgr: ProcessManager,
123    mnemonic: Vec<String>,
124    old_gw: Gatewayd,
125    new_ln: LightningNode,
126    fed: &Federation,
127) -> anyhow::Result<Gatewayd> {
128    let gateway_balances =
129        serde_json::from_value::<GatewayBalances>(cmd!(old_gw, "get-balances").out_json().await?)?;
130    let before_onchain_balance = gateway_balances.onchain_balance_sats;
131
132    // Stop the Gateway
133    let gw_type = old_gw.ln.ln_type();
134    let gw_name = old_gw.gw_name.clone();
135    old_gw.terminate().await?;
136    info!(target: LOG_TEST, "Terminated Gateway");
137
138    // Delete the gateway's database
139    let data_dir: PathBuf = env::var(FM_DATA_DIR_ENV)
140        .expect("Data dir is not set")
141        .parse()
142        .expect("Could not parse data dir");
143    let gw_db = data_dir.join(gw_name.clone()).join("gatewayd.db");
144    if gw_db.is_file() {
145        // db is single file on redb
146        remove_file(gw_db)?;
147    } else {
148        remove_dir_all(gw_db)?;
149    }
150    info!(target: LOG_TEST, "Deleted the Gateway's database");
151
152    if gw_type == LightningNodeType::Ldk {
153        // Delete LDK's database as well
154        let ldk_data_dir = data_dir.join(gw_name).join("ldk_node");
155        remove_dir_all(ldk_data_dir)?;
156        info!(target: LOG_TEST, "Deleted LDK's database");
157    }
158
159    let seed = mnemonic.join(" ");
160    // TODO: Audit that the environment access only happens in single-threaded code.
161    unsafe { std::env::set_var("FM_GATEWAY_MNEMONIC", seed) };
162    let new_gw = Gatewayd::new(&process_mgr, new_ln).await?;
163    let new_mnemonic = new_gw.get_mnemonic().await?.mnemonic;
164    assert_eq!(mnemonic, new_mnemonic);
165    info!(target: LOG_TEST, "Verified mnemonic is the same after creating new Gateway");
166
167    let federations = serde_json::from_value::<Vec<FederationInfo>>(
168        new_gw.get_info().await?["federations"].clone(),
169    )?;
170    assert_eq!(0, federations.len());
171    info!(target: LOG_TEST, "Verified new Gateway has no federations");
172
173    new_gw.recover_fed(fed).await?;
174
175    let gateway_balances =
176        serde_json::from_value::<GatewayBalances>(cmd!(new_gw, "get-balances").out_json().await?)?;
177    let ecash_balance = gateway_balances
178        .ecash_balances
179        .first()
180        .expect("Should have one joined federation");
181    almost_equal(
182        ecash_balance.ecash_balance_msats.sats_round_down(),
183        10_000_000,
184        10,
185    )
186    .unwrap();
187    let after_onchain_balance = gateway_balances.onchain_balance_sats;
188    assert_eq!(before_onchain_balance, after_onchain_balance);
189    info!(target: LOG_TEST, "Verified balances after recovery");
190
191    Ok(new_gw)
192}
193
194/// TODO(v0.5.0): We do not need to run the `gatewayd-mnemonic` test from v0.4.0
195/// -> v0.5.0 over and over again. Once we have verified this test passes for
196/// v0.5.0, it can safely be removed.
197async fn mnemonic_upgrade_test(
198    old_gatewayd_path: PathBuf,
199    new_gatewayd_path: PathBuf,
200    old_gateway_cli_path: PathBuf,
201    new_gateway_cli_path: PathBuf,
202) -> anyhow::Result<()> {
203    // TODO: Audit that the environment access only happens in single-threaded code.
204    unsafe { std::env::set_var("FM_GATEWAYD_BASE_EXECUTABLE", old_gatewayd_path) };
205    // TODO: Audit that the environment access only happens in single-threaded code.
206    unsafe { std::env::set_var("FM_GATEWAY_CLI_BASE_EXECUTABLE", old_gateway_cli_path) };
207    // TODO: Audit that the environment access only happens in single-threaded code.
208    unsafe { std::env::set_var("FM_ENABLE_MODULE_LNV2", "0") };
209
210    devimint::run_devfed_test()
211        .call(|dev_fed, process_mgr| async move {
212            let gatewayd_version = util::Gatewayd::version_or_default().await;
213            let gateway_cli_version = util::GatewayCli::version_or_default().await;
214            info!(
215                target: LOG_TEST,
216                gatewayd_version = %gatewayd_version,
217                gateway_cli_version = %gateway_cli_version,
218                "Running gatewayd mnemonic test"
219            );
220
221            let mut gw_lnd = dev_fed.gw_lnd_registered().await?.to_owned();
222            let fed = dev_fed.fed().await?;
223            let federation_id = FederationId::from_str(fed.calculate_federation_id().as_str())?;
224
225            gw_lnd
226                .restart_with_bin(&process_mgr, &new_gatewayd_path, &new_gateway_cli_path)
227                .await?;
228
229            // Verify that we have a legacy federation
230            let mnemonic_response = gw_lnd.get_mnemonic().await?;
231            assert!(
232                mnemonic_response
233                    .legacy_federations
234                    .contains(&federation_id)
235            );
236
237            info!(target: LOG_TEST, "Verified a legacy federation exists");
238
239            // Leave federation
240            gw_lnd.leave_federation(federation_id).await?;
241
242            // Rejoin federation
243            gw_lnd.connect_fed(fed).await?;
244
245            // Verify that the legacy federation is recognized
246            let mnemonic_response = gw_lnd.get_mnemonic().await?;
247            assert!(
248                mnemonic_response
249                    .legacy_federations
250                    .contains(&federation_id)
251            );
252            assert_eq!(mnemonic_response.legacy_federations.len(), 1);
253
254            info!(target: LOG_TEST, "Verified leaving and re-joining preservers legacy federation");
255
256            // Leave federation and delete database to force migration to mnemonic
257            gw_lnd.leave_federation(federation_id).await?;
258
259            let data_dir: PathBuf = env::var(FM_DATA_DIR_ENV)
260                .expect("Data dir is not set")
261                .parse()
262                .expect("Could not parse data dir");
263            let gw_fed_db = data_dir
264                .join(gw_lnd.gw_name.clone())
265                .join(format!("{federation_id}.db"));
266            remove_dir_all(gw_fed_db)?;
267
268            gw_lnd.connect_fed(fed).await?;
269
270            // Verify that the re-connected federation is not a legacy federation
271            let mnemonic_response = gw_lnd.get_mnemonic().await?;
272            assert!(
273                !mnemonic_response
274                    .legacy_federations
275                    .contains(&federation_id)
276            );
277            assert_eq!(mnemonic_response.legacy_federations.len(), 0);
278
279            info!(target: LOG_TEST, "Verified deleting database will migrate the federation to use mnemonic");
280
281            info!(target: LOG_TEST, "Successfully completed mnemonic upgrade test");
282
283            Ok(())
284        })
285        .await
286}
287
288/// Test that sets and verifies configurations within the gateway
289#[allow(clippy::too_many_lines)]
290async fn config_test(gw_type: LightningNodeType) -> anyhow::Result<()> {
291    Box::pin(
292        devimint::run_devfed_test()
293            .num_feds(2)
294            .call(|dev_fed, process_mgr| async move {
295                let gw = match gw_type {
296                    LightningNodeType::Lnd => dev_fed.gw_lnd_registered().await?,
297                    LightningNodeType::Ldk => dev_fed.gw_ldk_connected().await?,
298                };
299
300                // Try to connect to already connected federation
301                let invite_code = dev_fed.fed().await?.invite_code()?;
302                let output = cmd!(gw, "connect-fed", invite_code.clone())
303                    .out_json()
304                    .await;
305                assert!(
306                    output.is_err(),
307                    "Connecting to the same federation succeeded"
308                );
309                info!(target: LOG_TEST, "Verified that gateway couldn't connect to already connected federation");
310
311                let gatewayd_version = util::Gatewayd::version_or_default().await;
312
313                // Change the routing fees for a specific federation
314                let fed_id = dev_fed.fed().await?.calculate_federation_id();
315                gw.set_federation_routing_fee(fed_id.clone(), 20, 20000)
316                    .await?;
317
318                let lightning_fee = gw.get_lightning_fee(fed_id.clone()).await?;
319                assert_eq!(
320                    lightning_fee.base.msats, 20,
321                    "Federation base msat is not 20"
322                );
323                assert_eq!(
324                    lightning_fee.parts_per_million, 20000,
325                    "Federation proportional millionths is not 20000"
326                );
327                info!(target: LOG_TEST, "Verified per-federation routing fees changed");
328
329                let info_value = cmd!(gw, "info").out_json().await?;
330                let federations = info_value["federations"]
331                    .as_array()
332                    .expect("federations is an array");
333                assert_eq!(
334                    federations.len(),
335                    1,
336                    "Gateway did not have one connected federation"
337                );
338
339                // Get the federation's config and verify it parses correctly
340                let config_val = cmd!(gw, "cfg", "client-config", "--federation-id", fed_id)
341                    .out_json()
342                    .await?;
343
344                serde_json::from_value::<GatewayFedConfig>(config_val)?;
345
346                // Spawn new federation
347                let bitcoind = dev_fed.bitcoind().await?;
348                let new_fed = Federation::new(
349                    &process_mgr,
350                    bitcoind.clone(),
351                    false,
352                    false,
353                    1,
354                    "config-test".to_string(),
355                )
356                .await?;
357                let new_fed_id = new_fed.calculate_federation_id();
358                info!(target: LOG_TEST, "Successfully spawned new federation");
359
360                let new_invite_code = new_fed.invite_code()?;
361                cmd!(gw, "connect-fed", new_invite_code.clone())
362                    .out_json()
363                    .await?;
364
365
366                let (default_base, default_ppm) = if gatewayd_version >= *VERSION_0_8_0_ALPHA {
367                    (2000, 3000)
368                } else {
369                    (50000, 5000)
370                };
371
372                let lightning_fee = gw.get_lightning_fee(new_fed_id.clone()).await?;
373                assert_eq!(
374                    lightning_fee.base.msats, default_base,
375                    "Default Base msat for new federation was not correct"
376                );
377                assert_eq!(
378                    lightning_fee.parts_per_million, default_ppm,
379                    "Default Base msat for new federation was not correct"
380                );
381
382                info!(target: LOG_TEST, federation_id = %new_fed_id, "Verified new federation");
383
384                // Peg-in sats to gw for the new fed
385                let pegin_amount = Amount::from_msats(10_000_000);
386                new_fed
387                    .pegin_gateways(pegin_amount.sats_round_down(), vec![gw])
388                    .await?;
389
390                // Verify `info` returns multiple federations
391                let info_value = cmd!(gw, "info").out_json().await?;
392                let federations = info_value["federations"]
393                    .as_array()
394                    .expect("federations is an array");
395
396                assert_eq!(
397                    federations.len(),
398                    2,
399                    "Gateway did not have two connected federations"
400                );
401
402                let federation_fake_scids =
403                    serde_json::from_value::<Option<BTreeMap<u64, FederationId>>>(
404                        info_value
405                            .get("channels")
406                            .or_else(|| info_value.get("federation_fake_scids"))
407                            .expect("field  exists")
408                            .to_owned(),
409                    )
410                    .expect("cannot parse")
411                    .expect("should have scids");
412
413                assert_eq!(
414                    federation_fake_scids.keys().copied().collect::<Vec<u64>>(),
415                    vec![1, 2]
416                );
417
418                let first_fed_info = federations
419                    .iter()
420                    .find(|i| {
421                        *i["federation_id"]
422                            .as_str()
423                            .expect("should parse as str")
424                            .to_string()
425                            == fed_id
426                    })
427                    .expect("Could not find federation");
428
429                let second_fed_info = federations
430                    .iter()
431                    .find(|i| {
432                        *i["federation_id"]
433                            .as_str()
434                            .expect("should parse as str")
435                            .to_string()
436                            == new_fed_id
437                    })
438                    .expect("Could not find federation");
439
440                let first_fed_balance_msat =
441                    serde_json::from_value::<Amount>(first_fed_info["balance_msat"].clone())
442                        .expect("fed should have balance");
443
444                let second_fed_balance_msat =
445                    serde_json::from_value::<Amount>(second_fed_info["balance_msat"].clone())
446                        .expect("fed should have balance");
447
448                assert_eq!(first_fed_balance_msat, Amount::ZERO);
449                almost_equal(second_fed_balance_msat.msats, pegin_amount.msats, 10_000).unwrap();
450
451                leave_federation(gw, fed_id, 1).await?;
452                leave_federation(gw, new_fed_id, 2).await?;
453
454                // Rejoin new federation, verify that the balance is the same
455                let output = cmd!(gw, "connect-fed", new_invite_code.clone())
456                    .out_json()
457                    .await?;
458                let rejoined_federation_balance_msat =
459                    serde_json::from_value::<Amount>(output["balance_msat"].clone())
460                        .expect("fed has balance");
461
462                assert_eq!(second_fed_balance_msat, rejoined_federation_balance_msat);
463
464                info!(target: LOG_TEST, "Gateway configuration test successful");
465                Ok(())
466            }),
467    )
468    .await
469}
470
471/// Test that verifies the various liquidity tools (onchain, lightning, ecash)
472/// work correctly.
473#[allow(clippy::too_many_lines)]
474async fn liquidity_test() -> anyhow::Result<()> {
475    devimint::run_devfed_test()
476        .call(|dev_fed, _process_mgr| async move {
477            let federation = dev_fed.fed().await?;
478
479            if !devimint::util::supports_lnv2() {
480                info!(target: LOG_TEST, "LNv2 is not supported, which is necessary for LDK GW and liquidity test");
481                return Ok(());
482            }
483
484            let gw_lnd = dev_fed.gw_lnd_registered().await?;
485            let gw_ldk = dev_fed.gw_ldk_connected().await?;
486            let gw_ldk_second = dev_fed.gw_ldk_second_connected().await?;
487            let gateways = [gw_lnd, gw_ldk].to_vec();
488
489            let gateway_matrix = gateways
490                .iter()
491                .cartesian_product(gateways.iter())
492                .filter(|(a, b)| a.ln.ln_type() != b.ln.ln_type());
493
494            info!(target: LOG_TEST, "Pegging-in gateways...");
495            federation
496                .pegin_gateways(1_000_000, gateways.clone())
497                .await?;
498
499            info!(target: LOG_TEST, "Testing ecash payments between gateways...");
500            for (gw_send, gw_receive) in gateway_matrix.clone() {
501                info!(
502                    target: LOG_TEST,
503                    gw_send = %gw_send.ln.ln_type(),
504                    gw_receive = %gw_receive.ln.ln_type(),
505                    "Testing ecash payment",
506                );
507
508                let fed_id = federation.calculate_federation_id();
509                let prev_send_ecash_balance = gw_send.ecash_balance(fed_id.clone()).await?;
510                let prev_receive_ecash_balance = gw_receive.ecash_balance(fed_id.clone()).await?;
511                let ecash = gw_send.send_ecash(fed_id.clone(), 500_000).await?;
512                gw_receive.receive_ecash(ecash).await?;
513                let after_send_ecash_balance = gw_send.ecash_balance(fed_id.clone()).await?;
514                let after_receive_ecash_balance = gw_receive.ecash_balance(fed_id.clone()).await?;
515                assert_eq!(prev_send_ecash_balance - 500_000, after_send_ecash_balance);
516                almost_equal(
517                    prev_receive_ecash_balance + 500_000,
518                    after_receive_ecash_balance,
519                    2_000,
520                )
521                .unwrap();
522            }
523
524            info!(target: LOG_TEST, "Testing payments between gateways...");
525            for (gw_send, gw_receive) in gateway_matrix.clone() {
526                info!(
527                    target: LOG_TEST,
528                    gw_send = %gw_send.ln.ln_type(),
529                    gw_receive = %gw_receive.ln.ln_type(),
530                    "Testing lightning payment",
531                );
532
533                let invoice = gw_receive.create_invoice(1_000_000).await?;
534                gw_send.pay_invoice(invoice).await?;
535            }
536
537            if devimint::util::Gatewayd::version_or_default().await >= *VERSION_0_7_0_ALPHA {
538                let start = now() - Duration::from_secs(5 * 60);
539                let end = now() + Duration::from_secs(5 * 60);
540                info!(target: LOG_TEST, "Verifying list of transactions");
541                let lnd_transactions = gw_lnd.list_transactions(start, end).await?;
542                // One inbound and one outbound transaction
543                assert_eq!(lnd_transactions.len(), 2);
544
545                let ldk_transactions = gw_ldk.list_transactions(start, end).await?;
546                assert_eq!(ldk_transactions.len(), 2);
547
548                // Verify that transactions are filtered by time
549                let start = now() - Duration::from_secs(10 * 60);
550                let end = now() - Duration::from_secs(5 * 60);
551                let lnd_transactions = gw_lnd.list_transactions(start, end).await?;
552                assert_eq!(lnd_transactions.len(), 0);
553            }
554
555            if devimint::util::Gatewayd::version_or_default().await >= *VERSION_0_7_0_ALPHA {
556                info!(target: LOG_TEST, "Testing paying Bolt12 Offers...");
557                // TODO: investigate why the first BOLT12 payment attempt is expiring consistently
558                poll_with_timeout("First BOLT12 payment", Duration::from_secs(30), || async {
559                    let offer_with_amount = gw_ldk_second.create_offer(Some(Amount::from_msats(10_000_000))).await.map_err(ControlFlow::Continue)?;
560                    gw_ldk.pay_offer(offer_with_amount, None).await.map_err(ControlFlow::Continue)?;
561                    assert!(get_transaction(gw_ldk_second, PaymentKind::Bolt12Offer, Amount::from_msats(10_000_000), PaymentStatus::Succeeded).await.is_some());
562                    Ok(())
563                }).await?;
564
565                let offer_without_amount = gw_ldk.create_offer(None).await?;
566                gw_ldk_second.pay_offer(offer_without_amount.clone(), Some(Amount::from_msats(5_000_000))).await?;
567                assert!(get_transaction(gw_ldk, PaymentKind::Bolt12Offer, Amount::from_msats(5_000_000), PaymentStatus::Succeeded).await.is_some());
568
569                // Cannot pay an offer without an amount without specifying an amount
570                gw_ldk_second.pay_offer(offer_without_amount.clone(), None).await.expect_err("Cannot pay amountless offer without specifying an amount");
571
572                // Verify we can pay the offer again
573                gw_ldk_second.pay_offer(offer_without_amount, Some(Amount::from_msats(3_000_000))).await?;
574                assert!(get_transaction(gw_ldk, PaymentKind::Bolt12Offer, Amount::from_msats(3_000_000), PaymentStatus::Succeeded).await.is_some());
575            }
576
577            info!(target: LOG_TEST, "Pegging-out gateways...");
578            federation
579                .pegout_gateways(500_000_000, gateways.clone())
580                .await?;
581
582            info!(target: LOG_TEST, "Testing sending onchain...");
583            let bitcoind = dev_fed.bitcoind().await?;
584            for gw in &gateways {
585                let txid = gw
586                    .send_onchain(dev_fed.bitcoind().await?, BitcoinAmountOrAll::All, 10)
587                    .await?;
588                bitcoind.poll_get_transaction(txid).await?;
589            }
590
591            info!(target: LOG_TEST, "Testing closing all channels...");
592
593            // Gracefully close one of LND's channel's
594            let gw_ldk_pubkey = gw_ldk.lightning_pubkey().await?;
595            gw_lnd.close_channel(gw_ldk_pubkey, false).await?;
596
597            // Force close LDK's channels
598            gw_ldk_second.close_all_channels(true).await?;
599
600            // Verify none of the channels are active
601            for gw in gateways {
602                let channels = gw.list_channels().await?;
603                let active_channel = channels.into_iter().any(|chan| chan.is_active);
604                assert!(!active_channel);
605            }
606
607            Ok(())
608        })
609        .await
610}
611
612async fn esplora_test() -> anyhow::Result<()> {
613    let args = cli::CommonArgs::parse_from::<_, ffi::OsString>(vec![]);
614    let (process_mgr, task_group) = cli::setup(args).await?;
615    cleanup_on_exit(
616        async {
617            info!("Spawning bitcoind...");
618            let bitcoind = Bitcoind::new(&process_mgr, false).await?;
619            info!("Spawning esplora...");
620            let _esplora = Esplora::new(&process_mgr, bitcoind).await?;
621            let network = bitcoin::Network::from_str(&process_mgr.globals.FM_GATEWAY_NETWORK)
622                .expect("Could not parse network");
623            let esplora_port = process_mgr.globals.FM_PORT_ESPLORA.to_string();
624            let esplora = default_esplora_server(network, Some(esplora_port));
625            unsafe {
626                std::env::remove_var("FM_BITCOIND_URL");
627                std::env::set_var("FM_ESPLORA_URL", esplora.url.to_string());
628            }
629            info!("Spawning ldk gateway...");
630            let ldk = Gatewayd::new(
631                &process_mgr,
632                LightningNode::Ldk {
633                    name: "gateway-ldk-esplora".to_string(),
634                    gw_port: process_mgr.globals.FM_PORT_GW_LDK,
635                    ldk_port: process_mgr.globals.FM_PORT_LDK,
636                },
637            )
638            .await?;
639
640            info!("Waiting for ldk gatewy to be ready...");
641            poll("Waiting for LDK to be ready", || async {
642                let info = ldk.get_info().await.map_err(ControlFlow::Continue)?;
643                let state: String = serde_json::from_value(info["gateway_state"].clone())
644                    .expect("Could not get gateway state");
645                if state == "Running" {
646                    Ok(())
647                } else {
648                    Err(ControlFlow::Continue(anyhow::anyhow!(
649                        "Gateway not running"
650                    )))
651                }
652            })
653            .await?;
654
655            ldk.get_ln_onchain_address().await?;
656            info!(target:LOG_TEST, "ldk gateway successfully spawned and connected to esplora");
657            Ok(())
658        },
659        task_group,
660    )
661    .await?;
662    Ok(())
663}
664
665async fn get_transaction(
666    gateway: &Gatewayd,
667    kind: PaymentKind,
668    amount: Amount,
669    status: PaymentStatus,
670) -> Option<PaymentDetails> {
671    let transactions = gateway
672        .list_transactions(
673            now() - Duration::from_secs(5 * 60),
674            now() + Duration::from_secs(5 * 60),
675        )
676        .await
677        .ok()?;
678    transactions.into_iter().find(|details| {
679        details.payment_kind == kind && details.amount == amount && details.status == status
680    })
681}
682
683/// Leaves the specified federation by issuing a `leave-fed` POST request to the
684/// gateway.
685async fn leave_federation(gw: &Gatewayd, fed_id: String, expected_scid: u64) -> anyhow::Result<()> {
686    let leave_fed = cmd!(gw, "leave-fed", "--federation-id", fed_id.clone())
687        .out_json()
688        .await
689        .expect("Leaving the federation failed");
690
691    let federation_id: FederationId = serde_json::from_value(leave_fed["federation_id"].clone())?;
692    assert_eq!(federation_id.to_string(), fed_id);
693
694    let scid = serde_json::from_value::<u64>(leave_fed["config"]["federation_index"].clone())?;
695
696    assert_eq!(scid, expected_scid);
697
698    info!(target: LOG_TEST, federation_id = %fed_id, "Verified gateway left federation");
699    Ok(())
700}