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