github.com/bhs-gq/quorum-hotstuff@v21.1.0+incompatible/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  	"errors"
    21  	"math/big"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/davecgh/go-spew/spew"
    26  	"github.com/ethereum/go-ethereum/common"
    27  
    28  	//"github.com/ethereum/go-ethereum/consensus/ethash"
    29  	"github.com/ethereum/go-ethereum/core/rawdb"
    30  	//"github.com/ethereum/go-ethereum/core/vm"
    31  	"github.com/ethereum/go-ethereum/ethdb"
    32  	"github.com/ethereum/go-ethereum/params"
    33  )
    34  
    35  func TestDefaultGenesisBlock(t *testing.T) {
    36  	block := DefaultGenesisBlock().ToBlock(nil)
    37  	if block.Hash() != params.MainnetGenesisHash {
    38  		t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash)
    39  	}
    40  	block = DefaultTestnetGenesisBlock().ToBlock(nil)
    41  	if block.Hash() != params.TestnetGenesisHash {
    42  		t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash(), params.TestnetGenesisHash)
    43  	}
    44  }
    45  
    46  func TestSetupGenesis(t *testing.T) {
    47  	// Quorum: customized test cases for quorum
    48  	var (
    49  		customg = Genesis{
    50  			Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3), IsQuorum: true},
    51  			Alloc: GenesisAlloc{
    52  				{1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
    53  			},
    54  		}
    55  		oldcustomg = customg
    56  	)
    57  	oldcustomg.Config = &params.ChainConfig{HomesteadBlock: big.NewInt(2)}
    58  	tests := []struct {
    59  		name       string
    60  		fn         func(ethdb.Database) (*params.ChainConfig, common.Hash, error)
    61  		wantConfig *params.ChainConfig
    62  		wantHash   common.Hash
    63  		wantErr    error
    64  	}{
    65  		{
    66  			name: "genesis without ChainConfig",
    67  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    68  				return SetupGenesisBlock(db, new(Genesis))
    69  			},
    70  			wantErr:    errGenesisNoConfig,
    71  			wantConfig: params.AllEthashProtocolChanges,
    72  		},
    73  		{
    74  			name: "no block in DB, genesis == nil",
    75  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    76  				return SetupGenesisBlock(db, nil)
    77  			},
    78  			wantHash:   params.MainnetGenesisHash,
    79  			wantConfig: params.MainnetChainConfig,
    80  		},
    81  		{
    82  			name: "mainnet block in DB, genesis == nil",
    83  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    84  				DefaultGenesisBlock().MustCommit(db)
    85  				return SetupGenesisBlock(db, nil)
    86  			},
    87  			wantHash:   params.MainnetGenesisHash,
    88  			wantConfig: params.MainnetChainConfig,
    89  		},
    90  		{
    91  			name: "genesis with incorrect SizeLimit",
    92  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    93  				customg.Config.TransactionSizeLimit = 100000
    94  				customg.Config.MaxCodeSize = 32
    95  				return SetupGenesisBlock(db, &customg)
    96  			},
    97  			wantErr:    errors.New("Genesis transaction size limit must be between 32 and 128"),
    98  			wantConfig: customg.Config,
    99  		},
   100  		{
   101  			name: "genesis with incorrect max code size ",
   102  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   103  				customg.Config.TransactionSizeLimit = 64
   104  				customg.Config.MaxCodeSize = 100000
   105  				return SetupGenesisBlock(db, &customg)
   106  			},
   107  			wantErr:    errors.New("Genesis max code size must be between 24 and 128"),
   108  			wantConfig: customg.Config,
   109  		},
   110  	}
   111  
   112  	for _, test := range tests {
   113  		db := rawdb.NewMemoryDatabase()
   114  		config, hash, err := test.fn(db)
   115  		// Check the return values.
   116  		if !reflect.DeepEqual(err, test.wantErr) {
   117  			spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true}
   118  			t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
   119  		}
   120  		if !reflect.DeepEqual(config, test.wantConfig) {
   121  			t.Errorf("%s:\nreturned %v\nwant     %v", test.name, config, test.wantConfig)
   122  		}
   123  		if hash != test.wantHash {
   124  			t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex())
   125  		} else if err == nil {
   126  			// Check database content.
   127  			stored := rawdb.ReadBlock(db, test.wantHash, 0)
   128  			if stored.Hash() != test.wantHash {
   129  				t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
   130  			}
   131  		}
   132  	}
   133  }