github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/core/forkid/forkid_test.go (about) 1 // Copyright 2019 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 forkid 18 19 import ( 20 "bytes" 21 "hash/crc32" 22 "math" 23 "math/big" 24 "testing" 25 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/core" 28 "github.com/ethereum/go-ethereum/core/types" 29 "github.com/ethereum/go-ethereum/params" 30 "github.com/ethereum/go-ethereum/rlp" 31 ) 32 33 // TestCreation tests that different genesis and fork rule combinations result in 34 // the correct fork ID. 35 func TestCreation(t *testing.T) { 36 type testcase struct { 37 head uint64 38 time uint64 39 want ID 40 } 41 tests := []struct { 42 config *params.ChainConfig 43 genesis *types.Block 44 cases []testcase 45 }{ 46 // Mainnet test cases 47 { 48 params.MainnetChainConfig, 49 core.DefaultGenesisBlock().ToBlock(), 50 []testcase{ 51 {0, 0, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Unsynced 52 {1149999, 0, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Last Frontier block 53 {1150000, 0, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // First Homestead block 54 {1919999, 0, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // Last Homestead block 55 {1920000, 0, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // First DAO block 56 {2462999, 0, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // Last DAO block 57 {2463000, 0, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // First Tangerine block 58 {2674999, 0, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // Last Tangerine block 59 {2675000, 0, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // First Spurious block 60 {4369999, 0, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // Last Spurious block 61 {4370000, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // First Byzantium block 62 {7279999, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // Last Byzantium block 63 {7280000, 0, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // First and last Constantinople, first Petersburg block 64 {9068999, 0, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // Last Petersburg block 65 {9069000, 0, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}}, // First Istanbul and first Muir Glacier block 66 {9199999, 0, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}}, // Last Istanbul and first Muir Glacier block 67 {9200000, 0, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // First Muir Glacier block 68 {12243999, 0, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // Last Muir Glacier block 69 {12244000, 0, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // First Berlin block 70 {12964999, 0, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // Last Berlin block 71 {12965000, 0, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // First London block 72 {13772999, 0, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // Last London block 73 {13773000, 0, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // First Arrow Glacier block 74 {15049999, 0, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // Last Arrow Glacier block 75 {15050000, 0, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 1681338455}}, // First Gray Glacier block 76 {20000000, 1681338454, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 1681338455}}, // Last Gray Glacier block 77 {20000000, 1681338455, ID{Hash: checksumToBytes(0xdce96c2d), Next: 1710338135}}, // First Shanghai block 78 {30000000, 1710338134, ID{Hash: checksumToBytes(0xdce96c2d), Next: 1710338135}}, // Last Shanghai block 79 {40000000, 1710338135, ID{Hash: checksumToBytes(0x9f3d2254), Next: 0}}, // First Cancun block 80 {50000000, 2000000000, ID{Hash: checksumToBytes(0x9f3d2254), Next: 0}}, // Future Cancun block 81 }, 82 }, 83 // Goerli test cases 84 { 85 params.GoerliChainConfig, 86 core.DefaultGoerliGenesisBlock().ToBlock(), 87 []testcase{ 88 {0, 0, ID{Hash: checksumToBytes(0xa3f5ab08), Next: 1561651}}, // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople and first Petersburg block 89 {1561650, 0, ID{Hash: checksumToBytes(0xa3f5ab08), Next: 1561651}}, // Last Petersburg block 90 {1561651, 0, ID{Hash: checksumToBytes(0xc25efa5c), Next: 4460644}}, // First Istanbul block 91 {4460643, 0, ID{Hash: checksumToBytes(0xc25efa5c), Next: 4460644}}, // Last Istanbul block 92 {4460644, 0, ID{Hash: checksumToBytes(0x757a1c47), Next: 5062605}}, // First Berlin block 93 {5000000, 0, ID{Hash: checksumToBytes(0x757a1c47), Next: 5062605}}, // Last Berlin block 94 {5062605, 0, ID{Hash: checksumToBytes(0xB8C6299D), Next: 1678832736}}, // First London block 95 {6000000, 1678832735, ID{Hash: checksumToBytes(0xB8C6299D), Next: 1678832736}}, // Last London block 96 {6000001, 1678832736, ID{Hash: checksumToBytes(0xf9843abf), Next: 1705473120}}, // First Shanghai block 97 {6500002, 1705473119, ID{Hash: checksumToBytes(0xf9843abf), Next: 1705473120}}, // Last Shanghai block 98 {6500003, 1705473120, ID{Hash: checksumToBytes(0x70cc14e2), Next: 0}}, // First Cancun block 99 {6500003, 2705473120, ID{Hash: checksumToBytes(0x70cc14e2), Next: 0}}, // Future Cancun block 100 }, 101 }, 102 // Sepolia test cases 103 { 104 params.SepoliaChainConfig, 105 core.DefaultSepoliaGenesisBlock().ToBlock(), 106 []testcase{ 107 {0, 0, ID{Hash: checksumToBytes(0xfe3366e7), Next: 1735371}}, // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople, Petersburg, Istanbul, Berlin and first London block 108 {1735370, 0, ID{Hash: checksumToBytes(0xfe3366e7), Next: 1735371}}, // Last London block 109 {1735371, 0, ID{Hash: checksumToBytes(0xb96cbd13), Next: 1677557088}}, // First MergeNetsplit block 110 {1735372, 1677557087, ID{Hash: checksumToBytes(0xb96cbd13), Next: 1677557088}}, // Last MergeNetsplit block 111 {1735372, 1677557088, ID{Hash: checksumToBytes(0xf7f9bc08), Next: 1706655072}}, // First Shanghai block 112 {1735372, 1706655071, ID{Hash: checksumToBytes(0xf7f9bc08), Next: 1706655072}}, // Last Shanghai block 113 {1735372, 1706655072, ID{Hash: checksumToBytes(0x88cf81d9), Next: 0}}, // First Cancun block 114 {1735372, 2706655072, ID{Hash: checksumToBytes(0x88cf81d9), Next: 0}}, // Future Cancun block 115 }, 116 }, 117 // Holesky test cases 118 { 119 params.HoleskyChainConfig, 120 core.DefaultHoleskyGenesisBlock().ToBlock(), 121 []testcase{ 122 {0, 0, ID{Hash: checksumToBytes(0xc61a6098), Next: 1696000704}}, // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople, Petersburg, Istanbul, Berlin, London, Paris block 123 {123, 0, ID{Hash: checksumToBytes(0xc61a6098), Next: 1696000704}}, // First MergeNetsplit block 124 {123, 1696000704, ID{Hash: checksumToBytes(0xfd4f016b), Next: 1707305664}}, // First Shanghai block 125 {123, 1707305663, ID{Hash: checksumToBytes(0xfd4f016b), Next: 1707305664}}, // Last Shanghai block 126 {123, 1707305664, ID{Hash: checksumToBytes(0x9b192ad0), Next: 0}}, // First Cancun block 127 {123, 2707305664, ID{Hash: checksumToBytes(0x9b192ad0), Next: 0}}, // Future Cancun block 128 }, 129 }, 130 } 131 for i, tt := range tests { 132 for j, ttt := range tt.cases { 133 if have := NewID(tt.config, tt.genesis, ttt.head, ttt.time); have != ttt.want { 134 t.Errorf("test %d, case %d: fork ID mismatch: have %x, want %x", i, j, have, ttt.want) 135 } 136 } 137 } 138 } 139 140 // TestValidation tests that a local peer correctly validates and accepts a remote 141 // fork ID. 142 func TestValidation(t *testing.T) { 143 // Config that has not timestamp enabled 144 legacyConfig := *params.MainnetChainConfig 145 legacyConfig.ShanghaiTime = nil 146 legacyConfig.CancunTime = nil 147 148 tests := []struct { 149 config *params.ChainConfig 150 head uint64 151 time uint64 152 id ID 153 err error 154 }{ 155 //------------------ 156 // Block based tests 157 //------------------ 158 159 // Local is mainnet Gray Glacier, remote announces the same. No future fork is announced. 160 {&legacyConfig, 15050000, 0, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 0}, nil}, 161 162 // Local is mainnet Gray Glacier, remote announces the same. Remote also announces a next fork 163 // at block 0xffffffff, but that is uncertain. 164 {&legacyConfig, 15050000, 0, ID{Hash: checksumToBytes(0xf0afd0e3), Next: math.MaxUint64}, nil}, 165 166 // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces 167 // also Byzantium, but it's not yet aware of Petersburg (e.g. non updated node before the fork). 168 // In this case we don't know if Petersburg passed yet or not. 169 {&legacyConfig, 7279999, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, nil}, 170 171 // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces 172 // also Byzantium, and it's also aware of Petersburg (e.g. updated node before the fork). We 173 // don't know if Petersburg passed yet (will pass) or not. 174 {&legacyConfig, 7279999, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}, nil}, 175 176 // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces 177 // also Byzantium, and it's also aware of some random fork (e.g. misconfigured Petersburg). As 178 // neither forks passed at neither nodes, they may mismatch, but we still connect for now. 179 {&legacyConfig, 7279999, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: math.MaxUint64}, nil}, 180 181 // Local is mainnet exactly on Petersburg, remote announces Byzantium + knowledge about Petersburg. Remote 182 // is simply out of sync, accept. 183 {&legacyConfig, 7280000, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}, nil}, 184 185 // Local is mainnet Petersburg, remote announces Byzantium + knowledge about Petersburg. Remote 186 // is simply out of sync, accept. 187 {&legacyConfig, 7987396, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}, nil}, 188 189 // Local is mainnet Petersburg, remote announces Spurious + knowledge about Byzantium. Remote 190 // is definitely out of sync. It may or may not need the Petersburg update, we don't know yet. 191 {&legacyConfig, 7987396, 0, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}, nil}, 192 193 // Local is mainnet Byzantium, remote announces Petersburg. Local is out of sync, accept. 194 {&legacyConfig, 7279999, 0, ID{Hash: checksumToBytes(0x668db0af), Next: 0}, nil}, 195 196 // Local is mainnet Spurious, remote announces Byzantium, but is not aware of Petersburg. Local 197 // out of sync. Local also knows about a future fork, but that is uncertain yet. 198 {&legacyConfig, 4369999, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, nil}, 199 200 // Local is mainnet Petersburg. remote announces Byzantium but is not aware of further forks. 201 // Remote needs software update. 202 {&legacyConfig, 7987396, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, ErrRemoteStale}, 203 204 // Local is mainnet Petersburg, and isn't aware of more forks. Remote announces Petersburg + 205 // 0xffffffff. Local needs software update, reject. 206 {&legacyConfig, 7987396, 0, ID{Hash: checksumToBytes(0x5cddc0e1), Next: 0}, ErrLocalIncompatibleOrStale}, 207 208 // Local is mainnet Byzantium, and is aware of Petersburg. Remote announces Petersburg + 209 // 0xffffffff. Local needs software update, reject. 210 {&legacyConfig, 7279999, 0, ID{Hash: checksumToBytes(0x5cddc0e1), Next: 0}, ErrLocalIncompatibleOrStale}, 211 212 // Local is mainnet Petersburg, remote is Rinkeby Petersburg. 213 {&legacyConfig, 7987396, 0, ID{Hash: checksumToBytes(0xafec6b27), Next: 0}, ErrLocalIncompatibleOrStale}, 214 215 // Local is mainnet Gray Glacier, far in the future. Remote announces Gopherium (non existing fork) 216 // at some future block 88888888, for itself, but past block for local. Local is incompatible. 217 // 218 // This case detects non-upgraded nodes with majority hash power (typical Ropsten mess). 219 {&legacyConfig, 88888888, 0, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 88888888}, ErrLocalIncompatibleOrStale}, 220 221 // Local is mainnet Byzantium. Remote is also in Byzantium, but announces Gopherium (non existing 222 // fork) at block 7279999, before Petersburg. Local is incompatible. 223 {&legacyConfig, 7279999, 0, ID{Hash: checksumToBytes(0xa00bc324), Next: 7279999}, ErrLocalIncompatibleOrStale}, 224 225 //------------------------------------ 226 // Block to timestamp transition tests 227 //------------------------------------ 228 229 // Local is mainnet currently in Gray Glacier only (so it's aware of Shanghai), remote announces 230 // also Gray Glacier, but it's not yet aware of Shanghai (e.g. non updated node before the fork). 231 // In this case we don't know if Shanghai passed yet or not. 232 {params.MainnetChainConfig, 15050000, 0, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 0}, nil}, 233 234 // Local is mainnet currently in Gray Glacier only (so it's aware of Shanghai), remote announces 235 // also Gray Glacier, and it's also aware of Shanghai (e.g. updated node before the fork). We 236 // don't know if Shanghai passed yet (will pass) or not. 237 {params.MainnetChainConfig, 15050000, 0, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 1681338455}, nil}, 238 239 // Local is mainnet currently in Gray Glacier only (so it's aware of Shanghai), remote announces 240 // also Gray Glacier, and it's also aware of some random fork (e.g. misconfigured Shanghai). As 241 // neither forks passed at neither nodes, they may mismatch, but we still connect for now. 242 {params.MainnetChainConfig, 15050000, 0, ID{Hash: checksumToBytes(0xf0afd0e3), Next: math.MaxUint64}, nil}, 243 244 // Local is mainnet exactly on Shanghai, remote announces Gray Glacier + knowledge about Shanghai. Remote 245 // is simply out of sync, accept. 246 {params.MainnetChainConfig, 20000000, 1681338455, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 1681338455}, nil}, 247 248 // Local is mainnet Shanghai, remote announces Gray Glacier + knowledge about Shanghai. Remote 249 // is simply out of sync, accept. 250 {params.MainnetChainConfig, 20123456, 1681338456, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 1681338455}, nil}, 251 252 // Local is mainnet Shanghai, remote announces Arrow Glacier + knowledge about Gray Glacier. Remote 253 // is definitely out of sync. It may or may not need the Shanghai update, we don't know yet. 254 {params.MainnetChainConfig, 20000000, 1681338455, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}, nil}, 255 256 // Local is mainnet Gray Glacier, remote announces Shanghai. Local is out of sync, accept. 257 {params.MainnetChainConfig, 15050000, 0, ID{Hash: checksumToBytes(0xdce96c2d), Next: 0}, nil}, 258 259 // Local is mainnet Arrow Glacier, remote announces Gray Glacier, but is not aware of Shanghai. Local 260 // out of sync. Local also knows about a future fork, but that is uncertain yet. 261 {params.MainnetChainConfig, 13773000, 0, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 0}, nil}, 262 263 // Local is mainnet Shanghai. remote announces Gray Glacier but is not aware of further forks. 264 // Remote needs software update. 265 {params.MainnetChainConfig, 20000000, 1681338455, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 0}, ErrRemoteStale}, 266 267 // Local is mainnet Gray Glacier, and isn't aware of more forks. Remote announces Gray Glacier + 268 // 0xffffffff. Local needs software update, reject. 269 {params.MainnetChainConfig, 15050000, 0, ID{Hash: checksumToBytes(checksumUpdate(0xf0afd0e3, math.MaxUint64)), Next: 0}, ErrLocalIncompatibleOrStale}, 270 271 // Local is mainnet Gray Glacier, and is aware of Shanghai. Remote announces Shanghai + 272 // 0xffffffff. Local needs software update, reject. 273 {params.MainnetChainConfig, 15050000, 0, ID{Hash: checksumToBytes(checksumUpdate(0xdce96c2d, math.MaxUint64)), Next: 0}, ErrLocalIncompatibleOrStale}, 274 275 // Local is mainnet Gray Glacier, far in the future. Remote announces Gopherium (non existing fork) 276 // at some future timestamp 8888888888, for itself, but past block for local. Local is incompatible. 277 // 278 // This case detects non-upgraded nodes with majority hash power (typical Ropsten mess). 279 {params.MainnetChainConfig, 888888888, 1660000000, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 1660000000}, ErrLocalIncompatibleOrStale}, 280 281 // Local is mainnet Gray Glacier. Remote is also in Gray Glacier, but announces Gopherium (non existing 282 // fork) at block 7279999, before Shanghai. Local is incompatible. 283 {params.MainnetChainConfig, 19999999, 1667999999, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 1667999999}, ErrLocalIncompatibleOrStale}, 284 285 //---------------------- 286 // Timestamp based tests 287 //---------------------- 288 289 // Local is mainnet Shanghai, remote announces the same. No future fork is announced. 290 {params.MainnetChainConfig, 20000000, 1681338455, ID{Hash: checksumToBytes(0xdce96c2d), Next: 0}, nil}, 291 292 // Local is mainnet Shanghai, remote announces the same. Remote also announces a next fork 293 // at time 0xffffffff, but that is uncertain. 294 {params.MainnetChainConfig, 20000000, 1681338455, ID{Hash: checksumToBytes(0xdce96c2d), Next: math.MaxUint64}, nil}, 295 296 // Local is mainnet currently in Shanghai only (so it's aware of Cancun), remote announces 297 // also Shanghai, but it's not yet aware of Cancun (e.g. non updated node before the fork). 298 // In this case we don't know if Cancun passed yet or not. 299 {params.MainnetChainConfig, 20000000, 1668000000, ID{Hash: checksumToBytes(0xdce96c2d), Next: 0}, nil}, 300 301 // Local is mainnet currently in Shanghai only (so it's aware of Cancun), remote announces 302 // also Shanghai, and it's also aware of Cancun (e.g. updated node before the fork). We 303 // don't know if Cancun passed yet (will pass) or not. 304 {params.MainnetChainConfig, 20000000, 1668000000, ID{Hash: checksumToBytes(0xdce96c2d), Next: 1710338135}, nil}, 305 306 // Local is mainnet currently in Shanghai only (so it's aware of Cancun), remote announces 307 // also Shanghai, and it's also aware of some random fork (e.g. misconfigured Cancun). As 308 // neither forks passed at neither nodes, they may mismatch, but we still connect for now. 309 {params.MainnetChainConfig, 20000000, 1668000000, ID{Hash: checksumToBytes(0xdce96c2d), Next: math.MaxUint64}, nil}, 310 311 // Local is mainnet exactly on Cancun, remote announces Shanghai + knowledge about Cancun. Remote 312 // is simply out of sync, accept. 313 {params.MainnetChainConfig, 21000000, 1710338135, ID{Hash: checksumToBytes(0xdce96c2d), Next: 1710338135}, nil}, 314 315 // Local is mainnet Cancun, remote announces Shanghai + knowledge about Cancun. Remote 316 // is simply out of sync, accept. 317 {params.MainnetChainConfig, 21123456, 1710338136, ID{Hash: checksumToBytes(0xdce96c2d), Next: 1710338135}, nil}, 318 319 // Local is mainnet Prague, remote announces Shanghai + knowledge about Cancun. Remote 320 // is definitely out of sync. It may or may not need the Prague update, we don't know yet. 321 // 322 // TODO(karalabe): Enable this when Cancun **and** Prague is specced, update all the numbers 323 //{params.MainnetChainConfig, 0, 0, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}, nil}, 324 325 // Local is mainnet Shanghai, remote announces Cancun. Local is out of sync, accept. 326 {params.MainnetChainConfig, 21000000, 1700000000, ID{Hash: checksumToBytes(0x9f3d2254), Next: 0}, nil}, 327 328 // Local is mainnet Shanghai, remote announces Cancun, but is not aware of Prague. Local 329 // out of sync. Local also knows about a future fork, but that is uncertain yet. 330 // 331 // TODO(karalabe): Enable this when Cancun **and** Prague is specced, update remote checksum 332 //{params.MainnetChainConfig, 21000000, 1678000000, ID{Hash: checksumToBytes(0x00000000), Next: 0}, nil}, 333 334 // Local is mainnet Cancun. remote announces Shanghai but is not aware of further forks. 335 // Remote needs software update. 336 {params.MainnetChainConfig, 21000000, 1710338135, ID{Hash: checksumToBytes(0xdce96c2d), Next: 0}, ErrRemoteStale}, 337 338 // Local is mainnet Shanghai, and isn't aware of more forks. Remote announces Shanghai + 339 // 0xffffffff. Local needs software update, reject. 340 {params.MainnetChainConfig, 20000000, 1681338455, ID{Hash: checksumToBytes(checksumUpdate(0xdce96c2d, math.MaxUint64)), Next: 0}, ErrLocalIncompatibleOrStale}, 341 342 // Local is mainnet Shanghai, and is aware of Cancun. Remote announces Cancun + 343 // 0xffffffff. Local needs software update, reject. 344 {params.MainnetChainConfig, 20000000, 1668000000, ID{Hash: checksumToBytes(checksumUpdate(0x9f3d2254, math.MaxUint64)), Next: 0}, ErrLocalIncompatibleOrStale}, 345 346 // Local is mainnet Shanghai, remote is random Shanghai. 347 {params.MainnetChainConfig, 20000000, 1681338455, ID{Hash: checksumToBytes(0x12345678), Next: 0}, ErrLocalIncompatibleOrStale}, 348 349 // Local is mainnet Cancun, far in the future. Remote announces Gopherium (non existing fork) 350 // at some future timestamp 8888888888, for itself, but past block for local. Local is incompatible. 351 // 352 // This case detects non-upgraded nodes with majority hash power (typical Ropsten mess). 353 {params.MainnetChainConfig, 88888888, 8888888888, ID{Hash: checksumToBytes(0x9f3d2254), Next: 8888888888}, ErrLocalIncompatibleOrStale}, 354 355 // Local is mainnet Shanghai. Remote is also in Shanghai, but announces Gopherium (non existing 356 // fork) at timestamp 1668000000, before Cancun. Local is incompatible. 357 {params.MainnetChainConfig, 20999999, 1699999999, ID{Hash: checksumToBytes(0x71147644), Next: 1700000000}, ErrLocalIncompatibleOrStale}, 358 } 359 genesis := core.DefaultGenesisBlock().ToBlock() 360 for i, tt := range tests { 361 filter := newFilter(tt.config, genesis, func() (uint64, uint64) { return tt.head, tt.time }) 362 if err := filter(tt.id); err != tt.err { 363 t.Errorf("test %d: validation error mismatch: have %v, want %v", i, err, tt.err) 364 } 365 } 366 } 367 368 // Tests that IDs are properly RLP encoded (specifically important because we 369 // use uint32 to store the hash, but we need to encode it as [4]byte). 370 func TestEncoding(t *testing.T) { 371 tests := []struct { 372 id ID 373 want []byte 374 }{ 375 {ID{Hash: checksumToBytes(0), Next: 0}, common.Hex2Bytes("c6840000000080")}, 376 {ID{Hash: checksumToBytes(0xdeadbeef), Next: 0xBADDCAFE}, common.Hex2Bytes("ca84deadbeef84baddcafe,")}, 377 {ID{Hash: checksumToBytes(math.MaxUint32), Next: math.MaxUint64}, common.Hex2Bytes("ce84ffffffff88ffffffffffffffff")}, 378 } 379 for i, tt := range tests { 380 have, err := rlp.EncodeToBytes(tt.id) 381 if err != nil { 382 t.Errorf("test %d: failed to encode forkid: %v", i, err) 383 continue 384 } 385 if !bytes.Equal(have, tt.want) { 386 t.Errorf("test %d: RLP mismatch: have %x, want %x", i, have, tt.want) 387 } 388 } 389 } 390 391 // Tests that time-based forks which are active at genesis are not included in 392 // forkid hash. 393 func TestTimeBasedForkInGenesis(t *testing.T) { 394 var ( 395 time = uint64(1690475657) 396 genesis = types.NewBlockWithHeader(&types.Header{Time: time}) 397 forkidHash = checksumToBytes(crc32.ChecksumIEEE(genesis.Hash().Bytes())) 398 config = func(shanghai, cancun uint64) *params.ChainConfig { 399 return ¶ms.ChainConfig{ 400 ChainID: big.NewInt(1337), 401 HomesteadBlock: big.NewInt(0), 402 DAOForkBlock: nil, 403 DAOForkSupport: true, 404 EIP150Block: big.NewInt(0), 405 EIP155Block: big.NewInt(0), 406 EIP158Block: big.NewInt(0), 407 ByzantiumBlock: big.NewInt(0), 408 ConstantinopleBlock: big.NewInt(0), 409 PetersburgBlock: big.NewInt(0), 410 IstanbulBlock: big.NewInt(0), 411 MuirGlacierBlock: big.NewInt(0), 412 BerlinBlock: big.NewInt(0), 413 LondonBlock: big.NewInt(0), 414 TerminalTotalDifficulty: big.NewInt(0), 415 TerminalTotalDifficultyPassed: true, 416 MergeNetsplitBlock: big.NewInt(0), 417 ShanghaiTime: &shanghai, 418 CancunTime: &cancun, 419 Ethash: new(params.EthashConfig), 420 } 421 } 422 ) 423 tests := []struct { 424 config *params.ChainConfig 425 want ID 426 }{ 427 // Shanghai active before genesis, skip 428 {config(time-1, time+1), ID{Hash: forkidHash, Next: time + 1}}, 429 430 // Shanghai active at genesis, skip 431 {config(time, time+1), ID{Hash: forkidHash, Next: time + 1}}, 432 433 // Shanghai not active, skip 434 {config(time+1, time+2), ID{Hash: forkidHash, Next: time + 1}}, 435 } 436 for _, tt := range tests { 437 if have := NewID(tt.config, genesis, 0, time); have != tt.want { 438 t.Fatalf("incorrect forkid hash: have %x, want %x", have, tt.want) 439 } 440 } 441 }