Skip to content

Commit

Permalink
Merge branch 'dev' into zingo_sync_transparent_with_GetTaddressTxids
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar-Pepper authored Nov 29, 2024
2 parents f9998b9 + fdcc914 commit 2e90f86
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 128 deletions.
226 changes: 121 additions & 105 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions darkside-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ chain_generic_tests = []

[dependencies]
zingolib = { path = "../zingolib", features = ["darkside_tests", "testvectors"] }
zingo-sync = { path = "../zingo-sync" }
zingo-netutils = { path = "../zingo-netutils" }
tokio = { workspace = true, features = ["full"] }
json = { workspace = true }
Expand Down
41 changes: 34 additions & 7 deletions darkside-tests/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::{future::Future, pin::Pin};

use darkside_tests::utils::{
prepare_darksidewalletd, update_tree_states_for_transaction, DarksideConnector, DarksideHandler,
};
use tokio::time::sleep;
use zingolib::config::RegtestNetwork;
use zingolib::get_base_address_macro;
use zingolib::lightclient::PoolBalances;
use zingolib::testutils::{lightclient::from_inputs, scenarios::setup::ClientBuilder};
use zingolib::testvectors::seeds::DARKSIDE_SEED;
use zingolib::{config::RegtestNetwork, lightclient::LightClient};

