github.com/codingfuture/orig-energi3@v0.8.4/core/genesis_test.go (about) 1 // Copyright 2018 The Energi Core Authors 2 // Copyright 2017 The go-ethereum Authors 3 // This file is part of the Energi Core library. 4 // 5 // The Energi Core library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The Energi Core library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 17 18 package core 19 20 import ( 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 var ( 35 DefaultMainnetGenesisHash = common.HexToHash("0x493c7b54d31001e4e9f8c37f96186fc96088f71b050c61351b6dbda90bf59bf0") 36 DefaultTestnetGenesisHash = common.HexToHash("0x8f25a6ec9252577d66f4f1905414cf9c43424b95af1bbc5f97823624cfa3dd51") 37 ) 38 39 func TestDefaultGenesisBlock(t *testing.T) { 40 block := DefaultGenesisBlock().ToBlock(nil) 41 if block.Hash() != DefaultMainnetGenesisHash { 42 t.Errorf("wrong default mainnet genesis hash, got %v, want %v", block.Hash().String(), DefaultMainnetGenesisHash.String()) 43 } 44 block = DefaultTestnetGenesisBlock().ToBlock(nil) 45 if block.Hash() != DefaultTestnetGenesisHash { 46 t.Errorf("wrong default testnet genesis hash, got %v, want %v", block.Hash().String(), DefaultTestnetGenesisHash.String()) 47 } 48 } 49 50 func TestEnergiGenesisBlock(t *testing.T) { 51 block := DefaultEnergiMainnetGenesisBlock().ToBlock(nil) 52 if block.Hash() != params.MainnetGenesisHash { 53 t.Errorf("wrong energi mainnet genesis hash, got %v, want %v", block.Hash().String(), params.MainnetGenesisHash.String()) 54 } 55 block = DefaultEnergiTestnetGenesisBlock().ToBlock(nil) 56 if block.Hash() != params.TestnetGenesisHash { 57 t.Errorf("wrong energi testnet genesis hash, got %v, want %v", block.Hash().String(), params.TestnetGenesisHash.String()) 58 } 59 } 60 61 func TestSetupGenesis(t *testing.T) { 62 var ( 63 customghash = common.HexToHash("0x3355576b6a5756a0040828c9b41d4afa58de21b537034253e57cf4759faf295b") 64 customg = Genesis{ 65 Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3)}, 66 Alloc: GenesisAlloc{ 67 {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, 68 }, 69 } 70 oldcustomg = customg 71 ) 72 oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2)} 73 tests := []struct { 74 name string 75 fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error) 76 wantConfig *params.ChainConfig 77 wantHash common.Hash 78 wantErr error 79 }{ 80 { 81 name: "genesis without ChainConfig", 82 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 83 return SetupGenesisBlock(db, new(Genesis)) 84 }, 85 wantErr: errGenesisNoConfig, 86 wantConfig: params.AllEthashProtocolChanges, 87 }, 88 { 89 name: "no block in DB, genesis == nil", 90 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 91 return SetupGenesisBlock(db, nil) 92 }, 93 wantHash: DefaultMainnetGenesisHash, 94 wantConfig: params.MainnetChainConfig, 95 }, 96 { 97 name: "default mainnet block in DB, genesis == nil", 98 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 99 DefaultGenesisBlock().MustCommit(db) 100 return SetupGenesisBlock(db, nil) 101 }, 102 wantHash: DefaultMainnetGenesisHash, 103 wantConfig: params.MainnetChainConfig, 104 }, 105 { 106 name: "energi mainnet block in DB, genesis == nil", 107 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 108 DefaultEnergiMainnetGenesisBlock().MustCommit(db) 109 return SetupGenesisBlock(db, nil) 110 }, 111 wantHash: params.MainnetGenesisHash, 112 wantConfig: DefaultEnergiMainnetGenesisBlock().Config, 113 }, 114 { 115 name: "custom block in DB, genesis == nil", 116 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 117 customg.MustCommit(db) 118 return SetupGenesisBlock(db, nil) 119 }, 120 wantHash: customghash, 121 wantConfig: customg.Config, 122 }, 123 { 124 name: "custom block in DB, genesis == default testnet", 125 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 126 customg.MustCommit(db) 127 return SetupGenesisBlock(db, DefaultTestnetGenesisBlock()) 128 }, 129 wantErr: &GenesisMismatchError{Stored: customghash, New: DefaultTestnetGenesisHash}, 130 wantHash: DefaultTestnetGenesisHash, 131 wantConfig: params.TestnetChainConfig, 132 }, 133 { 134 name: "custom block in DB, genesis == energi testnet", 135 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 136 customg.MustCommit(db) 137 return SetupGenesisBlock(db, DefaultEnergiTestnetGenesisBlock()) 138 }, 139 wantErr: &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash}, 140 wantHash: params.TestnetGenesisHash, 141 wantConfig: params.EnergiTestnetChainConfig, 142 }, 143 { 144 name: "compatible config in DB", 145 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 146 oldcustomg.MustCommit(db) 147 return SetupGenesisBlock(db, &customg) 148 }, 149 wantHash: customghash, 150 wantConfig: customg.Config, 151 }, 152 { 153 name: "incompatible config in DB", 154 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 155 // Commit the 'old' genesis block with Homestead transition at #2. 156 // Advance to block #4, past the homestead transition block of customg. 157 genesis := oldcustomg.MustCommit(db) 158 159 bc, _ := NewBlockChain(db, nil, oldcustomg.Config, ethash.NewFullFaker(), vm.Config{}, nil) 160 defer bc.Stop() 161 162 blocks, _ := GenerateChain(oldcustomg.Config, genesis, ethash.NewFaker(), db, 4, nil) 163 bc.InsertChain(blocks) 164 bc.CurrentBlock() 165 // This should return a compatibility error. 166 return SetupGenesisBlock(db, &customg) 167 }, 168 wantHash: customghash, 169 wantConfig: customg.Config, 170 wantErr: ¶ms.ConfigCompatError{ 171 What: "Homestead fork block", 172 StoredConfig: big.NewInt(2), 173 NewConfig: big.NewInt(3), 174 RewindTo: 1, 175 }, 176 }, 177 } 178 179 for _, test := range tests { 180 t.Run(test.name, func(t *testing.T) { 181 db := ethdb.NewMemDatabase() 182 config, hash, err := test.fn(db) 183 // Check the return values. 184 if !reflect.DeepEqual(err, test.wantErr) { 185 spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true} 186 t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr)) 187 } 188 if !reflect.DeepEqual(config, test.wantConfig) { 189 t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig) 190 } 191 if hash != test.wantHash { 192 t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex()) 193 } else if err == nil { 194 // Check database content. 195 stored := rawdb.ReadBlock(db, test.wantHash, 0) 196 if stored.Hash() != test.wantHash { 197 t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash) 198 } 199 } 200 }) 201 } 202 }