Skip to content

Commit

Permalink
refactor(state): move finalized state test methods to a test module
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Feb 18, 2022
1 parent f2b4184 commit 6493d05
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 47 deletions.
45 changes: 0 additions & 45 deletions zebra-state/src/service/finalized_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,51 +721,6 @@ impl FinalizedState {
.zs_get(value_pool_cf, &())
.unwrap_or_else(ValueBalance::zero)
}

/// Allow to set up a fake value pool in the database for testing purposes.
#[cfg(any(test, feature = "proptest-impl"))]
#[allow(dead_code)]
pub fn set_current_value_pool(&self, fake_value_pool: ValueBalance<NonNegative>) {
let mut batch = DiskWriteBatch::new();
let value_pool_cf = self.db.cf_handle("tip_chain_value_pool").unwrap();

batch.zs_insert(value_pool_cf, (), fake_value_pool);
self.db.write(batch).unwrap();
}

/// Artificially prime the note commitment tree anchor sets with anchors
/// referenced in a block, for testing purposes _only_.
#[cfg(test)]
pub fn populate_with_anchors(&self, block: &Block) {
let mut batch = DiskWriteBatch::new();

let sprout_anchors = self.db.cf_handle("sprout_anchors").unwrap();
let sapling_anchors = self.db.cf_handle("sapling_anchors").unwrap();
let orchard_anchors = self.db.cf_handle("orchard_anchors").unwrap();

for transaction in block.transactions.iter() {
// Sprout
for joinsplit in transaction.sprout_groth16_joinsplits() {
batch.zs_insert(
sprout_anchors,
joinsplit.anchor,
sprout::tree::NoteCommitmentTree::default(),
);
}

// Sapling
for anchor in transaction.sapling_anchors() {
batch.zs_insert(sapling_anchors, anchor, ());
}

// Orchard
if let Some(orchard_shielded_data) = transaction.orchard_shielded_data() {
batch.zs_insert(orchard_anchors, orchard_shielded_data.shared_anchor, ());
}
}

self.db.write(batch).unwrap();
}
}

fn block_precommit_metrics(block: &Block, hash: block::Hash, height: block::Height) {
Expand Down
57 changes: 55 additions & 2 deletions zebra-state/src/service/finalized_state/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ use std::sync::Arc;

use proptest::prelude::*;

use zebra_chain::block;
use zebra_chain::{
amount::NonNegative,
block::{self, Block},
sprout,
value_balance::ValueBalance,
};

use crate::service::finalized_state::disk_format::{FromDisk, IntoDisk, TransactionLocation};
use crate::service::finalized_state::{
disk_db::{DiskWriteBatch, WriteDisk},
disk_format::{FromDisk, IntoDisk, TransactionLocation},
FinalizedState,
};

impl Arbitrary for TransactionLocation {
type Parameters = ();
Expand Down Expand Up @@ -84,3 +93,47 @@ where
assert_round_trip_arc(Arc::new(input.clone()));
assert_round_trip(input);
}

impl FinalizedState {
/// Allow to set up a fake value pool in the database for testing purposes.
pub fn set_current_value_pool(&self, fake_value_pool: ValueBalance<NonNegative>) {
let mut batch = DiskWriteBatch::new();
let value_pool_cf = self.db.cf_handle("tip_chain_value_pool").unwrap();

batch.zs_insert(value_pool_cf, (), fake_value_pool);
self.db.write(batch).unwrap();
}

/// Artificially prime the note commitment tree anchor sets with anchors
/// referenced in a block, for testing purposes _only_.
pub fn populate_with_anchors(&self, block: &Block) {
let mut batch = DiskWriteBatch::new();

let sprout_anchors = self.db.cf_handle("sprout_anchors").unwrap();
let sapling_anchors = self.db.cf_handle("sapling_anchors").unwrap();
let orchard_anchors = self.db.cf_handle("orchard_anchors").unwrap();

for transaction in block.transactions.iter() {
// Sprout
for joinsplit in transaction.sprout_groth16_joinsplits() {
batch.zs_insert(
sprout_anchors,
joinsplit.anchor,
sprout::tree::NoteCommitmentTree::default(),
);
}

// Sapling
for anchor in transaction.sapling_anchors() {
batch.zs_insert(sapling_anchors, anchor, ());
}

// Orchard
if let Some(orchard_shielded_data) = transaction.orchard_shielded_data() {
batch.zs_insert(orchard_anchors, orchard_shielded_data.shared_anchor, ());
}
}

self.db.write(batch).unwrap();
}
}

0 comments on commit 6493d05

Please sign in to comment.