github.com/edxfund/masterchain@v1.8.16-0.20190112084457-6ad8bdd0f74a/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  	"github.com/EDXFund/MasterChain/core/types"
    21  	"math/big"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/EDXFund/MasterChain/common"
    26  	"github.com/EDXFund/MasterChain/consensus/ethash"
    27  	"github.com/EDXFund/MasterChain/core/rawdb"
    28  	"github.com/EDXFund/MasterChain/core/vm"
    29  	"github.com/EDXFund/MasterChain/ethdb"
    30  	"github.com/EDXFund/MasterChain/params"
    31  	"github.com/davecgh/go-spew/spew"
    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  func TestSetupGenesisM(t *testing.T){
    45  	testSetupGenesis(t,types.ShardMaster)
    46  }
    47  func TestSetupGenesisS(t *testing.T){
    48  	testSetupGenesis(t,0)
    49  }
    50  func testSetupGenesis(t *testing.T,ShardId uint16) {
    51  	var (
    52  		customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50")
    53  		customg     = Genesis{
    54  			Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3)},
    55  			Alloc: GenesisAlloc{
    56  				{1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
    57  			},
    58  		}
    59  		oldcustomg = customg
    60  	)
    61  	oldcustomg.Config = &params.ChainConfig{HomesteadBlock: big.NewInt(2)}
    62  	tests := []struct {
    63  		name       string
    64  		fn         func(ethdb.Database) (*params.ChainConfig, common.Hash, error)
    65  		wantConfig *params.ChainConfig
    66  		wantHash   common.Hash
    67  		wantErr    error
    68  	}{
    69  		{
    70  			name: "genesis without ChainConfig",
    71  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    72  				return SetupGenesisBlock(db, new(Genesis),ShardId)
    73  			},
    74  			wantErr:    errGenesisNoConfig,
    75  			wantConfig: params.AllEthashProtocolChanges,
    76  		},
    77  		{
    78  			name: "no block in DB, genesis == nil",
    79  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    80  				return SetupGenesisBlock(db, nil,ShardId)
    81  			},
    82  			wantHash:   params.MainnetGenesisHash,
    83  			wantConfig: params.MainnetChainConfig,
    84  		},
    85  		{
    86  			name: "mainnet block in DB, genesis == nil",
    87  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    88  				DefaultGenesisBlock().MustCommit(db,ShardId)
    89  				return SetupGenesisBlock(db, nil,ShardId)
    90  			},
    91  			wantHash:   params.MainnetGenesisHash,
    92  			wantConfig: params.MainnetChainConfig,
    93  		},
    94  		{
    95  			name: "custom block in DB, genesis == nil",
    96  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    97  				customg.MustCommit(db,ShardId)
    98  				return SetupGenesisBlock(db, nil,ShardId)
    99  			},
   100  			wantHash:   customghash,
   101  			wantConfig: customg.Config,
   102  		},
   103  		{
   104  			name: "custom block in DB, genesis == testnet",
   105  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   106  				customg.MustCommit(db,ShardId)
   107  				return SetupGenesisBlock(db, DefaultTestnetGenesisBlock(),ShardId)
   108  			},
   109  			wantErr:    &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash},
   110  			wantHash:   params.TestnetGenesisHash,
   111  			wantConfig: params.TestnetChainConfig,
   112  		},
   113  		{
   114  			name: "compatible config in DB",
   115  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   116  				oldcustomg.MustCommit(db,ShardId)
   117  				return SetupGenesisBlock(db, &customg,ShardId)
   118  			},
   119  			wantHash:   customghash,
   120  			wantConfig: customg.Config,
   121  		},
   122  		{
   123  			name: "incompatible config in DB",
   124  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   125  				// Commit the 'old' genesis block with Homestead transition at #2.
   126  				// Advance to block #4, past the homestead transition block of customg.
   127  				genesis := oldcustomg.MustCommit(db,ShardId)
   128  
   129  				bc, _ := NewBlockChain(db, nil, oldcustomg.Config, ethash.NewFullFaker(), vm.Config{}, nil,ShardId)
   130  				defer bc.Stop()
   131  
   132  				blocks, _ := GenerateChain(oldcustomg.Config, genesis, ethash.NewFaker(), db, 4, nil)
   133  				bc.InsertChain(blocks)
   134  				bc.CurrentBlock()
   135  				// This should return a compatibility error.
   136  				return SetupGenesisBlock(db, &customg,ShardId)
   137  			},
   138  			wantHash:   customghash,
   139  			wantConfig: customg.Config,
   140  			wantErr: &params.ConfigCompatError{
   141  				What:         "Homestead fork block",
   142  				StoredConfig: big.NewInt(2),
   143  				NewConfig:    big.NewInt(3),
   144  				RewindTo:     1,
   145  			},
   146  		},
   147  	}
   148  
   149  	for _, test := range tests {
   150  		db := ethdb.NewMemDatabase()
   151  		config, hash, err := test.fn(db)
   152  		// Check the return values.
   153  		if !reflect.DeepEqual(err, test.wantErr) {
   154  			spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true}
   155  			t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
   156  		}
   157  		if !reflect.DeepEqual(config, test.wantConfig) {
   158  			t.Errorf("%s:\nreturned %v\nwant     %v", test.name, config, test.wantConfig)
   159  		}
   160  		if hash != test.wantHash {
   161  			t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex())
   162  		} else if err == nil {
   163  			// Check database content.
   164  			stored := rawdb.ReadBlock(db, test.wantHash, 0)
   165  			if stored.Hash() != test.wantHash {
   166  				t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
   167  			}
   168  		}
   169  	}
   170  }