gitlab.com/lightnet1/evrynet-node@v1.1.0/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 26 "gitlab.com/lightnet1/evrynet-node/common" 27 "gitlab.com/lightnet1/evrynet-node/consensus/ethash" 28 "gitlab.com/lightnet1/evrynet-node/core/rawdb" 29 "gitlab.com/lightnet1/evrynet-node/core/vm" 30 "gitlab.com/lightnet1/evrynet-node/evrdb" 31 "gitlab.com/lightnet1/evrynet-node/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().Hex(), params.MainnetGenesisHash.Hex()) 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().Hex(), params.TestnetGenesisHash.Hex()) 42 } 43 } 44 45 func TestSetupGenesis(t *testing.T) { 46 var ( 47 customghash = common.HexToHash("0x9079ce192cd7b41d97af1b246eb371ef45f1d934cc43504f5a0787f6b3cdc0ba") 48 customg = Genesis{ 49 Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3)}, 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(evrdb.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 evrdb.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 evrdb.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 evrdb.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: "custom block in DB, genesis == nil", 91 fn: func(db evrdb.Database) (*params.ChainConfig, common.Hash, error) { 92 customg.MustCommit(db) 93 return SetupGenesisBlock(db, nil) 94 }, 95 wantHash: customghash, 96 wantConfig: customg.Config, 97 }, 98 { 99 name: "custom block in DB, genesis == testnet", 100 fn: func(db evrdb.Database) (*params.ChainConfig, common.Hash, error) { 101 customg.MustCommit(db) 102 return SetupGenesisBlock(db, DefaultTestnetGenesisBlock()) 103 }, 104 wantErr: &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash}, 105 wantHash: params.TestnetGenesisHash, 106 wantConfig: params.TestnetChainConfig, 107 }, 108 { 109 name: "compatible config in DB", 110 fn: func(db evrdb.Database) (*params.ChainConfig, common.Hash, error) { 111 oldcustomg.MustCommit(db) 112 return SetupGenesisBlock(db, &customg) 113 }, 114 wantHash: customghash, 115 wantConfig: customg.Config, 116 }, 117 { 118 name: "incompatible config in DB", 119 fn: func(db evrdb.Database) (*params.ChainConfig, common.Hash, error) { 120 // Commit the 'old' genesis block with Homestead transition at #2. 121 // Advance to block #4, past the homestead transition block of customg. 122 genesis := oldcustomg.MustCommit(db) 123 124 bc, _ := NewBlockChain(db, nil, oldcustomg.Config, ethash.NewFullFaker(), vm.Config{}, nil) 125 defer bc.Stop() 126 127 blocks, _ := GenerateChain(oldcustomg.Config, genesis, ethash.NewFaker(), db, 4, nil) 128 bc.InsertChain(blocks) 129 bc.CurrentBlock() 130 // This should return a compatibility error. 131 return SetupGenesisBlock(db, &customg) 132 }, 133 wantHash: customghash, 134 wantConfig: customg.Config, 135 wantErr: ¶ms.ConfigCompatError{ 136 What: "Homestead fork block", 137 StoredConfig: big.NewInt(2), 138 NewConfig: big.NewInt(3), 139 RewindTo: 1, 140 }, 141 }, 142 } 143 144 for _, test := range tests { 145 db := rawdb.NewMemoryDatabase() 146 config, hash, err := test.fn(db) 147 // Check the return values. 148 if !reflect.DeepEqual(err, test.wantErr) { 149 spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true} 150 t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr)) 151 } 152 if !reflect.DeepEqual(config, test.wantConfig) { 153 t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig) 154 } 155 if hash != test.wantHash { 156 t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex()) 157 } else if err == nil { 158 // Check database content. 159 stored := rawdb.ReadBlock(db, test.wantHash, 0) 160 if stored.Hash() != test.wantHash { 161 t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash) 162 } 163 } 164 } 165 }