#[tokio::test]
async fn simple_sync() {
Expand Down Expand Up @@ -48,7 +50,31 @@ async fn simple_sync() {
}

#[tokio::test]
async fn reorg_away_receipt() {
async fn reorg_away_receipt_blaze() {
reorg_receipt_sync_generic(|lc| Box::pin(async { lc.do_sync(true).await.map(|_| ()) })).await;
}

#[ignore = "attempts to unwrap failed checked_sub on sapling output count"]
#[tokio::test]
async fn reorg_away_receipt_pepper() {
reorg_receipt_sync_generic(|lc| {
Box::pin(async {
let uri = lc.config().lightwalletd_uri.read().unwrap().clone();
let client = zingo_netutils::GrpcConnector::new(uri)
.get_client()
.await
.unwrap();
zingo_sync::sync::sync(client, &lc.config().chain.clone(), &mut lc.wallet)
.await
.map_err(|e| e.to_string())
})
})
.await;
}
async fn reorg_receipt_sync_generic<F>(sync_fn: F)
where
F: for<'a> Fn(&'a mut LightClient) -> Pin<Box<dyn Future<Output = Result<(), String>> + 'a>>,
{
let darkside_handler = DarksideHandler::new(None);

let server_id = zingolib::config::construct_lightwalletd_uri(Some(format!(
Expand All @@ -60,11 +86,12 @@ async fn reorg_away_receipt() {
.unwrap();

let regtest_network = RegtestNetwork::all_upgrades_active();
let light_client = ClientBuilder::new(server_id.clone(), darkside_handler.darkside_dir.clone())
.build_client(DARKSIDE_SEED.to_string(), 0, true, regtest_network)
.await;
let mut light_client =
ClientBuilder::new(server_id.clone(), darkside_handler.darkside_dir.clone())
.build_client(DARKSIDE_SEED.to_string(), 0, true, regtest_network)
.await;

light_client.do_sync(true).await.unwrap();
sync_fn(&mut light_client).await.unwrap();
assert_eq!(
light_client.do_balance().await,
PoolBalances {
Expand All @@ -82,7 +109,7 @@ async fn reorg_away_receipt() {
prepare_darksidewalletd(server_id.clone(), false)
.await
.unwrap();
light_client.do_sync(true).await.unwrap();
sync_fn(&mut light_client).await.unwrap();
assert_eq!(
light_client.do_balance().await,
PoolBalances {
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.82"
channel = "1.83"
components = [ "clippy", "rustfmt" ]
2 changes: 1 addition & 1 deletion zingo-sync/src/client/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async fn receive_fetch_requests(
// return `None` if a fetch request could not be selected
fn select_fetch_request(fetch_request_queue: &mut Vec<FetchRequest>) -> Option<FetchRequest> {
// TODO: improve priority logic
if fetch_request_queue.first().is_some() {
if !fetch_request_queue.is_empty() {
Some(fetch_request_queue.remove(0))
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion zingo-sync/src/scan/compact_blocks/runners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl<D: Domain, M> DynamicUsage for BatchReceiver<D, M> {
// linked list. `crossbeam_channel` allocates memory for the linked list in blocks
// of 31 items.
const ITEMS_PER_BLOCK: usize = 31;
let num_blocks = (num_items + ITEMS_PER_BLOCK - 1) / ITEMS_PER_BLOCK;
let num_blocks = num_items.div_ceil(ITEMS_PER_BLOCK);

// The structure of a block is:
// - A pointer to the next block.
Expand Down
61 changes: 52 additions & 9 deletions zingolib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
sync::{Arc, RwLock},
};

use log::LevelFilter;
use log::{info, LevelFilter};
use log4rs::{
append::rolling_file::{
policy::compound::{
Expand Down Expand Up @@ -63,25 +63,29 @@ pub fn margin_fee() -> u64 {
zcash_primitives::transaction::fees::zip317::MARGINAL_FEE.into_u64()
}

/// TODO: Add Doc Comment Here!
/// Same as load_clientconfig but doesn't panic when the server can't be reached
pub fn load_clientconfig(
lightwallet_uri: http::Uri,
data_dir: Option<PathBuf>,
chain: ChainType,
monitor_mempool: bool,
) -> std::io::Result<ZingoConfig> {
use std::net::ToSocketAddrs;
format!(
match format!(
"{}:{}",
lightwallet_uri.host().unwrap(),
lightwallet_uri.port().unwrap()
)
.to_socket_addrs()?
.next()
.ok_or(std::io::Error::new(
ErrorKind::ConnectionRefused,
"Couldn't resolve server!",
))?;
.to_socket_addrs()
{
Ok(_) => {
info!("Connected to {}", lightwallet_uri);
}
Err(e) => {
info!("Couldn't resolve server: {}", e);
}
}
info!("Connected to {}", lightwallet_uri);

// Create a Light Client Config
let config = ZingoConfig {
Expand Down Expand Up @@ -726,3 +730,42 @@ impl ActivationHeights {
}
}
}

mod tests {

#[tokio::test]
async fn test_load_clientconfig_serverless() {
rustls::crypto::ring::default_provider()
.install_default()
.expect("Ring to work as a default");
tracing_subscriber::fmt().init();

let valid_uri = crate::config::construct_lightwalletd_uri(Some(
crate::config::DEFAULT_LIGHTWALLETD_SERVER.to_string(),
));
// let invalid_uri = construct_lightwalletd_uri(Some("Invalid URI".to_string()));
let temp_dir = tempfile::TempDir::new().unwrap();

let temp_path = temp_dir.path().to_path_buf();
// let temp_path_invalid = temp_dir.path().to_path_buf();

let valid_config = crate::config::load_clientconfig(
valid_uri.clone(),
Some(temp_path),
crate::config::ChainType::Mainnet,
true,
)
.unwrap();

assert_eq!(valid_config.get_lightwalletd_uri(), valid_uri);
assert_eq!(valid_config.chain, crate::config::ChainType::Mainnet);

// let invalid_config = load_clientconfig_serverless(
// invalid_uri.clone(),
// Some(temp_path_invalid),
// ChainType::Mainnet,
// true,
// );
// assert_eq!(invalid_config.is_err(), true);
}
}
2 changes: 1 addition & 1 deletion zingolib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(missing_docs)]
#![allow(missing_docs)]
#![forbid(unsafe_code)]
//! ZingoLib
//! Zingo backend code base
Expand Down
4 changes: 1 addition & 3 deletions zingolib/src/lightclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ impl LightWalletSendProgress {
let tx_json_values: Vec<String> = binding
.unwrap()
.iter()
.map(|x| {
return x.as_str().unwrap().to_string();
})
.map(|x| x.as_str().unwrap().to_string())
.collect();
tx_json_values
}
Expand Down

0 comments on commit 2e90f86

Please sign in to comment.