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