Skip to content

knapsack_solver_test exceeds 21M BTC #35449

Closed

Description

@hodlinator

In resuming my experimentation around making CAmount a safer type, but ran into the unit test case knapsack_solver_test in coinselector_tests.cpp exceeding 21M BTC. This can be observed on master as of 654a522 by applying:

--- a/src/wallet/test/coinselector_tests.cpp
+++ b/src/wallet/test/coinselector_tests.cpp
@@ -505,6 +505,7 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
         available_coins.Clear();
         for (int j = 0; j < 20; j++)
             add_coin(available_coins, *wallet, 50000 * COIN);
+        assert(available_coins.GetTotalAmount() <= MAX_MONEY);
 
         const auto result19 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 500000 * COIN, CENT);
         BOOST_CHECK(result19);

There is an outer loop with 100 iterations, so 100x20x50'000 BTC = 100'000'000 BTC.

I'm sure there is an easy fix. Just don't want to be resetting too much state to make the tests not cover some case they should be, so hoping this issue will help uncover a good solution.

Activity

  1. fanquake commented on Jun 3, 2026

    @fanquake
    Member
  2. furszy commented on Jun 3, 2026

    @furszy
    Member

    CENT instead of COIN and done?

  3. hodlinator commented on Jun 3, 2026

    @hodlinator
    ContributorAuthor

    CENT instead of COIN and done?

    I think COIN is pretty essential to the case we are testing for, given the comment:

    // run the 'mtgox' test (see https://blockexplorer.com/tx/29a3efd3ef04f9153d47a990bd7b048a4b2d213daaa5fb8ed670fb85f13bdbcf)
    // they tried to consolidate 10 50k coins into one 500k coin, and ended up with 50k in change
    available_coins.Clear();
    for (int j = 0; j < 20; j++)
    add_coin(available_coins, *wallet, 50000 * COIN);

  4. furszy commented on Jun 3, 2026

    @furszy
    Member

    hehe, I only read the lines you shared in the description.
    Just went deeper.. the issue isn't the test case; it clears the available coins right before start, so available coins shouldn't be compounding across loop iterations. The problem is inside CoinsResult::Clear:

    void CoinsResult::Clear() {
        coins.clear();
    }

    We are missing to clear the other two cached fields total_amount and total_effective_amount. That should fix the issue.

    void CoinsResult::Clear() {
         coins.clear();
    +    total_amount = 0;
    +    total_effective_amount = std::nullopt;
     }
  5. hodlinator commented on Jun 3, 2026

    @hodlinator
    ContributorAuthor

    Thanks @furszy, I'm cooking up a PR with a commit in the direction you suggested! Should be ready for review together with some other minor changes by tomorrow.

  6. murchandamus commented on Jun 3, 2026

    @murchandamus
    Member

    The correct solution is of course to remove the Knapsack coinselection algorithm. ;)

    Jokes aside, thanks for surfacing why the amount compounded across loops, @furszy, I was about to check why it did when I saw your comment.

  7. hodlinator commented on Jun 4, 2026

    @hodlinator
    ContributorAuthor

    PR: #35456

  8. added 2 commits that reference this issue on Jun 8, 2026
  9. added 2 commits that reference this issue on Jun 28, 2026
  10. added 2 commits that reference this issue on Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions