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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use std::{env, fs};

use fedimint_bitcoind::{create_bitcoind, DynBitcoindRpc};
use fedimint_client::module::init::{
    ClientModuleInitRegistry, DynClientModuleInit, IClientModuleInit,
};
use fedimint_core::bitcoinrpc::BitcoinRpcConfig;
use fedimint_core::config::{
    ModuleInitParams, ServerModuleConfigGenParamsRegistry, ServerModuleInitRegistry,
};
use fedimint_core::core::{ModuleInstanceId, ModuleKind};
use fedimint_core::module::{DynServerModuleInit, IServerModuleInit};
use fedimint_core::task::{MaybeSend, MaybeSync, TaskGroup};
use fedimint_core::util::SafeUrl;
use fedimint_logging::{TracingSetup, LOG_TEST};
use tempfile::TempDir;
use tracing::info;

use crate::btc::mock::FakeBitcoinFactory;
use crate::btc::real::RealBitcoinTest;
use crate::btc::BitcoinTest;
use crate::federation::FederationTest;
use crate::gateway::GatewayTest;
use crate::ln::mock::FakeLightningTest;
use crate::ln::real::{ClnLightningTest, LdkLightningTest, LndLightningTest};
use crate::ln::LightningTest;

/// A default timeout for things happening in tests
pub const TIMEOUT: Duration = Duration::from_secs(10);

/// A tool for easily writing fedimint integration tests
pub struct Fixtures {
    num_peers: u16,
    clients: Vec<DynClientModuleInit>,
    servers: Vec<DynServerModuleInit>,
    params: ServerModuleConfigGenParamsRegistry,
    primary_client: ModuleInstanceId,
    bitcoin_rpc: BitcoinRpcConfig,
    bitcoin: Arc<dyn BitcoinTest>,
    dyn_bitcoin_rpc: DynBitcoindRpc,
    id: ModuleInstanceId,
}

impl Fixtures {
    pub fn new_primary(
        client: impl IClientModuleInit + MaybeSend + MaybeSync + 'static,
        server: impl IServerModuleInit + MaybeSend + MaybeSync + 'static,
        params: impl ModuleInitParams,
    ) -> Self {
        // Ensure tracing has been set once
        let _ = TracingSetup::default().init();
        let real_testing = Fixtures::is_real_test();
        let num_peers = 4;
        let task_group = TaskGroup::new();
        let (dyn_bitcoin_rpc, bitcoin, config): (
            DynBitcoindRpc,
            Arc<dyn BitcoinTest>,
            BitcoinRpcConfig,
        ) = if real_testing {
            let rpc_config = BitcoinRpcConfig::from_env_vars().unwrap();
            let dyn_bitcoin_rpc = create_bitcoind(&rpc_config, task_group.make_handle()).unwrap();
            let bitcoincore_url = env::var("FM_TEST_BITCOIND_RPC")
                .expect("Must have bitcoind RPC defined for real tests")
                .parse()
                .expect("Invalid bitcoind RPC URL");
            let bitcoin = RealBitcoinTest::new(&bitcoincore_url, dyn_bitcoin_rpc.clone());
            (dyn_bitcoin_rpc, Arc::new(bitcoin), rpc_config)
        } else {
            let FakeBitcoinFactory { bitcoin, config } = FakeBitcoinFactory::register_new();
            let dyn_bitcoin_rpc = DynBitcoindRpc::from(bitcoin.clone());
            let bitcoin = Arc::new(bitcoin);
            (dyn_bitcoin_rpc, bitcoin, config)
        };

        Self {
            num_peers,
            clients: vec![],
            servers: vec![],
            params: Default::default(),
            primary_client: 0,
            bitcoin_rpc: config,
            bitcoin,
            dyn_bitcoin_rpc,
            id: 0,
        }
        .with_module(client, server, params)
    }

    pub fn is_real_test() -> bool {
        env::var("FM_TEST_USE_REAL_DAEMONS") == Ok("1".to_string())
    }

    // TODO: Auto-assign instance ids after removing legacy id order
    /// Add a module to the fed
    pub fn with_module(
        mut self,
        client: impl IClientModuleInit + MaybeSend + MaybeSync + 'static,
        server: impl IServerModuleInit + MaybeSend + MaybeSync + 'static,
        params: impl ModuleInitParams,
    ) -> Self {
        self.params
            .attach_config_gen_params(self.id, server.module_kind(), params);
        self.clients.push(DynClientModuleInit::from(client));
        self.servers.push(DynServerModuleInit::from(server));
        self.id += 1;

        self
    }

