github.com/phillinzzz/newBsc@v1.1.6/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  	"math/big"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/davecgh/go-spew/spew"
    25  	"github.com/phillinzzz/newBsc/common"
    26  	"github.com/phillinzzz/newBsc/consensus/ethash"
    27  	"github.com/phillinzzz/newBsc/core/rawdb"
    28  	"github.com/phillinzzz/newBsc/core/vm"
    29  	"github.com/phillinzzz/newBsc/ethdb"
    30  	"github.com/phillinzzz/newBsc/params"
    31  )
    32  
    33  func TestDefaultGenesisBlock(t *testing.T) {
    34  	block := DefaultGenesisBlock().ToBlock(nil)
    35  	if block.Hash() != params.MainnetGenesisHash {
    36  		t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash)
    37  	}
    38  	block = DefaultRopstenGenesisBlock().ToBlock(nil)
    39  	if block.Hash() != params.RopstenGenesisHash {
    40  		t.Errorf("wrong ropsten genesis hash, got %v, want %v", block.Hash(), params.RopstenGenesisHash)
    41  	}
    42  }
    43  
    44  func TestSetupGenesis(t *testing.T) {
    45  	var (
    46  		customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50")
    47  		customg     = Genesis{
    48  			Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3)},
    49  			Alloc: GenesisAlloc{
    50  				{1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
    51  			},
    52  		}
    53  		oldcustomg = customg
    54  	)
    55  	oldcustomg.Config = &params.ChainConfig{HomesteadBlock: big.NewInt(2)}
    56  	tests := []struct {
    57  		name       string
    58  		fn         func(ethdb.Database) (*params.ChainConfig, common.Hash, error)
    59  		wantConfig *params.ChainConfig
    60  		wantHash   common.Hash
    61  		wantErr    error
    62  	}{
    63  		{
    64  			name: "genesis without ChainConfig",
    65  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    66  				return SetupGenesisBlock(db, new(Genesis))
    67  			},
    68  			wantErr:    errGenesisNoConfig,
    69  			wantConfig: params.AllEthashProtocolChanges,
    70  		},
    71  		{
    72  			name: "no block in DB, genesis == nil",
    73  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    74  				return SetupGenesisBlock(db, nil)
    75  			},
    76  			wantHash:   params.MainnetGenesisHash,
    77  			wantConfig: params.MainnetChainConfig,
    78  		},
    79  		{
    80  			name: "custom block in DB, genesis == nil",
    81  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    82  				customg.MustCommit(db)
    83  				return SetupGenesisBlock(db, nil)
    84  			},
    85  			wantHash:   customghash,
    86  			wantConfig: customg.Config,
    87  		},
    88  		{
    89  			name: "custom block in DB, genesis == ropsten",
    90  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    91  				customg.MustCommit(db)
    92  				return SetupGenesisBlock(db, DefaultRopstenGenesisBlock())
    93  			},
    94  			wantErr:    &GenesisMismatchError{Stored: customghash, New: params.RopstenGenesisHash},
    95  			wantHash:   params.RopstenGenesisHash,
    96  			wantConfig: params.RopstenChainConfig,
    97  		},
    98  		{
    99  			name: "compatible config in DB",
   100  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   101  				oldcustomg.MustCommit(db)
   102  				return SetupGenesisBlock(db, &customg)
   103  			},
   104  			wantHash:   customghash,
   105  			wantConfig: customg.Config,
   106  		},
   107  		{
   108  			name: "incompatible config in DB",
   109  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   110  				// Commit the 'old' genesis block with Homestead transition at #2.
   111  				// Advance to block #4, past the homestead transition block of customg.
   112  				genesis := oldcustomg.MustCommit(db)
   113  
   114  				bc, _ := NewBlockChain(db, nil, oldcustomg.Config, ethash.NewFullFaker(), vm.Config{}, nil, nil)
   115  				defer bc.Stop()
   116  
   117  				blocks, _ := GenerateChain(oldcustomg.Config, genesis, ethash.NewFaker(), db, 4, nil)
   118  				bc.InsertChain(blocks)
   119  				bc.CurrentBlock()
   120  				// This should return a compatibility error.
   121  				return SetupGenesisBlock(db, &customg)
   122  			},
   123  			wantHash:   customghash,
   124  			wantConfig: customg.Config,
   125  			wantErr: &params.ConfigCompatError{
   126  				What:         "Homestead fork block",
   127  				StoredConfig: big.NewInt(2),
   128  				NewConfig:    big.NewInt(3),
   129  				RewindTo:     1,
   130  			},
   131  		},
   132  	}
   133  
   134  	for _, test := range tests {
   135  		db := rawdb.NewMemoryDatabase()
   136  		config, hash, err := test.fn(db)
   137  		// Check the return values.
   138  		if !reflect.DeepEqual(err, test.wantErr) {
   139  			spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true}
   140  			t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
   141  		}
   142  		if !reflect.DeepEqual(config, test.wantConfig) {
   143  			t.Errorf("%s:\nreturned %v\nwant     %v", test.name, config, test.wantConfig)
   144  		}
   145  		if hash != test.wantHash {
   146  			t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex())
   147  		} else if err == nil {
   148  			// Check database content.
   149  			stored := rawdb.ReadBlock(db, test.wantHash, 0)
   150  			if stored.Hash() != test.wantHash {
   151  				t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
   152  			}
   153  		}
   154  	}
   155  }
   156  
   157  // TestGenesisHashes checks the congruity of default genesis data to corresponding hardcoded genesis hash values.
   158  func TestGenesisHashes(t *testing.T) {
   159  	cases := []struct {
   160  		genesis *Genesis
   161  		hash    common.Hash
   162  	}{
   163  		{
   164  			genesis: DefaultGenesisBlock(),
   165  			hash:    params.MainnetGenesisHash,
   166  		},
   167  		{
   168  			genesis: DefaultGoerliGenesisBlock(),
   169  			hash:    params.GoerliGenesisHash,
   170  		},
   171  		{
   172  			genesis: DefaultRopstenGenesisBlock(),
   173  			hash:    params.RopstenGenesisHash,
   174  		},
   175  		{
   176  			genesis: DefaultRinkebyGenesisBlock(),
   177  			hash:    params.RinkebyGenesisHash,
   178  		},
   179  		{
   180  			genesis: DefaultYoloV3GenesisBlock(),
   181  			hash:    params.YoloV3GenesisHash,
   182  		},
   183  	}
   184  	for i, c := range cases {
   185  		b := c.genesis.MustCommit(rawdb.NewMemoryDatabase())
   186  		if got := b.Hash(); got != c.hash {
   187  			t.Errorf("case: %d, want: %s, got: %s", i, c.hash.Hex(), got.Hex())
   188  		}
   189  	}
   190  }