Skip to content

Commit b3df0ca

Browse files
committed
tests: Test GetSelectionWaste
Tests for some waste calculation scenarios add_coin is modified to allow fee and long term fee to be set for an added CInputCoin.
1 parent 4f5ad43 commit b3df0ca

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

src/wallet/test/coinselector_tests.cpp

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@ static void add_coin(const CAmount& nValue, int nInput, std::vector<CInputCoin>&
4949
set.emplace_back(MakeTransactionRef(tx), nInput);
5050
}
5151

52-
static void add_coin(const CAmount& nValue, int nInput, CoinSet& set)
52+
static void add_coin(const CAmount& nValue, int nInput, CoinSet& set, CAmount fee = 0, CAmount long_term_fee = 0)
5353
{
5454
CMutableTransaction tx;
5555
tx.vout.resize(nInput + 1);
5656
tx.vout[nInput].nValue = nValue;
57-
set.emplace(MakeTransactionRef(tx), nInput);
57+
CInputCoin coin(MakeTransactionRef(tx), nInput);
58+
coin.effective_value = nValue - fee;
59+
coin.m_fee = fee;
60+
coin.m_long_term_fee = long_term_fee;
61+
set.insert(coin);
5862
}
5963

6064
static void add_coin(CWallet& wallet, const CAmount& nValue, int nAge = 6*24, bool fIsFromMe = false, int nInput=0, bool spendable = false)
@@ -658,4 +662,73 @@ BOOST_AUTO_TEST_CASE(SelectCoins_test)
658662
}
659663
}
660664

665+
BOOST_AUTO_TEST_CASE(waste_test)
666+
{
667+
CoinSet selection;
668+
const CAmount fee{100};
669+
const CAmount change_cost{125};
670+
const CAmount fee_diff{40};
671+
const CAmount in_amt{3 * COIN};
672+
const CAmount target{2 * COIN};
673+
const CAmount excess{in_amt - fee * 2 - target};
674+
675+
// Waste with change is the change cost and difference between fee and long term fee
676+
add_coin(1 * COIN, 1, selection, fee, fee - fee_diff);
677+
add_coin(2 * COIN, 2, selection, fee, fee - fee_diff);
678+
const CAmount waste1 = GetSelectionWaste(selection, change_cost, target);
679+
BOOST_CHECK_EQUAL(fee_diff * 2 + change_cost, waste1);
680+
selection.clear();
681+
682+
// Waste without change is the excess and difference between fee and long term fee
683+
add_coin(1 * COIN, 1, selection, fee, fee - fee_diff);
684+
add_coin(2 * COIN, 2, selection, fee, fee - fee_diff);
685+
const CAmount waste_nochange1 = GetSelectionWaste(selection, 0, target);
686+
BOOST_CHECK_EQUAL(fee_diff * 2 + excess, waste_nochange1);
687+
selection.clear();
688+
689+
// Waste with change and fee == long term fee is just cost of change
690+
add_coin(1 * COIN, 1, selection, fee, fee);
691+
add_coin(2 * COIN, 2, selection, fee, fee);
692+
BOOST_CHECK_EQUAL(change_cost, GetSelectionWaste(selection, change_cost, target));
693+
selection.clear();
694+
695+
// Waste without change and fee == long term fee is just the excess
696+
add_coin(1 * COIN, 1, selection, fee, fee);
697+
add_coin(2 * COIN, 2, selection, fee, fee);
698+
BOOST_CHECK_EQUAL(excess, GetSelectionWaste(selection, 0, target));
699+
selection.clear();
700+
701+
// Waste will be greater when fee is greater, but long term fee is the same
702+
add_coin(1 * COIN, 1, selection, fee * 2, fee - fee_diff);
703+
add_coin(2 * COIN, 2, selection, fee * 2, fee - fee_diff);
704+
const CAmount waste2 = GetSelectionWaste(selection, change_cost, target);
705+
BOOST_CHECK_GT(waste2, waste1);
706+
selection.clear();
707+
708+
// Waste with change is the change cost and difference between fee and long term fee
709+
// With long term fee greater than fee, waste should be less than when long term fee is less than fee
710+
add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
711+
add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
712+
const CAmount waste3 = GetSelectionWaste(selection, change_cost, target);
713+
BOOST_CHECK_EQUAL(fee_diff * -2 + change_cost, waste3);
714+
BOOST_CHECK_LT(waste3, waste1);
715+
selection.clear();
716+
717+
// Waste without change is the excess and difference between fee and long term fee
718+
// With long term fee greater than fee, waste should be less than when long term fee is less than fee
719+
add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
720+
add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
721+
const CAmount waste_nochange2 = GetSelectionWaste(selection, 0, target);
722+
BOOST_CHECK_EQUAL(fee_diff * -2 + excess, waste_nochange2);
723+
BOOST_CHECK_LT(waste_nochange2, waste_nochange1);
724+
selection.clear();
725+
726+
// 0 Waste only when fee == long term fee, no change, and no excess
727+
add_coin(1 * COIN, 1, selection, fee, fee);
728+
add_coin(2 * COIN, 2, selection, fee, fee);
729+
const CAmount exact_target = in_amt - 2 * fee;
730+
BOOST_CHECK_EQUAL(0, GetSelectionWaste(selection, 0, exact_target));
731+
732+
}
733+
661734
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)