|
| 1 | +// (c) 2025 Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package extras |
| 5 | + |
| 6 | +import ( |
| 7 | + "math/big" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/ava-labs/avalanchego/snow" |
| 12 | + "github.com/ava-labs/avalanchego/upgrade" |
| 13 | + "github.com/ava-labs/libevm/common" |
| 14 | + "github.com/ava-labs/subnet-evm/commontype" |
| 15 | + "github.com/ava-labs/subnet-evm/precompile/contracts/txallowlist" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func pointer[T any](v T) *T { return &v } |
| 21 | + |
| 22 | +func TestChainConfigDescription(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + tests := map[string]struct { |
| 26 | + config *ChainConfig |
| 27 | + wantRegex string |
| 28 | + }{ |
| 29 | + "nil": {}, |
| 30 | + "empty": { |
| 31 | + config: &ChainConfig{}, |
| 32 | + wantRegex: `Avalanche Upgrades \(timestamp based\)\: |
| 33 | + - SubnetEVM Timestamp: ( )+@nil( )+\(https:\/\/github\.com\/ava-labs\/avalanchego\/releases\/tag\/v1\.10\.0\) |
| 34 | +( - .+Timestamp: .+\n)+ |
| 35 | +Upgrade Config: {} |
| 36 | +Fee Config: {} |
| 37 | +Allow Fee Recipients: false |
| 38 | +$`, |
| 39 | + }, |
| 40 | + "set": { |
| 41 | + config: &ChainConfig{ |
| 42 | + NetworkUpgrades: NetworkUpgrades{ |
| 43 | + SubnetEVMTimestamp: pointer(uint64(1)), |
| 44 | + DurangoTimestamp: pointer(uint64(2)), |
| 45 | + EtnaTimestamp: pointer(uint64(3)), |
| 46 | + FortunaTimestamp: pointer(uint64(4)), |
| 47 | + }, |
| 48 | + FeeConfig: commontype.FeeConfig{ |
| 49 | + GasLimit: big.NewInt(5), |
| 50 | + TargetBlockRate: 6, |
| 51 | + MinBaseFee: big.NewInt(7), |
| 52 | + TargetGas: big.NewInt(8), |
| 53 | + BaseFeeChangeDenominator: big.NewInt(9), |
| 54 | + MinBlockGasCost: big.NewInt(10), |
| 55 | + MaxBlockGasCost: big.NewInt(11), |
| 56 | + BlockGasCostStep: big.NewInt(12), |
| 57 | + }, |
| 58 | + AllowFeeRecipients: true, |
| 59 | + UpgradeConfig: UpgradeConfig{ |
| 60 | + NetworkUpgradeOverrides: &NetworkUpgrades{ |
| 61 | + SubnetEVMTimestamp: pointer(uint64(13)), |
| 62 | + }, |
| 63 | + StateUpgrades: []StateUpgrade{ |
| 64 | + { |
| 65 | + BlockTimestamp: pointer(uint64(14)), |
| 66 | + StateUpgradeAccounts: map[common.Address]StateUpgradeAccount{ |
| 67 | + common.Address{15}: { |
| 68 | + Code: []byte{16}, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + wantRegex: `Avalanche Upgrades \(timestamp based\)\: |
| 76 | + - SubnetEVM Timestamp: ( )+@1( )+\(https:\/\/github\.com\/ava-labs\/avalanchego\/releases\/tag\/v1\.10\.0\) |
| 77 | +( - .+Timestamp: .+\n)+ |
| 78 | +Upgrade Config: {"networkUpgradeOverrides":{"subnetEVMTimestamp":13},"stateUpgrades":\[{"blockTimestamp":14,"accounts":{"0x0f00000000000000000000000000000000000000":{"code":"0x10"}}}\]} |
| 79 | +Fee Config: {"gasLimit":5,"targetBlockRate":6,"minBaseFee":7,"targetGas":8,"baseFeeChangeDenominator":9,"minBlockGasCost":10,"maxBlockGasCost":11,"blockGasCostStep":12} |
| 80 | +Allow Fee Recipients: true |
| 81 | +$`, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for name, test := range tests { |
| 86 | + t.Run(name, func(t *testing.T) { |
| 87 | + got := test.config.Description() |
| 88 | + assert.Regexp(t, test.wantRegex, got, "config description mismatch") |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestChainConfigVerify(t *testing.T) { |
| 94 | + t.Parallel() |
| 95 | + |
| 96 | + validFeeConfig := commontype.FeeConfig{ |
| 97 | + GasLimit: big.NewInt(1), |
| 98 | + TargetBlockRate: 1, |
| 99 | + MinBaseFee: big.NewInt(1), |
| 100 | + TargetGas: big.NewInt(1), |
| 101 | + BaseFeeChangeDenominator: big.NewInt(1), |
| 102 | + MinBlockGasCost: big.NewInt(1), |
| 103 | + MaxBlockGasCost: big.NewInt(1), |
| 104 | + BlockGasCostStep: big.NewInt(1), |
| 105 | + } |
| 106 | + |
| 107 | + tests := map[string]struct { |
| 108 | + config ChainConfig |
| 109 | + errRegex string |
| 110 | + }{ |
| 111 | + "invalid_feeconfig": { |
| 112 | + config: ChainConfig{ |
| 113 | + FeeConfig: commontype.FeeConfig{ |
| 114 | + GasLimit: nil, |
| 115 | + }, |
| 116 | + }, |
| 117 | + errRegex: "^invalid fee config: ", |
| 118 | + }, |
| 119 | + "invalid_precompile_upgrades": { |
| 120 | + // Also see precompile_config_test.go TestVerifyWithChainConfig* tests |
| 121 | + config: ChainConfig{ |
| 122 | + FeeConfig: validFeeConfig, |
| 123 | + UpgradeConfig: UpgradeConfig{ |
| 124 | + PrecompileUpgrades: []PrecompileUpgrade{ |
| 125 | + // same precompile cannot be configured twice for the same timestamp |
| 126 | + {Config: txallowlist.NewDisableConfig(pointer(uint64(1)))}, |
| 127 | + {Config: txallowlist.NewDisableConfig(pointer(uint64(1)))}, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + errRegex: "^invalid precompile upgrades: ", |
| 132 | + }, |
| 133 | + "invalid_state_upgrades": { |
| 134 | + config: ChainConfig{ |
| 135 | + FeeConfig: validFeeConfig, |
| 136 | + UpgradeConfig: UpgradeConfig{ |
| 137 | + StateUpgrades: []StateUpgrade{ |
| 138 | + {BlockTimestamp: nil}, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + errRegex: "^invalid state upgrades: ", |
| 143 | + }, |
| 144 | + "invalid_network_upgrades": { |
| 145 | + config: ChainConfig{ |
| 146 | + FeeConfig: validFeeConfig, |
| 147 | + NetworkUpgrades: NetworkUpgrades{ |
| 148 | + SubnetEVMTimestamp: nil, |
| 149 | + }, |
| 150 | + AvalancheContext: AvalancheContext{SnowCtx: &snow.Context{}}, |
| 151 | + }, |
| 152 | + errRegex: "^invalid network upgrades: ", |
| 153 | + }, |
| 154 | + "valid": { |
| 155 | + config: ChainConfig{ |
| 156 | + FeeConfig: validFeeConfig, |
| 157 | + NetworkUpgrades: NetworkUpgrades{ |
| 158 | + SubnetEVMTimestamp: pointer(uint64(1)), |
| 159 | + DurangoTimestamp: pointer(uint64(2)), |
| 160 | + EtnaTimestamp: pointer(uint64(3)), |
| 161 | + FortunaTimestamp: pointer(uint64(4)), |
| 162 | + }, |
| 163 | + AvalancheContext: AvalancheContext{SnowCtx: &snow.Context{ |
| 164 | + NetworkUpgrades: upgrade.Config{ |
| 165 | + DurangoTime: time.Unix(2, 0), |
| 166 | + EtnaTime: time.Unix(3, 0), |
| 167 | + FortunaTime: time.Unix(4, 0), |
| 168 | + }, |
| 169 | + }}, |
| 170 | + }, |
| 171 | + }, |
| 172 | + } |
| 173 | + |
| 174 | + for name, test := range tests { |
| 175 | + t.Run(name, func(t *testing.T) { |
| 176 | + err := test.config.Verify() |
| 177 | + if test.errRegex == "" { |
| 178 | + assert.NoError(t, err) |
| 179 | + } else { |
| 180 | + require.Error(t, err) |
| 181 | + assert.Regexp(t, test.errRegex, err.Error()) |
| 182 | + } |
| 183 | + }) |
| 184 | + } |
| 185 | +} |
0 commit comments