Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

factor code to smaller crates #20

Merged
merged 4 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ log = "0.4.17"
shellwords = "1.1.0"
json = "0.12.4"
http = "0.2.8"
tokio = { version = "1.6.0", features = ["full"] }

zingolib = { path = "../lib/" }
zingoconfig = { path = "../config/" }
59 changes: 54 additions & 5 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::sync::Arc;

use log::{error, info};

use zingoconfig::Network;
use zingolib::lightclient::lightclient_config::LightClientConfig;
use zingoconfig::{Network, ZingoConfig};
use zingolib::{commands, lightclient::LightClient};

pub mod version;
Expand Down Expand Up @@ -87,6 +86,57 @@ pub fn report_permission_error() {
}
}

use std::io::{ErrorKind, Result};
use tokio::runtime::Runtime;
trait ClientAsync {
fn create_on_data_dir(
server: http::Uri,
data_dir: Option<String>,
) -> Result<(ZingoConfig, u64)> {
use std::net::ToSocketAddrs;

let lc = Runtime::new().unwrap().block_on(async move {
// Test for a connection first
format!("{}:{}", server.host().unwrap(), server.port().unwrap())
.to_socket_addrs()?
.next()
.ok_or(std::io::Error::new(
ErrorKind::ConnectionRefused,
"Couldn't resolve server!",
))?;

// Do a getinfo first, before opening the wallet
let info = zingolib::grpc_connector::GrpcConnector::get_info(server.clone())
.await
.map_err(|e| std::io::Error::new(ErrorKind::ConnectionRefused, e))?;

// Create a Light Client Config
let config = ZingoConfig {
server,
chain: match info.chain_name.as_str() {
"main" => Network::Mainnet,
"test" => Network::Testnet,
"regtest" => Network::Regtest,
"fakemainnet" => Network::FakeMainnet,
_ => panic!("Unknown network"),
},
monitor_mempool: true,
anchor_offset: zingoconfig::ANCHOR_OFFSET,
data_dir,
};

Ok((config, info.block_height))
});

lc
}
fn create(server: http::Uri) -> std::io::Result<(ZingoConfig, u64)> {
Self::create_on_data_dir(server, None)
}
}

impl ClientAsync for ZingoConfig {}

pub fn startup(
server: http::Uri,
seed: Option<String>,
Expand All @@ -97,8 +147,7 @@ pub fn startup(
regtest: bool,
) -> std::io::Result<(Sender<(String, Vec<String>)>, Receiver<String>)> {
// Try to get the configuration
let (config, latest_block_height) =
LightClientConfig::create_on_data_dir(server.clone(), data_dir)?;
let (config, latest_block_height) = ZingoConfig::create_on_data_dir(server.clone(), data_dir)?;

// check for regtest flag and network in config.
if regtest && config.chain == Network::Regtest {
Expand Down Expand Up @@ -274,7 +323,7 @@ pub fn command_loop(

pub fn attempt_recover_seed(_password: Option<String>) {
// Create a Light Client Config in an attempt to recover the file.
let _config = LightClientConfig {
let _config = ZingoConfig {
server: "0.0.0.0:0".parse().unwrap(),
chain: zingoconfig::Network::Mainnet,
monitor_mempool: false,
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use zingo_cli::{
attempt_recover_seed, configure_clapapp, report_permission_error, start_interactive, startup,
version::VERSION,
};
use zingolib::lightclient::{self, lightclient_config::LightClientConfig};
use zingoconfig::ZingoConfig;

pub fn main() {
// Get command line arguments
Expand Down Expand Up @@ -52,7 +52,7 @@ pub fn main() {
}
};

let server = LightClientConfig::get_server_or_default(maybe_server);
let server = ZingoConfig::get_server_or_default(maybe_server);

// Test to make sure the server has all of scheme, host and port
if server.scheme_str().is_none() || server.host().is_none() || server.port().is_none() {
Expand Down
4 changes: 4 additions & 0 deletions config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log4rs = "1.1.1"
log = "0.4.14"
http = "0.2.4"
dirs = "3.0.2"
zcash_primitives = { git = "https://github.com/zcash/librustzcash", rev = "8ad60a0fc020ac0338615fd9f242e6eff7b66542", features = ["transparent-inputs", "test-dependencies"] }
zcash_address = { git = "https://github.com/zcash/librustzcash", rev = "8ad60a0fc020ac0338615fd9f242e6eff7b66542"}
Loading