github.com/xfond/eth-implementation@v1.8.9-0.20180514135602-f6bc65fc6811/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/rawdb" 28 "github.com/ethereum/go-ethereum/core/vm" 29 "github.com/ethereum/go-ethereum/ethdb" 30 "github.com/ethereum/go-ethereum/params" 31 ) 32 33 func TestDefaultGenesisBlock(t *testing.T) { 34 block := DefaultGenesisBlock().ToBlock(nil) 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(nil) 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.AllEthashProtocolChanges, 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 123 bc, _ := NewBlockChain(db, nil, oldcustomg.Config, ethash.NewFullFaker(), vm.Config{}) 124 defer bc.Stop() 125 126 blocks, _ := GenerateChain(oldcustomg.Config, genesis, ethash.NewFaker(), db, 4, nil) 127 bc.InsertChain(blocks) 128 bc.CurrentBlock() 129 // This should return a compatibility error. 130 return SetupGenesisBlock(db, &customg) 131 }, 132 wantHash: customghash, 133 wantConfig: customg.Config, 134 wantErr: ¶ms.ConfigCompatError{ 135 What: "Homestead fork block", 136 StoredConfig: big.NewInt(2), 137 NewConfig: big.NewInt(3), 138 RewindTo: 1, 139 }, 140 }, 141 } 142 143 for _, test := range tests { 144 db := ethdb.NewMemDatabase() 145 config, hash, err := test.fn(db) 146 // Check the return values. 147 if !reflect.DeepEqual(err, test.wantErr) { 148 spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true} 149 t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr)) 150 } 151 if !reflect.DeepEqual(config, test.wantConfig) { 152 t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig) 153 } 154 if hash != test.wantHash { 155 t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex()) 156 } else if err == nil { 157 // Check database content. 158 stored := rawdb.ReadBlock(db, test.wantHash, 0) 159 if stored.Hash() != test.wantHash { 160 t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash) 161 } 162 } 163 } 164 }