mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
add config file to client cli
This commit is contained in:
@@ -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<SocketAddr>, 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<Executor<'_>>, options: Arc<WalletCli>) -> 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<Executor<'_>>,
|
||||
config: Arc<cli_config::Config>,
|
||||
_options: Arc<WalletCli>,
|
||||
) -> 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::<columns::Slabs>::new(rocks.clone());
|
||||
@@ -194,6 +190,12 @@ async fn start(executor: Arc<Executor<'_>>, options: Arc<WalletCli>) -> Result<(
|
||||
let merkle_roots = RocksColumn::<columns::MerkleRoots>::new(rocks.clone());
|
||||
let nullifiers = RocksColumn::<columns::Nullifiers>::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<Executor<'_>>, options: Arc<WalletCli>) -> 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>(())
|
||||
})
|
||||
|
||||
@@ -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<Config> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,82 +1,29 @@
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use crate::Result;
|
||||
|
||||
use clap::{App, Arg};
|
||||
|
||||
|
||||
|
||||
pub struct WalletCli {
|
||||
pub connect_addr: Option<SocketAddr>,
|
||||
pub sub_addr: Option<SocketAddr>,
|
||||
pub verbose: bool,
|
||||
pub database_path: Box<std::path::PathBuf>,
|
||||
pub log_path: Box<std::path::PathBuf>,
|
||||
pub rpc_port: u16,
|
||||
}
|
||||
|
||||
impl WalletCli {
|
||||
pub fn load() -> Result<Self> {
|
||||
// let app = App::new("dfi").version("0.1.0").author("Amir Taaki <amir@dyne.org>").about("Run Service Client");
|
||||
let app = clap_app!(dfi =>
|
||||
(version: "0.1.0")
|
||||
(author: "Amir Taaki <amir@dyne.org>")
|
||||
(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 <amir@dyne.org>")
|
||||
.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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
|
||||
@@ -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<Executor<'_>>,
|
||||
options: Arc<WalletCli>,
|
||||
config: Arc<cli_config::Config>,
|
||||
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::<TcpListener>::bind(([127, 0, 0, 1], options.rpc_port))?,
|
||||
Async::<TcpListener>::bind(rpc_url)?,
|
||||
None,
|
||||
);
|
||||
|
||||
|
||||
@@ -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<WalletDB>;
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user