Merge bitcoin/bitcoin#26720: wallet: coin selection, don't return results that exceed the max allowed weight

25ab14712b refactor: coinselector_tests, unify wallet creation code (furszy)
ba9431c505 test: coverage for bnb max weight (furszy)
5a2bc45ee0 wallet: clean post coin selection max weight filter (furszy)
2d112584e3 coin selection: BnB, don't return selection if exceeds max allowed tx weight (furszy)
d3a1c098e4 test: coin selection, add coverage for SRD (furszy)
9d9689e5a6 coin selection: heap-ify SRD, don't return selection if exceeds max tx weight (furszy)
6107ec2229 coin selection: knapsack, select closest UTXO above target if result exceeds max tx size (furszy)
1284223691 wallet: refactor coin selection algos to return util::Result (furszy)

Pull request description:

  Coming from the following comment https://github.com/bitcoin/bitcoin/pull/25729#discussion_r1029324367.

  The reason why we are adding hundreds of UTXO from different sources when the target
  amount is covered only by one of them is because only SRD returns a usable result.

  Context:
  In the test, we create 1515 UTXOs with 0.033 BTC each, and 1 UTXO with 50 BTC. Then
  perform Coin Selection to fund 49.5 BTC.

  As the selection of the 1515 small UTXOs exceeds the max allowed tx size, the
  expectation here is to receive a selection result that only contain the big UTXO.
  Which is not happening for the following reason:

  Knapsack returns a result that exceeds the max allowed transaction size, when
  it should return the closest utxo above the target, so we fallback to SRD who
  selects coins randomly up until the target is met. So we end up with a selection
  result with lot more coins than what is needed.

ACKs for top commit:
  S3RK:
    ACK 25ab14712b
  achow101:
    ACK 25ab14712b
  Xekyo:
    reACK 25ab14712b
  theStack:
    Code-review ACK 25ab14712b

Tree-SHA512: 2425de4cc479b4db999b3b2e02eb522a2130a06379cca0418672a51c4076971a1d427191173820db76a0f85a8edfff100114e1c38fb3b5dc51598d07cabe1a60
This commit is contained in:
Andrew Chow
2023-04-20 16:25:38 -04:00
6 changed files with 288 additions and 104 deletions

View File

@@ -13,6 +13,7 @@
#include <random.h>
#include <util/system.h>
#include <util/check.h>
#include <util/result.h>
#include <optional>
@@ -408,20 +409,24 @@ public:
int GetWeight() const { return m_weight; }
};
std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CAmount& cost_of_change);
util::Result<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CAmount& cost_of_change,
int max_weight);
/** Select coins by Single Random Draw. OutputGroups are selected randomly from the eligible
* outputs until the target is satisfied
*
* @param[in] utxo_pool The positive effective value OutputGroups eligible for selection
* @param[in] target_value The target value to select for
* @returns If successful, a SelectionResult, otherwise, std::nullopt
* @param[in] rng The randomness source to shuffle coins
* @param[in] max_weight The maximum allowed weight for a selection result to be valid
* @returns If successful, a valid SelectionResult, otherwise, util::Error
*/
std::optional<SelectionResult> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value, FastRandomContext& rng);
util::Result<SelectionResult> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value, FastRandomContext& rng,
int max_weight);
// Original coin selection algorithm as a fallback
std::optional<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups, const CAmount& nTargetValue,
CAmount change_target, FastRandomContext& rng);
util::Result<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups, const CAmount& nTargetValue,
CAmount change_target, FastRandomContext& rng, int max_weight);
} // namespace wallet
#endif // BITCOIN_WALLET_COINSELECTION_H