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

ZIP-221: integrate MMR tree from librustcash (without Orchard) #2227

Merged
merged 13 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
91 changes: 2 additions & 89 deletions zebra-chain/src/primitives/zcash_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// TODO: remove after this module gets to be used
#![allow(dead_code)]

mod tests;

use std::{collections::HashMap, convert::TryInto, io, sync::Arc};

use crate::{
Expand Down Expand Up @@ -282,92 +284,3 @@ fn count_sapling_transactions(block: Arc<Block>) -> u64 {
.try_into()
.expect("number of transactions must fit u64")
}

#[cfg(test)]
mod test {
use crate::{
block::Commitment::{self, ChainHistoryActivationReserved},
serialization::ZcashDeserializeInto,
};

use super::*;
use color_eyre::eyre;
use eyre::Result;
use zebra_test::vectors::{
MAINNET_BLOCKS, MAINNET_FINAL_SAPLING_ROOTS, TESTNET_BLOCKS, TESTNET_FINAL_SAPLING_ROOTS,
};

/// Test the MMR tree using the activation block of a network upgrade
/// and its next block.
#[test]
fn tree() -> Result<()> {
tree_for_network_upgrade(Network::Mainnet, NetworkUpgrade::Heartwood)?;
tree_for_network_upgrade(Network::Testnet, NetworkUpgrade::Heartwood)?;
tree_for_network_upgrade(Network::Mainnet, NetworkUpgrade::Canopy)?;
tree_for_network_upgrade(Network::Testnet, NetworkUpgrade::Canopy)?;
Ok(())
}

fn tree_for_network_upgrade(network: Network, network_upgrade: NetworkUpgrade) -> Result<()> {
let (blocks, sapling_roots) = match network {
Network::Mainnet => (&*MAINNET_BLOCKS, &*MAINNET_FINAL_SAPLING_ROOTS),
Network::Testnet => (&*TESTNET_BLOCKS, &*TESTNET_FINAL_SAPLING_ROOTS),
};
let height = network_upgrade.activation_height(network).unwrap().0;

// Load Block 0 (activation block of the given network upgrade)
let block0 = Arc::new(
blocks
.get(&height)
.expect("test vector exists")
.zcash_deserialize_into::<Block>()
.expect("block is structurally valid"),
);

// Check its commitment
let commitment0 = block0.commitment(network)?;
if network_upgrade == NetworkUpgrade::Heartwood {
// Heartwood is the only upgrade that has a reserved value.
// (For other upgrades we could compare with the expected commitment,
// but we haven't calculated them.)
assert_eq!(commitment0, ChainHistoryActivationReserved);
}

// Build initial MMR tree with only Block 0
let sapling_root0 =
sapling::tree::Root(**sapling_roots.get(&height).expect("test vector exists"));
let mut tree = Tree::new_from_block(network, block0, &sapling_root0)?;

// Compute root hash of the MMR tree, which will be included in the next block
let hash0 = tree.hash();

// Load Block 1 (activation + 1)
let block1 = Arc::new(
blocks
.get(&(height + 1))
.expect("test vector exists")
.zcash_deserialize_into::<Block>()
.expect("block is structurally valid"),
);

// Check its commitment
let commitment1 = block1.commitment(network)?;
assert_eq!(commitment1, Commitment::ChainHistoryRoot(hash0));

// Append Block to MMR tree
let sapling_root1 = sapling::tree::Root(
**sapling_roots
.get(&(height + 1))
.expect("test vector exists"),
);
let append = tree.append_leaf(block1, &sapling_root1).unwrap();

// Tree how has 3 nodes: two leafs for each block, and one parent node
// which is the new root
assert_eq!(tree.inner.len(), 3);
// Two nodes were appended: the new leaf and the parent node
assert_eq!(append.len(), 2);

Ok(())
}
}
4 changes: 4 additions & 0 deletions zebra-chain/src/primitives/zcash_history/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! Tests for Zebra history trees

#[cfg(test)]
mod vectors;
85 changes: 85 additions & 0 deletions zebra-chain/src/primitives/zcash_history/tests/vectors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::{
block::Commitment::{self, ChainHistoryActivationReserved},
serialization::ZcashDeserializeInto,
};

use crate::primitives::zcash_history::*;
use color_eyre::eyre;
use eyre::Result;
use zebra_test::vectors::{
MAINNET_BLOCKS, MAINNET_FINAL_SAPLING_ROOTS, TESTNET_BLOCKS, TESTNET_FINAL_SAPLING_ROOTS,
};

/// Test the MMR tree using the activation block of a network upgrade
/// and its next block.
#[test]
fn tree() -> Result<()> {
tree_for_network_upgrade(Network::Mainnet, NetworkUpgrade::Heartwood)?;
tree_for_network_upgrade(Network::Testnet, NetworkUpgrade::Heartwood)?;
tree_for_network_upgrade(Network::Mainnet, NetworkUpgrade::Canopy)?;
tree_for_network_upgrade(Network::Testnet, NetworkUpgrade::Canopy)?;
Ok(())
}

fn tree_for_network_upgrade(network: Network, network_upgrade: NetworkUpgrade) -> Result<()> {
let (blocks, sapling_roots) = match network {
Network::Mainnet => (&*MAINNET_BLOCKS, &*MAINNET_FINAL_SAPLING_ROOTS),
Network::Testnet => (&*TESTNET_BLOCKS, &*TESTNET_FINAL_SAPLING_ROOTS),
};
let height = network_upgrade.activation_height(network).unwrap().0;

// Load Block 0 (activation block of the given network upgrade)
let block0 = Arc::new(
blocks
.get(&height)
.expect("test vector exists")
.zcash_deserialize_into::<Block>()
.expect("block is structurally valid"),
);

// Check its commitment
let commitment0 = block0.commitment(network)?;
if network_upgrade == NetworkUpgrade::Heartwood {
// Heartwood is the only upgrade that has a reserved value.
// (For other upgrades we could compare with the expected commitment,
// but we haven't calculated them.)
assert_eq!(commitment0, ChainHistoryActivationReserved);
}

// Build initial MMR tree with only Block 0
let sapling_root0 =
sapling::tree::Root(**sapling_roots.get(&height).expect("test vector exists"));
let mut tree = Tree::new_from_block(network, block0, &sapling_root0)?;

// Compute root hash of the MMR tree, which will be included in the next block
let hash0 = tree.hash();

// Load Block 1 (activation + 1)
let block1 = Arc::new(
blocks
.get(&(height + 1))
.expect("test vector exists")
.zcash_deserialize_into::<Block>()
.expect("block is structurally valid"),
);

// Check its commitment
let commitment1 = block1.commitment(network)?;
assert_eq!(commitment1, Commitment::ChainHistoryRoot(hash0));

// Append Block to MMR tree
let sapling_root1 = sapling::tree::Root(
**sapling_roots
.get(&(height + 1))
.expect("test vector exists"),
);
let append = tree.append_leaf(block1, &sapling_root1).unwrap();

// Tree how has 3 nodes: two leafs for each block, and one parent node
// which is the new root
assert_eq!(tree.inner.len(), 3);
// Two nodes were appended: the new leaf and the parent node
assert_eq!(append.len(), 2);

Ok(())
}