Просмотр исходного кода

remove async functions from walletdb.rs and rpc/adapter.rs

ghassmo 5 лет назад
Родитель
Сommit
b8890995b2
4 измененных файлов с 51 добавлено и 54 удалено
  1. 5 8
      src/bin/darkfid.rs
  2. 18 18
      src/rpc/adapter.rs
  3. 9 9
      src/rpc/jsonserver.rs
  4. 19 19
      src/wallet/walletdb.rs

+ 5 - 8
src/bin/darkfid.rs

@@ -117,9 +117,7 @@ impl State {
                 // Make a new witness for this coin
                 let witness = IncrementalWitness::from_tree(&self.tree);
 
-                self.wallet
-                    .put_own_coins(coin, note, witness, secret)
-                    .await?;
+                self.wallet.put_own_coins(coin, note, witness, secret)?;
             }
         }
         Ok(())
@@ -130,7 +128,6 @@ impl State {
         let secret = self
             .wallet
             .get_value_deserialized::<jubjub::Fr>(vec)
-            .await
             .expect("Deserialize failed");
         match ciphertext.decrypt(&secret) {
             Ok(note) => {
@@ -197,10 +194,6 @@ async fn start(
 
     let ex = executor.clone();
 
-    let adapter = RpcAdapter::new(wallet.clone())?;
-    // start the rpc server
-    jsonserver::start(ex.clone(), config.clone(), adapter).await?;
-
     let state = State {
         tree: CommitmentTree::empty(),
         merkle_roots,
@@ -224,6 +217,10 @@ async fn start(
     debug!(target: "fn::start client", "start() Client started");
     client.start().await?;
 
+    let adapter = RpcAdapter::new(wallet.clone())?;
+    // start the rpc server
+    jsonserver::start(ex.clone(), config.clone(), adapter).await?;
+
     subscribe_task.cancel().await;
     Ok(())
 }

+ 18 - 18
src/rpc/adapter.rs

@@ -16,49 +16,49 @@ impl RpcAdapter {
         Ok(Self { wallet })
     }
 
-    pub async fn init_db(&self) -> Result<()> {
+    pub fn init_db(&self) -> Result<()> {
         debug!(target: "adapter", "init_db() [START]");
-        self.wallet.init_db().await?;
+        self.wallet.init_db()?;
         Ok(())
     }
 
-    pub async fn init_cashier_db(&self) -> Result<()> {
+    pub fn init_cashier_db(&self) -> Result<()> {
         debug!(target: "adapter", "init_cashier_db() [START]");
-        self.wallet.init_cashier_db().await?;
+        self.wallet.init_cashier_db()?;
         Ok(())
     }
 
-    pub async fn key_gen(&self) -> Result<()> {
+    pub fn key_gen(&self) -> Result<()> {
         debug!(target: "adapter", "key_gen() [START]");
-        let (public, private) = self.wallet.key_gen().await;
+        let (public, private) = self.wallet.key_gen();
         debug!(target: "adapter", "Created keypair...");
         debug!(target: "adapter", "Attempting to write to database...");
-        self.wallet.put_keypair(public, private).await?;
+        self.wallet.put_keypair(public, private)?;
         Ok(())
     }
 
-    pub async fn cash_key_gen(&self) -> Result<()> {
+    pub fn cash_key_gen(&self) -> Result<()> {
         debug!(target: "adapter", "key_gen() [START]");
-        let (public, private) = self.wallet.key_gen().await;
-        self.wallet.put_keypair(public, private).await?;
+        let (public, private) = self.wallet.key_gen();
+        self.wallet.put_keypair(public, private)?;
         Ok(())
     }
 
-    pub async fn get_key(&self) -> Result<()> {
+    pub fn get_key(&self) -> Result<()> {
         debug!(target: "adapter", "get_key() [START]");
-        let key_public = self.wallet.get_public().await?;
+        let key_public = self.wallet.get_public()?;
         println!("{:?}", key_public);
         Ok(())
     }
 
-    pub async fn get_cash_key(&self) -> Result<()> {
+    pub fn get_cash_key(&self) -> Result<()> {
         debug!(target: "adapter", "get_cash_key() [START]");
-        let cashier_public = self.wallet.get_cashier_public().await?;
+        let cashier_public = self.wallet.get_cashier_public()?;
         println!("{:?}", cashier_public);
         Ok(())
     }
 
-    pub async fn test_wallet(&self) -> Result<()> {
+    pub fn test_wallet(&self) -> Result<()> {
         self.wallet.test_wallet()?;
         debug!(target: "adapter", "test wallet: START");
         Ok(())
@@ -83,9 +83,9 @@ impl RpcAdapter {
     //    Ok(())
     //}
 
-    pub async fn get_info(&self) {}
+    pub fn get_info(&self) {}
 
-    pub async fn say_hello(&self) {}
+    pub fn say_hello(&self) {}
 
-    pub async fn stop(&self) {}
+    pub fn stop(&self) {}
 }

+ 9 - 9
src/rpc/jsonserver.rs

@@ -145,7 +145,7 @@ impl RpcInterface {
         io.add_method("get_key", move |_| {
             let self2 = self1.clone();
             async move {
-                self2.adapter.get_key().await?;
+                self2.adapter.get_key()?;
                 Ok(jsonrpc_core::Value::String("Getting cashier key...".into()))
             }
         });
@@ -153,7 +153,7 @@ impl RpcInterface {
         io.add_method("get_cash_key", move |_| {
             let self2 = self1.clone();
             async move {
-                self2.adapter.get_cash_key().await?;
+                self2.adapter.get_cash_key()?;
                 Ok(jsonrpc_core::Value::String("Getting cashier key...".into()))
             }
         });
@@ -162,7 +162,7 @@ impl RpcInterface {
         io.add_method("get_info", move |_| {
             let self2 = self1.clone();
             async move {
-                self2.adapter.get_info().await;
+                self2.adapter.get_info();
                 Ok(jsonrpc_core::Value::Null)
             }
         });
@@ -171,7 +171,7 @@ impl RpcInterface {
         io.add_method("stop", move |_| {
             let self2 = self1.clone();
             async move {
-                self2.adapter.stop().await;
+                self2.adapter.stop();
                 Ok(jsonrpc_core::Value::Null)
             }
         });
@@ -183,7 +183,7 @@ impl RpcInterface {
                     "Attempting wallet generation at path {:?}",
                     self2.adapter.wallet.path
                 );
-                self2.adapter.init_db().await?;
+                self2.adapter.init_db()?;
                 Ok(jsonrpc_core::Value::String("Created wallet".into()))
             }
         });
@@ -192,7 +192,7 @@ impl RpcInterface {
             let self2 = self1.clone();
             async move {
                 println!("Key generation method called...");
-                self2.adapter.key_gen().await?;
+                self2.adapter.key_gen()?;
                 Ok(jsonrpc_core::Value::String(
                     "Key generation successful".into(),
                 ))
@@ -203,7 +203,7 @@ impl RpcInterface {
             let self2 = self1.clone();
             async move {
                 println!("Key generation method called...");
-                self2.adapter.cash_key_gen().await?;
+                self2.adapter.cash_key_gen()?;
                 Ok(jsonrpc_core::Value::String(
                     "Attempted key generation".into(),
                 ))
@@ -214,7 +214,7 @@ impl RpcInterface {
             let self2 = self1.clone();
             async move {
                 println!("Test wallet method called...");
-                self2.adapter.test_wallet().await?;
+                self2.adapter.test_wallet()?;
                 Ok(jsonrpc_core::Value::String("Test wallet".into()))
             }
         });
@@ -223,7 +223,7 @@ impl RpcInterface {
             let self2 = self1.clone();
             async move {
                 println!("New wallet method called...");
-                self2.adapter.init_cashier_db().await?;
+                self2.adapter.init_cashier_db()?;
                 println!("Wallet created at path {:?}", self2.adapter.wallet.path);
                 Ok(jsonrpc_core::Value::String("Created cashier wallet".into()))
             }

+ 19 - 19
src/wallet/walletdb.rs

@@ -49,7 +49,7 @@ impl WalletDB {
         })
     }
 
-    pub async fn init_db(&self) -> Result<()> {
+    pub fn init_db(&self) -> Result<()> {
         let conn = Connection::open(&self.path)?;
         debug!(target: "walletdb", "OPENED CONNECTION AT PATH {:?}", self.path);
         let contents = include_str!("../../res/schema.sql");
@@ -60,7 +60,7 @@ impl WalletDB {
         Ok(())
     }
 
-    pub async fn init_cashier_db(&self) -> Result<()> {
+    pub fn init_cashier_db(&self) -> Result<()> {
         let conn = Connection::open(&self.path)?;
         debug!(target: "walletdb", "OPENED CONNECTION AT PATH {:?}", self.path);
         let contents = include_str!("../../res/schema.sql");
@@ -71,7 +71,7 @@ impl WalletDB {
         Ok(())
     }
 
-    pub async fn put_own_coins(
+    pub fn put_own_coins(
         &self,
         coin: Coin,
         note: Note,
@@ -79,14 +79,14 @@ impl WalletDB {
         secret: jubjub::Fr,
     ) -> Result<()> {
         // prepare the values
-        let coin = self.get_value_serialized(&coin.repr).await?;
-        let serial = self.get_value_serialized(&note.serial).await?;
-        let coin_blind = self.get_value_serialized(&note.coin_blind).await?;
-        let valcom_blind = self.get_value_serialized(&note.valcom_blind).await?;
-        let value = self.get_value_serialized(&note.value).await?;
-        let asset_id = self.get_value_serialized(&note.asset_id).await?;
-        let witness = self.get_value_serialized(&witness).await?;
-        let secret = self.get_value_serialized(&secret).await?;
+        let coin = self.get_value_serialized(&coin.repr)?;
+        let serial = self.get_value_serialized(&note.serial)?;
+        let coin_blind = self.get_value_serialized(&note.coin_blind)?;
+        let valcom_blind = self.get_value_serialized(&note.valcom_blind)?;
+        let value = self.get_value_serialized(&note.value)?;
+        let asset_id = self.get_value_serialized(&note.asset_id)?;
+        let witness = self.get_value_serialized(&witness)?;
+        let secret = self.get_value_serialized(&secret)?;
         // open connection
         let conn = Connection::open(&self.path)?;
         // unlock database
@@ -117,7 +117,7 @@ impl WalletDB {
         Ok(())
     }
 
-    pub async fn key_gen(&self) -> (Vec<u8>, Vec<u8>) {
+    pub fn key_gen(&self) -> (Vec<u8>, Vec<u8>) {
         debug!(target: "key_gen", "Attempting to generate keys...");
         let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
         let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
@@ -126,7 +126,7 @@ impl WalletDB {
         (pubkey, privkey)
     }
 
-    pub async fn cash_key_gen(&self) -> (Vec<u8>, Vec<u8>) {
+    pub fn cash_key_gen(&self) -> (Vec<u8>, Vec<u8>) {
         debug!(target: "cash key_gen", "Generating cashier keys...");
         let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
         let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
@@ -135,7 +135,7 @@ impl WalletDB {
         (pubkey, privkey)
     }
 
-    pub async fn put_keypair(&self, key_public: Vec<u8>, key_private: Vec<u8>) -> Result<()> {
+    pub fn put_keypair(&self, key_public: Vec<u8>, key_private: Vec<u8>) -> Result<()> {
         let conn = Connection::open(&self.path)?;
         let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
         let _rows = stmt.query([])?;
@@ -146,7 +146,7 @@ impl WalletDB {
         Ok(())
     }
 
-    pub async fn put_cashier_pub(&self, key_public: Vec<u8>) -> Result<()> {
+    pub fn put_cashier_pub(&self, key_public: Vec<u8>) -> Result<()> {
         debug!(target: "save_cash_key", "Save cashier keys...");
         let conn = Connection::open(&self.path)?;
         let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
@@ -158,7 +158,7 @@ impl WalletDB {
         Ok(())
     }
 
-    pub async fn get_public(&self) -> Result<Vec<u8>> {
+    pub fn get_public(&self) -> Result<Vec<u8>> {
         debug!(target: "get", "Returning keys...");
         let conn = Connection::open(&self.path)?;
         let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
@@ -172,7 +172,7 @@ impl WalletDB {
         Ok(pub_keys)
     }
 
-    pub async fn get_cashier_public(&self) -> Result<Vec<u8>> {
+    pub fn get_cashier_public(&self) -> Result<Vec<u8>> {
         debug!(target: "get_cashier_public", "Returning keys...");
         let conn = Connection::open(&self.path)?;
         let mut stmt = conn.prepare("PRAGMA key = 'testkey'")?;
@@ -209,11 +209,11 @@ impl WalletDB {
         Ok(())
     }
 
-    pub async fn get_value_serialized<T: Encodable>(&self, data: &T) -> Result<Vec<u8>> {
+    pub fn get_value_serialized<T: Encodable>(&self, data: &T) -> Result<Vec<u8>> {
         let v = serialize(data);
         Ok(v)
     }
-    pub async fn get_value_deserialized<D: Decodable>(&self, key: Vec<u8>) -> Result<D> {
+    pub fn get_value_deserialized<D: Decodable>(&self, key: Vec<u8>) -> Result<D> {
         let v: D = deserialize(&key)?;
         Ok(v)
     }