Skip to content

Commit 26f8387

Browse files
committed
Merge pull request ethereum#1583 from obscuren/miner-price-order
miner, core: sort txs by price, nonce
2 parents f12e016 + 35f271b commit 26f8387

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

core/types/transaction.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,22 @@ type TxByNonce struct{ Transactions }
289289
func (s TxByNonce) Less(i, j int) bool {
290290
return s.Transactions[i].data.AccountNonce < s.Transactions[j].data.AccountNonce
291291
}
292+
293+
type TxByPrice struct{ Transactions }
294+
295+
func (s TxByPrice) Less(i, j int) bool {
296+
return s.Transactions[i].data.Price.Cmp(s.Transactions[j].data.Price) > 0
297+
}
298+
299+
type TxByPriceAndNonce struct{ Transactions }
300+
301+
func (s TxByPriceAndNonce) Less(i, j int) bool {
302+
// we can ignore the error here. Sorting shouldn't care about validness
303+
ifrom, _ := s.Transactions[i].From()
304+
jfrom, _ := s.Transactions[j].From()
305+
// favour nonce if they are from the same recipient
306+
if ifrom == jfrom {
307+
return s.Transactions[i].data.AccountNonce < s.Transactions[j].data.AccountNonce
308+
}
309+
return s.Transactions[i].data.Price.Cmp(s.Transactions[j].data.Price) > 0
310+
}

miner/worker.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,42 @@ func (self *worker) commitNewWork() {
457457
self.makeCurrent(parent, header)
458458
work := self.current
459459

460-
// commit transactions for this run.
460+
/* //approach 1
461461
transactions := self.eth.TxPool().GetTransactions()
462462
sort.Sort(types.TxByNonce{transactions})
463+
*/
464+
465+
//approach 2
466+
transactions := self.eth.TxPool().GetTransactions()
467+
sort.Sort(types.TxByPriceAndNonce{transactions})
468+
469+
/* // approach 3
470+
// commit transactions for this run.
471+
txPerOwner := make(map[common.Address]types.Transactions)
472+
// Sort transactions by owner
473+
for _, tx := range self.eth.TxPool().GetTransactions() {
474+
from, _ := tx.From() // we can ignore the sender error
475+
txPerOwner[from] = append(txPerOwner[from], tx)
476+
}
477+
var (
478+
singleTxOwner types.Transactions
479+
multiTxOwner types.Transactions
480+
)
481+
// Categorise transactions by
482+
// 1. 1 owner tx per block
483+
// 2. multi txs owner per block
484+
for _, txs := range txPerOwner {
485+
if len(txs) == 1 {
486+
singleTxOwner = append(singleTxOwner, txs[0])
487+
} else {
488+
multiTxOwner = append(multiTxOwner, txs...)
489+
}
490+
}
491+
sort.Sort(types.TxByPrice{singleTxOwner})
492+
sort.Sort(types.TxByNonce{multiTxOwner})
493+
transactions := append(singleTxOwner, multiTxOwner...)
494+
*/
495+
463496
work.coinbase.SetGasLimit(header.GasLimit)
464497
work.commitTransactions(transactions, self.gasPrice, self.proc)
465498
self.eth.TxPool().RemoveTransactions(work.lowGasTxs)

0 commit comments

Comments
 (0)