From 847f621e416f4c9e487901282f7f14bb28767cc5 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Wed, 19 Feb 2025 14:55:53 +0800 Subject: [PATCH] core/txpool: relocate error --- core/txpool/errors.go | 4 ---- core/txpool/legacypool/legacypool.go | 26 +++++++---------------- core/txpool/legacypool/legacypool_test.go | 4 ++-- core/txpool/validation.go | 6 ++++-- 4 files changed, 14 insertions(+), 26 deletions(-) diff --git a/core/txpool/errors.go b/core/txpool/errors.go index 3a6a913976..c38644857e 100644 --- a/core/txpool/errors.go +++ b/core/txpool/errors.go @@ -51,10 +51,6 @@ var ( // making the transaction invalid, rather a DOS protection. ErrOversizedData = errors.New("oversized data") - // ErrFutureReplacePending is returned if a future transaction replaces a pending - // one. Future transactions should only be able to replace other future transactions. - ErrFutureReplacePending = errors.New("future transaction tries to replace pending") - // ErrAlreadyReserved is returned if the sender address has a pending transaction // in a different subpool. For example, this error is returned in response to any // input transaction of non-blob type when a blob transaction from this sender diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 12c4f9d141..07a4d70eb1 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -70,6 +70,10 @@ var ( // signed by an address which already has in-flight transactions known to the // pool. ErrAuthorityReserved = errors.New("authority already reserved") + + // ErrFutureReplacePending is returned if a future transaction replaces a pending + // one. Future transactions should only be able to replace other future transactions. + ErrFutureReplacePending = errors.New("future transaction tries to replace pending") ) var ( @@ -581,17 +585,8 @@ func (pool *LegacyPool) validateTx(tx *types.Transaction) error { opts := &txpool.ValidationOptionsWithState{ State: pool.currentState, - FirstNonceGap: nil, // Pool allows arbitrary arrival order, don't invalidate nonce gaps - UsedAndLeftSlots: func(addr common.Address) (int, int) { - var have int - if list := pool.pending[addr]; list != nil { - have += list.Len() - } - if list := pool.queue[addr]; list != nil { - have += list.Len() - } - return have, math.MaxInt - }, + FirstNonceGap: nil, // Pool allows arbitrary arrival order, don't invalidate nonce gaps + UsedAndLeftSlots: nil, // Pool has own mechanism to limit the number of transactions ExistingExpenditure: func(addr common.Address) *big.Int { if list := pool.pending[addr]; list != nil { return list.totalcost.ToBig() @@ -610,7 +605,7 @@ func (pool *LegacyPool) validateTx(tx *types.Transaction) error { if err := txpool.ValidateTransactionWithState(tx, pool.signer, opts); err != nil { return err } - return nil + return pool.validateAuth(tx) } // validateAuth verifies that the transaction complies with code authorization @@ -672,11 +667,6 @@ func (pool *LegacyPool) add(tx *types.Transaction) (replaced bool, err error) { invalidTxMeter.Mark(1) return false, err } - if err := pool.validateAuth(tx); err != nil { - log.Trace("Discarding invalid transaction", "hash", hash, "err", err) - invalidTxMeter.Mark(1) - return false, err - } // already validated by this point from, _ := types.Sender(pool.signer, tx) @@ -747,7 +737,7 @@ func (pool *LegacyPool) add(tx *types.Transaction) (replaced bool, err error) { pool.priced.Put(dropTx) } log.Trace("Discarding future transaction replacing pending tx", "hash", hash) - return false, txpool.ErrFutureReplacePending + return false, ErrFutureReplacePending } } diff --git a/core/txpool/legacypool/legacypool_test.go b/core/txpool/legacypool/legacypool_test.go index 4831c0f7cb..85b2138bf9 100644 --- a/core/txpool/legacypool/legacypool_test.go +++ b/core/txpool/legacypool/legacypool_test.go @@ -1645,8 +1645,8 @@ func TestUnderpricing(t *testing.T) { t.Fatalf("failed to add well priced transaction: %v", err) } // Ensure that replacing a pending transaction with a future transaction fails - if err := pool.addRemoteSync(pricedTransaction(5, 100000, big.NewInt(6), keys[1])); err != txpool.ErrFutureReplacePending { - t.Fatalf("adding future replace transaction error mismatch: have %v, want %v", err, txpool.ErrFutureReplacePending) + if err := pool.addRemoteSync(pricedTransaction(5, 100000, big.NewInt(6), keys[1])); err != ErrFutureReplacePending { + t.Fatalf("adding future replace transaction error mismatch: have %v, want %v", err, ErrFutureReplacePending) } pending, queued = pool.Stats() if pending != 4 { diff --git a/core/txpool/validation.go b/core/txpool/validation.go index e464839546..ca4fc15a32 100644 --- a/core/txpool/validation.go +++ b/core/txpool/validation.go @@ -268,8 +268,10 @@ func ValidateTransactionWithState(tx *types.Transaction, signer types.Signer, op // Transaction takes a new nonce value out of the pool. Ensure it doesn't // overflow the number of permitted transactions from a single account // (i.e. max cancellable via out-of-bound transaction). - if used, left := opts.UsedAndLeftSlots(from); left <= 0 { - return fmt.Errorf("%w: pooled %d txs", ErrAccountLimitExceeded, used) + if opts.UsedAndLeftSlots != nil { + if used, left := opts.UsedAndLeftSlots(from); left <= 0 { + return fmt.Errorf("%w: pooled %d txs", ErrAccountLimitExceeded, used) + } } } return nil