Parcourir la source

create Client object for CashierService

ghassmo il y a 5 ans
Parent
commit
b6a27a60c5
2 fichiers modifiés avec 78 ajouts et 72 suppressions
  1. 19 16
      src/bin/cashierd.rs
  2. 59 56
      src/service/cashier.rs

+ 19 - 16
src/bin/cashierd.rs

@@ -2,14 +2,9 @@ use async_std::sync::Arc;
 use std::net::SocketAddr;
 use std::net::SocketAddr;
 
 
 use std::{path::Path, path::PathBuf};
 use std::{path::Path, path::PathBuf};
-//use toml;
 
 
-use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
 use drk::cli::{CashierdCli, CashierdConfig, Config};
 use drk::cli::{CashierdCli, CashierdConfig, Config};
-use drk::rpc::adapters::cashier_adapter::CashierAdapter;
-use drk::rpc::jsonserver;
 use drk::service::CashierService;
 use drk::service::CashierService;
-use drk::service::GatewayClient;
 use drk::util::join_config_path;
 use drk::util::join_config_path;
 use drk::wallet::CashierDb;
 use drk::wallet::CashierDb;
 use drk::{Error, Result};
 use drk::{Error, Result};
@@ -28,24 +23,32 @@ async fn start(executor: Arc<Executor<'_>>, config: Arc<CashierdConfig>) -> Resu
 
 
     let database_path = config.database_path.clone();
     let database_path = config.database_path.clone();
     let database_path = join_config_path(&PathBuf::from(database_path))?;
     let database_path = join_config_path(&PathBuf::from(database_path))?;
-    let rocks = Rocks::new(&database_path)?;
-    let slabstore = RocksColumn::<columns::Slabs>::new(rocks.clone());
 
 
     let wallet = Arc::new(CashierDb::new("cashier.db", config.password.clone())?);
     let wallet = Arc::new(CashierDb::new("cashier.db", config.password.clone())?);
 
 
-    debug!(target: "Client", "Creating gateway client");
-    let mut gateway = GatewayClient::new(gateway_addr, "127.0.0.1:4444".parse()?, slabstore)?;
-
-    gateway.start().await?;
+    // TODO add to config 
+    let client_wallet_path = "cashier_client_wallet.db";
 
 
     debug!(target: "cashierd", "starting cashier service");
     debug!(target: "cashierd", "starting cashier service");
-    let cashier = CashierService::new(accept_addr, btc_endpoint, wallet.clone(), gateway)?;
+    let cashier = CashierService::new(
+        accept_addr,
+        btc_endpoint,
+        wallet.clone(),
+        database_path,
+        (gateway_addr, "127.0.0.1:4444".parse()?),
+        (
+            PathBuf::from("cashier_mint.params"),
+            PathBuf::from("cashier_spend.params"),
+        ),
+        PathBuf::from(client_wallet_path),
+    ).await?;
+
     cashier.start(ex.clone()).await?;
     cashier.start(ex.clone()).await?;
 
 
-    let rpc_url: std::net::SocketAddr = config.rpc_url.parse()?;
-    let adapter = Arc::new(CashierAdapter::new(wallet.clone())?);
-    let io = Arc::new(adapter.handle_input()?);
-    jsonserver::start(ex, rpc_url, io).await?;
+    //let rpc_url: std::net::SocketAddr = config.rpc_url.parse()?;
+    //let adapter = Arc::new(CashierAdapter::new(wallet.clone())?);
+    //let io = Arc::new(adapter.handle_input()?);
+    //jsonserver::start(ex, rpc_url, io).await?;
 
 
     Ok(())
     Ok(())
 }
 }

+ 59 - 56
src/service/cashier.rs

@@ -1,15 +1,12 @@
 use super::btc::{BitcoinKeys, PubAddress};
 use super::btc::{BitcoinKeys, PubAddress};
 use super::reqrep::{PeerId, RepProtocol, Reply, ReqProtocol, Request};
 use super::reqrep::{PeerId, RepProtocol, Reply, ReqProtocol, Request};
-use super::GatewayClient;
-use crate::blockchain::Slab;
-use crate::crypto::load_params;
-use crate::serial::{deserialize, serialize, Encodable};
-use crate::tx;
+use crate::blockchain::Rocks;
+use crate::client::Client;
+use crate::serial::{deserialize, serialize};
 use crate::wallet::CashierDbPtr;
 use crate::wallet::CashierDbPtr;
+use crate::wallet::WalletDb;
 use crate::{Error, Result};
 use crate::{Error, Result};
 
 
-use bellman::groth16;
-use bls12_381::Bls12;
 use ff::Field;
 use ff::Field;
 use rand::rngs::OsRng;
 use rand::rngs::OsRng;
 
 
@@ -19,8 +16,8 @@ use electrum_client::Client as ElectrumClient;
 use log::*;
 use log::*;
 
 
 use async_std::sync::Arc;
 use async_std::sync::Arc;
-
 use std::net::SocketAddr;
 use std::net::SocketAddr;
