github.com/zjj1991/quorum@v0.0.0-20190524123704-ae4b0a1e1a19/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  	//"github.com/ethereum/go-ethereum/consensus/ethash"
    28  	"github.com/ethereum/go-ethereum/core/rawdb"
    29  	//"github.com/ethereum/go-ethereum/core/vm"
    30  	"github.com/ethereum/go-ethereum/ethdb"
    31  	"github.com/ethereum/go-ethereum/params"
    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 = DefaultTestnetGenesisBlock().ToBlock(nil)
    40  	if block.Hash() != params.TestnetGenesisHash {
    41  		t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash(), params.TestnetGenesisHash)
    42  	}
    43  }
    44  
    45  func TestSetupGenesis(t *testing.T) {
    46  	var (
    47  		// customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50")
    48  		customg = Genesis{
    49  			Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3), IsQuorum: true},
    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 = &params.ChainConfig{HomesteadBlock: big.NewInt(2)}
    57  	tests := []struct {
    58  		name       string
    59  		fn         func(ethdb.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 ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    67  				return SetupGenesisBlock(db, new(Genesis))
    68  			},
    69  			wantErr:    errGenesisNoConfig,
    70  			wantConfig: params.AllEthashProtocolChanges,
    71  		},
    72  		{
    73  			name: "no block in DB, genesis == nil",
    74  			fn: func(db ethdb.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 ethdb.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: "genesis with incorrect SizeLimit",
    91  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    92  				customg.Config.TransactionSizeLimit = 100000
    93  				return SetupGenesisBlock(db, &customg)
    94  			},
    95  			wantErr:    errors.New("Genesis transaction size limit must be between 32 and 128"),
    96  			wantConfig: customg.Config,
    97  		},
    98  		// {
    99  		// 	name: "custom block in DB, genesis == nil",
   100  		// 	fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   101  		// 		customg.MustCommit(db)
   102  		// 		return SetupGenesisBlock(db, nil)
   103  		// 	},
   104  		// 	wantHash:   customghash,
   105  		// 	wantConfig: &params.ChainConfig{HomesteadBlock: big.NewInt(3), IsQuorum: true},
   106  		// // },
   107  		// {
   108  		// 	name: "custom block in DB, genesis == testnet",
   109  		// 	fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   110  		// 		customg.MustCommit(db)
   111  		// 		return SetupGenesisBlock(db, DefaultTestnetGenesisBlock())
   112  		// 	},
   113  		// 	wantErr:    &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash},
   114  		// 	wantHash:   params.TestnetGenesisHash,
   115  		// 	wantConfig: params.TestnetChainConfig,
   116  		// },
   117  		// {
   118  		// 	name: "compatible config in DB",
   119  		// 	fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   120  		// 		oldcustomg.MustCommit(db)
   121  		// 		return SetupGenesisBlock(db, &customg)
   122  		// 	},
   123  		// 	wantHash:   customghash,
   124  		// 	wantConfig: customg.Config,
   125  		// },
   126  		// {
   127  		// 	name: "incompatible config in DB",
   128  		// 	fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   129  		// 		// Commit the 'old' genesis block with Homestead transition at #2.
   130  		// 		// Advance to block #4, past the homestead transition block of customg.
   131  		// 		genesis := oldcustomg.MustCommit(db)
   132  		// 		bc, _ := NewBlockChain(db, oldcustomg.Config, ethash.NewFullFaker(), vm.Config{})
   133  		// 		defer bc.Stop()
   134  		// 		bc.SetValidator(bproc{})
   135  		// 		bc.InsertChain(makeBlockChainWithDiff(genesis, []int{2, 3, 4, 5}, 0))
   136  		// 		bc.CurrentBlock()
   137  		// 		// This should return a compatibility error.
   138  		// 		return SetupGenesisBlock(db, &customg)
   139  		// 	},
   140  		// 	wantHash:   customghash,
   141  		// 	wantConfig: customg.Config,
   142  		// 	wantErr: &params.ConfigCompatError{
   143  		// 		What:         "Homestead fork block",
   144  		// 		StoredConfig: big.NewInt(2),
   145  		// 		NewConfig:    big.NewInt(3),
   146  		// 		RewindTo:     1,
   147  		// 	},
   148  		// },
   149  	}
   150  
   151  	for _, test := range tests {
   152  		db := ethdb.NewMemDatabase()
   153  		config, hash, err := test.fn(db)
   154  		// Check the return values.
   155  		if !reflect.DeepEqual(err, test.wantErr) {
   156  			spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true}
   157  			t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
   158  		}
   159  		if !reflect.DeepEqual(config, test.wantConfig) {
   160  			t.Errorf("%s:\nreturned %v\nwant     %v", test.name, config, test.wantConfig)
   161  		}
   162  		if hash != test.wantHash {
   163  			t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex())
   164  		} else if err == nil {
   165  			// Check database content.
   166  			stored := rawdb.ReadBlock(db, test.wantHash, 0)
   167  			if stored.Hash() != test.wantHash {
   168  				t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
   169  			}
   170  		}
   171  	}
   172  }