这篇文章上次修改于 499 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

1、准备创世区块文件

新建文件genesis.json 写入一下内容


{ "nonce": "0x0000000000000042", "config": { "chainId": 101, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 }, "alloc": { }, "timestamp": "0x0", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x00", "gasLimit": "0xffffffff", "difficulty": "0x10", //挖矿难度 "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x3333333333333333333333333333333333333333" }

2、初始化,写入创世快

将genesis.json放到下面创建的目录/data/schain/chain 下面


mkdir -p /data/schain/chain //新建存放区块链数据目录 cd /data/schain/ geth --datadir /data/schain/chain init genesis.json

上面的命令的主体是geth init,表示初始化区块链,命令可以带有选项和参数,其中--datadir选项后面跟一个目录名,这里为/data/schain/chain,表示指定数据存放目录为/data/schain/chain,genesis.json是init命令的参数。

 

运行上面的命令,会读取genesis.json文件,根据其中的内容,将创世区块写入到区块链中。如果看到以下的输出内容,说明初始化成功了。


INFO [04-17|10:14:54] Maximum peer count ETH=25 LES=0 total=25 INFO [04-17|10:14:54] Allocated cache and file handles database=/data/schain/chain/geth/chaindata cache=16 handles=16 INFO [04-17|10:14:55] Writing custom genesis block INFO [04-17|10:14:55] Persisted trie from memory database nodes=0 size=0.00B time=14.338µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [04-17|10:14:55] Successfully wrote genesis state database=chaindata hash=09452d…319a20 INFO [04-17|10:14:55] Allocated cache and file handles database=/data/schain/chain/geth/lightchaindata cache=16 handles=16 INFO [04-17|10:14:55] Writing custom genesis block INFO [04-17|10:14:55] Persisted trie from memory database nodes=0 size=0.00B time=3.659µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [04-17|10:14:55] Successfully wrote genesis state database=lightchaindata

3、启动私链


geth --identity "900chain" --rpc --rpccorsdomain "*" --datadir "/data/schain/chain" --port "30303" --rpcapi "db,eth,net,web3,txpool,miner,personal" --networkid 101 --nodiscover console

 

启动成功 进入控制台


Welcome to the Geth JavaScript console! instance: Geth/900chain/v1.8.3-stable-329ac18e/linux-amd64/go1.10 INFO [04-17|10:18:11] Etherbase automatically configured address=0x316FefF2905e224f801eE2770c3672b34a36c553 coinbase: 0x316feff2905e224f801ee2770c3672b34a36c553 at block: 10 (Mon, 16 Apr 2018 13:20:49 CST) datadir: /data/schain/chain modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0 >