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

fix(state): Use correct end heights for end of block subtrees during the full sync #7566

Merged
merged 56 commits into from
Sep 19, 2023

Conversation

teor2345
Copy link
Contributor

@teor2345 teor2345 commented Sep 15, 2023

Reviewer Questions

Does this PR have any data bugs?
Are there any other blocking issues? (For example, security bugs.)

Since this PR is on the critical path, I'd like other suggestions to go in other PRs or tickets.

Motivation

When a subtree is created during the full sync, and the final note commitment is at the end of a block, the end height is one higher than it should be.

Close #7532

Specifications

zcashd uses the height of the block the subtree ends in:
zcash/zcash#6694

Complex Code or Requirements

Because this bug fix is complex, I added some extra tests, and bumped the state version so the whole database will be upgraded again. I also cleared any data from the previous upgrade. This will make sure all our tests run on the new data.

Solution

Refactor the subtree upgrade so it uses block validation code that has already been tested
Bump the state version and delete previous data, so the tests run on the data from this upgrade only

Use correct heights for subtrees completed at the end of a block. Previously, these subtrees had a height that was 1 higher than the block their final note was in. This bug only happened for subtrees created by new blocks. (And not for upgraded blocks.)

This bug would have been caught by ticket #7446, but only if we found a test vector for "Completed Subtree at the end of the block", and "subtrees added with new tip blocks", and ran it on a full sync Zebra state.

I refactored the upgrade and tip block subtrees to use the same code, which is how I found this bug. So a similar bug shouldn't happen again.

Related Changes

Wait for the state upgrade in tests:

  • before subtree gRPC tests
  • before writing updated cached states (full syncs are ok, so are tests that don't write states)

Refactor the subtree upgrade so the upgrade can be done in reverse order, but don't reverse the order yet.

Fix some clippy lints.

Testing

Add extra state validity tests, including a quick check before the upgrade runs for 20 minutes. The quick check runs on every Zebra launch, even in production.

Review

This is a routine change, but it is on the critical path for getting the RPCs finished and tested. It is also blocking all the rest of the test tickets.

I'd like non-blocking suggestions to go in other PRs or tickets.

Reviewer Checklist

  • Will the PR name make sense to users?
    • Does it need extra CHANGELOG info? (new features, breaking changes, large changes)
  • Are the PR labels correct?
  • Does the code do what the ticket and PR says?
    • Does it change concurrent code, unsafe code, or consensus rules?
  • How do you know it works? Does it have tests?

teor2345 and others added 30 commits September 12, 2023 09:58
Merge branch 'main' into upgrade-subtrees-backwards
@teor2345
Copy link
Contributor Author

Questions for reviewers

Does this PR have any data bugs?
Are there any other blocking issues? (For example, security bugs.)

Since this PR is on the critical path, I'd like other suggestions to go in other PRs or tickets.

@upbqdn
Copy link
Member

upbqdn commented Sep 18, 2023

I just reviewed this PR. Feel free to close all comments labeled as optional right away without addressing them.

@teor2345
Copy link
Contributor Author

teor2345 commented Sep 19, 2023

My first fix in 5a1deb1 was wrong, but I think I fixed it in 41eef09.

There was also a bug in the quick check which skipped the same subtrees, which is why it didn't detect this issue on my local machine.

Edit: ok let's try again with 54f2bba. Good news is that the quick check detected that bug.

@teor2345 teor2345 requested a review from upbqdn September 19, 2023 02:30
Copy link
Contributor

@oxarbitrage oxarbitrage left a comment

Choose a reason for hiding this comment

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

approving without reviewing so it can be merged fast, @upbqdn approved previously.

mergify bot added a commit that referenced this pull request Sep 19, 2023
@mergify mergify bot merged commit 7a7d79d into main Sep 19, 2023
@mergify mergify bot deleted the fix-flsync-subt branch September 19, 2023 14:49
arya2 pushed a commit that referenced this pull request Sep 29, 2023
…the full sync (#7566)

* Avoid manual handling of previous sapling trees by using iterator windows instead

* Avoid manual sapling subtree index handling by comparing prev and current subtree indexes instead

* Simplify adding notes by using the exact number of remaining notes

* Simplify by skipping the first block, because it can't complete a subtree

* Re-use existing tree update code

* Apply the sapling changes to orchard subtree updates

* add a reverse database column family iterator function

* Make skipping the lowest tree independent of iteration order

* Move new subtree checks into the iterator, rename to end_height

* Split subtree calculation into a new method

* Split the calculate and write methods

* Quickly check the first subtree before running the full upgrade

* Do the quick checks every time Zebra runs, and refactor slow check error handling

* Do quick checks for orchard as well

* Make orchard tree upgrade match sapling upgrade code

* Upgrade subtrees in reverse height order

* Bump the database patch version so the upgrade runs again

* Reset previous subtree upgrade data before doing this one

* Add extra checks to subtree calculation to diagnose errors

* Use correct heights for subtrees completed at the end of a block

* Add even more checks to diagnose issues

* Instrument upgrade methods to improve diagnostics

* Prevent modification of re-used trees

* Debug with subtree positions as well

* Fix an off-by-one error with completed subtrees

* Fix typos and confusing comments

Co-authored-by: Marek <[email protected]>

* Fix mistaken previous tree handling and end tree comments

* Remove unnecessary subtraction in remaining leaves calc

* Log heights when assertions fail

* Fix new subtree detection filter

* Move new subtree check into a method, cleanup unused code

* Remove redundant assertions

* Wait for subtree upgrade before testing RPCs

* Fix subtree search in quick check

* Temporarily upgrade subtrees in forward height order

* Clarify some comments

* Fix missing test imports

* Fix subtree logging

* Add a comment about a potential hang with future upgrades

* Fix zebrad var ownership

* Log more info when add_subtrees.rs fails

* cargo fmt --all

* Fix unrelated clippy::unnecessary_unwrap

* cargo clippy --fix --all-features --all-targets; cargo fmt --all

* Stop the quick check depending on tree de-duplication

* Refactor waiting for the upgrade into functions

* Wait for state upgrades whenever the cached state is updated

* Wait for the testnet upgrade in the right place

* Fix unused variable

* Fix a subtree detection bug and comments

* Remove an early reference to reverse direction

* Stop skipping subtrees completed at the end of blocks

* Actually fix new subtree code

---------

Co-authored-by: Marek <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-consensus Area: Consensus rule updates A-state Area: State / database changes C-bug Category: This is a bug I-invalid-data Zebra relies on invalid or untrusted data, or sends invalid data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: some subtrees have incorrect end heights after a full sync
3 participants