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

wallet: Implement independent BDB parser #26606

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

achow101
Copy link
Member

Split from #26596

This PR adds BerkeleyRODatabase which is an independent implementation of a BDB file parser. It provides read only access to a BDB file, and can therefore be used as a read only database backend for wallets. This will be used for dumping legacy wallet records and migrating legacy wallets without the need for BDB itself.

Wallettool's dump command is changed to use BerkeleyRODatabase instead of BerkeleyDatabase (and CWallet itself) to demonstrate that this parser works and to test it against the existing wallettool functional tests.

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 29, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK laanwj
Concept ACK darosior, Sjors, theStack
Stale ACK TheCharlatan

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #29071 (refactor: Remove Span operator==, Use std::ranges::equal by maflcko)
  • #28236 (fuzz: wallet, add target for Spend by Ayush170-Future)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link

@Marek777777 Marek777777 left a comment

Choose a reason for hiding this comment

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

.

Copy link
Member

@Sjors Sjors left a comment

Choose a reason for hiding this comment

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

Some review nibbles.

src/streams.h Outdated Show resolved Hide resolved
src/streams.h Outdated
@@ -530,6 +530,14 @@ class AutoFile
//
// Stream subset
//
void seek(int64_t offset, int origin)
Copy link
Member

Choose a reason for hiding this comment

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

1d093bb: Note for other reviewers: yes

https://en.cppreference.com/w/c/io/fseek

src/streams.h Outdated
if (!file)
throw std::ios_base::failure("CAutoFile::read: file handle is nullptr");
if (fseek(file, offset, origin) != 0)
throw std::ios_base::failure(feof(file) ? "CAutoFile::seek: end of file" : "CAutoFile::seek: fseek failed");
Copy link
Member

Choose a reason for hiding this comment

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

1d093bb: maybe log the exact error code.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's kind of annoying to do that, so I'll leave it as is. The other functions do the same.

// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_WALLET_MIGRATE_H
#define BITCOIN_WALLET_MIGRATE_H
Copy link
Member

Choose a reason for hiding this comment

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

1224f45: maybe call this file bdb_ro_wallet_db.h?

Copy link
Member Author

Choose a reason for hiding this comment

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

The intent is to have all of the migration stuff in these files, so I will leave it as is.

src/wallet/migrate.cpp Outdated Show resolved Hide resolved
@darosior
Copy link
Member

Concept ACK. Have you looked into a fuzz target for this parser?

@achow101
Copy link
Member Author

Have you looked into a fuzz target for this parser?

I haven't. If someone would like to write one, I'd be happy to add it.

@Sjors
Copy link
Member

Sjors commented Jun 5, 2023

Adding a fuzz target that compares this to the real thing seems useful. Given that this PR changes dumpwallet to rely on the new parser, I think we should do that sooner rather than later. Otherwise I suggest changing e88ad38 so that the read-only parser is optional, and then change the tests to always use it.

It seems a bit daunting to compare the low level DBD parsing code in this PR to the real thing. Having a fuzzer also reduces the need to be very thorough in that. I'm not sure how much confidence I have in the existing test coverage for dumpwallet.

@achow101 achow101 force-pushed the berkeleyro branch 2 times, most recently from 944a020 to f108c6b Compare June 8, 2023 09:42
@DrahtBot DrahtBot removed the CI failed label Jun 8, 2023
@achow101
Copy link
Member Author

achow101 commented Apr 22, 2024

For testing big endian wallets, you can use db_dump wallet.dat | db_load -c db_lorder=4321 wallet_big_endian.dat to make big endian databasese using BDB's tools.

However, I also realized that BDB lets us set the endianness to use for a new database, so I've added a bdb_swap format to createfromdump that will make a BDB database with the other endianness. This will flip it depending on the system that you use, so on little endian, it will make a big endian wallet, and on big endian, it will make a little endian wallet. Adding this lets us have a test of BerkeleyRO dumping other endian wallets.

I've also added a test for the overflow page issue that was discovered earlier, and another test for the LSN reset check.

@DrahtBot
Copy link
Contributor

🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the
documentation.

Possibly this is due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.

Leave a comment here, if you need help tracking down a confusing failure.

Debug: https://github.com/bitcoin/bitcoin/runs/24124143992

src/wallet/init.cpp Outdated Show resolved Hide resolved
Implements MakeBerkeleyRODatabase and adds DatabaseFormat::BERKELEY_RO
so that MakeDatabase can use BerkeleyRO as the backend database.
…lets

In order to ease the transition to not having BDB, make the dump tool
use DatabaseFormmat::BERKELEY_RO when -withinternalbdb is set.
@DrahtBot
Copy link
Contributor

🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the
documentation.

Possibly this is due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.

Leave a comment here, if you need help tracking down a confusing failure.

Debug: https://github.com/bitcoin/bitcoin/runs/24275967354

@laanwj
Copy link
Member

laanwj commented Apr 28, 2024

New tests LGTM
Re-ACK a0943b8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Priority Project Primary Blocker
Development

Successfully merging this pull request may close these issues.

None yet