Skip to content

Commit 4e55cf0

Browse files
committed
eth: backend returns txpool's error through rpc resp and success rpc resp for tx already tracked
1 parent c8a9a9c commit 4e55cf0

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

core/txpool/locals/tx_tracker.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,23 @@ type TxTracker struct {
5858

5959
// New creates a new TxTracker
6060
func New(journalPath string, journalTime time.Duration, chainConfig *params.ChainConfig, next *txpool.TxPool) *TxTracker {
61-
pool := &TxTracker{
61+
tracker := &TxTracker{
6262
all: make(map[common.Hash]*types.Transaction),
6363
byAddr: make(map[common.Address]*legacypool.SortedMap),
6464
signer: types.LatestSigner(chainConfig),
6565
shutdownCh: make(chan struct{}),
6666
pool: next,
6767
}
6868
if journalPath != "" {
69-
pool.journal = newTxJournal(journalPath)
70-
pool.rejournal = journalTime
69+
tracker.journal = newTxJournal(journalPath)
70+
tracker.rejournal = journalTime
7171
}
72-
return pool
72+
return tracker
73+
}
74+
75+
// HasTx checks if TxTracker has this tx already.
76+
func (tracker *TxTracker) HasTx(hash common.Hash) bool {
77+
return tracker.all[hash] != nil
7378
}
7479

7580
// Track adds a transaction to the tracked set.

eth/api_backend.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package eth
1919
import (
2020
"context"
2121
"errors"
22+
"github.com/ethereum/go-ethereum/log"
2223
"math/big"
2324
"time"
2425

@@ -274,18 +275,23 @@ func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscri
274275
func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
275276
locals := b.eth.localTxTracker
276277
if locals != nil {
278+
// If tx tracker already has the tx, that indicates
279+
// the transaction has been submitted once.
280+
// So local tracker should make sure the transaction
281+
// submitted in the end. Just return nil error to inform
282+
// the rpc client that tx has been received.
283+
if locals.HasTx(signedTx.Hash()) {
284+
log.Trace("Tx tracker has transaction and no need to add it to tx-pool", "hash", signedTx.Hash().Hex())
285+
return nil
286+
}
287+
// Transaction won't be tracked if tracker already has it
277288
if err := locals.Track(signedTx); err != nil {
278289
return err
279290
}
280291
}
281-
// No error will be returned to user if the transaction fails stateful
282-
// validation (e.g., no available slot), as the locally submitted transactions
283-
// may be resubmitted later via the local tracker.
284-
err := b.eth.txPool.Add([]*types.Transaction{signedTx}, false)[0]
285-
if err != nil && locals == nil {
286-
return err
287-
}
288-
return nil
292+
// The rpc client needs to be informed if there is
293+
// stateful validation problems.
294+
return b.eth.txPool.Add([]*types.Transaction{signedTx}, false)[0]
289295
}
290296

291297
func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {

0 commit comments

Comments
 (0)