Skip to content

Commit

Permalink
Merge pull request #1 from lennybakkalian/patch-1
Browse files Browse the repository at this point in the history
core: fix unit test for tx hash ordering
  • Loading branch information
ktrout1111 authored May 18, 2022
2 parents 478bb2e + 113df3d commit d2004bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ func (s TxByPriceAndHash) Less(i, j int) bool {
// deterministic sorting
cmp := s[i].ImmutableGasPrice().Cmp(s[j].ImmutableGasPrice())
if cmp == 0 {
hi := s[i].Hash()
hj := s[j].Hash()
hi := s[i].Hash()
hj := s[j].Hash()
return bytes.Compare(hi[:], hj[:]) == -1
}
return cmp > 0
Expand Down
14 changes: 7 additions & 7 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,11 @@ 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)
keys := make([]*ecdsa.PrivateKey, 10)
for i := 0; i < len(keys); i++ {
keys[i], _ = crypto.GenerateKey()
}
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

0 comments on commit d2004bd

Please sign in to comment.