Skip to content

Commit bf26745

Browse files
fix(transaction): use EstimateGasAtBlock instead of EstimateGas (#5252)
1 parent 23ef082 commit bf26745

File tree

7 files changed

+29
-31
lines changed

7 files changed

+29
-31
lines changed

pkg/node/chain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ func (m noOpChainBackend) SuggestGasTipCap(context.Context) (*big.Int, error) {
372372
panic("chain no op: SuggestGasTipCap")
373373
}
374374

375-
func (m noOpChainBackend) EstimateGas(context.Context, ethereum.CallMsg) (uint64, error) {
376-
panic("chain no op: EstimateGas")
375+
func (m noOpChainBackend) EstimateGasAtBlock(context.Context, ethereum.CallMsg, *big.Int) (uint64, error) {
376+
panic("chain no op: EstimateGasAtBlock")
377377
}
378378

379379
func (m noOpChainBackend) SendTransaction(context.Context, *types.Transaction) error {

pkg/transaction/backend/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Geth interface {
2020
CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
2121
ChainID(ctx context.Context) (*big.Int, error)
2222
Close()
23-
EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error)
23+
EstimateGasAtBlock(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (uint64, error)
2424
FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
2525
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
2626
NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)

pkg/transaction/backendmock/backend.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type backendMock struct {
2020
sendTransaction func(ctx context.Context, tx *types.Transaction) error
2121
suggestedFeeAndTip func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error)
2222
suggestGasTipCap func(ctx context.Context) (*big.Int, error)
23-
estimateGas func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)
23+
estimateGasAtBlock func(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (gas uint64, err error)
2424
transactionReceipt func(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
2525
pendingNonceAt func(ctx context.Context, account common.Address) (uint64, error)
2626
transactionByHash func(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error)
@@ -51,9 +51,9 @@ func (m *backendMock) SuggestedFeeAndTip(ctx context.Context, gasPrice *big.Int,
5151
return nil, nil, errors.New("not implemented")
5252
}
5353

54-
func (m *backendMock) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
55-
if m.estimateGas != nil {
56-
return m.estimateGas(ctx, call)
54+
func (m *backendMock) EstimateGasAtBlock(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (uint64, error) {
55+
if m.estimateGasAtBlock != nil {
56+
return m.estimateGasAtBlock(ctx, msg, blockNumber)
5757
}
5858
return 0, errors.New("not implemented")
5959
}
@@ -171,9 +171,9 @@ func WithSuggestGasTipCapFunc(f func(ctx context.Context) (*big.Int, error)) Opt
171171
})
172172
}
173173

174-
func WithEstimateGasFunc(f func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)) Option {
174+
func WithEstimateGasAtBlockFunc(f func(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (gas uint64, err error)) Option {
175175
return optionFunc(func(s *backendMock) {
176-
s.estimateGas = f
176+
s.estimateGasAtBlock = f
177177
})
178178
}
179179

pkg/transaction/backendsimulation/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (m *simulatedBackend) SuggestedFeeAndTip(ctx context.Context, gasPrice *big
9595
return nil, nil, errors.New("not implemented")
9696
}
9797

98-
func (m *simulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
98+
func (m *simulatedBackend) EstimateGasAtBlock(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (uint64, error) {
9999
return 0, errors.New("not implemented")
100100
}
101101

pkg/transaction/transaction.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
const loggerName = "transaction"
3232

3333
const (
34-
noncePrefix = "transaction_nonce_"
3534
storedTransactionPrefix = "transaction_stored_"
3635
pendingTransactionPrefix = "transaction_pending_"
3736
)
@@ -275,11 +274,11 @@ func (t *transactionService) StoredTransaction(txHash common.Hash) (*StoredTrans
275274
func (t *transactionService) prepareTransaction(ctx context.Context, request *TxRequest, nonce uint64, boostPercent int) (tx *types.Transaction, err error) {
276275
var gasLimit uint64
277276
if request.GasLimit == 0 {
278-
gasLimit, err = t.backend.EstimateGas(ctx, ethereum.CallMsg{
277+
gasLimit, err = t.backend.EstimateGasAtBlock(ctx, ethereum.CallMsg{
279278
From: t.sender,
280279
To: request.To,
281280
Data: request.Data,
282-
})
281+
}, nil) // nil for latest block
283282
if err != nil {
284283
t.logger.Debug("estimate gas failed", "error", err)
285284
gasLimit = request.MinEstimatedGasLimit

pkg/transaction/transaction_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ func TestTransactionSend(t *testing.T) {
151151
}
152152
return nil
153153
}),
154-
backendmock.WithEstimateGasFunc(func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
155-
if !bytes.Equal(call.To.Bytes(), recipient.Bytes()) {
156-
t.Fatalf("estimating with wrong recipient. wanted %x, got %x", recipient, call.To)
154+
backendmock.WithEstimateGasAtBlockFunc(func(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (gas uint64, err error) {
155+
if !bytes.Equal(msg.To.Bytes(), recipient.Bytes()) {
156+
t.Fatalf("estimating with wrong recipient. wanted %x, got %x", recipient, msg.To)
157157
}
158-
if !bytes.Equal(call.Data, txData) {
158+
if !bytes.Equal(msg.Data, txData) {
159159
t.Fatal("estimating with wrong data")
160160
}
161161
return estimatedGasLimit, nil
@@ -234,7 +234,7 @@ func TestTransactionSend(t *testing.T) {
234234
}
235235
return nil
236236
}),
237-
backendmock.WithEstimateGasFunc(func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
237+
backendmock.WithEstimateGasAtBlockFunc(func(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (gas uint64, err error) {
238238
return 0, errors.New("estimate failure")
239239
}),
240240
backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) {
@@ -314,11 +314,11 @@ func TestTransactionSend(t *testing.T) {
314314
}
315315
return nil
316316
}),
317-
backendmock.WithEstimateGasFunc(func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
318-
if !bytes.Equal(call.To.Bytes(), recipient.Bytes()) {
319-
t.Fatalf("estimating with wrong recipient. wanted %x, got %x", recipient, call.To)
317+
backendmock.WithEstimateGasAtBlockFunc(func(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (gas uint64, err error) {
318+
if !bytes.Equal(msg.To.Bytes(), recipient.Bytes()) {
319+
t.Fatalf("estimating with wrong recipient. wanted %x, got %x", recipient, msg.To)
320320
}
321-
if !bytes.Equal(call.Data, txData) {
321+
if !bytes.Equal(msg.Data, txData) {
322322
t.Fatal("estimating with wrong data")
323323
}
324324
return estimatedGasLimit, nil
@@ -396,11 +396,11 @@ func TestTransactionSend(t *testing.T) {
396396
}
397397
return nil
398398
}),
399-
backendmock.WithEstimateGasFunc(func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
400-
if !bytes.Equal(call.To.Bytes(), recipient.Bytes()) {
401-
t.Fatalf("estimating with wrong recipient. wanted %x, got %x", recipient, call.To)
399+
backendmock.WithEstimateGasAtBlockFunc(func(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (gas uint64, err error) {
400+
if !bytes.Equal(msg.To.Bytes(), recipient.Bytes()) {
401+
t.Fatalf("estimating with wrong recipient. wanted %x, got %x", recipient, msg.To)
402402
}
403-
if !bytes.Equal(call.Data, txData) {
403+
if !bytes.Equal(msg.Data, txData) {
404404
t.Fatal("estimating with wrong data")
405405
}
406406
return estimatedGasLimit, nil
@@ -461,7 +461,7 @@ func TestTransactionSend(t *testing.T) {
461461
}
462462
return nil
463463
}),
464-
backendmock.WithEstimateGasFunc(func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
464+
backendmock.WithEstimateGasAtBlockFunc(func(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) (gas uint64, err error) {
465465
if !bytes.Equal(call.To.Bytes(), recipient.Bytes()) {
466466
t.Fatalf("estimating with wrong recipient. wanted %x, got %x", recipient, call.To)
467467
}
@@ -527,7 +527,7 @@ func TestTransactionSend(t *testing.T) {
527527
}
528528
return nil
529529
}),
530-
backendmock.WithEstimateGasFunc(func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
530+
backendmock.WithEstimateGasAtBlockFunc(func(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) (gas uint64, err error) {
531531
if !bytes.Equal(call.To.Bytes(), recipient.Bytes()) {
532532
t.Fatalf("estimating with wrong recipient. wanted %x, got %x", recipient, call.To)
533533
}

pkg/transaction/wrapped/wrapped.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,10 @@ func (b *wrappedBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error)
138138
}
139139
return gasTipCap, nil
140140
}
141-
142-
func (b *wrappedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
141+
func (b *wrappedBackend) EstimateGasAtBlock(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (uint64, error) {
143142
b.metrics.TotalRPCCalls.Inc()
144143
b.metrics.EstimateGasCalls.Inc()
145-
gas, err = b.backend.EstimateGas(ctx, call)
144+
gas, err := b.backend.EstimateGasAtBlock(ctx, msg, blockNumber)
146145
if err != nil {
147146
b.metrics.TotalRPCErrors.Inc()
148147
return 0, err

0 commit comments

Comments
 (0)