github.com/ccm-chain/ccmchain@v1.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/ccm-chain/ccmchain/common" 25 "github.com/ccm-chain/ccmchain/params" 26 "github.com/ccm-chain/ccmchain/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(0x659dd140), Next: 0}}, // Unsynced 47 {1149999, ID{Hash: checksumToBytes(0x659dd140), Next: 0}}, // Last Frontier block 48 {1150000, ID{Hash: checksumToBytes(0x659dd140), Next: 0}}, // First Homestead block 49 {1919999, ID{Hash: checksumToBytes(0x659dd140), Next: 0}}, // Last Homestead block 50 {4370000, ID{Hash: checksumToBytes(0x659dd140), Next: 0}}, // First Byzantium block 51 {7279999, ID{Hash: checksumToBytes(0x659dd140), Next: 0}}, // Last Byzantium block 52 {7280000, ID{Hash: checksumToBytes(0x659dd140), Next: 0}}, // First and last Constantinople, first Petersburg block 53 {7987396, ID{Hash: checksumToBytes(0x659dd140), Next: 0}}, // Today Petersburg block 54 {1769535, ID{Hash: checksumToBytes(0x659dd140), Next: 0}}, // Today block 55 }, 56 }, 57 // Ropsten test cases 58 { 59 params.TestnetChainConfig, 60 params.TestnetGenesisHash, 61 []testcase{ 62 {0, ID{Hash: checksumToBytes(0x12edcbe4), Next: 0}}, // Unsynced, last Frontier, Homestead and first Tangerine block 63 {9, ID{Hash: checksumToBytes(0x12edcbe4), Next: 0}}, // Last Tangerine block 64 {10, ID{Hash: checksumToBytes(0x12edcbe4), Next: 0}}, // First Spurious block 65 {1699999, ID{Hash: checksumToBytes(0x12edcbe4), Next: 0}}, // Last Spurious block 66 {1700000, ID{Hash: checksumToBytes(0x12edcbe4), Next: 0}}, // First Byzantium block 67 {4229999, ID{Hash: checksumToBytes(0x12edcbe4), Next: 0}}, // Last Byzantium block 68 {4230000, ID{Hash: checksumToBytes(0x12edcbe4), Next: 0}}, // First Constantinople block 69 {4939393, ID{Hash: checksumToBytes(0x12edcbe4), Next: 0}}, // Last Constantinople block 70 {4939394, ID{Hash: checksumToBytes(0x12edcbe4), Next: 0}}, // First Petersburg block 71 {5822692, ID{Hash: checksumToBytes(0x12edcbe4), Next: 0}}, // Today Petersburg block 72 }, 73 }, 74 } 75 for i, tt := range tests { 76 for j, ttt := range tt.cases { 77 if have := newID(tt.config, tt.genesis, ttt.head); have != ttt.want { 78 t.Errorf("test %d, case %d: fork ID mismatch: have %x, want %x", i, j, have, ttt.want) 79 } 80 } 81 } 82 } 83 84 // TestValidation tests that a local peer correctly validates and accepts a remote 85 // fork ID. 86 func TestValidation(t *testing.T) { 87 tests := []struct { 88 head uint64 89 id ID 90 err error 91 }{ 92 // Local is mainnet Petersburg, remote announces the same. No future fork is announced. 93 {7987396, ID{Hash: checksumToBytes(0x659dd140), Next: 0}, nil}, 94 95 // Local is mainnet Petersburg, remote announces the same. Remote also announces a next fork 96 // at block 0xffffffff, but that is uncertain. 97 {7987396, ID{Hash: checksumToBytes(0x659dd140), Next: math.MaxUint64}, nil}, 98 99 // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces 100 // also Byzantium, but it's not yet aware of Petersburg (e.g. non updated node before the fork). 101 // In this case we don't know if Petersburg passed yet or not. 102 {7279999, ID{Hash: checksumToBytes(0x659dd140), Next: 0}, nil}, 103 104 // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces 105 // also Byzantium, and it's also aware of Petersburg (e.g. updated node before the fork). We 106 // don't know if Petersburg passed yet (will pass) or not. 107 // {7279999, ID{Hash: checksumToBytes(0x30c7ddbc), Next: 0}, nil}, 108 109 // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces 110 // also Byzantium, and it's also aware of some random fork (e.g. misconfigured Petersburg). As 111 // neither forks passed at neither nodes, they may mismatch, but we still connect for now. 112 {7279999, ID{Hash: checksumToBytes(0x659dd140), Next: math.MaxUint64}, nil}, 113 114 // Local is mainnet Petersburg, remote announces Byzantium + knowledge about Petersburg. Remote 115 // is simply out of sync, accept. 116 // {7987396, ID{Hash: checksumToBytes(0x30c7ddbc), Next: 0}, nil}, 117 118 // Local is mainnet Petersburg, remote announces Spurious + knowledge about Byzantium. Remote 119 // is definitely out of sync. It may or may not need the Petersburg update, we don't know yet. 120 // {7987396, ID{Hash: checksumToBytes(0x30c7ddbc), Next: 0}, nil}, 121 122 // Local is mainnet Byzantium, remote announces Petersburg. Local is out of sync, accept. 123 // {7279999, ID{Hash: checksumToBytes(0x30c7ddbc), Next: 0}, nil}, 124 125 // Local is mainnet Spurious, remote announces Byzantium, but is not aware of Petersburg. Local 126 // out of sync. Local also knows about a future fork, but that is uncertain yet. 127 {4369999, ID{Hash: checksumToBytes(0x659dd140), Next: 0}, nil}, 128 129 // Local is mainnet Petersburg. remote announces Byzantium but is not aware of further forks. 130 // Remote needs software update. 131 // {7987396, ID{Hash: checksumToBytes(0xfc64ec04), Next: 0}, ErrRemoteStale}, 132 133 // Local is mainnet Petersburg, and isn't aware of more forks. Remote announces Petersburg + 134 // 0xffffffff. Local needs software update, reject. 135 // {7987396, ID{Hash: checksumToBytes(0xfc64ec04), Next: 0}, ErrLocalIncompatibleOrStale}, 136 137 // Local is mainnet Byzantium, and is aware of Petersburg. Remote announces Petersburg + 138 // 0xffffffff. Local needs software update, reject. 139 // {7279999, ID{Hash: checksumToBytes(0xfc64ec04), Next: 0}, ErrLocalIncompatibleOrStale}, 140 141 // Local is mainnet Petersburg, remote is Rinkeby Petersburg. 142 // {7987396, ID{Hash: checksumToBytes(0x30c7ddbc), Next: 0}, ErrLocalIncompatibleOrStale}, 143 } 144 for i, tt := range tests { 145 filter := newFilter(params.MainnetChainConfig, params.MainnetGenesisHash, func() uint64 { return tt.head }) 146 if err := filter(tt.id); err != tt.err { 147 t.Errorf("test %d: validation error mismatch: have %v, want %v", i, err, tt.err) 148 } 149 } 150 } 151 152 // Tests that IDs are properly RLP encoded (specifically important because we 153 // use uint32 to store the hash, but we need to encode it as [4]byte). 154 func TestEncoding(t *testing.T) { 155 tests := []struct { 156 id ID 157 want []byte 158 }{ 159 {ID{Hash: checksumToBytes(0), Next: 0}, common.Hex2Bytes("c6840000000080")}, 160 {ID{Hash: checksumToBytes(0xdeadbeef), Next: 0xBADDCAFE}, common.Hex2Bytes("ca84deadbeef84baddcafe,")}, 161 {ID{Hash: checksumToBytes(math.MaxUint32), Next: math.MaxUint64}, common.Hex2Bytes("ce84ffffffff88ffffffffffffffff")}, 162 } 163 for i, tt := range tests { 164 have, err := rlp.EncodeToBytes(tt.id) 165 if err != nil { 166 t.Errorf("test %d: failed to encode forkid: %v", i, err) 167 continue 168 } 169 if !bytes.Equal(have, tt.want) { 170 t.Errorf("test %d: RLP mismatch: have %x, want %x", i, have, tt.want) 171 } 172 } 173 }