github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/genesis_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:35</date>
    10  //</624450078928539648>
    11  
    12  
    13  package core
    14  
    15  import (
    16  	"math/big"
    17  	"reflect"
    18  	"testing"
    19  
    20  	"github.com/davecgh/go-spew/spew"
    21  	"github.com/ethereum/go-ethereum/common"
    22  	"github.com/ethereum/go-ethereum/consensus/ethash"
    23  	"github.com/ethereum/go-ethereum/core/rawdb"
    24  	"github.com/ethereum/go-ethereum/core/vm"
    25  	"github.com/ethereum/go-ethereum/ethdb"
    26  	"github.com/ethereum/go-ethereum/params"
    27  )
    28  
    29  func TestDefaultGenesisBlock(t *testing.T) {
    30  	block := DefaultGenesisBlock().ToBlock(nil)
    31  	if block.Hash() != params.MainnetGenesisHash {
    32  		t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash)
    33  	}
    34  	block = DefaultTestnetGenesisBlock().ToBlock(nil)
    35  	if block.Hash() != params.TestnetGenesisHash {
    36  		t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash(), params.TestnetGenesisHash)
    37  	}
    38  }
    39  
    40  func TestSetupGenesis(t *testing.T) {
    41  	var (
    42  		customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50")
    43  		customg     = Genesis{
    44  			Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3)},
    45  			Alloc: GenesisAlloc{
    46  				{1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
    47  			},
    48  		}
    49  		oldcustomg = customg
    50  	)
    51  	oldcustomg.Config = &params.ChainConfig{HomesteadBlock: big.NewInt(2)}
    52  	tests := []struct {
    53  		name       string
    54  		fn         func(ethdb.Database) (*params.ChainConfig, common.Hash, error)
    55  		wantConfig *params.ChainConfig
    56  		wantHash   common.Hash
    57  		wantErr    error
    58  	}{
    59  		{
    60  			name: "genesis without ChainConfig",
    61  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    62  				return SetupGenesisBlock(db, new(Genesis))
    63  			},
    64  			wantErr:    errGenesisNoConfig,
    65  			wantConfig: params.AllEthashProtocolChanges,
    66  		},
    67  		{
    68  			name: "no block in DB, genesis == nil",
    69  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    70  				return SetupGenesisBlock(db, nil)
    71  			},
    72  			wantHash:   params.MainnetGenesisHash,
    73  			wantConfig: params.MainnetChainConfig,
    74  		},
    75  		{
    76  			name: "mainnet block in DB, genesis == nil",
    77  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    78  				DefaultGenesisBlock().MustCommit(db)
    79  				return SetupGenesisBlock(db, nil)
    80  			},
    81  			wantHash:   params.MainnetGenesisHash,
    82  			wantConfig: params.MainnetChainConfig,
    83  		},
    84  		{
    85  			name: "custom block in DB, genesis == nil",
    86  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    87  				customg.MustCommit(db)
    88  				return SetupGenesisBlock(db, nil)
    89  			},
    90  			wantHash:   customghash,
    91  			wantConfig: customg.Config,
    92  		},
    93  		{
    94  			name: "custom block in DB, genesis == testnet",
    95  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    96  				customg.MustCommit(db)
    97  				return SetupGenesisBlock(db, DefaultTestnetGenesisBlock())
    98  			},
    99  			wantErr:    &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash},
   100  			wantHash:   params.TestnetGenesisHash,
   101  			wantConfig: params.TestnetChainConfig,
   102  		},
   103  		{
   104  			name: "compatible config in DB",
   105  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   106  				oldcustomg.MustCommit(db)
   107  				return SetupGenesisBlock(db, &customg)
   108  			},
   109  			wantHash:   customghash,
   110  			wantConfig: customg.Config,
   111  		},
   112  		{
   113  			name: "incompatible config in DB",
   114  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   115  //在2进行“古老”的创世地块和宅基地过渡。
   116  //前进到4号街区,经过海关的宅基地过渡街区。
   117  				genesis := oldcustomg.MustCommit(db)
   118  
   119  				bc, _ := NewBlockChain(db, nil, oldcustomg.Config, ethash.NewFullFaker(), vm.Config{}, nil)
   120  				defer bc.Stop()
   121  
   122  				blocks, _ := GenerateChain(oldcustomg.Config, genesis, ethash.NewFaker(), db, 4, nil)
   123  				bc.InsertChain(blocks)
   124  				bc.CurrentBlock()
   125  //这将返回一个兼容性错误。
   126  				return SetupGenesisBlock(db, &customg)
   127  			},
   128  			wantHash:   customghash,
   129  			wantConfig: customg.Config,
   130  			wantErr: &params.ConfigCompatError{
   131  				What:         "Homestead fork block",
   132  				StoredConfig: big.NewInt(2),
   133  				NewConfig:    big.NewInt(3),
   134  				RewindTo:     1,
   135  			},
   136  		},
   137  	}
   138  
   139  	for _, test := range tests {
   140  		db := ethdb.NewMemDatabase()
   141  		config, hash, err := test.fn(db)
   142  //检查返回值。
   143  		if !reflect.DeepEqual(err, test.wantErr) {
   144  			spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true}
   145  			t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
   146  		}
   147  		if !reflect.DeepEqual(config, test.wantConfig) {
   148  			t.Errorf("%s:\nreturned %v\nwant     %v", test.name, config, test.wantConfig)
   149  		}
   150  		if hash != test.wantHash {
   151  			t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex())
   152  		} else if err == nil {
   153  //检查数据库内容。
   154  			stored := rawdb.ReadBlock(db, test.wantHash, 0)
   155  			if stored.Hash() != test.wantHash {
   156  				t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
   157  			}
   158  		}
   159  	}
   160  }
   161