<p>There are a lot of things that need to be considered when selecting coins. Just covering the amount that you want to send is not enough, you have not considered the transaction fees that you will be paying for your transaction. So even though you have UTXOs of 0.1 BTC in value, using just one of those means that you will not have enough value in the transaction to cover the transaction fee so your transaction won't be relayed or confirmed.</p>

<p>So if you have to go over the target value, now you need to have change outputs. But even here you have to be careful. If you choose just enough Bitcoin to cover the outgoing amount and the transaction fee, you may end up producing a very small change output that would be considered dust. Dust outputs are not good as they are uneconomical to spend, i.e. they cost more in transaction fees than they are worth.</p>

<hr>

<p>Bitcoin Core uses two different coin selection algorithms for different cases, and they have different target values.</p>

<p>The first one (the one that you found), is known as Branch and Bound. It tries to exactly match the amount needed for the transaction (outgoing amount + transaction fees) and not produce change. There is a small buffer allowing the algorithm to select slightly more coins than necessary in order to have a better match and avoid producing change outputs.</p>

<p>The general idea for this buffer is that if you are willing to create a change output and have to consume the cost of spending that output in the future, then you are willing to take that cost now and simply not produce a change output. So long as the extra amount that was selected is less than the predicted cost of spending a change output in the future, it is worth it to burn that amount now as extra transaction fee rather than producing a change output. The cost to the user is at most the same, and more likely to be less.</p>

<p>In general, this will produce smaller transactions and thus pay less in fees due to avoiding change outputs. It also helps with privacy because there is no change to link back to your wallet.</p>

<p>If the Branch and Bound exact matcher fails and it must produce a change output, then Bitcoin Core falls back to its original algorithm. This original algorithm is largely also an exact matcher, it just has a bunch of random fixes and hacks that were put into it to fix various issues. At a high level, it randomly selects coins.</p>

<p>An important part of this fallback algorithm is that, because it must create a change output, it chooses to target a minimum change amount to avoid the dust issue I mentioned earlier. Bitcoin Core defaults a minimum change amount of 0.01 Bitcoin. Of course there are exceptions to this when there are not enough coins in the wallet.</p>

<hr>

<p>So what happened here is that you did not have coins of the correct values to exactly match 0.01 BTC + transaction fees, so the Branch and Bound algorithm fails. It falls back to the fallback algorithm and now targets 0.01 BTC (outgoing) + 0.01 (minimum change) + transaction fees. The fallback algorithm is able to select coins for this target value and thus you get the transaction that you have.</p>

CC BY-SA 4.0
2019-07-31T01:55:31.837Z
2019-07-31T01:55:31.837Z
Ava Chow
<p>Playing around with Bitcoin Core Client v0.18.0, I used this command:</p>

<pre><code>$ bitcoin-cli sendtoaddress "mozazucvLK179u41bF5Vd2Agp4wsm7HsPo" 0.1
</code></pre>

<p>which created
<a href="https://live.blockcypher.com/btc-testnet/tx/ba4feb21868656fabe5ee7653d8d8b6f4a3d961f9719435b05c69e2a6d865426/" rel="nofollow noreferrer">this transaction</a>.</p>

<p>I noticed that the transaction created consumed three inputs, even though there were UTXOs of the exact amount available.</p>

<p>Looking for the UTXO selection algorithm, I came across the <a href="https://github.com/bitcoin/bitcoin/blob/74f1a27f2f45af7dafcc34df766cf76d29c7c6ed/src/wallet/coinselection.cpp#L1" rel="nofollow noreferrer">Bound Coin Selection Algorithm</a>, but I'm not really familiar with the codebase.</p>

<p>Is this the only coin selection algorithm used by the client? Can someone explain how does it work?</p>

