github.com/ylsGit/go-ethereum@v1.6.5/core/genesis_test.go (about) 1 // Copyright 2017 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 core 18 19 import ( 20 "math/big" 21 "reflect" 22 "testing" 23 24 "github.com/davecgh/go-spew/spew" 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/consensus/ethash" 27 "github.com/ethereum/go-ethereum/core/vm" 28 "github.com/ethereum/go-ethereum/ethdb" 29 "github.com/ethereum/go-ethereum/event" 30 "github.com/ethereum/go-ethereum/params" 31 ) 32 33 func TestDefaultGenesisBlock(t *testing.T) { 34 block, _ := DefaultGenesisBlock().ToBlock() 35 if block.Hash() != params.MainNetGenesisHash { 36 t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainNetGenesisHash) 37 } 38 block, _ = DefaultTestnetGenesisBlock().ToBlock() 39 if block.Hash() != params.TestNetGenesisHash { 40 t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash(), params.TestNetGenesisHash) 41 } 42 } 43 44 func TestSetupGenesis(t *testing.T) { 45 var ( 46 customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50") 47 customg = Genesis{ 48 Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3)}, 49 Alloc: GenesisAlloc{ 50 {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, 51 }, 52 } 53 oldcustomg = customg 54 ) 55 oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2)} 56 tests := []struct { 57 name string 58 fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error) 59 wantConfig *params.ChainConfig 60 wantHash common.Hash 61 wantErr error 62 }{ 63 { 64 name: "genesis without ChainConfig", 65 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 66 return SetupGenesisBlock(db, new(Genesis)) 67 }, 68 wantErr: errGenesisNoConfig, 69 wantConfig: params.AllProtocolChanges, 70 }, 71 { 72 name: "no block in DB, genesis == nil", 73 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 74 return SetupGenesisBlock(db, nil) 75 }, 76 wantHash: params.MainNetGenesisHash, 77 wantConfig: params.MainnetChainConfig, 78 }, 79 { 80 name: "mainnet block in DB, genesis == nil", 81 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 82 DefaultGenesisBlock().MustCommit(db) 83 return SetupGenesisBlock(db, nil) 84 }, 85 wantHash: params.MainNetGenesisHash, 86 wantConfig: params.MainnetChainConfig, 87 }, 88 { 89 name: "custom block in DB, genesis == nil", 90 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 91 customg.MustCommit(db) 92 return SetupGenesisBlock(db, nil) 93 }, 94 wantHash: customghash, 95 wantConfig: customg.Config, 96 }, 97 { 98 name: "custom block in DB, genesis == testnet", 99 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 100 customg.MustCommit(db) 101 return SetupGenesisBlock(db, DefaultTestnetGenesisBlock()) 102 }, 103 wantErr: &GenesisMismatchError{Stored: customghash, New: params.TestNetGenesisHash}, 104 wantHash: params.TestNetGenesisHash, 105 wantConfig: params.TestnetChainConfig, 106 }, 107 { 108 name: "compatible config in DB", 109 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 110 oldcustomg.MustCommit(db) 111 return SetupGenesisBlock(db, &customg) 112 }, 113 wantHash: customghash, 114 wantConfig: customg.Config, 115 }, 116 { 117 name: "incompatible config in DB", 118 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 119 // Commit the 'old' genesis block with Homestead transition at #2. 120 // Advance to block #4, past the homestead transition block of customg. 121 genesis := oldcustomg.MustCommit(db) 122 bc, _ := NewBlockChain(db, oldcustomg.Config, ethash.NewFullFaker(), new(event.TypeMux), vm.Config{}) 123 bc.SetValidator(bproc{}) 124 bc.InsertChain(makeBlockChainWithDiff(genesis, []int{2, 3, 4, 5}, 0)) 125 bc.CurrentBlock() 126 // This should return a compatibility error. 127 return SetupGenesisBlock(db, &customg) 128 }, 129 wantHash: customghash, 130 wantConfig: customg.Config, 131 wantErr: ¶ms.ConfigCompatError{ 132 What: "Homestead fork block", 133 StoredConfig: big.NewInt(2), 134 NewConfig: big.NewInt(3), 135 RewindTo: 1, 136 }, 137 }, 138 } 139 140 for _, test := range tests { 141 db, _ := ethdb.NewMemDatabase() 142 config, hash, err := test.fn(db) 143 // Check the return values. 144 if !reflect.DeepEqual(err, test.wantErr) { 145 spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true} 146 t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr)) 147 } 148 if !reflect.DeepEqual(config, test.wantConfig) { 149 t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig) 150 } 151 if hash != test.wantHash { 152 t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex()) 153 } else if err == nil { 154 // Check database content. 155 stored := GetBlock(db, test.wantHash, 0) 156 if stored.Hash() != test.wantHash { 157 t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash) 158 } 159 } 160 } 161 }