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