CC BY-SA 4.0
2022-11-26T17:01:20.180Z
Does this answer your question? [What is the coin selection algorithm?](https://bitcoin.stackexchange.com/questions/1077/what-is-the-coin-selection-algorithm)
Geremia
CC BY-SA 4.0
2022-11-29T19:31:05.677Z
What kind of address is that? Is this about an alternative chain?
Mercedes
<bitcoin-core><client><coin-selection>
Coin selection algorithm
azavalla
# Coin selection algorithm

(Asked by: username_0 on 2019-07-30)

Playing around with Bitcoin Core Client v0.18.0, I used this command:

```
$ bitcoin-cli sendtoaddress "mozazucvLK179u41bF5Vd2Agp4wsm7HsPo" 0.1

```

which created
[this transaction](https://live.blockcypher.com/btc-testnet/tx/ba4feb21868656fabe5ee7653d8d8b6f4a3d961f9719435b05c69e2a6d865426/).

I noticed that the transaction created consumed three inputs, even though there were UTXOs of the exact amount available.

Looking for the UTXO selection algorithm, I came across the [Bound Coin Selection Algorithm](https://github.com/bitcoin/bitcoin/blob/74f1a27f2f45af7dafcc34df766cf76d29c7c6ed/src/wallet/coinselection.cpp#L1), but I'm not really familiar with the codebase.

Is this the only coin selection algorithm used by the client? Can someone explain how does it work?

**Tags:** bitcoin-core, client, coin-selection

> **Comments:**
> **username_1**: Does this answer your question? [What is the coin selection algorithm?](https://bitcoin.stackexchange.com/questions/1077/what-is-the-coin-selection-algorithm)  
> **username_2**: What kind of address is that? Is this about an alternative chain?  

---

## 1 answers

### Answer by username_3 on 2019-07-31. Score: 5 | Accepted answer

There are a lot of things that need to be considered when selecting coins. Just covering the amount that you want to send is not enough, you have not considered the transaction fees that you will be paying for your transaction. So even though you have UTXOs of 0.1 BTC in value, using just one of those means that you will not have enough value in the transaction to cover the transaction fee so your transaction won't be relayed or confirmed.

So if you have to go over the target value, now you need to have change outputs. But even here you have to be careful. If you choose just enough Bitcoin to cover the outgoing amount and the transaction fee, you may end up producing a very small change output that would be considered dust. Dust outputs are not good as they are uneconomical to spend, i.e. they cost more in transaction fees than they are worth.

---

Bitcoin Core uses two different coin selection algorithms for different cases, and they have different target values.

The first one (the one that you found), is known as Branch and Bound. It tries to exactly match the amount needed for the transaction (outgoing amount + transaction fees) and not produce change. There is a small buffer allowing the algorithm to select slightly more coins than necessary in order to have a better match and avoid producing change outputs.

The general idea for this buffer is that if you are willing to create a change output and have to consume the cost of spending that output in the future, then you are willing to take that cost now and simply not produce a change output. So long as the extra amount that was selected is less than the predicted cost of spending a change output in the future, it is worth it to burn that amount now as extra transaction fee rather than producing a change output. The cost to the user is at most the same, and more likely to be less.

In general, this will produce smaller transactions and thus pay less in fees due to avoiding change outputs. It also helps with privacy because there is no change to link back to your wallet.

If the Branch and Bound exact matcher fails and it must produce a change output, then Bitcoin Core falls back to its original algorithm. This original algorithm is largely also an exact matcher, it just has a bunch of random fixes and hacks that were put into it to fix various issues. At a high level, it randomly selects coins.

An important part of this fallback algorithm is that, because it must create a change output, it chooses to target a minimum change amount to avoid the dust issue I mentioned earlier. Bitcoin Core defaults a minimum change amount of 0.01 Bitcoin. Of course there are exceptions to this when there are not enough coins in the wallet.

---

So what happened here is that you did not have coins of the correct values to exactly match 0.01 BTC + transaction fees, so the Branch and Bound algorithm fails. It falls back to the fallback algorithm and now targets 0.01 BTC (outgoing) + 0.01 (minimum change) + transaction fees. The fallback algorithm is able to select coins for this target value and thus you get the transaction that you have.


---

