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