@@ -881,6 +881,7 @@ BOOST_AUTO_TEST_CASE(waste_test)
881
881
const CAmount in_amt{3 * COIN};
882
882
const CAmount target{2 * COIN};
883
883
const CAmount excess{80 };
884
+ const CAmount exact_target{in_amt - fee * 2 }; // Maximum spendable amount after fees: no change, no excess
884
885
885
886
// In the following, we test that the waste is calculated correctly in various scenarios.
886
887
// Usually, RecalculateWaste would compute change_fee and change_cost on basis of the
@@ -889,7 +890,7 @@ BOOST_AUTO_TEST_CASE(waste_test)
889
890
{
890
891
// Waste with change is the change cost and difference between fee and long term fee
891
892
SelectionResult selection1{target, SelectionAlgorithm::MANUAL};
892
- add_coin (1 * COIN, 1 , selection1, fee, fee - fee_diff);
893
+ add_coin (1 * COIN, 1 , selection1, /* fee= */ fee, /* long_term_fee= */ fee - fee_diff);
893
894
add_coin (2 * COIN, 2 , selection1, fee, fee - fee_diff);
894
895
selection1.RecalculateWaste (min_viable_change, change_cost, change_fee);
895
896
BOOST_CHECK_EQUAL (fee_diff * 2 + change_cost, selection1.GetWaste ());
@@ -913,15 +914,15 @@ BOOST_AUTO_TEST_CASE(waste_test)
913
914
914
915
{
915
916
// Waste without change is the excess and difference between fee and long term fee
916
- SelectionResult selection_nochange1{/* target creates no change */ in_amt - 2 * fee - excess, SelectionAlgorithm::MANUAL};
917
+ SelectionResult selection_nochange1{exact_target - excess, SelectionAlgorithm::MANUAL};
917
918
add_coin (1 * COIN, 1 , selection_nochange1, fee, fee - fee_diff);
918
919
add_coin (2 * COIN, 2 , selection_nochange1, fee, fee - fee_diff);
919
920
selection_nochange1.RecalculateWaste (min_viable_change, change_cost, change_fee);
920
921
BOOST_CHECK_EQUAL (fee_diff * 2 + excess, selection_nochange1.GetWaste ());
921
922
922
923
// Waste without change is the excess and difference between fee and long term fee
923
924
// With long term fee greater than fee, waste should be less than when long term fee is less than fee
924
- SelectionResult selection_nochange2{/* target creates no change */ in_amt - 2 * fee - excess, SelectionAlgorithm::MANUAL};
925
+ SelectionResult selection_nochange2{exact_target - excess, SelectionAlgorithm::MANUAL};
925
926
add_coin (1 * COIN, 1 , selection_nochange2, fee, fee + fee_diff);
926
927
add_coin (2 * COIN, 2 , selection_nochange2, fee, fee + fee_diff);
927
928
selection_nochange2.RecalculateWaste (min_viable_change, change_cost, change_fee);
@@ -940,7 +941,7 @@ BOOST_AUTO_TEST_CASE(waste_test)
940
941
941
942
{
942
943
// Waste without change and fee == long term fee is just the excess
943
- SelectionResult selection{/* target creates no change */ in_amt - 2 * fee - excess, SelectionAlgorithm::MANUAL};
944
+ SelectionResult selection{exact_target - excess, SelectionAlgorithm::MANUAL};
944
945
add_coin (1 * COIN, 1 , selection, fee, fee);
945
946
add_coin (2 * COIN, 2 , selection, fee, fee);
946
947
selection.RecalculateWaste (min_viable_change, change_cost, change_fee);
@@ -949,7 +950,6 @@ BOOST_AUTO_TEST_CASE(waste_test)
949
950
950
951
{
951
952
// Waste is 0 when fee == long_term_fee, no change, and no excess
952
- const CAmount exact_target{in_amt - fee * 2 };
953
953
SelectionResult selection{exact_target, SelectionAlgorithm::MANUAL};
954
954
add_coin (1 * COIN, 1 , selection, fee, fee);
955
955
add_coin (2 * COIN, 2 , selection, fee, fee);
@@ -968,7 +968,7 @@ BOOST_AUTO_TEST_CASE(waste_test)
968
968
969
969
{
970
970
// Waste is 0 when (fee - long_term_fee) == (-excess), no change cost
971
- const CAmount new_target{in_amt - fee * 2 - /* excess=*/ fee_diff * 2 };
971
+ const CAmount new_target{exact_target - /* excess=*/ fee_diff * 2 };
972
972
SelectionResult selection{new_target, SelectionAlgorithm::MANUAL};
973
973
add_coin (1 * COIN, 1 , selection, fee, fee + fee_diff);
974
974
add_coin (2 * COIN, 2 , selection, fee, fee + fee_diff);
@@ -978,7 +978,6 @@ BOOST_AUTO_TEST_CASE(waste_test)
978
978
979
979
{
980
980
// Negative waste when the long term fee is greater than the current fee and the selected value == target
981
- const CAmount exact_target{in_amt - 2 * fee};
982
981
SelectionResult selection{exact_target, SelectionAlgorithm::MANUAL};
983
982
const CAmount target_waste1{-2 * fee_diff}; // = (2 * fee) - (2 * (fee + fee_diff))
984
983
add_coin (1 * COIN, 1 , selection, fee, fee + fee_diff);
@@ -991,7 +990,11 @@ BOOST_AUTO_TEST_CASE(waste_test)
991
990
// Negative waste when the long term fee is greater than the current fee and change_cost < - (inputs * (fee - long_term_fee))
992
991
SelectionResult selection{target, SelectionAlgorithm::MANUAL};
993
992
const CAmount large_fee_diff{90 };
994
- const CAmount target_waste2{-2 * large_fee_diff + change_cost}; // = (2 * fee) - (2 * (fee + large_fee_diff)) + change_cost
993
+ const CAmount target_waste2{-2 * large_fee_diff + change_cost};
994
+ // = (2 * fee) - (2 * (fee + large_fee_diff)) + change_cost
995
+ // = (2 * 100) - (2 * (100 + 90)) + 125
996
+ // = 200 - 380 + 125 = -55
997
+ assert (target_waste2 == -55 );
995
998
add_coin (1 * COIN, 1 , selection, fee, fee + large_fee_diff);
996
999
add_coin (2 * COIN, 2 , selection, fee, fee + large_fee_diff);
997
1000
selection.RecalculateWaste (min_viable_change, change_cost, change_fee);
0 commit comments