test: speed up wallet unit test #36832

pull ViniciusCestarii wants to merge 2 commits into bitcoin:master from ViniciusCestarii:speedup-wallet-unit-test changing 6 files +26 −36
  1. ViniciusCestarii commented at 8:20 PM on November 10, 2026: 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
  2. DrahtBot added the label Tests on Nov 10, 2026
  3. DrahtBot commented at 8:21 PM on November 10, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    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 <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36843 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36843.svg"></sub> (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)
    • #27865 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/27865.svg"></sub> (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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. in src/wallet/test/util.cpp:25 in aebed915d0
      18 | @@ -19,18 +19,23 @@
      19 |  #include <memory>
      20 |  
      21 |  namespace wallet {
      22 | +std::unique_ptr<CWallet> NewWallet(interfaces::Chain& chain, const std::string& wallet_name)
      23 | +{
      24 | +    std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(&chain, wallet_name, CreateMockableWalletDatabase());
      25 | +    wallet->m_keypool_size = 1; // Avoid pre-deriving a large keypool
    


    maflcko commented at 8:35 PM on November 10, 2026:

    concept ack. this follows the functional tests:

    test/functional/test_framework/util.py:        f.write("keypool=1\n")
    
  5. in src/wallet/test/coinselector_tests.cpp:1322 in 4ebe3d9597 outdated
    1305 | @@ -1315,11 +1306,11 @@ BOOST_AUTO_TEST_CASE(SelectCoins_effective_value_test)
    1306 |      // This test creates a coin whose value is higher than the target but whose effective value is lower than the target.
    1307 |      // The coin is selected using coin control, with m_allow_other_inputs = false. SelectCoins should fail due to insufficient funds.
    1308 |  
    1309 | -    std::unique_ptr<CWallet> wallet = NewWallet(m_node);
    1310 | +    std::unique_ptr<CWallet> wallet = NewWallet(*m_node.chain);
    1311 |  
    1312 |      CoinsResult available_coins;
    1313 |      {
    1314 | -        std::unique_ptr<CWallet> dummyWallet = NewWallet(m_node, /*wallet_name=*/"dummy");
    


    davidgumberg commented at 10:21 PM on November 10, 2026:

    nit: Since the only "users" of wallet_name here and below set dummy values, maybe the name parameter can be dropped from NewWallet()?


    ViniciusCestarii commented at 12:31 PM on November 12, 2026:

    Nice. Done on 5b70468637e00349ec9a7be35c84995f405a91e0

  6. in src/wallet/test/coinselector_tests.cpp:158 in 4ebe3d9597 outdated
     154 | @@ -155,15 +155,6 @@ inline std::vector<OutputGroup>& KnapsackGroupOutputs(const CoinsResult& availab
     155 |      return static_groups.all_groups.mixed_group;
     156 |  }
     157 |  
     158 | -static std::unique_ptr<CWallet> NewWallet(const node::NodeContext& m_node, const std::string& wallet_name = "")
    


    davidgumberg commented at 10:34 PM on November 10, 2026:

    In https://github.com/bitcoin/bitcoin/pull/36832/changes/4ebe3d9597fa090438f95d33801428894ba69c9b (test: deduplicate wallet creation into NewWallet helper)

    nanonit: If retouching this commit message should probably include refactor:


    ViniciusCestarii commented at 12:31 PM on November 12, 2026:

    nanonit taken haha

  7. in src/wallet/test/util.cpp:33 in 4ebe3d9597
      29 | +}
      30 | +
      31 |  std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
      32 |  {
      33 | -    auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase());
      34 | +    auto wallet = NewWallet(chain);
    


    davidgumberg commented at 10:36 PM on November 10, 2026:

    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.


    ViniciusCestarii commented at 12:43 PM on November 12, 2026:

    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 5b70468637e00349ec9a7be35c84995f405a91e0.


    davidgumberg commented at 12:05 AM on November 13, 2026:

    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.


    ViniciusCestarii commented at 12:24 PM on November 13, 2026:

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

    https://github.com/bitcoin/bitcoin/pull/36832/changes/e8f9a7c51a1e68af44123a9016edd2ead4356b3a#diff-de599d9a8f1ba87b441fa95ddb92737eef323ec968f0a9f3c38ec5a3b374045cL29-L33

    -    {
    -        LOCK(wallet->cs_wallet);
    -        wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
    -        wallet->SetupDescriptorScriptPubKeyMans();
    -    }
    
  8. davidgumberg commented at 10:49 PM on November 10, 2026: contributor

    ACK aebed915d0698dc875e100df06c7780360dc3374

    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.

  9. Zippo76 commented at 9:37 PM on November 11, 2026: none

    tACK aebed915d0

    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%
  10. refactor: test: deduplicate wallet creation into NewWallet helper and simplify CreateSyncedWallet e8f9a7c51a
  11. test: avoid pre-deriving a large keypool 5b70468637
  12. ViniciusCestarii force-pushed on Nov 12, 2026
  13. ViniciusCestarii commented at 12:30 PM on November 12, 2026: contributor

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

  14. Zippo76 commented at 9:08 PM on November 12, 2026: none

    tACK 5b70468637

    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.

  15. DrahtBot requested review from davidgumberg on Nov 12, 2026

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-18 16:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me