1

please help me understand the algorithm for selecting UTXO when forming a new transaction in Bitcoin.

For example, my wallet has a lot of incoming transactions (thousands of transactions in amounts of 0.001-0.09 BTC that have more than 70 confirmations and several transactions in amounts of 2-3 BTC that have 2-5 confirmations). When sending a new transaction in the amount of 1.5 BTC, the node for some reason creates a transaction with 500+ inputs for small amounts, thereby the weight of the transaction is 75 kb and, accordingly, the amount of miner's fees increases to enormous values. But if the node sent a transaction from an address that had 2 BTC, the miners's fees to would be minimal.

Please tell me how the node chooses which UTXOs to assemble a new transaction from? What settings on the node can regulate this?

I tried to find an answer on Google, but I couldn't find anything similar.

Pieter Wuille
  • 105,497
  • 9
  • 194
  • 308

1 Answers1

0

By default, Bitcoin Core will only spend foreign inputs that have at least six confirmations, and change outputs that have at least one confirmation. If you want to use younger foreign inputs, you can use the minconf parameter present e.g. on sendmany.

Beyond that, Bitcoin Core will split its UTXO pool by output type, and then for each output type uses multiple algorithms to construct one input set candidate. Among all of these candidate input sets, it chooses the least wasteful per the waste metric.

The algorithms in use in Bitcoin Core v25.1 are knapsack algorithm, single random draw, and branch and bound.

  • Knapsack will perform numerous random walks on all UTXOs with amounts lower than the target to assemble an input set that overshoots target + min_change the least, or pick the lowest larger UTXO if that is closer to target + min_change.
  • SRD will shuffle all UTXOs and pick from this shuffled list until it has collected enough funds to fund the transaction
  • BnB will attempt to find an input set that avoids creating change. If there are multiple solutions, it will prefer the least wasteful.

None of the above algorithms actually minimize the input set weight. Hence, I am proposing that we add an algorithm that minimizes the input set weight in PR#27877, which at the very least should be run at high feerates. The downside of such an algorithm would be that it could bloat a wallet’s UTXO pool when used excessively, but it would ensure an optimal outcome in the short-term. So far, this proposal has not gotten sufficient support to be added to Bitcoin Core.

There are currently no configuration options that directly affect the general coin selection behavior. You would have to perform coin selection manually or externally and build raw transactions to optimize fee expenditure.

Murch
  • 75,206
  • 34
  • 186
  • 622