Эх сурвалжийг харах

walletdb: remove initialized field

ghassmo 4 жил өмнө
parent
commit
b75e8bc8c8

+ 1 - 1
src/bin/darkfid.rs

@@ -142,7 +142,7 @@ impl Darkfid {
     // --> {"method": "create_wallet", "params": []}
     // <-- {"result": true}
     async fn create_wallet(&self, id: Value, _params: Value) -> JsonResult {
-        match self.client.lock().await.init_db().await {
+        match self.client.lock().await.init_db() {
             Ok(()) => JsonResult::Resp(jsonresp(json!(true), id)),
             Err(e) => JsonResult::Err(jsonerr(ServerError(-32001), Some(e.to_string()), id)),
         }

+ 4 - 3
src/client.rs

@@ -57,7 +57,8 @@ impl Client {
         mint_params: bellman::groth16::Parameters<Bls12>,
         spend_params: bellman::groth16::Parameters<Bls12>,
     ) -> Result<Self> {
-        wallet.init_db().await?;
+
+        wallet.init_db()?;
 
         if wallet.get_keypairs()?.is_empty() {
             wallet.key_gen()?;
@@ -345,8 +346,8 @@ impl Client {
         Ok(())
     }
 
-    pub async fn init_db(&self) -> Result<()> {
-        self.wallet.init_db().await
+    pub fn init_db(&self) -> Result<()> {
+        self.wallet.init_db()
     }
 
     pub fn get_own_coins(&self) -> Result<Vec<OwnCoin>> {

+ 17 - 23
src/wallet/walletdb.rs

@@ -1,4 +1,4 @@
-use async_std::sync::{Arc, Mutex};
+use async_std::sync::Arc;
 use std::collections::HashMap;
 use std::path::{Path, PathBuf};
 
@@ -53,7 +53,6 @@ impl Balances {
 pub struct WalletDb {
     pub path: PathBuf,
     pub password: String,
-    pub initialized: Mutex<bool>,
 }
 
 impl WalletApi for WalletDb {
@@ -71,29 +70,24 @@ impl WalletDb {
         Ok(Arc::new(Self {
             path: path.to_owned(),
             password,
-            initialized: Mutex::new(false),
         }))
     }
 
-    pub async fn init_db(&self) -> Result<()> {
-        if !*self.initialized.lock().await {
-            if !self.password.trim().is_empty() {
-                let contents = include_str!("../../sql/schema.sql");
-                let conn = Connection::open(&self.path)?;
-                debug!(target: "WALLETDB", "OPENED CONNECTION AT PATH {:?}", self.path);
-                conn.pragma_update(None, "key", &self.password)?;
-                conn.execute_batch(contents)?;
-                *self.initialized.lock().await = true;
-            } else {
-                debug!(
-                    target: "WALLETDB",
-                    "Password is empty. You must set a password to use the wallet."
-                );
-                return Err(Error::from(ClientFailed::EmptyPassword));
-            }
+    pub fn init_db(&self) -> Result<()> {
+        debug!(target: "WALLETDB", "Initialize...");
+        if !self.password.trim().is_empty() {
+            let contents = include_str!("../../sql/schema.sql");
+            println!("{}", contents);
+            let conn = Connection::open(&self.path)?;
+            debug!(target: "WALLETDB", "OPENED CONNECTION AT PATH {:?}", self.path);
+            conn.pragma_update(None, "key", &self.password)?;
+            conn.execute_batch(contents)?;
         } else {
-            debug!(target: "WALLETDB", "Wallet already initialized.");
-            return Err(Error::from(ClientFailed::WalletInitialized));
+            debug!(
+                target: "WALLETDB",
+                "Password is empty. You must set a password to use the wallet."
+            );
+            return Err(Error::from(ClientFailed::EmptyPassword));
         }
         Ok(())
     }
@@ -240,10 +234,10 @@ impl WalletDb {
 
         conn.execute(
             "INSERT OR REPLACE INTO coins
-            (coin, serial, value, token_id, coin_blind, 
+            (coin, serial, value, token_id, coin_blind,
             valcom_blind, witness, secret, is_spent, nullifier)
             VALUES
-            (:coin, :serial, :value, :token_id, :coin_blind, 
+            (:coin, :serial, :value, :token_id, :coin_blind,
              :valcom_blind, :witness, :secret, :is_spent, :nullifier);",
             named_params! {
                 ":coin": coin,