10#include <boost/test/unit_test.hpp>
34 dcsp.m_change_fee = dcsp.m_effective_feerate.
GetFee(dcsp.change_output_size);
35 dcsp.min_viable_change = dcsp.m_discard_feerate.GetFee(dcsp.change_spend_size);
36 dcsp.m_cost_of_change = dcsp.min_viable_change + dcsp.m_change_fee;
37 dcsp.m_subtract_fee_outputs =
false;
49 CAmount fees = cs_params.m_effective_feerate.GetFee(custom_spending_vsize);
50 tx.
vout[0].nValue = amount + int(is_eff_value) * fees;
53 group.Insert(std::make_shared<COutput>(
COutPoint(tx.
GetHash(), 0), tx.
vout.at(0), 1, custom_spending_vsize,
true,
true,
true, 0,
false, fees), 0, 0);
61 utxo_pool.push_back(
MakeCoin(c,
true, cs_params));
67 for (
int i = 0 ; i <
count; ++i) {
68 utxo_pool.push_back(
MakeCoin(amount,
true, cs_params));
76 std::vector<CAmount> a_amts;
77 std::vector<CAmount> b_amts;
79 a_amts.push_back(coin->txout.nValue);
82 b_amts.push_back(coin->txout.nValue);
84 std::sort(a_amts.begin(), a_amts.end());
85 std::sort(b_amts.begin(), b_amts.end());
87 auto ret = std::mismatch(a_amts.begin(), a_amts.end(), b_amts.begin());
88 return ret.first == a_amts.end() &&
ret.second == b_amts.end();
93 return "[" +
util::Join(selection.
GetInputSet(),
" ", [](
const auto& input){ return util::ToString(input->txout.nValue);}) +
"]";
100 for (
CAmount input_amount : expected_input_amounts) {
102 expected_amount +=
group.m_value;
107 BOOST_CHECK_MESSAGE(result,
"Falsy result in BnB-Success: " + test_title);
109 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()));
112static void TestBnBFail(std::string test_title, std::vector<OutputGroup>& utxo_pool,
const CAmount& selection_target)
119 std::vector<int> feerates = {0, 1, 5'000, 10'000, 25'000, 59'764, 500'000, 999'000, 1'500'000};
121 for (
int feerate : feerates) {
122 std::vector<OutputGroup> utxo_pool;
147 TestBnBFail(
"No UTXO combination in target window", utxo_pool, 7 *
CENT);
151 std::vector<OutputGroup> clone_pool;
172 std::vector<OutputGroup> doppelganger_pool;
173 std::vector<CAmount> doppelgangers;
174 std::vector<CAmount> expected_inputs;
175 for (
int i = 0; i < 17; ++i) {
178 doppelgangers.push_back(1 *
CENT + i);
179 expected_inputs.push_back(doppelgangers[i]);
185 AddCoins(doppelganger_pool, doppelgangers, cs_params);
187 TestBnBSuccess(
"Combine smallest 8 of 17 unique UTXOs", doppelganger_pool, 8 *
CENT, expected_inputs, cs_params);
191 TestBnBFail(
"Exhaust looking for smallest 8 of 18 unique UTXOs", doppelganger_pool, 8 *
CENT);
198 std::vector<OutputGroup> low_feerate_pool;
203 high_feerate_params.m_effective_feerate =
CFeeRate{25'000};
204 std::vector<OutputGroup> high_feerate_pool;
206 TestBnBSuccess(
"Select one input at high feerates", high_feerate_pool, 10 *
CENT, {10 *
CENT}, high_feerate_params);
213 high_feerate_pool.push_back(
MakeCoin(6 *
CENT,
true, high_feerate_params, 500));
214 high_feerate_pool.push_back(
MakeCoin(7 *
CENT,
true, high_feerate_params, 500));
215 TestBnBSuccess(
"Prefer two light inputs over two heavy inputs at high feerates", high_feerate_pool, 13 *
CENT, {3 *
CENT, 10 *
CENT}, high_feerate_params);
int64_t CAmount
Amount in satoshis (Can be negative)
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
CAmount GetFee(uint32_t num_bytes) const
Return the fee in satoshis for the given vsize in vbytes.
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()
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
static OutputGroup MakeCoin(const CAmount &amount, bool is_eff_value=true, CoinSelectionParams cs_params=default_cs_params, int custom_spending_vsize=68)
Make one OutputGroup with a single UTXO that either has a given effective value (default) or a given ...
util::Result< SelectionResult > SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change, int max_selection_weight)
static bool HaveEquivalentValues(const SelectionResult &a, const SelectionResult &b)
Check if SelectionResult a is equivalent to SelectionResult b.
static void TestBnBFail(std::string test_title, std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target)
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 CoinSelectionParams init_default_params()
Default coin selection parameters (dcsp) allow us to only explicitly set parameters when a diverging ...
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, int custom_spending_vsize=68)
static FastRandomContext default_rand
static std::string InputAmountsToString(const SelectionResult &selection)
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.
CFeeRate m_effective_feerate
The targeted feerate of the transaction being built.
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.
const std::set< std::shared_ptr< COutput > > & GetInputSet() const
Get m_selected_inputs.
void AddInput(const OutputGroup &group)