Skip to content

Commit

Permalink
Merge branch 'main' into scannin-tests-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
upbqdn committed Dec 5, 2023
2 parents a1bf2d0 + d3dc7d0 commit 4f2eee2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
14 changes: 5 additions & 9 deletions zebra-scan/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ use zcash_primitives::{
};

use zebra_chain::{
block::Block,
chain_tip::ChainTip,
diagnostic::task::WaitForPanics,
parameters::Network,
serialization::ZcashSerialize,
transaction::{self, Transaction},
block::Block, chain_tip::ChainTip, diagnostic::task::WaitForPanics, parameters::Network,
serialization::ZcashSerialize, transaction::Transaction,
};
use zebra_state::{ChainTipChange, SaplingScannedResult};

Expand Down Expand Up @@ -191,7 +187,7 @@ pub async fn start(
/// - Add prior block metadata once we have access to Zebra's state.
pub fn scan_block<K: ScanningKey>(
network: Network,
block: &Arc<Block>,
block: &Block,
sapling_tree_size: u32,
scanning_keys: &[K],
) -> Result<ScannedBlock<K::Nf>, ScanError> {
Expand Down Expand Up @@ -255,7 +251,7 @@ pub fn sapling_key_to_scan_block_keys(
}

/// Converts a zebra block and meta data into a compact block.
pub fn block_to_compact(block: &Arc<Block>, chain_metadata: ChainMetadata) -> CompactBlock {
pub fn block_to_compact(block: &Block, chain_metadata: ChainMetadata) -> CompactBlock {
CompactBlock {
height: block
.coinbase_height()
Expand Down Expand Up @@ -344,6 +340,6 @@ fn scanned_block_to_db_result<Nf>(scanned_block: ScannedBlock<Nf>) -> Vec<Saplin
scanned_block
.transactions()
.iter()
.map(|tx| transaction::Hash::from_bytes_in_display_order(tx.txid.as_ref()))
.map(|tx| SaplingScannedResult::from(tx.txid.as_ref()))
.collect()
}
33 changes: 8 additions & 25 deletions zebra-scan/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ use zebra_chain::{
primitives::{redjubjub, Groth16Proof},
sapling::{self, PerSpendAnchor, Spend, TransferData},
serialization::{AtLeastOne, ZcashDeserializeInto},
transaction::{self, LockTime, Transaction},
transaction::{LockTime, Transaction},
transparent::{CoinbaseData, Input},
work::{difficulty::CompactDifficulty, equihash::Solution},
};
use zebra_state::SaplingScannedResult;

use crate::{
config::Config,
Expand All @@ -66,13 +67,7 @@ async fn scanning_from_fake_generated_blocks() -> Result<()> {

assert_eq!(block.transactions.len(), 4);

let res = scan_block(
Network::Mainnet,
&Arc::new(block.clone()),
sapling_tree_size,
&[&dfvk],
)
.unwrap();
let res = scan_block(Network::Mainnet, &block, sapling_tree_size, &[&dfvk]).unwrap();

// The response should have one transaction relevant to the key we provided.
assert_eq!(res.transactions().len(), 1);
Expand Down Expand Up @@ -207,32 +202,20 @@ fn scanning_fake_blocks_store_key_and_results() -> Result<()> {

let (block, sapling_tree_size) = fake_block(1u32.into(), nf, &dfvk, 1, true, Some(0));

let res = scan_block(
Network::Mainnet,
&Arc::new(block),
sapling_tree_size,
&[&dfvk],
)
.unwrap();
let result = scan_block(Network::Mainnet, &block, sapling_tree_size, &[&dfvk]).unwrap();

// The response should have one transaction relevant to the key we provided.
assert_eq!(res.transactions().len(), 1);
assert_eq!(result.transactions().len(), 1);

// Get transaction hash
let found_txid = res.transactions()[0].txid.as_ref();
let found_transaction_hash = transaction::Hash::from_bytes_in_display_order(found_txid);
let result = SaplingScannedResult::from(result.transactions()[0].txid.as_ref());

// Add result to database
s.add_sapling_result(
key_to_be_stored.clone(),
Height(1),
vec![found_transaction_hash],
);
s.add_sapling_result(key_to_be_stored.clone(), Height(1), vec![result]);

// Check the result was added
assert_eq!(
s.sapling_results(&key_to_be_stored).get(&Height(1)),
Some(&vec![found_transaction_hash])
Some(&vec![result])
);

Ok(())
Expand Down
34 changes: 32 additions & 2 deletions zebra-state/src/service/finalized_state/disk_format/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,24 @@ pub const SAPLING_SCANNING_RESULT_LENGTH: usize = 32;
/// It can represent a full viewing key or an individual viewing key.
pub type SaplingScanningKey = String;

/// The type used in Zebra to store Sapling scanning results.
pub type SaplingScannedResult = transaction::Hash;
/// Stores a scanning result.
///
/// 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)]
pub struct SaplingScannedResult([u8; 32]);

impl From<SaplingScannedResult> for transaction::Hash {
fn from(scanned_result: SaplingScannedResult) -> Self {
transaction::Hash::from_bytes_in_display_order(&scanned_result.0)
}
}

impl From<&[u8; 32]> for SaplingScannedResult {
fn from(bytes: &[u8; 32]) -> Self {
Self(*bytes)
}
}

/// A database column family entry for a block scanned with a Sapling vieweing key.
#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -123,6 +139,20 @@ impl FromDisk for SaplingScannedDatabaseIndex {
}
}

impl IntoDisk for SaplingScannedResult {
type Bytes = [u8; 32];

fn as_bytes(&self) -> Self::Bytes {
self.0
}
}

impl FromDisk for SaplingScannedResult {
fn from_bytes(bytes: impl AsRef<[u8]>) -> Self {
SaplingScannedResult(bytes.as_ref().try_into().unwrap())
}
}

impl IntoDisk for Vec<SaplingScannedResult> {
type Bytes = Vec<u8>;

Expand Down

0 comments on commit 4f2eee2

Please sign in to comment.