Skip to content

Commit

Permalink
Add snapshot tests for sprout note commitment trees
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jun 23, 2023
1 parent 80407d8 commit 1c5ffc0
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 3 deletions.
2 changes: 1 addition & 1 deletion zebra-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ once_cell = "1.18.0"
spandoc = "0.2.2"

hex = { version = "0.4.3", features = ["serde"] }
insta = { version = "1.30.0", features = ["ron"] }
insta = { version = "1.30.0", features = ["ron", "redactions"] }

proptest = "1.2.0"
proptest-derive = "0.3.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ fn snapshot_block_and_transaction_data(state: &FinalizedState) {
if let Some((max_height, tip_block_hash)) = tip {
// Check that the database returns empty note commitment trees for the
// genesis block.
//
// We only store the sprout tree for the tip by height, so we can't check sprout here.
let sapling_tree = state
.sapling_note_commitment_tree_by_height(&block::Height::MIN)
.expect("the genesis block in the database has a Sapling tree");
Expand All @@ -241,9 +243,11 @@ fn snapshot_block_and_transaction_data(state: &FinalizedState) {

// Shielded

let stored_sprout_trees = state.sprout_note_commitments_full_map();
let mut stored_sapling_trees = Vec::new();
let mut stored_orchard_trees = Vec::new();

let sprout_tree_at_tip = state.sprout_note_commitment_tree();
let sapling_tree_at_tip = state.sapling_note_commitment_tree();
let orchard_tree_at_tip = state.orchard_note_commitment_tree();

Expand All @@ -268,9 +272,11 @@ fn snapshot_block_and_transaction_data(state: &FinalizedState) {
.block(query_height.into())
.expect("heights up to tip have blocks");

// Check the sapling and orchard note commitment trees.
// Check the shielded note commitment trees.
//
// We only store the sprout tree for the tip by height, so we can't check sprout here.
//
// TODO: test the rest of the shielded data (anchors, nullifiers, sprout)
// TODO: test the rest of the shielded data (anchors, nullifiers)
let sapling_tree_by_height = state
.sapling_note_commitment_tree_by_height(&query_height)
.expect("heights up to tip have Sapling trees");
Expand All @@ -297,6 +303,18 @@ fn snapshot_block_and_transaction_data(state: &FinalizedState) {
if query_height == max_height {
assert_eq!(stored_block_hash, tip_block_hash);

// We only store the sprout tree for the tip by height,
// so the sprout check is less strict.
// We enforce the tip tree order by snapshotting it as well.
if let Some(stored_tree) = stored_sprout_trees.get(&sprout_tree_at_tip.root()) {
assert_eq!(
&sprout_tree_at_tip, stored_tree,
"unexpected missing sprout tip tree:\n\
all trees: {stored_sprout_trees:?}"
);
} else {
assert_eq!(sprout_tree_at_tip, Default::default());
}
assert_eq!(sapling_tree_at_tip, sapling_tree_by_height);
assert_eq!(orchard_tree_at_tip, orchard_tree_by_height);

Expand Down Expand Up @@ -427,6 +445,14 @@ fn snapshot_block_and_transaction_data(state: &FinalizedState) {
// These snapshots will change if the trees do not have cached roots.
// But we expect them to always have cached roots,
// because those roots are used to populate the anchor column families.
insta::assert_ron_snapshot!("sprout_tree_at_tip", sprout_tree_at_tip);
insta::assert_ron_snapshot!(
"sprout_trees",
stored_sprout_trees,
{
"." => insta::sorted_redaction()
}
);
insta::assert_ron_snapshot!("sapling_trees", stored_sapling_trees);
insta::assert_ron_snapshot!("orchard_trees", stored_orchard_trees);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: sprout_tree_at_tip
---
NoteCommitmentTree(
inner: Frontier(
frontier: None,
),
cached_root: Some(Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89))),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: sprout_tree_at_tip
---
NoteCommitmentTree(
inner: Frontier(
frontier: None,
),
cached_root: Some(Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89))),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: sprout_tree_at_tip
---
NoteCommitmentTree(
inner: Frontier(
frontier: None,
),
cached_root: Some(Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89))),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: sprout_tree_at_tip
---
NoteCommitmentTree(
inner: Frontier(
frontier: None,
),
cached_root: Some(Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89))),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: sprout_tree_at_tip
---
NoteCommitmentTree(
inner: Frontier(
frontier: None,
),
cached_root: Some(Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89))),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: sprout_tree_at_tip
---
NoteCommitmentTree(
inner: Frontier(
frontier: None,
),
cached_root: Some(Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89))),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: stored_sprout_trees
---
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: stored_sprout_trees
---
{
Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89)): NoteCommitmentTree(
inner: Frontier(
frontier: None,
),
cached_root: Some(Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89))),
),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: stored_sprout_trees
---
{
Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89)): NoteCommitmentTree(
inner: Frontier(
frontier: None,
),
cached_root: Some(Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89))),
),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: stored_sprout_trees
---
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: stored_sprout_trees
---
{
Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89)): NoteCommitmentTree(
inner: Frontier(
frontier: None,
),
cached_root: Some(Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89))),
),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: zebra-state/src/service/finalized_state/zebra_db/block/tests/snapshot.rs
expression: stored_sprout_trees
---
{
Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89)): NoteCommitmentTree(
inner: Frontier(
frontier: None,
),
cached_root: Some(Root((215, 198, 18, 200, 23, 121, 49, 145, 161, 230, 134, 82, 18, 24, 118, 214, 179, 189, 228, 15, 79, 165, 43, 195, 20, 20, 92, 230, 229, 205, 210, 89))),
),
}

0 comments on commit 1c5ffc0

Please sign in to comment.