【介绍】
go-ethereum 是以太坊常用的客户端,简称 Geth 。
官网:https://geth.ethereum.org/
下载:https://geth.ethereum.org/downloads/
【依赖】
安装Git, Nodejs, gcc-c++, ntp, epel 等辅助工具:
1 | yum install git bzip2 gcc -c++ ntp epel-release nodejs |
安装cmake:
安装golang(go-ethereum是golang编写的):
【安装】
1 2 3 4 5 | git clone https: //github .com /ethereum/go-ethereum .git cd go-ethereum/ make all echo "export PATH=$PATH:/root/go-ethereum/build/bin" >> /etc/profile source /etc/profile |
报错:
1 2 3 4 5 | ... github.com /ethereum/go-ethereum/swarm/testutil util.go:45: exit status 2 exit status 1 make : *** [all] Error 1 |
参考:https://github.com/ethereum/go-ethereum/issues/15533
参考:https://blog.csdn.net/bravezhe/article/details/81838430
参考:https://bitshuo.com/topic/592503e7e358dd8c2f65d859
1 | mv /usr/local/include/iconv .h /usr/local/include/iconv .h.back |
启动网络时间同步,开启防火墙,打开8087和30303端口(geth用到的端口):
1 2 3 4 5 | systemctl enable ntpd systemctl start ntpd systemctl start firewalld firewall-cmd --zone=public --add-port=8087 /tcp --permanent firewall-cmd --zone=public --add-port=30303 /tcp --permanent |
【命令】
语法:
1 | geth [options] command [ command options] [arguments...] |
帮助:
1 | geth --help |
【配置】
搭建私有链前需要创建 genesis.json 配置文件用于初始化创世区块:
参考:https://bitshuo.com/topic/59a531a0acad6ff75e6297af
参考:http://wp.iter-cc.com/?p=1055
参考:http://bigdatadecode.club/Ethereum-private-chain.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | { "config" : { "chainId" : 8, "homesteadBlock" : 0, "eip155Block" : 0, "eip158Block" : 0 }, "alloc" : { "0x0be5196a5503e4051b21e713f245b3ab24bcfeaf" : { "balance" : "2000000000000000000000000" }}, "coinbase" : "0x0be5196a5503e4051b21e713f245b3ab24bcfeaf" , "difficulty" : "0x20000" , "extraData" : "0x74686520726f73657320696e206865722068616e642c2074686520666c61766f7220696e206d696e65" , "gasLimit" : "0x2fefd8" , "nonce" : "0x00000000c000ff59" , "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000" , "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000" , "timestamp" : "0x599DA33A" } |
配置说明:
config.chainId // 区块链的ID,在 geth 命令中的 –networkid 参数需要与 chainId 的值一致
config.homesteadBlock // Homestead 硬分叉区块高度,不需要关注
config.eip155Block // EIP 155 硬分叉高度,不需要关注
config.eip158Block // EIP 158 硬分叉高度,不需要关注
coinbase // 矿工账号,第一个区块挖出后将给这个矿工账号发送奖励的以太币
difficulty // 难度值,越大越难
extraData // 附加信息随便填
gasLimit // gas 的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大
nonce // 一个 64 位随机数
mixhash // 与 nonce 配合用于挖矿,由上一个区块的一部分生成的 hash
parentHash // 上一个区块的 hash 值
alloc // 预设账号以及账号的以太币数量,私有链挖矿比较容易可以不配置
创建区块文件夹,使用 geth 的 init 命令初始化创世区块:
1 2 3 | mkdir -p /root/ethbase/chain cd root geth --datadir "/root/ethbase/chain" init genesis.json |
输出:
1 | INFO [11-30|22:42:41.552] Successfully wrote genesis state database=lightchaindata hash =1e7bea…deffa5 |
输出调试log:
1 | geth --dev console 2>> geth_dev_log |
输出:
1 2 3 4 5 6 7 | Welcome to the Geth JavaScript console! instance: Geth /v1 .8.20-unstable-ef8ced41 /linux-amd64/go1 .11.2 coinbase: 0xf13ddcac8c11027fac5305882ec4c3236c20b265 at block: 0 (Thu, 01 Jan 1970 08:00:00 CST) datadir: modules: admin:1.0 clique:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 shh:1.0 txpool:1.0 web3:1.0 |
然后在大于号后输入尝试:
1 2 | > eth.accounts "0x3341e796e5d43919287d49a86cb595d74ddea657" |
即完成Geth的安装。
【同步】
参考:https://www.jianshu.com/p/3445ff08229a
参考:https://blog.csdn.net/guokaikevin/article/details/79254785
Ethereum(以太坊)经过历年的累积,交易区块大小已有几百G左右。如果全部同步,并且严格逐个验证,需要太多的时间和计算。
「同步」的意思是把网络上的区块全部下载到本地,以同步到网络的最新状态。使用客户端前必须先同步区块。
以太坊有三种同步的模式:full 、 fast 和 light。
full 模式:
从开始到结束,获取区块的header,获取区块的body,从创始块开始校验每一个元素,需要下载所有区块数据信息。速度最慢,但是能获取到所有的历史数据。
1 | geth --syncmode full |
fast 模式:
快速同步模式,只下载状态(state downloads)。获取区块的header,获取区块的body,在同步到当前块之前不处理任何事务。然后获得一个快照,此后,像full节点一样进行后面的同步操作。
这种方法用得最多,目的在不要在意历史数据,将历史数据按照快照的方式,不逐一验证,沿着区块下载最近数据库中的交易,有可能丢失历史数据。此方法可能会对历史数据有部分丢失,但是不影响今后的使用。
使用此模式时注意需要设置 –cache,默认16M,建议设置为 1G(1024)到 2G(2048)。
1 | geth --fast --cache 512 |
light 模式
仅获取当前状态。验证元素需要向full节点发起相应的请求。
1 | geth --light |
更优雅的同步方式
同步时把输出日志重定向到network_sync.log中,并进入控制台。这样就可以边同步边使用控制台命令。
1 | geth --fast console 2>network_sync.log |
浏览日志:
1 | tail -f network_sync.log |
【账户】
参考:https://www.jianshu.com/p/3445ff08229a
新建账户:
1 2 | geth account new > personal.newAccount( "passphrase" ) |
非交互方式:
1 | geth --password <passwordfile> account new |
密码以明文方式写在文件 passwordfile 里
导入账户:
1 | geth account import <keyfile> |
私钥以明文方式写在文件 keyfile 里,每个一行。
是存着账户信息的 json 文件,其中私钥是被账号密码(password)加密后,存放在里面的。
不同平台的 keyfile 默认存储位置是不同的
1. Windows: C:\Users\username%appdata%\Roaming\Ethereum\keystore
2. Linux: ~/.ethereum/keystore
3. Mac: ~/Library/Ethereum/keystore
结合 –password,可以使用在导入账户时设置密码:
1 | geth --password <passwordfile> account import <keyfile> |
修改密码:
1 | geth account update <address> |
例如 geth account update 2:2是账户的编号,在 geth account list 中可以查看。
导入钱包:
1 | geth wallet import <etherwallet.json> |
创建多签名钱包,请参见 Creating a Multi-Signature Wallet in Mist
网址:http://www.ethdocs.org/en/latest/account-management.html#creating-a-multi-signature-wallet-in-mist
列出所有账户:
1 | geth account list |
对应的控制台命令为 > eth.accounts
查看账户余额:
列出账户余额,单位是 Wei :
1 | > eth.getBalance(<address>): |
列出账户余额,单位是 eth :
1 | > web3.fromWei(eth.getBalance(<address>), "ether" ) |
下面的代码可以打印所有的账户余额:
1 2 3 4 5 6 7 | function checkAllBalances() { var i =0; eth.accounts.forEach( function (e){ console.log( " eth.accounts[" +i+ "]: " + e + " \tbalance: " + web3.fromWei(eth.getBalance(e), "ether" ) + " ether" ); i++; }) }; |
可以把代码存到文件中。进入 geth 的控制台后,用 > loadScript() 导入文件中的函数。
【参考】
参考:https://blog.csdn.net/cowfishy/article/details/80211116
参考:http://www.voidcn.com/article/p-atytuled-bpk.html
参考:https://segmentfault.com/a/1190000012834708