Browse Source

chore: instrument logging on wallet db

thesimplekid 7 tháng trước cách đây
mục cha
commit
fa3f6c4b38
2 tập tin đã thay đổi với 62 bổ sung15 xóa
  1. 19 13
      crates/cdk-redb/src/wallet/mod.rs
  2. 43 2
      crates/cdk-sqlite/src/wallet/mod.rs

+ 19 - 13
crates/cdk-redb/src/wallet/mod.rs

@@ -350,7 +350,7 @@ impl WalletDatabase for WalletRedbDatabase {
         }
     }
 
-    #[instrument(skip(self))]
+    #[instrument(skip(self), fields(keyset_id = %keyset_id))]
     async fn get_keyset_by_id(&self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Self::Err> {
         let db = self.db.lock().await;
         let read_txn = db.begin_read().map_err(Into::<Error>::into)?;
@@ -514,28 +514,33 @@ impl WalletDatabase for WalletRedbDatabase {
         Ok(())
     }
 
-    #[instrument(skip(self))]
-    async fn get_keys(&self, id: &Id) -> Result<Option<Keys>, Self::Err> {
+    #[instrument(skip(self), fields(keyset_id = %keyset_id))]
+    async fn get_keys(&self, keyset_id: &Id) -> Result<Option<Keys>, Self::Err> {
         let db = self.db.lock().await;
         let read_txn = db.begin_read().map_err(Error::from)?;
         let table = read_txn.open_table(MINT_KEYS_TABLE).map_err(Error::from)?;
 
-        if let Some(mint_info) = table.get(id.to_string().as_str()).map_err(Error::from)? {
+        if let Some(mint_info) = table
+            .get(keyset_id.to_string().as_str())
+            .map_err(Error::from)?
+        {
             return Ok(serde_json::from_str(mint_info.value()).map_err(Error::from)?);
         }
 
         Ok(None)
     }
 
-    #[instrument(skip(self))]
-    async fn remove_keys(&self, id: &Id) -> Result<(), Self::Err> {
+    #[instrument(skip(self), fields(keyset_id = %keyset_id))]
+    async fn remove_keys(&self, keyset_id: &Id) -> Result<(), Self::Err> {
         let db = self.db.lock().await;
         let write_txn = db.begin_write().map_err(Error::from)?;
 
         {
             let mut table = write_txn.open_table(MINT_KEYS_TABLE).map_err(Error::from)?;
 
-            table.remove(id.to_string().as_str()).map_err(Error::from)?;
+            table
+                .remove(keyset_id.to_string().as_str())
+                .map_err(Error::from)?;
         }
 
         write_txn.commit().map_err(Error::from)?;
@@ -568,7 +573,7 @@ impl WalletDatabase for WalletRedbDatabase {
         Ok(())
     }
 
-    #[instrument(skip(self))]
+    #[instrument(skip_all)]
     async fn get_proofs(
         &self,
         mint_url: Option<UncheckedUrl>,
@@ -626,7 +631,7 @@ impl WalletDatabase for WalletRedbDatabase {
         Ok(())
     }
 
-    #[instrument(skip(self))]
+    #[instrument(skip(self, y))]
     async fn set_proof_state(&self, y: PublicKey, state: State) -> Result<(), Self::Err> {
         let db = self.db.lock().await;
         let read_txn = db.begin_read().map_err(Error::from)?;
@@ -662,7 +667,7 @@ impl WalletDatabase for WalletRedbDatabase {
         Ok(())
     }
 
-    #[instrument(skip(self))]
+    #[instrument(skip(self), fields(keyset_id = %keyset_id))]
     async fn increment_keyset_counter(&self, keyset_id: &Id, count: u32) -> Result<(), Self::Err> {
         let db = self.db.lock().await;
 
@@ -694,7 +699,7 @@ impl WalletDatabase for WalletRedbDatabase {
         Ok(())
     }
 
-    #[instrument(skip(self))]
+    #[instrument(skip(self), fields(keyset_id = %keyset_id))]
     async fn get_keyset_counter(&self, keyset_id: &Id) -> Result<Option<u32>, Self::Err> {
         let db = self.db.lock().await;
         let read_txn = db.begin_read().map_err(Error::from)?;
@@ -707,7 +712,7 @@ impl WalletDatabase for WalletRedbDatabase {
         Ok(counter.map(|c| c.value()))
     }
 
-    #[instrument(skip(self))]
+    #[instrument(skip_all)]
     async fn get_nostr_last_checked(
         &self,
         verifying_key: &PublicKey,
@@ -724,7 +729,8 @@ impl WalletDatabase for WalletRedbDatabase {
 
         Ok(last_checked.map(|c| c.value()))
     }
-    #[instrument(skip(self))]
+
+    #[instrument(skip(self, verifying_key))]
     async fn add_nostr_last_checked(
         &self,
         verifying_key: PublicKey,

+ 43 - 2
crates/cdk-sqlite/src/wallet/mod.rs

@@ -19,6 +19,7 @@ use cdk::wallet::MintQuote;
 use error::Error;
 use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqliteRow};
 use sqlx::{ConnectOptions, Row};
+use tracing::instrument;
 
 pub mod error;
 
@@ -58,6 +59,7 @@ impl WalletSqliteDatabase {
 impl WalletDatabase for WalletSqliteDatabase {
     type Err = cdk_database::Error;
 
+    #[instrument(skip(self, mint_info))]
     async fn add_mint(
         &self,
         mint_url: UncheckedUrl,
@@ -114,6 +116,7 @@ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
         Ok(())
     }
 
+    #[instrument(skip(self))]
     async fn remove_mint(&self, mint_url: UncheckedUrl) -> Result<(), Self::Err> {
         sqlx::query(
             r#"
@@ -129,6 +132,7 @@ WHERE mint_url=?
         Ok(())
     }
 
+    #[instrument(skip(self))]
     async fn get_mint(&self, mint_url: UncheckedUrl) -> Result<Option<MintInfo>, Self::Err> {
         let rec = sqlx::query(
             r#"
@@ -151,6 +155,8 @@ WHERE mint_url=?;
 
         Ok(Some(sqlite_row_to_mint_info(&rec)?))
     }
+
+    #[instrument(skip(self))]
     async fn get_mints(&self) -> Result<HashMap<UncheckedUrl, Option<MintInfo>>, Self::Err> {
         let rec = sqlx::query(
             r#"
@@ -176,6 +182,7 @@ FROM mint
         Ok(mints)
     }
 
+    #[instrument(skip(self))]
     async fn update_mint_url(
         &self,
         old_mint_url: UncheckedUrl,
@@ -202,6 +209,7 @@ FROM mint
         Ok(())
     }
 
+    #[instrument(skip(self, keysets))]
     async fn add_mint_keysets(
         &self,
         mint_url: UncheckedUrl,
@@ -227,6 +235,8 @@ VALUES (?, ?, ?, ?, ?);
 
         Ok(())
     }
+
+    #[instrument(skip(self))]
     async fn get_mint_keysets(
         &self,
         mint_url: UncheckedUrl,
@@ -257,6 +267,8 @@ WHERE mint_url=?
             true => Ok(None),
         }
     }
+
+    #[instrument(skip(self), fields(keyset_id = %keyset_id))]
     async fn get_keyset_by_id(&self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Self::Err> {
         let rec = sqlx::query(
             r#"
@@ -280,6 +292,7 @@ WHERE id=?
         Ok(Some(sqlite_row_to_keyset(&rec)?))
     }
 
+    #[instrument(skip_all)]
     async fn add_mint_quote(&self, quote: MintQuote) -> Result<(), Self::Err> {
         sqlx::query(
             r#"
@@ -301,6 +314,8 @@ VALUES (?, ?, ?, ?, ?, ?, ?);
 
         Ok(())
     }
+
+    #[instrument(skip(self))]
     async fn get_mint_quote(&self, quote_id: &str) -> Result<Option<MintQuote>, Self::Err> {
         let rec = sqlx::query(
             r#"
@@ -323,6 +338,8 @@ WHERE id=?;
 
         Ok(Some(sqlite_row_to_mint_quote(&rec)?))
     }
+
+    #[instrument(skip(self))]
     async fn get_mint_quotes(&self) -> Result<Vec<MintQuote>, Self::Err> {
         let rec = sqlx::query(
             r#"
@@ -338,6 +355,8 @@ FROM mint_quote
 
         Ok(mint_quotes)
     }
+
+    #[instrument(skip(self))]
     async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Self::Err> {
         sqlx::query(
             r#"
@@ -353,6 +372,7 @@ WHERE id=?
         Ok(())
     }
 
+    #[instrument(skip_all)]
     async fn add_melt_quote(&self, quote: wallet::MeltQuote) -> Result<(), Self::Err> {
         sqlx::query(
             r#"
@@ -374,6 +394,8 @@ VALUES (?, ?, ?, ?, ?, ?, ?);
 
         Ok(())
     }
+
+    #[instrument(skip(self))]
     async fn get_melt_quote(&self, quote_id: &str) -> Result<Option<wallet::MeltQuote>, Self::Err> {
         let rec = sqlx::query(
             r#"
@@ -396,6 +418,8 @@ WHERE id=?;
 
         Ok(Some(sqlite_row_to_melt_quote(&rec)?))
     }
+
+    #[instrument(skip(self))]
     async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Self::Err> {
         sqlx::query(
             r#"
@@ -411,6 +435,7 @@ WHERE id=?
         Ok(())
     }
 
+    #[instrument(skip_all)]
     async fn add_keys(&self, keys: Keys) -> Result<(), Self::Err> {
         sqlx::query(
             r#"
@@ -427,7 +452,9 @@ VALUES (?, ?);
 
         Ok(())
     }
-    async fn get_keys(&self, id: &Id) -> Result<Option<Keys>, Self::Err> {
+
+    #[instrument(skip(self), fields(keyset_id = %keyset_id))]
+    async fn get_keys(&self, keyset_id: &Id) -> Result<Option<Keys>, Self::Err> {
         let rec = sqlx::query(
             r#"
 SELECT *
@@ -435,7 +462,7 @@ FROM key
 WHERE id=?;
         "#,
         )
-        .bind(id.to_string())
+        .bind(keyset_id.to_string())
         .fetch_one(&self.pool)
         .await;
 
@@ -451,6 +478,8 @@ WHERE id=?;
 
         Ok(serde_json::from_str(&keys).map_err(Error::from)?)
     }
+
+    #[instrument(skip(self))]
     async fn remove_keys(&self, id: &Id) -> Result<(), Self::Err> {
         sqlx::query(
             r#"
@@ -466,6 +495,7 @@ WHERE id=?
         Ok(())
     }
 
+    #[instrument(skip_all)]
     async fn add_proofs(&self, proof_info: Vec<ProofInfo>) -> Result<(), Self::Err> {
         for proof in proof_info {
             sqlx::query(
@@ -501,6 +531,8 @@ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
 
         Ok(())
     }
+
+    #[instrument(skip(self, state, spending_conditions))]
     async fn get_proofs(
         &self,
         mint_url: Option<UncheckedUrl>,
@@ -551,6 +583,8 @@ FROM proof;
             true => return Ok(vec![]),
         }
     }
+
+    #[instrument(skip_all)]
     async fn remove_proofs(&self, proofs: &Proofs) -> Result<(), Self::Err> {
         // TODO: Generate a IN clause
         for proof in proofs {
@@ -569,6 +603,7 @@ WHERE y = ?
         Ok(())
     }
 
+    #[instrument(skip(self, y))]
     async fn set_proof_state(&self, y: PublicKey, state: State) -> Result<(), Self::Err> {
         sqlx::query(
             r#"
@@ -586,6 +621,7 @@ WHERE y IS ?;
         Ok(())
     }
 
+    #[instrument(skip(self), fields(keyset_id = %keyset_id))]
     async fn increment_keyset_counter(&self, keyset_id: &Id, count: u32) -> Result<(), Self::Err> {
         sqlx::query(
             r#"
@@ -602,6 +638,8 @@ WHERE id IS ?;
 
         Ok(())
     }
+
+    #[instrument(skip(self), fields(keyset_id = %keyset_id))]
     async fn get_keyset_counter(&self, keyset_id: &Id) -> Result<Option<u32>, Self::Err> {
         let rec = sqlx::query(
             r#"
@@ -628,6 +666,7 @@ WHERE id=?;
         Ok(count)
     }
 
+    #[instrument(skip_all)]
     async fn get_nostr_last_checked(
         &self,
         verifying_key: &PublicKey,
@@ -656,6 +695,8 @@ WHERE key=?;
 
         Ok(count)
     }
+
+    #[instrument(skip_all)]
     async fn add_nostr_last_checked(
         &self,
         verifying_key: PublicKey,