Sfoglia il codice sorgente

btc: undo unecessary check for spent address

Janus 4 anni fa
parent
commit
51ea7cc904
3 ha cambiato i file con 12 aggiunte e 41 eliminazioni
  1. 2 6
      src/bin/cashierd.rs
  2. 9 34
      src/service/btc.rs
  3. 1 1
      src/service/mod.rs

+ 2 - 6
src/bin/cashierd.rs

@@ -510,7 +510,7 @@ impl Cashierd {
                 #[cfg(feature = "btc")]
                 NetworkName::Bitcoin => {
                     debug!(target: "CASHIER DAEMON", "Add btc network");
-                    use drk::service::btc::{used_key, BtcClient, BtcFailed, Keypair};
+                    use drk::service::btc::{BtcClient, BtcFailed, Keypair};
 
                     let bridge2 = self.bridge.clone();
 
@@ -519,12 +519,8 @@ impl Cashierd {
                     let main_keypairs = self.cashier_wallet.get_main_keys(&NetworkName::Bitcoin)?;
 
                     if network.keypair.is_empty() {
-                        //TODO: There needs to be a better way to flag completed txs
+
                         if main_keypairs.is_empty()
-                            || used_key(
-                                &main_keypairs[main_keypairs.len() - 1].private_key,
-                                &network.blockchain,
-                            )?
                         {
                             main_keypair = Keypair::new();
                             self.cashier_wallet.put_main_keys(

+ 9 - 34
src/service/btc.rs

@@ -213,34 +213,6 @@ fn print_status_change(
 
     new
 }
-pub fn used_key(keys: &Vec<u8>, network: &str) -> Result<bool> {
-    let keypair: Keypair = deserialize(keys)?;
-
-    //TODO: Don't create an electrum client just to check address status
-    let (network, url) = match network {
-        "mainnet" => (Network::Bitcoin, "ssl://electrum.blockstream.info:50002"),
-        "testnet" => (Network::Testnet, "ssl://electrum.blockstream.info:60002"),
-        _ => return Err(Error::NotSupportedNetwork),
-    };
-    let btc_keys = Account::new(&keypair, network);
-    let script = btc_keys.script_pubkey;
-
-    let electrum =
-        ElectrumClient::new(url).map_err(|err| crate::Error::from(super::BtcFailed::from(err)))?;
-
-    let history = electrum
-        .script_get_history(&script)
-        .map_err(|err| crate::Error::from(super::BtcFailed::from(err)))?;
-    let balance = electrum
-        .script_get_balance(&script)
-        .map_err(|err| crate::Error::from(super::BtcFailed::from(err)))?;
-
-    if !history.is_empty() && balance.confirmed == 0 {
-        Ok(true)
-    } else {
-        Ok(false)
-    }
-}
 fn sync_interval(avg_block_time: Duration) -> Duration {
     max(avg_block_time / 10, Duration::from_secs(1))
 }
@@ -420,13 +392,15 @@ impl BtcClient {
             match new_status {
                 ScriptStatus::Unseen => continue,
                 ScriptStatus::InMempool => {
+
                     break;
                 }
                 ScriptStatus::Confirmed(inner) => {
+                    //Only break when confirmations happen
                     let confirmations = inner.confirmations();
-                    //if confirmations < 1 {
-                    break;
-                    //}
+                    if confirmations > 1 {
+                        break;
+                    }
                 }
             }
         }
@@ -446,12 +420,13 @@ impl BtcClient {
         cur_balance = client.lock().await.electrum.script_get_balance(&script)?;
 
         let send_notification = self.notify_channel.0.clone();
-        if cur_balance.confirmed < prev_balance.confirmed {
+        //FIXME: dev
+        if cur_balance.unconfirmed < prev_balance.unconfirmed {
             return Err(BtcFailed::Notification(
                 "New balance is less than previous balance".into(),
             ));
         }
-
+        //Just check unconfirmed for now
         let amnt = cur_balance.confirmed - prev_balance.confirmed;
         let ui_amnt = amnt;
         send_notification
@@ -514,7 +489,7 @@ impl BtcClient {
 
         let tx_size = transaction.get_size();
 
-        let fee_per_kb = client.estimate_fee(1)?;
+        let fee_per_kb = electrum.estimate_fee(1)?;
         let _fee = tx_size as f64 * fee_per_kb * 100000_f64;
 
         let transaction = Transaction {

+ 1 - 1
src/service/mod.rs

@@ -6,7 +6,7 @@ pub mod reqrep;
 #[cfg(feature = "btc")]
 pub mod btc;
 #[cfg(feature = "btc")]
-pub use btc::{Account, BtcFailed, BtcResult, Keypair, PubAddress, used_key};
+pub use btc::{Account, BtcFailed, BtcResult, Keypair, PubAddress};
 
 #[cfg(feature = "sol")]
 pub mod sol;