Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

precompile: improve test structure #517

Merged
39 changes: 13 additions & 26 deletions commontype/fee_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@ import (
"github.com/stretchr/testify/require"
)

var validFeeConfig = 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),
}

func TestVerify(t *testing.T) {
tests := []struct {
name string
Expand All @@ -47,43 +34,43 @@ func TestVerify(t *testing.T) {
},
{
name: "invalid GasLimit in FeeConfig",
config: func() *FeeConfig { c := validFeeConfig; c.GasLimit = big.NewInt(0); return &c }(),
config: func() *FeeConfig { c := ValidTestFeeConfig; c.GasLimit = big.NewInt(0); return &c }(),
expectedError: "gasLimit = 0 cannot be less than or equal to 0",
},
{
name: "invalid TargetBlockRate in FeeConfig",
config: func() *FeeConfig { c := validFeeConfig; c.TargetBlockRate = 0; return &c }(),
config: func() *FeeConfig { c := ValidTestFeeConfig; c.TargetBlockRate = 0; return &c }(),
expectedError: "targetBlockRate = 0 cannot be less than or equal to 0",
},
{
name: "invalid MinBaseFee in FeeConfig",
config: func() *FeeConfig { c := validFeeConfig; c.MinBaseFee = big.NewInt(-1); return &c }(),
config: func() *FeeConfig { c := ValidTestFeeConfig; c.MinBaseFee = big.NewInt(-1); return &c }(),
expectedError: "minBaseFee = -1 cannot be less than 0",
},
{
name: "invalid TargetGas in FeeConfig",
config: func() *FeeConfig { c := validFeeConfig; c.TargetGas = big.NewInt(0); return &c }(),
config: func() *FeeConfig { c := ValidTestFeeConfig; c.TargetGas = big.NewInt(0); return &c }(),
expectedError: "targetGas = 0 cannot be less than or equal to 0",
},
{
name: "invalid BaseFeeChangeDenominator in FeeConfig",
config: func() *FeeConfig { c := validFeeConfig; c.BaseFeeChangeDenominator = big.NewInt(0); return &c }(),
config: func() *FeeConfig { c := ValidTestFeeConfig; c.BaseFeeChangeDenominator = big.NewInt(0); return &c }(),
expectedError: "baseFeeChangeDenominator = 0 cannot be less than or equal to 0",
},
{
name: "invalid MinBlockGasCost in FeeConfig",
config: func() *FeeConfig { c := validFeeConfig; c.MinBlockGasCost = big.NewInt(-1); return &c }(),
config: func() *FeeConfig { c := ValidTestFeeConfig; c.MinBlockGasCost = big.NewInt(-1); return &c }(),
expectedError: "minBlockGasCost = -1 cannot be less than 0",
},
{
name: "valid FeeConfig",
config: &validFeeConfig,
config: &ValidTestFeeConfig,
expectedError: "",
},
{
name: "MinBlockGasCost bigger than MaxBlockGasCost in FeeConfig",
config: func() *FeeConfig {
c := validFeeConfig
c := ValidTestFeeConfig
c.MinBlockGasCost = big.NewInt(2)
c.MaxBlockGasCost = big.NewInt(1)
return &c
Expand All @@ -92,7 +79,7 @@ func TestVerify(t *testing.T) {
},
{
name: "invalid BlockGasCostStep in FeeConfig",
config: func() *FeeConfig { c := validFeeConfig; c.BlockGasCostStep = big.NewInt(-1); return &c }(),
config: func() *FeeConfig { c := ValidTestFeeConfig; c.BlockGasCostStep = big.NewInt(-1); return &c }(),
expectedError: "blockGasCostStep = -1 cannot be less than 0",
},
}
Expand All @@ -119,7 +106,7 @@ func TestEqual(t *testing.T) {
}{
{
name: "equal",
a: &validFeeConfig,
a: &ValidTestFeeConfig,
b: &FeeConfig{
GasLimit: big.NewInt(8_000_000),
TargetBlockRate: 2, // in seconds
Expand All @@ -136,13 +123,13 @@ func TestEqual(t *testing.T) {
},
{
name: "not equal",
a: &validFeeConfig,
b: func() *FeeConfig { c := validFeeConfig; c.GasLimit = big.NewInt(1); return &c }(),
a: &ValidTestFeeConfig,
b: func() *FeeConfig { c := ValidTestFeeConfig; c.GasLimit = big.NewInt(1); return &c }(),
expected: false,
},
{
name: "not equal nil",
a: &validFeeConfig,
a: &ValidTestFeeConfig,
b: nil,
expected: false,
},
Expand Down
19 changes: 19 additions & 0 deletions commontype/test_fee_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package commontype

import "math/big"

var ValidTestFeeConfig = 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),
}
20 changes: 20 additions & 0 deletions core/state/test_statedb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package state

import (
"testing"

"github.com/ava-labs/subnet-evm/ethdb/memorydb"
"github.com/ava-labs/subnet-evm/precompile/contract"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

func NewTestStateDB(t *testing.T) contract.StateDB {
db := memorydb.New()
stateDB, err := New(common.Hash{}, NewDatabase(db), nil)
require.NoError(t, err)
return stateDB
}
Loading