Skip to main content

fedimint_cli/
command_wait.rs

1use std::future::pending;
2use std::time::Duration;
3
4use anyhow::format_err;
5use fedimint_core::runtime;
6use fedimint_core::util::{backoff_util, retry};
7use fedimint_logging::LOG_CLIENT;
8use fedimint_wallet_client::WalletClientModule;
9use fedimint_wallet_client::api::WalletFederationApi;
10use tracing::info;
11
12use crate::cli::Opts;
13use crate::{CliOutput, CliOutputResult, CliResultExt, FedimintCli};
14
15impl FedimintCli {
16    /// Retry the wallet consensus height, reopening the client for each
17    /// attempt.
18    pub(super) async fn wait_block_count(&self, cli: &Opts, target: u64) -> CliOutputResult {
19        retry(
20            "wait_block_count",
21            backoff_util::custom_backoff(Duration::from_millis(100), Duration::from_secs(5), None),
22            || async {
23                let client = self.client_open(cli).await?;
24                let wallet = client.get_first_module::<WalletClientModule>()?;
25                let count = client
26                    .api()
27                    .with_module(wallet.id)
28                    .fetch_consensus_block_count()
29                    .await?;
30                if count >= target {
31                    Ok(CliOutput::WaitBlockCount { reached: count })
32                } else {
33                    info!(target: LOG_CLIENT, current=count, target, "Block count not reached");
34                    Err(format_err!("target not reached"))
35                }
36            },
37        )
38        .await
39        .map_err_cli()
40    }
41
42    /// Keep the client alive while giving background networking a chance to
43    /// start.
44    pub(super) async fn wait_with_networking(
45        &self,
46        cli: &Opts,
47        seconds: Option<f32>,
48    ) -> CliOutputResult {
49        let client = self.client_open(cli).await?;
50        // Trigger a network call so background work can make progress while waiting.
51        client
52            .task_group()
53            .spawn_cancellable("fedimint-cli dev wait: init networking", {
54                let client = client.clone();
55                async move {
56                    let _ = client.api().session_count().await;
57                }
58            });
59        if let Some(secs) = seconds {
60            runtime::sleep(Duration::from_secs_f32(secs)).await;
61        } else {
62            pending::<()>().await;
63        }
64        Ok(CliOutput::Raw(serde_json::Value::Null))
65    }
66}