+use std::path::PathBuf;
 
 
 #[repr(u8)]
 #[repr(u8)]
 enum CashierError {
 enum CashierError {
@@ -36,41 +33,48 @@ enum CashierCommand {
 pub struct CashierService {
 pub struct CashierService {
     addr: SocketAddr,
     addr: SocketAddr,
     wallet: CashierDbPtr,
     wallet: CashierDbPtr,
-    gateway: GatewayClient,
     btc_client: Arc<ElectrumClient>,
     btc_client: Arc<ElectrumClient>,
-    mint_params: groth16::Parameters<Bls12>,
-    mint_pvk: groth16::PreparedVerifyingKey<Bls12>,
-    spend_params: groth16::Parameters<Bls12>,
-    spend_pvk: groth16::PreparedVerifyingKey<Bls12>,
+    client: Client,
 }
 }
 
 
 impl CashierService {
 impl CashierService {
-    pub fn new(
+    pub async fn new(
         addr: SocketAddr,
         addr: SocketAddr,
         btc_endpoint: String,
         btc_endpoint: String,
         wallet: CashierDbPtr,
         wallet: CashierDbPtr,
-        gateway: GatewayClient,
+        cashier_database_path: PathBuf,
+        gateway_addrs: (SocketAddr, SocketAddr),
+        params_paths: (PathBuf, PathBuf),
+        client_wallet_path: PathBuf,
     ) -> Result<Arc<CashierService>> {
     ) -> Result<Arc<CashierService>> {
         // Load trusted setup parameters
         // Load trusted setup parameters
 
 
         // Pull address from config later
         // Pull address from config later
         let client_address = btc_endpoint;
         let client_address = btc_endpoint;
 
 
-        // create client
+        // create btc client
         let btc_client = Arc::new(ElectrumClient::new(&client_address)?);
         let btc_client = Arc::new(ElectrumClient::new(&client_address)?);
 
 
-        let (mint_params, mint_pvk) = load_params("mint.params")?;
-        let (spend_params, spend_pvk) = load_params("spend.params")?;
+        wallet.init_db()?;
+        let keys = wallet.cash_key_gen();
+        wallet.put_keypair(keys.0, keys.1)?;
+        let cashier_secret = wallet.get_cashier_private()?;
+        let rocks = Rocks::new(&cashier_database_path)?;
+
+        // TODO find a way to start the connection and subscribe to gateway
+        let client = Client::new(
+            cashier_secret,
+            rocks,
+            gateway_addrs,
+            params_paths,
+            client_wallet_path.clone(),
+        )?;
 
 
         Ok(Arc::new(CashierService {
         Ok(Arc::new(CashierService {
             addr,
             addr,
             wallet,
             wallet,
-            gateway,
             btc_client,
             btc_client,
-            mint_params,
-            mint_pvk,
-            spend_params,
-            spend_pvk,
+            client,
         }))
         }))
     }
     }
     pub async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
     pub async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
@@ -88,43 +92,42 @@ impl CashierService {
 
 
         let _ = handle_request_task.cancel().await;
         let _ = handle_request_task.cancel().await;
 
 
-        //let rpc_url: std::net::SocketAddr = config.rpc_url.parse()?;
-
         Ok(())
         Ok(())
     }
     }
 
 
-    async fn mint_dbtc(&mut self, dkey_pub: jubjub::SubgroupPoint, value: u64) -> Result<()> {
-        let cashier_secret = self.wallet.get_cashier_private().unwrap();
-
-        let builder = tx::TransactionBuilder {
-            clear_inputs: vec![tx::TransactionBuilderClearInputInfo {
-                value: value,
-                asset_id: 1,
-                signature_secret: cashier_secret,
-            }],
-            inputs: vec![],
-            outputs: vec![tx::TransactionBuilderOutputInfo {
-                value: value,
-                asset_id: 1,
-                public: dkey_pub,
-            }],
-        };
-
-        let mut tx_data = vec![];
-        {
-            // Build the tx
-            let tx = builder.build(&self.mint_params, &self.spend_params);
-            // Now serialize it
-            tx.encode(&mut tx_data).expect("encode tx");
-        }
-
-        //Add to blockchain
-        let slab = Slab::new(tx_data);
-        //let mut gateway = self.gateway.lock().await;
-        self.gateway.put_slab(slab).await.expect("put slab");
 
 
-        Ok(())
-    }
+    //async fn mint_dbtc(&mut self, dkey_pub: jubjub::SubgroupPoint, value: u64) -> Result<()> {
+    //    let cashier_secret = self.wallet.get_cashier_private().unwrap();
+
+    //    let builder = tx::TransactionBuilder {
+    //        clear_inputs: vec![tx::TransactionBuilderClearInputInfo {
+    //            value: value,
+    //            asset_id: 1,
+    //            signature_secret: cashier_secret,
+    //        }],
+    //        inputs: vec![],
+    //        outputs: vec![tx::TransactionBuilderOutputInfo {
+    //            value: value,
+    //            asset_id: 1,
+    //            public: dkey_pub,
+    //        }],
+    //    };
+
+    //    let mut tx_data = vec![];
+    //    {
+    //        // Build the tx
+    //        let tx = builder.build(&self.mint_params, &self.spend_params);
+    //        // Now serialize it
+    //        tx.encode(&mut tx_data).expect("encode tx");
+    //    }
+
+    //    //Add to blockchain
+    //    let slab = Slab::new(tx_data);
+    //    //let mut gateway = self.gateway.lock().await;
+    //    self.gateway.put_slab(slab).await.expect("put slab");
+
+    //    Ok(())
+    //}
 
 
     async fn handle_request_loop(
     async fn handle_request_loop(
         self: Arc<Self>,
         self: Arc<Self>,