    /// Starts a new federation with default number of peers for testing
    pub async fn new_fed(&self) -> FederationTest {
        self.new_fed_with_peers(self.num_peers).await
    }

    /// Starts a new federation with number of peers
    pub async fn new_fed_with_peers(&self, num_peers: u16) -> FederationTest {
        info!(target: LOG_TEST, num_peers, "Setting federation with peers");
        FederationTest::new(
            num_peers,
            tokio::task::block_in_place(|| fedimint_portalloc::port_alloc(num_peers * 2))
                .expect("Failed to allocate a port range"),
            self.params.clone(),
            ServerModuleInitRegistry::from(self.servers.clone()),
            ClientModuleInitRegistry::from(self.clients.clone()),
            self.primary_client,
        )
        .await
    }

    /// Starts a new gateway with a given lightning node
    pub async fn new_gateway(
        &self,
        ln: Box<dyn LightningTest>,
        num_route_hints: u32,
        cli_password: Option<String>,
    ) -> GatewayTest {
        // TODO: Make construction easier
        let server_gens = ServerModuleInitRegistry::from(self.servers.clone());
        let module_kinds = self.params.iter_modules().map(|(id, kind, _)| (id, kind));
        let decoders = server_gens.available_decoders(module_kinds).unwrap();
        let clients = self.clients.clone().into_iter();

        GatewayTest::new(
            tokio::task::block_in_place(|| fedimint_portalloc::port_alloc(1))
                .expect("Failed to allocate a port range"),
            cli_password,
            ln,
            decoders,
            ClientModuleInitRegistry::from_iter(clients.filter(|client| {
                // Remove LN module because the gateway adds one
                client.to_dyn_common().module_kind() != ModuleKind::from_static_str("ln")
            })),
            num_route_hints,
        )
        .await
    }

    /// Returns the LND lightning node
    pub async fn lnd(&self) -> Box<dyn LightningTest> {
        match Fixtures::is_real_test() {
            true => Box::new(LndLightningTest::new().await),
            false => Box::new(FakeLightningTest::new()),
        }
    }

    /// Returns the CLN lightning node
    pub async fn cln(&self) -> Box<dyn LightningTest> {
        match Fixtures::is_real_test() {
            true => Box::new(ClnLightningTest::new().await),
            false => Box::new(FakeLightningTest::new()),
        }
    }

    /// Spawns and returns a newly created LDK Node
    pub async fn spawn_ldk(bitcoin: Arc<dyn BitcoinTest>) -> LdkLightningTest {
        let db_dir = test_dir(&format!("LDKNode-{}", rand::random::<u64>())).0;
        LdkLightningTest::new(db_dir, bitcoin.clone())
            .await
            .expect("Error spawning LDK Node")
    }

    /// Get a server bitcoin RPC config
    pub fn bitcoin_server(&self) -> BitcoinRpcConfig {
        self.bitcoin_rpc.clone()
    }

    /// Get a client bitcoin RPC config
    // TODO: Right now we only support mocks or esplora, we should support others in
    // the future
    pub fn bitcoin_client(&self) -> BitcoinRpcConfig {
        match Fixtures::is_real_test() {
            true => BitcoinRpcConfig {
                kind: "esplora".to_string(),
                url: SafeUrl::parse(&format!(
                    "http://127.0.0.1:{}/",
                    env::var("FM_PORT_ESPLORA").unwrap_or(String::from("50002"))
                ))
                .expect("Failed to parse default esplora server"),
            },
            false => self.bitcoin_rpc.clone(),
        }
    }

    /// Get a test bitcoin fixture
    pub fn bitcoin(&self) -> Arc<dyn BitcoinTest> {
        self.bitcoin.clone()
    }

    pub fn dyn_bitcoin_rpc(&self) -> DynBitcoindRpc {
        self.dyn_bitcoin_rpc.clone()
    }
}

/// If `FM_TEST_DIR` is set, use it as a base, otherwise use a tempdir
///
/// Callers must hold onto the tempdir until it is no longer needed
pub fn test_dir(pathname: &str) -> (PathBuf, Option<TempDir>) {
    let (parent, maybe_tmp_dir_guard) = match env::var("FM_TEST_DIR") {
        Ok(directory) => (directory, None),
        Err(_) => {
            let random = format!("test-{}", rand::random::<u64>());
            let guard = tempfile::Builder::new().prefix(&random).tempdir().unwrap();
            let directory = guard.path().to_str().unwrap().to_owned();
            (directory, Some(guard))
        }
    };
    let fullpath = PathBuf::from(parent).join(pathname);
    fs::create_dir_all(fullpath.clone()).expect("Can make dirs");
    (fullpath, maybe_tmp_dir_guard)
}