github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/core/genesis_test.go (about) 1 // Copyright 2017 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum 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 Spectrum 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 Spectrum 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/SmartMeshFoundation/Spectrum/common" 25 "github.com/SmartMeshFoundation/Spectrum/consensus/ethash" 26 "github.com/SmartMeshFoundation/Spectrum/core/vm" 27 "github.com/SmartMeshFoundation/Spectrum/ethdb" 28 "github.com/SmartMeshFoundation/Spectrum/params" 29 "github.com/davecgh/go-spew/spew" 30 ) 31 32 func TestDefaultGenesisBlock(t *testing.T) { 33 block, _ := DefaultGenesisBlock().ToBlock() 34 if block.Hash() != params.MainnetGenesisHash { 35 t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash) 36 } 37 block, _ = DefaultTestnetGenesisBlock().ToBlock() 38 if block.Hash() != params.TestnetGenesisHash { 39 t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash(), params.TestnetGenesisHash) 40 } 41 } 42 43 func TestSetupGenesis(t *testing.T) { 44 var ( 45 customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50") 46 customg = Genesis{ 47 Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3)}, 48 Alloc: GenesisAlloc{ 49 {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, 50 }, 51 } 52 oldcustomg = customg 53 ) 54 oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2)} 55 tests := []struct { 56 name string 57 fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error) 58 wantConfig *params.ChainConfig 59 wantHash common.Hash 60 wantErr error 61 }{ 62 { 63 name: "genesis without ChainConfig", 64 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 65 return SetupGenesisBlock(db, new(Genesis)) 66 }, 67 wantErr: errGenesisNoConfig, 68 wantConfig: params.AllEthashProtocolChanges, 69 }, 70 { 71 name: "no block in DB, genesis == nil", 72 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 73 return SetupGenesisBlock(db, nil) 74 }, 75 wantHash: params.MainnetGenesisHash, 76 wantConfig: params.MainnetChainConfig, 77 }, 78 { 79 name: "mainnet block in DB, genesis == nil", 80 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 81 DefaultGenesisBlock().MustCommit(db) 82 return SetupGenesisBlock(db, nil) 83 }, 84 wantHash: params.MainnetGenesisHash, 85 wantConfig: params.MainnetChainConfig, 86 }, 87 { 88 name: "custom block in DB, genesis == nil", 89 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 90 customg.MustCommit(db) 91 return SetupGenesisBlock(db, nil) 92 }, 93 wantHash: customghash, 94 wantConfig: customg.Config, 95 }, 96 { 97 name: "custom block in DB, genesis == testnet", 98 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 99 customg.MustCommit(db) 100 return SetupGenesisBlock(db, DefaultTestnetGenesisBlock()) 101 }, 102 wantErr: &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash}, 103 wantHash: params.TestnetGenesisHash, 104 wantConfig: params.TestnetChainConfig, 105 }, 106 { 107 name: "compatible config in DB", 108 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 109 oldcustomg.MustCommit(db) 110 return SetupGenesisBlock(db, &customg) 111 }, 112 wantHash: customghash, 113 wantConfig: customg.Config, 114 }, 115 { 116 name: "incompatible config in DB", 117 fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { 118 // Commit the 'old' genesis block with Homestead transition at #2. 119 // Advance to block #4, past the homestead transition block of customg. 120 genesis := oldcustomg.MustCommit(db) 121 bc, _ := NewBlockChain(db, oldcustomg.Config, ethash.NewFullFaker(), vm.Config{}) 122 defer bc.Stop() 123 //bc.SetValidator(bproc{}) 124 bc.InsertChain(makeBlockChainWithDiff(genesis, []int{2, 3, 4, 5}, 0)) 125 bc.CurrentBlock() 126 // This should return a compatibility error. 127 return SetupGenesisBlock(db, &customg) 128 }, 129 wantHash: customghash, 130 wantConfig: customg.Config, 131 wantErr: ¶ms.ConfigCompatError{ 132 What: "Homestead fork block", 133 StoredConfig: big.NewInt(2), 134 NewConfig: big.NewInt(3), 135 RewindTo: 1, 136 }, 137 }, 138 } 139 140 for _, test := range tests { 141 db, _ := ethdb.NewMemDatabase() 142 config, hash, err := test.fn(db) 143 // Check the return values. 144 if !reflect.DeepEqual(err, test.wantErr) { 145 spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true} 146 t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr)) 147 } 148 if !reflect.DeepEqual(config, test.wantConfig) { 149 t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig) 150 } 151 if hash != test.wantHash { 152 t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex()) 153 } else if err == nil { 154 // Check database content. 155 stored := GetBlock(db, test.wantHash, 0) 156 if stored.Hash() != test.wantHash { 157 t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash) 158 } 159 } 160 } 161 } 162 163 func TestDecodePrealloc(t *testing.T) { 164 fn := func(alloc string) { 165 mal := decodePrealloc(alloc) 166 c := big.NewInt(0) 167 for k, v := range mal { 168 e := new(big.Int).Div(v.Balance, big.NewInt(1)) 169 c = new(big.Int).Add(e, c) 170 t.Log(k.Hex(), new(big.Int).Div(v.Balance, big.NewInt(1))) 171 } 172 t.Log("---->", len(mal), c) 173 } 174 //fn(mainnetAllocData) 175 fn(testnetAllocData) 176 }