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: Track no-longer-spendable TXOs separately #27865

Draft
wants to merge 28 commits into
base: master
Choose a base branch
from

Conversation

achow101
Copy link
Member

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

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 12, 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
Concept ACK remyers, murchandamus

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:

  • #bitcoin-core/gui/733 (Deniability - a tool to automatically improve coin ownership privacy by denavila)
  • #29415 (Broadcast own transactions only via short-lived Tor or I2P connections by vasild)
  • #29401 (test: Remove struct.pack from almost all places by maflcko)
  • #29278 (Wallet: Add maxfeerate wallet startup option by ismaelsadeeq)
  • #29062 (Wallet: (Refactor) GetBalance to calculate used balance by BrandonOdiwuor)
  • #28616 (Show transactions as not fully confirmed during background validation by Sjors)
  • #28574 (wallet: optimize migration process, batch db transactions by furszy)
  • #28201 (Silent Payments: sending by josibake)
  • #27792 (wallet: Deniability API (Unilateral Transaction Meta-Privacy) by denavila)
  • #27286 (wallet: Keep track of the wallet's own transaction outputs in memory by achow101)
  • #26732 ([WIP] wallet: tx creation, don't select outputs from txes that are being replaced by furszy)
  • #25722 (refactor: Use util::Result class for wallet loading by ryanofsky)
  • #22838 (descriptors: Be able to specify change and receiving in a single descriptor string by achow101)
  • #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.

This was referenced Jun 12, 2023
@achow101 achow101 force-pushed the wallet-unspent-txos branch 2 times, most recently from 8c4b04a to 9730ec0 Compare June 28, 2023 22:26
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants