123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- // 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 <http://www.gnu.org/licenses/>.
- package core
- import (
- "errors"
- "math/big"
- "reflect"
- "testing"
- "github.com/davecgh/go-spew/spew"
- "github.com/ethereum/go-ethereum/common"
- //"github.com/ethereum/go-ethereum/consensus/ethash"
- "github.com/ethereum/go-ethereum/core/rawdb"
- //"github.com/ethereum/go-ethereum/core/vm"
- "github.com/ethereum/go-ethereum/ethdb"
- "github.com/ethereum/go-ethereum/params"
- )
- func TestDefaultGenesisBlock(t *testing.T) {
- block := DefaultGenesisBlock().ToBlock(nil)
- if block.Hash() != params.MainnetGenesisHash {
- t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash)
- }
- block = DefaultRopstenGenesisBlock().ToBlock(nil)
- if block.Hash() != params.RopstenGenesisHash {
- t.Errorf("wrong ropsten genesis hash, got %v, want %v", block.Hash(), params.RopstenGenesisHash)
- }
- }
- func TestSetupGenesis(t *testing.T) {
- // Quorum: customized test cases for quorum
- var (
- customg = Genesis{
- Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3), IsQuorum: true},
- Alloc: 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.MainnetChainConfig,
- },
- {
- 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.MainnetChainConfig,
- },
- {
- name: "genesis with incorrect SizeLimit",
- fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
- customg.Config.TransactionSizeLimit = 100000
- customg.Config.MaxCodeSize = 32
- return SetupGenesisBlock(db, &customg)
- },
- wantErr: errors.New("Genesis transaction size limit must be between 32 and 128"),
- wantConfig: customg.Config,
- },
- {
- name: "genesis with incorrect max code size ",
- fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
- customg.Config.TransactionSizeLimit = 64
- customg.Config.MaxCodeSize = 100000
- return SetupGenesisBlock(db, &customg)
- },
- wantErr: errors.New("Genesis max code size must be between 24 and 128"),
- wantConfig: customg.Config,
- },
- }
- for _, test := range tests {
- db := rawdb.NewMemoryDatabase()
- config, hash, err := test.fn(db)
- // Check the return values.
- if !reflect.DeepEqual(err, test.wantErr) {
- 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)
- }
- }
- }
- }
- // TestGenesisHashes checks the congruity of default genesis data to corresponding hardcoded genesis hash values.
- func TestGenesisHashes(t *testing.T) {
- cases := []struct {
- genesis *Genesis
- hash common.Hash
- }{
- {
- genesis: DefaultGenesisBlock(),
- hash: params.MainnetGenesisHash,
- },
- {
- genesis: DefaultGoerliGenesisBlock(),
- hash: params.GoerliGenesisHash,
- },
- {
- genesis: DefaultRopstenGenesisBlock(),
- hash: params.RopstenGenesisHash,
- },
- {
- genesis: DefaultRinkebyGenesisBlock(),
- hash: params.RinkebyGenesisHash,
- },
- {
- genesis: DefaultYoloV3GenesisBlock(),
- hash: params.YoloV3GenesisHash,
- },
- }
- for i, c := range cases {
- b := c.genesis.MustCommit(rawdb.NewMemoryDatabase())
- if got := b.Hash(); got != c.hash {
- t.Errorf("case: %d, want: %s, got: %s", i, c.hash.Hex(), got.Hex())
- }
- }
- }
|