Skip to content

Commit

Permalink
[test] Recreate BnB iteration exhaustion test
Browse files Browse the repository at this point in the history
  • Loading branch information
murchandamus committed Jun 27, 2024
1 parent 81b95a3 commit 504873c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
22 changes: 22 additions & 0 deletions src/wallet/test/coinselection_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ BOOST_AUTO_TEST_CASE(bnb_test)
AddCoins(clone_pool, {2 * CENT, 7 * CENT, 7 * CENT});
AddDuplicateCoins(clone_pool, 50'000, 5 * CENT);
TestBnBSuccess("Skip equivalent input sets", clone_pool, /*selection_target=*/ 16 * CENT, /*expected_input_amounts=*/ {2 * CENT, 7 * CENT, 7 * CENT});

// Test BnB attempt limit
std::vector<OutputGroup> doppelganger_pool;
std::vector<CAmount> doppelgangers;
std::vector<CAmount> expected_inputs;
for (int i = 0; i < 17; ++i) {
if (i < 8) {
// 8 smallest UTXOs can be combined to create expected_result
doppelgangers.push_back(1 * CENT + i);
expected_inputs.push_back(doppelgangers[i]);
} else {
// Any 8 UTXOs including at least one UTXO with the added cost_of_change will exceed target window
doppelgangers.push_back(1 * CENT + default_cs_params.m_cost_of_change + i);
}
}
AddCoins(doppelganger_pool, doppelgangers);
// Among up to 17 unique UTXOs of similar effective value we will find a solution composed of the eight smallest
TestBnBSuccess("Combine smallest 8 of 17 unique UTXOs", doppelganger_pool, /*selection_target=*/ 8 * CENT, /*expected_input_amounts=*/ expected_inputs);

// Starting with 18 unique UTXOs of similar effective value we will not find the solution
AddCoins(doppelganger_pool, {1 * CENT + default_cs_params.m_cost_of_change + 17});
TestBnBFail("Exhaust looking for smallest 8 of 18 unique UTXOs", doppelganger_pool, /*selection_target=*/ 8 * CENT);
}

BOOST_AUTO_TEST_CASE(bnb_feerate_sensitivity_test)
Expand Down
32 changes: 0 additions & 32 deletions src/wallet/test/coinselector_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ static const CoinEligibilityFilter filter_confirmed(1, 1, 0);
static const CoinEligibilityFilter filter_standard_extra(6, 6, 0);
static int nextLockTime = 0;

static void add_coin(const CAmount& nValue, int nInput, std::vector<COutput>& set)
{
CMutableTransaction tx;
tx.vout.resize(nInput + 1);
tx.vout[nInput].nValue = nValue;
tx.nLockTime = nextLockTime++; // so all transactions get different hashes
set.emplace_back(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, /*input_bytes=*/ -1, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, /*fees=*/ 0);
}

static void add_coin(const CAmount& nValue, int nInput, SelectionResult& result)
{
CMutableTransaction tx;
Expand Down Expand Up @@ -133,18 +124,6 @@ static bool EqualResult(const SelectionResult& a, const SelectionResult& b)
return ret.first == a.GetInputSet().end() && ret.second == b.GetInputSet().end();
}

static CAmount make_hard_case(int utxos, std::vector<COutput>& utxo_pool)
{
utxo_pool.clear();
CAmount target = 0;
for (int i = 0; i < utxos; ++i) {
target += CAmount{1} << (utxos+i);
add_coin(CAmount{1} << (utxos+i), 2*i, utxo_pool);
add_coin((CAmount{1} << (utxos+i)) + (CAmount{1} << (utxos-1-i)), 2*i + 1, utxo_pool);
}
return target;
}

inline std::vector<OutputGroup>& GroupCoins(const std::vector<COutput>& available_coins, bool subtract_fee_outputs = false)
{
static std::vector<OutputGroup> static_groups;
Expand Down Expand Up @@ -195,17 +174,6 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
std::vector<COutput> utxo_pool;
SelectionResult expected_result(CAmount(0), SelectionAlgorithm::BNB);

/////////////////////////
// Known Outcome tests //
/////////////////////////

// Iteration exhaustion test
CAmount target = make_hard_case(17, utxo_pool);
BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), target, 1)); // Should exhaust
target = make_hard_case(14, utxo_pool);
const auto result7 = SelectCoinsBnB(GroupCoins(utxo_pool), target, 1); // Should not exhaust
BOOST_CHECK(result7);

////////////////////
// Behavior tests //
////////////////////
Expand Down

0 comments on commit 504873c

Please sign in to comment.