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 654a5223af532e965998bb1d6988be421fba186f 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.