zcash_client_sqlite: Use a seeded RNG in `TestState`

This makes individual instances of `TestState` deterministic, which
helps with debugging.
This commit is contained in:
Jack Grigg 2024-03-09 23:47:51 +00:00
parent 390b536950
commit 75c5f31ab0
4 changed files with 21 additions and 6 deletions

1
Cargo.lock generated
View File

@ -3069,6 +3069,7 @@ dependencies = [
"pasta_curves",
"proptest",
"prost",
"rand_chacha",
"rand_core",
"regex",
"rusqlite",

View File

@ -109,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

View File

@ -83,6 +83,7 @@ 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"

View File

@ -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;
@ -102,6 +103,7 @@ pub(crate) struct TestBuilder<Cache> {
network: LocalNetwork,
cache: Cache,
test_account_birthday: Option<AccountBirthday>,
rng: ChaChaRng,
}
impl TestBuilder<()> {
@ -124,6 +126,7 @@ impl TestBuilder<()> {
},
cache: (),
test_account_birthday: None,
rng: ChaChaRng::seed_from_u64(0),
}
}
@ -133,6 +136,7 @@ impl TestBuilder<()> {
network: self.network,
cache: BlockCache::new(),
test_account_birthday: self.test_account_birthday,
rng: self.rng,
}
}
@ -143,6 +147,7 @@ impl TestBuilder<()> {
network: self.network,
cache: FsBlockCache::new(),
test_account_birthday: self.test_account_birthday,
rng: self.rng,
}
}
}
@ -176,6 +181,7 @@ impl<Cache> TestBuilder<Cache> {
_data_file: data_file,
db_data,
test_account,
rng: self.rng,
}
}
}
@ -236,6 +242,7 @@ pub(crate) struct TestState<Cache> {
UnifiedSpendingKey,
AccountBirthday,
)>,
rng: ChaChaRng,
}
impl<Cache: TestCache> TestState<Cache>
@ -305,6 +312,7 @@ where
value,
initial_sapling_tree_size,
initial_orchard_tree_size,
&mut self.rng,
);
let res = self.cache.insert(&cb);
@ -346,6 +354,7 @@ where
value,
cached_block.sapling_end_size,
cached_block.orchard_end_size,
&mut self.rng,
);
let res = self.cache.insert(&cb);
@ -397,6 +406,7 @@ where
tx,
cached_block.sapling_end_size,
cached_block.orchard_end_size,
&mut self.rng,
);
let res = self.cache.insert(&cb);
@ -1150,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(
@ -1172,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)
}
@ -1184,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 {
@ -1214,6 +1224,7 @@ fn fake_compact_block_from_tx(
prev_hash,
initial_sapling_tree_size,
initial_orchard_tree_size,
rng,
)
}
@ -1230,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
@ -1313,6 +1324,7 @@ fn fake_compact_block_spending<P: consensus::Parameters, Fvk: TestFvk>(
prev_hash,
initial_sapling_tree_size,
initial_orchard_tree_size,
rng,
)
}
@ -1322,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];