Skip to content

Commit

Permalink
Merge pull request zingolabs#1279 from fluidvanadium/statassert
Browse files Browse the repository at this point in the history
Generic Test Status
  • Loading branch information
Oscar-Pepper authored Jul 12, 2024
2 parents 180d02c + 20a664d commit 5636e74
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 45 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions darkside-tests/src/chain_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,9 @@ pub(crate) mod impl_conduct_chain_for_darkside_environment {
self.staged_blockheight = self.staged_blockheight + 1;
self.apply_blocks(u64::from(self.staged_blockheight)).await;
}

fn get_chain_height(&mut self) -> u32 {
self.staged_blockheight.into()
}
}
}
7 changes: 7 additions & 0 deletions libtonode-tests/tests/chain_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ mod chain_generics {
target
);
}

fn get_chain_height(&mut self) -> u32 {
self.scenario_builder
.regtest_manager
.get_current_height()
.unwrap()
}
}
}
}
2 changes: 1 addition & 1 deletion zingo-status/src/confirmation_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use zcash_primitives::consensus::BlockHeight;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ConfirmationStatus {
/// The transaction is pending confirmation to the zcash blockchain. It may be waiting in the mempool.
/// The BlockHeight is the height of the chain as the transaction was broadcast.
/// The BlockHeight is the 1 + the height of the chain as the transaction was broadcast, i.e. the target height.
Pending(BlockHeight),
/// The transaction has been included in at-least one block mined to the zcash blockchain.
/// The height of a confirmed block that contains the transaction.
Expand Down
1 change: 1 addition & 0 deletions zingo-testutils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ zingoconfig = { path = "../zingoconfig" }
zingolib = { path = "../zingolib" }
zingo-netutils = { path = "../zingo-netutils", features = ["test-features"] }
zingo-testvectors = { path = "../zingo-testvectors" }
zingo-status = { path = "../zingo-status" }

zcash_client_backend = { workspace = true }
zcash_primitives = { workspace = true }
Expand Down
7 changes: 6 additions & 1 deletion zingo-testutils/src/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use nonempty::NonEmpty;

use zcash_client_backend::proposal::Proposal;
use zcash_primitives::transaction::TxId;

use zingo_status::confirmation_status::ConfirmationStatus;
use zingolib::{lightclient::LightClient, wallet::notes::query::OutputQuery};

/// currently checks:
Expand All @@ -12,10 +14,11 @@ use zingolib::{lightclient::LightClient, wallet::notes::query::OutputQuery};
/// 3. if the fee from the calculate_transaction_fee matches the sum of the per-step fees
/// this currently fails for any broadcast but not confirmed transaction: it seems like get_transaction_fee does not recognize pending spends
/// returns the total fee for the transfer
pub async fn assert_sender_fee<NoteId>(
pub async fn assert_record_fee_and_status<NoteId>(
client: &LightClient,
proposal: &Proposal<zcash_primitives::transaction::fees::zip317::FeeRule, NoteId>,
txids: &NonEmpty<TxId>,
expected_status: ConfirmationStatus,
) -> u64 {
let records = &client
.wallet
Expand All @@ -30,6 +33,8 @@ pub async fn assert_sender_fee<NoteId>(
for (i, step) in proposal.steps().iter().enumerate() {
let record = records.get(&txids[i]).expect("sender must recognize txid");
// does this record match this step?
// we can check that it has the expected status
assert_eq!(record.status, expected_status);
// may fail in uncertain ways if used on a transaction we dont have an OutgoingViewingKey for
let recorded_fee = records.calculate_transaction_fee(record).unwrap();
assert_eq!(recorded_fee, step.balance().fee_required().into_u64());
Expand Down
3 changes: 3 additions & 0 deletions zingo-testutils/src/chain_generics/conduct_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub trait ConductChain {
/// and confirming transactions that were received by the server
async fn bump_chain(&mut self);

/// gets the height. does not yet need to be async
fn get_chain_height(&mut self) -> u32;

/// builds a client and funds it in orchard and syncs it
async fn fund_client_orchard(&mut self, value: u64) -> LightClient {
let faucet = self.create_faucet().await;
Expand Down
82 changes: 56 additions & 26 deletions zingo-testutils/src/chain_generics/fixtures.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! these functions are each meant to be 'test-in-a-box'
//! simply plug in a mock server as a chain conductor and provide some values
use std::sync::Arc;

use zcash_client_backend::PoolType;
use zcash_client_backend::PoolType::Shielded;
use zcash_client_backend::PoolType::Transparent;
Expand All @@ -8,6 +10,7 @@ use zcash_client_backend::ShieldedProtocol::Orchard;
use zcash_client_backend::ShieldedProtocol::Sapling;
use zcash_primitives::transaction::fees::zip317::MARGINAL_FEE;

use zingolib::lightclient::LightClient;
use zingolib::wallet::notes::query::OutputSpendStatusQuery;
use zingolib::wallet::notes::{query::OutputPoolQuery, OutputInterface};
use zingolib::wallet::{data::summaries::ValueTransferKind, notes::query::OutputQuery};
Expand All @@ -31,7 +34,7 @@ where
println!("client is ready to send");

let recipient = environment.create_client().await;
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&sender,
vec![
Expand All @@ -49,6 +52,7 @@ where
),
(&sender, PoolType::Transparent, send_value_self, None),
],
false,
)
.await;
assert_eq!(sender.value_transfers().await.0.len(), 3);
Expand All @@ -70,10 +74,11 @@ where
ValueTransferKind::Received
);

with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&sender,
vec![(&sender, PoolType::Shielded(Orchard), send_value_self, None)],
false,
)
.await;
assert_eq!(sender.value_transfers().await.0.len(), 4);
Expand All @@ -82,7 +87,7 @@ where
ValueTransferKind::SendToSelf
);

with_assertions::propose_shield_bump_sync(&mut environment, &sender).await;
with_assertions::propose_shield_bump_sync(&mut environment, &sender, false).await;
assert_eq!(sender.value_transfers().await.0.len(), 5);
assert_eq!(
sender.value_transfers().await.0[4].kind(),
Expand Down Expand Up @@ -129,10 +134,11 @@ where

println!("recipient ready");

let recorded_fee = with_assertions::propose_send_bump_sync_recipient(
let recorded_fee = with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&sender,
vec![(&recipient, pooltype, send_value, None)],
false,
)
.await;

Expand All @@ -149,13 +155,14 @@ where
let secondary = environment.create_client().await;

assert_eq!(
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&primary,
vec![
(&secondary, Shielded(Orchard), 1, None),
(&secondary, Shielded(Orchard), 29_999, None)
]
],
false,
)
.await,
3 * MARGINAL_FEE.into_u64()
Expand All @@ -175,28 +182,30 @@ where

for _ in 0..n {
assert_eq!(
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&primary,
vec![
(&secondary, Transparent, 100_000, None),
(&secondary, Transparent, 4_000, None)
],
false,
)
.await,
MARGINAL_FEE.into_u64() * 4
);

assert_eq!(
with_assertions::propose_shield_bump_sync(&mut environment, &secondary).await,
with_assertions::propose_shield_bump_sync(&mut environment, &secondary, false,).await,
MARGINAL_FEE.into_u64() * 3
);

assert_eq!(
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&secondary,
vec![(&primary, Shielded(Orchard), 50_000, None)],
false,
)
.await,
MARGINAL_FEE.into_u64() * 2
Expand All @@ -214,23 +223,25 @@ where
let secondary = environment.create_client().await;

assert_eq!(
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&primary,
vec![
(&secondary, Shielded(Orchard), 1, None),
(&secondary, Shielded(Orchard), 99_999, None)
]
],
false,
)
.await,
3 * MARGINAL_FEE.into_u64()
);

