Skip to content

Commit bd34dd8

Browse files
committed
Use exact_target shorthand in coinselector_tests
1 parent 7aa7e30 commit bd34dd8

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/wallet/test/coinselector_tests.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ BOOST_AUTO_TEST_CASE(waste_test)
881881
const CAmount in_amt{3 * COIN};
882882
const CAmount target{2 * COIN};
883883
const CAmount excess{80};
884+
const CAmount exact_target{in_amt - fee * 2}; // Maximum spendable amount after fees: no change, no excess
884885

885886
// In the following, we test that the waste is calculated correctly in various scenarios.
886887
// Usually, RecalculateWaste would compute change_fee and change_cost on basis of the
@@ -889,7 +890,7 @@ BOOST_AUTO_TEST_CASE(waste_test)
889890
{
890891
// Waste with change is the change cost and difference between fee and long term fee
891892
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);
893894
add_coin(2 * COIN, 2, selection1, fee, fee - fee_diff);
894895
selection1.RecalculateWaste(min_viable_change, change_cost, change_fee);
895896
BOOST_CHECK_EQUAL(fee_diff * 2 + change_cost, selection1.GetWaste());
@@ -913,15 +914,15 @@ BOOST_AUTO_TEST_CASE(waste_test)
913914

914915
{
915916
// 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};
917918
add_coin(1 * COIN, 1, selection_nochange1, fee, fee - fee_diff);
918919
add_coin(2 * COIN, 2, selection_nochange1, fee, fee - fee_diff);
919920
selection_nochange1.RecalculateWaste(min_viable_change, change_cost, change_fee);
920921
BOOST_CHECK_EQUAL(fee_diff * 2 + excess, selection_nochange1.GetWaste());
921922

922923
// Waste without change is the excess and difference between fee and long term fee
923924
// 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};
925926
add_coin(1 * COIN, 1, selection_nochange2, fee, fee + fee_diff);
926927
add_coin(2 * COIN, 2, selection_nochange2, fee, fee + fee_diff);
927928
selection_nochange2.RecalculateWaste(min_viable_change, change_cost, change_fee);
@@ -940,7 +941,7 @@ BOOST_AUTO_TEST_CASE(waste_test)
940941

941942
{
942943
// 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};
944945
add_coin(1 * COIN, 1, selection, fee, fee);
945946
add_coin(2 * COIN, 2, selection, fee, fee);
946947
selection.RecalculateWaste(min_viable_change, change_cost, change_fee);
@@ -949,7 +950,6 @@ BOOST_AUTO_TEST_CASE(waste_test)
949950

950951
{
951952
// Waste is 0 when fee == long_term_fee, no change, and no excess
952-
const CAmount exact_target{in_amt - fee * 2};
953953
SelectionResult selection{exact_target, SelectionAlgorithm::MANUAL};
954954
add_coin(1 * COIN, 1, selection, fee, fee);
955955
add_coin(2 * COIN, 2, selection, fee, fee);
@@ -968,7 +968,7 @@ BOOST_AUTO_TEST_CASE(waste_test)
968968

969969
{
970970
// 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};
972972
SelectionResult selection{new_target, SelectionAlgorithm::MANUAL};
973973
add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
974974
add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
@@ -978,7 +978,6 @@ BOOST_AUTO_TEST_CASE(waste_test)
978978

979979
{
980980
// 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};
982981
SelectionResult selection{exact_target, SelectionAlgorithm::MANUAL};
983982
const CAmount target_waste1{-2 * fee_diff}; // = (2 * fee) - (2 * (fee + fee_diff))
984983
add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
@@ -991,7 +990,11 @@ BOOST_AUTO_TEST_CASE(waste_test)
991990
// Negative waste when the long term fee is greater than the current fee and change_cost < - (inputs * (fee - long_term_fee))
992991
SelectionResult selection{target, SelectionAlgorithm::MANUAL};
993992
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);
995998
add_coin(1 * COIN, 1, selection, fee, fee + large_fee_diff);
996999
add_coin(2 * COIN, 2, selection, fee, fee + large_fee_diff);
9971000
selection.RecalculateWaste(min_viable_change, change_cost, change_fee);

0 commit comments

Comments
 (0)