<p>The other answers describe the rules currently implemented in the client, but I expect those rules to be replaced once block size limits become relevant. Since those rules aren't enforced when clients verify the blockchain, they are only guidelines, and will be violated if they're against the interest of miners.</p>

<p>Miners will simply choose the subset of candidate transactions that respects the limits (blocksize and signature count) and maximizes the fee.</p>

<p>Disregarding a few minor complications (knapsack and dependent transactions) this means that miners sort the transactions by <code>fee/transactionSize</code> if the maximal size is the limiting factor, and <code>fee/signatureCount</code> if the signature count is the limiting factor.</p>

CC BY-SA 3.0
2012-04-09T20:38:53.060Z
2012-04-10T02:59:18.280Z
2012-04-10T02:59:18.280Z
CodesInChaos
<p>I don't know the formula from any authoritative source, but based on the examples in the text, it is</p>

<pre><code>Fee = Normal fee / (1 - Block size / 500KB)
</code></pre>

<p>Or equivalently (and I suspect the actual code uses this form)</p>

<pre><code>Fee = Normal fee * (500 KB) / (500KB - Block size)
</code></pre>

CC BY-SA 3.0
2012-04-09T18:57:02.323Z
Did you add the "Or equivalently" part after posting your initial answer?  There's no "edited by..." under your question, but I could swear your answer was originally half its current size.
Chris Moore
CC BY-SA 3.0
2012-04-09T19:33:04.847Z
@ChrisMoore: The author of a post can edit it during some time (5 minutes I think) without the change showing up as an edit.
D.H.
CC BY-SA 3.0
2012-04-10T07:18:04.377Z
@ChrisMoore: You are of course correct, as is D.H. .
Meni Rosenfeld
CC BY-SA 3.0
2012-05-16T03:59:18.333Z
Where is the "Normal fee" defined for this calculation?
Highly Irregular
CC BY-SA 3.0
2012-04-09T18:22:16.293Z
2012-04-09T18:22:16.293Z
Meni Rosenfeld
<p>See <a href="https://github.com/bitcoin/bitcoin/blob/v0.6.0/src/main.h#L542">GetMinFee()</a> in main.h for the code which determines the minimum transaction fee.</p>

<p>The <a href="https://github.com/bitcoin/bitcoin/blob/v0.6.0/src/main.h#L575">part you're describing</a> is implemented as follows:</p>

<pre><code>// Raise the price as the block approaches full
if (nBlockSize != 1 &amp;&amp; nNewBlockSize &gt;= MAX_BLOCK_SIZE_GEN/2)
{
    if (nNewBlockSize &gt;= MAX_BLOCK_SIZE_GEN)
        return MAX_MONEY;
    nMinFee *= MAX_BLOCK_SIZE_GEN / (MAX_BLOCK_SIZE_GEN - nNewBlockSize);
}
</code></pre>

<p>where:</p>

<ul>
<li><code>nBlockSize</code> depends on who we are:
<ul>
<li>if we're a user making a new transaction, <code>nBlockSize</code> is 1</li>
<li>if we're a node deciding whether to relay an incoming transaction, <code>nBlockSize</code> is 1000</li>
<li>if we're a miner deciding which transactions to include in a new block, <code>nBlockSize</code> is the size of the block we're building before adding the new transaction</li>
</ul></li>
<li><code>nNewBlockSize</code> is the size of the block including the new transaction</li>
<li><code>MAX_BLOCK_SIZE_GEN</code> is 500kB</li>
<li><code>MAX_MONEY</code> is 21 million BTC (all the money in the world)</li>
</ul>

<p>so it's saying:</p>

<ul>
<li>if we're either deciding whether to relay or mine the transaction, and adding the transaction would make the block over 50% full:
<ul>
<li>never allow a transaction that makes the block 100% full</li>
<li>otherwise scale the fee up by a factor of <code>500kB / (500kB - nNewBlockSize)</code></li>
</ul></li>
</ul>

<p>Note: by rearranging the symbols, we get exactly what Meni wrote in his answer:</p>

<p><code>Fee</code> = <code>Normal fee / ((500kB - size) / 500kB)</code>
         = <code>Normal fee / (1 - size / 500kB)</code></p>

CC BY-SA 3.0
2012-04-09T20:24:25.007Z
That rule is pretty strange, and doesn't look stable to me, since it's against the miners interests.
CodesInChaos
CC BY-SA 3.0
2012-04-12T05:23:07.777Z
It's not against the miner's interests. If the miner fills up the block with low-fee transactions, he loses the opportunity to get high-fee transaction in there. Remember, not all transactions are the same size, so you can't just go in fee order. (The optimum algorithm would be to sort the transactions into two pools, one for fee transactions in fee-per-byte order, the other for free transactions in priority order. Fill a block from the first pool in order if possible until no more paid transactions fit, then take as many from the second pool as fit.)
David Schwartz
CC BY-SA 3.0
2012-04-16T15:18:17.750Z
@DavidSchwartz I argue that miners will converge to a fee-per-byte auction instead of this algorithm because that's in their interest. You agree with the fee-per-byte part, but disagree with the current algorithm being against miners interest. So your comment seems inconsistent to me.
CodesInChaos
CC BY-SA 3.0
2012-04-16T18:20:12.937Z
@CodeInChaos: The effort required to act in their interest would actually make it not in their interest overall. The difference between an ideal algorithm and the current one, today, is about 0.02% or so. It's like driving across town to pay a penny a gallon less for gas.
David Schwartz
CC BY-SA 3.0
2012-05-31T03:56:52.920Z
Even if the algorithm wouldn't require a fee, if the the coin used for input is new a fee still could be required: http://bitcointalk.org/index.php?topic=84434.msg931988#msg931988
Stephen Gornick
CC BY-SA 3.0
2012-04-09T18:52:41.113Z
2012-04-09T18:52:41.113Z
Chris Moore
<p>According to the <a href="https://en.bitcoin.it/wiki/Transaction_fee">Wiki</a>:</p>

