Skip to content

Commit efcaefc

Browse files
test: Add remaining scenarios of 0 waste
There are two more cases where waste can be 0, when: - (Fee - LTF) == -Change Cost - (Fee - LTF) == -Excess Adding these two conditions explicitly in the unit test will help pin the behavior, also demonstrate waste calculation scenarios to new readers.
1 parent 053a5fc commit efcaefc

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/wallet/test/coinselector_tests.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,12 +724,25 @@ BOOST_AUTO_TEST_CASE(waste_test)
724724
BOOST_CHECK_LT(waste_nochange2, waste_nochange1);
725725
selection.clear();
726726

727-
// 0 Waste only when fee == long term fee, no change, and no excess
727+
// No Waste when fee == long_term_fee, no change, and no excess
728728
add_coin(1 * COIN, 1, selection, fee, fee);
729729
add_coin(2 * COIN, 2, selection, fee, fee);
730-
const CAmount exact_target = in_amt - 2 * fee;
731-
BOOST_CHECK_EQUAL(0, GetSelectionWaste(selection, 0, exact_target));
730+
const CAmount exact_target{in_amt - fee * 2};
731+
BOOST_CHECK_EQUAL(0, GetSelectionWaste(selection, /* change_cost */ 0, exact_target));
732+
selection.clear();
732733

734+
// No Waste when (fee - long_term_fee) == (-cost_of_change), and no excess
735+
const CAmount new_change_cost{fee_diff * 2};
736+
add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
737+
add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
738+
BOOST_CHECK_EQUAL(0, GetSelectionWaste(selection, new_change_cost, target));
739+
selection.clear();
740+
741+
// No Waste when (fee - long_term_fee) == (-excess), no change cost
742+
const CAmount new_target{in_amt - fee * 2 - fee_diff * 2};
743+
add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
744+
add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
745+
BOOST_CHECK_EQUAL(0, GetSelectionWaste(selection, /* change cost */ 0, new_target));
733746
}
734747

735748
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)