mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +00:00
Implements ethereum/go-ethereum PR #31202 and #31618. When local tracking is enabled: - EthAPIBackend.SendTx tracks transactions after pool submission and keeps tracking temporary rejects so they can be retried by the local tracker. - TxPool.AddLocal tracks accepted submissions and temporary rejects for local re-journal/re-submit flows, while preserving the original txpool error return to the caller. This avoids persisting permanently invalid transactions while preserving retry signals for transient failures without masking submission outcomes in caller workflows. Also included: - classify temporary rejection reasons in core/txpool/locals - expose SubPool.ValidateTxBasics and align LegacyPool implementation - split low-tip rejection into ErrTxGasPriceTooLow - simplify local tracker integration in txpool - update txpool and eth tests for accepted vs retryable local tracking behavior Refs: ethereum/go-ethereum#31202 Refs: ethereum/go-ethereum#31618
217 lines
6.8 KiB
Go
217 lines
6.8 KiB
Go
// Copyright 2023 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 implements tracking for "local" transactions
|
|
package locals
|
|
|
|
import (
|
|
"cmp"
|
|
"slices"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/XinFinOrg/XDPoSChain/common"
|
|
"github.com/XinFinOrg/XDPoSChain/core/txpool"
|
|
"github.com/XinFinOrg/XDPoSChain/core/txpool/legacypool"
|
|
"github.com/XinFinOrg/XDPoSChain/core/types"
|
|
"github.com/XinFinOrg/XDPoSChain/log"
|
|
"github.com/XinFinOrg/XDPoSChain/metrics"
|
|
"github.com/XinFinOrg/XDPoSChain/params"
|
|
)
|
|
|
|
var (
|
|
recheckInterval = time.Minute
|
|
localGauge = metrics.GetOrRegisterGauge("txpool/local", nil)
|
|
)
|
|
|
|
// TxTracker is a struct used to track priority transactions; it will check from
|
|
// time to time if the main pool has forgotten about any of the transaction
|
|
// it is tracking, and if so, submit it again.
|
|
// This is used to track 'locals'.
|
|
// This struct does not care about transaction validity, price-bumps or account limits,
|
|
// but optimistically accepts transactions.
|
|
type TxTracker struct {
|
|
all map[common.Hash]*types.Transaction // All tracked transactions
|
|
byAddr map[common.Address]*legacypool.SortedMap // Transactions by address
|
|
|
|
journal *journal // Journal of local transaction to back up to disk
|
|
rejournal time.Duration // How often to rotate journal
|
|
pool *txpool.TxPool // The tx pool to interact with
|
|
signer types.Signer
|
|
|
|
shutdownCh chan struct{}
|
|
mu sync.Mutex
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
// New creates a new TxTracker
|
|
func New(journalPath string, journalTime time.Duration, chainConfig *params.ChainConfig, next *txpool.TxPool) *TxTracker {
|
|
pool := &TxTracker{
|
|
all: make(map[common.Hash]*types.Transaction),
|
|
byAddr: make(map[common.Address]*legacypool.SortedMap),
|
|
signer: types.LatestSigner(chainConfig),
|
|
shutdownCh: make(chan struct{}),
|
|
pool: next,
|
|
}
|
|
if journalPath != "" {
|
|
pool.journal = newTxJournal(journalPath)
|
|
pool.rejournal = journalTime
|
|
}
|
|
return pool
|
|
}
|
|
|
|
// Track adds a transaction to the tracked set.
|
|
func (tracker *TxTracker) Track(tx *types.Transaction) {
|
|
tracker.TrackAll([]*types.Transaction{tx})
|
|
}
|
|
|
|
// IsRetryableReject determines whether an add error is temporary and retryable.
|
|
func (tracker *TxTracker) IsRetryableReject(err error) bool {
|
|
return IsTemporaryReject(err)
|
|
}
|
|
|
|
// TrackAll adds a list of transactions to the tracked set.
|
|
func (tracker *TxTracker) TrackAll(txs []*types.Transaction) {
|
|
tracker.mu.Lock()
|
|
defer tracker.mu.Unlock()
|
|
|
|
for _, tx := range txs {
|
|
// If we're already tracking it, it's a no-op
|
|
if _, ok := tracker.all[tx.Hash()]; ok {
|
|
continue
|
|
}
|
|
// Theoretically, checking the error here is unnecessary since sender recovery
|
|
// is already part of basic validation. However, retrieving the sender address
|
|
// from the transaction cache is effectively a no-op if it was previously verified.
|
|
// Therefore, the error is still checked just in case.
|
|
addr, err := types.Sender(tracker.signer, tx)
|
|
if err != nil { // Ignore this tx
|
|
continue
|
|
}
|
|
tracker.all[tx.Hash()] = tx
|
|
if tracker.byAddr[addr] == nil {
|
|
tracker.byAddr[addr] = legacypool.NewSortedMap()
|
|
}
|
|
tracker.byAddr[addr].Put(tx)
|
|
|
|
if tracker.journal != nil {
|
|
_ = tracker.journal.insert(tx)
|
|
}
|
|
}
|
|
localGauge.Update(int64(len(tracker.all)))
|
|
}
|
|
|
|
// recheck checks and returns any transactions that needs to be resubmitted.
|
|
func (tracker *TxTracker) recheck(journalCheck bool) (resubmits []*types.Transaction, rejournal map[common.Address]types.Transactions) {
|
|
tracker.mu.Lock()
|
|
defer tracker.mu.Unlock()
|
|
|
|
var (
|
|
numStales = 0
|
|
numOk = 0
|
|
)
|
|
for sender, txs := range tracker.byAddr {
|
|
// Wipe the stales
|
|
stales := txs.Forward(tracker.pool.Nonce(sender))
|
|
for _, tx := range stales {
|
|
delete(tracker.all, tx.Hash())
|
|
}
|
|
numStales += len(stales)
|
|
|
|
// Check the non-stale
|
|
for _, tx := range txs.Flatten() {
|
|
if tracker.pool.Has(tx.Hash()) {
|
|
numOk++
|
|
continue
|
|
}
|
|
resubmits = append(resubmits, tx)
|
|
}
|
|
}
|
|
|
|
if journalCheck { // rejournal
|
|
rejournal = make(map[common.Address]types.Transactions)
|
|
for _, tx := range tracker.all {
|
|
addr, _ := types.Sender(tracker.signer, tx)
|
|
rejournal[addr] = append(rejournal[addr], tx)
|
|
}
|
|
// Sort them
|
|
for _, list := range rejournal {
|
|
// cmp(a, b) should return a negative number when a < b,
|
|
slices.SortFunc(list, func(a, b *types.Transaction) int {
|
|
return cmp.Compare(a.Nonce(), b.Nonce())
|
|
})
|
|
}
|
|
}
|
|
localGauge.Update(int64(len(tracker.all)))
|
|
log.Debug("Tx tracker status", "need-resubmit", len(resubmits), "stale", numStales, "ok", numOk)
|
|
return resubmits, rejournal
|
|
}
|
|
|
|
// Start implements node.Lifecycle interface
|
|
// Start is called after all services have been constructed and the networking
|
|
// layer was also initialized to spawn any goroutines required by the service.
|
|
func (tracker *TxTracker) Start() error {
|
|
tracker.wg.Add(1)
|
|
go tracker.loop()
|
|
return nil
|
|
}
|
|
|
|
// Stop implements node.Lifecycle interface
|
|
// Stop terminates all goroutines belonging to the service, blocking until they
|
|
// are all terminated.
|
|
func (tracker *TxTracker) Stop() error {
|
|
close(tracker.shutdownCh)
|
|
tracker.wg.Wait()
|
|
return nil
|
|
}
|
|
|
|
func (tracker *TxTracker) loop() {
|
|
defer tracker.wg.Done()
|
|
|
|
if tracker.journal != nil {
|
|
tracker.journal.load(func(transactions []*types.Transaction) []error {
|
|
tracker.TrackAll(transactions)
|
|
return nil
|
|
})
|
|
defer tracker.journal.close()
|
|
}
|
|
var (
|
|
lastJournal = time.Now()
|
|
timer = time.NewTimer(10 * time.Second) // Do initial check after 10 seconds, do rechecks more seldom.
|
|
)
|
|
for {
|
|
select {
|
|
case <-tracker.shutdownCh:
|
|
return
|
|
case <-timer.C:
|
|
checkJournal := tracker.journal != nil && time.Since(lastJournal) > tracker.rejournal
|
|
resubmits, rejournal := tracker.recheck(checkJournal)
|
|
if len(resubmits) > 0 {
|
|
tracker.pool.Add(resubmits, false)
|
|
}
|
|
if checkJournal {
|
|
// Lock to prevent journal.rotate <-> journal.insert (via TrackAll) conflicts
|
|
tracker.mu.Lock()
|
|
lastJournal = time.Now()
|
|
if err := tracker.journal.rotate(rejournal); err != nil {
|
|
log.Warn("Transaction journal rotation failed", "err", err)
|
|
}
|
|
tracker.mu.Unlock()
|
|
}
|
|
timer.Reset(recheckInterval)
|
|
}
|
|
}
|
|
}
|