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

Commit d6f6ad5

Browse files
committed
update sender
1 parent 9ba8629 commit d6f6ad5

File tree

5 files changed

+90
-67
lines changed

5 files changed

+90
-67
lines changed

driver/driver_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ func (s *DriverTestSuite) TestCheckL1ReorgToHigherFork() {
168168
s.Equal(l1Head3.Number.Uint64(), l1Head1.Number.Uint64())
169169
s.Equal(l1Head3.Hash(), l1Head1.Hash())
170170

171+
// Because of evm_revert operation, the nonce of the proposer need to be adjusted.
172+
s.p.Sender.AdjustNonce(nil)
171173
// Propose ten blocks on another fork
172174
for i := 0; i < 10; i++ {
173175
testutils.ProposeInvalidTxListBytes(&s.ClientTestSuite, s.p)
@@ -231,6 +233,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToLowerFork() {
231233
s.Equal(l1Head3.Number.Uint64(), l1Head1.Number.Uint64())
232234
s.Equal(l1Head3.Hash(), l1Head1.Hash())
233235

236+
s.p.Sender.AdjustNonce(nil)
234237
// Propose one blocks on another fork
235238
testutils.ProposeInvalidTxListBytes(&s.ClientTestSuite, s.p)
236239

@@ -291,6 +294,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToSameHeightFork() {
291294
s.Equal(l1Head3.Number.Uint64(), l1Head1.Number.Uint64())
292295
s.Equal(l1Head3.Hash(), l1Head1.Hash())
293296

297+
s.p.Sender.AdjustNonce(nil)
294298
// Propose two blocks on another fork
295299
testutils.ProposeInvalidTxListBytes(&s.ClientTestSuite, s.p)
296300
time.Sleep(3 * time.Second)

internal/sender/sender.go

+17-35
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/ecdsa"
66
"fmt"
77
"math/big"
8+
"os"
89
"strings"
910
"sync"
1011
"sync/atomic"
@@ -93,6 +94,9 @@ func NewSender(ctx context.Context, cfg *Config, client *rpc.EthClient, priv *ec
9394
if cfg.MaxWaitingTime == 0 {
9495
cfg.MaxWaitingTime = time.Minute * 5
9596
}
97+
if cfg.GasLimit == 0 {
98+
cfg.GasLimit = 21000
99+
}
96100

97101
sender := &Sender{
98102
ctx: ctx,
@@ -106,7 +110,7 @@ func NewSender(ctx context.Context, cfg *Config, client *rpc.EthClient, priv *ec
106110
stopCh: make(chan struct{}),
107111
}
108112
// Set the nonce
109-
sender.adjustNonce(nil)
113+
sender.AdjustNonce(nil)
110114
// Update the gas tip and gas fee.
111115
err = sender.updateGasTipGasFee(header)
112116
if err != nil {
@@ -116,7 +120,9 @@ func NewSender(ctx context.Context, cfg *Config, client *rpc.EthClient, priv *ec
116120
if rootSender[opts.From] != nil {
117121
return nil, fmt.Errorf("sender already exists")
118122
}
119-
rootSender[opts.From] = sender
123+
if os.Getenv("RUN_TESTS") == "" {
124+
rootSender[opts.From] = sender
125+
}
120126

121127
sender.wg.Add(1)
122128
go sender.loop()
@@ -193,7 +199,7 @@ func (s *Sender) sendTx(confirmTx *TxConfirm) error {
193199
// Check if the error is nonce too low.
194200
if err != nil {
195201
if strings.Contains(err.Error(), "nonce too low") {
196-
s.adjustNonce(baseTx)
202+
s.AdjustNonce(baseTx)
197203
log.Warn("nonce is not correct, retry to send transaction", "tx_hash", rawTx.Hash().String(), "err", err)
198204
return nil
199205
}
@@ -210,31 +216,6 @@ func (s *Sender) sendTx(confirmTx *TxConfirm) error {
210216
return nil
211217
}
212218

213-
func (s *Sender) updateGasTipGasFee(head *types.Header) error {
214-
// Get the gas tip cap
215-
gasTipCap, err := s.client.SuggestGasTipCap(s.ctx)
216-
if err != nil {
217-
return err
218-
}
219-
220-
// Get the gas fee cap
221-
gasFeeCap := new(big.Int).Add(gasTipCap, new(big.Int).Mul(head.BaseFee, big.NewInt(2)))
222-
// Check if the gas fee cap is less than the gas tip cap
223-
if gasFeeCap.Cmp(gasTipCap) < 0 {
224-
return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", gasFeeCap, gasTipCap)
225-
}
226-
maxGasFee := big.NewInt(int64(s.MaxGasFee))
227-
if gasFeeCap.Cmp(maxGasFee) > 0 {
228-
gasFeeCap = new(big.Int).Set(maxGasFee)
229-
gasTipCap = new(big.Int).Set(maxGasFee)
230-
}
231-
232-
s.Opts.GasTipCap = gasTipCap
233-
s.Opts.GasFeeCap = gasFeeCap
234-
235-
return nil
236-
}
237-
238219
func (s *Sender) loop() {
239220
defer s.wg.Done()
240221

@@ -248,6 +229,8 @@ func (s *Sender) loop() {
248229
select {
249230
case <-tickResend.C:
250231
s.resendTransaction()
232+
// Check the unconfirmed transactions
233+
s.checkPendingTransactions()
251234
case <-tickHead.C:
252235
header, err := s.client.HeaderByNumber(s.ctx, nil)
253236
if err != nil {
@@ -263,8 +246,6 @@ func (s *Sender) loop() {
263246
if err != nil {
264247
log.Warn("failed to update gas tip and gas fee", "err", err)
265248
}
266-
// Check the unconfirmed transactions
267-
s.checkPendingTransactions()
268249
case <-s.ctx.Done():
269250
return
270251
case <-s.stopCh:
@@ -288,20 +269,21 @@ func (s *Sender) resendTransaction() {
288269
}
289270

290271
func (s *Sender) checkPendingTransactions() {
291-
curTime := time.Now()
292272
for txID, txConfirm := range s.unconfirmedTxs.Items() {
293273
if txConfirm.Err != nil {
294274
continue
295275
}
296276
if txConfirm.Receipt == nil {
297277
// Ignore the transaction if it is pending.
298278
tx, isPending, err := s.client.TransactionByHash(s.ctx, txConfirm.Tx.Hash())
299-
if err != nil || isPending {
279+
if err != nil {
300280
continue
301281
}
302-
// If the transaction is in mempool for too long, replace it.
303-
if waitTime := curTime.Sub(tx.Time()); waitTime > s.MaxWaitingTime {
304-
txConfirm.Err = fmt.Errorf("transaction in mempool for too long")
282+
if isPending {
283+
// If the transaction is in mempool for too long, replace it.
284+
if waitTime := time.Since(tx.Time()); waitTime > s.MaxWaitingTime {
285+
txConfirm.Err = fmt.Errorf("transaction in mempool for too long")
286+
}
305287
continue
306288
}
307289
// Get the transaction receipt.

internal/sender/transaction.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (s *Sender) adjustGas(txData types.TxData) {
4242
}
4343
}
4444

45-
func (s *Sender) adjustNonce(txData types.TxData) {
45+
func (s *Sender) AdjustNonce(txData types.TxData) {
4646
nonce, err := s.client.NonceAt(s.ctx, s.Opts.From, nil)
4747
if err != nil {
4848
log.Warn("failed to get the nonce", "from", s.Opts.From, "err", err)
@@ -58,6 +58,31 @@ func (s *Sender) adjustNonce(txData types.TxData) {
5858
}
5959
}
6060

61+
func (s *Sender) updateGasTipGasFee(head *types.Header) error {
62+
// Get the gas tip cap
63+
gasTipCap, err := s.client.SuggestGasTipCap(s.ctx)
64+
if err != nil {
65+
return err
66+
}
67+
68+
// Get the gas fee cap
69+
gasFeeCap := new(big.Int).Add(gasTipCap, new(big.Int).Mul(head.BaseFee, big.NewInt(2)))
70+
// Check if the gas fee cap is less than the gas tip cap
71+
if gasFeeCap.Cmp(gasTipCap) < 0 {
72+
return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", gasFeeCap, gasTipCap)
73+
}
74+
maxGasFee := big.NewInt(int64(s.MaxGasFee))
75+
if gasFeeCap.Cmp(maxGasFee) > 0 {
76+
gasFeeCap = new(big.Int).Set(maxGasFee)
77+
gasTipCap = new(big.Int).Set(maxGasFee)
78+
}
79+
80+
s.Opts.GasTipCap = gasTipCap
81+
s.Opts.GasFeeCap = gasFeeCap
82+
83+
return nil
84+
}
85+
6186
func (s *Sender) makeTxData(tx *types.Transaction) (types.TxData, error) {
6287
switch tx.Type() {
6388
case types.DynamicFeeTxType:

proposer/proposer.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type Proposer struct {
6060
CustomProposeOpHook func() error
6161
AfterCommitHook func() error
6262

63-
sender *sender.Sender
63+
Sender *sender.Sender
6464

6565
ctx context.Context
6666
wg sync.WaitGroup
@@ -104,11 +104,11 @@ func (p *Proposer) InitFromConfig(ctx context.Context, cfg *Config) (err error)
104104
return err
105105
}
106106

107-
if p.sender, err = sender.NewSender(ctx, &sender.Config{
108-
MaxGasFee: 20000000000,
109-
GasGrowthRate: 10,
110-
RetryTimes: 0,
111-
GasLimit: cfg.ProposeBlockTxGasLimit,
107+
if p.Sender, err = sender.NewSender(ctx, &sender.Config{
108+
MaxGasFee: 20000000000,
109+
GasGrowthRate: 20,
110+
GasLimit: cfg.ProposeBlockTxGasLimit,
111+
MaxWaitingTime: time.Second * 30,
112112
}, p.rpc.L1, cfg.L1ProposerPrivKey); err != nil {
113113
return err
114114
}
@@ -184,7 +184,7 @@ func (p *Proposer) eventLoop() {
184184

185185
// Close closes the proposer instance.
186186
func (p *Proposer) Close(ctx context.Context) {
187-
p.sender.Close()
187+
p.Sender.Close()
188188
p.wg.Wait()
189189
}
190190

@@ -359,7 +359,7 @@ func (p *Proposer) makeProposeBlockTxWithBlobHash(
359359
return nil, err
360360
}
361361

362-
opts := p.sender.Opts
362+
opts := p.Sender.Opts
363363
opts.Value = maxFee
364364
rawTx, err := p.rpc.TaikoL1.ProposeBlock(
365365
opts,
@@ -394,7 +394,7 @@ func (p *Proposer) makeProposeBlockTx(
394394
return nil, err
395395
}
396396

397-
opts := p.sender.Opts
397+
opts := p.Sender.Opts
398398
opts.Value = maxFee
399399

400400
var parentMetaHash = [32]byte{}
@@ -487,7 +487,7 @@ func (p *Proposer) ProposeTxList(
487487
log.Warn("Failed to make taikoL1.proposeBlock transaction", "error", encoding.TryParsingCustomError(err))
488488
return err
489489
}
490-
txID, err = p.sender.SendTransaction(tx)
490+
txID, err = p.Sender.SendTransaction(tx)
491491
if err != nil {
492492
log.Warn("Failed to send taikoL1.proposeBlock transaction", "error", encoding.TryParsingCustomError(err))
493493
return err
@@ -506,7 +506,7 @@ func (p *Proposer) ProposeTxList(
506506
}
507507

508508
// Waiting for the transaction to be confirmed.
509-
confirmCh, _ := p.sender.WaitTxConfirm(txID)
509+
confirmCh, _ := p.Sender.WaitTxConfirm(txID)
510510
confirm := <-confirmCh
511511
if confirm.Err != nil {
512512
return confirm.Err

proposer/proposer_test.go

+32-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package proposer
22

33
import (
44
"context"
5+
"fmt"
56
"math/big"
67
"os"
78
"testing"
@@ -15,6 +16,7 @@ import (
1516

1617
"github.com/taikoxyz/taiko-client/bindings"
1718
"github.com/taikoxyz/taiko-client/internal/testutils"
19+
"github.com/taikoxyz/taiko-client/internal/utils"
1820
"github.com/taikoxyz/taiko-client/pkg/rpc"
1921
)
2022

@@ -155,39 +157,48 @@ func (s *ProposerTestSuite) TestCustomProposeOpHook() {
155157
}
156158

157159
func (s *ProposerTestSuite) TestSendProposeBlockTx() {
160+
s.SetL1Automine(false)
161+
defer s.SetL1Automine(true)
162+
163+
s.p.Sender.AdjustNonce(nil)
164+
158165
fee := big.NewInt(10000)
159-
opts := s.p.sender.Opts
166+
opts := s.p.Sender.Opts
160167
opts.Value = fee
161168
s.Greater(opts.GasTipCap.Uint64(), uint64(0))
162169

163170
nonce, err := s.RPCClient.L1.PendingNonceAt(context.Background(), s.p.proposerAddress)
164171
s.Nil(err)
165172

166-
tx := types.NewTransaction(
167-
nonce,
168-
common.BytesToAddress([]byte{}),
169-
common.Big1,
170-
100000,
171-
opts.GasTipCap,
172-
[]byte{},
173-
)
174-
175-
s.SetL1Automine(false)
176-
defer s.SetL1Automine(true)
173+
var txID string
174+
txID, err = s.p.Sender.SendRaw(nonce, &common.Address{}, common.Big1, nil)
175+
confirmCh, _ := s.p.Sender.WaitTxConfirm(txID)
176+
confirm := <-confirmCh
177+
tx := confirm.Tx
177178

178-
signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(s.RPCClient.L1ChainID), s.p.L1ProposerPrivKey)
179+
pendingNonce, err := s.RPCClient.L1.PendingNonceAt(context.Background(), s.p.proposerAddress)
179180
s.Nil(err)
180-
s.Nil(s.RPCClient.L1.SendTransaction(context.Background(), signedTx))
181+
fmt.Println(pendingNonce)
181182

182-
var emptyTxs []types.Transaction
183-
encoded, err := rlp.EncodeToBytes(emptyTxs)
183+
encoded, err := rlp.EncodeToBytes([]types.Transaction{})
184+
s.Nil(err)
185+
var newTx *types.Transaction
186+
if s.p.BlobAllowed {
187+
newTx, err = s.p.makeProposeBlockTxWithBlobHash(context.Background(), encoded)
188+
} else {
189+
newTx, err = s.p.makeProposeBlockTx(
190+
context.Background(),
191+
encoded,
192+
)
193+
}
184194
s.Nil(err)
185195

186-
newTx, err := s.p.makeProposeBlockTx(
187-
context.Background(),
188-
encoded,
189-
)
196+
txID, err = s.p.Sender.SendTransaction(newTx)
190197
s.Nil(err)
198+
confirmCh, _ = s.p.Sender.WaitTxConfirm(txID)
199+
confirm = <-confirmCh
200+
s.Nil(confirm.Err)
201+
191202
s.Greater(newTx.GasTipCap().Uint64(), tx.GasTipCap().Uint64())
192203
}
193204

@@ -217,5 +228,6 @@ func (s *ProposerTestSuite) TestStartClose() {
217228
}
218229

219230
func TestProposerTestSuite(t *testing.T) {
231+
utils.LoadEnv()
220232
suite.Run(t, new(ProposerTestSuite))
221233
}

0 commit comments

Comments
 (0)