github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/core/forkid/forkid_test.go (about)

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