Skip to content

Commit 09ca490

Browse files
authored
Fetched gasLimit value from config if estimateGas fails (#1108)
* Replaced generic retry with manual retry for client.estimateGas * if gaslimitOverride not present in config then gasLimit = core.HigherGaslimit * Added log when using hardcoded higher gas limit value
1 parent c21abf3 commit 09ca490

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

core/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ var BlockNumberInterval = 5
5353

5454
//SwitchClientDuration is the time after which alternate client from secondary RPC will be switched back to client from primary RPC
5555
var SwitchClientDuration = 5 * EpochLength
56+
57+
//HigherGasLimitValue is the gas limit which will be used when estimateGas fails
58+
var HigherGasLimitValue uint64 = 50000000

utils/client_methods.go

+30-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package utils
22

33
import (
44
"context"
5+
"github.com/avast/retry-go"
56
"github.com/ethereum/go-ethereum"
67
"github.com/ethereum/go-ethereum/common"
78
"github.com/ethereum/go-ethereum/core/types"
89
"github.com/ethereum/go-ethereum/ethclient"
910
"math/big"
11+
"razor/core"
1012
)
1113

1214
func (*ClientStruct) GetNonceAtWithRetry(client *ethclient.Client, accountAddress common.Address) (uint64, error) {
@@ -27,19 +29,43 @@ func (*ClientStruct) GetLatestBlockWithRetry(client *ethclient.Client) (*types.H
2729
}
2830

2931
func (*ClientStruct) SuggestGasPriceWithRetry(client *ethclient.Client) (*big.Int, error) {
30-
returnedValues, err := InvokeFunctionWithRetryAttempts(ClientInterface, "SuggestGasPrice", client, context.Background())
32+
var (
33+
gasPrice *big.Int
34+
err error
35+
)
36+
err = retry.Do(
37+
func() error {
38+
gasPrice, err = ClientInterface.SuggestGasPrice(client, context.Background())
39+
if err != nil {
40+
log.Error("Error in fetching gas price.... Retrying")
41+
return err
42+
}
43+
return nil
44+
}, RetryInterface.RetryAttempts(core.MaxRetries))
3145
if err != nil {
3246
return nil, err
3347
}
34-
return returnedValues[0].Interface().(*big.Int), nil
48+
return gasPrice, nil
3549
}
3650

3751
func (*ClientStruct) EstimateGasWithRetry(client *ethclient.Client, message ethereum.CallMsg) (uint64, error) {
38-
returnedValues, err := InvokeFunctionWithRetryAttempts(ClientInterface, "EstimateGas", client, context.Background(), message)
52+
var (
53+
gasLimit uint64
54+
err error
55+
)
56+
err = retry.Do(
57+
func() error {
58+
gasLimit, err = ClientInterface.EstimateGas(client, context.Background(), message)
59+
if err != nil {
60+
log.Error("Error in estimating gas limit.... Retrying")
61+
return err
62+
}
63+
return nil
64+
}, RetryInterface.RetryAttempts(core.MaxRetries))
3965
if err != nil {
4066
return 0, err
4167
}
42-
return returnedValues[0].Interface().(uint64), nil
68+
return gasLimit, nil
4369
}
4470

4571
func (*ClientStruct) FilterLogsWithRetry(client *ethclient.Client, query ethereum.FilterQuery) ([]types.Log, error) {

utils/options.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"path/filepath"
7+
"razor/core"
78
"razor/core/types"
89
"strings"
910

@@ -88,10 +89,6 @@ func (*GasStruct) GetGasPrice(client *ethclient.Client, config types.Configurati
8889
}
8990

9091
func (*GasStruct) GetGasLimit(transactionData types.TransactionOptions, txnOpts *bind.TransactOpts) (uint64, error) {
91-
if transactionData.Config.GasLimitOverride != 0 {
92-
log.Debugf("Taking the gas limit value = %d from config", transactionData.Config.GasLimitOverride)
93-
return transactionData.Config.GasLimitOverride, nil
94-
}
9592
if transactionData.MethodName == "" {
9693
return 0, nil
9794
}
@@ -115,7 +112,14 @@ func (*GasStruct) GetGasLimit(transactionData types.TransactionOptions, txnOpts
115112
}
116113
gasLimit, err := ClientInterface.EstimateGasWithRetry(transactionData.Client, msg)
117114
if err != nil {
118-
return 0, err
115+
log.Error("GetGasLimit: Error in getting gasLimit: ", err)
116+
//If estimateGas throws an error for a transaction than gasLimit should be picked up from the config
117+
if transactionData.Config.GasLimitOverride != 0 {
118+
log.Debugf("As there was an error from estimateGas, taking the gas limit value = %d from config", transactionData.Config.GasLimitOverride)
119+
return transactionData.Config.GasLimitOverride, nil
120+
}
121+
log.Debugf("As there was an error from estimateGas, using the hardcoded higher gas limit value = %d", core.HigherGasLimitValue)
122+
return core.HigherGasLimitValue, nil
119123
}
120124
log.Debug("Estimated Gas: ", gasLimit)
121125
return GasInterface.IncreaseGasLimitValue(transactionData.Client, gasLimit, transactionData.Config.GasLimitMultiplier)

utils/options_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/rand"
88
"errors"
99
"math/big"
10+
"razor/core"
1011
"razor/core/types"
1112
"razor/utils/mocks"
1213
"reflect"
@@ -400,8 +401,8 @@ func TestUtilsStruct_GetGasLimit(t *testing.T) {
400401
inputData: inputData,
401402
gasLimitErr: errors.New("gasLimit error"),
402403
},
403-
want: 0,
404-
wantErr: errors.New("gasLimit error"),
404+
want: core.HigherGasLimitValue,
405+
wantErr: nil,
405406
},
406407
}
407408
for _, tt := range tests {

0 commit comments

Comments
 (0)