Skip to content

Commit a8bbd4c

Browse files
committed
Merge bitcoin/bitcoin#22938: test: Add remaining scenarios of 0 waste, in wallet waste_test
efcaefc test: Add remaining scenarios of 0 waste (rajarshimaitra) Pull request description: As per the [review club](https://bitcoincore.reviews/22009) discussion on #22009 , it was observed that there were other two fee scenarios in which selection waste could be zero. These are: - (LTF - Fee) == Change Cost - (LTF - Fee) == Excess Even though these are obvious by the definition of waste metric, adding tests for them can be helpful in explaining its behavior to new readers of the code base, along with pinning the behavior for future. This PR adds those two cases to waste calculation unit test. Also let me know if I am missing more scenarios. ACKs for top commit: jonatack: Tested re-ACK efcaefc achow101: ACK efcaefc meshcollider: ACK efcaefc Tree-SHA512: 13fe3e2c0ea7bb58d34e16c32908b84705130dec16382ff941e5e60ca5b379f9c5811b33f36c4c72d7a98cfbb6af2f196d0a69e96989afa4b9e49893eaadd7cb
2 parents 27836f2 + efcaefc commit a8bbd4c

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)