github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/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/ethw3/go-ethereuma/common"
    26  	"github.com/ethw3/go-ethereuma/params"
    27  	"github.com/ethw3/go-ethereuma/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  	mergeConfig := *params.MainnetChainConfig
    34  	mergeConfig.MergeNetsplitBlock = big.NewInt(18000000)
    35  	type testcase struct {
    36  		head uint64
    37  		want ID
    38  	}
    39  	tests := []struct {
    40  		config  *params.ChainConfig
    41  		genesis common.Hash
    42  		cases   []testcase
    43  	}{
    44  		// Mainnet test cases
    45  		{
    46  			params.MainnetChainConfig,
    47  			params.MainnetGenesisHash,
    48  			[]testcase{
    49  				{0, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}},         // Unsynced
    50  				{1149999, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}},   // Last Frontier block
    51  				{1150000, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}},   // First Homestead block
    52  				{1919999, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}},   // Last Homestead block
    53  				{1920000, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}},   // First DAO block
    54  				{2462999, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}},   // Last DAO block
    55  				{2463000, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}},   // First Tangerine block
    56  				{2674999, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}},   // Last Tangerine block
    57  				{2675000, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}},   // First Spurious block
    58  				{4369999, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}},   // Last Spurious block
    59  				{4370000, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}},   // First Byzantium block
    60  				{7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}},   // Last Byzantium block
    61  				{7280000, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}},   // First and last Constantinople, first Petersburg block
    62  				{9068999, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}},   // Last Petersburg block
    63  				{9069000, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}},   // First Istanbul and first Muir Glacier block
    64  				{9199999, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}},   // Last Istanbul and first Muir Glacier block
    65  				{9200000, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}},  // First Muir Glacier block
    66  				{12243999, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // Last Muir Glacier block
    67  				{12244000, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // First Berlin block
    68  				{12964999, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // Last Berlin block
    69  				{12965000, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // First London block
    70  				{13772999, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // Last London block
    71  				{13773000, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // First Arrow Glacier block
    72  				{15049999, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // Last Arrow Glacier block
    73  				{15050000, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 0xf42400}}, // First Gray Glacier block
    74  				{20000000, ID{Hash: checksumToBytes(0x20e53762), Next: 0}},        // Future Gray Glacier block
    75  			},
    76  		},
    77  		// Ropsten test cases
    78  		{
    79  			params.RopstenChainConfig,
    80  			params.RopstenGenesisHash,
    81  			[]testcase{
    82  				{0, ID{Hash: checksumToBytes(0x30c7ddbc), Next: 10}},              // Unsynced, last Frontier, Homestead and first Tangerine block
    83  				{9, ID{Hash: checksumToBytes(0x30c7ddbc), Next: 10}},              // Last Tangerine block
    84  				{10, ID{Hash: checksumToBytes(0x63760190), Next: 1700000}},        // First Spurious block
    85  				{1699999, ID{Hash: checksumToBytes(0x63760190), Next: 1700000}},   // Last Spurious block
    86  				{1700000, ID{Hash: checksumToBytes(0x3ea159c7), Next: 4230000}},   // First Byzantium block
    87  				{4229999, ID{Hash: checksumToBytes(0x3ea159c7), Next: 4230000}},   // Last Byzantium block
    88  				{4230000, ID{Hash: checksumToBytes(0x97b544f3), Next: 4939394}},   // First Constantinople block
    89  				{4939393, ID{Hash: checksumToBytes(0x97b544f3), Next: 4939394}},   // Last Constantinople block
    90  				{4939394, ID{Hash: checksumToBytes(0xd6e2149b), Next: 6485846}},   // First Petersburg block
    91  				{6485845, ID{Hash: checksumToBytes(0xd6e2149b), Next: 6485846}},   // Last Petersburg block
    92  				{6485846, ID{Hash: checksumToBytes(0x4bc66396), Next: 7117117}},   // First Istanbul block
    93  				{7117116, ID{Hash: checksumToBytes(0x4bc66396), Next: 7117117}},   // Last Istanbul block
    94  				{7117117, ID{Hash: checksumToBytes(0x6727ef90), Next: 9812189}},   // First Muir Glacier block
    95  				{9812188, ID{Hash: checksumToBytes(0x6727ef90), Next: 9812189}},   // Last Muir Glacier block
    96  				{9812189, ID{Hash: checksumToBytes(0xa157d377), Next: 10499401}},  // First Berlin block
    97  				{10499400, ID{Hash: checksumToBytes(0xa157d377), Next: 10499401}}, // Last Berlin block
    98  				{10499401, ID{Hash: checksumToBytes(0x7119b6b3), Next: 0}},        // First London block
    99  				{11000000, ID{Hash: checksumToBytes(0x7119b6b3), Next: 0}},        // Future London block
   100  			},
   101  		},
   102  		// Rinkeby test cases
   103  		{
   104  			params.RinkebyChainConfig,
   105  			params.RinkebyGenesisHash,
   106  			[]testcase{
   107  				{0, ID{Hash: checksumToBytes(0x3b8e0691), Next: 1}},             // Unsynced, last Frontier block
   108  				{1, ID{Hash: checksumToBytes(0x60949295), Next: 2}},             // First and last Homestead block
   109  				{2, ID{Hash: checksumToBytes(0x8bde40dd), Next: 3}},             // First and last Tangerine block
   110  				{3, ID{Hash: checksumToBytes(0xcb3a64bb), Next: 1035301}},       // First Spurious block
   111  				{1035300, ID{Hash: checksumToBytes(0xcb3a64bb), Next: 1035301}}, // Last Spurious block
   112  				{1035301, ID{Hash: checksumToBytes(0x8d748b57), Next: 3660663}}, // First Byzantium block
   113  				{3660662, ID{Hash: checksumToBytes(0x8d748b57), Next: 3660663}}, // Last Byzantium block
   114  				{3660663, ID{Hash: checksumToBytes(0xe49cab14), Next: 4321234}}, // First Constantinople block
   115  				{4321233, ID{Hash: checksumToBytes(0xe49cab14), Next: 4321234}}, // Last Constantinople block
   116  				{4321234, ID{Hash: checksumToBytes(0xafec6b27), Next: 5435345}}, // First Petersburg block
   117  				{5435344, ID{Hash: checksumToBytes(0xafec6b27), Next: 5435345}}, // Last Petersburg block
   118  				{5435345, ID{Hash: checksumToBytes(0xcbdb8838), Next: 8290928}}, // First Istanbul block
   119  				{8290927, ID{Hash: checksumToBytes(0xcbdb8838), Next: 8290928}}, // Last Istanbul block
   120  				{8290928, ID{Hash: checksumToBytes(0x6910c8bd), Next: 8897988}}, // First Berlin block
   121  				{8897987, ID{Hash: checksumToBytes(0x6910c8bd), Next: 8897988}}, // Last Berlin block
   122  				{8897988, ID{Hash: checksumToBytes(0x8E29F2F3), Next: 0}},       // First London block
   123  				{10000000, ID{Hash: checksumToBytes(0x8E29F2F3), Next: 0}},      // Future London block
   124  			},
   125  		},
   126  		// Goerli test cases
   127  		{
   128  			params.GoerliChainConfig,
   129  			params.GoerliGenesisHash,
   130  			[]testcase{
   131  				{0, ID{Hash: checksumToBytes(0xa3f5ab08), Next: 1561651}},       // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople and first Petersburg block
   132  				{1561650, ID{Hash: checksumToBytes(0xa3f5ab08), Next: 1561651}}, // Last Petersburg block
   133  				{1561651, ID{Hash: checksumToBytes(0xc25efa5c), Next: 4460644}}, // First Istanbul block
   134  				{4460643, ID{Hash: checksumToBytes(0xc25efa5c), Next: 4460644}}, // Last Istanbul block
   135  				{4460644, ID{Hash: checksumToBytes(0x757a1c47), Next: 5062605}}, // First Berlin block
   136  				{5000000, ID{Hash: checksumToBytes(0x757a1c47), Next: 5062605}}, // Last Berlin block
   137  				{5062605, ID{Hash: checksumToBytes(0xB8C6299D), Next: 0}},       // First London block
   138  				{6000000, ID{Hash: checksumToBytes(0xB8C6299D), Next: 0}},       // Future London block
   139  			},
   140  		},
   141  		// Sepolia test cases
   142  		{
   143  			params.SepoliaChainConfig,
   144  			params.SepoliaGenesisHash,
   145  			[]testcase{
   146  				{0, ID{Hash: checksumToBytes(0xfe3366e7), Next: 1735371}},       // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople, Petersburg, Istanbul, Berlin and first London block
   147  				{1735370, ID{Hash: checksumToBytes(0xfe3366e7), Next: 1735371}}, // Last London block
   148  				{1735371, ID{Hash: checksumToBytes(0xb96cbd13), Next: 0}},       // First MergeNetsplit block
   149  			},
   150  		},
   151  		// Merge test cases
   152  		{
   153  			&mergeConfig,
   154  			params.MainnetGenesisHash,
   155  			[]testcase{
   156  				{0, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}},         // Unsynced
   157  				{1149999, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}},   // Last Frontier block
   158  				{1150000, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}},   // First Homestead block
   159  				{1919999, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}},   // Last Homestead block
   160  				{1920000, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}},   // First DAO block
   161  				{2462999, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}},   // Last DAO block
   162  				{2463000, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}},   // First Tangerine block
   163  				{2674999, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}},   // Last Tangerine block
   164  				{2675000, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}},   // First Spurious block
   165  				{4369999, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}},   // Last Spurious block
   166  				{4370000, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}},   // First Byzantium block
   167  				{7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}},   // Last Byzantium block
   168  				{7280000, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}},   // First and last Constantinople, first Petersburg block
   169  				{9068999, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}},   // Last Petersburg block
   170  				{9069000, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}},   // First Istanbul and first Muir Glacier block
   171  				{9199999, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}},   // Last Istanbul and first Muir Glacier block
   172  				{9200000, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}},  // First Muir Glacier block
   173  				{12243999, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // Last Muir Glacier block
   174  				{12244000, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // First Berlin block
   175  				{12964999, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // Last Berlin block
   176  				{12965000, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // First London block
   177  				{13772999, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // Last London block
   178  				{13773000, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // First Arrow Glacier block
   179  				{15049999, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // Last Arrow Glacier block
   180  				{15050000, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 0xf42400}}, // First Gray Glacier block
   181  				{18000000, ID{Hash: checksumToBytes(0x3118e3fc), Next: 0}},        // First Merge Start block
   182  				{20000000, ID{Hash: checksumToBytes(0x3118e3fc), Next: 0}},        // Future Merge Start block
   183  			},
   184  		},
   185  	}
   186  	for i, tt := range tests {
   187  		for j, ttt := range tt.cases {
   188  			if have := NewID(tt.config, tt.genesis, ttt.head); have != ttt.want {
   189  				t.Errorf("test %d, case %d: fork ID mismatch: have %x, want %x", i, j, have, ttt.want)
   190  			}
   191  		}
   192  	}
   193  }
   194  
   195  // TestValidation tests that a local peer correctly validates and accepts a remote
   196  // fork ID.
   197  func TestValidation(t *testing.T) {
   198  	tests := []struct {
   199  		head uint64
   200  		id   ID
   201  		err  error
   202  	}{
   203  		// Local is mainnet Petersburg, remote announces the same. No future fork is announced.
   204  		{7987396, ID{Hash: checksumToBytes(0x668db0af), Next: 0}, nil},
   205  
   206  		// Local is mainnet Petersburg, remote announces the same. Remote also announces a next fork
   207  		// at block 0xffffffff, but that is uncertain.
   208  		{7987396, ID{Hash: checksumToBytes(0x668db0af), Next: math.MaxUint64}, nil},
   209  
   210  		// Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
   211  		// also Byzantium, but it's not yet aware of Petersburg (e.g. non updated node before the fork).
   212  		// In this case we don't know if Petersburg passed yet or not.
   213  		{7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, nil},
   214  
   215  		// Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
   216  		// also Byzantium, and it's also aware of Petersburg (e.g. updated node before the fork). We
   217  		// don't know if Petersburg passed yet (will pass) or not.
   218  		{7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}, nil},
   219  
   220  		// Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
   221  		// also Byzantium, and it's also aware of some random fork (e.g. misconfigured Petersburg). As
   222  		// neither forks passed at neither nodes, they may mismatch, but we still connect for now.
   223  		{7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: math.MaxUint64}, nil},
   224  
   225  		// Local is mainnet exactly on Petersburg, remote announces Byzantium + knowledge about Petersburg. Remote
   226  		// is simply out of sync, accept.
   227  		{7280000, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}, nil},
   228  
   229  		// Local is mainnet Petersburg, remote announces Byzantium + knowledge about Petersburg. Remote
   230  		// is simply out of sync, accept.
   231  		{7987396, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}, nil},
   232  
   233  		// Local is mainnet Petersburg, remote announces Spurious + knowledge about Byzantium. Remote
   234  		// is definitely out of sync. It may or may not need the Petersburg update, we don't know yet.
   235  		{7987396, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}, nil},
   236  
   237  		// Local is mainnet Byzantium, remote announces Petersburg. Local is out of sync, accept.
   238  		{7279999, ID{Hash: checksumToBytes(0x668db0af), Next: 0}, nil},
   239  
   240  		// Local is mainnet Spurious, remote announces Byzantium, but is not aware of Petersburg. Local
   241  		// out of sync. Local also knows about a future fork, but that is uncertain yet.
   242  		{4369999, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, nil},
   243  
   244  		// Local is mainnet Petersburg. remote announces Byzantium but is not aware of further forks.
   245  		// Remote needs software update.
   246  		{7987396, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, ErrRemoteStale},
   247  
   248  		// Local is mainnet Petersburg, and isn't aware of more forks. Remote announces Petersburg +
   249  		// 0xffffffff. Local needs software update, reject.
   250  		{7987396, ID{Hash: checksumToBytes(0x5cddc0e1), Next: 0}, ErrLocalIncompatibleOrStale},
   251  
   252  		// Local is mainnet Byzantium, and is aware of Petersburg. Remote announces Petersburg +
   253  		// 0xffffffff. Local needs software update, reject.
   254  		{7279999, ID{Hash: checksumToBytes(0x5cddc0e1), Next: 0}, ErrLocalIncompatibleOrStale},
   255  
   256  		// Local is mainnet Petersburg, remote is Rinkeby Petersburg.
   257  		{7987396, ID{Hash: checksumToBytes(0xafec6b27), Next: 0}, ErrLocalIncompatibleOrStale},
   258  
   259  		// Local is mainnet Gray Glacier, far in the future. Remote announces Gopherium (non existing fork)
   260  		// at some future block 88888888, for itself, but past block for local. Local is incompatible.
   261  		//
   262  		// This case detects non-upgraded nodes with majority hash power (typical Ropsten mess).
   263  		{88888888, ID{Hash: checksumToBytes(0x20e53762), Next: 88888888}, ErrLocalIncompatibleOrStale},
   264  
   265  		// Local is mainnet Byzantium. Remote is also in Byzantium, but announces Gopherium (non existing
   266  		// fork) at block 7279999, before Petersburg. Local is incompatible.
   267  		{7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7279999}, ErrLocalIncompatibleOrStale},
   268  	}
   269  	for i, tt := range tests {
   270  		filter := newFilter(params.MainnetChainConfig, params.MainnetGenesisHash, func() uint64 { return tt.head })
   271  		if err := filter(tt.id); err != tt.err {
   272  			t.Errorf("test %d: validation error mismatch: have %v, want %v", i, err, tt.err)
   273  		}
   274  	}
   275  }
   276  
   277  // Tests that IDs are properly RLP encoded (specifically important because we
   278  // use uint32 to store the hash, but we need to encode it as [4]byte).
   279  func TestEncoding(t *testing.T) {
   280  	tests := []struct {
   281  		id   ID
   282  		want []byte
   283  	}{
   284  		{ID{Hash: checksumToBytes(0), Next: 0}, common.Hex2Bytes("c6840000000080")},
   285  		{ID{Hash: checksumToBytes(0xdeadbeef), Next: 0xBADDCAFE}, common.Hex2Bytes("ca84deadbeef84baddcafe,")},
   286  		{ID{Hash: checksumToBytes(math.MaxUint32), Next: math.MaxUint64}, common.Hex2Bytes("ce84ffffffff88ffffffffffffffff")},
   287  	}
   288  	for i, tt := range tests {
   289  		have, err := rlp.EncodeToBytes(tt.id)
   290  		if err != nil {
   291  			t.Errorf("test %d: failed to encode forkid: %v", i, err)
   292  			continue
   293  		}
   294  		if !bytes.Equal(have, tt.want) {
   295  			t.Errorf("test %d: RLP mismatch: have %x, want %x", i, have, tt.want)
   296  		}
   297  	}
   298  }