// Copyright 2017 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . package core import ( "errors" "fmt" "math/big" "reflect" "testing" "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/consensus/ethash" "github.com/XinFinOrg/XDPoSChain/core/rawdb" "github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/core/vm" "github.com/XinFinOrg/XDPoSChain/ethdb" "github.com/XinFinOrg/XDPoSChain/params" "github.com/davecgh/go-spew/spew" ) func TestDefaultGenesisBlock(t *testing.T) { block := DefaultGenesisBlock().ToBlock() if block.Hash() != params.MainnetGenesisHash { t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash().String(), params.MainnetGenesisHash.String()) } block = DefaultTestnetGenesisBlock().ToBlock() if block.Hash() != params.TestnetGenesisHash { t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash().String(), params.TestnetGenesisHash.String()) } } func TestSetupGenesis(t *testing.T) { var ( customghash = common.HexToHash("0xfc8a143549950b0d3e3e7b70b0067b152e6b903ab5438f1ea87f0448ec93da48") customg = Genesis{ Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3)}, Alloc: types.GenesisAlloc{ {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, }, } oldcustomg = customg ) oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2)} tests := []struct { name string fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error) wantConfig *params.ChainConfig wantHash common.Hash wantErr error }{ { name: "genesis without ChainConfig", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { return SetupGenesisBlock(db, new(Genesis)) }, wantErr: errGenesisNoConfig, wantConfig: params.AllEthashProtocolChanges, }, { name: "no block in DB, genesis == nil", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { return SetupGenesisBlock(db, nil) }, wantHash: params.MainnetGenesisHash, wantConfig: params.XDCMainnetChainConfig, }, { name: "mainnet block in DB, genesis == nil", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { DefaultGenesisBlock().MustCommit(db) return SetupGenesisBlock(db, nil) }, wantHash: params.MainnetGenesisHash, wantConfig: params.XDCMainnetChainConfig, }, { name: "custom block in DB, genesis == nil", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { customg.MustCommit(db) return SetupGenesisBlock(db, nil) }, wantHash: customghash, wantConfig: customg.Config, }, { name: "custom block in DB, genesis == testnet", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { customg.MustCommit(db) return SetupGenesisBlock(db, DefaultTestnetGenesisBlock()) }, wantErr: &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash}, wantHash: params.TestnetGenesisHash, wantConfig: params.TestnetChainConfig, }, { name: "stored canonical hash without header", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { missingHash := common.HexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") rawdb.WriteCanonicalHash(db, missingHash, 0) return SetupGenesisBlock(db, nil) }, wantErr: fmt.Errorf("missing genesis header for hash: %s", common.HexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").Hex()), wantHash: common.HexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), wantConfig: params.AllEthashProtocolChanges, }, { name: "genesis header present but state missing", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { block := DefaultGenesisBlock().ToBlock() rawdb.WriteCanonicalHash(db, block.Hash(), 0) rawdb.WriteHeader(db, block.Header()) return SetupGenesisBlock(db, nil) }, wantHash: params.MainnetGenesisHash, wantConfig: params.XDCMainnetChainConfig, }, { name: "genesis block without chain config", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { block := DefaultGenesisBlock().ToBlock() rawdb.WriteBlock(db, block) rawdb.WriteCanonicalHash(db, block.Hash(), 0) return SetupGenesisBlock(db, nil) }, wantHash: params.MainnetGenesisHash, wantConfig: params.XDCMainnetChainConfig, }, { name: "missing block number for head header hash", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { DefaultGenesisBlock().MustCommit(db) rawdb.WriteHeadHeaderHash(db, common.HexToHash("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) return SetupGenesisBlock(db, nil) }, wantErr: errors.New("missing block number for head header hash"), wantHash: params.MainnetGenesisHash, wantConfig: params.XDCMainnetChainConfig, }, { name: "compatible config in DB", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { oldcustomg.MustCommit(db) return SetupGenesisBlock(db, &customg) }, wantHash: customghash, wantConfig: customg.Config, }, { name: "incompatible config in DB", fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) { // Commit the 'old' genesis block with Homestead transition at #2. // Advance to block #4, past the homestead transition block of customg. genesis := oldcustomg.MustCommit(db) bc, _ := NewBlockChain(db, nil, &oldcustomg, ethash.NewFullFaker(), vm.Config{}) defer bc.Stop() blocks, _ := GenerateChain(oldcustomg.Config, genesis, ethash.NewFaker(), db, 4, nil) bc.InsertChain(blocks) bc.CurrentBlock() // This should return a compatibility error. return SetupGenesisBlock(db, &customg) }, wantHash: customghash, wantConfig: customg.Config, wantErr: ¶ms.ConfigCompatError{ What: "Homestead fork block", StoredConfig: big.NewInt(2), NewConfig: big.NewInt(3), RewindTo: 1, }, }, } for _, test := range tests { db := rawdb.NewMemoryDatabase() config, hash, err := test.fn(db) // Check the return values. if (err == nil) != (test.wantErr == nil) || (err != nil && test.wantErr != nil && !errors.Is(err, test.wantErr) && err.Error() != test.wantErr.Error()) { spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true} t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr)) } if !reflect.DeepEqual(config, test.wantConfig) { t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig) } if hash != test.wantHash { t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex()) } else if err == nil { // Check database content. stored := rawdb.ReadBlock(db, test.wantHash, 0) if stored.Hash() != test.wantHash { t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash) } } } }