github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/cmd/geth/dao_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 12:09:28</date>
    10  //</624342591361519616>
    11  
    12  
    13  package main
    14  
    15  import (
    16  	"io/ioutil"
    17  	"math/big"
    18  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/core/rawdb"
    24  	"github.com/ethereum/go-ethereum/ethdb"
    25  	"github.com/ethereum/go-ethereum/params"
    26  )
    27  
    28  //不关心刀叉的节点的Genesis块(即未配置)
    29  var daoOldGenesis = `{
    30  	"alloc"      : {},
    31  	"coinbase"   : "0x0000000000000000000000000000000000000000",
    32  	"difficulty" : "0x20000",
    33  	"extraData"  : "",
    34  	"gasLimit"   : "0x2fefd8",
    35  	"nonce"      : "0x0000000000000042",
    36  	"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    37  	"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    38  	"timestamp"  : "0x00",
    39  	"config"     : {}
    40  }`
    41  
    42  //
    43  var daoNoForkGenesis = `{
    44  	"alloc"      : {},
    45  	"coinbase"   : "0x0000000000000000000000000000000000000000",
    46  	"difficulty" : "0x20000",
    47  	"extraData"  : "",
    48  	"gasLimit"   : "0x2fefd8",
    49  	"nonce"      : "0x0000000000000042",
    50  	"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    51  	"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    52  	"timestamp"  : "0x00",
    53  	"config"     : {
    54  		"daoForkBlock"   : 314,
    55  		"daoForkSupport" : false
    56  	}
    57  }`
    58  
    59  //主动支持道叉节点的Genesis块
    60  var daoProForkGenesis = `{
    61  	"alloc"      : {},
    62  	"coinbase"   : "0x0000000000000000000000000000000000000000",
    63  	"difficulty" : "0x20000",
    64  	"extraData"  : "",
    65  	"gasLimit"   : "0x2fefd8",
    66  	"nonce"      : "0x0000000000000042",
    67  	"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    68  	"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    69  	"timestamp"  : "0x00",
    70  	"config"     : {
    71  		"daoForkBlock"   : 314,
    72  		"daoForkSupport" : true
    73  	}
    74  }`
    75  
    76  var daoGenesisHash = common.HexToHash("5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0")
    77  var daoGenesisForkBlock = big.NewInt(314)
    78  
    79  //testDaoForkBlockNewChain测试DAO硬分叉号和节点支持/反对是否正确
    80  //在各种初始化过程和调用之后在数据库中设置。
    81  func TestDAOForkBlockNewChain(t *testing.T) {
    82  	for i, arg := range []struct {
    83  		genesis     string
    84  		expectBlock *big.Int
    85  		expectVote  bool
    86  	}{
    87  //测试DAO默认主网
    88  		{"", params.MainnetChainConfig.DAOForkBlock, true},
    89  //测试dao init旧privnet
    90  		{daoOldGenesis, nil, false},
    91  //测试DAO默认无fork privnet
    92  		{daoNoForkGenesis, daoGenesisForkBlock, false},
    93  //测试DAO默认pro-fork privnet
    94  		{daoProForkGenesis, daoGenesisForkBlock, true},
    95  	} {
    96  		testDAOForkBlockNewChain(t, i, arg.genesis, arg.expectBlock, arg.expectVote)
    97  	}
    98  }
    99  
   100  func testDAOForkBlockNewChain(t *testing.T, test int, genesis string, expectBlock *big.Int, expectVote bool) {
   101  //创建临时数据目录以供以后使用和检查
   102  	datadir := tmpdir(t)
   103  	defer os.RemoveAll(datadir)
   104  
   105  //启动一个设置了请求标志的geth实例并立即终止
   106  	if genesis != "" {
   107  		json := filepath.Join(datadir, "genesis.json")
   108  		if err := ioutil.WriteFile(json, []byte(genesis), 0600); err != nil {
   109  			t.Fatalf("test %d: failed to write genesis file: %v", test, err)
   110  		}
   111  		runGeth(t, "--datadir", datadir, "init", json).WaitExit()
   112  	} else {
   113  //强制链初始化
   114  		args := []string{"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", "--ipcdisable", "--datadir", datadir}
   115  		geth := runGeth(t, append(args, []string{"--exec", "2+2", "console"}...)...)
   116  		geth.WaitExit()
   117  	}
   118  //从数据库中检索DAO配置标志
   119  	path := filepath.Join(datadir, "geth", "chaindata")
   120  	db, err := ethdb.NewLDBDatabase(path, 0, 0)
   121  	if err != nil {
   122  		t.Fatalf("test %d: failed to open test database: %v", test, err)
   123  	}
   124  	defer db.Close()
   125  
   126  	genesisHash := common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
   127  	if genesis != "" {
   128  		genesisHash = daoGenesisHash
   129  	}
   130  	config := rawdb.ReadChainConfig(db, genesisHash)
   131  	if config == nil {
   132  		t.Errorf("test %d: failed to retrieve chain config: %v", test, err)
   133  return //我们想回到这里,其他的支票不能超过这一点(没有恐慌)。
   134  	}
   135  //根据预期值验证DAO硬分叉块号
   136  	if config.DAOForkBlock == nil {
   137  		if expectBlock != nil {
   138  			t.Errorf("test %d: dao hard-fork block mismatch: have nil, want %v", test, expectBlock)
   139  		}
   140  	} else if expectBlock == nil {
   141  		t.Errorf("test %d: dao hard-fork block mismatch: have %v, want nil", test, config.DAOForkBlock)
   142  	} else if config.DAOForkBlock.Cmp(expectBlock) != 0 {
   143  		t.Errorf("test %d: dao hard-fork block mismatch: have %v, want %v", test, config.DAOForkBlock, expectBlock)
   144  	}
   145  	if config.DAOForkSupport != expectVote {
   146  		t.Errorf("test %d: dao hard-fork support mismatch: have %v, want %v", test, config.DAOForkSupport, expectVote)
   147  	}
   148  }
   149