fedimint-client-module 0.12.0

Library for sending transactions to the Fedimint federation.
Documentation
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
pub mod recovery;

use std::collections::{BTreeMap, BTreeSet};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use anyhow::bail;
use fedimint_api_client::api::{DynGlobalApi, DynModuleApi};
use fedimint_bitcoind::DynBitcoindRpc;
use fedimint_connectors::ConnectorRegistry;
use fedimint_core::config::FederationId;
use fedimint_core::core::ModuleKind;
use fedimint_core::db::{Database, DatabaseVersion};
use fedimint_core::module::{ApiAuth, ApiVersion, CommonModuleInit, ModuleInit, MultiApiVersion};
use fedimint_core::task::{MaybeSend, ShuttingDownError, TaskGroup, TaskHandle};
use fedimint_core::util::SafeUrl;
use fedimint_core::{Amount, ChainId, NumPeers, apply, async_trait_maybe_send};
use fedimint_derive_secret::DerivableSecret;
use fedimint_logging::LOG_CLIENT;
use tokio::sync::oneshot;
use tracing::{Span, warn};

use super::ClientContext;
use super::recovery::RecoveryProgress;
use crate::db::ClientModuleMigrationFn;
use crate::module::ClientModule;
use crate::sm::ModuleNotifier;

/// Factory function type for creating a Bitcoin RPC client from a chain ID.
///
/// This allows applications to provide their own Bitcoin RPC client
/// implementation based on the chain the federation operates on.
pub type BitcoindRpcFactory = Box<
    dyn FnOnce(ChainId) -> Pin<Box<dyn Future<Output = Option<DynBitcoindRpc>> + Send>>
        + Send
        + Sync,
>;

/// Factory function type for creating a Bitcoin RPC client from a URL.
///
/// This is used when the federation does not have ChainId support yet.
/// The factory receives a URL (typically from the module config) and can be
/// called to get an RPC client.
pub type BitcoindRpcNoChainIdFactory = Arc<
    dyn Fn(SafeUrl) -> Pin<Box<dyn Future<Output = Option<DynBitcoindRpc>> + Send>> + Send + Sync,
>;

pub struct ClientModuleInitArgs<C>
where
    C: ClientModuleInit,
{
    pub federation_id: FederationId,
    pub peer_num: usize,
    pub cfg: <<C as ModuleInit>::Common as CommonModuleInit>::ClientConfig,
    pub db: Database,
    pub core_api_version: ApiVersion,
    pub module_api_version: ApiVersion,
    pub module_root_secret: DerivableSecret,
    pub notifier: ModuleNotifier<<<C as ClientModuleInit>::Module as ClientModule>::States>,
    pub api: DynGlobalApi,
    pub admin_auth: Option<ApiAuth>,
    pub module_api: DynModuleApi,
    pub context: ClientContext<<C as ClientModuleInit>::Module>,
    pub task_group: TaskGroup,
    /// Long-lived span carrying `fed_id`. Use [`Self::spawn_cancellable`] /
    /// [`Self::spawn`] (or pass [`Self::client_span`] to
    /// [`TaskGroup::spawn_cancellable_with_span`]) so log events from
    /// background tasks carry the federation prefix.
    pub client_span: Span,
    pub connector_registry: ConnectorRegistry,
    /// User-provided Bitcoin RPC client
    ///
    /// If set by the application using `ClientBuilder::with_bitcoind_rpc`,
    /// modules (particularly the wallet module) can use this instead of
    /// creating their own Bitcoin RPC connection.
    pub user_bitcoind_rpc: Option<DynBitcoindRpc>,
    /// User-provided Bitcoin RPC factory for when ChainId is not available
    ///
    /// If set by the application using
    /// `ClientBuilder::with_bitcoind_rpc_no_chain_id`, modules can call
    /// this with a URL from their config to get an RPC client. This is used
    /// as a fallback when `user_bitcoind_rpc` is None.
    pub user_bitcoind_rpc_no_chain_id: Option<BitcoindRpcNoChainIdFactory>,
}

