Skip to content
This repository was archived by the owner on May 11, 2024. It is now read-only.

Commit 27f79c0

Browse files
authored
feat(sender): change to use tick and remove handle reorg function (#571)
1 parent 1bd56c0 commit 27f79c0

File tree

2 files changed

+12
-63
lines changed

2 files changed

+12
-63
lines changed

internal/sender/common.go

-48
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import (
99
"github.com/ethereum/go-ethereum/core/types"
1010
"github.com/ethereum/go-ethereum/log"
1111
"github.com/holiman/uint256"
12-
"github.com/pborman/uuid"
1312
"modernc.org/mathutil"
14-
15-
"github.com/taikoxyz/taiko-client/pkg/rpc"
1613
)
1714

1815
// adjustGas adjusts the gas fee cap and gas tip cap of the given transaction with the configured
@@ -132,51 +129,6 @@ func (s *Sender) buildTxData(tx *types.Transaction) (types.TxData, error) {
132129
}
133130
}
134131

135-
// handleReorgTransactions handles the transactions which are backed to the mempool due to reorg.
136-
func (s *Sender) handleReorgTransactions() { // nolint: unused
137-
content, err := rpc.Content(s.ctx, s.client)
138-
if err != nil {
139-
log.Warn("failed to get the unconfirmed transactions", "address", s.Opts.From.String(), "err", err)
140-
return
141-
}
142-
if len(content) == 0 {
143-
return
144-
}
145-
146-
txs := map[common.Hash]*types.Transaction{}
147-
for _, txMapStatus := range content {
148-
for key, txMapNonce := range txMapStatus {
149-
addr := common.HexToAddress(key)
150-
if addr != s.Opts.From {
151-
continue
152-
}
153-
for _, tx := range txMapNonce {
154-
txs[tx.Hash()] = tx
155-
}
156-
}
157-
}
158-
// Remove the already handled transactions.
159-
for _, confirm := range s.unconfirmedTxs.Items() {
160-
delete(txs, confirm.CurrentTx.Hash())
161-
}
162-
for _, tx := range txs {
163-
baseTx, err := s.buildTxData(tx)
164-
if err != nil {
165-
log.Warn("failed to make the transaction data when handle reorg txs", "tx_hash", tx.Hash().String(), "err", err)
166-
return
167-
}
168-
txID := uuid.New()
169-
confirm := &TxToConfirm{
170-
ID: txID,
171-
CurrentTx: tx,
172-
originalTx: baseTx,
173-
}
174-
s.unconfirmedTxs.Set(txID, confirm)
175-
s.txToConfirmCh.Set(txID, make(chan *TxToConfirm, 1))
176-
log.Info("handle reorg tx", "tx_hash", tx.Hash().String(), "tx_id", txID)
177-
}
178-
}
179-
180132
// setDefault sets the default value if the given value is 0.
181133
func setDefault[T uint64 | time.Duration](src, dest T) T {
182134
if src == 0 {

internal/sender/sender.go

+12-15
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var (
2626
unconfirmedTxsCap = 100
2727
nonceIncorrectRetrys = 3
2828
unconfirmedTxsCheckInternal = 2 * time.Second
29-
chainHeadFetchInterval = 3 * time.Second // nolint:unused
29+
chainHeadFetchInterval = 3 * time.Second
3030
errTimeoutInMempool = fmt.Errorf("transaction in mempool for too long")
3131
DefaultConfig = &Config{
3232
ConfirmationDepth: 0,
@@ -259,14 +259,8 @@ func (s *Sender) send(tx *TxToConfirm) error {
259259
func (s *Sender) loop() {
260260
defer s.wg.Done()
261261

262-
// Subscribe new head.
263-
headCh := make(chan *types.Header, 3)
264-
sub, err := s.client.SubscribeNewHead(s.ctx, headCh)
265-
if err != nil {
266-
log.Error("failed to subscribe new head", "err", err)
267-
return
268-
}
269-
defer sub.Unsubscribe()
262+
chainHeadFetchTicker := time.NewTicker(chainHeadFetchInterval)
263+
defer chainHeadFetchTicker.Stop()
270264

271265
unconfirmedTxsCheckTicker := time.NewTicker(unconfirmedTxsCheckInternal)
272266
defer unconfirmedTxsCheckTicker.Stop()
@@ -279,12 +273,15 @@ func (s *Sender) loop() {
279273
return
280274
case <-unconfirmedTxsCheckTicker.C:
281275
s.resendUnconfirmedTxs()
282-
case newHead := <-headCh:
283-
// If chain appear reorg then handle mempool transactions.
284-
// TODO(Huan): handle reorg transactions
285-
//if s.header.Hash() != header.ParentHash {
286-
//s.handleReorgTransactions()
287-
//}
276+
case <-chainHeadFetchTicker.C:
277+
newHead, err := s.client.HeaderByNumber(s.ctx, nil)
278+
if err != nil {
279+
log.Error("Failed to get the latest header", "err", err)
280+
continue
281+
}
282+
if s.head.Hash() == newHead.Hash() {
283+
continue
284+
}
288285
s.head = newHead
289286
// Update the gas tip and gas fee
290287
if err = s.updateGasTipGasFee(newHead); err != nil {

0 commit comments

Comments
 (0)