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