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::federation::Federation;
15use devimint::gatewayd::LdkChainSource;
16use devimint::util::{ProcessManager, 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};
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    assert_eq!(
182        10_000_000,
183        ecash_balance.ecash_balance_msats.sats_round_down()
184    );
185    let after_onchain_balance = gateway_balances.onchain_balance_sats;
186    assert_eq!(before_onchain_balance, after_onchain_balance);
187    info!(target: LOG_TEST, "Verified balances after recovery");
188
189    Ok(new_gw)
190}
191
192/// TODO(v0.5.0): We do not need to run the `gatewayd-mnemonic` test from v0.4.0
193/// -> v0.5.0 over and over again. Once we have verified this test passes for
194/// v0.5.0, it can safely be removed.
195async fn mnemonic_upgrade_test(
196    old_gatewayd_path: PathBuf,
197    new_gatewayd_path: PathBuf,
198    old_gateway_cli_path: PathBuf,
199    new_gateway_cli_path: PathBuf,
200) -> anyhow::Result<()> {
201    // TODO: Audit that the environment access only happens in single-threaded code.
202    unsafe { std::env::set_var("FM_GATEWAYD_BASE_EXECUTABLE", old_gatewayd_path) };
203    // TODO: Audit that the environment access only happens in single-threaded code.
204    unsafe { std::env::set_var("FM_GATEWAY_CLI_BASE_EXECUTABLE", old_gateway_cli_path) };
205    // TODO: Audit that the environment access only happens in single-threaded code.
206    unsafe { std::env::set_var("FM_ENABLE_MODULE_LNV2", "0") };
207
208    devimint::run_devfed_test()
209        .call(|dev_fed, process_mgr| async move {
210            let gatewayd_version = util::Gatewayd::version_or_default().await;
211            let gateway_cli_version = util::GatewayCli::version_or_default().await;
212            info!(
213                target: LOG_TEST,
214                gatewayd_version = %gatewayd_version,
215                gateway_cli_version = %gateway_cli_version,
216                "Running gatewayd mnemonic test"
217            );
218
219            let mut gw_lnd = dev_fed.gw_lnd_registered().await?.to_owned();
220            let fed = dev_fed.fed().await?;
221            let federation_id = FederationId::from_str(fed.calculate_federation_id().as_str())?;
222
223            gw_lnd
224                .restart_with_bin(&process_mgr, &new_gatewayd_path, &new_gateway_cli_path)
225                .await?;
226
227            // Verify that we have a legacy federation
228            let mnemonic_response = gw_lnd.get_mnemonic().await?;
229            assert!(
230                mnemonic_response
231                    .legacy_federations
232                    .contains(&federation_id)
233            );
234
235            info!(target: LOG_TEST, "Verified a legacy federation exists");
236
237            // Leave federation
238            gw_lnd.leave_federation(federation_id).await?;
239
240            // Rejoin federation
241            gw_lnd.connect_fed(fed).await?;
242
243            // Verify that the legacy federation is recognized
244            let mnemonic_response = gw_lnd.get_mnemonic().await?;
245            assert!(
246                mnemonic_response
247                    .legacy_federations
248                    .contains(&federation_id)
249            );
250            assert_eq!(mnemonic_response.legacy_federations.len(), 1);
251
252            info!(target: LOG_TEST, "Verified leaving and re-joining preservers legacy federation");
253
254            // Leave federation and delete database to force migration to mnemonic
255            gw_lnd.leave_federation(federation_id).await?;
256
257            let data_dir: PathBuf = env::var(FM_DATA_DIR_ENV)
258                .expect("Data dir is not set")
259                .parse()
260                .expect("Could not parse data dir");
261            let gw_fed_db = data_dir
262                .join(gw_lnd.gw_name.clone())
263                .join(format!("{federation_id}.db"));
264            remove_dir_all(gw_fed_db)?;
265
266            gw_lnd.connect_fed(fed).await?;
267
268            // Verify that the re-connected federation is not a legacy federation
269            let mnemonic_response = gw_lnd.get_mnemonic().await?;
270            assert!(
271                !mnemonic_response
272                    .legacy_federations
273                    .contains(&federation_id)
274            );
275            assert_eq!(mnemonic_response.legacy_federations.len(), 0);
276
277            info!(target: LOG_TEST, "Verified deleting database will migrate the federation to use mnemonic");
278
279            info!(target: LOG_TEST, "Successfully completed mnemonic upgrade test");
280
281            Ok(())
282        })
283        .await
284}
285
286/// Test that sets and verifies configurations within the gateway
287#[allow(clippy::too_many_lines)]
288async fn config_test(gw_type: LightningNodeType) -> anyhow::Result<()> {
289    Box::pin(
290        devimint::run_devfed_test()
291            .num_feds(2)
292            .call(|dev_fed, process_mgr| async move {
293                let gw = match gw_type {
294                    LightningNodeType::Lnd => dev_fed.gw_lnd_registered().await?,
295                    LightningNodeType::Ldk => dev_fed.gw_ldk_connected().await?,
296                };
297
298                // Try to connect to already connected federation
299                let invite_code = dev_fed.fed().await?.invite_code()?;
300                let output = cmd!(gw, "connect-fed", invite_code.clone())
301                    .out_json()
302                    .await;
303                assert!(
304                    output.is_err(),
305                    "Connecting to the same federation succeeded"
306                );
307                info!(target: LOG_TEST, "Verified that gateway couldn't connect to already connected federation");
308
309                let gatewayd_version = util::Gatewayd::version_or_default().await;
310
311                // Change the routing fees for a specific federation
312                let fed_id = dev_fed.fed().await?.calculate_federation_id();
313                gw.set_federation_routing_fee(fed_id.clone(), 20, 20000)
314                    .await?;
315
316                let lightning_fee = gw.get_lightning_fee(fed_id.clone()).await?;
317                assert_eq!(
318                    lightning_fee.base.msats, 20,
319                    "Federation base msat is not 20"
320                );
321                assert_eq!(
322                    lightning_fee.parts_per_million, 20000,
323                    "Federation proportional millionths is not 20000"
324                );
325                info!(target: LOG_TEST, "Verified per-federation routing fees changed");
326
327                let info_value = cmd!(gw, "info").out_json().await?;
328                let federations = info_value["federations"]
329                    .as_array()
330                    .expect("federations is an array");
331                assert_eq!(
332                    federations.len(),
333                    1,
334                    "Gateway did not have one connected federation"
335                );
336
337                // Get the federation's config and verify it parses correctly
338                let config_val = cmd!(gw, "cfg", "client-config", "--federation-id", fed_id)
339                    .out_json()
340                    .await?;
341
342                serde_json::from_value::<GatewayFedConfig>(config_val)?;
343
344                // Spawn new federation
345                let bitcoind = dev_fed.bitcoind().await?;
346                let new_fed = Federation::new(
347                    &process_mgr,
348                    bitcoind.clone(),
349                    false,
350                    false,
351                    1,
352                    "config-test".to_string(),
353                )
354                .await?;
355                let new_fed_id = new_fed.calculate_federation_id();
356                info!(target: LOG_TEST, "Successfully spawned new federation");
357
358                let new_invite_code = new_fed.invite_code()?;
359                cmd!(gw, "connect-fed", new_invite_code.clone())
360                    .out_json()
361                    .await?;
362
363
364                let (default_base, default_ppm) = if gatewayd_version >= *VERSION_0_8_0_ALPHA {
365                    (2000, 3000)
366                } else {
367                    (50000, 5000)
368                };
369
370                let lightning_fee = gw.get_lightning_fee(new_fed_id.clone()).await?;
371                assert_eq!(
372                    lightning_fee.base.msats, default_base,
373                    "Default Base msat for new federation was not correct"
374                );
375                assert_eq!(
376                    lightning_fee.parts_per_million, default_ppm,
377                    "Default Base msat for new federation was not correct"
378                );
379
380                info!(target: LOG_TEST, federation_id = %new_fed_id, "Verified new federation");
381
382                // Peg-in sats to gw for the new fed
383                let pegin_amount = Amount::from_msats(10_000_000);
384                new_fed
385                    .pegin_gateways(pegin_amount.sats_round_down(), vec![gw])
386                    .await?;
387
388                // Verify `info` returns multiple federations
389                let info_value = cmd!(gw, "info").out_json().await?;
390                let federations = info_value["federations"]
391                    .as_array()
392                    .expect("federations is an array");
393
394                assert_eq!(
395                    federations.len(),
396                    2,
397                    "Gateway did not have two connected federations"
398                );
399
400                let federation_fake_scids =
401                    serde_json::from_value::<Option<BTreeMap<u64, FederationId>>>(
402                        info_value
403                            .get("channels")
404                            .or_else(|| info_value.get("federation_fake_scids"))
405                            .expect("field  exists")
406                            .to_owned(),
407                    )
408                    .expect("cannot parse")
409                    .expect("should have scids");
410
411                assert_eq!(
412                    federation_fake_scids.keys().copied().collect::<Vec<u64>>(),
413                    vec![1, 2]
414                );
415
416                let first_fed_info = federations
417                    .iter()
418                    .find(|i| {
419                        *i["federation_id"]
420                            .as_str()
421                            .expect("should parse as str")
422                            .to_string()
423                            == fed_id
424                    })
425                    .expect("Could not find federation");
426
427                let second_fed_info = federations
428                    .iter()
429                    .find(|i| {
430                        *i["federation_id"]
431                            .as_str()
432                            .expect("should parse as str")
433                            .to_string()
434                            == new_fed_id
435                    })
436                    .expect("Could not find federation");
437
438                let first_fed_balance_msat =
439                    serde_json::from_value::<Amount>(first_fed_info["balance_msat"].clone())
440                        .expect("fed should have balance");
441
442                let second_fed_balance_msat =
443                    serde_json::from_value::<Amount>(second_fed_info["balance_msat"].clone())
444                        .expect("fed should have balance");
445
446                assert_eq!(first_fed_balance_msat, Amount::ZERO);
447                assert_eq!(second_fed_balance_msat, pegin_amount);
448
449                leave_federation(gw, fed_id, 1).await?;
450                leave_federation(gw, new_fed_id, 2).await?;
451
452                // Rejoin new federation, verify that the balance is the same
453                let output = cmd!(gw, "connect-fed", new_invite_code.clone())
454                    .out_json()
455                    .await?;
456                let rejoined_federation_balance_msat =
457                    serde_json::from_value::<Amount>(output["balance_msat"].clone())
458                        .expect("fed has balance");
459
460                assert_eq!(second_fed_balance_msat, rejoined_federation_balance_msat);
461
462                info!(target: LOG_TEST, "Gateway configuration test successful");
463                Ok(())
464            }),
465    )
466    .await
467}
468
469/// Test that verifies the various liquidity tools (onchain, lightning, ecash)
470/// work correctly.
471#[allow(clippy::too_many_lines)]
472async fn liquidity_test() -> anyhow::Result<()> {
473    devimint::run_devfed_test()
474        .call(|dev_fed, _process_mgr| async move {
475            let federation = dev_fed.fed().await?;
476
477            if !devimint::util::supports_lnv2() {
478                info!(target: LOG_TEST, "LNv2 is not supported, which is necessary for LDK GW and liquidity test");
479                return Ok(());
480            }
481
482            let gw_lnd = dev_fed.gw_lnd_registered().await?;
483            let gw_ldk = dev_fed.gw_ldk_connected().await?;
484            let gw_ldk_second = dev_fed.gw_ldk_second_connected().await?;
485            let gateways = [gw_lnd, gw_ldk].to_vec();
486
487            let gateway_matrix = gateways
488                .iter()
489                .cartesian_product(gateways.iter())
490                .filter(|(a, b)| a.ln.ln_type() != b.ln.ln_type());
491
492            info!(target: LOG_TEST, "Pegging-in gateways...");
493            federation
494                .pegin_gateways(1_000_000, gateways.clone())
495                .await?;
496
497            info!(target: LOG_TEST, "Testing ecash payments between gateways...");
498            for (gw_send, gw_receive) in gateway_matrix.clone() {
499                info!(
500                    target: LOG_TEST,
501                    gw_send = %gw_send.ln.ln_type(),
502                    gw_receive = %gw_receive.ln.ln_type(),
503                    "Testing ecash payment",
504                );
505
506                let fed_id = federation.calculate_federation_id();
507                let prev_send_ecash_balance = gw_send.ecash_balance(fed_id.clone()).await?;
508                let prev_receive_ecash_balance = gw_receive.ecash_balance(fed_id.clone()).await?;
509                let ecash = gw_send.send_ecash(fed_id.clone(), 500_000).await?;
510                gw_receive.receive_ecash(ecash).await?;
511                let after_send_ecash_balance = gw_send.ecash_balance(fed_id.clone()).await?;
512                let after_receive_ecash_balance = gw_receive.ecash_balance(fed_id.clone()).await?;
513                assert_eq!(prev_send_ecash_balance - 500_000, after_send_ecash_balance);
514                assert_eq!(
515                    prev_receive_ecash_balance + 500_000,
516                    after_receive_ecash_balance
517                );
518            }
519
520            info!(target: LOG_TEST, "Testing payments between gateways...");
521            for (gw_send, gw_receive) in gateway_matrix.clone() {
522                info!(
523                    target: LOG_TEST,
524                    gw_send = %gw_send.ln.ln_type(),
525                    gw_receive = %gw_receive.ln.ln_type(),
526                    "Testing lightning payment",
527                );
528
529                let invoice = gw_receive.create_invoice(1_000_000).await?;
530                gw_send.pay_invoice(invoice).await?;
531            }
532
533            if devimint::util::Gatewayd::version_or_default().await >= *VERSION_0_7_0_ALPHA {
534                let start = now() - Duration::from_secs(5 * 60);
535                let end = now() + Duration::from_secs(5 * 60);
536                info!(target: LOG_TEST, "Verifying list of transactions");
537                let lnd_transactions = gw_lnd.list_transactions(start, end).await?;
538                // One inbound and one outbound transaction
539                assert_eq!(lnd_transactions.len(), 2);
540
541                let ldk_transactions = gw_ldk.list_transactions(start, end).await?;
542                assert_eq!(ldk_transactions.len(), 2);
543
544                // Verify that transactions are filtered by time
545                let start = now() - Duration::from_secs(10 * 60);
546                let end = now() - Duration::from_secs(5 * 60);
547                let lnd_transactions = gw_lnd.list_transactions(start, end).await?;
548                assert_eq!(lnd_transactions.len(), 0);
549            }
550
551            if devimint::util::Gatewayd::version_or_default().await >= *VERSION_0_7_0_ALPHA {
552                info!(target: LOG_TEST, "Testing paying Bolt12 Offers...");
553                // TODO: investigate why the first BOLT12 payment attempt is expiring consistently
554                poll_with_timeout("First BOLT12 payment", Duration::from_secs(30), || async {
555                    let offer_with_amount = gw_ldk_second.create_offer(Some(Amount::from_msats(10_000_000))).await.map_err(ControlFlow::Continue)?;
556                    gw_ldk.pay_offer(offer_with_amount, None).await.map_err(ControlFlow::Continue)?;
557                    assert!(get_transaction(gw_ldk_second, PaymentKind::Bolt12Offer, Amount::from_msats(10_000_000), PaymentStatus::Succeeded).await.is_some());
558                    Ok(())
559                }).await?;
560
561                let offer_without_amount = gw_ldk.create_offer(None).await?;
562                gw_ldk_second.pay_offer(offer_without_amount.clone(), Some(Amount::from_msats(5_000_000))).await?;
563                assert!(get_transaction(gw_ldk, PaymentKind::Bolt12Offer, Amount::from_msats(5_000_000), PaymentStatus::Succeeded).await.is_some());
564
565                // Cannot pay an offer without an amount without specifying an amount
566                gw_ldk_second.pay_offer(offer_without_amount.clone(), None).await.expect_err("Cannot pay amountless offer without specifying an amount");
567
568                // Verify we can pay the offer again
569                gw_ldk_second.pay_offer(offer_without_amount, Some(Amount::from_msats(3_000_000))).await?;
570                assert!(get_transaction(gw_ldk, PaymentKind::Bolt12Offer, Amount::from_msats(3_000_000), PaymentStatus::Succeeded).await.is_some());
571            }
572
573            info!(target: LOG_TEST, "Pegging-out gateways...");
574            federation
575                .pegout_gateways(500_000_000, gateways.clone())
576                .await?;
577
578            info!(target: LOG_TEST, "Testing sending onchain...");
579            let bitcoind = dev_fed.bitcoind().await?;
580            for gw in &gateways {
581                let txid = gw
582                    .send_onchain(dev_fed.bitcoind().await?, BitcoinAmountOrAll::All, 10)
583                    .await?;
584                bitcoind.poll_get_transaction(txid).await?;
585            }
586
587            info!(target: LOG_TEST, "Testing closing all channels...");
588
589            // Gracefully close one of LND's channel's
590            let gw_ldk_pubkey = gw_ldk.lightning_pubkey().await?;
591            gw_lnd.close_channel(gw_ldk_pubkey, false).await?;
592
593            // Force close LDK's channels
594            gw_ldk_second.close_all_channels(true).await?;
595
596            // Verify none of the channels are active
597            for gw in gateways {
598                let channels = gw.list_channels().await?;
599                let active_channel = channels.into_iter().any(|chan| chan.is_active);
600                assert!(!active_channel);
601            }
602
603            Ok(())
604        })
605        .await
606}
607
608/// This test cannot be run in CI because it connects to an external esplora
609/// server
610async fn esplora_test() -> anyhow::Result<()> {
611    let args = cli::CommonArgs::parse_from::<_, ffi::OsString>(vec![]);
612    let (process_mgr, task_group) = cli::setup(args).await?;
613    cleanup_on_exit(
614        async {
615            // Spawn mutinynet esplora Gatewayd instance
616            unsafe {
617                std::env::set_var("FM_GATEWAY_NETWORK", "signet");
618                std::env::set_var("FM_LDK_NETWORK", "signet");
619            }
620            let ldk = Gatewayd::new(
621                &process_mgr,
622                LightningNode::Ldk {
623                    name: "gateway-ldk-mutinynet".to_string(),
624                    gw_port: process_mgr.globals.FM_PORT_GW_LDK,
625                    ldk_port: process_mgr.globals.FM_PORT_LDK,
626                    chain_source: LdkChainSource::Esplora,
627                },
628            )
629            .await?;
630
631            poll("Waiting for LDK to be ready", || async {
632                let info = ldk.get_info().await.map_err(ControlFlow::Continue)?;
633                let state: String = serde_json::from_value(info["gateway_state"].clone())
634                    .expect("Could not get gateway state");
635                if state == "Running" {
636                    Ok(())
637                } else {
638                    Err(ControlFlow::Continue(anyhow::anyhow!(
639                        "Gateway not running"
640                    )))
641                }
642            })
643            .await?;
644
645            ldk.get_ln_onchain_address().await?;
646            info!(target:LOG_TEST, "Successfully connected to mutinynet esplora");
647            Ok(())
648        },
649        task_group,
650    )
651    .await?;
652    Ok(())
653}
654
655async fn get_transaction(
656    gateway: &Gatewayd,
657    kind: PaymentKind,
658    amount: Amount,
659    status: PaymentStatus,
660) -> Option<PaymentDetails> {
661    let transactions = gateway
662        .list_transactions(
663            now() - Duration::from_secs(5 * 60),
664            now() + Duration::from_secs(5 * 60),
665        )
666        .await
667        .ok()?;
668    transactions.into_iter().find(|details| {
669        details.payment_kind == kind && details.amount == amount && details.status == status
670    })
671}
672
673/// Leaves the specified federation by issuing a `leave-fed` POST request to the
674/// gateway.
675async fn leave_federation(gw: &Gatewayd, fed_id: String, expected_scid: u64) -> anyhow::Result<()> {
676    let leave_fed = cmd!(gw, "leave-fed", "--federation-id", fed_id.clone())
677        .out_json()
678        .await
679        .expect("Leaving the federation failed");
680
681    let federation_id: FederationId = serde_json::from_value(leave_fed["federation_id"].clone())?;
682    assert_eq!(federation_id.to_string(), fed_id);
683
684    let scid = serde_json::from_value::<u64>(leave_fed["config"]["federation_index"].clone())?;
685
686    assert_eq!(scid, expected_scid);
687
688    info!(target: LOG_TEST, federation_id = %fed_id, "Verified gateway left federation");
689    Ok(())
690}