github.com/core-coin/go-core/v2@v2.1.9/core/genesis_test.go (about) 1 // Copyright 2017 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core 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 "github.com/core-coin/go-core/v2/common" 27 "github.com/core-coin/go-core/v2/consensus/cryptore" 28 "github.com/core-coin/go-core/v2/core/rawdb" 29 "github.com/core-coin/go-core/v2/core/vm" 30 "github.com/core-coin/go-core/v2/params" 31 "github.com/core-coin/go-core/v2/xcbdb" 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 = DefaultDevinGenesisBlock().ToBlock(nil) 40 if block.Hash() != params.DevinGenesisHash { 41 t.Errorf("wrong devin genesis hash, got %v, want %v", block.Hash(), params.DevinGenesisHash) 42 } 43 } 44 45 func TestSetupGenesis(t *testing.T) { 46 var ( 47 customghash = common.HexToHash("0xd3fde1cd83b29aea233b42e6c72bc51f77d44911d829ae0e0627d5ccad74da66") 48 customg = Genesis{ 49 Config: ¶ms.ChainConfig{NetworkID: big.NewInt(1), EWASMBlock: 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{NetworkID: big.NewInt(1), EWASMBlock: big.NewInt(2)} 57 tests := []struct { 58 name string 59 fn func(xcbdb.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 xcbdb.Database) (*params.ChainConfig, common.Hash, error) { 67 return SetupGenesisBlock(db, new(Genesis)) 68 }, 69 wantErr: errGenesisNoConfig, 70 wantConfig: params.MainnetChainConfig, 71 }, 72 { 73 name: "no block in DB, genesis == nil", 74 fn: func(db xcbdb.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 xcbdb.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 xcbdb.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 == devin", 100 fn: func(db xcbdb.Database) (*params.ChainConfig, common.Hash, error) { 101 customg.MustCommit(db) 102 return SetupGenesisBlock(db, DefaultDevinGenesisBlock()) 103 }, 104 wantErr: &GenesisMismatchError{Stored: customghash, New: params.DevinGenesisHash}, 105 wantHash: params.DevinGenesisHash, 106 wantConfig: params.DevinChainConfig, 107 }, 108 { 109 name: "compatible config in DB", 110 fn: func(db xcbdb.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 xcbdb.Database) (*params.ChainConfig, common.Hash, error) { 120 // Commit the 'old' genesis block with transition at #2. 121 // Advance to block #4, past the transition block of customg. 122 genesis := oldcustomg.MustCommit(db) 123 bc, err := NewBlockChain(db, nil, oldcustomg.Config, cryptore.NewFullFaker(), vm.Config{}, nil, nil) 124 if err != nil { 125 panic(err) 126 } 127 defer bc.Stop() 128 blocks, _ := GenerateChain(oldcustomg.Config, genesis, cryptore.NewFaker(), db, 4, nil) 129 _, err = bc.InsertChain(blocks) 130 if err != nil { 131 panic(err) 132 } 133 bc, err = NewBlockChain(db, nil, oldcustomg.Config, cryptore.NewFullFaker(), vm.Config{}, nil, nil) 134 if err != nil { 135 panic(err) 136 } 137 bc.CurrentBlock() 138 // This should return a compatibility error. 139 return SetupGenesisBlock(db, &customg) 140 }, 141 wantHash: customghash, 142 wantConfig: customg.Config, 143 wantErr: ¶ms.ConfigCompatError{ 144 What: "ewasm fork block", 145 StoredConfig: big.NewInt(2), 146 NewConfig: big.NewInt(3), 147 RewindTo: 1, 148 }, 149 }, 150 } 151 152 for _, test := range tests { 153 db := rawdb.NewMemoryDatabase() 154 config, hash, err := test.fn(db) 155 // Check the return values. 156 if !reflect.DeepEqual(err, test.wantErr) { 157 spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true} 158 t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr)) 159 } 160 if !reflect.DeepEqual(config, test.wantConfig) { 161 t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig) 162 } 163 if hash != test.wantHash { 164 t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex()) 165 } else if err == nil { 166 // Check database content. 167 stored := rawdb.ReadBlock(db, test.wantHash, 0) 168 if stored.Hash() != test.wantHash { 169 t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash) 170 } 171 } 172 } 173 }