mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
wallet: track which coin selection algorithm produced a SelectionResult
This commit is contained in:
parent
b69fd5eaa9
commit
912f1ed181
4 changed files with 33 additions and 8 deletions
|
@ -64,7 +64,7 @@ static const size_t TOTAL_TRIES = 100000;
|
|||
|
||||
std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CAmount& cost_of_change)
|
||||
{
|
||||
SelectionResult result(selection_target);
|
||||
SelectionResult result(selection_target, SelectionAlgorithm::BNB);
|
||||
CAmount curr_value = 0;
|
||||
std::vector<size_t> curr_selection; // selected utxo indexes
|
||||
|
||||
|
@ -167,7 +167,7 @@ std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_poo
|
|||
|
||||
std::optional<SelectionResult> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value, FastRandomContext& rng)
|
||||
{
|
||||
SelectionResult result(target_value);
|
||||
SelectionResult result(target_value, SelectionAlgorithm::SRD);
|
||||
|
||||
std::vector<size_t> indexes;
|
||||
indexes.resize(utxo_pool.size());
|
||||
|
@ -249,7 +249,7 @@ static void ApproximateBestSubset(FastRandomContext& insecure_rand, const std::v
|
|||
std::optional<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups, const CAmount& nTargetValue,
|
||||
CAmount change_target, FastRandomContext& rng)
|
||||
{
|
||||
SelectionResult result(nTargetValue);
|
||||
SelectionResult result(nTargetValue, SelectionAlgorithm::KNAPSACK);
|
||||
|
||||
// List of values less than target
|
||||
std::optional<OutputGroup> lowest_larger;
|
||||
|
@ -460,4 +460,17 @@ std::string COutput::ToString() const
|
|||
{
|
||||
return strprintf("COutput(%s, %d, %d) [%s]", outpoint.hash.ToString(), outpoint.n, depth, FormatMoney(txout.nValue));
|
||||
}
|
||||
|
||||
std::string GetAlgorithmName(const SelectionAlgorithm algo)
|
||||
{
|
||||
switch (algo)
|
||||
{
|
||||
case SelectionAlgorithm::BNB: return "bnb";
|
||||
case SelectionAlgorithm::KNAPSACK: return "knapsack";
|
||||
case SelectionAlgorithm::SRD: return "srd";
|
||||
case SelectionAlgorithm::MANUAL: return "manual";
|
||||
// No default case to allow for compiler to warn
|
||||
}
|
||||
assert(false);
|
||||
}
|
||||
} // namespace wallet
|
||||
|
|
|
@ -239,6 +239,16 @@ struct OutputGroup
|
|||
*/
|
||||
[[nodiscard]] CAmount GenerateChangeTarget(CAmount payment_value, FastRandomContext& rng);
|
||||
|
||||
enum class SelectionAlgorithm : uint8_t
|
||||
{
|
||||
BNB = 0,
|
||||
KNAPSACK = 1,
|
||||
SRD = 2,
|
||||
MANUAL = 3,
|
||||
};
|
||||
|
||||
std::string GetAlgorithmName(const SelectionAlgorithm algo);
|
||||
|
||||
struct SelectionResult
|
||||
{
|
||||
private:
|
||||
|
@ -250,10 +260,12 @@ private:
|
|||
bool m_use_effective{false};
|
||||
/** The computed waste */
|
||||
std::optional<CAmount> m_waste;
|
||||
/** The algorithm used to produce this result */
|
||||
SelectionAlgorithm m_algo;
|
||||
|
||||
public:
|
||||
explicit SelectionResult(const CAmount target)
|
||||
: m_target(target) {}
|
||||
explicit SelectionResult(const CAmount target, SelectionAlgorithm algo)
|
||||
: m_target(target), m_algo(algo) {}
|
||||
|
||||
SelectionResult() = delete;
|
||||
|
||||
|
|
|
@ -435,7 +435,7 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, const std::vec
|
|||
*/
|
||||
preset_inputs.Insert(out, /*ancestors=*/ 0, /*descendants=*/ 0, /*positive_only=*/ false);
|
||||
}
|
||||
SelectionResult result(nTargetValue);
|
||||
SelectionResult result(nTargetValue, SelectionAlgorithm::MANUAL);
|
||||
result.AddInput(preset_inputs);
|
||||
if (result.GetSelectedValue() < nTargetValue) return std::nullopt;
|
||||
return result;
|
||||
|
@ -519,7 +519,7 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, const std::vec
|
|||
// permissive CoinEligibilityFilter.
|
||||
std::optional<SelectionResult> res = [&] {
|
||||
// Pre-selected inputs already cover the target amount.
|
||||
if (value_to_select <= 0) return std::make_optional(SelectionResult(nTargetValue));
|
||||
if (value_to_select <= 0) return std::make_optional(SelectionResult(nTargetValue, SelectionAlgorithm::MANUAL));
|
||||
|
||||
// If possible, fund the transaction with confirmed UTXOs only. Prefer at least six
|
||||
// confirmations on outputs received from other wallets and only spend confirmed change.
|
||||
|
|
|
@ -168,7 +168,7 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
|
|||
FastRandomContext rand{};
|
||||
// Setup
|
||||
std::vector<COutput> utxo_pool;
|
||||
SelectionResult expected_result(CAmount(0));
|
||||
SelectionResult expected_result(CAmount(0), SelectionAlgorithm::BNB);
|
||||
|
||||
/////////////////////////
|
||||
// Known Outcome tests //
|
||||
|
|
Loading…
Add table
Reference in a new issue