Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poc(scanner): add a populated state test for ZECpages viewing key #7916

Merged
merged 42 commits into from
Nov 8, 2023
Merged
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
743fcce
get started with the blockchain scanner poc
oxarbitrage Oct 17, 2023
65f5369
Merge remote-tracking branch 'origin/main' into issue7727
oxarbitrage Oct 17, 2023
d6600a9
rustfmt
oxarbitrage Oct 17, 2023
cb1a76a
fix the tests
oxarbitrage Oct 17, 2023
c0f7dbd
Reads blocks from db
arya2 Oct 17, 2023
c5c6c80
Adds conversion functions
arya2 Oct 18, 2023
de32a43
scans blocks and counts transactions
arya2 Oct 18, 2023
0d0117d
fix bug
arya2 Oct 18, 2023
cee1db6
split into 2 tests
oxarbitrage Oct 19, 2023
83e0542
add duplicated dependencies to deny.toml
oxarbitrage Oct 19, 2023
6913525
upgrade zebra-scanner version
oxarbitrage Oct 20, 2023
b384bf0
Merge remote-tracking branch 'origin/main' into issue7727
oxarbitrage Oct 20, 2023
8f1e6e1
try removing ecc duplicated dependencies
oxarbitrage Oct 20, 2023
b88e932
try fix deny.toml
oxarbitrage Oct 20, 2023
598cda6
remove unintended paste from deny.toml
oxarbitrage Oct 20, 2023
19dfaa7
remove duplicated code from the other test
oxarbitrage Oct 24, 2023
96bd5dd
Merge remote-tracking branch 'origin/main' into issue7727
oxarbitrage Oct 24, 2023
2ca1c93
remove strict version of `zcash_primitives` crate
oxarbitrage Oct 25, 2023
273fd95
change description
oxarbitrage Oct 25, 2023
b296769
remove feture
oxarbitrage Oct 25, 2023
51affba
remove tokio features
oxarbitrage Oct 25, 2023
5621836
change lib doc
oxarbitrage Oct 25, 2023
b455a37
add more documentation
oxarbitrage Oct 25, 2023
475a771
change expect
oxarbitrage Oct 25, 2023
3909795
do not use default in compact block creation
oxarbitrage Oct 25, 2023
25e205b
more docs
oxarbitrage Oct 25, 2023
550681e
add more checks to test
oxarbitrage Oct 25, 2023
6c83618
remove zebra-consensus dependency
oxarbitrage Oct 26, 2023
ad19e00
move all deps to dev-deps
oxarbitrage Oct 26, 2023
d10940e
change crate version
oxarbitrage Oct 26, 2023
6f8fe4c
Merge remote-tracking branch 'origin/main' into issue7727
oxarbitrage Oct 26, 2023
d8bd9ae
rename crate to zebra-scan
oxarbitrage Nov 1, 2023
b9d05c8
Merge remote-tracking branch 'origin/main' into issue7727
oxarbitrage Nov 1, 2023
9b08dea
lock file
oxarbitrage Nov 1, 2023
073495e
add test for zecpages populated state
oxarbitrage Nov 6, 2023
52958eb
scans all cached blocks for zecpages viewing key expecting Ok results.
arya2 Nov 7, 2023
133e461
use test blocks
arya2 Nov 7, 2023
46a5e36
fixes test
arya2 Nov 7, 2023
48a1d3a
fix expect messages
oxarbitrage Nov 8, 2023
d8af410
Merge branch 'main' into issue7903
teor2345 Nov 8, 2023
7d6ae23
Discard changes to Cargo.lock
teor2345 Nov 8, 2023
0fc1c1f
Discard changes to deny.toml
teor2345 Nov 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 116 additions & 1 deletion zebra-scan/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use std::sync::Arc;

