Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: change ordering of txs of equal gas price from arrival time to hash #915

Closed
wants to merge 7 commits into from
Closed
32 changes: 29 additions & 3 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,38 @@ func (s *TxByPriceAndTime) Pop() interface{} {
return x
}

type TxByPriceAndHash Transactions

func (s TxByPriceAndHash) Len() int { return len(s) }
func (s TxByPriceAndHash) Less(i, j int) bool {
// If the prices are equal, use the transaction hash for
// deterministic sorting
cmp := s[i].ImmutableGasPrice().Cmp(s[j].ImmutableGasPrice())
if cmp == 0 {
return bytes.Compare(s[i].Hash().Bytes(), s[j].Hash().Bytes()) == -1
}
return cmp > 0
}
func (s TxByPriceAndHash) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func (s *TxByPriceAndHash) Push(x interface{}) {
*s = append(*s, x.(*Transaction))
}

func (s *TxByPriceAndHash) Pop() interface{} {
old := *s
n := len(old)
x := old[n-1]
*s = old[0 : n-1]
return x
}

// TransactionsByPriceAndNonce represents a set of transactions that can return
// transactions in a profit-maximizing sorted order, while supporting removing
// entire batches of transactions for non-executable accounts.
type TransactionsByPriceAndNonce struct {
txs map[common.Address]Transactions // Per account nonce-sorted list of transactions
heads TxByPriceAndTime // Next transaction for each unique account (price heap)
heads TxByPriceAndHash // Next transaction for each unique account (price heap)
signer Signer // Signer for the set of transactions
}

Expand All @@ -437,8 +463,8 @@ type TransactionsByPriceAndNonce struct {
// Note, the input map is reowned so the caller should not interact any more with
// if after providing it to the constructor.
func NewTransactionsByPriceAndNonce(signer Signer, txs map[common.Address]Transactions) *TransactionsByPriceAndNonce {
// Initialize a price and received time based heap with the head transactions
heads := make(TxByPriceAndTime, 0, len(txs))
// Initialize a price and transaction hash based heap with the head transactions
heads := make(TxByPriceAndHash, 0, len(txs))
for from, accTxs := range txs {
// Ensure the sender address is from the signer
if acc, _ := Sender(signer, accTxs[0]); acc != from {
Expand Down
12 changes: 6 additions & 6 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ func TestTransactionPriceNonceSort(t *testing.T) {
}
}

// Tests that if multiple transactions have the same price, the ones seen earlier
// are prioritized to avoid network spam attacks aiming for a specific ordering.
func TestTransactionTimeSort(t *testing.T) {
// Tests if transactions with the same gas price are lexicographical sorted by hash
// to prevent network spam attacks and to make the transaction sorting traceable
func TestTransactionHashSort(t *testing.T) {
// Generate a batch of accounts to start with
keys := make([]*ecdsa.PrivateKey, 5)
for i := 0; i < len(keys); i++ {
Expand Down Expand Up @@ -350,9 +350,9 @@ func TestTransactionTimeSort(t *testing.T) {
if txi.GasPrice().Cmp(next.GasPrice()) < 0 {
t.Errorf("invalid gasprice ordering: tx #%d (A=%x P=%v) < tx #%d (A=%x P=%v)", i, fromi[:4], txi.GasPrice(), i+1, fromNext[:4], next.GasPrice())
}
// Make sure time order is ascending if the txs have the same gas price
if txi.GasPrice().Cmp(next.GasPrice()) == 0 && txi.time.After(next.time) {
t.Errorf("invalid received time ordering: tx #%d (A=%x T=%v) > tx #%d (A=%x T=%v)", i, fromi[:4], txi.time, i+1, fromNext[:4], next.time)
// Make sure hash order is lexicographical if the txs have the same gas price
if txi.GasPrice().Cmp(next.GasPrice()) == 0 && bytes.Compare(txi.Hash().Bytes(), next.Hash().Bytes()) > 0 {
t.Errorf("invalid hash ordering: tx #%d (H=%x) > tx #%d (H=%x)", i, txi.Hash().Bytes()[:4], i+1, next.Hash().Bytes()[:4])
}
}
}
Expand Down