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

create new error type for btc client

ghassmo 4 лет назад
Родитель
Сommit
1e210f9945
5 измененных файлов с 68 добавлено и 20 удалено
  1. 0 0
      src/bin/solana-poc
  2. 8 14
      src/error.rs
  3. 51 3
      src/service/btc.rs
  4. 8 2
      src/service/cashier.rs
  5. 1 1
      src/service/mod.rs

+ 0 - 0
src/bin/solana-poc.rs → src/bin/solana-poc


+ 8 - 14
src/error.rs

@@ -42,6 +42,7 @@ pub enum Error {
     ZmqError(String),
     ZmqError(String),
     VerifyFailed,
     VerifyFailed,
     ClientFailed(String),
     ClientFailed(String),
+    BtcFailed(String),
     TryIntoError,
     TryIntoError,
     TryFromError,
     TryFromError,
     JsonRpcError(String),
     JsonRpcError(String),
@@ -54,8 +55,6 @@ pub enum Error {
     CashierNoReply,
     CashierNoReply,
     Base58EncodeError(String),
     Base58EncodeError(String),
     Base58DecodeError(String),
     Base58DecodeError(String),
-    BadBTCAddress(String),
-    BtcClientError,
     ConfigNotFound,
     ConfigNotFound,
 }
 }
 
 
@@ -95,6 +94,7 @@ impl fmt::Display for Error {
             Error::ZmqError(ref err) => write!(f, "ZmqError: {}", err),
             Error::ZmqError(ref err) => write!(f, "ZmqError: {}", err),
             Error::VerifyFailed => f.write_str("Verify failed"),
             Error::VerifyFailed => f.write_str("Verify failed"),
             Error::ClientFailed(ref err) => write!(f, "Client failed: {}", err),
             Error::ClientFailed(ref err) => write!(f, "Client failed: {}", err),
+            Error::BtcFailed(ref err) => write!(f, "Btc client failed: {}", err),
             Error::TryIntoError => f.write_str("TryInto error"),
             Error::TryIntoError => f.write_str("TryInto error"),
             Error::TryFromError => f.write_str("TryFrom error"),
             Error::TryFromError => f.write_str("TryFrom error"),
             Error::RocksdbError(ref err) => write!(f, "Rocksdb Error: {}", err),
             Error::RocksdbError(ref err) => write!(f, "Rocksdb Error: {}", err),
@@ -107,8 +107,6 @@ impl fmt::Display for Error {
             Error::Base58EncodeError(ref err) => write!(f, "bs58 encode error: {}", err),
             Error::Base58EncodeError(ref err) => write!(f, "bs58 encode error: {}", err),
             Error::Base58DecodeError(ref err) => write!(f, "bs58 decode error: {}", err),
             Error::Base58DecodeError(ref err) => write!(f, "bs58 decode error: {}", err),
             Error::CashierNoReply => f.write_str("Cashier did not reply with BTC address"),
             Error::CashierNoReply => f.write_str("Cashier did not reply with BTC address"),
-            Error::BadBTCAddress(ref err) => write!(f, "could not parse BTC address: {}", err),
-            Error::BtcClientError => f.write_str("Unable to create Electrum Client"),
             Error::ConfigNotFound => {
             Error::ConfigNotFound => {
                 f.write_str("No config file detected. Please create a config file")
                 f.write_str("No config file detected. Please create a config file")
             }
             }
@@ -219,6 +217,12 @@ impl From<client::ClientFailed> for Error {
     }
     }
 }
 }
 
 
+impl From<crate::service::BtcFailed> for Error {
+    fn from(err: crate::service::BtcFailed) -> Error {
+        Error::BtcFailed(err.to_string())
+    }
+}
+
 impl From<surf::Error> for Error {
 impl From<surf::Error> for Error {
     fn from(err: surf::Error) -> Error {
     fn from(err: surf::Error) -> Error {
         Error::SurfHttpError(err.to_string())
         Error::SurfHttpError(err.to_string())
@@ -249,13 +253,3 @@ impl From<bs58::decode::Error> for Error {
     }
     }
 }
 }
 
 
-impl From<bitcoin::util::address::Error> for Error {
-    fn from(err: bitcoin::util::address::Error) -> Error {
-        Error::BadBTCAddress(err.to_string())
-    }
-}
-impl From<electrum_client::Error> for Error {
-    fn from(_err: electrum_client::Error) -> Error {
-        Error::BtcClientError
-    }
-}

+ 51 - 3
src/service/btc.rs

@@ -1,4 +1,4 @@
-use crate::{Error, Result};
+use crate::Result;
 use rand::distributions::Alphanumeric;
 use rand::distributions::Alphanumeric;
 use rand::{thread_rng, Rng};
 use rand::{thread_rng, Rng};
 
 
@@ -64,7 +64,7 @@ impl BitcoinKeys {
         }))
         }))
     }
     }
 
 
