1、geth是以太坊客户端,用go语言编写。这篇主要使用geth来搭建自己的私有链,并完成挖矿和转账功能。
2、安装geth
打开终端,执行命令
brew tap ethereum/ethereum
brew install ethereum
geth version
![](https://img.haomeiwen.com/i1745735/4e97fc895953f255.png)
![](https://img.haomeiwen.com/i1745735/bdaed20e6a41fb07.png)
因为已经安装过了。
![](https://img.haomeiwen.com/i1745735/3eeb8206b18282a8.png)
3、创建创世区块
cd 文件目录,配置创世区块信息
vim genesis.json
{
"coinbase" : "0x0000000000000000000000000000000000000001",
"difficulty" : "0x80000",
"extraData" : "",
"gasLimit" : "0x8000000",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc": {},
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
}
}
初始化创世区块
geth --datadir "./" init genesis.json
查看区块log
geth --datadir "./" --nodiscover console 2>>geth.log
![](https://img.haomeiwen.com/i1745735/0ab4a43160a7d92a.png)
4、挖矿
设置挖矿地址
miner.setEtherbase(eth.accounts[0])
开始挖矿
miner.start()
查询余额
eth.getBalance('0xB97168a67AB66E55B98B1439222Ee665E657fFc0')
查看节点信息
admin.nodeInfo
查看节点高度
eth.blockNumber
停止挖矿
miner.stop()
![](https://img.haomeiwen.com/i1745735/f85cc3235e2f51c1.png)
5、转账
解锁
personal.unlockAccount("0xa9436991e002986f58d948d79e737df190c4f26b")
转账
eth.sendTransaction({from:eth.coinbase, to:eth.accounts[1], value: web3.toWei(0.05, "ether")})
查询余额
eth.getBalance('0xB97168a67AB66E55B98B1439222Ee665E657fFc0')
![](https://img.haomeiwen.com/i1745735/96067a25d04d6f9b.png)
注意,在执行转账操作时,保持挖矿状态,可以快速完成转账,如果停止挖矿,需要等待再次开始挖矿后,转账操作才可以完成。
网友评论