Skip to content

Commit

Permalink
Merge branch 'main' into scan-todos
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 authored Dec 5, 2023
2 parents def4729 + d3dc7d0 commit e43f6c6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
6 changes: 3 additions & 3 deletions zebra-scan/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub async fn scan_height_and_store_results(
/// - Add other prior block metadata.
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 @@ -300,7 +300,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 @@ -389,6 +389,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()
}
20 changes: 8 additions & 12 deletions zebra-scan/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ use zebra_chain::{
chain_tip::ChainTip,
parameters::Network,
serialization::ZcashDeserializeInto,
transaction::Hash,
};
use zebra_state::SaplingScannedResult;

use crate::{
config::Config,
Expand Down Expand Up @@ -211,8 +211,7 @@ fn scanning_fake_blocks_store_key_and_results() -> Result<()> {
Some(0),
);

// Scan with our key
let res = zcash_client_backend::scanning::scan_block(
let result = zcash_client_backend::scanning::scan_block(
&zcash_primitives::consensus::MainNetwork,
cb.clone(),
&vks[..],
Expand All @@ -221,21 +220,18 @@ fn scanning_fake_blocks_store_key_and_results() -> Result<()> {
)
.unwrap();

// Get transaction hash
let found_transaction = res.transactions()[0].txid.as_ref();
let found_transaction_hash = Hash::from_bytes_in_display_order(found_transaction);
// The response should have one transaction relevant to the key we provided.
assert_eq!(result.transactions().len(), 1);

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 e43f6c6

Please sign in to comment.