Skip to content

Commit c1740e4

Browse files
markya0616fjl
authored andcommitted
core/types, miner: avoid tx sender miscaching (#14773)
1 parent e3db123 commit c1740e4

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

core/types/transaction.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,28 +381,32 @@ func (s *TxByPrice) Pop() interface{} {
381381
// transactions in a profit-maximising sorted order, while supporting removing
382382
// entire batches of transactions for non-executable accounts.
383383
type TransactionsByPriceAndNonce struct {
384-
txs map[common.Address]Transactions // Per account nonce-sorted list of transactions
385-
heads TxByPrice // Next transaction for each unique account (price heap)
384+
txs map[common.Address]Transactions // Per account nonce-sorted list of transactions
385+
heads TxByPrice // Next transaction for each unique account (price heap)
386+
signer Signer // Signer for the set of transactions
386387
}
387388

388389
// NewTransactionsByPriceAndNonce creates a transaction set that can retrieve
389390
// price sorted transactions in a nonce-honouring way.
390391
//
391392
// Note, the input map is reowned so the caller should not interact any more with
392-
// if after providng it to the constructor.
393-
func NewTransactionsByPriceAndNonce(txs map[common.Address]Transactions) *TransactionsByPriceAndNonce {
393+
// if after providing it to the constructor.
394+
func NewTransactionsByPriceAndNonce(signer Signer, txs map[common.Address]Transactions) *TransactionsByPriceAndNonce {
394395
// Initialize a price based heap with the head transactions
395396
heads := make(TxByPrice, 0, len(txs))
396-
for acc, accTxs := range txs {
397+
for _, accTxs := range txs {
397398
heads = append(heads, accTxs[0])
399+
// Ensure the sender address is from the signer
400+
acc, _ := Sender(signer, accTxs[0])
398401
txs[acc] = accTxs[1:]
399402
}
400403
heap.Init(&heads)
401404

402405
// Assemble and return the transaction set
403406
return &TransactionsByPriceAndNonce{
404-
txs: txs,
405-
heads: heads,
407+
txs: txs,
408+
heads: heads,
409+
signer: signer,
406410
}
407411
}
408412

@@ -416,9 +420,7 @@ func (t *TransactionsByPriceAndNonce) Peek() *Transaction {
416420

417421
// Shift replaces the current best head with the next one from the same account.
418422
func (t *TransactionsByPriceAndNonce) Shift() {
419-
signer := deriveSigner(t.heads[0].data.V)
420-
// derive signer but don't cache.
421-
acc, _ := Sender(signer, t.heads[0]) // we only sort valid txs so this cannot fail
423+
acc, _ := Sender(t.signer, t.heads[0])
422424
if txs, ok := t.txs[acc]; ok && len(txs) > 0 {
423425
t.heads[0], t.txs[acc] = txs[0], txs[1:]
424426
heap.Fix(&t.heads, 0)

core/types/transaction_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func TestTransactionPriceNonceSort(t *testing.T) {
143143
}
144144
}
145145
// Sort the transactions and cross check the nonce ordering
146-
txset := NewTransactionsByPriceAndNonce(groups)
146+
txset := NewTransactionsByPriceAndNonce(signer, groups)
147147

148148
txs := Transactions{}
149149
for {

miner/worker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (self *worker) update() {
268268
self.currentMu.Lock()
269269
acc, _ := types.Sender(self.current.signer, ev.Tx)
270270
txs := map[common.Address]types.Transactions{acc: {ev.Tx}}
271-
txset := types.NewTransactionsByPriceAndNonce(txs)
271+
txset := types.NewTransactionsByPriceAndNonce(self.current.signer, txs)
272272

273273
self.current.commitTransactions(self.mux, txset, self.chain, self.coinbase)
274274
self.currentMu.Unlock()
@@ -471,7 +471,7 @@ func (self *worker) commitNewWork() {
471471
log.Error("Failed to fetch pending transactions", "err", err)
472472
return
473473
}
474-
txs := types.NewTransactionsByPriceAndNonce(pending)
474+
txs := types.NewTransactionsByPriceAndNonce(self.current.signer, pending)
475475
work.commitTransactions(self.mux, txs, self.chain, self.coinbase)
476476

477477
// compute uncles for the new block.

0 commit comments

Comments
 (0)