github.com/MetalBlockchain/subnet-evm@v0.4.9/params/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 params 5 6 import ( 7 "math/big" 8 "testing" 9 10 "github.com/MetalBlockchain/subnet-evm/commontype" 11 "github.com/MetalBlockchain/subnet-evm/precompile" 12 "github.com/ethereum/go-ethereum/common" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestVerifyWithChainConfig(t *testing.T) { 18 admins := []common.Address{{1}} 19 baseConfig := *SubnetEVMDefaultChainConfig 20 config := &baseConfig 21 config.PrecompileUpgrade = PrecompileUpgrade{ 22 TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(2), nil, nil), 23 } 24 config.PrecompileUpgrades = []PrecompileUpgrade{ 25 { 26 // disable TxAllowList at timestamp 4 27 TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(4)), 28 }, 29 { 30 // re-enable TxAllowList at timestamp 5 31 TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(5), admins, nil), 32 }, 33 } 34 35 // check this config is valid 36 err := config.Verify() 37 assert.NoError(t, err) 38 39 // same precompile cannot be configured twice for the same timestamp 40 badConfig := *config 41 badConfig.PrecompileUpgrades = append( 42 badConfig.PrecompileUpgrades, 43 PrecompileUpgrade{ 44 TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(5)), 45 }, 46 ) 47 err = badConfig.Verify() 48 assert.ErrorContains(t, err, "config timestamp (5) <= previous timestamp (5)") 49 50 // cannot enable a precompile without disabling it first. 51 badConfig = *config 52 badConfig.PrecompileUpgrades = append( 53 badConfig.PrecompileUpgrades, 54 PrecompileUpgrade{ 55 TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(5), admins, nil), 56 }, 57 ) 58 err = badConfig.Verify() 59 assert.ErrorContains(t, err, "disable should be [true]") 60 } 61 62 func TestVerifyWithChainConfigAtNilTimestamp(t *testing.T) { 63 admins := []common.Address{{1}} 64 baseConfig := *SubnetEVMDefaultChainConfig 65 config := &baseConfig 66 config.PrecompileUpgrade = PrecompileUpgrade{ 67 // this does NOT enable the precompile, so it should be upgradeable. 68 TxAllowListConfig: precompile.NewTxAllowListConfig(nil, nil, nil), 69 } 70 require.False(t, config.IsTxAllowList(common.Big0)) // check the precompile is not enabled. 71 config.PrecompileUpgrades = []PrecompileUpgrade{ 72 { 73 // enable TxAllowList at timestamp 5 74 TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(5), admins, nil), 75 }, 76 } 77 78 // check this config is valid 79 require.NoError(t, config.Verify()) 80 } 81 82 func TestVerifyPrecompileUpgrades(t *testing.T) { 83 admins := []common.Address{{1}} 84 tests := []struct { 85 name string 86 upgrades []PrecompileUpgrade 87 expectedError string 88 }{ 89 { 90 name: "enable and disable tx allow list", 91 upgrades: []PrecompileUpgrade{ 92 { 93 TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(1), admins, nil), 94 }, 95 { 96 TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(2)), 97 }, 98 }, 99 expectedError: "", 100 }, 101 { 102 name: "invalid allow list config in tx allowlist", 103 upgrades: []PrecompileUpgrade{ 104 { 105 TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(1), admins, nil), 106 }, 107 { 108 TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(2)), 109 }, 110 { 111 TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(3), admins, admins), 112 }, 113 }, 114 expectedError: "cannot set address", 115 }, 116 { 117 name: "invalid initial fee manager config", 118 upgrades: []PrecompileUpgrade{ 119 { 120 FeeManagerConfig: precompile.NewFeeManagerConfig(big.NewInt(3), admins, nil, 121 func() *commontype.FeeConfig { 122 feeConfig := DefaultFeeConfig 123 feeConfig.GasLimit = big.NewInt(-1) 124 return &feeConfig 125 }()), 126 }, 127 }, 128 expectedError: "gasLimit = -1 cannot be less than or equal to 0", 129 }, 130 { 131 name: "invalid initial fee manager config gas limit 0", 132 upgrades: []PrecompileUpgrade{ 133 { 134 FeeManagerConfig: precompile.NewFeeManagerConfig(big.NewInt(3), admins, nil, 135 func() *commontype.FeeConfig { 136 feeConfig := DefaultFeeConfig 137 feeConfig.GasLimit = common.Big0 138 return &feeConfig 139 }()), 140 }, 141 }, 142 expectedError: "gasLimit = 0 cannot be less than or equal to 0", 143 }, 144 } 145 for _, tt := range tests { 146 t.Run(tt.name, func(t *testing.T) { 147 require := require.New(t) 148 baseConfig := *SubnetEVMDefaultChainConfig 149 config := &baseConfig 150 config.PrecompileUpgrades = tt.upgrades 151 152 err := config.Verify() 153 if tt.expectedError == "" { 154 require.NoError(err) 155 } else { 156 require.ErrorContains(err, tt.expectedError) 157 } 158 }) 159 } 160 } 161 162 func TestVerifyPrecompiles(t *testing.T) { 163 admins := []common.Address{{1}} 164 tests := []struct { 165 name string 166 upgrade PrecompileUpgrade 167 expectedError string 168 }{ 169 { 170 name: "invalid allow list config in tx allowlist", 171 upgrade: PrecompileUpgrade{ 172 TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(3), admins, admins), 173 }, 174 expectedError: "cannot set address", 175 }, 176 { 177 name: "invalid initial fee manager config", 178 upgrade: PrecompileUpgrade{ 179 FeeManagerConfig: precompile.NewFeeManagerConfig(big.NewInt(3), admins, nil, 180 func() *commontype.FeeConfig { 181 feeConfig := DefaultFeeConfig 182 feeConfig.GasLimit = big.NewInt(-1) 183 return &feeConfig 184 }()), 185 }, 186 expectedError: "gasLimit = -1 cannot be less than or equal to 0", 187 }, 188 } 189 for _, tt := range tests { 190 t.Run(tt.name, func(t *testing.T) { 191 require := require.New(t) 192 baseConfig := *SubnetEVMDefaultChainConfig 193 config := &baseConfig 194 config.PrecompileUpgrade = tt.upgrade 195 196 err := config.Verify() 197 if tt.expectedError == "" { 198 require.NoError(err) 199 } else { 200 require.ErrorContains(err, tt.expectedError) 201 } 202 }) 203 } 204 } 205 206 func TestVerifyRequiresSortedTimestamps(t *testing.T) { 207 admins := []common.Address{{1}} 208 baseConfig := *SubnetEVMDefaultChainConfig 209 config := &baseConfig 210 config.PrecompileUpgrades = []PrecompileUpgrade{ 211 { 212 TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(2), admins, nil), 213 }, 214 { 215 ContractDeployerAllowListConfig: precompile.NewContractDeployerAllowListConfig(big.NewInt(1), admins, nil), 216 }, 217 } 218 219 // block timestamps must be monotonically increasing, so this config is invalid 220 err := config.Verify() 221 assert.ErrorContains(t, err, "config timestamp (1) < previous timestamp (2)") 222 } 223 224 func TestGetPrecompileConfig(t *testing.T) { 225 assert := assert.New(t) 226 baseConfig := *SubnetEVMDefaultChainConfig 227 config := &baseConfig 228 config.PrecompileUpgrade = PrecompileUpgrade{ 229 ContractDeployerAllowListConfig: precompile.NewContractDeployerAllowListConfig(big.NewInt(10), nil, nil), 230 } 231 232 deployerConfig := config.GetContractDeployerAllowListConfig(big.NewInt(0)) 233 assert.Nil(deployerConfig) 234 235 deployerConfig = config.GetContractDeployerAllowListConfig(big.NewInt(10)) 236 assert.NotNil(deployerConfig) 237 238 deployerConfig = config.GetContractDeployerAllowListConfig(big.NewInt(11)) 239 assert.NotNil(deployerConfig) 240 241 txAllowListConfig := config.GetTxAllowListConfig(big.NewInt(0)) 242 assert.Nil(txAllowListConfig) 243 }