Skip to content

Commit

Permalink
Move connect_to_lightwalletd to Server::connect_direct
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Aug 20, 2024
1 parent 4f7b1c7 commit 93e8c44
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/commands/enhance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use zcash_protocol::consensus::{BlockHeight, BranchId, Network};

use crate::{
data::{get_db_paths, get_wallet_network},
remote::{connect_to_lightwalletd, Servers},
remote::Servers,
};

// Options accepted for the `enhance` command
Expand Down Expand Up @@ -89,7 +89,7 @@ impl Command {
anyhow!("Chain height must be available to perform transaction enhancement.")
})?;

let mut client = connect_to_lightwalletd(self.server.pick(params)?).await?;
let mut client = self.server.pick(params)?.connect_direct().await?;

let mut satisfied_requests = BTreeSet::new();
loop {
Expand Down
8 changes: 2 additions & 6 deletions src/commands/import_ufvk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ use zcash_client_sqlite::WalletDb;
use zcash_keys::keys::UnifiedFullViewingKey;
use zcash_primitives::consensus;

use crate::{
data::get_db_paths,
error,
remote::{connect_to_lightwalletd, Servers},
};
use crate::{data::get_db_paths, error, remote::Servers};

// Options accepted for the `import-ufvk` command
#[derive(Debug, Options)]
Expand Down Expand Up @@ -56,7 +52,7 @@ impl Command {
let birthday = {
// Fetch the tree state corresponding to the last block prior to the wallet's
// birthday height. NOTE: THIS APPROACH LEAKS THE BIRTHDAY TO THE SERVER!
let mut client = connect_to_lightwalletd(self.server.pick(params)?).await?;
let mut client = self.server.pick(params)?.connect_direct().await?;
let request = service::BlockId {
height: (self.birthday - 1).into(),
..Default::default()
Expand Down
4 changes: 2 additions & 2 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use zcash_primitives::consensus::{self, Parameters};
use crate::{
data::{get_db_paths, init_wallet_config, Network},
error,
remote::{connect_to_lightwalletd, Servers},
remote::Servers,
};

// Options accepted for the `init` command
Expand Down Expand Up @@ -50,7 +50,7 @@ impl Command {
let params = consensus::Network::from(opts.network);

// Get the current chain height (for the wallet's birthday).
let mut client = connect_to_lightwalletd(opts.server.pick(params)?).await?;
let mut client = opts.server.pick(params)?.connect_direct().await?;
let birthday = if let Some(birthday) = opts.birthday {
birthday
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use gumdrop::Options;

use crate::{
data::{erase_wallet_state, read_config},
remote::{connect_to_lightwalletd, Servers},
remote::Servers,
};

// Options accepted for the `reset` command
Expand All @@ -27,7 +27,7 @@ impl Command {
let params = keys.network();

// Connect to the client (for re-initializing the wallet).
let client = connect_to_lightwalletd(self.server.pick(params)?).await?;
let client = self.server.pick(params)?.connect_direct().await?;

// Erase the wallet state (excluding key material).
erase_wallet_state(wallet_dir.as_ref()).await;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
commands::propose::{parse_fee_rule, FeeRule},
data::{get_db_paths, read_config},
error,
remote::{connect_to_lightwalletd, Servers},
remote::Servers,
MIN_CONFIRMATIONS,
};

Expand Down Expand Up @@ -87,7 +87,7 @@ impl Command {
)
.map_err(error::Error::from)?;

let mut client = connect_to_lightwalletd(self.server.pick(params)?).await?;
let mut client = self.server.pick(params)?.connect_direct().await?;

// Create the transaction.
println!("Creating transaction...");
Expand Down
4 changes: 2 additions & 2 deletions src/commands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use zcash_protocol::consensus::{BlockHeight, Parameters};
use crate::{
data::{get_block_path, get_db_paths, get_wallet_network},
error,
remote::{connect_to_lightwalletd, Servers},
remote::Servers,
ShutdownListener,
};

Expand Down Expand Up @@ -80,7 +80,7 @@ impl Command {
let fsblockdb_root = fsblockdb_root.as_path();
let mut db_cache = FsBlockDb::for_path(fsblockdb_root).map_err(error::Error::from)?;
let mut db_data = WalletDb::for_path(db_data, params)?;
let mut client = connect_to_lightwalletd(self.server.pick(params)?).await?;
let mut client = self.server.pick(params)?.connect_direct().await?;

#[cfg(any(feature = "transparent-inputs", feature = "tui"))]
let wallet_birthday = db_data
Expand Down
28 changes: 13 additions & 15 deletions src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,23 @@ impl<'a> Server<'a> {
self.port
)
}
}

pub(crate) async fn connect_to_lightwalletd(
server: &Server<'_>,
) -> Result<CompactTxStreamerClient<Channel>, anyhow::Error> {
info!("Connecting to {}", server);
pub(crate) async fn connect_direct(&self) -> anyhow::Result<CompactTxStreamerClient<Channel>> {
info!("Connecting to {}", self);

let channel = Channel::from_shared(server.endpoint())?;
let channel = Channel::from_shared(self.endpoint())?;

let channel = if server.use_tls() {
let tls = ClientTlsConfig::new()
.domain_name(server.host.to_string())
.with_webpki_roots();
channel.tls_config(tls)?
} else {
channel
};
let channel = if self.use_tls() {
let tls = ClientTlsConfig::new()
.domain_name(self.host.to_string())
.with_webpki_roots();
channel.tls_config(tls)?
} else {
channel
};

Ok(CompactTxStreamerClient::new(channel.connect().await?))
Ok(CompactTxStreamerClient::new(channel.connect().await?))
}
}

pub(crate) async fn tor_client<P: AsRef<Path>>(
Expand Down

0 comments on commit 93e8c44

Please sign in to comment.