Skip to content

Commit

Permalink
Revert "Merge pull request ethereum#16 from yihuang/release/v1.11.x"
Browse files Browse the repository at this point in the history
This reverts commit ebb0950, reversing
changes made to 43cf32d.
  • Loading branch information
mmsqe committed Sep 25, 2024
1 parent ebb0950 commit c85340c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 8 additions & 2 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"math/big"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
Expand All @@ -48,7 +49,8 @@ const (

// Transaction is an Ethereum transaction.
type Transaction struct {
inner TxData // Consensus contents of a transaction
inner TxData // Consensus contents of a transaction
time time.Time // Time first seen locally (spam avoidance)

// caches
hash atomic.Value
Expand Down Expand Up @@ -198,6 +200,7 @@ func (tx *Transaction) decodeTyped(b []byte) (TxData, error) {
// setDecoded sets the inner transaction and size after decoding.
func (tx *Transaction) setDecoded(inner TxData, size uint64) {
tx.inner = inner
tx.time = time.Now()
if size > 0 {
tx.size.Store(size)
}
Expand Down Expand Up @@ -403,7 +406,7 @@ func (tx *Transaction) WithSignature(signer Signer, sig []byte) (*Transaction, e
}
cpy := tx.inner.copy()
cpy.setSignatureValues(signer.ChainID(), v, r, s)
return &Transaction{inner: cpy}, nil
return &Transaction{inner: cpy, time: tx.time}, nil
}

// Transactions implements DerivableList for transactions.
Expand Down Expand Up @@ -498,6 +501,9 @@ func (s TxByPriceAndTime) Less(i, j int) bool {
// If the prices are equal, use the time the transaction was first seen for
// deterministic sorting
cmp := s[i].minerFee.Cmp(s[j].minerFee)
if cmp == 0 {
return s[i].tx.time.Before(s[j].tx.time)
}
return cmp > 0
}
func (s TxByPriceAndTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
Expand Down
10 changes: 5 additions & 5 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"math/rand"
"reflect"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -359,8 +360,6 @@ func testTransactionPriceNonceSort(t *testing.T, baseFee *big.Int) {
// 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) {
t.Skip("time field is removed")

// Generate a batch of accounts to start with
keys := make([]*ecdsa.PrivateKey, 5)
for i := 0; i < len(keys); i++ {
Expand All @@ -370,10 +369,11 @@ func TestTransactionTimeSort(t *testing.T) {

// Generate a batch of transactions with overlapping prices, but different creation times
groups := map[common.Address]Transactions{}
for _, key := range keys {
for start, key := range keys {
addr := crypto.PubkeyToAddress(key.PublicKey)

tx, _ := SignTx(NewTransaction(0, common.Address{}, big.NewInt(100), 100, big.NewInt(1), nil), signer, key)
tx.time = time.Unix(0, int64(len(keys)-start))

groups[addr] = append(groups[addr], tx)
}
Expand All @@ -398,8 +398,8 @@ func TestTransactionTimeSort(t *testing.T) {
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 {
t.Errorf("invalid received time ordering: tx #%d (A=%x) > tx #%d (A=%x)", i, fromi[:4], i+1, fromNext[:4])
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)
}
}
}
Expand Down

0 comments on commit c85340c

Please sign in to comment.