use zcash_client_backend::{
data_api::BlockMetadata,
encoding::decode_extended_full_viewing_key,
proto::compact_formats::{
self as compact, ChainMetadata, CompactBlock, CompactSaplingOutput, CompactSaplingSpend,
CompactTx,
Expand All @@ -16,7 +18,7 @@ use zcash_note_encryption::Domain;
use zcash_primitives::{
block::BlockHash,
consensus::{BlockHeight, Network},
constants::SPENDING_KEY_GENERATOR,
constants::{mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, SPENDING_KEY_GENERATOR},
memo::MemoBytes,
sapling::{
note_encryption::{sapling_note_encryption, SaplingDomain},
Expand Down Expand Up @@ -168,6 +170,119 @@ async fn scanning_from_fake_generated_blocks() -> Result<()> {
Ok(())
}

/// Scan a populated state for the ZECpages viewing key.
/// This test is very similar to `scanning_from_populated_zebra_state` but with the ZECpages key.
/// There are no zechub transactions in the test data so we should get empty related transactions.
#[tokio::test]
async fn scanning_zecpages_from_populated_zebra_state() -> Result<()> {
/// The extended Sapling viewing key of [ZECpages](https://zecpages.com/boardinfo)
const ZECPAGES_VIEWING_KEY: &str = "zxviews1q0duytgcqqqqpqre26wkl45gvwwwd706xw608hucmvfalr759ejwf7qshjf5r9aa7323zulvz6plhttp5mltqcgs9t039cx2d09mgq05ts63n8u35hyv6h9nc9ctqqtue2u7cer2mqegunuulq2luhq3ywjcz35yyljewa4mgkgjzyfwh6fr6jd0dzd44ghk0nxdv2hnv4j5nxfwv24rwdmgllhe0p8568sgqt9ckt02v2kxf5ahtql6s0ltjpkckw8gtymxtxuu9gcr0swvz";

// Parse the key from ZECpages
let efvk = decode_extended_full_viewing_key(
HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
ZECPAGES_VIEWING_KEY,
)
.unwrap();

let account = AccountId::from(1);

// Build a vector of viewing keys `vks` to scan for.
let fvk = efvk.fvk;
let ivk = fvk.vk.ivk();
let vks: Vec<(&AccountId, &SaplingIvk)> = vec![(&account, &ivk)];

let network = zebra_chain::parameters::Network::Mainnet;

// Create a continuous chain of mainnet blocks from genesis
let blocks: Vec<Arc<Block>> = zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS
.iter()
.map(|(_height, block_bytes)| block_bytes.zcash_deserialize_into().unwrap())
.collect();

// Create a populated state service.
let (_state_service, read_only_state_service, latest_chain_tip, _chain_tip_change) =
zebra_state::populated_state(blocks.clone(), network).await;
arya2 marked this conversation as resolved.
Show resolved Hide resolved

let db = read_only_state_service.db();

// use the tip as starting height
let mut height = latest_chain_tip.best_tip_height().unwrap();

let mut transactions_found = 0;
let mut transactions_scanned = 0;
let mut blocks_scanned = 0;
while let Some(block) = db.block(height.into()) {
// zcash_client_backend doesn't support scanning the genesis block, but that's ok, because
// Sapling activates at height 419,200. So we'll never scan these blocks in production code.
let sapling_tree_size = if height.is_min() {
1
} else {
db.sapling_tree_by_hash_or_height(height.into())
.expect("each state block must have a sapling tree")
.count()
};

let orchard_tree_size = db
.orchard_tree_by_hash_or_height(height.into())
.expect("each state block must have a orchard tree")
.count();

let chain_metadata = ChainMetadata {
sapling_commitment_tree_size: sapling_tree_size
.try_into()
.expect("sapling position is limited to u32::MAX"),
orchard_commitment_tree_size: orchard_tree_size
.try_into()
.expect("orchard position is limited to u32::MAX"),
};

let block_metadata = if height.is_min() {
None
} else {
Some(BlockMetadata::from_parts(
height.previous()?.0.into(),
BlockHash(block.header.previous_block_hash.0),
db.sapling_tree_by_hash_or_height(block.header.previous_block_hash.into())
.expect("each state block must have a sapling tree")
.count()
.try_into()
.expect("sapling position is limited to u32::MAX"),
))
};

arya2 marked this conversation as resolved.
Show resolved Hide resolved
let compact_block = block_to_compact(block, chain_metadata);

let res = scan_block(
&zcash_primitives::consensus::MainNetwork,
compact_block.clone(),
&vks[..],
&[],
block_metadata.as_ref(),
)
.expect("scanning block for the ZECpages viewing key should work");

transactions_found += res.transactions().len();
transactions_scanned += compact_block.vtx.len();
blocks_scanned += 1;

// scan backwards
if height.is_min() {
break;
}
height = height.previous()?;
}

// make sure all blocks and transactions were scanned
assert_eq!(blocks_scanned, 11);
assert_eq!(transactions_scanned, 11);

// no relevant transactions should be found
assert_eq!(transactions_found, 0);
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

/// Convert a zebra block and meta data into a compact block.
fn block_to_compact(block: Arc<Block>, chain_metadata: ChainMetadata) -> CompactBlock {
CompactBlock {
Expand Down
Loading