ETH篇——MAC系统搭建基于以太坊的私链(含连接Mist钱包)
1. 安装Geth(Go-Ethereum)
Geth 又名Go Ethereum. 是以太坊协议的三种实现之一,由Go语言开发,完全开源的项目。Geth 可以被安装在很多操作系统上,包括Windows、Linux、Mac的OSX、Android或者IOS系统
前提
Geth依赖GO环境,所以安装之前先要进行GO的安装:https://golang.org/doc/install
Mac
mac系统通过homebrew
安装:
brew tap ethereum/ethereum
brew install ethereum
检查版本号,正常显示表示安装成功
JianjiangdeMacBook-Pro:~ user$ geth version
Geth
Version: 1.8.0-stable
Git Commit: 6e613cf3de6ebfd14edd5a332baf6e4079c1c86f
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
接下来的操作都以mac环境为准
其他
其他操作系统可前往官网参考:https://geth.ethereum.org/install/
2. 创建以太坊私有链
2.1 初始化一个创世区块
新建一个项目目录后进入,在目录中新建一个genesis.json
的配置文件,内容如下:
genesis.json:
{
"nonce": "0x0000000000000042",
"difficulty": "0x020000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x4c4b40",
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc": {}
}
接下来使用geth init ./genesis.json --datadir ./mychain
命令,初始化创世区块,--datadir ./mychain
用于指定区块链数据的存放位置为当前目录下的mychain
目录中
JianjiangdeMacBook-Pro:personal-eth user$ geth init genesis.json --datadir ./mychain
WARN [03-21|09:54:38] No etherbase set and no accounts found as default
INFO [03-21|09:54:38] Allocated cache and file handles database=/Users/jianjiangwang/Desktop/mychain/chain/geth/chaindata cache=16 handles=16
INFO [03-21|09:54:38] Successfully wrote genesis state database=chaindata hash=ee3898…ae7194
INFO [03-21|09:54:38] Allocated cache and file handles database=/Users/jianjiangwang/Desktop/mychain/chain/geth/lightchaindata cache=16 handles=16
INFO [03-21|09:54:38] Successfully wrote genesis state database=lightchaindata
出现上面的输出后表示创建完成,打开mychain
目录,会看到geth
和keystore
这两个子目录
2.2 启用私有链
启动方式1:
使用以下命令,启动私有链:
JianjiangdeMacBook-Pro:personal-eth user$ geth --datadir ./mychain --nodiscover console 2>>eth_output.log
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.0-unstable-6e613cf3/darwin-amd64/go1.9.2
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
>
--datadir : 指定区块链网络数据的存放位置
console : 启动命令行模式,可以在geth中执行命令
--nodiscover : 指定为私有链,不会被网上看到
另外看到指令中的>>eth_output.log
表示在当前文件夹下用eth_output.log
文件来记录输出
执行tail -f eth_output.log
, 可以看到输出日志
启动方式2:
geth --networkid 10 --datadir ./mychain --rpc --rpcapi "admin,debug,eth,miner,net,personal,shh,txpool,web3" rpcaddr "0.0.0.0" --rpccorsdomain "*" --nodiscover --dev console
再打开一个终端,输入下面的命令,进入基于该私链环境的控制台
geth attach "http://127.0.0.1:8545"
2.3 账户的添加和查看
成功启动geth控制台后,查看当前账户:
> web3.eth.accounts
目前没有账户,需要创建账户,c创建时需要设置密码:
> web3.personal.newAccount('123456')
"0xdaa65af5d348c25266a5588148a9c0e9e4c056f8"
返回的0xdaa65af5d348c25266a5588148a9c0e9e4c056f8
是账户地址, 123456
是账户的密码
这时再查看账户,看到刚刚创建的账户已经存在了
> web3.eth.accounts
["0xdaa65af5d348c25266a5588148a9c0e9e4c056f8"]
2.4 开始挖矿和停止挖矿
第一次执行挖矿前先要调用miner.setEtherbase
设置挖矿的账户,这里设置为刚刚创建的账户
> miner.setEtherbase("0xdaa65af5d348c25266a5588148a9c0e9e4c056f8")
执行miner.start()
开启挖矿:
> miner.start()
INFO [03-21|15:19:54] Starting mining operation
INFO [03-21|15:19:54] Commit new mining work number=59 txs=0 uncles=0 elapsed=276.402µs
INFO [03-21|15:19:55] Successfully sealed new block number=59 hash=14de08…85cdfe
INFO [03-21|15:19:55] 🔨 mined potential block number=59 hash=14de08…85cdfe
INFO [03-21|15:19:55] Commit new mining work number=60 txs=0 uncles=0 elapsed=143.737µs
INFO [03-21|15:19:56] Successfully sealed new block number=60 hash=ce1660…12c6a5
INFO [03-21|15:19:56] 🔨 mined potential block number=60 hash=ce1660…12c6a5
INFO [03-21|15:19:56] Commit new mining work number=61 txs=0 uncles=0 elapsed=229.035µs
如果要停止挖矿:
> miner.stop()
true
2.5 查看账户余额
通过web3.eth.getBalance()
来查看指定地址的余额:
> web3.eth.getBalance(0xdaa65af5d348c25266a5588148a9c0e9e4c056f8)
825000000000000000000
eth的最小单位是wei, 1 ether = 1e18 wei
3. 通过Mist客户端(Ethereum Wallet)连接私链
3.1 Ethereum Wallet和geth的区别
Ethereum Wallet客户端对应的是Mist项目,现在此客户端大多都称为Ethereum Wallet,也有称作Mist客户端的,知道它们两个指的是通一个客户端即可。此客户端使用JavaScript进行开发,支持windows、linux和OSX三类操作系统,是一个图形化操作界面的客户端。
3.2 安装Ethereum Wallet
https://github.com/ethereum/mist/releases下载操作系统对应的安装包安装
3.3 启动Ethereum Wallet
打开Ethereum Wallet客户端, 弹出界面,这时Ethereum Wallet默认连接的是ETH主网的数据, mac系统下,数据默认存放在Geth的目录下:~/Library/Ethereum
但现在是要将Ethereum Wallet连接到之前创建的私链, 关闭Ethereum Wallet
3.4 启动私链
按照先前的方法启动私链,成功后输出信息如下:
INFO [03-21|16:04:47] Maximum peer count ETH=25 LES=0 total=25
INFO [03-21|16:04:47] Starting peer-to-peer node instance=Geth/v1.8.2-stable/darwin-amd64/go1.10
INFO [03-21|16:04:47] Allocated cache and file handles database=/Users/jianjiangwang/Desktop/personal-eth/mychain/geth/chaindata cache=768 handles=1024
INFO [03-21|16:04:47] Initialised chain configuration config="{ChainID: 18 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> Engine: unknown}"
INFO [03-21|16:04:47] Disk storage enabled for ethash caches dir=/Users/jianjiangwang/Desktop/personal-eth/mychain/geth/ethash count=3
INFO [03-21|16:04:47] Disk storage enabled for ethash DAGs dir=/Users/jianjiangwang/.ethash count=2
INFO [03-21|16:04:47] Initialising Ethereum protocol versions="[63 62]" network=10
INFO [03-21|16:04:47] Loaded most recent local header number=165 hash=3bb44c…95201a td=22222305
INFO [03-21|16:04:47] Loaded most recent local full block number=165 hash=3bb44c…95201a td=22222305
INFO [03-21|16:04:47] Loaded most recent local fast block number=165 hash=3bb44c…95201a td=22222305
INFO [03-21|16:04:47] Loaded local transaction journal transactions=0 dropped=0
INFO [03-21|16:04:47] Regenerated local transaction journal transactions=0 accounts=0
WARN [03-21|16:04:47] Blockchain not empty, fast sync disabled
INFO [03-21|16:04:47] Starting P2P networking
INFO [03-21|16:04:47] RLPx listener up self="enode://6d3dccf06a620006499b2f01b5bfab40bb36af3611273abf77c4e308108bfe44d95520840f1771e71557688532fc962db9d32cec577eb4db2999012f8c92a485@[::]:30303?discport=0"
INFO [03-21|16:04:47] IPC endpoint opened url=/Users/jianjiangwang/Desktop/personal-eth/mychain/geth.ipc
先来读读信息:
INFO [03-21|16:04:47] IPC endpoint opened url=/Users/jianjiangwang/Desktop/personal-eth/mychain/geth.ipc
, 写明了私链的ipc端点位置, 笔者这里是/Users/jianjiangwang/Desktop/personal-eth/mychain/geth.ipc
, 复制下来备用
3.5 通过命令行启用Ethereum Wallet
启动私链节点后,如果直接打开Ethereum Wallet, 会报如下错误:
mist-error.png
这是由于打开Ethereum Wallet时,默认连接geth主网的ipc:
Users/jianjiangwang/Library/Ethereum/geth.ipc
, 而私链的ipc位置是/Users/jianjiangwang/Desktop/personal-eth/mychain/geth.ipc
解决办法是通过命令行,附加参数来启动Ethereum Wallet客户端, mac系统下,Ethereum Wallet的启动程序位于/Applications/Ethereum\ Wallet.app/Contents/MacOS/Ethereum\ Wallet
,新开一个终端,通过命令行指定ipc启动Ethereum Wallet:
JianjiangdeMacBook-Pro:~ user$ /Applications/Ethereum\ Wallet.app/Contents/MacOS/Ethereum\ Wallet --rpc /Users/jianjiangwang/Desktop/mychain/chain/geth.ipc
通过--rpc
指定私链的ipc端点位置/Users/jianjiangwang/Desktop/mychain/chain/geth.ipc
,客户端启动界面可以看到右上角的private-net标识:
点击LAUNCH APPLICATION
: