Skip to content

test: speed up wallet unit test - #36832

Open
ViniciusCestarii wants to merge 2 commits into
bitcoin:masterfrom
ViniciusCestarii:speedup-wallet-unit-test
Open

ViniciusCestarii wants to merge 2 commits into
bitcoin:masterfrom
ViniciusCestarii:speedup-wallet-unit-test

Conversation

@ViniciusCestarii

Copy link
Copy Markdown
Contributor

Deduplicates wallet creation code and avoid pre-deriving a large keypool (default 1000) by setting wallet keypool_size = 1

Speedup:

Suite Before After
coinselector_tests 8.26 s 3.35 s
wallet_tests 2.10 s 1.68 s
spend_tests 0.59 s 0.18 s
group_outputs_tests 0.33 s 0.12 s

@DrahtBot DrahtBot added the Tests label Nov 10, 2026
@DrahtBot

DrahtBot commented Nov 10, 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
ACK Zippo76, davidgumberg

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:

  • #36946 (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)
  • #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.

Comment thread src/wallet/test/util.cpp Outdated
std::unique_ptr<CWallet> NewWallet(interfaces::Chain& chain, const std::string& wallet_name)
{
std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(&chain, wallet_name, CreateMockableWalletDatabase());
wallet->m_keypool_size = 1; // Avoid pre-deriving a large keypool

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

concept ack. this follows the functional tests:

test/functional/test_framework/util.py:        f.write("keypool=1\n")

Comment thread src/wallet/test/coinselector_tests.cpp Resolved
Comment thread src/wallet/test/coinselector_tests.cpp Resolved
Comment thread src/wallet/test/util.cpp Outdated
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
{
auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase());
auto wallet = NewWallet(chain);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Observation/question: Why were tests using CreateSyncedWallet() passing before this change?

Also, if you avoid refactoring this line, I think the interface for NewWallet is a little better taking the node argument since then you don't need to do the member access and pointer dereference at every call site, the same could apply to CreateSyncedWallet() whose callers are all also doing the same thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Why were tests using CreateSyncedWallet() passing before this change?

I'm not sure what you mean. It didn't change any behaviour because CreateSyncedWallet() used to create a wallet just like NewWallet() but now it reuses the NewWallet() to deduplicate code.

I think the interface for NewWallet is a little better taking the node argument

At first I thought passing chain makes more sense for a wallet function but with your comment I realized it doesn't matter much and the benefits of passing node context directly are better. Done on 5b70468.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right, but NewWallet() does more initialization stuff than just creating the CWallet object, it just seemed like a smell to me that those tests should pass without the wallet having the descriptor flag, and without SPKM's set up, but it's probably fine, I have not looked close.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I understand what you mean now. It actually used to set those too but it is just in a lower block of code:

e8f9a7c#diff-de599d9a8f1ba87b441fa95ddb92737eef323ec968f0a9f3c38ec5a3b374045cL29-L33

-    {
-        LOCK(wallet->cs_wallet);
-        wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
-        wallet->SetupDescriptorScriptPubKeyMans();
-    }

@davidgumberg

Copy link
Copy Markdown
Contributor

ACK aebed91

Reproduced the performence improvement in coinselector_tests using hyperfine, 6.952 s ± 0.050 s on master, 2.797 s ± 0.021 s on this branch.

@Zippo76 Zippo76 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tACK aebed91

Tested on macOS 27.2 arm64 against master c68583ab5a. All 18 wallet unit test suites (83 test cases) pass on both master and this PR.

I reproduced the performance improvement in the four suites listed in the PR. Times below are in seconds, with five runs per suite on each revision:

Suite Master: median (min–max) PR: median (min–max) Time reduction
coinselector_tests 10.12 (9.95–11.15) 3.92 (3.85–3.94) 61%
wallet_tests 2.89 (2.83–3.13) 2.33 (2.30–2.40) 19%
spend_tests 0.74 (0.74–0.75) 0.24 (0.24–0.24) 68%
group_outputs_tests 0.38 (0.38–0.38) 0.13 (0.13–0.13) 66%

@ViniciusCestarii

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews! Force-pushed 5b70468 addressing @davidgumberg comments. I also dropped the cchain param of CreateSyncedWallet to simplify it since all callers just passed the active chain.

@Zippo76 Zippo76 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tACK 5b70468

Retested on macOS 27.2.1 arm64 against master 9183edd25e. The update addresses davidgumberg's nits; the keypool remains at 1.

All 18 wallet unit test suites (83 test cases) pass on both revisions. The performance improvement remains: median time for coinselector_tests was 10.16 s on master versus 3.88 s with this PR.

@davidgumberg

Copy link
Copy Markdown
Contributor

reACK 5b70468

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.

5 participants