Skip to content

Commit 980b994

Browse files
authored
Added gasLimit to override as a config parameter (#1063)
* Added gasLimitOverride config parameter * Taking the gas limit value from config if present * Added tests * Added default gas limit override value * Added gas limit overridr in readMe
1 parent c480c3e commit 980b994

13 files changed

+235
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ There are a set of parameters that are configurable. These include:
9999
- Gas Price: The value of gas price if you want to set manually. If you don't provide any value or simply keep it to 1, the razor client will automatically calculate the optimum gas price and send it.
100100
- Log Level: Normally debug logs are not logged into the log file. But if you want you can set `logLevel` to `debug` and fetch the debug logs.
101101
- Gas Limit: The value with which the gas limit will be multiplied while sending every transaction.
102+
- Gas Limit Override: This value would be used as a gas limit for all the transactions instead of estimating for each transaction.
102103
- RPC Timeout: This is the threshold number of seconds after which any contract and client calls will time out.
103104

104105
The config is set while the build is generated, but if you need to change any of the above parameter, you can use the `setConfig` command.

cmd/config-utils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func (*UtilsStruct) GetConfigData() (types.Configurations, error) {
4949
if err != nil {
5050
return config, err
5151
}
52+
gasLimitOverride, err := cmdUtils.GetGasLimitOverride()
53+
if err != nil {
54+
return config, err
55+
}
5256
rpcTimeout, err := cmdUtils.GetRPCTimeout()
5357
if err != nil {
5458
return config, err
@@ -60,6 +64,7 @@ func (*UtilsStruct) GetConfigData() (types.Configurations, error) {
6064
config.GasPrice = gasPrice
6165
config.LogLevel = logLevel
6266
config.GasLimitMultiplier = gasLimit
67+
config.GasLimitOverride = gasLimitOverride
6368
config.RPCTimeout = rpcTimeout
6469
utils.RPCTimeout = rpcTimeout
6570

@@ -189,6 +194,23 @@ func (*UtilsStruct) GetGasLimit() (float32, error) {
189194
return gasLimit, nil
190195
}
191196

197+
//This function returns the gas limit to override
198+
func (*UtilsStruct) GetGasLimitOverride() (uint64, error) {
199+
gasLimitOverride, err := flagSetUtils.GetRootUint64GasLimitOverride()
200+
if err != nil {
201+
return uint64(core.DefaultGasLimitOverride), err
202+
}
203+
if gasLimitOverride == 0 {
204+
if viper.IsSet("gasLimitOverride") {
205+
gasLimitOverride = viper.GetUint64("gasLimitOverride")
206+
} else {
207+
gasLimitOverride = uint64(core.DefaultGasLimitOverride)
208+
log.Debug("GasLimitOverride is not set, taking its default value ", gasLimitOverride)
209+
}
210+
}
211+
return gasLimitOverride, nil
212+
}
213+
192214
//This function returns the RPC timeout
193215
func (*UtilsStruct) GetRPCTimeout() (int64, error) {
194216
rpcTimeout, err := flagSetUtils.GetRootInt64RPCTimeout()

cmd/config-utils_test.go

Lines changed: 97 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,29 @@ func TestGetConfigData(t *testing.T) {
2626
WaitTime: 1,
2727
LogLevel: "debug",
2828
GasLimitMultiplier: 3,
29+
GasLimitOverride: 1000000,
2930
RPCTimeout: 10,
3031
}
3132

3233
type args struct {
33-
provider string
34-
providerErr error
35-
gasMultiplier float32
36-
gasMultiplierErr error
37-
bufferPercent int32
38-
bufferPercentErr error
39-
waitTime int32
40-
waitTimeErr error
41-
gasPrice int32
42-
gasPriceErr error
43-
logLevel string
44-
logLevelErr error
45-
gasLimit float32
46-
rpcTimeout int64
47-
rpcTimeoutErr error
48-
gasLimitErr error
34+
provider string
35+
providerErr error
36+
gasMultiplier float32
37+
gasMultiplierErr error
38+
bufferPercent int32
39+
bufferPercentErr error
40+
waitTime int32
41+
waitTimeErr error
42+
gasPrice int32
43+
gasPriceErr error
44+
logLevel string
45+
logLevelErr error
46+
gasLimit float32
47+
gasLimitOverride uint64
48+
gasLimitOverrideErr error
49+
rpcTimeout int64
50+
rpcTimeoutErr error
51+
gasLimitErr error
4952
}
5053
tests := []struct {
5154
name string
@@ -56,13 +59,14 @@ func TestGetConfigData(t *testing.T) {
5659
{
5760
name: "Test 1: When GetConfigData function executes successfully",
5861
args: args{
59-
provider: "",
60-
gasMultiplier: 1,
61-
bufferPercent: 20,
62-
waitTime: 1,
63-
logLevel: "debug",
64-
gasLimit: 3,
65-
rpcTimeout: 10,
62+
provider: "",
63+
gasMultiplier: 1,
64+
bufferPercent: 20,
65+
waitTime: 1,
66+
logLevel: "debug",
67+
gasLimit: 3,
68+
gasLimitOverride: 1000000,
69+
rpcTimeout: 10,
6670
},
6771
want: configData,
6872
wantErr: nil,
@@ -131,6 +135,14 @@ func TestGetConfigData(t *testing.T) {
131135
want: config,
132136
wantErr: errors.New("rpcTimeout error"),
133137
},
138+
{
139+
name: "When there is an error in getitng gasLimitOverride",
140+
args: args{
141+
gasLimitOverrideErr: errors.New("gasLimitOverride error"),
142+
},
143+
want: config,
144+
wantErr: errors.New("gasLimitOverride error"),
145+
},
134146
}
135147
for _, tt := range tests {
136148
t.Run(tt.name, func(t *testing.T) {
@@ -143,6 +155,7 @@ func TestGetConfigData(t *testing.T) {
143155
cmdUtilsMock.On("GetGasPrice").Return(tt.args.gasPrice, tt.args.gasPriceErr)
144156
cmdUtilsMock.On("GetLogLevel").Return(tt.args.logLevel, tt.args.logLevelErr)
145157
cmdUtilsMock.On("GetGasLimit").Return(tt.args.gasLimit, tt.args.gasLimitErr)
158+
cmdUtilsMock.On("GetGasLimitOverride").Return(tt.args.gasLimitOverride, tt.args.gasLimitOverrideErr)
146159
cmdUtilsMock.On("GetBufferPercent").Return(tt.args.bufferPercent, tt.args.bufferPercentErr)
147160
cmdUtilsMock.On("GetRPCTimeout").Return(tt.args.rpcTimeout, tt.args.rpcTimeoutErr)
148161

@@ -287,6 +300,67 @@ func TestGetGasLimit(t *testing.T) {
287300
}
288301
}
289302

303+
func TestGetGasLimitOverride(t *testing.T) {
304+
type args struct {
305+
gasLimitOverride uint64
306+
gasLimitOverrideErr error
307+
}
308+
tests := []struct {
309+
name string
310+
args args
311+
want uint64
312+
wantErr error
313+
}{
314+
{
315+
name: "Test 1: When getGasLimitOverride function executes successfully",
316+
args: args{
317+
gasLimitOverride: 5000000,
318+
},
319+
want: 5000000,
320+
wantErr: nil,
321+
},
322+
{
323+
name: "Test 2: When gasLimitOverride is 0",
324+
args: args{
325+
gasLimitOverride: 0,
326+
},
327+
want: 0,
328+
wantErr: nil,
329+
},
330+
{
331+
name: "Test 3: When there is an error in getting gasLimitOverride",
332+
args: args{
333+
gasLimitOverrideErr: errors.New("gasLimitOverride error"),
334+
},
335+
want: 0,
336+
wantErr: errors.New("gasLimitOverride error"),
337+
},
338+
}
339+
for _, tt := range tests {
340+
t.Run(tt.name, func(t *testing.T) {
341+
flagSetUtilsMock := new(mocks.FlagSetInterface)
342+
flagSetUtils = flagSetUtilsMock
343+
344+
flagSetUtilsMock.On("GetRootUint64GasLimitOverride").Return(tt.args.gasLimitOverride, tt.args.gasLimitOverrideErr)
345+
utils := &UtilsStruct{}
346+
347+
got, err := utils.GetGasLimitOverride()
348+
if got != tt.want {
349+
t.Errorf("getGasLimitOverride() got = %v, want %v", got, tt.want)
350+
}
351+
if err == nil || tt.wantErr == nil {
352+
if err != tt.wantErr {
353+
t.Errorf("Error for getGasLimitOverride function, got = %v, want = %v", err, tt.wantErr)
354+
}
355+
} else {
356+
if err.Error() != tt.wantErr.Error() {
357+
t.Errorf("Error for getGasLimitOverride function, got = %v, want = %v", err, tt.wantErr)
358+
}
359+
}
360+
})
361+
}
362+
}
363+
290364
func TestGetGasPrice(t *testing.T) {
291365
type args struct {
292366
gasPrice int32

cmd/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ type FlagSetInterface interface {
194194
GetInt32Wait(flagSet *pflag.FlagSet) (int32, error)
195195
GetInt32GasPrice(flagSet *pflag.FlagSet) (int32, error)
196196
GetFloat32GasLimit(flagSet *pflag.FlagSet) (float32, error)
197+
GetUint64GasLimitOverride(flagSet *pflag.FlagSet) (uint64, error)
197198
GetStringLogLevel(flagSet *pflag.FlagSet) (string, error)
198199
GetInt64RPCTimeout(flagSet *pflag.FlagSet) (int64, error)
199200
GetUint32BountyId(flagSet *pflag.FlagSet) (uint32, error)
@@ -204,6 +205,7 @@ type FlagSetInterface interface {
204205
GetRootInt32GasPrice() (int32, error)
205206
GetRootStringLogLevel() (string, error)
206207
GetRootFloat32GasLimit() (float32, error)
208+
GetRootUint64GasLimitOverride() (uint64, error)
207209
GetRootInt64RPCTimeout() (int64, error)
208210
GetStringFrom(flagSet *pflag.FlagSet) (string, error)
209211
GetStringTo(flagSet *pflag.FlagSet) (string, error)
@@ -241,6 +243,7 @@ type UtilsCmdInterface interface {
241243
GetGasPrice() (int32, error)
242244
GetLogLevel() (string, error)
243245
GetGasLimit() (float32, error)
246+
GetGasLimitOverride() (uint64, error)
244247
GetBufferPercent() (int32, error)
245248
GetRPCTimeout() (int64, error)
246249
GetConfigData() (types.Configurations, error)

cmd/mocks/flag_set_interface.go

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mocks/utils_cmd_interface.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var (
2020
GasPrice int32
2121
LogLevel string
2222
GasLimitMultiplier float32
23+
GasLimitOverride uint64
2324
LogFile string
2425
RPCTimeout int64
2526
)
@@ -61,6 +62,7 @@ func init() {
6162
rootCmd.PersistentFlags().Int32VarP(&GasPrice, "gasprice", "", -1, "gas price")
6263
rootCmd.PersistentFlags().StringVarP(&LogLevel, "logLevel", "", "", "log level")
6364
rootCmd.PersistentFlags().Float32VarP(&GasLimitMultiplier, "gasLimit", "", -1, "gas limit percentage increase")
65+
rootCmd.PersistentFlags().Uint64VarP(&GasLimitOverride, "gasLimitOverride", "", 0, "gas limit to be over ridden for a transaction")
6466
rootCmd.PersistentFlags().StringVarP(&LogFile, "logFile", "", "", "name of log file")
6567
rootCmd.PersistentFlags().Int64VarP(&RPCTimeout, "rpcTimeout", "", 0, "RPC timeout if its not responding")
6668
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
@@ -110,5 +112,6 @@ func setLogLevel() {
110112
log.Debugf("Gas Price: %d", config.GasPrice)
111113
log.Debugf("Log Level: %s", config.LogLevel)
112114
log.Debugf("Gas Limit: %.2f", config.GasLimitMultiplier)
115+
log.Debugf("Gas Limit Override: %d", config.GasLimitOverride)
113116
log.Debugf("RPC Timeout: %d", config.RPCTimeout)
114117
}

cmd/setConfig.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func (*UtilsStruct) SetConfig(flagSet *pflag.FlagSet) error {
5454
if err != nil {
5555
return err
5656
}
57+
gasLimitOverride, err := flagSetUtils.GetUint64GasLimitOverride(flagSet)
58+
if err != nil {
59+
return err
60+
}
5761
gasLimit, err := flagSetUtils.GetFloat32GasLimit(flagSet)
5862
if err != nil {
5963
return err
@@ -117,17 +121,21 @@ func (*UtilsStruct) SetConfig(flagSet *pflag.FlagSet) error {
117121
if gasLimit != -1 {
118122
viper.Set("gasLimit", gasLimit)
119123
}
124+
if gasLimitOverride != 0 {
125+
viper.Set("gasLimitOverride", gasLimitOverride)
126+
}
120127
if rpcTimeout != 0 {
121128
viper.Set("rpcTimeout", rpcTimeout)
122129
}
123-
if provider == "" && gasMultiplier == -1 && bufferPercent == 0 && waitTime == -1 && gasPrice == -1 && logLevel == "" && gasLimit == -1 && rpcTimeout == 0 {
130+
if provider == "" && gasMultiplier == -1 && bufferPercent == 0 && waitTime == -1 && gasPrice == -1 && logLevel == "" && gasLimit == -1 && gasLimitOverride == 0 && rpcTimeout == 0 {
124131
viper.Set("provider", core.DefaultProvider)
125132
viper.Set("gasmultiplier", core.DefaultGasMultiplier)
126133
viper.Set("buffer", core.DefaultBufferPercent)
127134
viper.Set("wait", core.DefaultWaitTime)
128135
viper.Set("gasprice", core.DefaultGasPrice)
129136
viper.Set("logLevel", core.DefaultLogLevel)
130137
viper.Set("gasLimit", core.DefaultGasLimit)
138+
viper.Set("gasLimitOverride", core.DefaultGasLimitOverride)
131139
viper.Set("rpcTimeout", core.DefaultRPCTimeout)
132140
//viper.Set("exposeMetricsPort", "")
133141
log.Info("Config values set to default. Use setConfig to modify the values.")
@@ -152,6 +160,7 @@ func init() {
152160
GasPrice int32
153161
LogLevel string
154162
GasLimitMultiplier float32
163+
GasLimitOverride uint64
155164
RPCTimeout int64
156165
ExposeMetrics string
157166
CertFile string
@@ -164,6 +173,7 @@ func init() {
164173
setConfig.Flags().Int32VarP(&GasPrice, "gasprice", "", -1, "custom gas price")
165174
setConfig.Flags().StringVarP(&LogLevel, "logLevel", "", "", "log level")
166175
setConfig.Flags().Float32VarP(&GasLimitMultiplier, "gasLimit", "", -1, "gas limit percentage increase")
176+
setConfig.Flags().Uint64VarP(&GasLimitOverride, "gasLimitOverride", "", 0, "gas limit to be over ridden for a transaction")
167177
setConfig.Flags().Int64VarP(&RPCTimeout, "rpcTimeout", "", 0, "RPC timeout if its not responding")
168178
setConfig.Flags().StringVarP(&ExposeMetrics, "exposeMetrics", "", "", "port number")
169179
setConfig.Flags().StringVarP(&CertFile, "certFile", "", "", "ssl certificate path")

0 commit comments

Comments
 (0)