You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
// 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
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:
voidCoinsResult::Clear() {
coins.clear();
}
We are missing to clear the other two cached fields total_amount and total_effective_amount. That should fix the issue.
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.
Activity
fanquake commentedon Jun 3, 2026
cc @murchandamus
furszy commentedon Jun 3, 2026
CENTinstead ofCOINand done?hodlinator commentedon Jun 3, 2026
I think
COINis pretty essential to the case we are testing for, given the comment:bitcoin/src/wallet/test/coinselector_tests.cpp
Lines 503 to 507 in 654a522
furszy commentedon Jun 3, 2026
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:We are missing to clear the other two cached fields
total_amountandtotal_effective_amount. That should fix the issue.void CoinsResult::Clear() { coins.clear(); + total_amount = 0; + total_effective_amount = std::nullopt; }hodlinator commentedon Jun 3, 2026
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.
murchandamus commentedon Jun 3, 2026
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.
hodlinator commentedon Jun 4, 2026
PR: #35456
Merge #35456: test: Perform full reset of CoinsResult in order to avo…
wallet: Remove CoinsResult::Clear()
wallet: Remove CoinsResult::Clear()
Merge bitcoin/bitcoin#35456: test: Perform full reset of CoinsResult …
wallet: Remove CoinsResult::Clear()
Merge bitcoin/bitcoin#35456: test: Perform full reset of CoinsResult …