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 Oct 31, 2024
1 parent 8bf2b22 commit 280e65d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
35 changes: 35 additions & 0 deletions src/wallet/test/coinselection_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,41 @@ 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 (`TOTAL_TRIES`)
*
* Generally, on a diverse UTXO pool BnB will quickly pass over UTXOs bigger than the target and then start
* combining small counts of UTXOs that in sum remain under the selection_target+cost_of_change. When there are
* multiple UTXOs that have matching amount and cost, combinations with equivalent input sets are skipped. The UTXO
* pool for this test is specifically crafted to create as much branching as possible. The selection target is
* 8 CENT while all UTXOs are slightly bigger than 1 CENT. The smallest eight are 100,000…100,007 sats, while the larger
* ten are 100,009,…,100,018 sats plus cost_of_change.
*
* Because BnB will only select input sets that fall between selection_target and selection_target + cost_of_change,
* and the search traverses the UTXO pool from large amount to small amount, the search will traverse every single
* combination of eight inputs, but all except the last will overshoot by more than cost_of_change on the eighth
* input. Only the very last combination with the eight smallest UTXOs falls into the target window.
*/
std::vector<OutputGroup> doppelganger_pool;
std::vector<CAmount> doppelgangers;
std::vector<CAmount> expected_inputs;
for (int i = 0; i < 17; ++i) {
if (i < 8) {
// The eight smallest UTXOs can be combined to create expected_result
doppelgangers.push_back(1 * CENT + i);
expected_inputs.push_back(doppelgangers[i]);
} else {
// Any eight 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 UTXOs
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 due to exceeding the attempt limit
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 280e65d

Please sign in to comment.