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

Commit

Permalink
feat(sender): change to use tick and remove handle reorg function (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp committed Feb 25, 2024
1 parent 1bd56c0 commit 27f79c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 63 deletions.
48 changes: 0 additions & 48 deletions internal/sender/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 12 additions & 15 deletions internal/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down

0 comments on commit 27f79c0

Please sign in to comment.