github.com/ubiq/go-ubiq/v6@v6.0.0/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/ubiq/go-ubiq/v6/common" 25 "github.com/ubiq/go-ubiq/v6/params" 26 "github.com/ubiq/go-ubiq/v6/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(0xf3073ee8), Next: 10}}, // Unsynced, last Frontier, Homestead, 47 {10, ID{Hash: checksumToBytes(0x3f0fcc99), Next: 1075090}}, // First Spurious block 48 {1075089, ID{Hash: checksumToBytes(0x3f0fcc99), Next: 1075090}}, // Last Spurious block 49 {1075090, ID{Hash: checksumToBytes(0xa4ecb4b6), Next: 1500000}}, // First Byzantium, Constantinople, Petersbug, (andromeda) 50 {1499999, ID{Hash: checksumToBytes(0xa4ecb4b6), Next: 1500000}}, // Last Byzantium, Constantinople, Petersbug, (andromeda) 51 {1500000, ID{Hash: checksumToBytes(0x65ea97e0), Next: 1791793}}, // First Istanbul (taurus) 52 {1791792, ID{Hash: checksumToBytes(0x65ea97e0), Next: 1791793}}, // Last Istanbul (taurus) 53 {1791793, ID{Hash: checksumToBytes(0x9ec7f55b), Next: 0}}, // First Berlin, London (orion) 54 // {5000000, ID{Hash: checksumToBytes(0x757a1c47), Next: 0}}, // Future Berlin block 55 }, 56 }, 57 // Rinkeby test cases 58 { 59 params.RinkebyChainConfig, 60 params.RinkebyGenesisHash, 61 []testcase{ 62 {0, ID{Hash: checksumToBytes(0x3b8e0691), Next: 1}}, // Unsynced, last Frontier block 63 {1, ID{Hash: checksumToBytes(0x60949295), Next: 2}}, // First and last Homestead block 64 {2, ID{Hash: checksumToBytes(0x8bde40dd), Next: 3}}, // First and last Tangerine block 65 {3, ID{Hash: checksumToBytes(0xcb3a64bb), Next: 1035301}}, // First Spurious block 66 {1035300, ID{Hash: checksumToBytes(0xcb3a64bb), Next: 1035301}}, // Last Spurious block 67 {1035301, ID{Hash: checksumToBytes(0x8d748b57), Next: 3660663}}, // First Byzantium block 68 {3660662, ID{Hash: checksumToBytes(0x8d748b57), Next: 3660663}}, // Last Byzantium block 69 {3660663, ID{Hash: checksumToBytes(0xe49cab14), Next: 4321234}}, // First Constantinople block 70 {4321233, ID{Hash: checksumToBytes(0xe49cab14), Next: 4321234}}, // Last Constantinople block 71 {4321234, ID{Hash: checksumToBytes(0xafec6b27), Next: 5435345}}, // First Petersburg block 72 {5435344, ID{Hash: checksumToBytes(0xafec6b27), Next: 5435345}}, // Last Petersburg block 73 {5435345, ID{Hash: checksumToBytes(0xcbdb8838), Next: 8290928}}, // First Istanbul block 74 {8290927, ID{Hash: checksumToBytes(0xcbdb8838), Next: 8290928}}, // Last Istanbul block 75 {8290928, ID{Hash: checksumToBytes(0x6910c8bd), Next: 8897988}}, // First Berlin block 76 {8897987, ID{Hash: checksumToBytes(0x6910c8bd), Next: 8897988}}, // Last Berlin block 77 {8897988, ID{Hash: checksumToBytes(0x8E29F2F3), Next: 0}}, // First London block 78 {10000000, ID{Hash: checksumToBytes(0x8E29F2F3), Next: 0}}, // Future London block 79 }, 80 }, 81 // Goerli test cases 82 { 83 params.GoerliChainConfig, 84 params.GoerliGenesisHash, 85 []testcase{ 86 {0, ID{Hash: checksumToBytes(0xa3f5ab08), Next: 1561651}}, // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople and first Petersburg block 87 {1561650, ID{Hash: checksumToBytes(0xa3f5ab08), Next: 1561651}}, // Last Petersburg block 88 {1561651, ID{Hash: checksumToBytes(0xc25efa5c), Next: 4460644}}, // First Istanbul block 89 {4460643, ID{Hash: checksumToBytes(0xc25efa5c), Next: 4460644}}, // Last Istanbul block 90 {4460644, ID{Hash: checksumToBytes(0x757a1c47), Next: 5062605}}, // First Berlin block 91 {5000000, ID{Hash: checksumToBytes(0x757a1c47), Next: 5062605}}, // Last Berlin block 92 {5062605, ID{Hash: checksumToBytes(0xB8C6299D), Next: 0}}, // First London block 93 {6000000, ID{Hash: checksumToBytes(0xB8C6299D), Next: 0}}, // Future London block 94 }, 95 }, 96 } 97 for i, tt := range tests { 98 for j, ttt := range tt.cases { 99 if have := NewID(tt.config, tt.genesis, ttt.head); have != ttt.want { 100 t.Errorf("test %d, case %d: fork ID mismatch: have %x, want %x", i, j, have, ttt.want) 101 } 102 } 103 } 104 } 105 106 // TestValidation tests that a local peer correctly validates and accepts a remote 107 // fork ID. 108 func TestValidation(t *testing.T) { 109 tests := []struct { 110 head uint64 111 id ID 112 err error 113 }{ 114 // Local is mainnet Andromeda, remote announces the same. No future fork is announced. 115 {1075099, ID{Hash: checksumToBytes(0xa4ecb4b6), Next: 0}, nil}, 116 117 // Local is mainnet Andromeda, remote announces the same. Remote also announces a next fork 118 // at block 0xffffffff, but that is uncertain. 119 {1075102, ID{Hash: checksumToBytes(0xa4ecb4b6), Next: math.MaxUint64}, nil}, 120 121 // Local is mainnet currently in Spurious only (so it's aware of Andromeda), remote announces 122 // also Spurious, but it's not yet aware of Andromeda (e.g. non updated node before the fork). 123 // In this case we don't know if Andromeda passed yet or not. 124 {1075000, ID{Hash: checksumToBytes(0x3f0fcc99), Next: 0}, nil}, 125 126 // Local is mainnet currently in Spurious only (so it's aware of Andromeda), remote announces 127 // also Spurious, and it's also aware of Andromeda (e.g. updated node before the fork). We 128 // don't know if Andromeda passed yet (will pass) or not. 129 {1075089, ID{Hash: checksumToBytes(0x3f0fcc99), Next: 1075090}, nil}, 130 131 // Local is mainnet currently in Spurious only (so it's aware of Andromeda), remote announces 132 // also Spurious, and it's also aware of some random fork (e.g. misconfigured Andromeda). As 133 // neither forks passed at neither nodes, they may mismatch, but we still connect for now. 134 {1075089, ID{Hash: checksumToBytes(0x3f0fcc99), Next: math.MaxUint64}, nil}, 135 136 // Local is mainnet Andromeda, remote announces Spurious + knowledge about Andromeda. Remote 137 // is simply out of sync, accept. 138 {1075200, ID{Hash: checksumToBytes(0x3f0fcc99), Next: 1075090}, nil}, 139 140 // Local is mainnet Taurus, remote announces Spurious + knowledge about Andromeda. Remote 141 // is definitely out of sync. It may or may not need the Taurus update, we don't know yet. 142 {1500200, ID{Hash: checksumToBytes(0x3f0fcc99), Next: 1075090}, nil}, 143 144 // Local is mainnet Andromeda, remote announces Taurus. Local is out of sync, accept. 145 {1400000, ID{Hash: checksumToBytes(0x65ea97e0), Next: 0}, nil}, 146 147 // Local is mainnet Spurious, remote announces Andromeda, but is not aware of Taurus. Local 148 // out of sync. Local also knows about a future fork, but that is uncertain yet. 149 {1075089, ID{Hash: checksumToBytes(0xa4ecb4b6), Next: 0}, nil}, 150 151 // Local is mainnet Taurus. remote announces Andromeda but is not aware of further forks. 152 // Remote needs software update. 153 {1500500, ID{Hash: checksumToBytes(0xa4ecb4b6), Next: 0}, ErrRemoteStale}, 154 155 // Local is mainnet Petersburg, and isn't aware of more forks. Remote announces Petersburg + 156 // 0xffffffff. Local needs software update, reject. 157 {7987396, ID{Hash: checksumToBytes(0x5cddc0e1), Next: 0}, ErrLocalIncompatibleOrStale}, 158 159 // Local is mainnet Byzantium, and is aware of Petersburg. Remote announces Petersburg + 160 // 0xffffffff. Local needs software update, reject. 161 {7279999, ID{Hash: checksumToBytes(0x5cddc0e1), Next: 0}, ErrLocalIncompatibleOrStale}, 162 163 // Local is mainnet Petersburg, remote is Rinkeby Petersburg. 164 {7987396, ID{Hash: checksumToBytes(0xafec6b27), Next: 0}, ErrLocalIncompatibleOrStale}, 165 166 // Local is mainnet Arrow Glacier, far in the future. Remote announces Gopherium (non existing fork) 167 // at some future block 88888888, for itself, but past block for local. Local is incompatible. 168 // 169 // This case detects non-upgraded nodes with majority hash power (typical Ropsten mess). 170 {88888888, ID{Hash: checksumToBytes(0x20c327fc), Next: 88888888}, ErrLocalIncompatibleOrStale}, 171 172 // Local is mainnet Byzantium. Remote is also in Byzantium, but announces Gopherium (non existing 173 // fork) at block 7279999, before Petersburg. Local is incompatible. 174 {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7279999}, ErrLocalIncompatibleOrStale}, 175 } 176 for i, tt := range tests { 177 filter := newFilter(params.MainnetChainConfig, params.MainnetGenesisHash, func() uint64 { return tt.head }) 178 if err := filter(tt.id); err != tt.err { 179 t.Errorf("test %d: validation error mismatch: have %v, want %v", i, err, tt.err) 180 } 181 } 182 } 183 184 // Tests that IDs are properly RLP encoded (specifically important because we 185 // use uint32 to store the hash, but we need to encode it as [4]byte). 186 func TestEncoding(t *testing.T) { 187 tests := []struct { 188 id ID 189 want []byte 190 }{ 191 {ID{Hash: checksumToBytes(0), Next: 0}, common.Hex2Bytes("c6840000000080")}, 192 {ID{Hash: checksumToBytes(0xdeadbeef), Next: 0xBADDCAFE}, common.Hex2Bytes("ca84deadbeef84baddcafe,")}, 193 {ID{Hash: checksumToBytes(math.MaxUint32), Next: math.MaxUint64}, common.Hex2Bytes("ce84ffffffff88ffffffffffffffff")}, 194 } 195 for i, tt := range tests { 196 have, err := rlp.EncodeToBytes(tt.id) 197 if err != nil { 198 t.Errorf("test %d: failed to encode forkid: %v", i, err) 199 continue 200 } 201 if !bytes.Equal(have, tt.want) { 202 t.Errorf("test %d: RLP mismatch: have %x, want %x", i, have, tt.want) 203 } 204 } 205 }