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