猿哥的环境是 Linux,实际操作时,需要替换路径、钱包地址等。
1. 到 https://geth.ethereum.org/downloads/ 下载编译好的 geth
2. 创建 genesis.json,内容如下:
{
"config": {
"chainId": 2206,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0
},
"alloc": {},
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x20000",
"extraData": "",
"gasLimit": "0xffffffff",
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00"
}
注意事项:
- 上述内容修改了https://github.com/ethereum/go-ethereum 中的的chainId和nonce, 不修改chainId的话,可能在交易时会出现错误“ Error: insufficient funds for gas * price + value ”。
- 猿哥原先的 gasLimit 是 0x2fefd8,但使用 truffle 发布合约到私有链的时候出现了“Error encountered, bailing. Network state unknown. Review successful transactions manually. Error: exceeds block gas limit” 这样的错误。后来修改genesis.json 中的 gasLimit 为 “0xffffffff” ,删除区块文件并重新开始挖矿,之后可以正常发布合约到私有链了。
3. 生成创世区块:
/data/eth/geth --datadir "/data/eth/database" init /data/eth/database/genesis.json
4. 创建账户,使用其它工具如 MyEtherWallet、MetaMask 创建新帐号,获得钱包地址和私钥。
5. 启动节点
nohup /data/eth/geth --gcmode archive --networkid 2006 --mine --minerthreads=1 --etherbase=你的挖矿收益钱包地址 --rpcapi admin,eth,web3,personal --verbosity 0 --rpc --rpcport 8545 --nodiscover --datadir "/data/eth/database" &
上述命令中 --nodiscover 表示不链接其它节点;--gcmode archive 表示即时将内存中的数据写入到文件中,否则重启节点可能会导致区块高度归零而丢失数据。
6. 通过私钥导导入帐号到本地
/data/eth/geth --datadir "/data/eth/database" account import ./key.txt
key.txt 的内容只有一行,就是钱包的私钥。
7. 使用 console 模式启动命令行工具
/data/eth/geth attach http://127.0.0.1:8545
之后就可以在命令行模式下使用命令了, 具体命令可以参考 https://github.com/ethereum/wiki/wiki/JavaScript-API