github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/forkid/forkid_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package forkid 19 20 import ( 21 "bytes" 22 "math" 23 "testing" 24 25 "github.com/AigarNetwork/aigar/common" 26 "github.com/AigarNetwork/aigar/params" 27 "github.com/AigarNetwork/aigar/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 type testcase struct { 34 head uint64 35 want ID 36 } 37 tests := []struct { 38 config *params.ChainConfig 39 genesis common.Hash 40 cases []testcase 41 }{ 42 // Mainnet test cases 43 { 44 params.MainnetChainConfig, 45 params.MainnetGenesisHash, 46 []testcase{ 47 {0, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Unsynced 48 {1149999, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Last Frontier block 49 {1150000, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // First Homestead block 50 {1919999, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // Last Homestead block 51 {1920000, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // First DAO block 52 {2462999, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // Last DAO block 53 {2463000, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // First Tangerine block 54 {2674999, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // Last Tangerine block 55 {2675000, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // First Spurious block 56 {4369999, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // Last Spurious block 57 {4370000, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // First Byzantium block 58 {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // Last Byzantium block 59 {7280000, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // First and last Constantinople, first Petersburg block 60 {9068999, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // Last Petersburg block 61 {9069000, ID{Hash: checksumToBytes(0x879d6e30), Next: 0}}, // Today Istanbul block 62 {10000000, ID{Hash: checksumToBytes(0x879d6e30), Next: 0}}, // Future Istanbul block 63 }, 64 }, 65 // Ropsten test cases 66 { 67 params.TestnetChainConfig, 68 params.TestnetGenesisHash, 69 []testcase{ 70 {0, ID{Hash: checksumToBytes(0x30c7ddbc), Next: 10}}, // Unsynced, last Frontier, Homestead and first Tangerine block 71 {9, ID{Hash: checksumToBytes(0x30c7ddbc), Next: 10}}, // Last Tangerine block 72 {10, ID{Hash: checksumToBytes(0x63760190), Next: 1700000}}, // First Spurious block 73 {1699999, ID{Hash: checksumToBytes(0x63760190), Next: 1700000}}, // Last Spurious block 74 {1700000, ID{Hash: checksumToBytes(0x3ea159c7), Next: 4230000}}, // First Byzantium block 75 {4229999, ID{Hash: checksumToBytes(0x3ea159c7), Next: 4230000}}, // Last Byzantium block 76 {4230000, ID{Hash: checksumToBytes(0x97b544f3), Next: 4939394}}, // First Constantinople block 77 {4939393, ID{Hash: checksumToBytes(0x97b544f3), Next: 4939394}}, // Last Constantinople block 78 {4939394, ID{Hash: checksumToBytes(0xd6e2149b), Next: 6485846}}, // First Petersburg block 79 {6485845, ID{Hash: checksumToBytes(0xd6e2149b), Next: 6485846}}, // Last Petersburg block 80 {6485846, ID{Hash: checksumToBytes(0x4bc66396), Next: 0}}, // First Istanbul block 81 {7500000, ID{Hash: checksumToBytes(0x4bc66396), Next: 0}}, // Future Istanbul block 82 }, 83 }, 84 // Rinkeby test cases 85 { 86 params.RinkebyChainConfig, 87 params.RinkebyGenesisHash, 88 []testcase{ 89 {0, ID{Hash: checksumToBytes(0x3b8e0691), Next: 1}}, // Unsynced, last Frontier block 90 {1, ID{Hash: checksumToBytes(0x60949295), Next: 2}}, // First and last Homestead block 91 {2, ID{Hash: checksumToBytes(0x8bde40dd), Next: 3}}, // First and last Tangerine block 92 {3, ID{Hash: checksumToBytes(0xcb3a64bb), Next: 1035301}}, // First Spurious block 93 {1035300, ID{Hash: checksumToBytes(0xcb3a64bb), Next: 1035301}}, // Last Spurious block 94 {1035301, ID{Hash: checksumToBytes(0x8d748b57), Next: 3660663}}, // First Byzantium block 95 {3660662, ID{Hash: checksumToBytes(0x8d748b57), Next: 3660663}}, // Last Byzantium block 96 {3660663, ID{Hash: checksumToBytes(0xe49cab14), Next: 4321234}}, // First Constantinople block 97 {4321233, ID{Hash: checksumToBytes(0xe49cab14), Next: 4321234}}, // Last Constantinople block 98 {4321234, ID{Hash: checksumToBytes(0xafec6b27), Next: 5435345}}, // First Petersburg block 99 {5435344, ID{Hash: checksumToBytes(0xafec6b27), Next: 5435345}}, // Last Petersburg block 100 {5435345, ID{Hash: checksumToBytes(0xcbdb8838), Next: 0}}, // First Istanbul block 101 {6000000, ID{Hash: checksumToBytes(0xcbdb8838), Next: 0}}, // Future Istanbul block 102 }, 103 }, 104 // Goerli test cases 105 { 106 params.GoerliChainConfig, 107 params.GoerliGenesisHash, 108 []testcase{ 109 {0, ID{Hash: checksumToBytes(0xa3f5ab08), Next: 1561651}}, // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople and first Petersburg block 110 {1561650, ID{Hash: checksumToBytes(0xa3f5ab08), Next: 1561651}}, // Last Petersburg block 111 {1561651, ID{Hash: checksumToBytes(0xc25efa5c), Next: 0}}, // First Istanbul block 112 {2000000, ID{Hash: checksumToBytes(0xc25efa5c), Next: 0}}, // Future Istanbul block 113 }, 114 }, 115 } 116 for i, tt := range tests { 117 for j, ttt := range tt.cases { 118 if have := newID(tt.config, tt.genesis, ttt.head); have != ttt.want { 119 t.Errorf("test %d, case %d: fork ID mismatch: have %x, want %x", i, j, have, ttt.want) 120 } 121 } 122 } 123 } 124 125 // TestValidation tests that a local peer correctly validates and accepts a remote 126 // fork ID. 127 func TestValidation(t *testing.T) { 128 tests := []struct { 129 head uint64 130 id ID 131 err error 132 }{ 133 // Local is mainnet Petersburg, remote announces the same. No future fork is announced. 134 {7987396, ID{Hash: checksumToBytes(0x668db0af), Next: 0}, nil}, 135 136 // Local is mainnet Petersburg, remote announces the same. Remote also announces a next fork 137 // at block 0xffffffff, but that is uncertain. 138 {7987396, ID{Hash: checksumToBytes(0x668db0af), Next: math.MaxUint64}, nil}, 139 140 // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces 141 // also Byzantium, but it's not yet aware of Petersburg (e.g. non updated node before the fork). 142 // In this case we don't know if Petersburg passed yet or not. 143 {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, nil}, 144 145 // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces 146 // also Byzantium, and it's also aware of Petersburg (e.g. updated node before the fork). We 147 // don't know if Petersburg passed yet (will pass) or not. 148 {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}, nil}, 149 150 // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces 151 // also Byzantium, and it's also aware of some random fork (e.g. misconfigured Petersburg). As 152 // neither forks passed at neither nodes, they may mismatch, but we still connect for now. 153 {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: math.MaxUint64}, nil}, 154 155 // Local is mainnet Petersburg, remote announces Byzantium + knowledge about Petersburg. Remote 156 // is simply out of sync, accept. 157 {7987396, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}, nil}, 158 159 // Local is mainnet Petersburg, remote announces Spurious + knowledge about Byzantium. Remote 160 // is definitely out of sync. It may or may not need the Petersburg update, we don't know yet. 161 {7987396, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}, nil}, 162 163 // Local is mainnet Byzantium, remote announces Petersburg. Local is out of sync, accept. 164 {7279999, ID{Hash: checksumToBytes(0x668db0af), Next: 0}, nil}, 165 166 // Local is mainnet Spurious, remote announces Byzantium, but is not aware of Petersburg. Local 167 // out of sync. Local also knows about a future fork, but that is uncertain yet. 168 {4369999, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, nil}, 169 170 // Local is mainnet Petersburg. remote announces Byzantium but is not aware of further forks. 171 // Remote needs software update. 172 {7987396, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, ErrRemoteStale}, 173 174 // Local is mainnet Petersburg, and isn't aware of more forks. Remote announces Petersburg + 175 // 0xffffffff. Local needs software update, reject. 176 {7987396, ID{Hash: checksumToBytes(0x5cddc0e1), Next: 0}, ErrLocalIncompatibleOrStale}, 177 178 // Local is mainnet Byzantium, and is aware of Petersburg. Remote announces Petersburg + 179 // 0xffffffff. Local needs software update, reject. 180 {7279999, ID{Hash: checksumToBytes(0x5cddc0e1), Next: 0}, ErrLocalIncompatibleOrStale}, 181 182 // Local is mainnet Petersburg, remote is Rinkeby Petersburg. 183 {7987396, ID{Hash: checksumToBytes(0xafec6b27), Next: 0}, ErrLocalIncompatibleOrStale}, 184 185 // Local is mainnet Istanbul, far in the future. Remote announces Gopherium (non existing fork) 186 // at some future block 88888888, for itself, but past block for local. Local is incompatible. 187 // 188 // This case detects non-upgraded nodes with majority hash power (typical Ropsten mess). 189 {88888888, ID{Hash: checksumToBytes(0x879d6e30), Next: 88888888}, ErrLocalIncompatibleOrStale}, 190 191 // Local is mainnet Byzantium. Remote is also in Byzantium, but announces Gopherium (non existing 192 // fork) at block 7279999, before Petersburg. Local is incompatible. 193 {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7279999}, ErrLocalIncompatibleOrStale}, 194 } 195 for i, tt := range tests { 196 filter := newFilter(params.MainnetChainConfig, params.MainnetGenesisHash, func() uint64 { return tt.head }) 197 if err := filter(tt.id); err != tt.err { 198 t.Errorf("test %d: validation error mismatch: have %v, want %v", i, err, tt.err) 199 } 200 } 201 } 202 203 // Tests that IDs are properly RLP encoded (specifically important because we 204 // use uint32 to store the hash, but we need to encode it as [4]byte). 205 func TestEncoding(t *testing.T) { 206 tests := []struct { 207 id ID 208 want []byte 209 }{ 210 {ID{Hash: checksumToBytes(0), Next: 0}, common.Hex2Bytes("c6840000000080")}, 211 {ID{Hash: checksumToBytes(0xdeadbeef), Next: 0xBADDCAFE}, common.Hex2Bytes("ca84deadbeef84baddcafe,")}, 212 {ID{Hash: checksumToBytes(math.MaxUint32), Next: math.MaxUint64}, common.Hex2Bytes("ce84ffffffff88ffffffffffffffff")}, 213 } 214 for i, tt := range tests { 215 have, err := rlp.EncodeToBytes(tt.id) 216 if err != nil { 217 t.Errorf("test %d: failed to encode forkid: %v", i, err) 218 continue 219 } 220 if !bytes.Equal(have, tt.want) { 221 t.Errorf("test %d: RLP mismatch: have %x, want %x", i, have, tt.want) 222 } 223 } 224 }