github.com/MetalBlockchain/subnet-evm@v0.4.9/params/upgrade_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/precompile"
    11  	"github.com/ethereum/go-ethereum/common"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestVerifyUpgradeConfig(t *testing.T) {
    16  	admins := []common.Address{{1}}
    17  	chainConfig := *TestChainConfig
    18  	chainConfig.TxAllowListConfig = precompile.NewTxAllowListConfig(big.NewInt(1), admins, nil)
    19  
    20  	type test struct {
    21  		upgrades            []PrecompileUpgrade
    22  		expectedErrorString string
    23  	}
    24  
    25  	tests := map[string]test{
    26  		"upgrade bytes conflicts with genesis (re-enable without disable)": {
    27  			expectedErrorString: "disable should be [true]",
    28  			upgrades: []PrecompileUpgrade{
    29  				{
    30  					TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(2), admins, nil),
    31  				},
    32  			},
    33  		},
    34  		"upgrade bytes conflicts with genesis (disable before enable)": {
    35  			expectedErrorString: "config timestamp (0) <= previous timestamp (1)",
    36  			upgrades: []PrecompileUpgrade{
    37  				{
    38  					TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(0)),
    39  				},
    40  			},
    41  		},
    42  		"upgrade bytes conflicts with genesis (disable same time as enable)": {
    43  			expectedErrorString: "config timestamp (1) <= previous timestamp (1)",
    44  			upgrades: []PrecompileUpgrade{
    45  				{
    46  					TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(1)),
    47  				},
    48  			},
    49  		},
    50  	}
    51  
    52  	for name, tt := range tests {
    53  		t.Run(name, func(t *testing.T) {
    54  			// make a local copy of the chainConfig
    55  			chainConfig := chainConfig
    56  
    57  			// verify with the upgrades from the test
    58  			chainConfig.UpgradeConfig.PrecompileUpgrades = tt.upgrades
    59  			err := chainConfig.Verify()
    60  
    61  			if tt.expectedErrorString != "" {
    62  				assert.ErrorContains(t, err, tt.expectedErrorString)
    63  			} else {
    64  				assert.NoError(t, err)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func TestCheckCompatibleUpgradeConfigs(t *testing.T) {
    71  	admins := []common.Address{{1}}
    72  	chainConfig := *TestChainConfig
    73  	chainConfig.TxAllowListConfig = precompile.NewTxAllowListConfig(big.NewInt(1), admins, nil)
    74  	chainConfig.ContractDeployerAllowListConfig = precompile.NewContractDeployerAllowListConfig(big.NewInt(10), admins, nil)
    75  
    76  	type test struct {
    77  		configs             []*UpgradeConfig
    78  		startTimestamps     []*big.Int
    79  		expectedErrorString string
    80  	}
    81  
    82  	tests := map[string]test{
    83  		"disable and re-enable": {
    84  			startTimestamps: []*big.Int{big.NewInt(5)},
    85  			configs: []*UpgradeConfig{
    86  				{
    87  					PrecompileUpgrades: []PrecompileUpgrade{
    88  						{
    89  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
    90  						},
    91  						{
    92  							TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(7), admins, nil),
    93  						},
    94  					},
    95  				},
    96  			},
    97  		},
    98  		"disable and re-enable, reschedule upgrade before it happens": {
    99  			startTimestamps: []*big.Int{big.NewInt(5), big.NewInt(6)},
   100  			configs: []*UpgradeConfig{
   101  				{
   102  					PrecompileUpgrades: []PrecompileUpgrade{
   103  						{
   104  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   105  						},
   106  						{
   107  							TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(7), admins, nil),
   108  						},
   109  					},
   110  				},
   111  				{
   112  					PrecompileUpgrades: []PrecompileUpgrade{
   113  						{
   114  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   115  						},
   116  						{
   117  							TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(8), admins, nil),
   118  						},
   119  					},
   120  				},
   121  			},
   122  		},
   123  		"disable and re-enable, reschedule upgrade after it happens": {
   124  			expectedErrorString: "mismatching PrecompileUpgrade",
   125  			startTimestamps:     []*big.Int{big.NewInt(5), big.NewInt(8)},
   126  			configs: []*UpgradeConfig{
   127  				{
   128  					PrecompileUpgrades: []PrecompileUpgrade{
   129  						{
   130  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   131  						},
   132  						{
   133  							TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(7), admins, nil),
   134  						},
   135  					},
   136  				},
   137  				{
   138  					PrecompileUpgrades: []PrecompileUpgrade{
   139  						{
   140  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   141  						},
   142  						{
   143  							TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(8), admins, nil),
   144  						},
   145  					},
   146  				},
   147  			},
   148  		},
   149  		"disable and re-enable, cancel upgrade before it happens": {
   150  			startTimestamps: []*big.Int{big.NewInt(5), big.NewInt(6)},
   151  			configs: []*UpgradeConfig{
   152  				{
   153  					PrecompileUpgrades: []PrecompileUpgrade{
   154  						{
   155  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   156  						},
   157  						{
   158  							TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(7), admins, nil),
   159  						},
   160  					},
   161  				},
   162  				{
   163  					PrecompileUpgrades: []PrecompileUpgrade{
   164  						{
   165  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   166  						},
   167  					},
   168  				},
   169  			},
   170  		},
   171  		"disable and re-enable, cancel upgrade after it happens": {
   172  			expectedErrorString: "mismatching missing PrecompileUpgrade",
   173  			startTimestamps:     []*big.Int{big.NewInt(5), big.NewInt(8)},
   174  			configs: []*UpgradeConfig{
   175  				{
   176  					PrecompileUpgrades: []PrecompileUpgrade{
   177  						{
   178  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   179  						},
   180  						{
   181  							TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(7), admins, nil),
   182  						},
   183  					},
   184  				},
   185  				{
   186  					PrecompileUpgrades: []PrecompileUpgrade{
   187  						{
   188  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   189  						},
   190  					},
   191  				},
   192  			},
   193  		},
   194  		"disable and re-enable, change upgrade config after upgrade not allowed": {
   195  			expectedErrorString: "mismatching PrecompileUpgrade",
   196  			startTimestamps:     []*big.Int{big.NewInt(5), big.NewInt(8)},
   197  			configs: []*UpgradeConfig{
   198  				{
   199  					PrecompileUpgrades: []PrecompileUpgrade{
   200  						{
   201  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   202  						},
   203  						{
   204  							TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(7), admins, nil),
   205  						},
   206  					},
   207  				},
   208  				{
   209  					PrecompileUpgrades: []PrecompileUpgrade{
   210  						{
   211  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   212  						},
   213  						{
   214  							// uses a different (empty) admin list, not allowed
   215  							TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(7), []common.Address{}, nil),
   216  						},
   217  					},
   218  				},
   219  			},
   220  		},
   221  		"disable and re-enable, identical upgrade config should be accepted": {
   222  			startTimestamps: []*big.Int{big.NewInt(5), big.NewInt(8)},
   223  			configs: []*UpgradeConfig{
   224  				{
   225  					PrecompileUpgrades: []PrecompileUpgrade{
   226  						{
   227  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   228  						},
   229  						{
   230  							TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(7), admins, nil),
   231  						},
   232  					},
   233  				},
   234  				{
   235  					PrecompileUpgrades: []PrecompileUpgrade{
   236  						{
   237  							TxAllowListConfig: precompile.NewDisableTxAllowListConfig(big.NewInt(6)),
   238  						},
   239  						{
   240  							TxAllowListConfig: precompile.NewTxAllowListConfig(big.NewInt(7), admins, nil),
   241  						},
   242  					},
   243  				},
   244  			},
   245  		},
   246  	}
   247  
   248  	for name, tt := range tests {
   249  		t.Run(name, func(t *testing.T) {
   250  			// make a local copy of the chainConfig
   251  			chainConfig := chainConfig
   252  
   253  			// apply all the upgrade bytes specified in order
   254  			for i, upgrade := range tt.configs {
   255  				newCfg := chainConfig
   256  				newCfg.UpgradeConfig = *upgrade
   257  
   258  				err := chainConfig.checkCompatible(&newCfg, nil, tt.startTimestamps[i])
   259  
   260  				// if this is not the final upgradeBytes, continue applying
   261  				// the next upgradeBytes. (only check the result on the last apply)
   262  				if i != len(tt.configs)-1 {
   263  					if err != nil {
   264  						t.Fatalf("expecting ApplyUpgradeBytes call %d to return nil, got %s", i+1, err)
   265  					}
   266  					chainConfig = newCfg
   267  					continue
   268  				}
   269  
   270  				if tt.expectedErrorString != "" {
   271  					assert.ErrorContains(t, err, tt.expectedErrorString)
   272  				} else {
   273  					if err != nil {
   274  						t.Fatal(err)
   275  					}
   276  				}
   277  			}
   278  		})
   279  	}
   280  }