10#include <boost/test/unit_test.hpp>
34static const
std::vector<
int>
FEERATES = {0, 1, 99, 100, 315, 1'000, 2'345, 10'292, 59'764, 1'500'000};
53 csp.m_change_fee = csp.m_effective_feerate.GetFee(csp.change_output_size);
54 csp.min_viable_change = csp.m_discard_feerate.GetFee(csp.change_spend_size);
55 csp.m_cost_of_change = csp.min_viable_change + csp.m_change_fee;
56 csp.m_subtract_fee_outputs =
false;
68 CAmount fees = cs_params.m_effective_feerate.GetFee(custom_spending_vsize);
69 tx.
vout[0].nValue = amount + int(is_eff_value) * fees;
72 group.Insert(std::make_shared<COutput>(
COutPoint(tx.
GetHash(), 0), tx.
vout.at(0), 1, custom_spending_vsize,
true,
true, 0,
false, fees), 0, 0);
80 utxo_pool.push_back(
MakeCoin(c,
true, cs_params));
86 for (
int i = 0 ; i <
count; ++i) {
87 utxo_pool.push_back(
MakeCoin(amount,
true, cs_params));
95 std::vector<CAmount> a_amts;
96 std::vector<CAmount> b_amts;
98 a_amts.push_back(coin->txout.nValue);
101 b_amts.push_back(coin->txout.nValue);
103 std::sort(a_amts.begin(), a_amts.end());
104 std::sort(b_amts.begin(), b_amts.end());
106 auto ret = std::mismatch(a_amts.begin(), a_amts.end(), b_amts.begin());
107 return ret.first == a_amts.end() &&
ret.second == b_amts.end();
112 return "[" +
util::Join(selection.
GetInputSet(),
" ", [](
const auto& input){ return util::ToString(input->txout.nValue);}) +
"]";
119 for (
CAmount input_amount : expected_input_amounts) {
121 expected_amount +=
group.m_value;
125 const auto result =
SelectCoinsBnB(utxo_pool, selection_target, cs_params.m_cost_of_change, max_selection_weight);
126 BOOST_CHECK_MESSAGE(result,
"Falsy result in BnB-Success: " + test_title);
128 BOOST_CHECK_MESSAGE(result->GetSelectedValue() == expected_amount,
strprintf(
"Selected amount mismatch in BnB-Success: %s. Expected %d, but got %d", test_title, expected_amount, result->GetSelectedValue()));
129 BOOST_CHECK_MESSAGE(result->GetWeight() <= max_selection_weight,
strprintf(
"Selected weight is higher than permitted in BnB-Success: %s. Expected %d, but got %d", test_title, max_selection_weight, result->GetWeight()));
134 const auto result =
SelectCoinsBnB(utxo_pool, selection_target, cs_params.m_cost_of_change, max_selection_weight);
135 BOOST_CHECK_MESSAGE(!result,
"BnB-Fail: " + test_title);
136 bool max_weight_exceeded =
util::ErrorString(result).
original.find(
"The inputs size exceeds the maximum weight") != std::string::npos;
137 BOOST_CHECK(expect_max_weight_exceeded == max_weight_exceeded);
143 std::vector<OutputGroup> utxo_pool;
169 TestBnBFail(
"Smallest combination too big", utxo_pool, 0.5 *
CENT, cs_params);
170 TestBnBFail(
"No UTXO combination in target window", utxo_pool, 7 *
CENT, cs_params);
171 TestBnBFail(
"Select more than available", utxo_pool, 10 *
CENT, cs_params);
174 std::vector<OutputGroup> clone_pool;
195 std::vector<OutputGroup> doppelganger_pool;
196 std::vector<CAmount> doppelgangers;
197 std::vector<CAmount> expected_inputs;
198 for (
int i = 0; i < 17; ++i) {
201 doppelgangers.push_back(1 *
CENT + i);
202 expected_inputs.push_back(doppelgangers[i]);
208 AddCoins(doppelganger_pool, doppelgangers, cs_params);
210 TestBnBSuccess(
"Combine smallest 8 of 17 unique UTXOs", doppelganger_pool, 8 *
CENT, expected_inputs, cs_params);
214 TestBnBFail(
"Exhaust looking for smallest 8 of 18 unique UTXOs", doppelganger_pool, 8 *
CENT, cs_params);
221 std::vector<OutputGroup> low_feerate_pool;
226 std::vector<OutputGroup> high_feerate_pool;
228 TestBnBSuccess(
"Select one input at high feerates", high_feerate_pool, 10 *
CENT, {10 *
CENT}, high_feerate_params);
235 high_feerate_pool.push_back(
MakeCoin(6 *
CENT,
true, high_feerate_params, 500));
236 high_feerate_pool.push_back(
MakeCoin(7 *
CENT,
true, high_feerate_params, 500));
237 TestBnBSuccess(
"Prefer two light inputs over two heavy inputs at high feerates", high_feerate_pool, 13 *
CENT, {3 *
CENT, 10 *
CENT}, high_feerate_params);
244 const auto result =
SelectCoinsSRD(utxo_pool, selection_target, cs_params.m_change_fee, cs_params.rng_fast, max_selection_weight);
245 BOOST_CHECK_MESSAGE(result,
"Falsy result in SRD-Success: " + test_title);
246 const CAmount selected_effective_value = result->GetSelectedEffectiveValue();
247 BOOST_CHECK_MESSAGE(selected_effective_value >= expected_min_amount,
strprintf(
"Selected effective value is lower than expected in SRD-Success: %s. Expected %d, but got %d", test_title, expected_min_amount, selected_effective_value));
248 BOOST_CHECK_MESSAGE(result->GetWeight() <= max_selection_weight,
strprintf(
"Selected weight is higher than permitted in SRD-Success: %s. Expected %d, but got %d", test_title, max_selection_weight, result->GetWeight()));
253 const auto result =
SelectCoinsSRD(utxo_pool, selection_target, cs_params.m_change_fee, cs_params.rng_fast, max_selection_weight);
254 BOOST_CHECK_MESSAGE(!result,
"SRD-Fail: " + test_title);
255 bool max_weight_exceeded =
util::ErrorString(result).
original.find(
"The inputs size exceeds the maximum weight") != std::string::npos;
256 BOOST_CHECK(expect_max_weight_exceeded == max_weight_exceeded);
262 std::vector<OutputGroup> utxo_pool;
272 TestSRDSuccess(
"Select 3.125 CENT", utxo_pool, 3'125'000, cs_params);
279 TestSRDFail(
"Spend more than available", utxo_pool, 9 *
CENT + 1, cs_params);
int64_t CAmount
Amount in satoshis (Can be negative)
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
An outpoint - a combination of a transaction hash and an index n into its vout.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
bilingual_str ErrorString(const Result< T > &result)
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
static constexpr CAmount CHANGE_LOWER
lower bound for randomly-chosen target change amount
util::Result< SelectionResult > SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change, int max_selection_weight)
static void TestBnBFail(std::string test_title, std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CoinSelectionParams &cs_params=default_cs_params, int max_selection_weight=MAX_STANDARD_TX_WEIGHT, const bool expect_max_weight_exceeded=false)
static CoinSelectionParams init_cs_params(int eff_feerate=5000)
Default coin selection parameters allow us to only explicitly set parameters when a diverging value i...
static const int P2WPKH_OUTPUT_VSIZE
static bool HaveEquivalentValues(const SelectionResult &a, const SelectionResult &b)
Check if SelectionResult a is equivalent to SelectionResult b.
static const int P2WPKH_INPUT_VSIZE
static void TestSRDFail(std::string test_title, std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CoinSelectionParams &cs_params=default_cs_params, int max_selection_weight=MAX_STANDARD_TX_WEIGHT, const bool expect_max_weight_exceeded=false)
static void AddDuplicateCoins(std::vector< OutputGroup > &utxo_pool, int count, int amount, CoinSelectionParams cs_params=default_cs_params)
Make multiple coins that share the same effective value.
BOOST_AUTO_TEST_CASE(bnb_test)
static int next_lock_time
static const CoinSelectionParams default_cs_params
static void AddCoins(std::vector< OutputGroup > &utxo_pool, std::vector< CAmount > coins, CoinSelectionParams cs_params=default_cs_params)
Make multiple OutputGroups with the given values as their effective value.
static void TestBnBSuccess(std::string test_title, std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const std::vector< CAmount > &expected_input_amounts, const CoinSelectionParams &cs_params=default_cs_params, const int custom_spending_vsize=P2WPKH_INPUT_VSIZE, const int max_selection_weight=MAX_STANDARD_TX_WEIGHT)
static OutputGroup MakeCoin(const CAmount &amount, bool is_eff_value=true, CoinSelectionParams cs_params=default_cs_params, int custom_spending_vsize=P2WPKH_INPUT_VSIZE)
Make one OutputGroup with a single UTXO that either has a given effective value (default) or a given ...
static FastRandomContext default_rand
static void TestSRDSuccess(std::string test_title, std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CoinSelectionParams &cs_params=default_cs_params, const int max_selection_weight=MAX_STANDARD_TX_WEIGHT)
static const std::vector< int > FEERATES
This set of feerates is used in the tests to test edge cases around the default minimum feerate and o...
util::Result< SelectionResult > SelectCoinsSRD(const std::vector< OutputGroup > &utxo_pool, CAmount target_value, CAmount change_fee, FastRandomContext &rng, int max_selection_weight)
Select coins by Single Random Draw (SRD).
static std::string InputAmountsToString(const SelectionResult &selection)
#define BOOST_CHECK(expr)
static constexpr int32_t MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we're willing to relay/mine.
static constexpr CAmount CENT
A mutable version of CTransaction.
std::vector< CTxOut > vout
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Testing setup that configures a complete environment.
Parameters for one iteration of Coin Selection.
CAmount m_cost_of_change
Cost of creating the change output + cost of spending the change output in the future.
CAmount m_change_fee
Cost of creating the change output.
A group of UTXOs paid to the same output script.
const OutputSet & GetInputSet() const
Get m_selected_inputs.