github.com/MetalBlockchain/subnet-evm@v0.4.9/precompile/config_test.go (about)

     1  // (c) 2022 Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package precompile
     5  
     6  import (
     7  	"math/big"
     8  	"testing"
     9  
    10  	"github.com/MetalBlockchain/subnet-evm/commontype"
    11  	"github.com/ethereum/go-ethereum/common"
    12  	"github.com/ethereum/go-ethereum/common/math"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  var validFeeConfig = commontype.FeeConfig{
    17  	GasLimit:        big.NewInt(8_000_000),
    18  	TargetBlockRate: 2, // in seconds
    19  
    20  	MinBaseFee:               big.NewInt(25_000_000_000),
    21  	TargetGas:                big.NewInt(15_000_000),
    22  	BaseFeeChangeDenominator: big.NewInt(36),
    23  
    24  	MinBlockGasCost:  big.NewInt(0),
    25  	MaxBlockGasCost:  big.NewInt(1_000_000),
    26  	BlockGasCostStep: big.NewInt(200_000),
    27  }
    28  
    29  func TestVerifyPrecompileUpgrades(t *testing.T) {
    30  	admins := []common.Address{{1}}
    31  	enableds := []common.Address{{2}}
    32  	tests := []struct {
    33  		name          string
    34  		config        StatefulPrecompileConfig
    35  		expectedError string
    36  	}{
    37  		{
    38  			name:          "invalid allow list config in tx allowlist",
    39  			config:        NewTxAllowListConfig(big.NewInt(3), admins, admins),
    40  			expectedError: "cannot set address",
    41  		},
    42  		{
    43  			name:          "nil member allow list config in tx allowlist",
    44  			config:        NewTxAllowListConfig(big.NewInt(3), nil, nil),
    45  			expectedError: "",
    46  		},
    47  		{
    48  			name:          "empty member allow list config in tx allowlist",
    49  			config:        NewTxAllowListConfig(big.NewInt(3), []common.Address{}, []common.Address{}),
    50  			expectedError: "",
    51  		},
    52  		{
    53  			name:          "valid allow list config in tx allowlist",
    54  			config:        NewTxAllowListConfig(big.NewInt(3), admins, enableds),
    55  			expectedError: "",
    56  		},
    57  		{
    58  			name:          "invalid allow list config in deployer allowlist",
    59  			config:        NewContractDeployerAllowListConfig(big.NewInt(3), admins, admins),
    60  			expectedError: "cannot set address",
    61  		},
    62  		{
    63  			name:          "invalid allow list config in native minter allowlist",
    64  			config:        NewContractNativeMinterConfig(big.NewInt(3), admins, admins, nil),
    65  			expectedError: "cannot set address",
    66  		},
    67  		{
    68  			name:          "duplicate admins in config in native minter allowlist",
    69  			config:        NewContractNativeMinterConfig(big.NewInt(3), append(admins, admins[0]), enableds, nil),
    70  			expectedError: "duplicate address",
    71  		},
    72  		{
    73  			name:          "duplicate enableds in config in native minter allowlist",
    74  			config:        NewContractNativeMinterConfig(big.NewInt(3), admins, append(enableds, enableds[0]), nil),
    75  			expectedError: "duplicate address",
    76  		},
    77  		{
    78  			name:          "invalid allow list config in fee manager allowlist",
    79  			config:        NewFeeManagerConfig(big.NewInt(3), admins, admins, nil),
    80  			expectedError: "cannot set address",
    81  		},
    82  		{
    83  			name: "invalid initial fee manager config",
    84  			config: NewFeeManagerConfig(big.NewInt(3), admins, nil,
    85  				func() *commontype.FeeConfig {
    86  					feeConfig := validFeeConfig
    87  					feeConfig.GasLimit = big.NewInt(0)
    88  					return &feeConfig
    89  				}()),
    90  
    91  			expectedError: "gasLimit = 0 cannot be less than or equal to 0",
    92  		},
    93  		{
    94  			name: "nil amount in native minter config",
    95  			config: NewContractNativeMinterConfig(big.NewInt(3), admins, nil,
    96  				map[common.Address]*math.HexOrDecimal256{
    97  					common.HexToAddress("0x01"): math.NewHexOrDecimal256(123),
    98  					common.HexToAddress("0x02"): nil,
    99  				}),
   100  			expectedError: "initial mint cannot contain nil",
   101  		},
   102  		{
   103  			name: "negative amount in native minter config",
   104  			config: NewContractNativeMinterConfig(big.NewInt(3), admins, nil,
   105  				map[common.Address]*math.HexOrDecimal256{
   106  					common.HexToAddress("0x01"): math.NewHexOrDecimal256(123),
   107  					common.HexToAddress("0x02"): math.NewHexOrDecimal256(-1),
   108  				}),
   109  			expectedError: "initial mint cannot contain invalid amount",
   110  		},
   111  		{
   112  			name:          "duplicate enableds in config in reward manager allowlist",
   113  			config:        NewRewardManagerConfig(big.NewInt(3), admins, append(enableds, enableds[0]), nil),
   114  			expectedError: "duplicate address",
   115  		},
   116  		{
   117  			name: "both reward mechanisms should not be activated at the same time in reward manager",
   118  			config: NewRewardManagerConfig(big.NewInt(3), admins, enableds, &InitialRewardConfig{
   119  				AllowFeeRecipients: true,
   120  				RewardAddress:      common.HexToAddress("0x01"),
   121  			}),
   122  			expectedError: ErrCannotEnableBothRewards.Error(),
   123  		},
   124  	}
   125  	for _, tt := range tests {
   126  		t.Run(tt.name, func(t *testing.T) {
   127  			require := require.New(t)
   128  
   129  			err := tt.config.Verify()
   130  			if tt.expectedError == "" {
   131  				require.NoError(err)
   132  			} else {
   133  				require.ErrorContains(err, tt.expectedError)
   134  			}
   135  		})
   136  	}
   137  }
   138  
   139  func TestEqualTxAllowListConfig(t *testing.T) {
   140  	admins := []common.Address{{1}}
   141  	enableds := []common.Address{{2}}
   142  	tests := []struct {
   143  		name     string
   144  		config   StatefulPrecompileConfig
   145  		other    StatefulPrecompileConfig
   146  		expected bool
   147  	}{
   148  		{
   149  			name:     "non-nil config and nil other",
   150  			config:   NewTxAllowListConfig(big.NewInt(3), admins, enableds),
   151  			other:    nil,
   152  			expected: false,
   153  		},
   154  		{
   155  			name:     "different type",
   156  			config:   NewTxAllowListConfig(big.NewInt(3), admins, enableds),
   157  			other:    NewContractDeployerAllowListConfig(big.NewInt(3), admins, enableds),
   158  			expected: false,
   159  		},
   160  		{
   161  			name:     "different admin",
   162  			config:   NewTxAllowListConfig(big.NewInt(3), admins, enableds),
   163  			other:    NewTxAllowListConfig(big.NewInt(3), []common.Address{{3}}, enableds),
   164  			expected: false,
   165  		},
   166  		{
   167  			name:     "different enabled",
   168  			config:   NewTxAllowListConfig(big.NewInt(3), admins, enableds),
   169  			other:    NewTxAllowListConfig(big.NewInt(3), admins, []common.Address{{3}}),
   170  			expected: false,
   171  		},
   172  		{
   173  			name:     "different timestamp",
   174  			config:   NewTxAllowListConfig(big.NewInt(3), admins, enableds),
   175  			other:    NewTxAllowListConfig(big.NewInt(4), admins, enableds),
   176  			expected: false,
   177  		},
   178  		{
   179  			name:     "same config",
   180  			config:   NewTxAllowListConfig(big.NewInt(3), admins, enableds),
   181  			other:    NewTxAllowListConfig(big.NewInt(3), admins, enableds),
   182  			expected: true,
   183  		},
   184  	}
   185  	for _, tt := range tests {
   186  		t.Run(tt.name, func(t *testing.T) {
   187  			require := require.New(t)
   188  
   189  			require.Equal(tt.expected, tt.config.Equal(tt.other))
   190  		})
   191  	}
   192  }
   193  
   194  func TestEqualContractDeployerAllowListConfig(t *testing.T) {
   195  	admins := []common.Address{{1}}
   196  	enableds := []common.Address{{2}}
   197  	tests := []struct {
   198  		name     string
   199  		config   StatefulPrecompileConfig
   200  		other    StatefulPrecompileConfig
   201  		expected bool
   202  	}{
   203  		{
   204  			name:     "non-nil config and nil other",
   205  			config:   NewContractDeployerAllowListConfig(big.NewInt(3), admins, enableds),
   206  			other:    nil,
   207  			expected: false,
   208  		},
   209  		{
   210  			name:     "different type",
   211  			config:   NewContractDeployerAllowListConfig(big.NewInt(3), admins, enableds),
   212  			other:    NewTxAllowListConfig(big.NewInt(3), admins, enableds),
   213  			expected: false,
   214  		},
   215  		{
   216  			name:     "different admin",
   217  			config:   NewContractDeployerAllowListConfig(big.NewInt(3), admins, enableds),
   218  			other:    NewContractDeployerAllowListConfig(big.NewInt(3), []common.Address{{3}}, enableds),
   219  			expected: false,
   220  		},
   221  		{
   222  			name:     "different enabled",
   223  			config:   NewContractDeployerAllowListConfig(big.NewInt(3), admins, enableds),
   224  			other:    NewContractDeployerAllowListConfig(big.NewInt(3), admins, []common.Address{{3}}),
   225  			expected: false,
   226  		},
   227  		{
   228  			name:     "different timestamp",
   229  			config:   NewContractDeployerAllowListConfig(big.NewInt(3), admins, enableds),
   230  			other:    NewContractDeployerAllowListConfig(big.NewInt(4), admins, enableds),
   231  			expected: false,
   232  		},
   233  		{
   234  			name:     "same config",
   235  			config:   NewContractDeployerAllowListConfig(big.NewInt(3), admins, enableds),
   236  			other:    NewContractDeployerAllowListConfig(big.NewInt(3), admins, enableds),
   237  			expected: true,
   238  		},
   239  	}
   240  	for _, tt := range tests {
   241  		t.Run(tt.name, func(t *testing.T) {
   242  			require := require.New(t)
   243  
   244  			require.Equal(tt.expected, tt.config.Equal(tt.other))
   245  		})
   246  	}
   247  }
   248  
   249  func TestEqualContractNativeMinterConfig(t *testing.T) {
   250  	admins := []common.Address{{1}}
   251  	enableds := []common.Address{{2}}
   252  	tests := []struct {
   253  		name     string
   254  		config   StatefulPrecompileConfig
   255  		other    StatefulPrecompileConfig
   256  		expected bool
   257  	}{
   258  		{
   259  			name:     "non-nil config and nil other",
   260  			config:   NewContractNativeMinterConfig(big.NewInt(3), admins, enableds, nil),
   261  			other:    nil,
   262  			expected: false,
   263  		},
   264  		{
   265  			name:     "different type",
   266  			config:   NewContractNativeMinterConfig(big.NewInt(3), admins, enableds, nil),
   267  			other:    NewTxAllowListConfig(big.NewInt(3), []common.Address{{1}}, []common.Address{{2}}),
   268  			expected: false,
   269  		},
   270  		{
   271  			name:     "different timestamps",
   272  			config:   NewContractNativeMinterConfig(big.NewInt(3), admins, nil, nil),
   273  			other:    NewContractNativeMinterConfig(big.NewInt(4), admins, nil, nil),
   274  			expected: false,
   275  		},
   276  		{
   277  			name:     "different enabled",
   278  			config:   NewContractNativeMinterConfig(big.NewInt(3), admins, nil, nil),
   279  			other:    NewContractNativeMinterConfig(big.NewInt(3), admins, enableds, nil),
   280  			expected: false,
   281  		},
   282  		{
   283  			name: "different initial mint amounts",
   284  			config: NewContractNativeMinterConfig(big.NewInt(3), admins, nil,
   285  				map[common.Address]*math.HexOrDecimal256{
   286  					common.HexToAddress("0x01"): math.NewHexOrDecimal256(1),
   287  				}),
   288  			other: NewContractNativeMinterConfig(big.NewInt(3), admins, nil,
   289  				map[common.Address]*math.HexOrDecimal256{
   290  					common.HexToAddress("0x01"): math.NewHexOrDecimal256(2),
   291  				}),
   292  			expected: false,
   293  		},
   294  		{
   295  			name: "different initial mint addresses",
   296  			config: NewContractNativeMinterConfig(big.NewInt(3), admins, nil,
   297  				map[common.Address]*math.HexOrDecimal256{
   298  					common.HexToAddress("0x01"): math.NewHexOrDecimal256(1),
   299  				}),
   300  			other: NewContractNativeMinterConfig(big.NewInt(3), admins, nil,
   301  				map[common.Address]*math.HexOrDecimal256{
   302  					common.HexToAddress("0x02"): math.NewHexOrDecimal256(1),
   303  				}),
   304  			expected: false,
   305  		},
   306  
   307  		{
   308  			name: "same config",
   309  			config: NewContractNativeMinterConfig(big.NewInt(3), admins, nil,
   310  				map[common.Address]*math.HexOrDecimal256{
   311  					common.HexToAddress("0x01"): math.NewHexOrDecimal256(1),
   312  				}),
   313  			other: NewContractNativeMinterConfig(big.NewInt(3), admins, nil,
   314  				map[common.Address]*math.HexOrDecimal256{
   315  					common.HexToAddress("0x01"): math.NewHexOrDecimal256(1),
   316  				}),
   317  			expected: true,
   318  		},
   319  	}
   320  	for _, tt := range tests {
   321  		t.Run(tt.name, func(t *testing.T) {
   322  			require := require.New(t)
   323  
   324  			require.Equal(tt.expected, tt.config.Equal(tt.other))
   325  		})
   326  	}
   327  }
   328  
   329  func TestEqualFeeConfigManagerConfig(t *testing.T) {
   330  	admins := []common.Address{{1}}
   331  	enableds := []common.Address{{2}}
   332  	tests := []struct {
   333  		name     string
   334  		config   StatefulPrecompileConfig
   335  		other    StatefulPrecompileConfig
   336  		expected bool
   337  	}{
   338  		{
   339  			name:     "non-nil config and nil other",
   340  			config:   NewFeeManagerConfig(big.NewInt(3), admins, enableds, nil),
   341  			other:    nil,
   342  			expected: false,
   343  		},
   344  		{
   345  			name:     "different type",
   346  			config:   NewFeeManagerConfig(big.NewInt(3), admins, enableds, nil),
   347  			other:    NewTxAllowListConfig(big.NewInt(3), []common.Address{{1}}, []common.Address{{2}}),
   348  			expected: false,
   349  		},
   350  		{
   351  			name:     "different timestamp",
   352  			config:   NewFeeManagerConfig(big.NewInt(3), admins, nil, nil),
   353  			other:    NewFeeManagerConfig(big.NewInt(4), admins, nil, nil),
   354  			expected: false,
   355  		},
   356  		{
   357  			name:     "different enabled",
   358  			config:   NewFeeManagerConfig(big.NewInt(3), admins, nil, nil),
   359  			other:    NewFeeManagerConfig(big.NewInt(3), admins, enableds, nil),
   360  			expected: false,
   361  		},
   362  		{
   363  			name:     "non-nil initial config and nil initial config",
   364  			config:   NewFeeManagerConfig(big.NewInt(3), admins, nil, &validFeeConfig),
   365  			other:    NewFeeManagerConfig(big.NewInt(3), admins, nil, nil),
   366  			expected: false,
   367  		},
   368  		{
   369  			name:   "different initial config",
   370  			config: NewFeeManagerConfig(big.NewInt(3), admins, nil, &validFeeConfig),
   371  			other: NewFeeManagerConfig(big.NewInt(3), admins, nil,
   372  				func() *commontype.FeeConfig {
   373  					c := validFeeConfig
   374  					c.GasLimit = big.NewInt(123)
   375  					return &c
   376  				}()),
   377  			expected: false,
   378  		},
   379  		{
   380  			name:     "same config",
   381  			config:   NewFeeManagerConfig(big.NewInt(3), admins, nil, &validFeeConfig),
   382  			other:    NewFeeManagerConfig(big.NewInt(3), admins, nil, &validFeeConfig),
   383  			expected: true,
   384  		},
   385  	}
   386  	for _, tt := range tests {
   387  		t.Run(tt.name, func(t *testing.T) {
   388  			require := require.New(t)
   389  
   390  			require.Equal(tt.expected, tt.config.Equal(tt.other))
   391  		})
   392  	}
   393  }
   394  
   395  func TestEqualRewardManagerConfig(t *testing.T) {
   396  	admins := []common.Address{{1}}
   397  	enableds := []common.Address{{2}}
   398  	tests := []struct {
   399  		name     string
   400  		config   StatefulPrecompileConfig
   401  		other    StatefulPrecompileConfig
   402  		expected bool
   403  	}{
   404  		{
   405  			name:     "non-nil config and nil other",
   406  			config:   NewRewardManagerConfig(big.NewInt(3), admins, enableds, nil),
   407  			other:    nil,
   408  			expected: false,
   409  		},
   410  		{
   411  			name:     "different type",
   412  			config:   NewRewardManagerConfig(big.NewInt(3), admins, enableds, nil),
   413  			other:    NewTxAllowListConfig(big.NewInt(3), []common.Address{{1}}, []common.Address{{2}}),
   414  			expected: false,
   415  		},
   416  		{
   417  			name:     "different timestamp",
   418  			config:   NewRewardManagerConfig(big.NewInt(3), admins, nil, nil),
   419  			other:    NewRewardManagerConfig(big.NewInt(4), admins, nil, nil),
   420  			expected: false,
   421  		},
   422  		{
   423  			name:     "different enabled",
   424  			config:   NewRewardManagerConfig(big.NewInt(3), admins, nil, nil),
   425  			other:    NewRewardManagerConfig(big.NewInt(3), admins, enableds, nil),
   426  			expected: false,
   427  		},
   428  		{
   429  			name: "non-nil initial config and nil initial config",
   430  			config: NewRewardManagerConfig(big.NewInt(3), admins, nil, &InitialRewardConfig{
   431  				AllowFeeRecipients: true,
   432  			}),
   433  			other:    NewRewardManagerConfig(big.NewInt(3), admins, nil, nil),
   434  			expected: false,
   435  		},
   436  		{
   437  			name: "different initial config",
   438  			config: NewRewardManagerConfig(big.NewInt(3), admins, nil, &InitialRewardConfig{
   439  				RewardAddress: common.HexToAddress("0x01"),
   440  			}),
   441  			other: NewRewardManagerConfig(big.NewInt(3), admins, nil,
   442  				&InitialRewardConfig{
   443  					RewardAddress: common.HexToAddress("0x02"),
   444  				}),
   445  			expected: false,
   446  		},
   447  		{
   448  			name: "same config",
   449  			config: NewRewardManagerConfig(big.NewInt(3), admins, nil, &InitialRewardConfig{
   450  				RewardAddress: common.HexToAddress("0x01"),
   451  			}),
   452  			other: NewRewardManagerConfig(big.NewInt(3), admins, nil, &InitialRewardConfig{
   453  				RewardAddress: common.HexToAddress("0x01"),
   454  			}),
   455  			expected: true,
   456  		},
   457  	}
   458  	for _, tt := range tests {
   459  		t.Run(tt.name, func(t *testing.T) {
   460  			require := require.New(t)
   461  
   462  			require.Equal(tt.expected, tt.config.Equal(tt.other))
   463  		})
   464  	}
   465  }