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