10 #include <boost/test/unit_test.hpp> 31 /*effective_feerate=*/CFeeRate(5000), 32 /*long_term_feerate=*/CFeeRate(10'000),
37 dcsp.m_change_fee = dcsp.m_effective_feerate.GetFee(dcsp.change_output_size);
38 dcsp.min_viable_change = dcsp.m_discard_feerate.GetFee(dcsp.change_spend_size);
39 dcsp.m_cost_of_change = dcsp.min_viable_change + dcsp.m_change_fee;
40 dcsp.m_subtract_fee_outputs =
false;
52 CAmount fees = cs_params.m_effective_feerate.GetFee(custom_spending_vsize);
53 tx.
vout[0].nValue = amount + int(is_eff_value) * fees;
56 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);
64 utxo_pool.push_back(
MakeCoin(c,
true, cs_params));
70 for (
int i = 0 ; i <
count; ++i) {
71 utxo_pool.push_back(
MakeCoin(amount,
true, cs_params));
79 std::vector<CAmount> a_amts;
80 std::vector<CAmount> b_amts;
82 a_amts.push_back(coin->txout.nValue);
85 b_amts.push_back(coin->txout.nValue);
87 std::sort(a_amts.begin(), a_amts.end());
88 std::sort(b_amts.begin(), b_amts.end());
90 auto ret = std::mismatch(a_amts.begin(), a_amts.end(), b_amts.begin());
91 return ret.first == a_amts.end() &&
ret.second == b_amts.end();
103 for (
CAmount input_amount : expected_input_amounts) {
105 expected_amount +=
group.m_value;
110 BOOST_CHECK_MESSAGE(
result,
"Falsy result in BnB-Success: " + test_title);
112 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()));
115 static void TestBnBFail(std::string test_title, std::vector<OutputGroup>& utxo_pool,
const CAmount& selection_target,
int max_selection_weight =
MAX_STANDARD_TX_WEIGHT,
const bool expect_max_weight_exceeded =
false)
118 BOOST_CHECK_MESSAGE(!
result,
"BnB-Fail: " + test_title);
120 BOOST_CHECK(expect_max_weight_exceeded == max_weight_exceeded);
125 std::vector<int> feerates = {0, 1, 5
'000, 10'000, 25
'000, 59'764, 500
'000, 999'000, 1
'500'000};
127 for (
int feerate : feerates) {
128 std::vector<OutputGroup> utxo_pool;
157 TestBnBFail(
"No UTXO combination in target window", utxo_pool, 7 *
CENT);
161 std::vector<OutputGroup> clone_pool;
164 TestBnBSuccess("Skip equivalent input sets", clone_pool, /*selection_target=*/16 * CENT, /*expected_input_amounts=*/{2 * CENT, 7 * CENT, 7 * CENT}, cs_params); 166 /* Test BnB attempt limit (`TOTAL_TRIES`) 168 * Generally, on a diverse UTXO pool BnB will quickly pass over UTXOs bigger than the target and then start 169 * combining small counts of UTXOs that in sum remain under the selection_target+cost_of_change. When there are 170 * multiple UTXOs that have matching amount and cost, combinations with equivalent input sets are skipped. The 171 * UTXO pool for this test is specifically crafted to create as much branching as possible. The selection target 172 * is 8 CENT while all UTXOs are slightly bigger than 1 CENT. The smallest eight are 100,000…100,007 sats, while 173 * the larger nine are 100,368…100,375 (i.e., 100,008…100,016 sats plus cost_of_change (359 sats)). 175 * Because BnB will only select input sets that fall between selection_target and selection_target + 176 * cost_of_change, and the search traverses the UTXO pool from large to small amounts, the search will visit 177 * every single combination of eight inputs. All except the last combination will overshoot by more than 178 * cost_of_change on the eighth input, because the larger nine inputs each exceed 1 CENT by more than 179 * cost_of_change. Only the last combination consisting of the eight smallest UTXOs falls into the target 182 std::vector<OutputGroup> doppelganger_pool; 183 std::vector<CAmount> doppelgangers; 184 std::vector<CAmount> expected_inputs; 185 for (int i = 0; i < 17; ++i) { 187 // The eight smallest UTXOs can be combined to create expected_result 188 doppelgangers.push_back(1 * CENT + i); 189 expected_inputs.push_back(doppelgangers[i]); 191 // Any eight UTXOs including at least one UTXO with the added cost_of_change will exceed target window 192 doppelgangers.push_back(1 * CENT + default_cs_params.m_cost_of_change + i); 195 AddCoins(doppelganger_pool, doppelgangers, cs_params); 196 // Among up to 17 unique UTXOs of similar effective value we will find a solution composed of the eight smallest UTXOs 197 TestBnBSuccess("Combine smallest 8 of 17 unique UTXOs", doppelganger_pool, /*selection_target=*/8 * CENT, /*expected_input_amounts=*/expected_inputs, cs_params); 199 // Starting with 18 unique UTXOs of similar effective value we will not find the solution due to exceeding the attempt limit 200 AddCoins(doppelganger_pool, {1 * CENT + default_cs_params.m_cost_of_change + 17}, cs_params); 201 TestBnBFail("Exhaust looking for smallest 8 of 18 unique UTXOs", doppelganger_pool, /*selection_target=*/8 * CENT); 205 BOOST_AUTO_TEST_CASE(bnb_feerate_sensitivity_test) 207 // Create sets of UTXOs with the same effective amounts at different feerates (but different absolute amounts) 208 std::vector<OutputGroup> low_feerate_pool; // 5 sat/vB (default, and lower than long_term_feerate of 10 sat/vB) 209 AddCoins(low_feerate_pool, {2 * CENT, 3 * CENT, 5 * CENT, 10 * CENT}); 210 TestBnBSuccess("Select many inputs at low feerates", low_feerate_pool, /*selection_target=*/10 * CENT, /*expected_input_amounts=*/{2 * CENT, 3 * CENT, 5 * CENT}); 212 CoinSelectionParams high_feerate_params = init_default_params(); 213 high_feerate_params.m_effective_feerate = CFeeRate{25'000};
214 std::vector<OutputGroup> high_feerate_pool;
216 TestBnBSuccess(
"Select one input at high feerates", high_feerate_pool, 10 *
CENT, {10 *
CENT}, high_feerate_params);
223 high_feerate_pool.push_back(
MakeCoin(6 *
CENT,
true, high_feerate_params, 500));
224 high_feerate_pool.push_back(
MakeCoin(7 *
CENT,
true, high_feerate_params, 500));
225 TestBnBSuccess(
"Prefer two light inputs over two heavy inputs at high feerates", high_feerate_pool, 13 *
CENT, {3 *
CENT, 10 *
CENT}, high_feerate_params);
static void TestBnBFail(std::string test_title, std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, int max_selection_weight=MAX_STANDARD_TX_WEIGHT, const bool expect_max_weight_exceeded=false)
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 const CoinSelectionParams default_cs_params
const OutputSet & GetInputSet() const
Get m_selected_inputs.
static bool HaveEquivalentValues(const SelectionResult &a, const SelectionResult &b)
Check if SelectionResult a is equivalent to SelectionResult b.
static const int P2WPKH_OUTPUT_VSIZE
static std::string InputAmountsToString(const SelectionResult &selection)
int64_t CAmount
Amount in satoshis (Can be negative)
CFeeRate m_effective_feerate
The targeted feerate of the transaction being built.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
CAmount m_cost_of_change
Cost of creating the change output + cost of spending the change output in the future.
A group of UTXOs paid to the same output script.
Txid GetHash() const
Compute the hash of this CMutableTransaction.
static int next_lock_time
An outpoint - a combination of a transaction hash and an index n into its vout.
std::vector< CTxOut > vout
Parameters for one iteration of Coin Selection.
util::Result< SelectionResult > SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change, int max_selection_weight)
static constexpr int32_t MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we're willing to relay/mine.
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.
BOOST_AUTO_TEST_CASE(bnb_test)
static FastRandomContext default_rand
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.
static CoinSelectionParams init_default_params()
Default coin selection parameters (dcsp) allow us to only explicitly set parameters when a diverging ...
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac...
bilingual_str ErrorString(const Result< T > &result)
A mutable version of CTransaction.
static const int P2WPKH_INPUT_VSIZE
static constexpr CAmount CENT
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Testing setup that configures a complete environment.
std::string ToString(const T &t)
Locale-independent version of std::to_string.
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)
#define BOOST_CHECK(expr)