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

change(db): Upgrade subtrees from the tip backwards, for compatibility with wallet syncing #7531

Merged
merged 59 commits into from
Sep 20, 2023

Conversation

teor2345
Copy link
Contributor

@teor2345 teor2345 commented Sep 12, 2023

Motivation

We want wallets to be able to sync correctly while Zebra is upgrading the state to add subtrees.

Close #7407

Specifications

Zebra currently adds new subtrees if the tip block completes them. This is the correct behaviour, even if an upgrade is in progress.

For subtrees added during state upgrades, Zebra upgrades the indexes live in reverse height order.

API Reference

Iterator prev/current:
https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.tuple_windows

Database iterators:
https://docs.rs/rocksdb/latest/rocksdb/struct.DBCommon.html#method.iterator_cf
https://docs.rs/rocksdb/latest/rocksdb/enum.IteratorMode.html

Complex Code or Requirements

The new database code needs to get the blocks in the correct order. Please review this carefully. The order can be checked manually in the logs, or using the subtree RPC during the upgrade.

Because these code changes are 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

Do subtree upgrades in reverse height order
Bump the state version and delete previous data, so the tests run on the data from this upgrade only

Review

This is a routine change, but it is on the critical path for getting the RPCs finished and tested.

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 teor2345 added C-bug Category: This is a bug P-Medium ⚡ I-invalid-data Zebra relies on invalid or untrusted data, or sends invalid data A-state Area: State / database changes A-compatibility Area: Compatibility with other nodes or wallets, or standard rules labels Sep 12, 2023
@teor2345 teor2345 self-assigned this Sep 12, 2023
@github-actions github-actions bot added the C-trivial Category: A trivial change that is not worth mentioning in the CHANGELOG label Sep 12, 2023
@teor2345 teor2345 changed the title change(db): Upgrade subtrees from the tip backwards, for compatibility with wallet syncing change(db): Upgrade subtrees from the tip backwards, for compatibility with wallet syncing. Fix incorrect subtree indexes Sep 12, 2023
@teor2345 teor2345 marked this pull request as ready for review September 18, 2023 22:15
@teor2345
Copy link
Contributor Author

I have started a full sync with this code, so I can use it for the test in ticket #7450.

Base automatically changed from fix-flsync-subt to main September 19, 2023 14:49
mergify bot added a commit that referenced this pull request Sep 19, 2023
@arya2
Copy link
Contributor

arya2 commented Sep 20, 2023

In the merge queue:

Message: assertion failed: (left == right)
left: 5,
right: 0: developers: should fail if get_mempool_tx start working.
Location: zebrad/tests/common/lightwalletd/send_transaction_test.rs:214

https://github.com/ZcashFoundation/zebra/actions/runs/6241861800/job/16946300444#step:9:572

@teor2345
Copy link
Contributor Author

In the merge queue:

Message: assertion failed: (left == right)
left: 5,
right: 0: developers: should fail if get_mempool_tx start working.
Location: zebrad/tests/common/lightwalletd/send_transaction_test.rs:214
ZcashFoundation/zebra/actions/runs/6241861800/job/16946300444#step:9:572

This bug has nothing to do with this PR, so let's try merging it again.

@teor2345
Copy link
Contributor Author

@Mergifyio refresh

@mergify
Copy link
Contributor

mergify bot commented Sep 20, 2023

refresh

✅ Pull request refreshed

mergify bot added a commit that referenced this pull request Sep 20, 2023
@mergify mergify bot merged commit d651ee3 into main Sep 20, 2023
119 checks passed
@mergify mergify bot deleted the upgrade-subtrees-backwards branch September 20, 2023 23:41
arya2 pushed a commit that referenced this pull request Sep 29, 2023
…y with wallet syncing (#7531)

* 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

* Upgrade subtrees in reverse height order

Reverts "Temporarily upgrade subtrees in forward height order"
This reverts commit a9558be.

* Bump the database patch version to re-run the upgrade (for testing)

* Revert "Remove an early reference to reverse direction"

This reverts commit c206404.

---------

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-compatibility Area: Compatibility with other nodes or wallets, or standard rules A-state Area: State / database changes C-enhancement Category: This is an improvement C-trivial Category: A trivial change that is not worth mentioning in the CHANGELOG
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make subtree index upgrade compatible with lightwalletd back-tracking algorithm
3 participants