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

Upgrade pending_notes_are_not_saved to use conduct_chain #1531

Merged
merged 3 commits into from
Nov 18, 2024
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
76 changes: 1 addition & 75 deletions libtonode-tests/tests/chain_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod chain_generics {
use zcash_client_backend::ShieldedProtocol::Sapling;

use zingolib::testutils::chain_generics::fixtures;
use zingolib::testutils::chain_generics::libtonode::LibtonodeEnvironment;

use conduct_chain::LibtonodeEnvironment;
#[tokio::test]
async fn generate_a_range_of_value_transfers() {
fixtures::create_various_value_transfers::<LibtonodeEnvironment>().await;
Expand Down Expand Up @@ -314,78 +314,4 @@ mod chain_generics {
fixtures::to_pool_unfunded_error::<LibtonodeEnvironment>(Shielded(Orchard), 1_000_000)
.await;
}
mod conduct_chain {
use zcash_client_backend::PoolType;

use zcash_client_backend::ShieldedProtocol::Sapling;

use zingolib::config::RegtestNetwork;
use zingolib::lightclient::LightClient;
use zingolib::testutils::chain_generics::conduct_chain::ConductChain;
use zingolib::testutils::scenarios::setup::ScenarioBuilder;

pub(crate) struct LibtonodeEnvironment {
regtest_network: RegtestNetwork,
scenario_builder: ScenarioBuilder,
}

/// known issues include --slow
/// these tests cannot portray the full range of network weather.
impl ConductChain for LibtonodeEnvironment {
async fn setup() -> Self {
let regtest_network = RegtestNetwork::all_upgrades_active();
let scenario_builder = ScenarioBuilder::build_configure_launch(
Some(PoolType::Shielded(Sapling)),
None,
None,
&regtest_network,
)
.await;
LibtonodeEnvironment {
regtest_network,
scenario_builder,
}
}

async fn create_faucet(&mut self) -> LightClient {
self.scenario_builder
.client_builder
.build_faucet(false, self.regtest_network)
.await
}

fn zingo_config(&mut self) -> zingolib::config::ZingoConfig {
self.scenario_builder
.client_builder
.make_unique_data_dir_and_load_config(self.regtest_network)
}

async fn bump_chain(&mut self) {
let start_height = self
.scenario_builder
.regtest_manager
.get_current_height()
.unwrap();
let target = start_height + 1;
self.scenario_builder
.regtest_manager
.generate_n_blocks(1)
.expect("Called for side effect, failed!");
assert_eq!(
self.scenario_builder
.regtest_manager
.get_current_height()
.unwrap(),
target
);
}

fn get_chain_height(&mut self) -> u32 {
self.scenario_builder
.regtest_manager
.get_current_height()
.unwrap()
}
}
}
}
82 changes: 45 additions & 37 deletions libtonode-tests/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod load_wallet {
use zingolib::lightclient::send::send_with_proposal::QuickSendError;
use zingolib::lightclient::LightClient;
use zingolib::lightclient::PoolBalances;
use zingolib::testutils::chain_generics::conduct_chain::ConductChain as _;
use zingolib::testutils::chain_generics::libtonode::LibtonodeEnvironment;
use zingolib::testutils::lightclient::from_inputs;
use zingolib::testutils::paths::get_cargo_manifest_dir;
use zingolib::testutils::scenarios;
Expand Down Expand Up @@ -206,17 +208,16 @@ mod load_wallet {

#[tokio::test]
async fn pending_notes_are_not_saved() {
let regtest_network = RegtestNetwork::all_upgrades_active();
let (regtest_manager, _cph, faucet, recipient) = scenarios::faucet_recipient(
PoolType::Shielded(ShieldedProtocol::Sapling),
regtest_network,
)
.await;
zingolib::testutils::increase_height_and_wait_for_client(&regtest_manager, &faucet, 1)
.await
.unwrap();
let mut environment = LibtonodeEnvironment::setup().await;

let faucet = environment.create_faucet().await;
let recipient = environment.create_client().await;

environment.bump_chain().await;
faucet.do_sync(false).await.unwrap();

check_client_balances!(faucet, o: 0 s: 2_500_000_000u64 t: 0u64);

let pending_txid = *from_inputs::quick_send(
&faucet,
vec![(
Expand All @@ -242,52 +243,59 @@ mod load_wallet {
faucet.do_list_notes(true).await["unspent_orchard_notes"].len(),
1
);

// Create a new client using the faucet's wallet
let faucet_reloaded = {
let mut wallet_location = environment.scenario_builder.regtest_manager.zingo_datadir;
wallet_location.pop();
wallet_location.push("zingo_client_1");
// Create zingo config
let zingo_config = ZingoConfig::build(zingolib::config::ChainType::Regtest(
environment.regtest_network,
))
.set_wallet_dir(wallet_location.clone())
.create();
wallet_location.push("zingo-wallet.dat");
let read_buffer = File::open(wallet_location.clone()).unwrap();

// Create zingo config
let mut wallet_location = regtest_manager.zingo_datadir;
wallet_location.pop();
wallet_location.push("zingo_client_1");
let zingo_config =
ZingoConfig::build(zingolib::config::ChainType::Regtest(regtest_network))
.set_wallet_dir(wallet_location.clone())
.create();
wallet_location.push("zingo-wallet.dat");
let read_buffer = File::open(wallet_location.clone()).unwrap();
// Create wallet from faucet zingo-wallet.dat
let faucet_wallet =
zingolib::wallet::LightWallet::read_internal(read_buffer, &zingo_config)
.await
.unwrap();

// Create wallet from faucet zingo-wallet.dat
let faucet_wallet =
zingolib::wallet::LightWallet::read_internal(read_buffer, &zingo_config)
// Create client based on config and wallet of faucet
LightClient::create_from_wallet_async(faucet_wallet)
.await
.unwrap();
.unwrap()
};

// Create client based on config and wallet of faucet
let faucet_copy = LightClient::create_from_wallet_async(faucet_wallet)
.await
.unwrap();
assert_eq!(
&faucet_copy.do_seed_phrase().await.unwrap(),
&faucet_reloaded.do_seed_phrase().await.unwrap(),
&faucet.do_seed_phrase().await.unwrap()
); // Sanity check identity

// assert that the unconfirmed unspent orchard note was dropped
assert_eq!(
faucet.do_list_notes(true).await["unspent_orchard_notes"].len(),
1
);
assert_eq!(
faucet_copy.do_list_notes(true).await["unspent_orchard_notes"].len(),
faucet_reloaded.do_list_notes(true).await["unspent_orchard_notes"].len(),
0
);
assert!(!faucet_copy

// assert that the unconfirmed transaction was dropped
assert!(!faucet_reloaded
.transaction_summaries()
.await
.iter()
.any(|transaction_summary| transaction_summary.txid() == pending_txid));

// sanity-check that the other transactions in the wallet are identical.
let mut faucet_transactions = faucet.do_list_transactions().await;
let mut faucet_reloaded_transactions = faucet_reloaded.do_list_transactions().await;

faucet_transactions.pop();
faucet_transactions.pop();
let mut faucet_copy_transactions = faucet_copy.do_list_transactions().await;
faucet_copy_transactions.pop();
assert_eq!(faucet_transactions, faucet_copy_transactions);
faucet_reloaded_transactions.pop();
assert_eq!(faucet_transactions, faucet_reloaded_transactions);
}

#[tokio::test]
Expand Down
1 change: 1 addition & 0 deletions zingolib/src/testutils/chain_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! - mempool

pub mod conduct_chain;
pub mod libtonode;
pub mod live_chain;

pub mod fixtures;
Expand Down
76 changes: 76 additions & 0 deletions zingolib/src/testutils/chain_generics/libtonode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! libtonode tests use zcashd regtest mode to mock a chain

use zcash_client_backend::PoolType;

use zcash_client_backend::ShieldedProtocol::Sapling;

use crate::config::RegtestNetwork;
use crate::lightclient::LightClient;
use crate::testutils::chain_generics::conduct_chain::ConductChain;
use crate::testutils::scenarios::setup::ScenarioBuilder;

/// includes utilities for connecting to zcashd regtest
pub struct LibtonodeEnvironment {
/// internal RegtestNetwork
pub regtest_network: RegtestNetwork,
/// internal ScenarioBuilder
pub scenario_builder: ScenarioBuilder,
}

/// known issues include --slow
/// these tests cannot portray the full range of network weather.
impl ConductChain for LibtonodeEnvironment {
async fn setup() -> Self {
let regtest_network = RegtestNetwork::all_upgrades_active();
let scenario_builder = ScenarioBuilder::build_configure_launch(
Some(PoolType::Shielded(Sapling)),
None,
None,
&regtest_network,
)
.await;
LibtonodeEnvironment {
regtest_network,
scenario_builder,
}
}

async fn create_faucet(&mut self) -> LightClient {
self.scenario_builder
.client_builder
.build_faucet(false, self.regtest_network)
.await
}

fn zingo_config(&mut self) -> crate::config::ZingoConfig {
self.scenario_builder
.client_builder
.make_unique_data_dir_and_load_config(self.regtest_network)
}

async fn bump_chain(&mut self) {
let start_height = self
.scenario_builder
.regtest_manager
.get_current_height()
.unwrap();
self.scenario_builder
.regtest_manager
.generate_n_blocks(1)
.expect("Called for side effect, failed!");
assert_eq!(
self.scenario_builder
.regtest_manager
.get_current_height()
.unwrap(),
start_height + 1
);
}

fn get_chain_height(&mut self) -> u32 {
self.scenario_builder
.regtest_manager
.get_current_height()
.unwrap()
}
}