github.com/ethereum/go-ethereum@v1.16.1/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: 1746612311}}, // First Cancun block
    80  				{30000000, 1746022486, ID{Hash: checksumToBytes(0x9f3d2254), Next: 1746612311}}, // Last Cancun block
    81  				{30000000, 1746612311, ID{Hash: checksumToBytes(0xc376cf8b), Next: 0}},          // First Prague block
    82  				{50000000, 2000000000, ID{Hash: checksumToBytes(0xc376cf8b), Next: 0}},          // Future Prague block
    83  			},
    84  		},
    85  		// Sepolia test cases
    86  		{
    87  			params.SepoliaChainConfig,
    88  			core.DefaultSepoliaGenesisBlock().ToBlock(),
    89  			[]testcase{
    90  				{0, 0, ID{Hash: checksumToBytes(0xfe3366e7), Next: 1735371}},                   // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople, Petersburg, Istanbul, Berlin and first London block
    91  				{1735370, 0, ID{Hash: checksumToBytes(0xfe3366e7), Next: 1735371}},             // Last London block
    92  				{1735371, 0, ID{Hash: checksumToBytes(0xb96cbd13), Next: 1677557088}},          // First MergeNetsplit block
    93  				{1735372, 1677557087, ID{Hash: checksumToBytes(0xb96cbd13), Next: 1677557088}}, // Last MergeNetsplit block
    94  				{1735372, 1677557088, ID{Hash: checksumToBytes(0xf7f9bc08), Next: 1706655072}}, // First Shanghai block
    95  				{1735372, 1706655071, ID{Hash: checksumToBytes(0xf7f9bc08), Next: 1706655072}}, // Last Shanghai block
    96  				{1735372, 1706655072, ID{Hash: checksumToBytes(0x88cf81d9), Next: 1741159776}}, // First Cancun block
    97  				{1735372, 1741159775, ID{Hash: checksumToBytes(0x88cf81d9), Next: 1741159776}}, // Last Cancun block
    98  				{1735372, 1741159776, ID{Hash: checksumToBytes(0xed88b5fd), Next: 0}},          // First Prague block
    99  				{1735372, 2741159776, ID{Hash: checksumToBytes(0xed88b5fd), Next: 0}},          // Future Prague block
   100  			},
   101  		},
   102  		// Holesky test cases
   103  		{
   104  			params.HoleskyChainConfig,
   105  			core.DefaultHoleskyGenesisBlock().ToBlock(),
   106  			[]testcase{
   107  				{0, 0, ID{Hash: checksumToBytes(0xc61a6098), Next: 1696000704}},            // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople, Petersburg, Istanbul, Berlin, London, Paris block
   108  				{123, 0, ID{Hash: checksumToBytes(0xc61a6098), Next: 1696000704}},          // First MergeNetsplit block
   109  				{123, 1696000704, ID{Hash: checksumToBytes(0xfd4f016b), Next: 1707305664}}, // First Shanghai block
   110  				{123, 1707305663, ID{Hash: checksumToBytes(0xfd4f016b), Next: 1707305664}}, // Last Shanghai block
   111  				{123, 1707305664, ID{Hash: checksumToBytes(0x9b192ad0), Next: 1740434112}}, // First Cancun block
   112  				{123, 1740434111, ID{Hash: checksumToBytes(0x9b192ad0), Next: 1740434112}}, // Last Cancun block
   113  				{123, 1740434112, ID{Hash: checksumToBytes(0xdfbd9bed), Next: 0}},          // First Prague block
   114  				{123, 2740434112, ID{Hash: checksumToBytes(0xdfbd9bed), Next: 0}},          // Future Prague block
   115  			},
   116  		},
   117  		// Hoodi test cases
   118  		{
   119  			params.HoodiChainConfig,
   120  			core.DefaultHoodiGenesisBlock().ToBlock(),
   121  			[]testcase{
   122  				{0, 0, ID{Hash: checksumToBytes(0xbef71d30), Next: 1742999832}},            // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople, Petersburg, Istanbul, Berlin, London, Paris, Shanghai, Cancun block
   123  				{123, 1742999831, ID{Hash: checksumToBytes(0xbef71d30), Next: 1742999832}}, // Last Cancun block
   124  				{123, 1742999832, ID{Hash: checksumToBytes(0x0929e24e), Next: 0}},          // First Prague block
   125  				{123, 2740434112, ID{Hash: checksumToBytes(0x0929e24e), Next: 0}},          // Future Prague block
   126  			},
   127  		},
   128  	}
   129  	for i, tt := range tests {
   130  		for j, ttt := range tt.cases {
   131  			if have := NewID(tt.config, tt.genesis, ttt.head, ttt.time); have != ttt.want {
   132  				t.Errorf("test %d, case %d: fork ID mismatch: have %x, want %x", i, j, have, ttt.want)
   133  			}
   134  		}
   135  	}
   136  }
   137  
   138  // TestValidation tests that a local peer correctly validates and accepts a remote
   139  // fork ID.
   140  func TestValidation(t *testing.T) {
   141  	// Config that has not timestamp enabled
   142  	// TODO(lightclient): this always needs to be updated when a mainnet timestamp is set.
   143  	legacyConfig := *params.MainnetChainConfig
   144  	legacyConfig.ShanghaiTime = nil
   145  	legacyConfig.CancunTime = nil
   146  	legacyConfig.PragueTime = 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  		{params.MainnetChainConfig, 0, 0, ID{Hash: checksumToBytes(0x3edd5b10), Next: 1710338135}, nil},
   322  
   323  		// Local is mainnet Shanghai, remote announces Cancun. Local is out of sync, accept.
   324  		{params.MainnetChainConfig, 21000000, 1700000000, ID{Hash: checksumToBytes(0x9f3d2254), Next: 0}, nil},
   325  
   326  		// Local is mainnet Shanghai, remote announces Cancun, but is not aware of Prague. Local
   327  		// out of sync. Local also knows about a future fork, but that is uncertain yet.
   328  		//
   329  		{params.MainnetChainConfig, 21000000, 1678000000, ID{Hash: checksumToBytes(0xc376cf8b), Next: 0}, nil},
   330  
   331  		// Local is mainnet Cancun. remote announces Shanghai but is not aware of further forks.
   332  		// Remote needs software update.
   333  		{params.MainnetChainConfig, 21000000, 1710338135, ID{Hash: checksumToBytes(0xdce96c2d), Next: 0}, ErrRemoteStale},
   334  
   335  		// Local is mainnet Shanghai, and isn't aware of more forks. Remote announces Shanghai +
   336  		// 0xffffffff. Local needs software update, reject.
   337  		{params.MainnetChainConfig, 20000000, 1681338455, ID{Hash: checksumToBytes(checksumUpdate(0xdce96c2d, math.MaxUint64)), Next: 0}, ErrLocalIncompatibleOrStale},
   338  
   339  		// Local is mainnet Shanghai, and is aware of Cancun. Remote announces Cancun +
   340  		// 0xffffffff. Local needs software update, reject.
   341  		{params.MainnetChainConfig, 20000000, 1668000000, ID{Hash: checksumToBytes(checksumUpdate(0x9f3d2254, math.MaxUint64)), Next: 0}, ErrLocalIncompatibleOrStale},
   342  
   343  		// Local is mainnet Shanghai, remote is random Shanghai.
   344  		{params.MainnetChainConfig, 20000000, 1681338455, ID{Hash: checksumToBytes(0x12345678), Next: 0}, ErrLocalIncompatibleOrStale},
   345  
   346  		// Local is mainnet Prague, far in the future. Remote announces Gopherium (non existing fork)
   347  		// at some future timestamp 8888888888, for itself, but past block for local. Local is incompatible.
   348  		//
   349  		// This case detects non-upgraded nodes with majority hash power (typical Ropsten mess).
   350  		{params.MainnetChainConfig, 88888888, 8888888888, ID{Hash: checksumToBytes(0xc376cf8b), Next: 8888888888}, ErrLocalIncompatibleOrStale},
   351  
   352  		// Local is mainnet Shanghai. Remote is also in Shanghai, but announces Gopherium (non existing
   353  		// fork) at timestamp 1668000000, before Cancun. Local is incompatible.
   354  		{params.MainnetChainConfig, 20999999, 1699999999, ID{Hash: checksumToBytes(0x71147644), Next: 1700000000}, ErrLocalIncompatibleOrStale},
   355  	}
   356  	genesis := core.DefaultGenesisBlock().ToBlock()
   357  	for i, tt := range tests {
   358  		filter := newFilter(tt.config, genesis, func() (uint64, uint64) { return tt.head, tt.time })
   359  		if err := filter(tt.id); err != tt.err {
   360  			t.Errorf("test %d: validation error mismatch: have %v, want %v", i, err, tt.err)
   361  		}
   362  	}
   363  }
   364  
   365  // Tests that IDs are properly RLP encoded (specifically important because we
   366  // use uint32 to store the hash, but we need to encode it as [4]byte).
   367  func TestEncoding(t *testing.T) {
   368  	tests := []struct {
   369  		id   ID
   370  		want []byte
   371  	}{
   372  		{ID{Hash: checksumToBytes(0), Next: 0}, common.Hex2Bytes("c6840000000080")},
   373  		{ID{Hash: checksumToBytes(0xdeadbeef), Next: 0xBADDCAFE}, common.Hex2Bytes("ca84deadbeef84baddcafe,")},
   374  		{ID{Hash: checksumToBytes(math.MaxUint32), Next: math.MaxUint64}, common.Hex2Bytes("ce84ffffffff88ffffffffffffffff")},
   375  	}
   376  	for i, tt := range tests {
   377  		have, err := rlp.EncodeToBytes(tt.id)
   378  		if err != nil {
   379  			t.Errorf("test %d: failed to encode forkid: %v", i, err)
   380  			continue
   381  		}
   382  		if !bytes.Equal(have, tt.want) {
   383  			t.Errorf("test %d: RLP mismatch: have %x, want %x", i, have, tt.want)
   384  		}
   385  	}
   386  }
   387  
   388  // Tests that time-based forks which are active at genesis are not included in
   389  // forkid hash.
   390  func TestTimeBasedForkInGenesis(t *testing.T) {
   391  	var (
   392  		time       = uint64(1690475657)
   393  		genesis    = types.NewBlockWithHeader(&types.Header{Time: time})
   394  		forkidHash = checksumToBytes(crc32.ChecksumIEEE(genesis.Hash().Bytes()))
   395  		config     = func(shanghai, cancun uint64) *params.ChainConfig {
   396  			return &params.ChainConfig{
   397  				ChainID:                 big.NewInt(1337),
   398  				HomesteadBlock:          big.NewInt(0),
   399  				DAOForkBlock:            nil,
   400  				DAOForkSupport:          true,
   401  				EIP150Block:             big.NewInt(0),
   402  				EIP155Block:             big.NewInt(0),
   403  				EIP158Block:             big.NewInt(0),
   404  				ByzantiumBlock:          big.NewInt(0),
   405  				ConstantinopleBlock:     big.NewInt(0),
   406  				PetersburgBlock:         big.NewInt(0),
   407  				IstanbulBlock:           big.NewInt(0),
   408  				MuirGlacierBlock:        big.NewInt(0),
   409  				BerlinBlock:             big.NewInt(0),
   410  				LondonBlock:             big.NewInt(0),
   411  				TerminalTotalDifficulty: big.NewInt(0),
   412  				MergeNetsplitBlock:      big.NewInt(0),
   413  				ShanghaiTime:            &shanghai,
   414  				CancunTime:              &cancun,
   415  				Ethash:                  new(params.EthashConfig),
   416  			}
   417  		}
   418  	)
   419  	tests := []struct {
   420  		config *params.ChainConfig
   421  		want   ID
   422  	}{
   423  		// Shanghai active before genesis, skip
   424  		{config(time-1, time+1), ID{Hash: forkidHash, Next: time + 1}},
   425  
   426  		// Shanghai active at genesis, skip
   427  		{config(time, time+1), ID{Hash: forkidHash, Next: time + 1}},
   428  
   429  		// Shanghai not active, skip
   430  		{config(time+1, time+2), ID{Hash: forkidHash, Next: time + 1}},
   431  	}
   432  	for _, tt := range tests {
   433  		if have := NewID(tt.config, genesis, 0, time); have != tt.want {
   434  			t.Fatalf("incorrect forkid hash: have %x, want %x", have, tt.want)
   435  		}
   436  	}
   437  }