impl<C> ClientModuleInitArgs<C>
where
    C: ClientModuleInit,
{
    pub fn federation_id(&self) -> &FederationId {
        &self.federation_id
    }

    pub fn peer_num(&self) -> usize {
        self.peer_num
    }

    pub fn cfg(&self) -> &<<C as ModuleInit>::Common as CommonModuleInit>::ClientConfig {
        &self.cfg
    }

    pub fn db(&self) -> &Database {
        &self.db
    }

    pub fn core_api_version(&self) -> &ApiVersion {
        &self.core_api_version
    }

    pub fn module_api_version(&self) -> &ApiVersion {
        &self.module_api_version
    }

    pub fn module_root_secret(&self) -> &DerivableSecret {
        &self.module_root_secret
    }

    pub fn notifier(
        &self,
    ) -> &ModuleNotifier<<<C as ClientModuleInit>::Module as ClientModule>::States> {
        &self.notifier
    }

    pub fn api(&self) -> &DynGlobalApi {
        &self.api
    }

    pub fn admin_auth(&self) -> Option<&ApiAuth> {
        self.admin_auth.as_ref()
    }

    pub fn module_api(&self) -> &DynModuleApi {
        &self.module_api
    }

    /// Get the [`ClientContext`] for later use
    ///
    /// Notably `ClientContext` can not be used during `ClientModuleInit::init`,
    /// as the outer context is not yet complete. But it can be stored to be
    /// used in the methods of [`ClientModule`], at which point it will be
    /// ready.
    pub fn context(&self) -> ClientContext<<C as ClientModuleInit>::Module> {
        self.context.clone()
    }

    pub fn task_group(&self) -> &TaskGroup {
        &self.task_group
    }

    /// Long-lived span identifying this client (with `fed_id`).
    pub fn client_span(&self) -> &Span {
        &self.client_span
    }

    /// Spawn a cancellable task on the client's task group, parented to the
    /// client's span so all events from the task carry `fed_id` (including
    /// the lifecycle events emitted by [`TaskGroup`] itself).
    pub fn spawn_cancellable<R>(
        &self,
        name: impl Into<String>,
        future: impl std::future::Future<Output = R> + MaybeSend + 'static,
    ) -> oneshot::Receiver<Result<R, ShuttingDownError>>
    where
        R: MaybeSend + 'static,
    {
        self.task_group
            .spawn_cancellable_with_span(self.client_span.clone(), name, future)
    }

    /// Spawn a task on the client's task group, parented to the client's span.
    pub fn spawn<Fut, R>(
        &self,
        name: impl Into<String>,
        f: impl FnOnce(TaskHandle) -> Fut + MaybeSend + 'static,
    ) -> oneshot::Receiver<R>
    where
        Fut: std::future::Future<Output = R> + MaybeSend + 'static,
        R: MaybeSend + 'static,
    {
        self.task_group
            .spawn_with_span(self.client_span.clone(), name, f)
    }

    pub fn connector_registry(&self) -> &ConnectorRegistry {
        &self.connector_registry
    }

    /// Returns the user-provided Bitcoin RPC client, if any
    ///
    /// Modules (particularly the wallet module) should check this first
    /// before creating their own Bitcoin RPC connection.
    pub fn user_bitcoind_rpc(&self) -> Option<&DynBitcoindRpc> {
        self.user_bitcoind_rpc.as_ref()
    }

    /// Returns the user-provided Bitcoin RPC factory for when ChainId is not
    /// available
    ///
    /// Modules can call this with a URL from their config to get an RPC client.
    /// This is used as a fallback when `user_bitcoind_rpc()` returns None.
    pub fn user_bitcoind_rpc_no_chain_id(&self) -> Option<&BitcoindRpcNoChainIdFactory> {
        self.user_bitcoind_rpc_no_chain_id.as_ref()
    }
}

/// How a module's recovery relates to using the module.
///
/// A recovering client runs a recovery for every module that has one, and only
/// the modules that can be used while that recovery runs join the module
/// registry; the rest become available once the client is reopened with their
/// recovery complete. This is a module's declaration of which of those it is,
/// returned from [`ClientModuleInit::recovery_mode`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecoveryMode {
    /// The module implements no recovery.
    ///
    /// It is left out of a recovering client's recovery entirely: no recovery
    /// is started for it and it is initialized as it would be on any other
    /// client open, rather than being held back for a recovery that would do
    /// nothing.
    None,
    /// The module has a recovery and cannot be used while it runs.
    ///
    /// The recovery runs, but the module stays out of the module registry — and
    /// is therefore unusable — until the client is reopened with the recovery
    /// complete.
    Unusable,
    /// The module has a recovery and can be used while it runs.
    ///
    /// [`ClientModuleInit::prepare_recovery`] commits the boundary between the
    /// recovery and live operation, after which the module is initialized and
    /// usable straight away, with its recovery running in the background.
    Usable,
}

/// Arguments to [`ClientModuleInit::prepare_recovery`], which runs before the
/// module exists and so gets only what it takes to record where the recovery
/// ends and live operation begins.
pub struct ClientModuleRecoveryPrepareArgs {
    pub db: Database,
    pub module_api: DynModuleApi,
}

impl ClientModuleRecoveryPrepareArgs {
    /// Database isolated for this module instance
    pub fn db(&self) -> &Database {
        &self.db
    }

    /// Api of this module instance
    pub fn module_api(&self) -> &DynModuleApi {
        &self.module_api
    }
}

