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: simplify and batch zap wallet txes process #28987

Merged
merged 4 commits into from Feb 12, 2024

Conversation

furszy
Copy link
Member

@furszy furszy commented Dec 2, 2023

Work decoupled from #28574. Brother of #28894.

Includes two different, yet interconnected, performance and code improvements to the zap wallet transactions process.

  1. As the goal of the ZapSelectTx function is to erase tx records that match any of the inputted hashes. There is no need to traverse the whole database record by record. We could just check if the tx exist, and remove it directly by calling EraseTx().

  2. Instead of performing single write operations per removed tx record, this PR batches them all within a single atomic db txn.

Moreover, these changes will enable us to consolidate all individual write operations that take place during the wallet migration process into a single db txn in the future.

@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 2, 2023

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 achow101, josibake

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:

  • #29403 (wallet: batch erase procedures and improve 'EraseRecords' performance by furszy)
  • #22693 (RPC/Wallet: Add "use_txids" to output of getaddressinfo by luke-jr)

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.

src/wallet/wallet.cpp Outdated Show resolved Hide resolved
src/wallet/wallet.cpp Outdated Show resolved Hide resolved
src/wallet/wallet.cpp Outdated Show resolved Hide resolved
The function does not return DBErrors::NEED_REWRITE.
The wallet is unloaded at the beginning of the migration process,
so no object is listening to the signals.
Copy link
Member Author

@furszy furszy left a comment

Choose a reason for hiding this comment

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

Updated per feedback. Thanks achow. Diff.

Copy link
Member Author

@furszy furszy left a comment

Choose a reason for hiding this comment

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

CI failure is unrelated. Relates to #29112.

src/wallet/walletdb.cpp Outdated Show resolved Hide resolved
src/wallet/wallet.cpp Outdated Show resolved Hide resolved

// Apply db changes and remove transactions from the memory map
if (!batch.TxnCommit()) return DBErrors::NONCRITICAL_ERROR;
Copy link
Member

Choose a reason for hiding this comment

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

In d26a285 "wallet: batch ZapSelectTx db operations"

I'm not sure if NONCRITICAL_ERROR is really the right return code. It seems like something has gone critically wrong if TxnCommit were to fail.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if NONCRITICAL_ERROR is really the right return code. It seems like something has gone critically wrong if TxnCommit were to fail.

The returned error code doesn't really matter. Anything different from LOAD_OK is an error for all the function's callers. And they behave exactly the same for all codes.
Actually, the DBErrors return doesn't make sense anymore after this PR changes. The function is no longer traversing the entire db nor checking for extra, unneeded, stuff like the version inside anymore.

Based on this, I have reworked the PR cleaning up this aspect as well.

@furszy furszy force-pushed the 2023_wallet_zaptx branch 3 times, most recently from 875dbfb to e4f8a02 Compare February 9, 2024 17:22
Copy link
Member Author

@furszy furszy left a comment

Choose a reason for hiding this comment

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

Updated per feedback. Thanks Marko and achow.

src/wallet/walletdb.cpp Outdated Show resolved Hide resolved
src/wallet/wallet.cpp Outdated Show resolved Hide resolved

// Apply db changes and remove transactions from the memory map
if (!batch.TxnCommit()) return DBErrors::NONCRITICAL_ERROR;
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if NONCRITICAL_ERROR is really the right return code. It seems like something has gone critically wrong if TxnCommit were to fail.

The returned error code doesn't really matter. Anything different from LOAD_OK is an error for all the function's callers. And they behave exactly the same for all codes.
Actually, the DBErrors return doesn't make sense anymore after this PR changes. The function is no longer traversing the entire db nor checking for extra, unneeded, stuff like the version inside anymore.

Based on this, I have reworked the PR cleaning up this aspect as well.

The goal of the function is to erase the wallet transactions that
match the inputted hashes. There is no need to traverse the database,
reading record by record, to then perform single entry removals for
each of them.

To ensure consistency and improve performance, this change-set removes
all tx records within a single atomic db batch operation, as well as
it cleans up code, improves error handling and simplifies the
transactions removal process entirely.

This optimizes the removal of watch-only transactions during the wallet
migration process and the 'removeprunedfunds' RPC command.
-BEGIN VERIFY SCRIPT-
sed -i 's/ZapSelectTx/RemoveTxs/g' $(git grep -l 'ZapSelectTx' ./src/wallet)
-END VERIFY SCRIPT-
@achow101
Copy link
Member

achow101 commented Feb 9, 2024

ACK 9a3c5c8

The new code is much simpler.

@josibake
Copy link
Member

ACK 9a3c5c8

looks good, also RemoveTxs is a much better name. Might be worth updating the description to mention the rename since the description ends up in the commit log.

@achow101 achow101 merged commit 6ff0aa0 into bitcoin:master Feb 12, 2024
16 checks passed
@furszy furszy deleted the 2023_wallet_zaptx branch February 12, 2024 18:45
if (ZapSelectTx(txids_to_delete, deleted_txids) != DBErrors::LOAD_OK) {
error = _("Error: Could not delete watchonly transactions");
if (auto res = RemoveTxs(txids_to_delete); !res) {
error = _("Error: Could not delete watchonly transactions. ") + util::ErrorString(res);
Copy link
Member

Choose a reason for hiding this comment

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

nit: The trailing space could be easily overlooked and dropped during translation.

Copy link
Member Author

Choose a reason for hiding this comment

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

nit: The trailing space could be easily overlooked and dropped during translation.

@hebasto, what about creating a constant to represent the whitespace, or alternatively, implementing a function that concatenates strings with the whitespace inserted between them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants