搭建以太坊私有网络可以很方便的进行开发、调试和测试。
下面将使用geth工具来进行搭建和挖矿。
1. 创建存放私有链的目录及创世区块描述文件

创世区块描述文件CustomGenesis.json内容为:
{
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00",
"gasLimit": "0x80000000",
"difficulty": "0x1",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {},
"config": {}
}
2. 创建创世区块
执行命令:
geth --datadir "./" init CustomGenesis.json


其中:
- geth存放区块链相关数据
- keystore存放用户信息
3. 创建私链
执行命令:
geth --datadir "./" --nodiscover console

参数说明:
- --datadir:私有链的数据目录,这里就使用当前目录
- --nodiscover: 节点不被发现,即不会被其他人有意或无异连接上私链节点
4. 创建用户
执行命令:
personal.newAccount()
eth.accounts


5. 开始私链挖矿
执行命令:
miner.start(1)

6. 查看挖到的以太币
新开一个终端,执行命令attach进去:
geth attach ./geth.ipc
执行命令,查看以太币余额:
eth.getBalance(eth.accounts[0])

可以看到余额一直在增加,因为不停在挖矿,单位是wei,要显示单位为以太币,可执行命令:
web3.fromWei(eth.getBalance(eth.accounts[0]))

7. 关联Mist钱包
执行命令:
meteor --no-release-check
yarn dev:electron --rpc ../dev/geth.ipc




网友评论