在以太坊上创建私链private chain
发布于 3 个月前 作者 stvenyin 347 次浏览 来自 以太坊
  • 老外讨论:
  • https://github.com/ethereum/cpp-ethereum/issues/4852
  • 有两种以太坊客户端可以选择:
  • 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’
3 回复

楼主可以把你的帖子里面代码部分用 markdown 代码段格式圈起来,这样方便别人阅读,比如

$ 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
回到顶部