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: Validate chain history commitments in the non-finalized state #2301

Merged
merged 38 commits into from
Jul 7, 2021

Conversation

conradoplg
Copy link
Collaborator

@conradoplg conradoplg commented Jun 14, 2021

Motivation

ZIP-221 specifies that the block header must have a commitment to the chain history. This implements that validation logic for non-finalized state.

Specifications

https://zips.z.cash/zip-0221

Designs

Following the generic design from https://zebra.zfnd.org/dev/rfcs/0005-state-updates.html

Solution

Depends on #2396, which was split off from here.

This adds a new step in the contextual validation logic that is invoked after a Chain is obtained. The Chain keeps a HistoryTree reference to keep track of the history tree and validate if incoming blocks have the correct commitment.

Review

@teor2345 reviewed the draft PR and will probably want to finish reviewing this

Reviewer Checklist

  • Code implements Specs and Designs
  • Tests for Expected Behaviour
  • Tests for Errors

Follow Up Work

Part of #2135
Proper Orchard support will be added later, see #2396.

@teor2345 teor2345 self-requested a review June 15, 2021 00:40
Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

This looks like a good start, I've suggested some ways we could handle the state and errors.

zebra-state/src/error.rs Outdated Show resolved Hide resolved
zebra-state/src/service/check.rs Outdated Show resolved Hide resolved
zebra-state/src/service/check.rs Outdated Show resolved Hide resolved
zebra-state/src/service/check.rs Outdated Show resolved Hide resolved
zebra-state/src/service/check.rs Show resolved Hide resolved
zebra-state/src/service/non_finalized_state/chain.rs Outdated Show resolved Hide resolved
zebra-state/src/service/non_finalized_state.rs Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@conradoplg conradoplg left a comment

Choose a reason for hiding this comment

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

I've expanded the implementation. Parts I'm currently blocked on:

  • How to solve the borrowing issue in validate_and_commit
  • Which specific types to use in mmr.rs (which bridges zebra-state and zcash_history). The types (blocks and block iterators) that come from zebra-state are a bit different from the types we've used in zcash_history. Should we use the same for both (which ones), or should we convert between them?

zebra-state/src/error.rs Outdated Show resolved Hide resolved
zebra-state/src/service/check.rs Outdated Show resolved Hide resolved
zebra-state/src/service/check.rs Outdated Show resolved Hide resolved
zebra-state/src/service/check.rs Outdated Show resolved Hide resolved
zebra-state/src/service/non_finalized_state.rs Outdated Show resolved Hide resolved
zebra-state/src/service/non_finalized_state/chain.rs Outdated Show resolved Hide resolved
zebra-chain/src/mmr.rs Outdated Show resolved Hide resolved
zebra-chain/src/mmr.rs Outdated Show resolved Hide resolved
zebra-chain/src/mmr.rs Outdated Show resolved Hide resolved
zebra-chain/src/mmr.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

As a general change, we might want to rename MerkleMountainRange and mmr to include the word history.

I also wonder if the mmr module belongs in zebra-chain, it seems like a primitive that the finalized and non-finalized state will want to use.

@teor2345
Copy link
Contributor

* Which specific types to use in `mmr.rs` (which bridges `zebra-state` and `zcash_history`). The types (blocks and block iterators) that come from `zebra-state` are a bit different from the types we've used in `zcash_history`. Should we use the same for both (which ones), or should we convert between them?

In general I'd like to use IntoIterator<Item = Arc<Block>>, but I'm not too concerned Item becomes &Block, because we're not actually storing any of those blocks in the Tree.

Anywhere we store blocks, Arc<Block> is a requirement to reduce memory usage.

Copy link
Collaborator Author

@conradoplg conradoplg left a comment

Choose a reason for hiding this comment

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

Another round of changes.

I'm not currently blocked and can keep on working on this, so feel free to delay any feedback if needed.

As a general change, we might want to rename MerkleMountainRange and mmr to include the word history.

I also wonder if the mmr module belongs in zebra-chain, it seems like a primitive that the finalized and non-finalized state will want to use

I've changed MerkleMountainRange to HistoryTree and mmr variables to history_tree.

