[Ethereum] the default ordering of transactions during mining


I know that the yellow paper does not specify how transactions are to be ordered in a block and this is up to the miner to decide. But I am interested in how this is practically handled (I assume not calling some rand function to get ordering). This answer to a related question suggests that gas price plays a role. How does, e.g. geth order transactions during the mining process?

Best Answer

Edit: 23 June 2017 - Added Parity details (see below)


For the vanilla Geth implementation, the commitNewWork() function in worker.go orders in the following way:

//approach 2
transactions := self.eth.TxPool().GetTransactions() <--- fetch from pool
types.SortByPriceAndNonce(transactions)      <---------- order

i.e. Sorts by gas price and nonce value (and by owner account - see below).

There are two other approaches in the code which are both commented out, but may give a clue as to previous ideas (or give examples to miners who want to use their own implementation). They are:

  • approach 1: sort by nonce only
  • approach 3: sort by owner (treating single- and multi-owner transactions differently), then by price and nonce

SortByPriceAndNonce() is defined in transaction.go:

// This method first sorts the separates the list of transactions into individual
// sender accounts and sorts them by nonce. After the account nonce ordering is
// satisfied, the results are merged back together by price, always comparing only
// the head transaction from each account. This is done via a heap to keep it fast

Once sorted, the transactions are further screened in commitTransactions() to remove any with "low gas".


For Parity things are a little easier: there's a CLI option. This will, at least, allow ordering to be changed in some basic ways.

 --tx-queue-strategy S          Prioritization strategy used to order transactions
                                 in the queue. S may be:
                                 gas - Prioritize txs with low gas limit;
                                 gas_price - Prioritize txs with high gas price;
                                 gas_factor - Prioritize txs using gas price
                                 and gas limit ratio (default: gas_price).
Related Topic