Skip to content

Commit

Permalink
[adityapk00#86] Add support for providing --data-dir to CLI
Browse files Browse the repository at this point in the history
minor comment

Add readme example
  • Loading branch information
pacu committed Aug 27, 2022
1 parent e3d9a36 commit 5471db5
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 132 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ Here are some CLI arguments you can pass to `zecwallet-cli`. Please run `zecwall
* Example: `./zecwallet-cli --seed "twenty four words seed phrase"`
* `--recover`: Attempt to recover the seed phrase from a corrupted wallet

* `--data-dir`: uses the specified path as data directory.
* Example: `./zecwallet-cli --server 127.0.0.1:9067 --data-dir /Users/ZecWalletRocks/my-test-wallet` will use the provided directory to store `zecwallet-light-wallet.dat` and logs. If the provided directory does not exist, it will create it.
34 changes: 16 additions & 18 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use log::{error, info};

use zecwalletlitelib::lightclient::lightclient_config::LightClientConfig;
use zecwalletlitelib::{commands, lightclient::LightClient};
use zecwalletlitelib::{MainNetwork, Parameters};

pub mod version;

Expand Down Expand Up @@ -43,7 +42,13 @@ macro_rules! configure_clapapp {
.value_name("server")
.help("Lightwalletd server to connect to.")
.takes_value(true)
.default_value(lightclient::lightclient_config::DEFAULT_SERVER))
.default_value(lightclient::lightclient_config::DEFAULT_SERVER)
.takes_value(true))
.arg(Arg::with_name("data-dir")
.long("data-dir")
.value_name("data-dir")
.help("Absolute path to use as data directory")
.takes_value(true))
.arg(Arg::with_name("COMMAND")
.help("Command to execute. If a command is not specified, zecwallet-cli will start in interactive mode.")
.required(false)
Expand Down Expand Up @@ -74,12 +79,13 @@ pub fn startup(
server: http::Uri,
seed: Option<String>,
birthday: u64,
data_dir: Option<String>,
first_sync: bool,
print_updates: bool,
) -> io::Result<(Sender<(String, Vec<String>)>, Receiver<String>)> {
// Try to get the configuration
let (config, latest_block_height) = LightClientConfig::create(MainNetwork, server.clone())?;

let (config, latest_block_height) = LightClientConfig::create_on_data_dir(server.clone(), data_dir)?;
let lightclient = match seed {
Some(phrase) => Arc::new(LightClient::new_from_phrase(phrase, &config, birthday, false)?),
None => {
Expand Down Expand Up @@ -139,14 +145,7 @@ pub fn start_interactive(command_tx: Sender<(String, Vec<String>)>, resp_rx: Rec
};

let info = send_command("info".to_string(), vec![]);
let chain_name = match json::parse(&info) {
Ok(s) => s["chain_name"].as_str().unwrap().to_string(),
Err(e) => {
error!("{}", e);
eprintln!("Couldn't get chain name. {}", e);
return;
}
};
let chain_name = json::parse(&info).unwrap()["chain_name"].as_str().unwrap().to_string();

loop {
// Read the height first
Expand Down Expand Up @@ -201,9 +200,7 @@ pub fn start_interactive(command_tx: Sender<(String, Vec<String>)>, resp_rx: Rec
}
}

pub fn command_loop<P: Parameters + Send + Sync + 'static>(
lightclient: Arc<LightClient<P>>,
) -> (Sender<(String, Vec<String>)>, Receiver<String>) {
pub fn command_loop(lightclient: Arc<LightClient>) -> (Sender<(String, Vec<String>)>, Receiver<String>) {
let (command_tx, command_rx) = channel::<(String, Vec<String>)>();
let (resp_tx, resp_rx) = channel::<String>();

Expand Down Expand Up @@ -233,13 +230,14 @@ pub fn command_loop<P: Parameters + Send + Sync + 'static>(

pub fn attempt_recover_seed(_password: Option<String>) {
// Create a Light Client Config in an attempt to recover the file.
let _config = LightClientConfig::<MainNetwork> {
let _config = LightClientConfig {
server: "0.0.0.0:0".parse().unwrap(),
chain_name: "main".to_string(),
sapling_activation_height: 0,
anchor_offset: 0,
monitor_mempool: false,
anchor_offset: [0u32; 5],
data_dir: None,
params: MainNetwork,
};

}

4 changes: 3 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub fn main() {

let maybe_server = matches.value_of("server").map(|s| s.to_string());

let maybe_data_dir = matches.value_of("data-dir").map(|s| s.to_string());

let seed = matches.value_of("seed").map(|s| s.to_string());
let maybe_birthday = matches.value_of("birthday");

Expand Down Expand Up @@ -60,7 +62,7 @@ pub fn main() {

let nosync = matches.is_present("nosync");

let startup_chan = startup(server, seed, birthday, !nosync, command.is_none());
let startup_chan = startup(server, seed, birthday, maybe_data_dir ,!nosync, command.is_none());
let (command_tx, resp_rx) = match startup_chan {
Ok(c) => c,
Err(e) => {
Expand Down
Loading

0 comments on commit 5471db5

Please sign in to comment.