mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
opt: Skip over barren combinations of tiny UTXOs
Given a lot of small amount UTXOs it is possible that the lookahead indicates sufficient funds, but any combination of them would push us beyond the current best_weight. We can estimate a lower bound for the minimal necessary weight to reach target from the maximal amount and minimal weight in the tail of the UTXO pool: if adding a number of hypothetical UTXOs of this maximum amount and minimum weight would not be able to beat `best_weight`, we can SHIFT to the omission branch, and CUT if the last selected UTXO is not heavier than the minimum weight of the remainder.
This commit is contained in:
parent
b7672c7cdd
commit
13161ecf03
2 changed files with 14 additions and 8 deletions
|
@ -454,6 +454,13 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
|
||||||
best_selection_weight = curr_weight;
|
best_selection_weight = curr_weight;
|
||||||
best_selection_amount = curr_amount;
|
best_selection_amount = curr_amount;
|
||||||
}
|
}
|
||||||
|
} else if (!best_selection.empty() && curr_weight + int64_t{min_tail_weight[curr_tail]} * ((total_target - curr_amount + utxo_pool[curr_tail].GetSelectionAmount() - 1) / utxo_pool[curr_tail].GetSelectionAmount()) > best_selection_weight) {
|
||||||
|
// Compare minimal tail weight and last selected amount with the amount missing to gauge whether a better weight is still possible.
|
||||||
|
if (utxo_pool[curr_tail].m_weight <= min_tail_weight[curr_tail]) {
|
||||||
|
should_cut = true;
|
||||||
|
} else {
|
||||||
|
should_shift = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (curr_try >= TOTAL_TRIES) {
|
if (curr_try >= TOTAL_TRIES) {
|
||||||
|
|
|
@ -1111,7 +1111,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
|
||||||
// 4) Test that two less valuable UTXOs with a combined lower weight are preferred over a more valuable heavier UTXO
|
// 4) Test that two less valuable UTXOs with a combined lower weight are preferred over a more valuable heavier UTXO
|
||||||
// 5) Test finding a solution in a UTXO pool with mixed weights
|
// 5) Test finding a solution in a UTXO pool with mixed weights
|
||||||
// 6) Test that the lightest solution among many clones is found
|
// 6) Test that the lightest solution among many clones is found
|
||||||
// 7) Lots of tiny UTXOs of different amounts quickly exhausts the search attempts
|
// 7) Test that lots of tiny UTXOs can be skipped if they are too heavy while there are enough funds in lookahead
|
||||||
|
|
||||||
FastRandomContext rand;
|
FastRandomContext rand;
|
||||||
CoinSelectionParams dummy_params{ // Only used to provide the 'avoid_partial' flag.
|
CoinSelectionParams dummy_params{ // Only used to provide the 'avoid_partial' flag.
|
||||||
|
@ -1180,7 +1180,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
|
||||||
});
|
});
|
||||||
BOOST_CHECK(res);
|
BOOST_CHECK(res);
|
||||||
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
||||||
size_t expected_attempts = 184;
|
size_t expected_attempts = 37;
|
||||||
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1231,7 +1231,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
|
||||||
add_coin(4 * COIN, 3, expected_result);
|
add_coin(4 * COIN, 3, expected_result);
|
||||||
BOOST_CHECK(EquivalentResult(expected_result, *res));
|
BOOST_CHECK(EquivalentResult(expected_result, *res));
|
||||||
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
||||||
size_t expected_attempts = 218;
|
size_t expected_attempts = 92;
|
||||||
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1270,14 +1270,13 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
|
||||||
add_coin(1 * COIN, 0, expected_result);
|
add_coin(1 * COIN, 0, expected_result);
|
||||||
BOOST_CHECK(EquivalentResult(expected_result, *res));
|
BOOST_CHECK(EquivalentResult(expected_result, *res));
|
||||||
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
||||||
// If this takes more attempts, the implementation has regressed
|
size_t expected_attempts = 38;
|
||||||
size_t expected_attempts = 42;
|
|
||||||
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
// #################################################################################################################
|
// #################################################################################################################
|
||||||
// 7) Lots of tiny UTXOs of different amounts quickly exhausts the search attempts
|
// 7) Test that lots of tiny UTXOs can be skipped if they are too heavy while there are enough funds in lookahead
|
||||||
// #################################################################################################################
|
// #################################################################################################################
|
||||||
CAmount target = 1.9L * COIN;
|
CAmount target = 1.9L * COIN;
|
||||||
int max_weight = 40000; // WU
|
int max_weight = 40000; // WU
|
||||||
|
@ -1293,11 +1292,11 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
|
||||||
return available_coins;
|
return available_coins;
|
||||||
});
|
});
|
||||||
SelectionResult expected_result(CAmount(0), SelectionAlgorithm::CG);
|
SelectionResult expected_result(CAmount(0), SelectionAlgorithm::CG);
|
||||||
add_coin(1.8 * COIN, 1, expected_result);
|
add_coin(1 * COIN, 1, expected_result);
|
||||||
add_coin(1 * COIN, 2, expected_result);
|
add_coin(1 * COIN, 2, expected_result);
|
||||||
BOOST_CHECK(EquivalentResult(expected_result, *res));
|
BOOST_CHECK(EquivalentResult(expected_result, *res));
|
||||||
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
|
||||||
size_t expected_attempts = 100'000;
|
size_t expected_attempts = 7;
|
||||||
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue