diff --git a/src/bin/darkfid.rs b/src/bin/darkfid.rs index 176ebe27b..f64cbc014 100644 --- a/src/bin/darkfid.rs +++ b/src/bin/darkfid.rs @@ -1,12 +1,5 @@ -use async_std::sync::Arc; -//use drk::rpc:: -use drk::rpc::adapter::RpcAdapter; -use drk::rpc::jsonserver; -//use drk::rpc::options::ProgramOptions; -use rand::rngs::OsRng; -use std::net::SocketAddr; - use drk::blockchain::{rocks::columns, Rocks, RocksColumn}; +use drk::cli::{cli_config, WalletCli}; use drk::crypto::{ load_params, merkle::{CommitmentTree, IncrementalWitness}, @@ -17,20 +10,26 @@ use drk::crypto::{ }; use drk::serial::Decodable; use drk::service::{GatewayClient, GatewaySlabsSubscriber}; -use drk::cli::WalletCli; use drk::state::{state_transition, ProgramState, StateUpdate}; +use drk::util::join_config_path; use drk::wallet::{WalletDB, WalletPtr}; use drk::{tx, Result}; -use drk::util::join_config_path; -use rusqlite::Connection; +//use drk::rpc:: +use drk::rpc::adapter::RpcAdapter; +use drk::rpc::jsonserver; use async_executor::Executor; use bellman::groth16; use bls12_381::Bls12; use easy_parallel::Parallel; use ff::Field; +use rand::rngs::OsRng; +use rusqlite::Connection; +use async_std::sync::Arc; +use std::net::SocketAddr; use std::path::Path; +use std::path::PathBuf; #[allow(dead_code)] pub struct State { @@ -142,13 +141,6 @@ impl State { } } -fn setup_addr(address: Option, default: SocketAddr) -> SocketAddr { - match address { - Some(addr) => addr, - None => default, - } -} - pub async fn subscribe(gateway_slabs_sub: GatewaySlabsSubscriber, mut state: State) -> Result<()> { loop { let slab = gateway_slabs_sub.recv().await?; @@ -159,12 +151,16 @@ pub async fn subscribe(gateway_slabs_sub: GatewaySlabsSubscriber, mut state: Sta } } -async fn start(executor: Arc>, options: Arc) -> Result<()> { - let connect_addr: SocketAddr = setup_addr(options.connect_addr, "127.0.0.1:3333".parse()?); - let sub_addr: SocketAddr = setup_addr(options.sub_addr, "127.0.0.1:4444".parse()?); - let database_path = options.database_path.clone(); +async fn start( + executor: Arc>, + config: Arc, + _options: Arc, +) -> Result<()> { + let connect_addr: SocketAddr = config.connect_url.parse()?; + let sub_addr: SocketAddr = config.subscriber_url.parse()?; + let database_path = config.database_path.clone(); - let database_path = join_config_path(&(*database_path))?; + let database_path = join_config_path(&PathBuf::from(database_path))?; let rocks = Rocks::new(&database_path)?; let slabstore = RocksColumn::::new(rocks.clone()); @@ -194,6 +190,12 @@ async fn start(executor: Arc>, options: Arc) -> Result<( let merkle_roots = RocksColumn::::new(rocks.clone()); let nullifiers = RocksColumn::::new(rocks); + //let wallet = adapter.wallet; + let wallet = Arc::new(WalletDB::new("wallet.db")?); + + //let wallet2 = wallet.clone(); + let ex = executor.clone(); + let state = State { tree: CommitmentTree::empty(), merkle_roots, @@ -216,7 +218,7 @@ async fn start(executor: Arc>, options: Arc) -> Result<( let adapter = RpcAdapter::new(wallet.clone())?; // start the rpc server - jsonserver::start(ex.clone(), options.clone(), adapter).await?; + jsonserver::start(ex.clone(), config.clone(), adapter).await?; subscribe_task.cancel().await; Ok(()) @@ -229,6 +231,7 @@ fn main() -> Result<()> { let (signal, shutdown) = async_channel::unbounded::<()>(); let options = Arc::new(WalletCli::load()?); + let config = Arc::new(cli_config::Config::default()); let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build(); @@ -238,12 +241,13 @@ fn main() -> Result<()> { LevelFilter::Off }; + let log_path = config.log_path.clone(); CombinedLogger::init(vec![ TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(), WriteLogger::new( LevelFilter::Debug, Config::default(), - std::fs::File::create(options.log_path.as_path()).unwrap(), + std::fs::File::create(log_path).unwrap(), ), ]) .unwrap(); @@ -256,7 +260,7 @@ fn main() -> Result<()> { // Run the main future on the current thread. .finish(|| { smol::future::block_on(async move { - start(ex2, options).await?; + start(ex2, config, options).await?; drop(signal); Ok::<(), drk::Error>(()) }) diff --git a/src/cli/client_cli/cli_config.rs b/src/cli/client_cli/cli_config.rs index 0ba7b3071..9a0030804 100644 --- a/src/cli/client_cli/cli_config.rs +++ b/src/cli/client_cli/cli_config.rs @@ -1,13 +1,13 @@ +use crate::serial::{Encodable, Decodable, serialize, deserialize}; +use crate::Result; +use crate::util::join_config_path; + use std::{ fs::{create_dir_all, File}, io::prelude::*, path::PathBuf, }; -use crate::serial::{Encodable, Decodable, serialize, deserialize}; -use crate::Result; - - pub struct Config { pub connect_url: String, pub subscriber_url: String, @@ -35,10 +35,11 @@ impl Default for Config { impl Config { pub fn load(path: PathBuf) -> Result { + let path = join_config_path(&path)?; load_config_file(path) } - pub fn save(&self, path: PathBuf) -> Result <()> { + let path = join_config_path(&path)?; save_config_file(self, path) } } diff --git a/src/cli/client_cli/wallet_cli.rs b/src/cli/client_cli/wallet_cli.rs index b012be006..afe3e0d70 100644 --- a/src/cli/client_cli/wallet_cli.rs +++ b/src/cli/client_cli/wallet_cli.rs @@ -1,82 +1,29 @@ - -use std::net::SocketAddr; - use crate::Result; use clap::{App, Arg}; - - pub struct WalletCli { - pub connect_addr: Option, - pub sub_addr: Option, pub verbose: bool, - pub database_path: Box, - pub log_path: Box, - pub rpc_port: u16, } impl WalletCli { pub fn load() -> Result { - // let app = App::new("dfi").version("0.1.0").author("Amir Taaki ").about("Run Service Client"); - let app = clap_app!(dfi => - (version: "0.1.0") - (author: "Amir Taaki ") - (about: "Run Service Client") - (@arg CONNECT: -c --connect +takes_value "Connect add//ress") - (@arg SUB_ADDR: -s --subaddr +takes_value "Subscriber addr") - (@arg VERBOSE: -v --verbose "Increase verbosity") - (@arg DATABASE_PATH: --database +takes_value "database path") - (@arg LOG_PATH: --log +takes_value "Logfile path") - (@arg RPC_PORT: -r --rpc +takes_value "RPC port") - ) - .get_matches(); - - let connect_addr = if let Some(connect_addr) = app.value_of("CONNECT") { - Some(connect_addr.parse()?) - } else { - None - }; - - let sub_addr = if let Some(sub_addr) = app.value_of("SUB_ADDR") { - Some(sub_addr.parse()?) - } else { - None - }; + let app = App::new("Wallet CLI") + .version("0.1.0") + .author("Amir Taaki ") + .about("Run Service Client") + .arg( + Arg::new("verbose") + .short('v') + .help_heading(Some("Increase verbosity")) + .long("verbose") + .takes_value(false) + ).get_matches(); let verbose = app.is_present("VERBOSE"); - let database_path = Box::new( - if let Some(database_path) = app.value_of("DATABASE_PATH") { - std::path::Path::new(database_path) - } else { - std::path::Path::new("database_client.db") - } - .to_path_buf(), - ); - - let log_path = Box::new( - if let Some(log_path) = app.value_of("LOG_PATH") { - std::path::Path::new(log_path) - } else { - std::path::Path::new("/tmp/darkfid_service_daemon.log") - } - .to_path_buf(), - ); - - let rpc_port = if let Some(rpc_port) = app.value_of("RPC_PORT") { - rpc_port.parse()? - } else { - 8000 - }; - - Ok(Self{ - connect_addr, - sub_addr, + Ok(Self { verbose, - database_path, - log_path, - rpc_port, }) } } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 849ab9371..aed15308b 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -3,6 +3,7 @@ pub mod client_cli; pub mod service_cli; -pub use client_cli::wallet_cli::WalletCli; +pub use client_cli::{cli_config, wallet_cli::WalletCli}; pub use service_cli::ServiceCli; + diff --git a/src/rpc/jsonserver.rs b/src/rpc/jsonserver.rs index 6046227fd..f7e4aff1b 100644 --- a/src/rpc/jsonserver.rs +++ b/src/rpc/jsonserver.rs @@ -1,6 +1,5 @@ -use jsonrpc_core::types::error::ErrorCode as JsonError; use crate::rpc::adapter::RpcAdapter; -use crate::cli::WalletCli; +use crate::cli::cli_config; use crate::{Error, Result}; use async_executor::Executor; use async_native_tls::TlsAcceptor; @@ -74,14 +73,15 @@ pub async fn listen( pub async fn start( executor: Arc>, - options: Arc, + config: Arc, adapter: RpcAdapter, ) -> Result<()> { let rpc = RpcInterface::new(adapter)?; + let rpc_url: std::net::SocketAddr = config.rpc_url.parse()?; let http = listen( executor.clone(), rpc.clone(), - Async::::bind(([127, 0, 0, 1], options.rpc_port))?, + Async::::bind(rpc_url)?, None, ); diff --git a/src/wallet/walletdb.rs b/src/wallet/walletdb.rs index a80256c7c..cbb4b8209 100644 --- a/src/wallet/walletdb.rs +++ b/src/wallet/walletdb.rs @@ -9,6 +9,7 @@ use ff::Field; use log::*; use rand::rngs::OsRng; use rusqlite::{named_params, Connection}; + use std::path::PathBuf; pub type WalletPtr = Arc; @@ -60,7 +61,6 @@ impl WalletDB { } pub async fn init_cashier_db(&self) -> Result<()> { - let path = Self::create_path("cashier.db")?; let conn = Connection::open(&self.path)?; debug!(target: "walletdb", "OPENED CONNECTION AT PATH {:?}", self.path); let contents = include_str!("../../res/schema.sql");