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

zcash_history: Implement v2 history tree with Orchard support #401

Merged
merged 3 commits into from
Jun 15, 2021

Conversation

str4d
Copy link
Contributor

@str4d str4d commented Jun 11, 2021

We add support for multiple history tree versions with a marker trait, and add marker structs for the two currently-defined history tree versions.

Closes #368.

Each Zcash epoch (between two network upgrades) has a separate history
tree, making it easy to switch the node data format at network upgrades.
This commit enables the general tree logic to be shared across history
tree versions.
@str4d str4d added this to the Core Sprint 2021-22 milestone Jun 11, 2021
V2 is implemented as a wrapper around V1, so this simply expands the
tested code.
@codecov
Copy link

codecov bot commented Jun 11, 2021

Codecov Report

Merging #401 (fcbe9a8) into master (cc533a9) will decrease coverage by 0.30%.
The diff coverage is 35.51%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #401      +/-   ##
==========================================
- Coverage   62.89%   62.59%   -0.31%     
==========================================
  Files          86       87       +1     
  Lines        8407     8465      +58     
==========================================
+ Hits         5288     5299      +11     
- Misses       3119     3166      +47     
Impacted Files Coverage Δ
zcash_history/examples/lib/shared.rs 0.00% <0.00%> (ø)
zcash_history/src/lib.rs 0.00% <ø> (ø)
zcash_history/src/entry.rs 8.33% <20.00%> (ø)
zcash_history/src/version.rs 25.86% <25.86%> (ø)
zcash_history/src/node_data.rs 67.96% <36.36%> (-5.12%) ⬇️
zcash_history/src/tree.rs 63.34% <66.66%> (+0.14%) ⬆️
...h_primitives/src/transaction/components/sapling.rs 77.01% <0.00%> (+0.57%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cc533a9...fcbe9a8. Read the comment docs.

32 + // start Orchard tree root
32 + // end Orchard tree root
9; // Orchard tx count (compact uint)
// = total of 244
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Zebra we're using MAX_NODE_DATA_SIZE to create fixed-size byte arrays.

So between merging this PR and NU5 activation, we would have some unused trailing zero bytes. But the RAM and disk impact is minimal because we only store one copy of the cache. So we just need to check that we're not expecting to read or write the whole array every time.

@conradoplg does ZcashFoundation/zebra#2227 assume that we're reading or writing every node data byte?

Copy link
Contributor Author

@str4d str4d Jun 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constant is intentionally the upper bound on the node data encoding, to reflect the fact that some of the data types are compact sizes, and thus variable width. This constant is used for e.g. predictable array sizes crossing the FFI in zcashd.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ZcashFoundation/zebra#2227 just hands it over to librustzcash (zcash_history::NodeData::from_bytes) and doesn't assume anything else

Comment on lines +175 to +177
pub struct V2 {
/// The V1 node data retained in V2.
pub v1: NodeData,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also looks good for Zebra.

We can take our block_to_history_node function that produces a zcash_history::NodeData, and wrap it in a block_to_history_node_v2 function that returns zcash_history::V2:
https://github.com/ZcashFoundation/zebra/pull/2227/files#diff-ccc89b2ef027c45e027753030442d684005ebf402fcc69be39c70ee6ed193773R230

We might want to rename the legacy function to _v1, but we can do that in our Orchard-specific PR.

Copy link
Contributor

@teor2345 teor2345 Jun 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, there will likely be some duplication if we ever change or delete a field (e.g. when Sapling is eventually tuned off). But that's unlikely to happen for quite some time.

Comment on lines +16 to +20
pub struct Tree<V: Version> {
stored: HashMap<u32, Entry<V>>,

// This can grow indefinitely if `Tree` is misused as a self-contained data structure
generated: Vec<Entry>,
generated: Vec<Entry<V>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks good for Zebra, we'd just add the same type parameter to our Tree, and pass it to the inner zcash_history::Tree.

Copy link
Contributor

@daira daira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

Copy link
Contributor

@nuttycom nuttycom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

@str4d str4d merged commit fe4b63c into zcash:master Jun 15, 2021
@str4d str4d deleted the 368-zip-221-orchard branch June 15, 2021 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

zcash_history: Add Orchard support
5 participants