github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/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/vntchain/go-vnt/common" 26 "github.com/vntchain/go-vnt/consensus/mock" 27 "github.com/vntchain/go-vnt/core/rawdb" 28 "github.com/vntchain/go-vnt/core/vm" 29 "github.com/vntchain/go-vnt/params" 30 "github.com/vntchain/go-vnt/vntdb" 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 } 39 40 func TestSetupGenesis(t *testing.T) { 41 var ( 42 customghash = common.HexToHash("0x5ebf429a94e504ecb570a2c0591ef098ffd1307d35152f0572c503ec442db4a3") 43 customg = Genesis{ 44 Config: ¶ms.ChainConfig{HubbleBlock: big.NewInt(3)}, 45 Alloc: GenesisAlloc{ 46 {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, 47 }, 48 } 49 oldcustomg = customg 50 ) 51 oldcustomg.Config = ¶ms.ChainConfig{HubbleBlock: big.NewInt(2)} 52 tests := []struct { 53 name string 54 fn func(vntdb.Database) (*params.ChainConfig, common.Hash, error) 55 wantConfig *params.ChainConfig 56 wantHash common.Hash 57 wantErr error 58 }{ 59 { 60 name: "genesis without ChainConfig", 61 fn: func(db vntdb.Database) (*params.ChainConfig, common.Hash, error) { 62 return SetupGenesisBlock(db, new(Genesis)) 63 }, 64 wantErr: errGenesisNoConfig, 65 wantConfig: params.TestChainConfig, 66 }, 67 { 68 name: "no block in DB, genesis == nil", 69 fn: func(db vntdb.Database) (*params.ChainConfig, common.Hash, error) { 70 return SetupGenesisBlock(db, nil) 71 }, 72 wantHash: params.MainnetGenesisHash, 73 wantConfig: params.MainnetChainConfig, 74 }, 75 { 76 name: "mainnet block in DB, genesis == nil", 77 fn: func(db vntdb.Database) (*params.ChainConfig, common.Hash, error) { 78 DefaultGenesisBlock().MustCommit(db) 79 return SetupGenesisBlock(db, nil) 80 }, 81 wantHash: params.MainnetGenesisHash, 82 wantConfig: params.MainnetChainConfig, 83 }, 84 { 85 name: "custom block in DB, genesis == nil", 86 fn: func(db vntdb.Database) (*params.ChainConfig, common.Hash, error) { 87 customg.MustCommit(db) 88 return SetupGenesisBlock(db, nil) 89 }, 90 wantHash: customghash, 91 wantConfig: customg.Config, 92 }, 93 { 94 name: "compatible config in DB", 95 fn: func(db vntdb.Database) (*params.ChainConfig, common.Hash, error) { 96 oldcustomg.MustCommit(db) 97 return SetupGenesisBlock(db, &customg) 98 }, 99 wantHash: customghash, 100 wantConfig: customg.Config, 101 }, 102 { 103 name: "incompatible config in DB", 104 fn: func(db vntdb.Database) (*params.ChainConfig, common.Hash, error) { 105 // Commit the 'old' genesis block with Hubble transition at #2. 106 // Advance to block #4, past the Hubble transition block of customg. 107 genesis := oldcustomg.MustCommit(db) 108 109 bc, _ := NewBlockChain(db, nil, oldcustomg.Config, mock.NewMock(), vm.Config{}) 110 defer bc.Stop() 111 112 blocks, _ := GenerateChain(oldcustomg.Config, genesis, mock.NewMock(), db, 4, nil) 113 bc.InsertChain(blocks) 114 bc.CurrentBlock() 115 // This should return a compatibility error. 116 return SetupGenesisBlock(db, &customg) 117 }, 118 wantHash: customghash, 119 wantConfig: customg.Config, 120 wantErr: ¶ms.ConfigCompatError{ 121 What: "Hubble fork block", 122 StoredConfig: big.NewInt(2), 123 NewConfig: big.NewInt(3), 124 RewindTo: 1, 125 }, 126 }, 127 } 128 129 for _, test := range tests { 130 db := vntdb.NewMemDatabase() 131 config, hash, err := test.fn(db) 132 // Check the return values. 133 if !reflect.DeepEqual(err, test.wantErr) { 134 spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true} 135 t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr)) 136 } 137 if !reflect.DeepEqual(config, test.wantConfig) { 138 t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig) 139 } 140 if hash != test.wantHash { 141 t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex()) 142 } else if err == nil { 143 // Check database content. 144 stored := rawdb.ReadBlock(db, test.wantHash, 0) 145 if stored.Hash() != test.wantHash { 146 t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash) 147 } 148 } 149 } 150 }