Explorar o código

Add scheduler and requests for watching btc address

Janus %!s(int64=5) %!d(string=hai) anos
pai
achega
f2b68bfaac
Modificáronse 4 ficheiros con 265 adicións e 8 borrados
  1. 227 0
      Cargo.lock
  2. 2 0
      Cargo.toml
  3. 21 0
      src/service/btc.rs
  4. 15 8
      src/service/cashier.rs

+ 227 - 0
Cargo.lock

@@ -704,6 +704,15 @@ dependencies = [
  "syn",
 ]
 
+[[package]]
+name = "clokwerk"
+version = "0.4.0-rc1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8cca22ab3f48e39a5e8432ea296da1063b3d000c3d3c37da9fa586961e21bf73"
+dependencies = [
+ "chrono",
+]
+
 [[package]]
 name = "concurrent-queue"
 version = "1.2.2"
@@ -1000,6 +1009,7 @@ dependencies = [
  "bs58",
  "bytes 1.0.1",
  "clap",
+ "clokwerk",
  "crypto_api_chachapoly",
  "ctrlc",
  "dirs 3.0.2",
@@ -1023,6 +1033,7 @@ dependencies = [
  "rand_core 0.5.1",
  "rand_xorshift",
  "regex",
+ "reqwest",
  "rocksdb",
  "rusqlite",
  "secp256k1",
@@ -1498,6 +1509,25 @@ dependencies = [
  "subtle",
 ]
 
+[[package]]
+name = "h2"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "825343c4eef0b63f541f8903f395dc5beb362a979b5799a84062527ef1e37726"
+dependencies = [
+ "bytes 1.0.1",
+ "fnv",
+ "futures-core",
+ "futures-sink",
+ "futures-util",
+ "http",
+ "indexmap",
+ "slab",
+ "tokio",
+ "tokio-util",
+ "tracing",
+]
+
 [[package]]
 name = "hashbrown"
 version = "0.11.2"
@@ -1571,6 +1601,17 @@ dependencies = [
  "itoa",
 ]
 
+[[package]]
+name = "http-body"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "399c583b2979440c60be0821a6199eca73bc3c8dcd9d070d75ac726e2c6186e5"
+dependencies = [
+ "bytes 1.0.1",
+ "http",
+ "pin-project-lite",
+]
+
 [[package]]
 name = "http-client"
 version = "6.4.1"
@@ -1614,6 +1655,49 @@ version = "1.4.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f3a87b616e37e93c22fb19bcd386f02f3af5ea98a25670ad0fce773de23c5e68"
 
+[[package]]
+name = "httpdate"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6456b8a6c8f33fee7d958fcd1b60d55b11940a79e63ae87013e6d22e26034440"
+
+[[package]]
+name = "hyper"
+version = "0.14.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0b61cf2d1aebcf6e6352c97b81dc2244ca29194be1b276f5d8ad5c6330fffb11"
+dependencies = [
+ "bytes 1.0.1",
+ "futures-channel",
+ "futures-core",
+ "futures-util",
+ "h2",
+ "http",
+ "http-body",
+ "httparse",
+ "httpdate",
+ "itoa",
+ "pin-project-lite",
+ "socket2",
+ "tokio",
+ "tower-service",
+ "tracing",
+ "want",
+]
+
+[[package]]
+name = "hyper-tls"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
+dependencies = [
+ "bytes 1.0.1",
+ "hyper",
+ "native-tls",
+ "tokio",
+ "tokio-native-tls",
+]
+
 [[package]]
 name = "idna"
 version = "0.2.3"
@@ -1650,6 +1734,12 @@ dependencies = [
  "cfg-if 1.0.0",
 ]
 
+[[package]]
+name = "ipnet"
+version = "2.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "68f2d64f2edebec4ce84ad108148e67e1064789bee435edc5b60ad398714a3a9"
+
 [[package]]
 name = "isahc"
 version = "0.9.14"
@@ -1887,6 +1977,28 @@ dependencies = [
  "autocfg",
 ]
 
+[[package]]
+name = "mio"
+version = "0.7.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8c2bdb6314ec10835cd3293dd268473a835c02b7b352e788be788b3c6ca6bb16"
+dependencies = [
+ "libc",
+ "log",
+ "miow",
+ "ntapi",
+ "winapi",
+]
+
+[[package]]
+name = "miow"
+version = "0.3.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21"
+dependencies = [
+ "winapi",
+]
+
 [[package]]
 name = "multimap"
 version = "0.8.3"
@@ -1936,6 +2048,15 @@ dependencies = [
  "version_check",
 ]
 
+[[package]]
+name = "ntapi"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44"
+dependencies = [
+ "winapi",
+]
+
 [[package]]
 name = "num-bigint"
 version = "0.3.2"
@@ -2392,6 +2513,40 @@ dependencies = [
  "winapi",
 ]
 
+[[package]]
+name = "reqwest"
+version = "0.11.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "246e9f61b9bb77df069a947682be06e31ac43ea37862e244a69f177694ea6d22"
+dependencies = [
+ "base64",
+ "bytes 1.0.1",
+ "encoding_rs",
+ "futures-core",
+ "futures-util",
+ "http",
+ "http-body",
+ "hyper",
+ "hyper-tls",
+ "ipnet",
+ "js-sys",
+ "lazy_static",
+ "log",
+ "mime",
+ "native-tls",
+ "percent-encoding",
+ "pin-project-lite",
+ "serde",
+ "serde_urlencoded",
+ "tokio",
+ "tokio-native-tls",
+ "url",
+ "wasm-bindgen",
+ "wasm-bindgen-futures",
+ "web-sys",
+ "winreg",
+]
+
 [[package]]
 name = "rocksdb"
 version = "0.16.0"
@@ -2950,6 +3105,45 @@ version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
 
+[[package]]
+name = "tokio"
+version = "1.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4b7b349f11a7047e6d1276853e612d152f5e8a352c61917887cc2169e2366b4c"
+dependencies = [
+ "autocfg",
+ "bytes 1.0.1",
+ "libc",
+ "memchr",
+ "mio",
+ "pin-project-lite",
+ "winapi",
+]
+
+[[package]]
+name = "tokio-native-tls"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b"
+dependencies = [
+ "native-tls",
+ "tokio",
+]
+
+[[package]]
+name = "tokio-util"
+version = "0.6.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1caa0b0c8d94a049db56b5acf8cba99dc0623aab1b26d5b5f5e2d945846b3592"
+dependencies = [
+ "bytes 1.0.1",
+ "futures-core",
+ "futures-sink",
+ "log",
+ "pin-project-lite",
+ "tokio",
+]
+
 [[package]]
 name = "toml"
 version = "0.5.8"
@@ -2959,6 +3153,12 @@ dependencies = [
  "serde",
 ]
 
+[[package]]
+name = "tower-service"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6"
+
 [[package]]
 name = "tracing"
 version = "0.1.26"
@@ -3002,6 +3202,12 @@ dependencies = [
  "tracing",
 ]
 
+[[package]]
+name = "try-lock"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
+
 [[package]]
 name = "typenum"
 version = "1.13.0"
@@ -3119,6 +3325,16 @@ version = "1.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca"
 
+[[package]]
+name = "want"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0"
+dependencies = [
+ "log",
+ "try-lock",
+]
+
 [[package]]
 name = "wasi"
 version = "0.9.0+wasi-snapshot-preview1"
@@ -3138,6 +3354,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd"
 dependencies = [
  "cfg-if 1.0.0",
+ "serde",
+ "serde_json",
  "wasm-bindgen-macro",
 ]
 
@@ -3247,6 +3465,15 @@ version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
 
+[[package]]
+name = "winreg"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0120db82e8a1e0b9fb3345a539c478767c0048d842860994d96113d5b667bd69"
+dependencies = [
+ "winapi",
+]
+
 [[package]]
 name = "wyz"
 version = "0.2.0"

+ 2 - 0
Cargo.toml

@@ -80,6 +80,8 @@ dirs = "3.0.2"
 
 bitcoin = "0.26.2"
 secp256k1 = "0.20.3"
+clokwerk = "0.4.0-rc1"
+reqwest = "0.11.4"
 
 [dependencies.rusqlite]
 version = "0.25.1"

+ 21 - 0
src/service/btc.rs

@@ -9,12 +9,18 @@ use secp256k1::key::SecretKey;
 
 use bitcoin::network::constants::Network;
 
+use async_executor::Executor;
+use async_std::sync::Arc;
+
+use clokwerk::AsyncScheduler;
+
 // Swap out these types for any future non bitcoin-rs types
 pub type PubAddress = Address;
 pub type PubKey = PublicKey;
 pub type PrivKey = PrivateKey;
 
 pub struct BitcoinKeys {
+    scheduler: AsyncScheduler,
     secret_key: SecretKey,
     bitcoin_private_key: PrivateKey,
     pub bitcoin_public_key: PublicKey,
@@ -47,13 +53,28 @@ impl BitcoinKeys {
 
         let pub_address = Address::p2pkh(&bitcoin_public_key, Network::Testnet);
 
+        // Create a scheduler for checking the address balance
+        let scheduler = AsyncScheduler::new();
+
         Ok(Self {
+            scheduler,
             secret_key,
             bitcoin_private_key,
             bitcoin_public_key,
             pub_address,
         })
     }
+    pub fn start_scheduler(&self, executor: Arc<Executor<'_>>) -> Result<()> {
+        //&self.scheduler.every(10.minutes()).run();
+
+        Ok(())
+    }
+
+    async fn _watch_address(&self) -> Result<()> {
+
+        Ok(())
+    }
+
     // This should do a db lookup to return the same obj
     pub fn address_from_slice(key: &[u8]) -> Result<Address> {
         let pub_key = PublicKey::from_slice(key).unwrap();

+ 15 - 8
src/service/cashier.rs

@@ -2,11 +2,11 @@ use super::reqrep::{PeerId, RepProtocol, Reply, ReqProtocol, Request};
 
 use super::btc::{BitcoinKeys, PubAddress};
 
-use crate::crypto::load_params;
-use crate::serial::{deserialize, serialize, Decodable, Encodable};
-use crate::tx;
-use crate::wallet::CashierDbPtr;
 use crate::{Error, Result};
+use crate::serial::{Decodable, Encodable, deserialize, serialize};
+use crate::wallet::CashierDbPtr;
+use crate::tx;
+use crate::crypto::load_params;
 
 use bellman::groth16;
 use bls12_381::Bls12;
@@ -70,6 +70,7 @@ impl CashierService {
     }
 
     fn mint_dbtc(&self, dkey_pub: jubjub::SubgroupPoint, value: u64) -> Result<Vec<u8>> {
+        // Change to adapter
         let cashier_secret = self.wallet.get_cashier_private().unwrap();
 
         let builder = tx::TransactionBuilder {
@@ -93,6 +94,10 @@ impl CashierService {
             // Now serialize it
             tx.encode(&mut tx_data).expect("encode tx");
         }
+        //Add to blockchain
+        // let slab = Slab::new(tx_data);
+
+        // client.put_slab(slab).await.expect("put slab");
 
         Ok(tx_data)
     }
@@ -112,6 +117,7 @@ impl CashierService {
                             msg,
                             cashier_wallet,
                             send_queue.clone(),
+                            executor.clone()
                         ))
                         .detach();
                 }
@@ -126,6 +132,7 @@ impl CashierService {
         msg: (PeerId, Request),
         cashier_wallet: CashierDbPtr,
         send_queue: async_channel::Sender<(PeerId, Reply)>,
+        executor: Arc<Executor<'_>>,
     ) -> Result<()> {
         let request = msg.1;
         let peer = msg.0;
@@ -153,10 +160,12 @@ impl CashierService {
 
                 // send reply
                 send_queue.send((peer, reply)).await?;
+                info!("Received dkey->btc msg");
 
-                // add to watchlist
+                // start scheduler for checking balance
+                let _result = btc_keys.start_scheduler(executor.clone());
+                info!("Waiting for address balance");
 
-                info!("Received dkey->btc msg");
             }
             1 => {
                 // Withdraw
@@ -203,8 +212,6 @@ impl CashierClient {
             .await?;
 
         if let Some(key) = rep {
-            //let pubkey = BitcoinPubKey::from_slice(&key).unwrap();
-            //let address: Address = Address::p2pkh(&pubkey, Network::Testnet);
             let address = BitcoinKeys::address_from_slice(&key).unwrap();
             return Ok(Some(address));
         }