Skip to content

wallet: Replace mapWallet and wtxOrdered with a boost::multi_index - #35716

Open
achow101 wants to merge 4 commits into
bitcoin:masterfrom
achow101:wallet-mapwallet-multiindex
Open

achow101 wants to merge 4 commits into
bitcoin:masterfrom
achow101:wallet-mapwallet-multiindex

Conversation

@achow101

@achow101 achow101 commented Jul 14, 2026 •

Copy link
Copy Markdown
Member

The wallet needs to access transactions both by txid, and by insertion order into the wallet. mapWallet was the main container for the wallet transactions, and it was keyed by txid. wtxOrdered was a secondary container which was keyed by order, and held pointers to the CWalletTx objects stored in mapWallet. These are replaced by a new boost multi index which indexes on txid and insertion order, allowing us to get rid of the 2 separate containers, and holding raw pointers to CWalletTx.

@DrahtBot

DrahtBot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK theuni, rkrux
Stale ACK pablomartin4btc

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36918 (Witness stacks in serialized form (optimization follow-ups) by sipa)
  • #36909 (kernel: add btck_witness_stack_get_items by pzafonte)
  • #36895 (Keep witness stacks in serialized form in CScriptWitness by sipa)
  • #36859 (wallet: guard null coin control in AvailableCoins TRUC filter by brunoerg)
  • #36825 (wallet: fix migration with watch-only transactions by psychemist)
  • #36823 (wallet, migration: Skip legacy wallet records rather than erasing them, and drop unused record erase functions by achow101)
  • #36398 (wallet: only rebroadcast transactions originating from the wallet by mzumsande)
  • #35998 (wallet: Handle or explicitly ignore WalletBatch write failures by achow101)
  • #35786 (wallet: drop spent parents redundant cache invalidation and notification by furszy)
  • #35294 (wallet: Update tx chain state during loading during AttachChain instead of before by achow101)
  • #35151 (wallet, follow-up: Refactor IsSpent to use HowSpent by musaHaruna)
  • #34909 (wallet, refactor: modularise wallet by extracting out legacy wallet migration by rkrux)
  • #34872 (wallet: fix mixed-input transaction accounting in history RPCs by w0xlt)
  • #30343 (wallet, logging: Replace WalletLogPrintf() with LogInfo() by ryanofsky)
  • #27865 (wallet: Track no-longer-spendable TXOs separately by achow101)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • src/wallet/migration.cpp: cannot be identified to belong → cannot be identified as belonging [the original phrasing is grammatically broken]

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • src/wallet/rpc/transactions.cpp: ListTransactions(*pwallet, *it, 0, true, ret, filter_label)
  • src/wallet/rpc/transactions.cpp: ListTransactions(wallet, tx, 0, true, transactions, filter_label, include_change)
  • src/wallet/rpc/transactions.cpp: ListTransactions(wallet, *wtx, -100000000, true, removed, filter_label, include_change)

2026-11-24 20:08:38

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/29296252667/job/86970309393
LLM reason (✨ experimental): CI failed because the fuzz build (src/test/fuzz/spend.cpp) stopped with a Clang -Werror,-Wunused-variable error for an unused txid variable.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly 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.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

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

@theuni

theuni commented Jul 14, 2026

Copy link
Copy Markdown
Member

Concept ACK. multi_index seems like the right thing to use here. Since this doesn't leak into consensus/node, I don't see the harm in spreading the dependency to wallet.

Getting rid of multi_index for the kernel will require something that essentially re-implements its functionality, and I don't see anything new here that would complicate that implementation.

Note that I'm biased: I'm currently working on such a re-implementation that I hope to propose for v33.

I do have a few requests to make my life easier though.


Firstly, please specify an index for each operation, as opposed to just using m_txs. What happens behind the scenes in that case is that the first index is used, which is not at all obvious to reviewers. In my rewrite I'm hoping to drop the implicit first index functionality and require an index for each call, as that greatly simplifies the implementation.

For ex, rather than m_txs.find(hash) use m_txs.get<index_by_txid>().find(hash). That also means adding the tag for the txid index, which is currently missing.

To simplify that, in wallet.h, you can do:

WalletTxs m_txs;
WalletTxs::index<index_by_txid>& m_txs_by_txid;
WalletTxs::index<index_by_pos>& m_txs_by_pos;

And in the constructor:

m_txs_by_txid(m_txs.get<index_by_txid>());
m_txs_by_pos(m_txs.get<m_txs_by_pos>());

Then everywhere you would use m_txs, use m_txs_by_txid or m_txs_by_pos instead. That makes the intent much more clear.

Here's an example of the mempool conversion to work that way (though I used indices rather than tags, which is uglier): theuni@b58ab92 .


Secondly, please use extract/insert rather than modify. The former is compatible with the std container apis and has familiar (and much simpler) semantics wrt re-insertion failure. I was planning on removing our existing modify calls for the same reason, so it'd be nice to not be adding more in the meantime.

@achow101

Copy link
Copy Markdown
Member Author

Secondly, please use extract/insert rather than modify. The former is compatible with the std container apis and has familiar (and much simpler) semantics wrt re-insertion failure. I was planning on removing our existing modify calls for the same reason, so it'd be nice to not be adding more in the meantime.

Hmm, modify is nicer though since it doesn't require remembering to re-insert and I find it easier to read. I'm also running into trouble with extract and insert where it doesn't work well for looping over the entire multi index and modifying each item.

@achow101
achow101 force-pushed the wallet-mapwallet-multiindex branch from 45d2803 to 1d8337a Compare July 17, 2026 19:17
@achow101
achow101 force-pushed the wallet-mapwallet-multiindex branch from 1d8337a to e57b085 Compare July 20, 2026 21:37
@achow101

Copy link
Copy Markdown
Member Author

I've changed all of the modify to extract then insert, with some changes to the functions that iterate over everything in the wallet. These mainly involved calling CWalletTx::MarkDirty. Since that function only changes non-key mutable members of CWalletTx, I made MarkDirty const so that it can be called without needing to do the extract then insert.

@achow101
achow101 force-pushed the wallet-mapwallet-multiindex branch from c34334e to f31dfae Compare September 14, 2026 23:44
@achow101
achow101 force-pushed the wallet-mapwallet-multiindex branch from f31dfae to 7146e2f Compare November 5, 2026 00:00
When a transaction needs to be added to the wallet for testing, we can
use AddToWallet instead of direct insertion into mapWallet.
Since all of the things that MarkDirty changes are mutable, we can mark
it as a const so it can be called on const CWalletTx objects.
Since we need to access transactions both by txid and insertion order,
instead of having 2 separate maps, the second with raw pointers into
first, we can use a multi index.
@achow101
achow101 force-pushed the wallet-mapwallet-multiindex branch from 7146e2f to a5c05e1 Compare November 24, 2026 20:08
@maflcko

maflcko commented Nov 26, 2026

Copy link
Copy Markdown
Member

Looks like the CI unit tests failed, but it is impossible for me to see the actual error in the 5 trillion lines of logs

@l0rinc

l0rinc commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

5 trillion lines of logs

It’s actually 6 trillion, your download was truncated :p

My clanker claims the job failed because coinselector_tests hit CTest’s 2,400 second timeout. The output contains roughly 357k SQLite trace lines and 100k AddToWallet lines. SelectCoins_test adds 1,000 synthetic coins to the same wallet in each of 100 iterations, and this PR changes those additions from in-memory inserts to AddToWallet calls that also write to SQLite. The robot suggests reverting this test helper to adding the coins directly to the new in-memory index. That should cut the repeated logging and likely fix the timeout.

@DrahtBot

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

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.

7 participants