<blockquote>
  <p>If the blocksize is more than 250 kB, transactions get increasingly more expensive as the blocksize approaches the limit of 500 kB. Sending a transaction when the blocksize is 400 kB will cost 5 times the normal amount; sending when it's 499 kB will cost 500x, etc.</p>
</blockquote>

<p>What is the exact formula that determines the transaction fee?</p>

<transactions><bitcoin-core><block><transaction-fees>
What is the exact formula for calculating transaction fees?
ThePiachu
# What is the exact formula for calculating transaction fees?

(Asked by: username_0 on 2012-04-09)

According to the [Wiki](https://en.bitcoin.it/wiki/Transaction_fee):

> If the blocksize is more than 250 kB, transactions get increasingly more expensive as the blocksize approaches the limit of 500 kB. Sending a transaction when the blocksize is 400 kB will cost 5 times the normal amount; sending when it's 499 kB will cost 500x, etc.

What is the exact formula that determines the transaction fee?

**Tags:** transactions, bitcoin-core, block, transaction-fees


---

## 3 answers

### Answer by username_3 on 2012-04-09. Score: 10 | Accepted answer

See [GetMinFee()](https://github.com/bitcoin/bitcoin/blob/v0.6.0/src/main.h#L542) in main.h for the code which determines the minimum transaction fee.

The [part you're describing](https://github.com/bitcoin/bitcoin/blob/v0.6.0/src/main.h#L575) is implemented as follows:

```
// Raise the price as the block approaches full
if (nBlockSize != 1 && nNewBlockSize >= MAX_BLOCK_SIZE_GEN/2)
{
    if (nNewBlockSize >= MAX_BLOCK_SIZE_GEN)
        return MAX_MONEY;
    nMinFee *= MAX_BLOCK_SIZE_GEN / (MAX_BLOCK_SIZE_GEN - nNewBlockSize);
}

```

where:

* `nBlockSize` depends on who we are:
  + if we're a user making a new transaction, `nBlockSize` is 1
  + if we're a node deciding whether to relay an incoming transaction, `nBlockSize` is 1000
  + if we're a miner deciding which transactions to include in a new block, `nBlockSize` is the size of the block we're building before adding the new transaction
* `nNewBlockSize` is the size of the block including the new transaction
* `MAX_BLOCK_SIZE_GEN` is 500kB
* `MAX_MONEY` is 21 million BTC (all the money in the world)

so it's saying:

* if we're either deciding whether to relay or mine the transaction, and adding the transaction would make the block over 50% full:
  + never allow a transaction that makes the block 100% full
  + otherwise scale the fee up by a factor of `500kB / (500kB - nNewBlockSize)`

Note: by rearranging the symbols, we get exactly what username_2 wrote in his answer:

`Fee` = `Normal fee / ((500kB - size) / 500kB)`
= `Normal fee / (1 - size / 500kB)`

> **Comments:**
> **username_1**: That rule is pretty strange, and doesn't look stable to me, since it's against the miners interests.  
> **username_6**: It's not against the miner's interests. If the miner fills up the block with low-fee transactions, he loses the opportunity to get high-fee transaction in there. Remember, not all transactions are the same size, so you can't just go in fee order. (The optimum algorithm would be to sort the transactions into two pools, one for fee transactions in fee-per-byte order, the other for free transactions in priority order. Fill a block from the first pool in order if possible until no more paid transactions fit, then take as many from the second pool as fit.)  
> **username_1**: @username_6 I argue that miners will converge to a fee-per-byte auction instead of this algorithm because that's in their interest. You agree with the fee-per-byte part, but disagree with the current algorithm being against miners interest. So your comment seems inconsistent to me.  
> **username_6**: @CodeInChaos: The effort required to act in their interest would actually make it not in their interest overall. The difference between an ideal algorithm and the current one, today, is about 0.02% or so. It's like driving across town to pay a penny a gallon less for gas.  
> **username_7**: Even if the algorithm wouldn't require a fee, if the the coin used for input is new a fee still could be required: http://bitcointalk.org/index.php?topic=84434.msg931988#msg931988  

---

### Answer by username_1 on 2012-04-09. Score: 1

The other answers describe the rules currently implemented in the client, but I expect those rules to be replaced once block size limits become relevant. Since those rules aren't enforced when clients verify the blockchain, they are only guidelines, and will be violated if they're against the interest of miners.

Miners will simply choose the subset of candidate transactions that respects the limits (blocksize and signature count) and maximizes the fee.

Disregarding a few minor complications (knapsack and dependent transactions) this means that miners sort the transactions by `fee/transactionSize` if the maximal size is the limiting factor, and `fee/signatureCount` if the signature count is the limiting factor.


---

### Answer by username_2 on 2012-04-09. Score: 1

I don't know the formula from any authoritative source, but based on the examples in the text, it is

```
Fee = Normal fee / (1 - Block size / 500KB)

```

Or equivalently (and I suspect the actual code uses this form)

```
Fee = Normal fee * (500 KB) / (500KB - Block size)

```

> **Comments:**
> **username_3**: Did you add the "Or equivalently" part after posting your initial answer?  There's no "edited by..." under your question, but I could swear your answer was originally half its current size.  
> **username_4**: @username_3: The author of a post can edit it during some time (5 minutes I think) without the change showing up as an edit.  
> **username_2**: @username_3: You are of course correct, as is D.H. .  
> **username_5**: Where is the "Normal fee" defined for this calculation?  

---