pub struct ClientModuleRecoverArgs<C>
where
    C: ClientModuleInit,
{
    pub federation_id: FederationId,
    pub num_peers: NumPeers,
    pub cfg: <<C as ModuleInit>::Common as CommonModuleInit>::ClientConfig,
    pub db: Database,
    pub core_api_version: ApiVersion,
    pub module_api_version: ApiVersion,
    pub module_root_secret: DerivableSecret,
    pub notifier: ModuleNotifier<<<C as ClientModuleInit>::Module as ClientModule>::States>,
    pub api: DynGlobalApi,
    pub admin_auth: Option<ApiAuth>,
    pub module_api: DynModuleApi,
    pub context: ClientContext<<C as ClientModuleInit>::Module>,
    pub progress_tx: tokio::sync::watch::Sender<RecoveryProgress>,
    pub task_group: TaskGroup,
    /// See [`ClientModuleInitArgs::client_span`].
    pub client_span: Span,
    /// User-provided Bitcoin RPC client
    ///
    /// If set by the application using `ClientBuilder::with_bitcoind_rpc`,
    /// modules (particularly the wallet module) can use this instead of
    /// creating their own Bitcoin RPC connection.
    pub user_bitcoind_rpc: Option<DynBitcoindRpc>,
    /// User-provided Bitcoin RPC factory for when ChainId is not available
    ///
    /// If set by the application using
    /// `ClientBuilder::with_bitcoind_rpc_no_chain_id`, modules can call
    /// this with a URL from their config to get an RPC client. This is used
    /// as a fallback when `user_bitcoind_rpc` is None.
    pub user_bitcoind_rpc_no_chain_id: Option<BitcoindRpcNoChainIdFactory>,
}

impl<C> ClientModuleRecoverArgs<C>
where
    C: ClientModuleInit,
{
    pub fn federation_id(&self) -> &FederationId {
        &self.federation_id
    }

    pub fn num_peers(&self) -> NumPeers {
        self.num_peers
    }

    pub fn cfg(&self) -> &<<C as ModuleInit>::Common as CommonModuleInit>::ClientConfig {
        &self.cfg
    }

    pub fn db(&self) -> &Database {
        &self.db
    }

    pub fn task_group(&self) -> &TaskGroup {
        &self.task_group
    }

    /// Long-lived span identifying this client (with `fed_id`).
    pub fn client_span(&self) -> &Span {
        &self.client_span
    }

    /// Spawn a cancellable task on the client's task group, parented to the
    /// client's span so all events from the task carry `fed_id`.
    pub fn spawn_cancellable<R>(
        &self,
        name: impl Into<String>,
        future: impl std::future::Future<Output = R> + MaybeSend + 'static,
    ) -> oneshot::Receiver<Result<R, ShuttingDownError>>
    where
        R: MaybeSend + 'static,
    {
        self.task_group
            .spawn_cancellable_with_span(self.client_span.clone(), name, future)
    }

    /// Spawn a task on the client's task group, parented to the client's span.
    pub fn spawn<Fut, R>(
        &self,
        name: impl Into<String>,
        f: impl FnOnce(TaskHandle) -> Fut + MaybeSend + 'static,
    ) -> oneshot::Receiver<R>
    where
        Fut: std::future::Future<Output = R> + MaybeSend + 'static,
        R: MaybeSend + 'static,
    {
        self.task_group
            .spawn_with_span(self.client_span.clone(), name, f)
    }

    pub fn core_api_version(&self) -> &ApiVersion {
        &self.core_api_version
    }

    pub fn module_api_version(&self) -> &ApiVersion {
        &self.module_api_version
    }

    pub fn module_root_secret(&self) -> &DerivableSecret {
        &self.module_root_secret
    }

    pub fn notifier(
        &self,
    ) -> &ModuleNotifier<<<C as ClientModuleInit>::Module as ClientModule>::States> {
        &self.notifier
    }

    pub fn api(&self) -> &DynGlobalApi {
        &self.api
    }

    pub fn admin_auth(&self) -> Option<&ApiAuth> {
        self.admin_auth.as_ref()
    }

    pub fn module_api(&self) -> &DynModuleApi {
        &self.module_api
    }

    /// Get the [`ClientContext`]
    ///
    /// Notably `ClientContext`, unlike [`ClientModuleInitArgs::context`],
    /// the client context is guaranteed to be usable immediately.
    pub fn context(&self) -> ClientContext<<C as ClientModuleInit>::Module> {
        self.context.clone()
    }

    pub fn update_recovery_progress(&self, progress: RecoveryProgress) {
        // we want a warning if the send channel was not connected to
        #[allow(clippy::disallowed_methods)]
        if progress.is_done() {
            // Recovery is complete when the recovery function finishes. To avoid
            // confusing any downstream code, we never send completed process.
            warn!(target: LOG_CLIENT, "Module trying to send a completed recovery progress. Ignoring");
        } else if progress.is_none() {
            // Recovery starts with "none" none progress. To avoid
            // confusing any downstream code, we never send none process afterwards.
            warn!(target: LOG_CLIENT, "Module trying to send a none recovery progress. Ignoring");
        } else if self.progress_tx.send(progress).is_err() {
            warn!(target: LOG_CLIENT, "Module trying to send a recovery progress but nothing is listening");
        }
    }

    /// Returns the user-provided Bitcoin RPC client, if any
    ///
    /// Modules (particularly the wallet module) should check this first
    /// before creating their own Bitcoin RPC connection.
    pub fn user_bitcoind_rpc(&self) -> Option<&DynBitcoindRpc> {
        self.user_bitcoind_rpc.as_ref()
    }

    /// Returns the user-provided Bitcoin RPC factory for when ChainId is not
    /// available
    ///
    /// Modules can call this with a URL from their config to get an RPC client.
    /// This is used as a fallback when `user_bitcoind_rpc()` returns None.
    pub fn user_bitcoind_rpc_no_chain_id(&self) -> Option<&BitcoindRpcNoChainIdFactory> {
        self.user_bitcoind_rpc_no_chain_id.as_ref()
    }
}

#[apply(async_trait_maybe_send!)]
pub trait ClientModuleInit: ModuleInit + Sized {
    type Module: ClientModule;

    /// Api versions of the corresponding server side module's API
    /// that this client module implementation can use.
    fn supported_api_versions(&self) -> MultiApiVersion;

    fn kind() -> ModuleKind {
        <Self::Module as ClientModule>::kind()
    }

    /// How this module's recovery relates to using the module, see
    /// [`RecoveryMode`].
    ///
    /// Must be overridden together with [`Self::recover`]: leaving this at
    /// [`RecoveryMode::None`] while implementing a recovery leaves that
    /// recovery unreachable, and overriding only this makes the module's
    /// recovery fail.
    fn recovery_mode(&self) -> RecoveryMode {
        RecoveryMode::None
    }

    /// Commit the boundary between a recovery and live operation.
    ///
    /// Called on every client open that starts or resumes a recovery, before
    /// [`Self::init`] and before the module joins the module registry.
    ///
    /// [`RecoveryMode::Usable`] is a claim that the recovery and the live
    /// module cannot interfere: the recovery must not rewrite state the module
    /// also writes, and must not rediscover what the live module is about to
    /// do. Whatever separates the two has to be recorded durably *here*,
    /// because everything after this point can run concurrently with the
    /// module. If this fails the client fails to open and neither the module
    /// nor its recovery is started, so [`Self::recover`] may rely on the
    /// boundary having been committed.
    ///
    /// Only called for modules whose [`Self::recovery_mode`] is
    /// [`RecoveryMode::Usable`].
    async fn prepare_recovery(
        &self,
        _args: &ClientModuleRecoveryPrepareArgs,
    ) -> anyhow::Result<()> {
        Ok(())
    }

    /// Recover the state of the client module, optionally from an existing
    /// snapshot.
    ///
    /// Only called for modules whose [`Self::recovery_mode`] is not
    /// [`RecoveryMode::None`].
    ///
    /// On success, returns the total amount recovered from this module, if the
    /// module tracks it (`None` for modules that can't determine the amount at
    /// recovery-completion time). This is surfaced in the
    /// `ModuleRecoveryCompleted` event.
    ///
    /// If `Err` is returned, the higher level client/application might try
    /// again at a different time (client restarted, code version changed, etc.)
    async fn recover(
        &self,
        _args: &ClientModuleRecoverArgs<Self>,
        _snapshot: Option<&<Self::Module as ClientModule>::Backup>,
    ) -> anyhow::Result<Option<Amount>> {
        bail!(
            "Module kind {} declares a recovery mode without implementing a recovery",
            <Self::Module as ClientModule>::kind()
        )
    }

    /// Initialize a [`ClientModule`] instance from its config
    async fn init(&self, args: &ClientModuleInitArgs<Self>) -> anyhow::Result<Self::Module>;

    /// Retrieves the database migrations from the module to be applied to the
    /// database before the module is initialized. The database migrations map
    /// is indexed on the "from" version.
    fn get_database_migrations(&self) -> BTreeMap<DatabaseVersion, ClientModuleMigrationFn> {
        BTreeMap::new()
    }

    /// Db prefixes used by the module
    ///
    /// If `Some` is returned, it should contain list of database
    /// prefixes actually used by the module for it's keys.
    ///
    /// In (some subset of) non-production tests,
    /// module database will be scanned for presence of keys
    /// that do not belong to this list to verify integrity
    /// of data and possibly catch any unforeseen bugs.
    fn used_db_prefixes(&self) -> Option<BTreeSet<u8>> {
        None
    }
}