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