test: Run simple tests at various feerates

This commit is contained in:
Murch 2025-04-28 20:36:55 -07:00
parent 7cb5caf726
commit 768305071d
No known key found for this signature in database
GPG key ID: 7BA035CA5B901713

View file

@ -63,9 +63,9 @@ static void AddCoins(std::vector<OutputGroup>& utxo_pool, std::vector<CAmount> c
}
/** Make multiple coins that share the same effective value */
static void AddDuplicateCoins(std::vector<OutputGroup>& utxo_pool, int count, int amount) {
static void AddDuplicateCoins(std::vector<OutputGroup>& utxo_pool, int count, int amount, CoinSelectionParams cs_params = default_cs_params) {
for (int i = 0 ; i < count; ++i) {
utxo_pool.push_back(MakeCoin(amount));
utxo_pool.push_back(MakeCoin(amount, true, cs_params));
}
}
@ -121,22 +121,28 @@ static void TestBnBFail(std::string test_title, std::vector<OutputGroup>& utxo_p
BOOST_AUTO_TEST_CASE(bnb_test)
{
std::vector<int> feerates = {0, 1, 5'000, 10'000, 25'000, 59'764, 500'000, 999'000, 1'500'000};
for (int feerate : feerates) {
std::vector<OutputGroup> utxo_pool;
CoinSelectionParams cs_params = init_default_params();
cs_params.m_effective_feerate = CFeeRate{feerate};
// Fail for empty UTXO pool
TestBnBFail("Empty UTXO pool", utxo_pool, /*selection_target=*/1 * CENT);
AddCoins(utxo_pool, {1 * CENT, 3 * CENT, 5 * CENT});
AddCoins(utxo_pool, {1 * CENT, 3 * CENT, 5 * CENT}, cs_params);
// Simple success cases
TestBnBSuccess("Select smallest UTXO", utxo_pool, /*selection_target=*/1 * CENT, /*expected_input_amounts=*/{1 * CENT});
TestBnBSuccess("Select middle UTXO", utxo_pool, /*selection_target=*/3 * CENT, /*expected_input_amounts=*/{3 * CENT});
TestBnBSuccess("Select biggest UTXO", utxo_pool, /*selection_target=*/5 * CENT, /*expected_input_amounts=*/{5 * CENT});
TestBnBSuccess("Select two UTXOs", utxo_pool, /*selection_target=*/4 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT});
TestBnBSuccess("Select all UTXOs", utxo_pool, /*selection_target=*/9 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT, 5 * CENT});
TestBnBSuccess("Select smallest UTXO", utxo_pool, /*selection_target=*/1 * CENT, /*expected_input_amounts=*/{1 * CENT}, cs_params);
TestBnBSuccess("Select middle UTXO", utxo_pool, /*selection_target=*/3 * CENT, /*expected_input_amounts=*/{3 * CENT}, cs_params);
TestBnBSuccess("Select biggest UTXO", utxo_pool, /*selection_target=*/5 * CENT, /*expected_input_amounts=*/{5 * CENT}, cs_params);
TestBnBSuccess("Select two UTXOs", utxo_pool, /*selection_target=*/4 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT}, cs_params);
TestBnBSuccess("Select all UTXOs", utxo_pool, /*selection_target=*/9 * CENT, /*expected_input_amounts=*/{1 * CENT, 3 * CENT, 5 * CENT}, cs_params);
// BnB finds changeless solution while overshooting by up to cost_of_change
TestBnBSuccess("Select upper bound", utxo_pool, /*selection_target=*/4 * CENT - default_cs_params.m_cost_of_change, /*expected_input_amounts=*/{1 * CENT, 3 * CENT});
TestBnBSuccess("Select upper bound", utxo_pool, /*selection_target=*/4 * CENT - default_cs_params.m_cost_of_change, /*expected_input_amounts=*/{1 * CENT, 3 * CENT}, cs_params);
// BnB fails to find changeless solution when overshooting by cost_of_change + 1 sat
TestBnBFail("Overshoot upper bound", utxo_pool, /*selection_target=*/4 * CENT - default_cs_params.m_cost_of_change - 1);
@ -148,9 +154,9 @@ BOOST_AUTO_TEST_CASE(bnb_test)
// Test skipping of equivalent input sets
std::vector<OutputGroup> clone_pool;
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});
AddCoins(clone_pool, {2 * CENT, 7 * CENT, 7 * CENT}, cs_params);
AddDuplicateCoins(clone_pool, 50'000, 5 * CENT, cs_params);
TestBnBSuccess("Skip equivalent input sets", clone_pool, /*selection_target=*/16 * CENT, /*expected_input_amounts=*/{2 * CENT, 7 * CENT, 7 * CENT}, cs_params);
/* Test BnB attempt limit (`TOTAL_TRIES`)
*
@ -179,14 +185,15 @@ BOOST_AUTO_TEST_CASE(bnb_test)
doppelgangers.push_back(1 * CENT + default_cs_params.m_cost_of_change + i);
}
}
AddCoins(doppelganger_pool, doppelgangers);
AddCoins(doppelganger_pool, doppelgangers, cs_params);
// 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);
TestBnBSuccess("Combine smallest 8 of 17 unique UTXOs", doppelganger_pool, /*selection_target=*/8 * CENT, /*expected_input_amounts=*/expected_inputs, cs_params);
// 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});
AddCoins(doppelganger_pool, {1 * CENT + default_cs_params.m_cost_of_change + 17}, cs_params);
TestBnBFail("Exhaust looking for smallest 8 of 18 unique UTXOs", doppelganger_pool, /*selection_target=*/8 * CENT);
}
}
BOOST_AUTO_TEST_CASE(bnb_feerate_sensitivity_test)
{