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: Track no-longer-spendable TXOs separately #27865
base: master
Are you sure you want to change the base?
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code CoverageFor detailed information about the code coverage, see the test coverage report. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsReviewers, this pull request conflicts with the following ones:
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. |
6925928
to
7a50755
Compare
8c4b04a
to
9730ec0
Compare
When loading transactions to the wallet, check the outputs, and keep track of the ones that are IsMine.
Since we track the outputs owned by the wallet with m_txos, we can now calculate the balance of the wallet by iterating m_txos and summing up the amounts of the unspent txos. As ISMINE_USED is not an actual isminetype that we attach to outputs and was just passed into `CachedTxGetAvailableCredit` for convenience, we pull the same determining logic from that function into `GetBalances` in order to preserve existing behavior.
Instead of iterating every transaction and every output stored in wallet when trying to figure out what outputs can be spent, iterate the TXO set which should be a lot smaller.
Instead of searching mapWallet for the preselected inputs, search m_txos.
When a legacy wallet has been migrated to contain descriptors, but before the transactions have been updated to match, we need to recompute the wallet TXOs so that the transaction update will work correctly.
Instead of looking up the previous tx in mapWallet, and then calling IsMine on the specified output, use the TXO set and its cached IsMine value.
These two functions are no longer used as GetBalances now uses the TXO set rather than per-tx cached balances
One of the main issues with AvailableCoins is its performance when txs have unrelated outputs, so update the benchmark to check the performance of that.
fFromMe was an unused variable in CWalletTx that did not accurately track the "from me" status of the transaction as the name would imply. m_from_me is a replacement which does track this status and can be used to determine whether a transaction is "from me" with the different ismine filters that we use.
Instead of looking at the cached amounts or searching every input of a transaction each time we want to determine whether it is "from me", use the m_from_me map which stores this value for us.
Since we need to know whether the transaction that creates a WalletTXO is "from me", we should store this state in the WalletTXO too, copied from its parent CWalletTx.
WalletTXOs need to know their parent tx's timestamp for AvailableCoins to work.
When the state of a CWalletTx changes, we need to change the state in the WalletTXOs too.
A min_conf parameter is added to IsSpent so that it can set a confirmation threshold for whether something is considered spent.
When a block is disconnected, we need to process the transactions in reverse order so that the wallet's TXO set is updated in the correct order.
CWallet::Create will properly connect the wallet to the chain, so we should be doing that rather than ad-hoc chain connection.
Definitely unusable TXOs are those that are spent by a confirmed transaction or were produced by a now conflicted transaction. However, we still need them for GetDebit, so we store them in a separate m_unusable_txos container. MarkConflicted, AbandonTransaction, and loading (via PruneSpentTXOs) will ensure that these unusable TXOs are properly moved.
91205a8
to
b677b00
Compare
In #27286, the wallet keeps track of all of its transaction outputs, even if they are already spent or are otherwise unspendable. This TXO set is iterated for balance checking and coin selection preparation, which can still be slow for wallets that have had a lot of activity. This PR aims to improve the performance of such wallets by moving UTXOs that are definitely no longer spendable to a different map in the wallet so that far fewer TXOs need to be iterated for the aforementioned functions.
Unspendable TXOs (not to be confused with Unspent TXOs) are those which have a spending transaction that has been confirmed, or are no longer valid due to reorgs. TXOs that are spent in unconfirmed transactions remain in the primary TXO set, and are filtered out of balance and coin selection as before.
Depends on #27286