github.com/ontio/ontology@v1.14.4/vm/evm/params/config_test.go (about)

     1  // Copyright (C) 2021 The Ontology Authors
     2  // Copyright 2017 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package params
    19  
    20  import (
    21  	"math/big"
    22  	"reflect"
    23  	"testing"
    24  )
    25  
    26  func TestCheckCompatible(t *testing.T) {
    27  	type test struct {
    28  		stored, new *ChainConfig
    29  		head        uint64
    30  		wantErr     *ConfigCompatError
    31  	}
    32  	tests := []test{
    33  		{stored: AllEthashProtocolChanges, new: AllEthashProtocolChanges, head: 0, wantErr: nil},
    34  		{stored: AllEthashProtocolChanges, new: AllEthashProtocolChanges, head: 100, wantErr: nil},
    35  		{
    36  			stored:  &ChainConfig{EIP150Block: big.NewInt(10)},
    37  			new:     &ChainConfig{EIP150Block: big.NewInt(20)},
    38  			head:    9,
    39  			wantErr: nil,
    40  		},
    41  		{
    42  			stored: AllEthashProtocolChanges,
    43  			new:    &ChainConfig{HomesteadBlock: nil},
    44  			head:   3,
    45  			wantErr: &ConfigCompatError{
    46  				What:         "Homestead fork block",
    47  				StoredConfig: big.NewInt(0),
    48  				NewConfig:    nil,
    49  				RewindTo:     0,
    50  			},
    51  		},
    52  		{
    53  			stored: AllEthashProtocolChanges,
    54  			new:    &ChainConfig{HomesteadBlock: big.NewInt(1)},
    55  			head:   3,
    56  			wantErr: &ConfigCompatError{
    57  				What:         "Homestead fork block",
    58  				StoredConfig: big.NewInt(0),
    59  				NewConfig:    big.NewInt(1),
    60  				RewindTo:     0,
    61  			},
    62  		},
    63  		{
    64  			stored: &ChainConfig{HomesteadBlock: big.NewInt(30), EIP150Block: big.NewInt(10)},
    65  			new:    &ChainConfig{HomesteadBlock: big.NewInt(25), EIP150Block: big.NewInt(20)},
    66  			head:   25,
    67  			wantErr: &ConfigCompatError{
    68  				What:         "EIP150 fork block",
    69  				StoredConfig: big.NewInt(10),
    70  				NewConfig:    big.NewInt(20),
    71  				RewindTo:     9,
    72  			},
    73  		},
    74  		{
    75  			stored:  &ChainConfig{ConstantinopleBlock: big.NewInt(30)},
    76  			new:     &ChainConfig{ConstantinopleBlock: big.NewInt(30), PetersburgBlock: big.NewInt(30)},
    77  			head:    40,
    78  			wantErr: nil,
    79  		},
    80  		{
    81  			stored: &ChainConfig{ConstantinopleBlock: big.NewInt(30)},
    82  			new:    &ChainConfig{ConstantinopleBlock: big.NewInt(30), PetersburgBlock: big.NewInt(31)},
    83  			head:   40,
    84  			wantErr: &ConfigCompatError{
    85  				What:         "Petersburg fork block",
    86  				StoredConfig: nil,
    87  				NewConfig:    big.NewInt(31),
    88  				RewindTo:     30,
    89  			},
    90  		},
    91  	}
    92  
    93  	for _, test := range tests {
    94  		err := test.stored.CheckCompatible(test.new, test.head)
    95  		if !reflect.DeepEqual(err, test.wantErr) {
    96  			t.Errorf("error mismatch:\nstored: %v\nnew: %v\nhead: %v\nerr: %v\nwant: %v", test.stored, test.new, test.head, err, test.wantErr)
    97  		}
    98  	}
    99  }