wallet_recovery_test/
wallet-recovery-test.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use anyhow::bail;
use devimint::cmd;
use devimint::util::{FedimintCli, FedimintdCmd};
use devimint::version_constants::{VERSION_0_4_0, VERSION_0_6_0_ALPHA};
use fedimint_core::util::{backoff_util, retry};
use futures::try_join;
use tracing::info;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    devimint::run_devfed_test(|dev_fed, _process_mgr| async move {
        let fedimint_cli_version = FedimintCli::version_or_default().await;
        let fedimintd_version = FedimintdCmd::version_or_default().await;
        // TODO(support:v0.4): recovery was introduced in v0.4.0
        // see: https://github.com/fedimint/fedimint/pull/5546
        if fedimint_cli_version < *VERSION_0_4_0 || fedimintd_version < *VERSION_0_4_0 {
            info!("Skipping whole test on old fedimint-cli/fedimintd that is missing some irrelevant bolts");
            return Ok(());
        }

        let (fed, _bitcoind) = try_join!(dev_fed.fed(), dev_fed.bitcoind())?;

        let peg_in_amount_sats = 100_000;

            // Start this client early, as we need to test waiting for session to close
        let client_slow = fed
            .new_joined_client("wallet-client-recovery-origin")
            .await?;
        info!("Join and claim");
        fed.pegin_client(peg_in_amount_sats, &client_slow).await?;

        let client_slow_pegin_session_count = client_slow.get_session_count().await?;

        info!("### Test wallet restore without a backup");
        {
            let client = fed
                .new_joined_client("wallet-client-recovery-origin")
                .await?;

            info!("Join, but not claim");
            let operation_id = fed
                .pegin_client_no_wait(peg_in_amount_sats, &client)
                .await?;

            info!("Restore without backup");
            let restored = client
                .new_restored("restored-without-backup", fed.invite_code()?)
                .await?;

            if fedimint_cli_version < *VERSION_0_6_0_ALPHA {
                cmd!(restored, "module", "wallet", "await-deposit", operation_id)
                    .run()
                    .await?;
            } else {
                cmd!(restored, "module", "wallet", "await-deposit", "--operation-id", operation_id)
                    .run()
                    .await?;
            }

            info!("Check if claimed");
            assert_eq!(peg_in_amount_sats * 1000, restored.balance().await?);
        }

        info!("### Test wallet restore with a backup");
        {
            let client = fed
                .new_joined_client("wallet-client-recovery-origin")
                .await?;
            assert_eq!(0, client.balance().await?);

            info!("Join and claim");
            fed.pegin_client(peg_in_amount_sats, &client).await?;

            info!("Make a backup");
            cmd!(client, "backup").run().await?;

            info!("Join more, but not claim");
            let operation_id = fed
                .pegin_client_no_wait(peg_in_amount_sats, &client)
                .await?;

            info!("Restore with backup");
            let restored = client
                .new_restored("restored-with-backup", fed.invite_code()?)
                .await?;

            if fedimint_cli_version < *VERSION_0_6_0_ALPHA {
                cmd!(restored, "module", "wallet", "await-deposit", operation_id)
                    .run()
                    .await?;
            } else {
                cmd!(restored, "module", "wallet", "await-deposit", "--operation-id", operation_id)
                    .run()
                    .await?;
            }

            info!("Check if claimed");
            assert_eq!(peg_in_amount_sats * 1000 * 2, restored.balance().await?);
        }

        info!("### Test wallet restore with a history and no backup");
        {
            let client = client_slow;

            retry("wait for next session", backoff_util::aggressive_backoff(), || async {
                if client_slow_pegin_session_count < client.get_session_count().await? {
                    return Ok(());
                }
                bail!("Session didn't close")
            })
            .await
            .expect("timeouted waiting for session to close");

            let operation_id = fed
                .pegin_client_no_wait(peg_in_amount_sats, &client)
                .await?;

            info!("Client slow: Restore without backup");
            let restored = client
                .new_restored("client-slow-restored-without-backup", fed.invite_code()?)
                .await?;

            if fedimint_cli_version < *VERSION_0_6_0_ALPHA {
                cmd!(restored, "module", "wallet", "await-deposit", operation_id)
                    .run()
                    .await?;
            } else {
                cmd!(restored, "module", "wallet", "await-deposit", "--operation-id", operation_id)
                    .run()
                    .await?;
            }

            info!("Client slow: Check if claimed");
            assert_eq!(peg_in_amount_sats * 1000 * 2, restored.balance().await?);
        }

        Ok(())
    })
    .await
}