教程:在以太坊上创建私链private chain
收藏
分享
微信扫一扫
分享到朋友或朋友圈
两种以太坊客户端可以选择:
Eth - C++ implementation
Geth - Go implementation
本教程是基于Geth. 安装教程可以到 frontier gitbook.
创建创世块
创世块的最大特点是第一个块,所以没有与之前的块产生联系。在比特币系统里,这个创世块是被写入源码,但对于以太坊而言,创世块可以是任何你喜欢的东西。你也可以把这个当成是系统的一个漏洞。但是共识算法确保其它人除非纳入你的创世块,否则是不会有效的。(以后再详细说)
Great, so how do we make one of these genesis blocks? Well its fairly simple the following JSON is all you really need:
{
"nonce": "0xdeadbeefdeadbeef",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x0",
"gasLimit": "0x8000000",
"difficulty": "0x400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {
}
}
Kudos to obscuren
在本地硬盘上存储这个 JSON,然后运行以下命令:
$ geth --genesis <genesis json file path> --datadir <some path to an empty folder> --networkid 123 --nodiscover --maxpeers 0 console
这个命令做了如下几个事情:
调用创世块JSON进区块链里。
使用 datadir 来存储相关数据,并且维护新创建的区块及其它数据 (declared to prevent you clobbering your main net data, wouldn’t want to overwrite all those blocks you spent time downloading!)
使用网络ID ‘1’ 确保我们无法与主网的节点进行通讯。 – “connections between nodes are valid only if peers have identical protocol version and network id”
禁用同伴发现。
把maxpeers 设置为 0禁用网络。
以控制台模式启动 geth客户端,这样你就可以与你的新区块链接/节点进行通讯。
接下来就可以根据 testing contracts and transactions 的指示创建区块和帐户了。我这里的区别是禁用了PRC, 表现证明的公认搜集过程( the proof performance metric gathering processes) ,extra verbosity and vmdebu,目的是简单化。
在上述创世块里,我把难度设置的很低,这样的话我的本地电脑就能够挖矿,很容易获得以太币。
种子帐户 accounts with allocation
接下来,就有必要创建新的种子帐户了(以便存储挖矿所得的以太币)。 很简单,先创建一个新区块链和新帐户:
$ geth --genesis <genesis json file path> --datadir /.../dapps/test-genesis/.ethereum --networkid 123 --nodiscover --maxpeers 0 console
I0829 13:30:07.340636 3987 database.go:74] Alloted 16MB cache to /.../dapps/test-genesis/.ethereum/blockchain
I0829 13:30:07.342982 3987 database.go:74] Alloted 16MB cache to /.../dapps/test-genesis/.ethereum/state
I0829 13:30:07.345055 3987 database.go:74] Alloted 16MB cache to /.../dapps/test-genesis/.ethereum/extra
I0829 13:30:07.347363 3987 backend.go:291] Protocol Versions: [61 60], Network Id: 12345
I0829 13:30:07.347738 3987 backend.go:303] Successfully wrote genesis block. New genesis hash = 82b6159155c00fb0b420046012a02257a176ad5dcfce4be4a15da39c166518e2
I0829 13:30:07.347771 3987 backend.go:328] Blockchain DB Version: 3
I0829 13:30:07.347866 3987 chain_manager.go:241] Last block (#0) 82b6159155c00fb0b420046012a02257a176ad5dcfce4be4a15da39c166518e2 TD=1024
I0829 13:30:07.353373 3987 cmd.go:124] Starting Geth/v1.0.1/darwin/go1.4.2
I0829 13:30:07.353470 3987 server.go:312] Starting Server
I0829 13:30:07.353610 3987 backend.go:564] Server started
I0829 13:30:07.353548 3987 server.go:549] Listening on [::]:30310
I0829 13:30:07.353961 3987 ipc_unix.go:78] IPC service started (/.../dapps/test-genesis/.ethereum/geth.ipc)
instance: Geth/v1.0.1/darwin/go1.4.2
datadir: /.../dapps/test-genesis/.ethereum
coinbase: 0x1fb891f92eb557f4d688463d0d7c560552263b5a
at block: 0 (1970-01-01 01:00:00)
modules: admin:1.0 db:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 shh:1.0 txpool:1.0 web3:1.0
> personal.newAccount("mypassword");
'0x1fb891f92eb557f4d688463d0d7c560552263b5a'
上面代码的最后一行创建了一个新的帐户,其地址是 0x1fb891f92eb557f4d688463d0d7c560552263b5a.
创建帐户后,用 <ctrl-c> 退出geth客户端,然后在你的datadir文件夹里,保留 keystore/ 这个文件夹,其余的全部删掉。
$ cd <your datadir>
$ rm -rf `ls | grep -v keystore`
接下来就是见证奇迹的时候,刷新你的创世块JSON,把下面的代码加入 alloc 键:
"alloc": {
"<your account address e.g. 0x1fb891f92eb557f4d688463d0d7c560552263b5a>": {
"balance": "10000000000000000000"
}
}
现在重新运行geth命令行,使用刚才刷新过的 genesis json文件,还有相同的datadir,然后你会发现帐户余额里,多了10个以太币出来。
$ geth --genesis <updated genesis json file path> --datadir /.../dapps/test-genesis/.ethereum --networkid 123 --nodiscover --maxpeers 0 console
I0829 13:30:07.340636 3987 database.go:74] Alloted 16MB cache to /.../dapps/test-genesis/.ethereum/blockchain
I0829 13:30:07.342982 3987 database.go:74] Alloted 16MB cache to /.../dapps/test-genesis/.ethereum/state
I0829 13:30:07.345055 3987 database.go:74] Alloted 16MB cache to /.../dapps/test-genesis/.ethereum/extra
I0829 13:30:07.347363 3987 backend.go:291] Protocol Versions: [61 60], Network Id: 12345
I0829 13:30:07.347738 3987 backend.go:303] Successfully wrote genesis block. New genesis hash = 82b6159155c00fb0b420046012a02257a176ad5dcfce4be4a15da39c166518e2
I0829 13:30:07.347771 3987 backend.go:328] Blockchain DB Version: 3
I0829 13:30:07.347866 3987 chain_manager.go:241] Last block (#0) 82b6159155c00fb0b420046012a02257a176ad5dcfce4be4a15da39c166518e2 TD=1024
I0829 13:30:07.353373 3987 cmd.go:124] Starting Geth/v1.0.1/darwin/go1.4.2
I0829 13:30:07.353470 3987 server.go:312] Starting Server
I0829 13:30:07.353610 3987 backend.go:564] Server started
I0829 13:30:07.353548 3987 server.go:549] Listening on [::]:30310
I0829 13:30:07.353961 3987 ipc_unix.go:78] IPC service started (/.../dapps/test-genesis/.ethereum/geth.ipc)
instance: Geth/v1.0.1/darwin/go1.4.2
datadir: /.../dapps/test-genesis/.ethereum
coinbase: 0x1fb891f92eb557f4d688463d0d7c560552263b5a
at block: 0 (1970-01-01 01:00:00)
modules: admin:1.0 db:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 shh:1.0 txpool:1.0 web3:1.0
> primary = eth.accounts[0];
'0x1fb891f92eb557f4d688463d0d7c560552263b5a'
> balance = web3.fromWei(eth.getBalance(primary), "ether");
'10'
免责声明:本文版权归原作者所有,不代表MyToken(www.mytokencap.com)观点和立场;如有关于内容、版权等问题,请与我们联系。
相关阅读
专访 Mable Jiang:复盘 Multicoin 投资方法论与 DeFi 洞察
原文标题:《对话 Mable:复盘 Multicoin Capital 的投资方法论与 DeFi 观察 | 链捕手》受访者:Mable Jiang,Multicoin Capital 执行董事撰文:王...
信标链、PoS、分片……接触以太坊 2.0 得先理解这些术语
原文标题:《以太坊 2.0 术语库》整理:ETH 中文网Attestation 证明证明是指验证者所发起的投票,由验证者的签名聚合而成,用以证明区块的有效性,投票通过验证者的余额进行加权。Attest...
手把手教你搭建 IPFS 私有网络
在联盟链的场景下,IPFS 作为去中心化存储的首选方案,本文将介绍如何使用 go-ipfs 搭建一个私有网络并进行简单使用。...