Skip to content

Commit

Permalink
Merge pull request #1250 from zcash/zcs-orchard-testing
Browse files Browse the repository at this point in the history
zcash_client_sqlite: Run single-shielded-pool tests on Orchard
  • Loading branch information
nuttycom authored Mar 10, 2024
2 parents 58f46e4 + 10e1bb6 commit 0d4a730
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 53 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
matrix.extra_flags != 'NOT_A_PUZZLE' && format(' with --features {0}', matrix.extra_flags) || ''
}}
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.extra_flags != 'NOT_A_PUZZLE' }}
strategy:
matrix:
os: [ubuntu-latest-8cores, windows-latest-8cores, macOS-latest]
Expand Down
16 changes: 1 addition & 15 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ members = [
"components/zcash_protocol",
"zcash_client_backend",
"zcash_client_sqlite",
"zcash_extensions",
# Disabled until we replace the `zfutures` feature flag with a compiler flag.
# "zcash_extensions",
"zcash_history",
"zcash_keys",
"zcash_primitives",
Expand Down Expand Up @@ -108,6 +109,7 @@ lazy_static = "1"
assert_matches = "1.5"
criterion = "0.4"
proptest = "1"
rand_chacha = "0.3"
rand_xorshift = "0.3"

# ZIP 32
Expand Down
4 changes: 4 additions & 0 deletions zcash_client_sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,22 @@ pasta_curves.workspace = true
shardtree = { workspace = true, features = ["legacy-api", "test-dependencies"] }
nonempty.workspace = true
proptest.workspace = true
rand_chacha.workspace = true
rand_core.workspace = true
regex = "1.4"
tempfile = "3.5.0"
zcash_keys = { workspace = true, features = ["test-dependencies"] }
zcash_note_encryption.workspace = true
zcash_proofs = { workspace = true, features = ["bundled-prover"] }
zcash_primitives = { workspace = true, features = ["test-dependencies"] }
zcash_protocol = { workspace = true, features = ["local-consensus"] }
zcash_client_backend = { workspace = true, features = ["test-dependencies", "unstable-serialization", "unstable-spanning-tree"] }
zcash_address = { workspace = true, features = ["test-dependencies"] }

[features]
default = ["multicore"]
unstable-nu6 = ["zcash_primitives/unstable-nu6"]
zfuture = ["zcash_primitives/zfuture"]

## Enables multithreading support for creating proofs and building subtrees.
multicore = ["maybe-rayon/threads", "zcash_primitives/multicore"]
Expand Down
2 changes: 2 additions & 0 deletions zcash_client_sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ pub(crate) const PRUNING_DEPTH: u32 = 100;
pub(crate) const VERIFY_LOOKAHEAD: u32 = 10;

pub(crate) const SAPLING_TABLES_PREFIX: &str = "sapling";
#[cfg(feature = "orchard")]
pub(crate) const ORCHARD_TABLES_PREFIX: &str = "orchard";

#[cfg(not(feature = "transparent-inputs"))]
pub(crate) const UA_TRANSPARENT: bool = false;
Expand Down
74 changes: 54 additions & 20 deletions zcash_client_sqlite/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::fs::File;

use nonempty::NonEmpty;
use prost::Message;
use rand_core::{CryptoRng, OsRng, RngCore};
use rand_chacha::ChaChaRng;
use rand_core::{CryptoRng, RngCore, SeedableRng};
use rusqlite::{params, Connection};
use secrecy::{Secret, SecretVec};
use tempfile::NamedTempFile;
Expand Down Expand Up @@ -50,7 +51,7 @@ use zcash_client_backend::{
use zcash_note_encryption::Domain;
use zcash_primitives::{
block::BlockHash,
consensus::{self, BlockHeight, Network, NetworkUpgrade, Parameters},
consensus::{self, BlockHeight, NetworkUpgrade, Parameters},
memo::{Memo, MemoBytes},
transaction::{
components::{amount::NonNegativeAmount, sapling::zip212_enforcement},
Expand All @@ -59,6 +60,7 @@ use zcash_primitives::{
},
zip32::DiversifierIndex,
};
use zcash_protocol::local_consensus::LocalNetwork;

use crate::{
chain::init::init_cache_database,
Expand Down Expand Up @@ -98,18 +100,33 @@ pub(crate) mod pool;

/// A builder for a `zcash_client_sqlite` test.
pub(crate) struct TestBuilder<Cache> {
network: Network,
network: LocalNetwork,
cache: Cache,
test_account_birthday: Option<AccountBirthday>,
rng: ChaChaRng,
}

impl TestBuilder<()> {
/// Constructs a new test.
pub(crate) fn new() -> Self {
TestBuilder {
network: Network::TestNetwork,
// Use a fake network where Sapling through NU5 activate at the same height.
// We pick 100,000 to be large enough to handle any hard-coded test offsets.
network: LocalNetwork {
overwinter: Some(BlockHeight::from_u32(1)),
sapling: Some(BlockHeight::from_u32(100_000)),
blossom: Some(BlockHeight::from_u32(100_000)),
heartwood: Some(BlockHeight::from_u32(100_000)),
canopy: Some(BlockHeight::from_u32(100_000)),
nu5: Some(BlockHeight::from_u32(100_000)),
#[cfg(feature = "unstable-nu6")]
nu6: None,
#[cfg(feature = "zfuture")]
z_future: None,
},
cache: (),
test_account_birthday: None,
rng: ChaChaRng::seed_from_u64(0),
}
}

Expand All @@ -119,6 +136,7 @@ impl TestBuilder<()> {
network: self.network,
cache: BlockCache::new(),
test_account_birthday: self.test_account_birthday,
rng: self.rng,
}
}

Expand All @@ -129,12 +147,13 @@ impl TestBuilder<()> {
network: self.network,
cache: FsBlockCache::new(),
test_account_birthday: self.test_account_birthday,
rng: self.rng,
}
}
}

impl<Cache> TestBuilder<Cache> {
pub(crate) fn with_test_account<F: FnOnce(&Network) -> AccountBirthday>(
pub(crate) fn with_test_account<F: FnOnce(&LocalNetwork) -> AccountBirthday>(
mut self,
birthday: F,
) -> Self {
Expand Down Expand Up @@ -162,6 +181,7 @@ impl<Cache> TestBuilder<Cache> {
_data_file: data_file,
db_data,
test_account,
rng: self.rng,
}
}
}
Expand Down Expand Up @@ -215,13 +235,14 @@ pub(crate) struct TestState<Cache> {
cache: Cache,
latest_cached_block: Option<CachedBlock>,
_data_file: NamedTempFile,
db_data: WalletDb<Connection, Network>,
db_data: WalletDb<Connection, LocalNetwork>,
test_account: Option<(
SecretVec<u8>,
AccountId,
UnifiedSpendingKey,
AccountBirthday,
)>,
rng: ChaChaRng,
}

impl<Cache: TestCache> TestState<Cache>
Expand Down Expand Up @@ -291,6 +312,7 @@ where
value,
initial_sapling_tree_size,
initial_orchard_tree_size,
&mut self.rng,
);
let res = self.cache.insert(&cb);

Expand Down Expand Up @@ -332,6 +354,7 @@ where
value,
cached_block.sapling_end_size,
cached_block.orchard_end_size,
&mut self.rng,
);
let res = self.cache.insert(&cb);

Expand Down Expand Up @@ -383,6 +406,7 @@ where
tx,
cached_block.sapling_end_size,
cached_block.orchard_end_size,
&mut self.rng,
);
let res = self.cache.insert(&cb);

