github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/geth/dao_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2016 Go Ethereum作者
    10  //此文件是Go以太坊的一部分。
    11  //
    12  //Go以太坊是免费软件:您可以重新发布和/或修改它
    13  //根据GNU通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊的分布希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU通用公共许可证了解更多详细信息。
    21  //
    22  //你应该已经收到一份GNU通用公共许可证的副本
    23  //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package main
    26  
    27  import (
    28  	"io/ioutil"
    29  	"math/big"
    30  	"os"
    31  	"path/filepath"
    32  	"testing"
    33  
    34  	"github.com/ethereum/go-ethereum/common"
    35  	"github.com/ethereum/go-ethereum/core/rawdb"
    36  	"github.com/ethereum/go-ethereum/ethdb"
    37  	"github.com/ethereum/go-ethereum/params"
    38  )
    39  
    40  //不关心刀叉的节点的Genesis块(即未配置)
    41  var daoOldGenesis = `{
    42  	"alloc"      : {},
    43  	"coinbase"   : "0x0000000000000000000000000000000000000000",
    44  	"difficulty" : "0x20000",
    45  	"extraData"  : "",
    46  	"gasLimit"   : "0x2fefd8",
    47  	"nonce"      : "0x0000000000000042",
    48  	"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    49  	"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    50  	"timestamp"  : "0x00",
    51  	"config"     : {}
    52  }`
    53  
    54  //
    55  var daoNoForkGenesis = `{
    56  	"alloc"      : {},
    57  	"coinbase"   : "0x0000000000000000000000000000000000000000",
    58  	"difficulty" : "0x20000",
    59  	"extraData"  : "",
    60  	"gasLimit"   : "0x2fefd8",
    61  	"nonce"      : "0x0000000000000042",
    62  	"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    63  	"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    64  	"timestamp"  : "0x00",
    65  	"config"     : {
    66  		"daoForkBlock"   : 314,
    67  		"daoForkSupport" : false
    68  	}
    69  }`
    70  
    71  //主动支持道叉节点的Genesis块
    72  var daoProForkGenesis = `{
    73  	"alloc"      : {},
    74  	"coinbase"   : "0x0000000000000000000000000000000000000000",
    75  	"difficulty" : "0x20000",
    76  	"extraData"  : "",
    77  	"gasLimit"   : "0x2fefd8",
    78  	"nonce"      : "0x0000000000000042",
    79  	"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    80  	"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    81  	"timestamp"  : "0x00",
    82  	"config"     : {
    83  		"daoForkBlock"   : 314,
    84  		"daoForkSupport" : true
    85  	}
    86  }`
    87  
    88  var daoGenesisHash = common.HexToHash("5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0")
    89  var daoGenesisForkBlock = big.NewInt(314)
    90  
    91  //testDaoForkBlockNewChain测试DAO硬分叉号和节点支持/反对是否正确
    92  //在各种初始化过程和调用之后在数据库中设置。
    93  func TestDAOForkBlockNewChain(t *testing.T) {
    94  	for i, arg := range []struct {
    95  		genesis     string
    96  		expectBlock *big.Int
    97  		expectVote  bool
    98  	}{
    99  //测试DAO默认主网
   100  		{"", params.MainnetChainConfig.DAOForkBlock, true},
   101  //测试dao init旧privnet
   102  		{daoOldGenesis, nil, false},
   103  //测试DAO默认无fork privnet
   104  		{daoNoForkGenesis, daoGenesisForkBlock, false},
   105  //测试DAO默认pro-fork privnet
   106  		{daoProForkGenesis, daoGenesisForkBlock, true},
   107  	} {
   108  		testDAOForkBlockNewChain(t, i, arg.genesis, arg.expectBlock, arg.expectVote)
   109  	}
   110  }
   111  
   112  func testDAOForkBlockNewChain(t *testing.T, test int, genesis string, expectBlock *big.Int, expectVote bool) {
   113  //创建临时数据目录以供以后使用和检查
   114  	datadir := tmpdir(t)
   115  	defer os.RemoveAll(datadir)
   116  
   117  //启动一个设置了请求标志的geth实例并立即终止
   118  	if genesis != "" {
   119  		json := filepath.Join(datadir, "genesis.json")
   120  		if err := ioutil.WriteFile(json, []byte(genesis), 0600); err != nil {
   121  			t.Fatalf("test %d: failed to write genesis file: %v", test, err)
   122  		}
   123  		runGeth(t, "--datadir", datadir, "init", json).WaitExit()
   124  	} else {
   125  //强制链初始化
   126  		args := []string{"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", "--ipcdisable", "--datadir", datadir}
   127  		geth := runGeth(t, append(args, []string{"--exec", "2+2", "console"}...)...)
   128  		geth.WaitExit()
   129  	}
   130  //从数据库中检索DAO配置标志
   131  	path := filepath.Join(datadir, "geth", "chaindata")
   132  	db, err := ethdb.NewLDBDatabase(path, 0, 0)
   133  	if err != nil {
   134  		t.Fatalf("test %d: failed to open test database: %v", test, err)
   135  	}
   136  	defer db.Close()
   137  
   138  	genesisHash := common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
   139  	if genesis != "" {
   140  		genesisHash = daoGenesisHash
   141  	}
   142  	config := rawdb.ReadChainConfig(db, genesisHash)
   143  	if config == nil {
   144  		t.Errorf("test %d: failed to retrieve chain config: %v", test, err)
   145  return //我们想回到这里,其他的支票不能超过这一点(没有恐慌)。
   146  	}
   147  //根据预期值验证DAO硬分叉块号
   148  	if config.DAOForkBlock == nil {
   149  		if expectBlock != nil {
   150  			t.Errorf("test %d: dao hard-fork block mismatch: have nil, want %v", test, expectBlock)
   151  		}
   152  	} else if expectBlock == nil {
   153  		t.Errorf("test %d: dao hard-fork block mismatch: have %v, want nil", test, config.DAOForkBlock)
   154  	} else if config.DAOForkBlock.Cmp(expectBlock) != 0 {
   155  		t.Errorf("test %d: dao hard-fork block mismatch: have %v, want %v", test, config.DAOForkBlock, expectBlock)
   156  	}
   157  	if config.DAOForkSupport != expectVote {
   158  		t.Errorf("test %d: dao hard-fork support mismatch: have %v, want %v", test, config.DAOForkSupport, expectVote)
   159  	}
   160  }