github.com/MetalBlockchain/subnet-evm@v0.4.9/commontype/fee_config_test.go (about)

     1  // (c) 2019-2022, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package commontype
     5  
     6  import (
     7  	"math/big"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  var validFeeConfig = FeeConfig{
    14  	GasLimit:        big.NewInt(8_000_000),
    15  	TargetBlockRate: 2, // in seconds
    16  
    17  	MinBaseFee:               big.NewInt(25_000_000_000),
    18  	TargetGas:                big.NewInt(15_000_000),
    19  	BaseFeeChangeDenominator: big.NewInt(36),
    20  
    21  	MinBlockGasCost:  big.NewInt(0),
    22  	MaxBlockGasCost:  big.NewInt(1_000_000),
    23  	BlockGasCostStep: big.NewInt(200_000),
    24  }
    25  
    26  func TestVerify(t *testing.T) {
    27  	tests := []struct {
    28  		name          string
    29  		config        *FeeConfig
    30  		expectedError string
    31  	}{
    32  		{
    33  			name: "nil gasLimit in FeeConfig",
    34  			config: &FeeConfig{
    35  				// GasLimit:        big.NewInt(8_000_000)
    36  				TargetBlockRate: 2, // in seconds
    37  
    38  				MinBaseFee:               big.NewInt(25_000_000_000),
    39  				TargetGas:                big.NewInt(15_000_000),
    40  				BaseFeeChangeDenominator: big.NewInt(36),
    41  
    42  				MinBlockGasCost:  big.NewInt(0),
    43  				MaxBlockGasCost:  big.NewInt(1_000_000),
    44  				BlockGasCostStep: big.NewInt(200_000),
    45  			},
    46  			expectedError: "gasLimit cannot be nil",
    47  		},
    48  		{
    49  			name:          "invalid GasLimit in FeeConfig",
    50  			config:        func() *FeeConfig { c := validFeeConfig; c.GasLimit = big.NewInt(0); return &c }(),
    51  			expectedError: "gasLimit = 0 cannot be less than or equal to 0",
    52  		},
    53  		{
    54  			name:          "invalid TargetBlockRate in FeeConfig",
    55  			config:        func() *FeeConfig { c := validFeeConfig; c.TargetBlockRate = 0; return &c }(),
    56  			expectedError: "targetBlockRate = 0 cannot be less than or equal to 0",
    57  		},
    58  		{
    59  			name:          "invalid MinBaseFee in FeeConfig",
    60  			config:        func() *FeeConfig { c := validFeeConfig; c.MinBaseFee = big.NewInt(-1); return &c }(),
    61  			expectedError: "minBaseFee = -1 cannot be less than 0",
    62  		},
    63  		{
    64  			name:          "invalid TargetGas in FeeConfig",
    65  			config:        func() *FeeConfig { c := validFeeConfig; c.TargetGas = big.NewInt(0); return &c }(),
    66  			expectedError: "targetGas = 0 cannot be less than or equal to 0",
    67  		},
    68  		{
    69  			name:          "invalid BaseFeeChangeDenominator in FeeConfig",
    70  			config:        func() *FeeConfig { c := validFeeConfig; c.BaseFeeChangeDenominator = big.NewInt(0); return &c }(),
    71  			expectedError: "baseFeeChangeDenominator = 0 cannot be less than or equal to 0",
    72  		},
    73  		{
    74  			name:          "invalid MinBlockGasCost in FeeConfig",
    75  			config:        func() *FeeConfig { c := validFeeConfig; c.MinBlockGasCost = big.NewInt(-1); return &c }(),
    76  			expectedError: "minBlockGasCost = -1 cannot be less than 0",
    77  		},
    78  		{
    79  			name:          "valid FeeConfig",
    80  			config:        &validFeeConfig,
    81  			expectedError: "",
    82  		},
    83  		{
    84  			name: "MinBlockGasCost bigger than MaxBlockGasCost in FeeConfig",
    85  			config: func() *FeeConfig {
    86  				c := validFeeConfig
    87  				c.MinBlockGasCost = big.NewInt(2)
    88  				c.MaxBlockGasCost = big.NewInt(1)
    89  				return &c
    90  			}(),
    91  			expectedError: "minBlockGasCost = 2 cannot be greater than maxBlockGasCost = 1",
    92  		},
    93  		{
    94  			name:          "invalid BlockGasCostStep in FeeConfig",
    95  			config:        func() *FeeConfig { c := validFeeConfig; c.BlockGasCostStep = big.NewInt(-1); return &c }(),
    96  			expectedError: "blockGasCostStep = -1 cannot be less than 0",
    97  		},
    98  	}
    99  
   100  	for _, test := range tests {
   101  		t.Run(test.name, func(t *testing.T) {
   102  			err := test.config.Verify()
   103  			if test.expectedError == "" {
   104  				require.NoError(t, err)
   105  			} else {
   106  				require.Error(t, err)
   107  				require.Contains(t, err.Error(), test.expectedError)
   108  			}
   109  		})
   110  	}
   111  }
   112  
   113  func TestEqual(t *testing.T) {
   114  	tests := []struct {
   115  		name     string
   116  		a        *FeeConfig
   117  		b        *FeeConfig
   118  		expected bool
   119  	}{
   120  		{
   121  			name: "equal",
   122  			a:    &validFeeConfig,
   123  			b: &FeeConfig{
   124  				GasLimit:        big.NewInt(8_000_000),
   125  				TargetBlockRate: 2, // in seconds
   126  
   127  				MinBaseFee:               big.NewInt(25_000_000_000),
   128  				TargetGas:                big.NewInt(15_000_000),
   129  				BaseFeeChangeDenominator: big.NewInt(36),
   130  
   131  				MinBlockGasCost:  big.NewInt(0),
   132  				MaxBlockGasCost:  big.NewInt(1_000_000),
   133  				BlockGasCostStep: big.NewInt(200_000),
   134  			},
   135  			expected: true,
   136  		},
   137  		{
   138  			name:     "not equal",
   139  			a:        &validFeeConfig,
   140  			b:        func() *FeeConfig { c := validFeeConfig; c.GasLimit = big.NewInt(1); return &c }(),
   141  			expected: false,
   142  		},
   143  		{
   144  			name:     "not equal nil",
   145  			a:        &validFeeConfig,
   146  			b:        nil,
   147  			expected: false,
   148  		},
   149  	}
   150  
   151  	for _, test := range tests {
   152  		t.Run(test.name, func(t *testing.T) {
   153  			require.Equal(t, test.expected, test.a.Equal(test.b))
   154  		})
   155  	}
   156  }