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]

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. But I don't understand the exact line.



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<TxPriority> 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
  • ♦ Thank you so much! I'm now reading the lines. – zono Jan 23 '15 at 16:51
  • ♦ 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 Jan 23 '15 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.