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

Refactor the wallet server/hub database use only leveldb (drop sqlite) and safely handle reorgs #3205

Merged
merged 206 commits into from
Oct 7, 2021

Conversation

jackrobison
Copy link
Member

@jackrobison jackrobison commented Feb 19, 2021

fixes #3143, #3142

Handling reorgs in sqlite is very difficult and would require completely refactoring the table structure to properly unwind blocks and unwind the changes to urls. As a result, the sqlite wallet server will have inconsistent results that get worse as reorgs occur.

To fix this, this PR implements the claimtrie in a key/value database (currently leveldb) such that each operation can be unwound.

Refactoring:

  • unify the different leveldb databases into one (utxo, hist, headers, transactions) so that caches are shared and we aren't doing operations between threads when doing lookups
  • remove the separate History class used for address histories, integrate it in to the LevelDB database class.
  • move all leveldb prefixes to DB_PREFIXES enum, previously these were split across several files and it was hard to see what prefixes were used where
  • atomic block advance and rewind (apply all changes for a new or undone block as one atomic write, for data integrity)
  • maintain in memory reverse mapping of txids to tx numbers (to save on i/o)

New interfaces:

  • PrefixDB
  • RevertableOp, used serialize and deserialize a reversible representation of an operation to put or delete to a key/value record.
  • PrefixRow, used pack and unpack the keys and values for a data type in the k/v database. Each 'table' in the database has a PrefixRow class to pack and unpack the items in it.

@lbry-bot lbry-bot assigned lyoshenka and unassigned jackrobison Feb 24, 2021
@lyoshenka lyoshenka removed their request for review March 17, 2021 14:53
@lbry-bot lbry-bot assigned jackrobison and unassigned lyoshenka Mar 17, 2021
@jackrobison jackrobison changed the title use leveldb to resolve urls use leveldb to resolve urls (WIP) May 5, 2021
@jackrobison jackrobison force-pushed the leveldb-resolve branch 2 times, most recently from d8f4404 to a0c8201 Compare May 26, 2021 21:25
@belikor
Copy link
Contributor

belikor commented Jul 8, 2021

Hi, I'm looking into the code of the SDK in a general way, and noticed some compilation issues with plyvel due to missing libleveldb-dev. See #3349.

I noticed that currently plyvel is compiled from source, as it's in setup.py. Currently it only seems to be used in lbry.wallet.server.storage.

I'm not sure how plyvel is used but wouldn't it make sense to simply use a prepackaged version of plyvel? In Ubuntu the package is in python3-plyvel, and in Arch it is in python-plyvel. I was thinking about mentioning the package as a dependency in INSTALL.md, and removing the variable PLYVEL from setup.py but I'm not sure if this affects anything in the code.

I tested by not installing plyvel at all, and then installing and running the unit tests; they seemed to work okay without referencing plyvel at all. So I'm wondering if the current code doesn't use plyvel, and it will only use it once this pull request is merged.

@belikor
Copy link
Contributor

belikor commented Jul 10, 2021

@jackrobison about my previous comment; I'm wondering if there is a particular reason to use plyvel 1.0.5.

The errors that I mentioned don't occur if we just set plyvel=1.2.0 or plyvel=1.3.0 in setup.py. This seems to be an easy solution. See #3357.

jackrobison added a commit that referenced this pull request Jul 12, 2021
@jackrobison
Copy link
Member Author

jackrobison commented Jul 12, 2021

@belikor updated plyvel to 1.3.0

@belikor
Copy link
Contributor

belikor commented Jul 12, 2021

Nice.

Is this branch meant to be merged soon? If that's the case, I will close #3357. Otherwise I expect #3357 to be merged before, and you'll have to rebase this branch before merging.

jackrobison added a commit that referenced this pull request Jul 14, 2021
@lyoshenka lyoshenka marked this pull request as ready for review July 19, 2021 18:37
@lyoshenka lyoshenka requested a review from shyba July 19, 2021 18:41
-access db through HubDB class, don't use plyvel.DB directly
-add channel count and support amount prefixes
@coveralls
Copy link

coveralls commented Oct 5, 2021

Coverage Status

Coverage remained the same at 68.972% when pulling 43432a9 on leveldb-resolve into 84908ec on master.

@jackrobison jackrobison force-pushed the leveldb-resolve branch 5 times, most recently from f9ecabc to 09af182 Compare October 6, 2021 22:11
…est_transactions into their own runner

-move test_resolve_command to its own runner
@jackrobison jackrobison merged commit bc6822e into master Oct 7, 2021
@jackrobison jackrobison deleted the leveldb-resolve branch October 7, 2021 06:07
@jackrobison jackrobison added area: hub type: improvement Existing (or partially existing) functionality needs to be changed type: refactor Minimal user-visible changes, but significant internal work labels Oct 22, 2021
@jackrobison jackrobison added type: new feature New functionality that does not exist yet and removed type: improvement Existing (or partially existing) functionality needs to be changed type: refactor Minimal user-visible changes, but significant internal work labels Dec 1, 2021
@jackrobison jackrobison changed the title drop sqlite and fix reorgs Refactor the wallet server/hub database to be all leveldb based and safely handle reorgs (drop sqlite) Dec 1, 2021
@jackrobison jackrobison changed the title Refactor the wallet server/hub database to be all leveldb based and safely handle reorgs (drop sqlite) Refactor the wallet server/hub database use only leveldb (drop sqlite) and safely handle reorgs Dec 2, 2021
shyba pushed a commit that referenced this pull request Mar 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: hub type: new feature New functionality that does not exist yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

handling support and claim reorgs in leveldb
8 participants