From 27f79c0500540b147bba180cbfd617474d60f165 Mon Sep 17 00:00:00 2001 From: maskpp Date: Sun, 25 Feb 2024 20:08:24 +0800 Subject: [PATCH] feat(sender): change to use tick and remove handle reorg function (#571) --- internal/sender/common.go | 48 --------------------------------------- internal/sender/sender.go | 27 ++++++++++------------ 2 files changed, 12 insertions(+), 63 deletions(-) diff --git a/internal/sender/common.go b/internal/sender/common.go index 87ab346b1..81b5689e2 100644 --- a/internal/sender/common.go +++ b/internal/sender/common.go @@ -9,10 +9,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" "github.com/holiman/uint256" - "github.com/pborman/uuid" "modernc.org/mathutil" - - "github.com/taikoxyz/taiko-client/pkg/rpc" ) // 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) { } } -// handleReorgTransactions handles the transactions which are backed to the mempool due to reorg. -func (s *Sender) handleReorgTransactions() { // nolint: unused - content, err := rpc.Content(s.ctx, s.client) - if err != nil { - log.Warn("failed to get the unconfirmed transactions", "address", s.Opts.From.String(), "err", err) - return - } - if len(content) == 0 { - return - } - - txs := map[common.Hash]*types.Transaction{} - for _, txMapStatus := range content { - for key, txMapNonce := range txMapStatus { - addr := common.HexToAddress(key) - if addr != s.Opts.From { - continue - } - for _, tx := range txMapNonce { - txs[tx.Hash()] = tx - } - } - } - // Remove the already handled transactions. - for _, confirm := range s.unconfirmedTxs.Items() { - delete(txs, confirm.CurrentTx.Hash()) - } - for _, tx := range txs { - baseTx, err := s.buildTxData(tx) - if err != nil { - log.Warn("failed to make the transaction data when handle reorg txs", "tx_hash", tx.Hash().String(), "err", err) - return - } - txID := uuid.New() - confirm := &TxToConfirm{ - ID: txID, - CurrentTx: tx, - originalTx: baseTx, - } - s.unconfirmedTxs.Set(txID, confirm) - s.txToConfirmCh.Set(txID, make(chan *TxToConfirm, 1)) - log.Info("handle reorg tx", "tx_hash", tx.Hash().String(), "tx_id", txID) - } -} - // setDefault sets the default value if the given value is 0. func setDefault[T uint64 | time.Duration](src, dest T) T { if src == 0 { diff --git a/internal/sender/sender.go b/internal/sender/sender.go index 3007b571d..14bc5033c 100644 --- a/internal/sender/sender.go +++ b/internal/sender/sender.go @@ -26,7 +26,7 @@ var ( unconfirmedTxsCap = 100 nonceIncorrectRetrys = 3 unconfirmedTxsCheckInternal = 2 * time.Second - chainHeadFetchInterval = 3 * time.Second // nolint:unused + chainHeadFetchInterval = 3 * time.Second errTimeoutInMempool = fmt.Errorf("transaction in mempool for too long") DefaultConfig = &Config{ ConfirmationDepth: 0, @@ -259,14 +259,8 @@ func (s *Sender) send(tx *TxToConfirm) error { func (s *Sender) loop() { defer s.wg.Done() - // Subscribe new head. - headCh := make(chan *types.Header, 3) - sub, err := s.client.SubscribeNewHead(s.ctx, headCh) - if err != nil { - log.Error("failed to subscribe new head", "err", err) - return - } - defer sub.Unsubscribe() + chainHeadFetchTicker := time.NewTicker(chainHeadFetchInterval) + defer chainHeadFetchTicker.Stop() unconfirmedTxsCheckTicker := time.NewTicker(unconfirmedTxsCheckInternal) defer unconfirmedTxsCheckTicker.Stop() @@ -279,12 +273,15 @@ func (s *Sender) loop() { return case <-unconfirmedTxsCheckTicker.C: s.resendUnconfirmedTxs() - case newHead := <-headCh: - // If chain appear reorg then handle mempool transactions. - // TODO(Huan): handle reorg transactions - //if s.header.Hash() != header.ParentHash { - //s.handleReorgTransactions() - //} + case <-chainHeadFetchTicker.C: + newHead, err := s.client.HeaderByNumber(s.ctx, nil) + if err != nil { + log.Error("Failed to get the latest header", "err", err) + continue + } + if s.head.Hash() == newHead.Hash() { + continue + } s.head = newHead // Update the gas tip and gas fee if err = s.updateGasTipGasFee(newHead); err != nil {