<p>The code you're looking for is in <a href="https://github.com/bitcoin/bitcoin/blob/v0.10.0rc3/src/miner.cpp#L91"><code>CreateNewBlock</code></a>. </p>

<p>The usual way transactions are added to a block is by sorting by priority. The priority of an input is the value being spent in the input multiplied by the number of confirmations it has. The priority of a transaction is the sum of all the priorities of the inputs. See <a href="https://github.com/bitcoin/bitcoin/blob/v0.10.0rc3/src/miner.cpp#L197">this line</a>:</p>

<pre><code>dPriority += (double)nValueIn * nConf;
</code></pre>

<p>Transactions are added to a <a href="https://github.com/bitcoin/bitcoin/blob/v0.10.0rc3/src/miner.cpp#L146">vector of transaction priority</a>:</p>

<pre><code>vector&lt;TxPriority&gt; vecPriority;
</code></pre>

<p>Then they are <a href="https://github.com/bitcoin/bitcoin/blob/v0.10.0rc3/src/miner.cpp#L225-L226">sorted according to their priority</a>:</p>

<pre><code>TxPriorityCompare comparer(fSortedByFee);
std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
</code></pre>

<p>And one by one, <a href="https://github.com/bitcoin/bitcoin/blob/v0.10.0rc3/src/miner.cpp#L228-L286">transactions are verified to be valid and then added to the block</a>, done by order of priority, higher priority first.:</p>

<pre><code>while (!vecPriority.empty())
...
// Added
pblock-&gt;vtx.push_back(tx);
</code></pre>

CC BY-SA 3.0
2015-01-23T17:15:20.047Z
♦ As far as I read the code, if nBlockPrioritySize is set larger than 1, transaction fee has higher priority than the sum of all the priorities of the inputs. Is it correct?
zono
CC BY-SA 3.0
2015-01-23T16:39:10.623Z
2015-01-23T16:39:10.623Z
morsecoder
<p>I am reading "Mastering Bitcoin written by Andreas" and I found this explanation of mining.</p>

<blockquote>
  <p>Transactions are added to the new block, prioritized by the
  highest-fee transactions first and a few other criteria.</p>
  
  <p>Source: [<a href="http://chimera.labs.oreilly.com/books/1234000001802/ch02.html#_mining_transactions_in_blocks" rel="nofollow">Mastering Bitcoins: Mining Transactions in Blocks</a>]</p>
</blockquote>

<p>I want to know what the a few other criteria are. And I want to see the source code where the system prioritizes by highest-fee transactions first and a few other criteria.</p>

<p>I guess that the code is in <a href="https://github.com/bitcoin/bitcoin/blob/master/src/miner.cpp" rel="nofollow"><code>miner.cpp</code></a>. But I don't understand the exact line.</p>

<p><a href="https://github.com/bitcoin/bitcoin/blob/master/src/miner.cpp" rel="nofollow">https://github.com/bitcoin/bitcoin/blob/master/src/miner.cpp</a></p>

<bitcoind><transaction-selection><coin-age-priority><mastering-bitcoin>
What is criteria of mining priority except for transaction-fee?
zono
# What is criteria of mining priority except for transaction-fee?

(Asked by: username_0 on 2015-01-23)

I am reading "Mastering Bitcoin written by Andreas" and I found this explanation of mining.

> Transactions are added to the new block, prioritized by the
> highest-fee transactions first and a few other criteria.
>
> Source: [[Mastering Bitcoins: Mining Transactions in Blocks](http://chimera.labs.oreilly.com/books/1234000001802/ch02.html#_mining_transactions_in_blocks)]

I want to know what the a few other criteria are. And I want to see the source code where the system prioritizes by highest-fee transactions first and a few other criteria.

I guess that the code is in [`miner.cpp`](https://github.com/bitcoin/bitcoin/blob/master/src/miner.cpp). But I don't understand the exact line.

<https://github.com/bitcoin/bitcoin/blob/master/src/miner.cpp>

**Tags:** bitcoind, transaction-selection, coin-age-priority, mastering-bitcoin


---

## 1 answers

### Answer by username_1 on 2015-01-23. Score: 6 | Accepted answer

The code you're looking for is in CreateNewBlock. 
The usual way transactions are added to a block is by sorting by priority. The priority of an input is the value being spent in the input multiplied by the number of confirmations it has. The priority of a transaction is the sum of all the priorities of the inputs. See this line:
dPriority += (double)nValueIn * nConf;

Transactions are added to a vector of transaction priority:
vector vecPriority;

Then they are sorted according to their priority:
TxPriorityCompare comparer(fSortedByFee);
std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);

And one by one, transactions are verified to be valid and then added to the block, done by order of priority, higher priority first.:
while (!vecPriority.empty())
...
// Added
pblock->vtx.push_back(tx);

> **Comments:**
> **username_0**: ♦ As far as I read the code, if nBlockPrioritySize is set larger than 1, transaction fee has higher priority than the sum of all the priorities of the inputs. Is it correct?  

---

