github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/core/genesis_test.go (about)

     1  package core
     2  
     3  import (
     4  	"math/big"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/davecgh/go-spew/spew"
     9  	"github.com/neatlab/neatio/chain/core/rawdb"
    10  	"github.com/neatlab/neatio/neatdb"
    11  	"github.com/neatlab/neatio/params"
    12  	"github.com/neatlab/neatio/utilities/common"
    13  )
    14  
    15  func TestDefaultGenesisBlock(t *testing.T) {
    16  	block := DefaultGenesisBlock().ToBlock(nil)
    17  	if block.Hash() != params.MainnetGenesisHash {
    18  		t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash)
    19  	}
    20  	block = DefaultTestnetGenesisBlock().ToBlock(nil)
    21  	if block.Hash() != params.TestnetGenesisHash {
    22  		t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash(), params.TestnetGenesisHash)
    23  	}
    24  }
    25  
    26  func TestSetupGenesis(t *testing.T) {
    27  	var (
    28  		customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50")
    29  		customg     = Genesis{
    30  			Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3)},
    31  			Alloc: GenesisAlloc{
    32  				{1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
    33  			},
    34  		}
    35  		oldcustomg = customg
    36  	)
    37  	oldcustomg.Config = &params.ChainConfig{HomesteadBlock: big.NewInt(2)}
    38  	tests := []struct {
    39  		name       string
    40  		fn         func(neatdb.Database) (*params.ChainConfig, common.Hash, error)
    41  		wantConfig *params.ChainConfig
    42  		wantHash   common.Hash
    43  		wantErr    error
    44  	}{
    45  		{
    46  			name: "genesis without ChainConfig",
    47  			fn: func(db neatdb.Database) (*params.ChainConfig, common.Hash, error) {
    48  				return SetupGenesisBlock(db, new(Genesis))
    49  			},
    50  			wantErr:    errGenesisNoConfig,
    51  			wantConfig: params.MainnetChainConfig,
    52  		},
    53  		{
    54  			name: "no block in DB, genesis == nil",
    55  			fn: func(db neatdb.Database) (*params.ChainConfig, common.Hash, error) {
    56  				return SetupGenesisBlock(db, nil)
    57  			},
    58  			wantHash:   params.MainnetGenesisHash,
    59  			wantConfig: params.MainnetChainConfig,
    60  		},
    61  		{
    62  			name: "mainnet block in DB, genesis == nil",
    63  			fn: func(db neatdb.Database) (*params.ChainConfig, common.Hash, error) {
    64  				DefaultGenesisBlock().MustCommit(db)
    65  				return SetupGenesisBlock(db, nil)
    66  			},
    67  			wantHash:   params.MainnetGenesisHash,
    68  			wantConfig: params.MainnetChainConfig,
    69  		},
    70  		{
    71  			name: "custom block in DB, genesis == nil",
    72  			fn: func(db neatdb.Database) (*params.ChainConfig, common.Hash, error) {
    73  				customg.MustCommit(db)
    74  				return SetupGenesisBlock(db, nil)
    75  			},
    76  			wantHash:   customghash,
    77  			wantConfig: customg.Config,
    78  		},
    79  		{
    80  			name: "custom block in DB, genesis == testnet",
    81  			fn: func(db neatdb.Database) (*params.ChainConfig, common.Hash, error) {
    82  				customg.MustCommit(db)
    83  				return SetupGenesisBlock(db, DefaultTestnetGenesisBlock())
    84  			},
    85  			wantErr:    &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash},
    86  			wantHash:   params.TestnetGenesisHash,
    87  			wantConfig: params.TestnetChainConfig,
    88  		},
    89  		{
    90  			name: "compatible config in DB",
    91  			fn: func(db neatdb.Database) (*params.ChainConfig, common.Hash, error) {
    92  				oldcustomg.MustCommit(db)
    93  				return SetupGenesisBlock(db, &customg)
    94  			},
    95  			wantHash:   customghash,
    96  			wantConfig: customg.Config,
    97  		},
    98  		{
    99  			name: "incompatible config in DB",
   100  
   101  			wantHash:   customghash,
   102  			wantConfig: customg.Config,
   103  			wantErr: &params.ConfigCompatError{
   104  				What:         "Homestead fork block",
   105  				StoredConfig: big.NewInt(2),
   106  				NewConfig:    big.NewInt(3),
   107  				RewindTo:     1,
   108  			},
   109  		},
   110  	}
   111  
   112  	for _, test := range tests {
   113  		db := rawdb.NewMemoryDatabase()
   114  		config, hash, err := test.fn(db)
   115  
   116  		if !reflect.DeepEqual(err, test.wantErr) {
   117  			spew := spew.ConfigState{}
   118  			t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
   119  		}
   120  		if !reflect.DeepEqual(config, test.wantConfig) {
   121  			t.Errorf("%s:\nreturned %v\nwant     %v", test.name, config, test.wantConfig)
   122  		}
   123  		if hash != test.wantHash {
   124  			t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex())
   125  		} else if err == nil {
   126  
   127  			stored := rawdb.ReadBlock(db, test.wantHash, 0)
   128  			if stored.Hash() != test.wantHash {
   129  				t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
   130  			}
   131  		}
   132  	}
   133  }