mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-17 21:32:00 +01:00
Merge ba82240553ddd534287845e10bc76b46b45329fe into 5f4422d68dc3530c353af1f87499de1c864b60ad
This commit is contained in:
commit
919a39d64b
@ -12,6 +12,7 @@
|
|||||||
#include <wallet/coinselection.h>
|
#include <wallet/coinselection.h>
|
||||||
|
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
|
#include <span>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace wallet {
|
namespace wallet {
|
||||||
@ -216,8 +217,14 @@ FUZZ_TARGET(coin_grinder_is_optimal)
|
|||||||
assert(!result_cg);
|
assert(!result_cg);
|
||||||
}
|
}
|
||||||
|
|
||||||
FUZZ_TARGET(coinselection)
|
enum class CoinSelectionAlgorithm {
|
||||||
{
|
BNB,
|
||||||
|
SRD,
|
||||||
|
KNAPSACK,
|
||||||
|
};
|
||||||
|
|
||||||
|
template<CoinSelectionAlgorithm Algorithm>
|
||||||
|
void FuzzCoinSelectionAlgorithm(std::span<const uint8_t> buffer) {
|
||||||
SeedRandomStateForTest(SeedRand::ZEROS);
|
SeedRandomStateForTest(SeedRand::ZEROS);
|
||||||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
||||||
std::vector<COutput> utxo_pool;
|
std::vector<COutput> utxo_pool;
|
||||||
@ -249,66 +256,72 @@ FUZZ_TARGET(coinselection)
|
|||||||
|
|
||||||
std::vector<OutputGroup> group_pos;
|
std::vector<OutputGroup> group_pos;
|
||||||
GroupCoins(fuzzed_data_provider, utxo_pool, coin_params, /*positive_only=*/true, group_pos);
|
GroupCoins(fuzzed_data_provider, utxo_pool, coin_params, /*positive_only=*/true, group_pos);
|
||||||
std::vector<OutputGroup> group_all;
|
|
||||||
GroupCoins(fuzzed_data_provider, utxo_pool, coin_params, /*positive_only=*/false, group_all);
|
|
||||||
|
|
||||||
for (const OutputGroup& group : group_all) {
|
|
||||||
const CoinEligibilityFilter filter{fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
|
|
||||||
(void)group.EligibleForSpending(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
int max_selection_weight = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max());
|
int max_selection_weight = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max());
|
||||||
|
|
||||||
// Run coinselection algorithms
|
std::optional<SelectionResult> result;
|
||||||
auto result_bnb = coin_params.m_subtract_fee_outputs ? util::Error{Untranslated("BnB disabled when SFFO is enabled")} :
|
|
||||||
SelectCoinsBnB(group_pos, target, coin_params.m_cost_of_change, max_selection_weight);
|
if constexpr (Algorithm == CoinSelectionAlgorithm::BNB) {
|
||||||
if (result_bnb) {
|
if (!coin_params.m_subtract_fee_outputs) {
|
||||||
assert(result_bnb->GetChange(coin_params.min_viable_change, coin_params.m_change_fee) == 0);
|
auto result_bnb = SelectCoinsBnB(group_pos, target, coin_params.m_cost_of_change, max_selection_weight);
|
||||||
assert(result_bnb->GetSelectedValue() >= target);
|
if (result_bnb) {
|
||||||
assert(result_bnb->GetWeight() <= max_selection_weight);
|
result = *result_bnb;
|
||||||
(void)result_bnb->GetShuffledInputVector();
|
assert(result_bnb->GetChange(coin_params.min_viable_change, coin_params.m_change_fee) == 0);
|
||||||
(void)result_bnb->GetInputSet();
|
assert(result_bnb->GetSelectedValue() >= target);
|
||||||
|
assert(result_bnb->GetWeight() <= max_selection_weight);
|
||||||
|
(void)result_bnb->GetShuffledInputVector();
|
||||||
|
(void)result_bnb->GetInputSet();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result_srd = SelectCoinsSRD(group_pos, target, coin_params.m_change_fee, fast_random_context, max_selection_weight);
|
if constexpr (Algorithm == CoinSelectionAlgorithm::SRD) {
|
||||||
if (result_srd) {
|
auto result_srd = SelectCoinsSRD(group_pos, target, coin_params.m_change_fee, fast_random_context, max_selection_weight);
|
||||||
assert(result_srd->GetSelectedValue() >= target);
|
if (result_srd) {
|
||||||
assert(result_srd->GetChange(CHANGE_LOWER, coin_params.m_change_fee) > 0); // Demonstrate that SRD creates change of at least CHANGE_LOWER
|
result = *result_srd;
|
||||||
assert(result_srd->GetWeight() <= max_selection_weight);
|
assert(result_srd->GetSelectedValue() >= target);
|
||||||
result_srd->RecalculateWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
|
assert(result_srd->GetChange(CHANGE_LOWER, coin_params.m_change_fee) > 0);
|
||||||
(void)result_srd->GetShuffledInputVector();
|
assert(result_srd->GetWeight() <= max_selection_weight);
|
||||||
(void)result_srd->GetInputSet();
|
result_srd->RecalculateWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
|
||||||
|
(void)result_srd->GetShuffledInputVector();
|
||||||
|
(void)result_srd->GetInputSet();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CAmount change_target{GenerateChangeTarget(target, coin_params.m_change_fee, fast_random_context)};
|
if constexpr (Algorithm == CoinSelectionAlgorithm::KNAPSACK) {
|
||||||
auto result_knapsack = KnapsackSolver(group_all, target, change_target, fast_random_context, max_selection_weight);
|
std::vector<OutputGroup> group_all;
|
||||||
if (result_knapsack) {
|
GroupCoins(fuzzed_data_provider, utxo_pool, coin_params, /*positive_only=*/false, group_all);
|
||||||
assert(result_knapsack->GetSelectedValue() >= target);
|
|
||||||
assert(result_knapsack->GetWeight() <= max_selection_weight);
|
|
||||||
result_knapsack->RecalculateWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
|
|
||||||
(void)result_knapsack->GetShuffledInputVector();
|
|
||||||
(void)result_knapsack->GetInputSet();
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the total balance is sufficient for the target and we are not using
|
for (const OutputGroup& group : group_all) {
|
||||||
// effective values, Knapsack should always find a solution (unless the selection exceeded the max tx weight).
|
const CoinEligibilityFilter filter{fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
|
||||||
if (total_balance >= target && subtract_fee_outputs && !HasErrorMsg(result_knapsack)) {
|
(void)group.EligibleForSpending(filter);
|
||||||
assert(result_knapsack);
|
}
|
||||||
|
|
||||||
|
CAmount change_target{GenerateChangeTarget(target, coin_params.m_change_fee, fast_random_context)};
|
||||||
|
auto result_knapsack = KnapsackSolver(group_all, target, change_target, fast_random_context, max_selection_weight);
|
||||||
|
// If the total balance is sufficient for the target and we are not using
|
||||||
|
// effective values, Knapsack should always find a solution (unless the selection exceeded the max tx weight).
|
||||||
|
if (total_balance >= target && subtract_fee_outputs && !HasErrorMsg(result_knapsack)) {
|
||||||
|
assert(result_knapsack);
|
||||||
|
}
|
||||||
|
if (result_knapsack) {
|
||||||
|
result = *result_knapsack;
|
||||||
|
assert(result_knapsack->GetSelectedValue() >= target);
|
||||||
|
assert(result_knapsack->GetWeight() <= max_selection_weight);
|
||||||
|
result_knapsack->RecalculateWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
|
||||||
|
(void)result_knapsack->GetShuffledInputVector();
|
||||||
|
(void)result_knapsack->GetInputSet();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<COutput> utxos;
|
std::vector<COutput> utxos;
|
||||||
std::vector<util::Result<SelectionResult>> results;
|
|
||||||
results.emplace_back(std::move(result_srd));
|
|
||||||
results.emplace_back(std::move(result_knapsack));
|
|
||||||
results.emplace_back(std::move(result_bnb));
|
|
||||||
CAmount new_total_balance{CreateCoins(fuzzed_data_provider, utxos, coin_params, next_locktime)};
|
CAmount new_total_balance{CreateCoins(fuzzed_data_provider, utxos, coin_params, next_locktime)};
|
||||||
if (new_total_balance > 0) {
|
if (new_total_balance > 0) {
|
||||||
std::set<std::shared_ptr<COutput>> new_utxo_pool;
|
std::set<std::shared_ptr<COutput>> new_utxo_pool;
|
||||||
for (const auto& utxo : utxos) {
|
for (const auto& utxo : utxos) {
|
||||||
new_utxo_pool.insert(std::make_shared<COutput>(utxo));
|
new_utxo_pool.insert(std::make_shared<COutput>(utxo));
|
||||||
}
|
}
|
||||||
for (auto& result : results) {
|
if (result) {
|
||||||
if (!result) continue;
|
|
||||||
const auto weight{result->GetWeight()};
|
const auto weight{result->GetWeight()};
|
||||||
result->AddInputs(new_utxo_pool, subtract_fee_outputs);
|
result->AddInputs(new_utxo_pool, subtract_fee_outputs);
|
||||||
assert(result->GetWeight() > weight);
|
assert(result->GetWeight() > weight);
|
||||||
@ -319,8 +332,7 @@ FUZZ_TARGET(coinselection)
|
|||||||
CAmount manual_balance{CreateCoins(fuzzed_data_provider, manual_inputs, coin_params, next_locktime)};
|
CAmount manual_balance{CreateCoins(fuzzed_data_provider, manual_inputs, coin_params, next_locktime)};
|
||||||
if (manual_balance == 0) return;
|
if (manual_balance == 0) return;
|
||||||
auto manual_selection{ManualSelection(manual_inputs, manual_balance, coin_params.m_subtract_fee_outputs)};
|
auto manual_selection{ManualSelection(manual_inputs, manual_balance, coin_params.m_subtract_fee_outputs)};
|
||||||
for (auto& result : results) {
|
if (result) {
|
||||||
if (!result) continue;
|
|
||||||
const CAmount old_target{result->GetTarget()};
|
const CAmount old_target{result->GetTarget()};
|
||||||
const std::set<std::shared_ptr<COutput>> input_set{result->GetInputSet()};
|
const std::set<std::shared_ptr<COutput>> input_set{result->GetInputSet()};
|
||||||
const int old_weight{result->GetWeight()};
|
const int old_weight{result->GetWeight()};
|
||||||
@ -331,4 +343,16 @@ FUZZ_TARGET(coinselection)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FUZZ_TARGET(coinselection_bnb) {
|
||||||
|
FuzzCoinSelectionAlgorithm<CoinSelectionAlgorithm::BNB>(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
FUZZ_TARGET(coinselection_srd) {
|
||||||
|
FuzzCoinSelectionAlgorithm<CoinSelectionAlgorithm::SRD>(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
FUZZ_TARGET(coinselection_knapsack) {
|
||||||
|
FuzzCoinSelectionAlgorithm<CoinSelectionAlgorithm::KNAPSACK>(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace wallet
|
} // namespace wallet
|
||||||
|
Loading…
x
Reference in New Issue
Block a user