fedimint_client_module/sm/
dbtx.rs

1use fedimint_core::core::ModuleInstanceId;
2use fedimint_core::db::DatabaseTransaction;
3
4/// A transaction that acts as isolated for module code but can be accessed as a
5/// normal transaction in this crate.
6pub struct ClientSMDatabaseTransaction<'inner, 'parent> {
7    dbtx: &'inner mut DatabaseTransaction<'parent>,
8    module_instance: ModuleInstanceId,
9}
10
11impl<'inner, 'parent> ClientSMDatabaseTransaction<'inner, 'parent> {
12    pub fn new(
13        dbtx: &'inner mut DatabaseTransaction<'parent>,
14        module_instance: ModuleInstanceId,
15    ) -> Self {
16        Self {
17            dbtx,
18            module_instance,
19        }
20    }
21
22    /// Returns the isolated database transaction for the module.
23    pub fn module_tx(&mut self) -> DatabaseTransaction<'_> {
24        self.dbtx
25            .to_ref_with_prefix_module_id(self.module_instance)
26            .0
27            .into_nc()
28    }
29
30    /// Returns the non-isolated database transaction only accessible to the
31    /// client internal code. This is useful for submitting Fedimint
32    /// transactions from within state transitions.
33    // TODO: We don't want the client module to call this directly, ideally this
34    // would be private, but after we've split fedimint-client-module and fedimint-client
35    // we need to make it public.
36    #[doc(hidden)]
37    pub fn global_tx(&mut self) -> &mut DatabaseTransaction<'parent> {
38        self.dbtx
39    }
40
41    pub(crate) fn module_id(&self) -> ModuleInstanceId {
42        self.module_instance
43    }
44}