github.com/bencicandrej/quorum@v2.2.6-0.20190909091323-878cab86f711+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 //"github.com/ethereum/go-ethereum/consensus/ethash" 28 "github.com/ethereum/go-ethereum/core/rawdb" 29 //"github.com/ethereum/go-ethereum/core/vm" 30 "github.com/ethereum/go-ethereum/ethdb" 31 "github.com/ethereum/go-ethereum/params" 32 ) 33 34 func TestDefaultGenesisBlock(t *testing.T) { 35 block := DefaultGenesisBlock().ToBlock(nil) 36 if block.Hash() != params.MainnetGenesisHash { 37 t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash) 38 } 39 block = DefaultTestnetGenesisBlock().ToBlock(nil) 40 if block.Hash() != params.TestnetGenesisHash { 41 t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash(), params.TestnetGenesisHash) 42 } 43 } 44 45 func TestSetupGenesis(t *testing.T) { 46 var ( 47 // customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50") 48 customg = Genesis{ 49 Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3), IsQuorum: true}, 50 Alloc: GenesisAlloc{ 51 {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, 52 }, 53 } 54 oldcustomg = customg 55 ) 56 oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2)} 57 tests := []struct { 58 name string 59 fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error) 60 wantConfig *params.ChainConfig 61 wantHash common.Hash 62 wantErr error 63 }{ 64 { 65 name: "genesis without ChainConfig", 66 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 67 return SetupGenesisBlock(db, new(Genesis)) 68 }, 69 wantErr: errGenesisNoConfig, 70 wantConfig: params.AllEthashProtocolChanges, 71 }, 72 { 73 name: "no block in DB, genesis == nil", 74 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 75 return SetupGenesisBlock(db, nil) 76 }, 77 wantHash: params.MainnetGenesisHash, 78 wantConfig: params.MainnetChainConfig, 79 }, 80 { 81 name: "mainnet block in DB, genesis == nil", 82 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 83 DefaultGenesisBlock().MustCommit(db) 84 return SetupGenesisBlock(db, nil) 85 }, 86 wantHash: params.MainnetGenesisHash, 87 wantConfig: params.MainnetChainConfig, 88 }, 89 { 90 name: "genesis with incorrect SizeLimit", 91 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 92 customg.Config.TransactionSizeLimit = 100000 93 customg.Config.MaxCodeSize = 32 94 return SetupGenesisBlock(db, &customg) 95 }, 96 wantErr: errors.New("Genesis transaction size limit must be between 32 and 128"), 97 wantConfig: customg.Config, 98 }, 99 { 100 name: "genesis with incorrect max code size ", 101 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 102 customg.Config.TransactionSizeLimit = 64 103 customg.Config.MaxCodeSize = 100000 104 return SetupGenesisBlock(db, &customg) 105 }, 106 wantErr: errors.New("Genesis max code size must be between 24 and 128"), 107 wantConfig: customg.Config, 108 }, 109 110 // { 111 // name: "custom block in DB, genesis == nil", 112 // fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 113 // customg.MustCommit(db) 114 // return SetupGenesisBlock(db, nil) 115 // }, 116 // wantHash: customghash, 117 // wantConfig: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3), IsQuorum: true}, 118 // // }, 119 // { 120 // name: "custom block in DB, genesis == testnet", 121 // fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 122 // customg.MustCommit(db) 123 // return SetupGenesisBlock(db, DefaultTestnetGenesisBlock()) 124 // }, 125 // wantErr: &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash}, 126 // wantHash: params.TestnetGenesisHash, 127 // wantConfig: params.TestnetChainConfig, 128 // }, 129 // { 130 // name: "compatible config in DB", 131 // fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 132 // oldcustomg.MustCommit(db) 133 // return SetupGenesisBlock(db, &customg) 134 // }, 135 // wantHash: customghash, 136 // wantConfig: customg.Config, 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 for _, test := range tests { 164 db := ethdb.NewMemDatabase() 165 config, hash, err := test.fn(db) 166 // Check the return values. 167 if !reflect.DeepEqual(err, test.wantErr) { 168 spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true} 169 t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr)) 170 } 171 if !reflect.DeepEqual(config, test.wantConfig) { 172 t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig) 173 } 174 if hash != test.wantHash { 175 t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex()) 176 } else if err == nil { 177 // Check database content. 178 stored := rawdb.ReadBlock(db, test.wantHash, 0) 179 if stored.Hash() != test.wantHash { 180 t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash) 181 } 182 } 183 } 184 }