-    pub async fn start_subscribe(self: Arc<Self>) -> Result<Option<GetBalanceRes>> {
+    pub async fn start_subscribe(self: Arc<Self>) -> BtcResult<Option<GetBalanceRes>> {
         debug!(target: "BTC CLIENT", "Subscribe to scriptpubkey");
         debug!(target: "BTC CLIENT", "Subscribe to scriptpubkey");
         let client = &self.btc_client;
         let client = &self.btc_client;
         // Check if script is already subscribed
         // Check if script is already subscribed
@@ -94,7 +94,9 @@ impl BitcoinKeys {
                 };
                 };
             } // Endloop
             } // Endloop
         } else {
         } else {
-            return Err(Error::ServicesError("Did not subscribe to scriptpubkey"));
+            return Err(BtcFailed::ElectrumError(
+                "Did not subscribe to scriptpubkey".to_string(),
+            ));
         }
         }
     }
     }
 
 
@@ -125,3 +127,49 @@ impl BitcoinKeys {
         &self.script
         &self.script
     }
     }
 }
 }
+
+#[derive(Debug)]
+pub enum BtcFailed {
+    NotEnoughValue(u64),
+    BadBTCAddress(String),
+    ElectrumError(String),
+    BtcError(String),
+}
+
+impl std::error::Error for BtcFailed {}
+
+impl std::fmt::Display for BtcFailed {
+    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+        match self {
+            BtcFailed::NotEnoughValue(i) => {
+                write!(f, "There is no enough value {}", i)
+            }
+            BtcFailed::BadBTCAddress(ref err) => {
+                write!(f, "Unable to create Electrum Client: {}", err)
+            }
+            BtcFailed::ElectrumError(ref err) => write!(f, "could not parse BTC address: {}", err),
+            BtcFailed::BtcError(i) => {
+                write!(f, "BtcFailed: {}", i)
+            }
+        }
+    }
+}
+
+impl From<crate::error::Error> for BtcFailed {
+    fn from(err: crate::error::Error) -> BtcFailed {
+        BtcFailed::BtcError(err.to_string())
+    }
+}
+
+impl From<bitcoin::util::address::Error> for BtcFailed {
+    fn from(err: bitcoin::util::address::Error) -> BtcFailed {
+        BtcFailed::BadBTCAddress(err.to_string())
+    }
+}
+impl From<electrum_client::Error> for BtcFailed {
+    fn from(err: electrum_client::Error) -> BtcFailed {
+        BtcFailed::ElectrumError(err.to_string())
+    }
+}
+
+pub type BtcResult<T> = std::result::Result<T, BtcFailed>;

+ 8 - 2
src/service/cashier.rs

@@ -50,7 +50,10 @@ impl CashierService {
         let client_address = btc_endpoint;
         let client_address = btc_endpoint;
 
 
         // create btc client
         // create btc client
-        let btc_client = Arc::new(ElectrumClient::new(&client_address)?);
+        let btc_client = Arc::new(
+            ElectrumClient::new(&client_address)
+                .map_err(|err| crate::Error::from(super::BtcFailed::from(err)))?,
+        );
 
 
         let rocks = Rocks::new(&cashier_database_path)?;
         let rocks = Rocks::new(&cashier_database_path)?;
 
 
@@ -207,7 +210,10 @@ impl CashierService {
                 debug!(target: "CASHIER DAEMON", "Received withdraw request");
                 debug!(target: "CASHIER DAEMON", "Received withdraw request");
                 let btc_address = request.get_payload();
                 let btc_address = request.get_payload();
                 let btc_address: String = deserialize(&btc_address)?;
                 let btc_address: String = deserialize(&btc_address)?;
-                let btc_address = bitcoin::util::address::Address::from_str(&btc_address)?;
+                let btc_address =
+                    bitcoin::util::address::Address::from_str(&btc_address).map_err(|err| {
+                        crate::Error::from(super::BtcFailed::from(err))
+                    })?;
 
 
                 let cashier_public: jubjub::SubgroupPoint;
                 let cashier_public: jubjub::SubgroupPoint;
 
 

+ 1 - 1
src/service/mod.rs

@@ -8,4 +8,4 @@ pub use gateway::{GatewayClient, GatewayService, GatewaySlabsSubscriber};
 
 
 pub use cashier::{CashierClient, CashierService};
 pub use cashier::{CashierClient, CashierService};
 
 
-pub use btc::{BitcoinKeys, PubAddress};
+pub use btc::{BitcoinKeys, PubAddress, BtcFailed, BtcResult};