core/txpool: define IsTemporaryReject API

This commit is contained in:
Gary Rong 2025-04-17 21:52:19 +08:00
parent b21ffa88af
commit aa702301bd
5 changed files with 76 additions and 62 deletions

View file

@ -68,30 +68,3 @@ var (
// transactions is reached for specific accounts.
ErrInflightTxLimitReached = errors.New("in-flight transaction limit reached for delegated accounts")
)
// ErrTxTemporarilyRejected represents a temporarily rejected transaction,
// typically due to a full transaction pool or other transient conditions.
// The transaction may be accepted later.
type ErrTxTemporarilyRejected struct {
Err error
}
// WrapTxTemporarilyRejected wraps the given error in an ErrTxTemporarilyRejected type.
func WrapTxTemporarilyRejected(err error) ErrTxTemporarilyRejected {
return ErrTxTemporarilyRejected{Err: err}
}
// Error implements the error interface.
func (e ErrTxTemporarilyRejected) Error() string {
return e.Err.Error()
}
// Unwrap allows unwrapping the underlying error.
func (e ErrTxTemporarilyRejected) Unwrap() error {
return e.Err
}
// Is enables errors.Is to match either the wrapper or the wrapped error.
func (e ErrTxTemporarilyRejected) Is(target error) bool {
return errors.Is(e.Err, target)
}

View file

@ -0,0 +1,45 @@
// Copyright 2025 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package legacypool
import (
"errors"
"github.com/ethereum/go-ethereum/core/txpool"
)
// IsTemporaryReject determines whether the given error indicates a temporary
// reason to reject a transaction from being included in the txpool. The result
// may change if the txpool's state changes later.
func IsTemporaryReject(err error) bool {
switch {
case errors.Is(err, ErrOutOfOrderTxFromDelegated):
return true
case errors.Is(err, txpool.ErrInflightTxLimitReached):
return true
case errors.Is(err, ErrAuthorityReserved):
return true
case errors.Is(err, txpool.ErrUnderpriced):
return true
case errors.Is(err, ErrTxPoolOverflow):
return true
case errors.Is(err, ErrFutureReplacePending):
return true
default:
return false
}
}

View file

@ -932,39 +932,6 @@ func (pool *LegacyPool) addRemoteSync(tx *types.Transaction) error {
return pool.Add([]*types.Transaction{tx}, true)[0]
}
// annotateErrors processes the provided errors and annotate them with a
// txpool.ErrTxTemporarilyRejected wrapper, reflecting the transactions
// are temporarily rejected with some reasons but may be accepted later.
func annotateErrors(errs []error) []error {
var annotated []error
for _, err := range errs {
switch {
/*
// These two errors are not possible in case of leagcy pool
case errors.Is(err, core.ErrNonceTooHigh):
annotated = append(annotated, txpool.WrapTxTemporarilyRejected(err))
case errors.Is(err, txpool.ErrAccountLimitExceeded):
annotated = append(annotated, txpool.WrapTxTemporarilyRejected(err))
*/
case errors.Is(err, ErrOutOfOrderTxFromDelegated):
annotated = append(annotated, txpool.WrapTxTemporarilyRejected(err))
case errors.Is(err, txpool.ErrInflightTxLimitReached):
annotated = append(annotated, txpool.WrapTxTemporarilyRejected(err))
case errors.Is(err, ErrAuthorityReserved):
annotated = append(annotated, txpool.WrapTxTemporarilyRejected(err))
case errors.Is(err, txpool.ErrUnderpriced):
annotated = append(annotated, txpool.WrapTxTemporarilyRejected(err))
case errors.Is(err, ErrTxPoolOverflow):
annotated = append(annotated, txpool.WrapTxTemporarilyRejected(err))
case errors.Is(err, ErrFutureReplacePending):
annotated = append(annotated, txpool.WrapTxTemporarilyRejected(err))
default:
annotated = append(annotated, err)
}
}
return annotated
}
// Add enqueues a batch of transactions into the pool if they are valid.
//
// Note, if sync is set the method will block until all internal maintenance
@ -1016,7 +983,7 @@ func (pool *LegacyPool) Add(txs []*types.Transaction, sync bool) []error {
if sync {
<-done
}
return annotateErrors(errs)
return errs
}
// addTxsLocked attempts to queue a batch of transactions if they are valid.

View file

@ -0,0 +1,28 @@
// Copyright 2025 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package locals
import (
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
)
// IsTemporaryReject determines whether the given error indicates a temporary
// reason to reject a transaction from being included in the txpool. The result
// may change if the txpool's state changes later.
func IsTemporaryReject(err error) bool {
return legacypool.IsTemporaryReject(err)
}

View file

@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/txpool/locals"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/ethconfig"
@ -318,7 +319,7 @@ func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction)
// very little chance it will be accepted later (e.g., the gas price is below the
// configured minimum, or the sender has insufficient funds to cover the cost),
// propagate the error to the user.
if err != nil && !errors.As(err, &txpool.ErrTxTemporarilyRejected{}) {
if err != nil && !locals.IsTemporaryReject(err) {
return err
}
// No error will be returned to user if the transaction fails with a temporary