Browse Source

feat: use only one table up (#590)

thesimplekid 1 month ago
parent
commit
8aa00fb0b2

+ 1 - 1
crates/cdk-integration-tests/Cargo.toml

@@ -36,7 +36,7 @@ uuid = { version = "1", features = ["v4"] }
 serde = "1"
 serde_json = "1"
 # ln-regtest-rs = { path = "../../../../ln-regtest-rs" }
-ln-regtest-rs = { git = "https://github.com/thesimplekid/ln-regtest-rs", rev = "f9e7bbbb" }
+ln-regtest-rs = { git = "https://github.com/thesimplekid/ln-regtest-rs", rev = "bf09ad6" }
 lightning-invoice = { version = "0.32.0", features = ["serde", "std"] }
 tracing = { version = "0.1", default-features = false, features = [
     "attributes",

+ 1 - 1
crates/cdk-redb/Cargo.toml

@@ -18,7 +18,7 @@ wallet = []
 [dependencies]
 async-trait = "0.1"
 cdk-common = { path = "../cdk-common", version = "0.7.0" }
-redb = "2.1.0"
+redb = "2.2.0"
 thiserror = "1"
 tracing = { version = "0.1", default-features = false, features = [
     "attributes",

+ 3 - 0
crates/cdk-redb/src/error.rs

@@ -58,6 +58,9 @@ pub enum Error {
     /// Unknown Proof Y
     #[error("Unknown proof Y")]
     UnknownY,
+    /// Unknown Quote
+    #[error("Unknown quote")]
+    UnknownQuote,
     /// Unknown Database Version
     #[error("Unknown database version")]
     UnknownDatabaseVersion,

+ 46 - 51
crates/cdk-redb/src/mint/mod.rs

@@ -295,38 +295,36 @@ impl MintDatabase for MintRedbDatabase {
     ) -> Result<MintQuoteState, Self::Err> {
         let write_txn = self.db.begin_write().map_err(Error::from)?;
 
-        let mut mint_quote: MintQuote;
+        let current_state;
         {
-            let table = write_txn
+            let mut mint_quote: MintQuote;
+            let mut table = write_txn
                 .open_table(MINT_QUOTES_TABLE)
                 .map_err(Error::from)?;
+            {
+                let quote_guard = table
+                    .get(quote_id.as_bytes())
+                    .map_err(Error::from)?
+                    .ok_or(Error::UnknownQuote)?;
 
-            let quote_guard = table
-                .get(quote_id.as_bytes())
-                .map_err(Error::from)?
-                .ok_or(Error::UnknownMintInfo)?;
-
-            let quote = quote_guard.value();
+                let quote = quote_guard.value();
 
-            mint_quote = serde_json::from_str(quote).map_err(Error::from)?;
-        }
+                mint_quote = serde_json::from_str(quote).map_err(Error::from)?;
+            }
 
-        let current_state = mint_quote.state;
-        mint_quote.state = state;
+            current_state = mint_quote.state;
+            mint_quote.state = state;
 
-        {
-            let mut table = write_txn
-                .open_table(MINT_QUOTES_TABLE)
-                .map_err(Error::from)?;
-
-            table
-                .insert(
-                    quote_id.as_bytes(),
-                    serde_json::to_string(&mint_quote)
-                        .map_err(Error::from)?
-                        .as_str(),
-                )
-                .map_err(Error::from)?;
+            {
+                table
+                    .insert(
+                        quote_id.as_bytes(),
+                        serde_json::to_string(&mint_quote)
+                            .map_err(Error::from)?
+                            .as_str(),
+                    )
+                    .map_err(Error::from)?;
+            }
         }
         write_txn.commit().map_err(Error::from)?;
 
@@ -454,39 +452,36 @@ impl MintDatabase for MintRedbDatabase {
     ) -> Result<MeltQuoteState, Self::Err> {
         let write_txn = self.db.begin_write().map_err(Error::from)?;
 
-        let mut melt_quote: mint::MeltQuote;
-
+        let current_state;
         {
-            let table = write_txn
+            let mut melt_quote: mint::MeltQuote;
+            let mut table = write_txn
                 .open_table(MELT_QUOTES_TABLE)
                 .map_err(Error::from)?;
+            {
+                let quote_guard = table
+                    .get(quote_id.as_bytes())
+                    .map_err(Error::from)?
+                    .ok_or(Error::UnknownQuote)?;
 
-            let quote_guard = table
-                .get(quote_id.as_bytes())
-                .map_err(Error::from)?
-                .ok_or(Error::UnknownMintInfo)?;
-
-            let quote = quote_guard.value();
-
-            melt_quote = serde_json::from_str(quote).map_err(Error::from)?;
-        }
+                let quote = quote_guard.value();
 
-        let current_state = melt_quote.state;
-        melt_quote.state = state;
+                melt_quote = serde_json::from_str(quote).map_err(Error::from)?;
+            }
 
-        {
-            let mut table = write_txn
-                .open_table(MELT_QUOTES_TABLE)
-                .map_err(Error::from)?;
+            current_state = melt_quote.state;
+            melt_quote.state = state;
 
-            table
-                .insert(
-                    quote_id.as_bytes(),
-                    serde_json::to_string(&melt_quote)
-                        .map_err(Error::from)?
-                        .as_str(),
-                )
-                .map_err(Error::from)?;
+            {
+                table
+                    .insert(
+                        quote_id.as_bytes(),
+                        serde_json::to_string(&melt_quote)
+                            .map_err(Error::from)?
+                            .as_str(),
+                    )
+                    .map_err(Error::from)?;
+            }
         }
         write_txn.commit().map_err(Error::from)?;