Do you think mmr.rs should be moved to zebra-state instead? (Also I'll rename it to history_tree.rs too)

zebra-chain/src/mmr.rs Outdated Show resolved Hide resolved
zebra-chain/src/mmr.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service/non_finalized_state.rs Outdated Show resolved Hide resolved
zebra-state/src/service/non_finalized_state/chain.rs Outdated Show resolved Hide resolved
zebra-state/src/service/non_finalized_state/chain.rs Outdated Show resolved Hide resolved
@conradoplg
Copy link
Collaborator Author

@teor2345 could you please take another look?

When doing the proper error handling I had to propagate a lot of stuff, and I'm not sure if there is a better way to handle everything.

Maybe considering carefully in which cases librustzcash can return errors, we could .expect() most of the errors since they don't seem to be possible in the "regular" use of the tree. But it seems risky.

What's missing for now:

  • Pruning the tree
  • Final commitment computation (that's actually ZIP-244 but it seems to make sense to include it here. Also it's relatively simple)

Copy link
Collaborator Author

@conradoplg conradoplg left a comment

Choose a reason for hiding this comment

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

Some leftover fixes form the last review

zebra-state/src/service/check.rs Outdated Show resolved Hide resolved
zebra-chain/src/mmr.rs Outdated Show resolved Hide resolved
zebra-state/src/error.rs Outdated Show resolved Hide resolved
zebra-state/src/tests.rs Outdated Show resolved Hide resolved
zebra-state/src/tests.rs Outdated Show resolved Hide resolved
@conradoplg conradoplg mentioned this pull request Jul 5, 2021
3 tasks
Copy link
Contributor

@dconnolly dconnolly left a comment

Choose a reason for hiding this comment

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

Looks great! 🎉

@dconnolly dconnolly requested a review from teor2345 July 6, 2021 01:16
Base automatically changed from history-tree to main July 6, 2021 22:22
@teor2345
Copy link
Contributor

teor2345 commented Jul 6, 2021

@conradoplg this needs a rebase after merging another PR, then we can merge.

Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

Let's merge after the rebase on main is done

@teor2345 teor2345 merged commit 91b1fcb into main Jul 7, 2021
@teor2345 teor2345 deleted the zip221-non-finalized-state branch July 7, 2021 00:29
@teor2345
Copy link
Contributor

teor2345 commented Jul 7, 2021

(Coverage builds are failing due to crates.io network issues.)

teor2345 added a commit that referenced this pull request Jul 8, 2021
@teor2345 teor2345 restored the zip221-non-finalized-state branch July 8, 2021 01:24
conradoplg pushed a commit that referenced this pull request Jul 8, 2021
conradoplg added a commit that referenced this pull request Aug 4, 2021
#2301)

* sketch of implementation

* refined implementation; still incomplete

* update librustzcash, change zcash_history to work with it

* simplified code per review; renamed MMR to HistoryTree

* expand HistoryTree implementation

* handle and propagate errors

* simplify check.rs tracing

* add suggested TODO

* add HistoryTree::prune

* fix bug in pruning

* fix compilation of tests; still need to make them pass

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* improvements from code review

* improve check.rs comments and variable names

* fix HistoryTree which should use BTreeMap and not HashMap; fix non_finalized_state prop tests

* fix finalized_state proptest

* fix non_finalized_state tests by setting the correct commitments

* renamed mmr.rs to history_tree.rs

* Add HistoryTree struct

* expand non_finalized_state protest

* fix typo

* Add HistoryTree struct

* Update zebra-chain/src/primitives/zcash_history.rs

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* fix formatting

* Apply suggestions from code review

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* history_tree.rs: fixes from code review

* fixes to work with updated HistoryTree

* Improvements from code review

* Add Debug implementations to allow comparing Chains with proptest_assert_eq

Co-authored-by: teor <teor@riseup.net>
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
conradoplg added a commit that referenced this pull request Aug 6, 2021
#2301)

* sketch of implementation

* refined implementation; still incomplete

* update librustzcash, change zcash_history to work with it

* simplified code per review; renamed MMR to HistoryTree

* expand HistoryTree implementation

* handle and propagate errors

* simplify check.rs tracing

* add suggested TODO

* add HistoryTree::prune

* fix bug in pruning

* fix compilation of tests; still need to make them pass

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* improvements from code review

* improve check.rs comments and variable names

* fix HistoryTree which should use BTreeMap and not HashMap; fix non_finalized_state prop tests

* fix finalized_state proptest

* fix non_finalized_state tests by setting the correct commitments

* renamed mmr.rs to history_tree.rs

* Add HistoryTree struct

* expand non_finalized_state protest

* fix typo

* Add HistoryTree struct

* Update zebra-chain/src/primitives/zcash_history.rs

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* fix formatting

* Apply suggestions from code review

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* history_tree.rs: fixes from code review

* fixes to work with updated HistoryTree

* Improvements from code review

* Add Debug implementations to allow comparing Chains with proptest_assert_eq

Co-authored-by: teor <teor@riseup.net>
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
conradoplg added a commit that referenced this pull request Aug 9, 2021
#2301)

* sketch of implementation

* refined implementation; still incomplete

* update librustzcash, change zcash_history to work with it

* simplified code per review; renamed MMR to HistoryTree

* expand HistoryTree implementation

* handle and propagate errors

* simplify check.rs tracing

* add suggested TODO

* add HistoryTree::prune

* fix bug in pruning

* fix compilation of tests; still need to make them pass

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* improvements from code review

* improve check.rs comments and variable names

* fix HistoryTree which should use BTreeMap and not HashMap; fix non_finalized_state prop tests

* fix finalized_state proptest

* fix non_finalized_state tests by setting the correct commitments

* renamed mmr.rs to history_tree.rs

* Add HistoryTree struct

* expand non_finalized_state protest

* fix typo

* Add HistoryTree struct

* Update zebra-chain/src/primitives/zcash_history.rs

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* fix formatting

* Apply suggestions from code review

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* history_tree.rs: fixes from code review

* fixes to work with updated HistoryTree

* Improvements from code review

* Add Debug implementations to allow comparing Chains with proptest_assert_eq

Co-authored-by: teor <teor@riseup.net>
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
conradoplg added a commit that referenced this pull request Aug 9, 2021
#2301)

* sketch of implementation

* refined implementation; still incomplete

* update librustzcash, change zcash_history to work with it

* simplified code per review; renamed MMR to HistoryTree

* expand HistoryTree implementation

* handle and propagate errors

* simplify check.rs tracing

* add suggested TODO

* add HistoryTree::prune

* fix bug in pruning

* fix compilation of tests; still need to make them pass

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* improvements from code review

* improve check.rs comments and variable names

* fix HistoryTree which should use BTreeMap and not HashMap; fix non_finalized_state prop tests

* fix finalized_state proptest

* fix non_finalized_state tests by setting the correct commitments

* renamed mmr.rs to history_tree.rs

* Add HistoryTree struct

* expand non_finalized_state protest

* fix typo

* Add HistoryTree struct

* Update zebra-chain/src/primitives/zcash_history.rs

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* fix formatting

* Apply suggestions from code review

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* history_tree.rs: fixes from code review

* fixes to work with updated HistoryTree

* Improvements from code review

* Add Debug implementations to allow comparing Chains with proptest_assert_eq

Co-authored-by: teor <teor@riseup.net>
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
conradoplg added a commit that referenced this pull request Aug 9, 2021
#2301)

* sketch of implementation

* refined implementation; still incomplete

* update librustzcash, change zcash_history to work with it

* simplified code per review; renamed MMR to HistoryTree

* expand HistoryTree implementation

* handle and propagate errors

* simplify check.rs tracing

* add suggested TODO

* add HistoryTree::prune

* fix bug in pruning

* fix compilation of tests; still need to make them pass

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* improvements from code review

* improve check.rs comments and variable names

* fix HistoryTree which should use BTreeMap and not HashMap; fix non_finalized_state prop tests

* fix finalized_state proptest

* fix non_finalized_state tests by setting the correct commitments

* renamed mmr.rs to history_tree.rs

* Add HistoryTree struct

* expand non_finalized_state protest

* fix typo

* Add HistoryTree struct

* Update zebra-chain/src/primitives/zcash_history.rs

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* fix formatting

* Apply suggestions from code review

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* history_tree.rs: fixes from code review

* fixes to work with updated HistoryTree

* Improvements from code review

* Add Debug implementations to allow comparing Chains with proptest_assert_eq

Co-authored-by: teor <teor@riseup.net>
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
conradoplg added a commit that referenced this pull request Aug 11, 2021
* Refactor HistoryTree into NonEmptyHistoryTree and HistoryTree

* HistoryTree: use Deref instead of AsRef; remove unneeded PartialEq

* ZIP-221: Validate chain history commitments in the non-finalized state (#2301)

* sketch of implementation

* refined implementation; still incomplete

* update librustzcash, change zcash_history to work with it

* simplified code per review; renamed MMR to HistoryTree

* expand HistoryTree implementation

* handle and propagate errors

* simplify check.rs tracing

* add suggested TODO

* add HistoryTree::prune

* fix bug in pruning

* fix compilation of tests; still need to make them pass

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* improvements from code review

* improve check.rs comments and variable names

* fix HistoryTree which should use BTreeMap and not HashMap; fix non_finalized_state prop tests

* fix finalized_state proptest

* fix non_finalized_state tests by setting the correct commitments

* renamed mmr.rs to history_tree.rs

* Add HistoryTree struct

* expand non_finalized_state protest

* fix typo

* Add HistoryTree struct

* Update zebra-chain/src/primitives/zcash_history.rs

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* fix formatting

* Apply suggestions from code review

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* history_tree.rs: fixes from code review

* fixes to work with updated HistoryTree

* Improvements from code review

* Add Debug implementations to allow comparing Chains with proptest_assert_eq

Co-authored-by: teor <teor@riseup.net>
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* Improvements from code review

* Restore blocks returned by PreparedChain since other tests broken; adjust tests with history trees

Co-authored-by: teor <teor@riseup.net>
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rust Area: Updates to Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants