github.com/ethereum/go-ethereum@v1.14.3/params/config_test.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package params 18 19 import ( 20 "math/big" 21 "reflect" 22 "testing" 23 "time" 24 25 "github.com/ethereum/go-ethereum/common/math" 26 "github.com/stretchr/testify/require" 27 ) 28 29 func TestCheckCompatible(t *testing.T) { 30 type test struct { 31 stored, new *ChainConfig 32 headBlock uint64 33 headTimestamp uint64 34 wantErr *ConfigCompatError 35 } 36 tests := []test{ 37 {stored: AllEthashProtocolChanges, new: AllEthashProtocolChanges, headBlock: 0, headTimestamp: 0, wantErr: nil}, 38 {stored: AllEthashProtocolChanges, new: AllEthashProtocolChanges, headBlock: 0, headTimestamp: uint64(time.Now().Unix()), wantErr: nil}, 39 {stored: AllEthashProtocolChanges, new: AllEthashProtocolChanges, headBlock: 100, wantErr: nil}, 40 { 41 stored: &ChainConfig{EIP150Block: big.NewInt(10)}, 42 new: &ChainConfig{EIP150Block: big.NewInt(20)}, 43 headBlock: 9, 44 wantErr: nil, 45 }, 46 { 47 stored: AllEthashProtocolChanges, 48 new: &ChainConfig{HomesteadBlock: nil}, 49 headBlock: 3, 50 wantErr: &ConfigCompatError{ 51 What: "Homestead fork block", 52 StoredBlock: big.NewInt(0), 53 NewBlock: nil, 54 RewindToBlock: 0, 55 }, 56 }, 57 { 58 stored: AllEthashProtocolChanges, 59 new: &ChainConfig{HomesteadBlock: big.NewInt(1)}, 60 headBlock: 3, 61 wantErr: &ConfigCompatError{ 62 What: "Homestead fork block", 63 StoredBlock: big.NewInt(0), 64 NewBlock: big.NewInt(1), 65 RewindToBlock: 0, 66 }, 67 }, 68 { 69 stored: &ChainConfig{HomesteadBlock: big.NewInt(30), EIP150Block: big.NewInt(10)}, 70 new: &ChainConfig{HomesteadBlock: big.NewInt(25), EIP150Block: big.NewInt(20)}, 71 headBlock: 25, 72 wantErr: &ConfigCompatError{ 73 What: "EIP150 fork block", 74 StoredBlock: big.NewInt(10), 75 NewBlock: big.NewInt(20), 76 RewindToBlock: 9, 77 }, 78 }, 79 { 80 stored: &ChainConfig{ConstantinopleBlock: big.NewInt(30)}, 81 new: &ChainConfig{ConstantinopleBlock: big.NewInt(30), PetersburgBlock: big.NewInt(30)}, 82 headBlock: 40, 83 wantErr: nil, 84 }, 85 { 86 stored: &ChainConfig{ConstantinopleBlock: big.NewInt(30)}, 87 new: &ChainConfig{ConstantinopleBlock: big.NewInt(30), PetersburgBlock: big.NewInt(31)}, 88 headBlock: 40, 89 wantErr: &ConfigCompatError{ 90 What: "Petersburg fork block", 91 StoredBlock: nil, 92 NewBlock: big.NewInt(31), 93 RewindToBlock: 30, 94 }, 95 }, 96 { 97 stored: &ChainConfig{ShanghaiTime: newUint64(10)}, 98 new: &ChainConfig{ShanghaiTime: newUint64(20)}, 99 headTimestamp: 9, 100 wantErr: nil, 101 }, 102 { 103 stored: &ChainConfig{ShanghaiTime: newUint64(10)}, 104 new: &ChainConfig{ShanghaiTime: newUint64(20)}, 105 headTimestamp: 25, 106 wantErr: &ConfigCompatError{ 107 What: "Shanghai fork timestamp", 108 StoredTime: newUint64(10), 109 NewTime: newUint64(20), 110 RewindToTime: 9, 111 }, 112 }, 113 } 114 115 for _, test := range tests { 116 err := test.stored.CheckCompatible(test.new, test.headBlock, test.headTimestamp) 117 if !reflect.DeepEqual(err, test.wantErr) { 118 t.Errorf("error mismatch:\nstored: %v\nnew: %v\nheadBlock: %v\nheadTimestamp: %v\nerr: %v\nwant: %v", test.stored, test.new, test.headBlock, test.headTimestamp, err, test.wantErr) 119 } 120 } 121 } 122 123 func TestConfigRules(t *testing.T) { 124 c := &ChainConfig{ 125 LondonBlock: new(big.Int), 126 ShanghaiTime: newUint64(500), 127 } 128 var stamp uint64 129 if r := c.Rules(big.NewInt(0), true, stamp); r.IsShanghai { 130 t.Errorf("expected %v to not be shanghai", stamp) 131 } 132 stamp = 500 133 if r := c.Rules(big.NewInt(0), true, stamp); !r.IsShanghai { 134 t.Errorf("expected %v to be shanghai", stamp) 135 } 136 stamp = math.MaxInt64 137 if r := c.Rules(big.NewInt(0), true, stamp); !r.IsShanghai { 138 t.Errorf("expected %v to be shanghai", stamp) 139 } 140 } 141 142 func TestTimestampCompatError(t *testing.T) { 143 require.Equal(t, new(ConfigCompatError).Error(), "") 144 145 errWhat := "Shanghai fork timestamp" 146 require.Equal(t, newTimestampCompatError(errWhat, nil, newUint64(1681338455)).Error(), 147 "mismatching Shanghai fork timestamp in database (have timestamp nil, want timestamp 1681338455, rewindto timestamp 1681338454)") 148 149 require.Equal(t, newTimestampCompatError(errWhat, newUint64(1681338455), nil).Error(), 150 "mismatching Shanghai fork timestamp in database (have timestamp 1681338455, want timestamp nil, rewindto timestamp 1681338454)") 151 152 require.Equal(t, newTimestampCompatError(errWhat, newUint64(1681338455), newUint64(600624000)).Error(), 153 "mismatching Shanghai fork timestamp in database (have timestamp 1681338455, want timestamp 600624000, rewindto timestamp 600623999)") 154 155 require.Equal(t, newTimestampCompatError(errWhat, newUint64(0), newUint64(1681338455)).Error(), 156 "mismatching Shanghai fork timestamp in database (have timestamp 0, want timestamp 1681338455, rewindto timestamp 0)") 157 }