|
| 1 | +package sender |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/big" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/common" |
| 9 | + "github.com/ethereum/go-ethereum/core/types" |
| 10 | + "github.com/ethereum/go-ethereum/log" |
| 11 | + "github.com/holiman/uint256" |
| 12 | + "github.com/pborman/uuid" |
| 13 | + "modernc.org/mathutil" |
| 14 | + |
| 15 | + "github.com/taikoxyz/taiko-client/pkg/rpc" |
| 16 | +) |
| 17 | + |
| 18 | +// adjustGas adjusts the gas fee cap and gas tip cap of the given transaction with the configured |
| 19 | +// growth rate. |
| 20 | +func (s *Sender) adjustGas(txData types.TxData) { |
| 21 | + rate := s.GasGrowthRate + 100 |
| 22 | + switch baseTx := txData.(type) { |
| 23 | + case *types.DynamicFeeTx: |
| 24 | + gasFeeCap := baseTx.GasFeeCap.Int64() |
| 25 | + gasFeeCap = gasFeeCap / 100 * int64(rate) |
| 26 | + gasFeeCap = mathutil.MinInt64(gasFeeCap, int64(s.MaxGasFee)) |
| 27 | + baseTx.GasFeeCap = big.NewInt(gasFeeCap) |
| 28 | + |
| 29 | + gasTipCap := baseTx.GasTipCap.Int64() |
| 30 | + gasTipCap = gasTipCap / 100 * int64(rate) |
| 31 | + gasTipCap = mathutil.MinInt64(gasFeeCap, mathutil.MinInt64(gasTipCap, int64(s.MaxGasFee))) |
| 32 | + baseTx.GasTipCap = big.NewInt(gasTipCap) |
| 33 | + case *types.BlobTx: |
| 34 | + gasFeeCap := baseTx.GasFeeCap.Uint64() |
| 35 | + gasFeeCap = gasFeeCap / 100 * rate |
| 36 | + gasFeeCap = mathutil.MinUint64(gasFeeCap, s.MaxGasFee) |
| 37 | + baseTx.GasFeeCap = uint256.NewInt(gasFeeCap) |
| 38 | + |
| 39 | + gasTipCap := baseTx.GasTipCap.Uint64() |
| 40 | + gasTipCap = gasTipCap / 100 * rate |
| 41 | + gasTipCap = mathutil.MinUint64(gasFeeCap, mathutil.MinUint64(gasTipCap, s.MaxGasFee)) |
| 42 | + baseTx.GasTipCap = uint256.NewInt(gasTipCap) |
| 43 | + |
| 44 | + blobFeeCap := baseTx.BlobFeeCap.Uint64() |
| 45 | + blobFeeCap = blobFeeCap / 100 * rate |
| 46 | + blobFeeCap = mathutil.MinUint64(blobFeeCap, s.MaxBlobFee) |
| 47 | + baseTx.BlobFeeCap = uint256.NewInt(blobFeeCap) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// AdjustNonce adjusts the nonce of the given transaction with the current nonce of the sender. |
| 52 | +func (s *Sender) AdjustNonce(txData types.TxData) { |
| 53 | + nonce, err := s.client.NonceAt(s.ctx, s.Opts.From, nil) |
| 54 | + if err != nil { |
| 55 | + log.Warn("Failed to get the nonce", "from", s.Opts.From, "err", err) |
| 56 | + return |
| 57 | + } |
| 58 | + s.Opts.Nonce = new(big.Int).SetUint64(nonce) |
| 59 | + |
| 60 | + switch tx := txData.(type) { |
| 61 | + case *types.DynamicFeeTx: |
| 62 | + tx.Nonce = nonce |
| 63 | + case *types.BlobTx: |
| 64 | + tx.Nonce = nonce |
| 65 | + default: |
| 66 | + log.Warn("Unsupported transaction type", "from", s.Opts.From) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// updateGasTipGasFee updates the gas tip cap and gas fee cap of the sender with the given chain head info. |
| 71 | +func (s *Sender) updateGasTipGasFee(head *types.Header) error { |
| 72 | + // Get the gas tip cap |
| 73 | + gasTipCap, err := s.client.SuggestGasTipCap(s.ctx) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + // Get the gas fee cap |
| 79 | + gasFeeCap := new(big.Int).Add(gasTipCap, new(big.Int).Mul(head.BaseFee, big.NewInt(2))) |
| 80 | + // Check if the gas fee cap is less than the gas tip cap |
| 81 | + if gasFeeCap.Cmp(gasTipCap) < 0 { |
| 82 | + return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", gasFeeCap, gasTipCap) |
| 83 | + } |
| 84 | + maxGasFee := new(big.Int).SetUint64(s.MaxGasFee) |
| 85 | + if gasFeeCap.Cmp(maxGasFee) > 0 { |
| 86 | + gasFeeCap = new(big.Int).Set(maxGasFee) |
| 87 | + gasTipCap = new(big.Int).Set(maxGasFee) |
| 88 | + } |
| 89 | + |
| 90 | + s.Opts.GasTipCap = gasTipCap |
| 91 | + s.Opts.GasFeeCap = gasFeeCap |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// buildTxData assembles the transaction data from the given transaction. |
| 97 | +func (s *Sender) buildTxData(tx *types.Transaction) (types.TxData, error) { |
| 98 | + switch tx.Type() { |
| 99 | + case types.DynamicFeeTxType: |
| 100 | + return &types.DynamicFeeTx{ |
| 101 | + ChainID: s.client.ChainID, |
| 102 | + To: tx.To(), |
| 103 | + Nonce: tx.Nonce(), |
| 104 | + GasFeeCap: s.Opts.GasFeeCap, |
| 105 | + GasTipCap: s.Opts.GasTipCap, |
| 106 | + Gas: tx.Gas(), |
| 107 | + Value: tx.Value(), |
| 108 | + Data: tx.Data(), |
| 109 | + AccessList: tx.AccessList(), |
| 110 | + }, nil |
| 111 | + case types.BlobTxType: |
| 112 | + var to common.Address |
| 113 | + if tx.To() != nil { |
| 114 | + to = *tx.To() |
| 115 | + } |
| 116 | + return &types.BlobTx{ |
| 117 | + ChainID: uint256.MustFromBig(s.client.ChainID), |
| 118 | + To: to, |
| 119 | + Nonce: tx.Nonce(), |
| 120 | + GasFeeCap: uint256.MustFromBig(s.Opts.GasFeeCap), |
| 121 | + GasTipCap: uint256.MustFromBig(s.Opts.GasTipCap), |
| 122 | + Gas: tx.Gas(), |
| 123 | + Value: uint256.MustFromBig(tx.Value()), |
| 124 | + Data: tx.Data(), |
| 125 | + AccessList: tx.AccessList(), |
| 126 | + BlobFeeCap: uint256.MustFromBig(tx.BlobGasFeeCap()), |
| 127 | + BlobHashes: tx.BlobHashes(), |
| 128 | + Sidecar: tx.BlobTxSidecar(), |
| 129 | + }, nil |
| 130 | + default: |
| 131 | + return nil, fmt.Errorf("unsupported transaction type: %v", tx.Type()) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 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 | + |
| 180 | +// setDefault sets the default value if the given value is 0. |
| 181 | +func setDefault[T uint64 | time.Duration](src, dest T) T { |
| 182 | + if src == 0 { |
| 183 | + return dest |
| 184 | + } |
| 185 | + return src |
| 186 | +} |
| 187 | + |
| 188 | +// setConfigWithDefaultValues sets the config with default values if the given config is nil. |
| 189 | +func setConfigWithDefaultValues(config *Config) *Config { |
| 190 | + if config == nil { |
| 191 | + return DefaultConfig |
| 192 | + } |
| 193 | + return &Config{ |
| 194 | + ConfirmationDepth: setDefault(config.ConfirmationDepth, DefaultConfig.ConfirmationDepth), |
| 195 | + MaxRetrys: setDefault(config.MaxRetrys, DefaultConfig.MaxRetrys), |
| 196 | + MaxWaitingTime: setDefault(config.MaxWaitingTime, DefaultConfig.MaxWaitingTime), |
| 197 | + GasLimit: setDefault(config.GasLimit, DefaultConfig.GasLimit), |
| 198 | + GasGrowthRate: setDefault(config.GasGrowthRate, DefaultConfig.GasGrowthRate), |
| 199 | + MaxGasFee: setDefault(config.MaxGasFee, DefaultConfig.MaxGasFee), |
| 200 | + MaxBlobFee: setDefault(config.MaxBlobFee, DefaultConfig.MaxBlobFee), |
| 201 | + } |
| 202 | +} |
0 commit comments