Skip to content

Commit

Permalink
add nil checks (#473)
Browse files Browse the repository at this point in the history
* add nil checks

* add unit test

* use non nil config

* use non-nil configs

---------

Co-authored-by: aaronbuchwald <[email protected]>
  • Loading branch information
ceyonur and aaronbuchwald authored Feb 1, 2023
1 parent 01a16a0 commit 009c86f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
17 changes: 17 additions & 0 deletions commontype/fee_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ var EmptyFeeConfig = FeeConfig{}

// Verify checks fields of this config to ensure a valid fee configuration is provided.
func (f *FeeConfig) Verify() error {
switch {
case f.GasLimit == nil:
return fmt.Errorf("gasLimit cannot be nil")
case f.MinBaseFee == nil:
return fmt.Errorf("minBaseFee cannot be nil")
case f.TargetGas == nil:
return fmt.Errorf("targetGas cannot be nil")
case f.BaseFeeChangeDenominator == nil:
return fmt.Errorf("baseFeeChangeDenominator cannot be nil")
case f.MinBlockGasCost == nil:
return fmt.Errorf("minBlockGasCost cannot be nil")
case f.MaxBlockGasCost == nil:
return fmt.Errorf("maxBlockGasCost cannot be nil")
case f.BlockGasCostStep == nil:
return fmt.Errorf("blockGasCostStep cannot be nil")
}

switch {
case f.GasLimit.Cmp(common.Big0) != 1:
return fmt.Errorf("gasLimit = %d cannot be less than or equal to 0", f.GasLimit)
Expand Down
16 changes: 16 additions & 0 deletions commontype/fee_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ func TestVerify(t *testing.T) {
config *FeeConfig
expectedError string
}{
{
name: "nil gasLimit in FeeConfig",
config: &FeeConfig{
// GasLimit: big.NewInt(8_000_000)
TargetBlockRate: 2, // in seconds

MinBaseFee: big.NewInt(25_000_000_000),
TargetGas: big.NewInt(15_000_000),
BaseFeeChangeDenominator: big.NewInt(36),

MinBlockGasCost: big.NewInt(0),
MaxBlockGasCost: big.NewInt(1_000_000),
BlockGasCostStep: big.NewInt(200_000),
},
expectedError: "gasLimit cannot be nil",
},
{
name: "invalid GasLimit in FeeConfig",
config: func() *FeeConfig { c := validFeeConfig; c.GasLimit = big.NewInt(0); return &c }(),
Expand Down
24 changes: 15 additions & 9 deletions params/precompile_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ func TestVerifyPrecompileUpgrades(t *testing.T) {
upgrades: []PrecompileUpgrade{
{
FeeManagerConfig: precompile.NewFeeManagerConfig(big.NewInt(3), admins, nil,
&commontype.FeeConfig{
GasLimit: big.NewInt(-1),
}),
func() *commontype.FeeConfig {
feeConfig := DefaultFeeConfig
feeConfig.GasLimit = big.NewInt(-1)
return &feeConfig
}()),
},
},
expectedError: "gasLimit = -1 cannot be less than or equal to 0",
Expand All @@ -130,9 +132,11 @@ func TestVerifyPrecompileUpgrades(t *testing.T) {
upgrades: []PrecompileUpgrade{
{
FeeManagerConfig: precompile.NewFeeManagerConfig(big.NewInt(3), admins, nil,
&commontype.FeeConfig{
GasLimit: big.NewInt(0),
}),
func() *commontype.FeeConfig {
feeConfig := DefaultFeeConfig
feeConfig.GasLimit = common.Big0
return &feeConfig
}()),
},
},
expectedError: "gasLimit = 0 cannot be less than or equal to 0",
Expand Down Expand Up @@ -173,9 +177,11 @@ func TestVerifyPrecompiles(t *testing.T) {
name: "invalid initial fee manager config",
upgrade: PrecompileUpgrade{
FeeManagerConfig: precompile.NewFeeManagerConfig(big.NewInt(3), admins, nil,
&commontype.FeeConfig{
GasLimit: big.NewInt(-1),
}),
func() *commontype.FeeConfig {
feeConfig := DefaultFeeConfig
feeConfig.GasLimit = big.NewInt(-1)
return &feeConfig
}()),
},
expectedError: "gasLimit = -1 cannot be less than or equal to 0",
},
Expand Down
9 changes: 6 additions & 3 deletions precompile/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ func TestVerifyPrecompileUpgrades(t *testing.T) {
{
name: "invalid initial fee manager config",
config: NewFeeManagerConfig(big.NewInt(3), admins, nil,
&commontype.FeeConfig{
GasLimit: big.NewInt(0),
}),
func() *commontype.FeeConfig {
feeConfig := validFeeConfig
feeConfig.GasLimit = big.NewInt(0)
return &feeConfig
}()),

expectedError: "gasLimit = 0 cannot be less than or equal to 0",
},
{
Expand Down

0 comments on commit 009c86f

Please sign in to comment.