From 009c86f00360ac00e724681709c3a26dec76d5f8 Mon Sep 17 00:00:00 2001 From: Ceyhun Onur Date: Wed, 1 Feb 2023 18:28:17 +0300 Subject: [PATCH] add nil checks (#473) * add nil checks * add unit test * use non nil config * use non-nil configs --------- Co-authored-by: aaronbuchwald --- commontype/fee_config.go | 17 +++++++++++++++++ commontype/fee_config_test.go | 16 ++++++++++++++++ params/precompile_config_test.go | 24 +++++++++++++++--------- precompile/config_test.go | 9 ++++++--- 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/commontype/fee_config.go b/commontype/fee_config.go index f09eceeff9..e1dee3182a 100644 --- a/commontype/fee_config.go +++ b/commontype/fee_config.go @@ -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) diff --git a/commontype/fee_config_test.go b/commontype/fee_config_test.go index 57c8a58b06..997cb708e3 100644 --- a/commontype/fee_config_test.go +++ b/commontype/fee_config_test.go @@ -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 }(), diff --git a/params/precompile_config_test.go b/params/precompile_config_test.go index 9802bf9648..42611899cc 100644 --- a/params/precompile_config_test.go +++ b/params/precompile_config_test.go @@ -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", @@ -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", @@ -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", }, diff --git a/precompile/config_test.go b/precompile/config_test.go index a396cec543..2772009390 100644 --- a/precompile/config_test.go +++ b/precompile/config_test.go @@ -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", }, {