Skip to content

Commit

Permalink
Implement scanner format round-trip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Dec 6, 2023
1 parent 1be140b commit 50c31dc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions zebra-chain/src/block/height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod json_conversion;
/// There are multiple formats for serializing a height, so we don't implement
/// `ZcashSerialize` or `ZcashDeserialize` for `Height`.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Default))]
pub struct Height(pub u32);

#[derive(Error, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions zebra-state/src/service/finalized_state/disk_format/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub const TRANSACTION_LOCATION_DISK_BYTES: usize = HEIGHT_DISK_BYTES + TX_INDEX_
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(
any(test, feature = "proptest-impl"),
derive(Arbitrary, Serialize, Deserialize)
derive(Arbitrary, Default, Serialize, Deserialize)
)]
pub struct TransactionIndex(pub(super) u16);

Expand Down Expand Up @@ -126,7 +126,7 @@ impl TransactionIndex {
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(
any(test, feature = "proptest-impl"),
derive(Arbitrary, Serialize, Deserialize)
derive(Arbitrary, Default, Serialize, Deserialize)
)]
pub struct TransactionLocation {
/// The block height of the transaction.
Expand Down
9 changes: 9 additions & 0 deletions zebra-state/src/service/finalized_state/disk_format/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ use crate::{FromDisk, IntoDisk, TransactionLocation};

use super::block::TRANSACTION_LOCATION_DISK_BYTES;

#[cfg(any(test, feature = "proptest-impl"))]
use proptest_derive::Arbitrary;

#[cfg(test)]
mod tests;

/// The type used in Zebra to store Sapling scanning keys.
/// It can represent a full viewing key or an individual viewing key.
pub type SaplingScanningKey = String;
Expand All @@ -22,6 +28,7 @@ pub type SaplingScanningKey = String;
/// Currently contains a TXID in "display order", which is big-endian byte order following the u256
/// convention set by Bitcoin and zcashd.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary, Default))]
pub struct SaplingScannedResult([u8; 32]);

impl From<SaplingScannedResult> for transaction::Hash {
Expand All @@ -38,6 +45,7 @@ impl From<&[u8; 32]> for SaplingScannedResult {

/// A database column family entry for a block scanned with a Sapling vieweing key.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary, Default))]
pub struct SaplingScannedDatabaseEntry {
/// The database column family key. Must be unique for each scanning key and scanned block.
pub index: SaplingScannedDatabaseIndex,
Expand All @@ -48,6 +56,7 @@ pub struct SaplingScannedDatabaseEntry {

/// A database column family key for a block scanned with a Sapling vieweing key.
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary, Default))]
pub struct SaplingScannedDatabaseIndex {
/// The Sapling viewing key used to scan the block.
pub sapling_key: SaplingScanningKey,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Tests for scanner database serialization.
mod prop;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Randomised proptests for scanner database formats.
use proptest::{arbitrary::any, prelude::*};

use crate::{
service::finalized_state::arbitrary::assert_value_properties, SaplingScannedDatabaseIndex,
SaplingScannedResult, SaplingScanningKey,
};

#[test]
fn roundtrip_sapling_scanning_key() {
let _init_guard = zebra_test::init();

proptest!(|(val in any::<SaplingScanningKey>())| assert_value_properties(val));
}

#[test]
fn roundtrip_sapling_db_index() {
let _init_guard = zebra_test::init();

proptest!(|(val in any::<SaplingScannedDatabaseIndex>())| assert_value_properties(val));
}

#[test]
fn roundtrip_sapling_result() {
let _init_guard = zebra_test::init();

proptest!(|(val in any::<SaplingScannedResult>())| assert_value_properties(val));
}

#[test]
fn roundtrip_option_sapling_result() {
let _init_guard = zebra_test::init();

proptest!(|(val in any::<Option<SaplingScannedResult>>())| assert_value_properties(val));
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ use crate::service::finalized_state::{

// Common

// TODO: turn this into a unit test, it has a fixed value
/// This test has a fixed value, so testing it once is sufficient.
#[test]
fn roundtrip_unit_type() {
let _init_guard = zebra_test::init();

proptest!(|(val in any::<()>())| assert_value_properties(val));
// The unit type `()` is serialized to the empty (zero-length) array `[]`.
#[allow(clippy::let_unit_value)]
let value = ();
assert_value_properties(value);
}

// Block
Expand Down

0 comments on commit 50c31dc

Please sign in to comment.