github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/geth/genesis_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  	"os"
    30  	"path/filepath"
    31  	"testing"
    32  )
    33  
    34  var customGenesisTests = []struct {
    35  	genesis string
    36  	query   string
    37  	result  string
    38  }{
    39  //纯Genesis文件,无任何额外内容
    40  	{
    41  		genesis: `{
    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  		}`,
    52  		query:  "eth.getBlock(0).nonce",
    53  		result: "0x0000000000000042",
    54  	},
    55  //具有空链配置的Genesis文件(确保丢失的字段工作)
    56  	{
    57  		genesis: `{
    58  			"alloc"      : {},
    59  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    60  			"difficulty" : "0x20000",
    61  			"extraData"  : "",
    62  			"gasLimit"   : "0x2fefd8",
    63  			"nonce"      : "0x0000000000000042",
    64  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    65  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    66  			"timestamp"  : "0x00",
    67  			"config"     : {}
    68  		}`,
    69  		query:  "eth.getBlock(0).nonce",
    70  		result: "0x0000000000000042",
    71  	},
    72  //具有特定链配置的Genesis文件
    73  	{
    74  		genesis: `{
    75  			"alloc"      : {},
    76  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    77  			"difficulty" : "0x20000",
    78  			"extraData"  : "",
    79  			"gasLimit"   : "0x2fefd8",
    80  			"nonce"      : "0x0000000000000042",
    81  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    82  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    83  			"timestamp"  : "0x00",
    84  			"config"     : {
    85  				"homesteadBlock" : 314,
    86  				"daoForkBlock"   : 141,
    87  				"daoForkSupport" : true
    88  			}
    89  		}`,
    90  		query:  "eth.getBlock(0).nonce",
    91  		result: "0x0000000000000042",
    92  	},
    93  }
    94  
    95  //使用自定义Genesis块和链定义初始化geth的测试
    96  //工作正常。
    97  func TestCustomGenesis(t *testing.T) {
    98  	for i, tt := range customGenesisTests {
    99  //创建临时数据目录以供以后使用和检查
   100  		datadir := tmpdir(t)
   101  		defer os.RemoveAll(datadir)
   102  
   103  //使用自定义Genesis块初始化数据目录
   104  		json := filepath.Join(datadir, "genesis.json")
   105  		if err := ioutil.WriteFile(json, []byte(tt.genesis), 0600); err != nil {
   106  			t.Fatalf("test %d: failed to write genesis file: %v", i, err)
   107  		}
   108  		runGeth(t, "--datadir", datadir, "init", json).WaitExit()
   109  
   110  //查询自定义Genesis块
   111  		geth := runGeth(t,
   112  			"--datadir", datadir, "--maxpeers", "0", "--port", "0",
   113  			"--nodiscover", "--nat", "none", "--ipcdisable",
   114  			"--exec", tt.query, "console")
   115  		geth.ExpectRegexp(tt.result)
   116  		geth.ExpectExit()
   117  	}
   118  }