Ver Fonte

walletdb: Implement options.

parazyd há 3 anos atrás
pai
commit
e50a0f3c8e
2 ficheiros alterados com 59 adições e 8 exclusões
  1. 50 5
      bin/darkfid/src/rpc_wallet.rs
  2. 9 3
      src/wallet/walletdb.rs

+ 50 - 5
bin/darkfid/src/rpc_wallet.rs

@@ -85,11 +85,8 @@ impl Darkfid {
 
 
         // Execute the query and see if we find a row
         // Execute the query and see if we find a row
         let row = match sqlx::query(params[0].as_str().unwrap()).fetch_one(&mut conn).await {
         let row = match sqlx::query(params[0].as_str().unwrap()).fetch_one(&mut conn).await {
-            Ok(v) => v,
-            Err(e) => {
-                error!("[RPC] wallet.query_row_single: Failed to execute SQL query: {}", e);
-                return server_error(RpcError::NoRowsFoundInWallet, id, None)
-            }
+            Ok(v) => Some(v),
+            Err(_) => None,
         };
         };
 
 
         // Try to decode the row into what was requested
         // Try to decode the row into what was requested
@@ -98,6 +95,11 @@ impl Darkfid {
         for (typ, col) in types.iter().zip(names) {
         for (typ, col) in types.iter().zip(names) {
             match typ {
             match typ {
                 QueryType::Integer => {
                 QueryType::Integer => {
+                    let Some(ref row) = row else {
+                        error!("[RPC] wallet.query_row_single: Got None for QueryType::Integer");
+                        return server_error(RpcError::NoRowsFoundInWallet, id, None)
+                    };
+
                     let value: i32 = match row.try_get(col) {
                     let value: i32 = match row.try_get(col) {
                         Ok(v) => v,
                         Ok(v) => v,
                         Err(e) => {
                         Err(e) => {
@@ -107,9 +109,51 @@ impl Darkfid {
                     };
                     };
 
 
                     ret.push(json!(value));
                     ret.push(json!(value));
+                    continue
                 }
                 }
 
 
                 QueryType::Blob => {
                 QueryType::Blob => {
+                    let Some(ref row) = row else {
+                        error!("[RPC] wallet.query_row_single: Got None for QueryType::Blob");
+                        return server_error(RpcError::NoRowsFoundInWallet, id, None)
+                    };
+
+                    let value: Vec<u8> = match row.try_get(col) {
+                        Ok(v) => v,
+                        Err(e) => {
+                            error!("[RPC] wallet.query_row_single: {}", e);
+                            return JsonError::new(ParseError, None, id).into()
+                        }
+                    };
+
+                    ret.push(json!(value));
+                    continue
+                }
+
+                QueryType::OptionInteger => {
+                    let Some(ref row) = row else {
+                        ret.push(json!(None::<i32>));
+                        continue
+                    };
+
+                    let value: i32 = match row.try_get(col) {
+                        Ok(v) => v,
+                        Err(e) => {
+                            error!("[RPC] wallet.query_row_single: {}", e);
+                            return JsonError::new(ParseError, None, id).into()
+                        }
+                    };
+
+                    ret.push(json!(value));
+                    continue
+                }
+
+                QueryType::OptionBlob => {
+                    let Some(ref row) = row else {
+                        ret.push(json!(None::<Vec<u8>>));
+                        continue
+                    };
+
                     let value: Vec<u8> = match row.try_get(col) {
                     let value: Vec<u8> = match row.try_get(col) {
                         Ok(v) => v,
                         Ok(v) => v,
                         Err(e) => {
                         Err(e) => {
@@ -119,6 +163,7 @@ impl Darkfid {
                     };
                     };
 
 
                     ret.push(json!(value));
                     ret.push(json!(value));
+                    continue
                 }
                 }
 
 
                 _ => unreachable!(),
                 _ => unreachable!(),

+ 9 - 3
src/wallet/walletdb.rs

@@ -40,12 +40,16 @@ pub async fn init_wallet(wallet_path: &str, wallet_pass: &str) -> Result<WalletP
 /// Types we want to allow to query from the SQL wallet
 /// Types we want to allow to query from the SQL wallet
 #[repr(u8)]
 #[repr(u8)]
 pub enum QueryType {
 pub enum QueryType {
-    /// Integer gets decoded into u64,
+    /// Integer gets decoded into u64
     Integer = 0x00,
     Integer = 0x00,
-    /// Blob gets decoded into Vec<u8>,
+    /// Blob gets decoded into Vec<u8>
     Blob = 0x01,
     Blob = 0x01,
+    /// OptionInteger gets decoded into Option<u64>
+    OptionInteger = 0x02,
+    /// OptionBlob gets decoded into Option<Vec<u8>>
+    OptionBlob = 0x03,
     /// Last type, increment this when you add new types.
     /// Last type, increment this when you add new types.
-    Last = 0x02,
+    Last = 0x04,
 }
 }
 
 
 impl From<u8> for QueryType {
 impl From<u8> for QueryType {
@@ -53,6 +57,8 @@ impl From<u8> for QueryType {
         match x {
         match x {
             0x00 => Self::Integer,
             0x00 => Self::Integer,
             0x01 => Self::Blob,
             0x01 => Self::Blob,
+            0x02 => Self::OptionInteger,
+            0x03 => Self::OptionBlob,
             _ => unimplemented!(),
             _ => unimplemented!(),
         }
         }
     }
     }