<p>It is important to account for these fees!</p>
<p>Bitcoin Core <a href="https://github.com/bitcoin/bitcoin/blob/c2ff87e1fa35e1edfa434ac6f341a141d3545257/src/wallet/spend.cpp#L952" rel="nofollow noreferrer">adds up recipient amounts and fees for header and recipient outputs to create <code>selection_target</code></a> in <code>CreateTransactionInternal(…)</code>. This value is then used a few lines later in <a href="https://github.com/bitcoin/bitcoin/blob/c2ff87e1fa35e1edfa434ac6f341a141d3545257/src/wallet/spend.cpp#L976" rel="nofollow noreferrer"><code>SelectCoins(…)</code></a>, from where calls go to <code>AutomaticCoinSelection(…)</code>, <code>AttemptSelection(…)</code>, and <code>ChooseSelectionResult(…)</code>.</p>
<p><code>ChooseSelectionResult(…)</code> then calls all coin selection algorithms with targets based on the above value, including <code>SelectCoinsSRD(…)</code>. So, the SRD algorithm’s target does include the fees for the recipient outputs and header.</p>

CC BY-SA 4.0
2023-07-26T12:59:12.797Z
2023-07-26T13:06:45.050Z
2023-07-26T13:06:45.050Z
Murch
<p>In the following post <a href="https://bitcoin.stackexchange.com/questions/103654/calculating-fee-based-on-fee-rate-for-bitcoin-transaction/114847#114847">Calculating fee based on fee rate for bitcoin transaction</a> the respondent describes how the fees can be calculated.</p>
<blockquote>
<p>Since your transaction building starts with a known target feerate, and you know the size of the transaction header data and outputs, you can calculate their fees in advance.</p>
</blockquote>
<p>However, AFAKT, the bitcoin core SRD algorithm only accounts for a single output, the change output (change_fee):</p>
<p><a href="https://github.com/bitcoin/bitcoin/blob/c2ff87e1fa35e1edfa434ac6f341a141d3545257/src/wallet/coinselection.cpp#L204C47-L204C47" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/c2ff87e1fa35e1edfa434ac6f341a141d3545257/src/wallet/coinselection.cpp#L204C47-L204C47</a></p>
<p>In the line about, the <code>target_value</code> is incremented by the <code>CHANGE_LOWER</code> (dust limit) and the cost of producing a change output (<code>change_fee</code>).  That way, the change amount is larger than the dust limit and can also pay for the fee of creating the change output.  However, this does not account for paying for the remaining output nor the transaction header fields as described in the above link.  Why is it not important to add the size of theses respective transaction parts when setting the target_value in SRD?</p>

<transaction-fees><bitcoin-core-development><fee-estimation><coin-selection>
Understanding fee calculation in Bitcoin Core
yancy
# Understanding fee calculation in Bitcoin Core

(Asked by: username_0 on 2023-07-26)

In the following post [Calculating fee based on fee rate for bitcoin transaction](https://bitcoin.stackexchange.com/questions/103654/calculating-fee-based-on-fee-rate-for-bitcoin-transaction/114847#114847) the respondent describes how the fees can be calculated.

> Since your transaction building starts with a known target feerate, and you know the size of the transaction header data and outputs, you can calculate their fees in advance.

However, AFAKT, the bitcoin core SRD algorithm only accounts for a single output, the change output (change\_fee):

<https://github.com/bitcoin/bitcoin/blob/c2ff87e1fa35e1edfa434ac6f341a141d3545257/src/wallet/coinselection.cpp#L204C47-L204C47>

In the line about, the `target_value` is incremented by the `CHANGE_LOWER` (dust limit) and the cost of producing a change output (`change_fee`). That way, the change amount is larger than the dust limit and can also pay for the fee of creating the change output. However, this does not account for paying for the remaining output nor the transaction header fields as described in the above link. Why is it not important to add the size of theses respective transaction parts when setting the target\_value in SRD?

**Tags:** transaction-fees, bitcoin-core-development, fee-estimation, coin-selection


---

## 1 answers

### Answer by username_1 on 2023-07-26. Score: 1

It is important to account for these fees!

Bitcoin Core [adds up recipient amounts and fees for header and recipient outputs to create `selection_target`](https://github.com/bitcoin/bitcoin/blob/c2ff87e1fa35e1edfa434ac6f341a141d3545257/src/wallet/spend.cpp#L952) in `CreateTransactionInternal(…)`. This value is then used a few lines later in [`SelectCoins(…)`](https://github.com/bitcoin/bitcoin/blob/c2ff87e1fa35e1edfa434ac6f341a141d3545257/src/wallet/spend.cpp#L976), from where calls go to `AutomaticCoinSelection(…)`, `AttemptSelection(…)`, and `ChooseSelectionResult(…)`.

`ChooseSelectionResult(…)` then calls all coin selection algorithms with targets based on the above value, including `SelectCoinsSRD(…)`. So, the SRD algorithm’s target does include the fees for the recipient outputs and header.


---