Expand Down Expand Up @@ -460,17 +484,17 @@ where

impl<Cache> TestState<Cache> {
/// Exposes an immutable reference to the test's [`WalletDb`].
pub(crate) fn wallet(&self) -> &WalletDb<Connection, Network> {
pub(crate) fn wallet(&self) -> &WalletDb<Connection, LocalNetwork> {
&self.db_data
}

/// Exposes a mutable reference to the test's [`WalletDb`].
pub(crate) fn wallet_mut(&mut self) -> &mut WalletDb<Connection, Network> {
pub(crate) fn wallet_mut(&mut self) -> &mut WalletDb<Connection, LocalNetwork> {
&mut self.db_data
}

/// Exposes the network in use.
pub(crate) fn network(&self) -> Network {
pub(crate) fn network(&self) -> LocalNetwork {
self.db_data.params
}

Expand Down Expand Up @@ -501,6 +525,14 @@ impl<Cache> TestState<Cache> {
.and_then(|(_, _, usk, _)| usk.to_unified_full_viewing_key().sapling().cloned())
}

/// Exposes the test account's Sapling DFVK, if enabled via [`TestBuilder::with_test_account`].
#[cfg(feature = "orchard")]
pub(crate) fn test_account_orchard(&self) -> Option<orchard::keys::FullViewingKey> {
self.test_account
.as_ref()
.and_then(|(_, _, usk, _)| usk.to_unified_full_viewing_key().orchard().cloned())
}

/// Invokes [`create_spend_to_address`] with the given arguments.
#[allow(deprecated)]
#[allow(clippy::type_complexity)]
Expand Down Expand Up @@ -561,7 +593,7 @@ impl<Cache> TestState<Cache> {
>,
>
where
InputsT: InputSelector<InputSource = WalletDb<Connection, Network>>,
InputsT: InputSelector<InputSource = WalletDb<Connection, LocalNetwork>>,
{
#![allow(deprecated)]
let params = self.network();
Expand Down Expand Up @@ -597,7 +629,7 @@ impl<Cache> TestState<Cache> {
>,
>
where
InputsT: InputSelector<InputSource = WalletDb<Connection, Network>>,
InputsT: InputSelector<InputSource = WalletDb<Connection, LocalNetwork>>,
{
let params = self.network();
propose_transfer::<_, _, _, Infallible>(
Expand Down Expand Up @@ -673,7 +705,7 @@ impl<Cache> TestState<Cache> {
>,
>
where
InputsT: ShieldingSelector<InputSource = WalletDb<Connection, Network>>,
InputsT: ShieldingSelector<InputSource = WalletDb<Connection, LocalNetwork>>,
{
let params = self.network();
propose_shielding::<_, _, _, Infallible>(
Expand Down Expand Up @@ -737,7 +769,7 @@ impl<Cache> TestState<Cache> {
>,
>
where
InputsT: ShieldingSelector<InputSource = WalletDb<Connection, Network>>,
InputsT: ShieldingSelector<InputSource = WalletDb<Connection, LocalNetwork>>,
{
let params = self.network();
let prover = test_prover();
Expand Down Expand Up @@ -1128,10 +1160,8 @@ fn fake_compact_block<P: consensus::Parameters, Fvk: TestFvk>(
value: NonNegativeAmount,
initial_sapling_tree_size: u32,
initial_orchard_tree_size: u32,
mut rng: impl RngCore + CryptoRng,
) -> (CompactBlock, Fvk::Nullifier) {
// Create a fake Note for the account
let mut rng = OsRng;

// Create a fake CompactBlock containing the note
let mut ctx = fake_compact_tx(&mut rng);
let nf = fvk.add_output(
Expand All @@ -1150,6 +1180,7 @@ fn fake_compact_block<P: consensus::Parameters, Fvk: TestFvk>(
prev_hash,
initial_sapling_tree_size,
initial_orchard_tree_size,
rng,
);
(cb, nf)
}
Expand All @@ -1162,6 +1193,7 @@ fn fake_compact_block_from_tx(
tx: &Transaction,
initial_sapling_tree_size: u32,
initial_orchard_tree_size: u32,
rng: impl RngCore,
) -> CompactBlock {
// Create a fake CompactTx containing the transaction.
let mut ctx = CompactTx {
Expand Down Expand Up @@ -1192,6 +1224,7 @@ fn fake_compact_block_from_tx(
prev_hash,
initial_sapling_tree_size,
initial_orchard_tree_size,
rng,
)
}

Expand All @@ -1208,8 +1241,8 @@ fn fake_compact_block_spending<P: consensus::Parameters, Fvk: TestFvk>(
value: NonNegativeAmount,
initial_sapling_tree_size: u32,
initial_orchard_tree_size: u32,
mut rng: impl RngCore + CryptoRng,
) -> CompactBlock {
let mut rng = OsRng;
let mut ctx = fake_compact_tx(&mut rng);

// Create a fake spend and a fake Note for the change
Expand Down Expand Up @@ -1291,6 +1324,7 @@ fn fake_compact_block_spending<P: consensus::Parameters, Fvk: TestFvk>(
prev_hash,
initial_sapling_tree_size,
initial_orchard_tree_size,
rng,
)
}

Expand All @@ -1300,8 +1334,8 @@ fn fake_compact_block_from_compact_tx(
prev_hash: BlockHash,
initial_sapling_tree_size: u32,
initial_orchard_tree_size: u32,
mut rng: impl RngCore,
) -> CompactBlock {
let mut rng = OsRng;
let mut cb = CompactBlock {
hash: {
let mut hash = vec![0; 32];
Expand Down Expand Up @@ -1428,7 +1462,7 @@ pub(crate) fn input_selector(
change_memo: Option<&str>,
fallback_change_pool: ShieldedProtocol,
) -> GreedyInputSelector<
WalletDb<rusqlite::Connection, Network>,
WalletDb<rusqlite::Connection, LocalNetwork>,
standard::SingleOutputChangeStrategy,
> {
let change_memo = change_memo.map(|m| MemoBytes::from(m.parse::<Memo>().unwrap()));
Expand All @@ -1440,7 +1474,7 @@ pub(crate) fn input_selector(
// Checks that a protobuf proposal serialized from the provided proposal value correctly parses to
// the same proposal value.
fn check_proposal_serialization_roundtrip(
db_data: &WalletDb<rusqlite::Connection, Network>,
db_data: &WalletDb<rusqlite::Connection, LocalNetwork>,
proposal: &Proposal<StandardFeeRule, ReceivedNoteId>,
) {
let proposal_proto = proposal::Proposal::from_standard_proposal(&db_data.params, proposal);
Expand Down
Loading

0 comments on commit 0d4a730

Please sign in to comment.