assert_eq!(
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&secondary,
vec![(&primary, Shielded(Orchard), 90_000, None)]
vec![(&primary, Shielded(Orchard), 90_000, None)],
false,
)
.await,
2 * MARGINAL_FEE.into_u64()
Expand All @@ -247,23 +258,25 @@ where
let secondary = environment.create_client().await;

assert_eq!(
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&primary,
vec![
(&secondary, Shielded(Orchard), 1, None),
(&secondary, Shielded(Orchard), 99_999, None)
]
],
false,
)
.await,
3 * MARGINAL_FEE.into_u64()
);

assert_eq!(
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&secondary,
vec![(&primary, Shielded(Orchard), 30_000, None)]
vec![(&primary, Shielded(Orchard), 30_000, None)],
false,
)
.await,
2 * MARGINAL_FEE.into_u64()
Expand Down Expand Up @@ -291,7 +304,7 @@ where

// send a bunch of dust
assert_eq!(
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&primary,
vec![
Expand All @@ -306,17 +319,19 @@ where
(&secondary, Shielded(Orchard), 1_000, None),
(&secondary, Shielded(Orchard), 15_000, None),
],
false,
)
.await,
11 * MARGINAL_FEE.into_u64()
);

// combine the only valid sapling note with the only valid orchard note to send
assert_eq!(
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&secondary,
vec![(&primary, Shielded(Orchard), 10_000, None),],
false,
)
.await,
4 * MARGINAL_FEE.into_u64()
Expand Down Expand Up @@ -387,12 +402,13 @@ where

// Send number_of_notes transfers in increasing 10_000 zat increments
assert_eq!(
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&primary,
transaction_1_values
.map(|value| (&secondary, Shielded(Sapling), value, None))
.collect()
.collect(),
false,
)
.await,
expected_fee_for_transaction_1
Expand Down Expand Up @@ -436,10 +452,11 @@ where
* MARGINAL_FEE.into_u64();
// the second client selects notes to cover the transaction.
assert_eq!(
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&secondary,
vec![(&primary, Shielded(Orchard), value_from_transaction_2, None)]
vec![(&primary, Shielded(Orchard), value_from_transaction_2, None)],
false,
)
.await,
expected_fee_for_transaction_2
Expand Down Expand Up @@ -487,10 +504,11 @@ where

let primary = environment.fund_client_orchard(1_000_000).await;
let secondary = environment.create_client().await;
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&primary,
vec![(&secondary, Shielded(shpool), 100_000 + make_change, None)],
false,
)
.await;

Expand All @@ -504,12 +522,24 @@ where
// .into_u64(),
// 0
// );

let ref_tertiary: Arc<LightClient> = Arc::new(tertiary);

// mempool monitor
let check_mempool = false;
if check_mempool {
LightClient::start_mempool_monitor(ref_tertiary.clone());
dbg!("mm started");
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
}

assert_eq!(
expected_fee,
with_assertions::propose_send_bump_sync_recipient(
with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
&secondary,
vec![(&tertiary, pool, 100_000 - expected_fee, None)],
vec![(&ref_tertiary, pool, 100_000 - expected_fee, None)],
check_mempool,
)
.await
);
Expand Down
Loading

0 comments on commit 5636e74

Please sign in to comment.