树梅派上搭建以太坊私有链并运行轻节点
安装最新版的go语言
apt-get install golang-go这样安装版本可能过低。
go version查看版本为 1.6.2。
apt-get 卸载此版本重新安装
- 在树莓派的命令行输入命令
cat /proc/cpuinfo
查看树莓派的型号 - 在go语言官网下载最新版适合自己操作系统和架构的版本:https://studygolang.com/dl。
- 比如我要下载的是go1.13.4.linux-armv6l.tar.gz
- 下载
wget https://studygolang.com/dl/golang/go1.13.4.linux-armv6l.tar.gz
- 解压缩
sudo tar -zxvf go1.13.4.linux-armv6l.tar.gz -C /usr/lib
- 设置添加环境变量
sudo vi /etc/profile
在最后面添加如下配置
export GOPATH=/opt/gopath
export GOROOT=/usr/lib/go
export GOARCH=386
export GOOS=linux
export GOTOOLS=$GOROOT/pkg/tool
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
- 执行
source /etc/profile
- 查看版本
go version
结果:go version go1.13.4 linux/arm
安装geth
在geth官网下载自己操作系统和架构匹配的版本:
https://geth.ethereum.org/downloads/
我下载的是geth-linux-arm7-1.9.7-a718daa6.tar.gz
这是官方提供了编译好的二进制安装包,直接下载解压后即可使用。Linux下可以将geth所在目录添加到环境变量,或者直接sudo mv geth /usr/bin。
准备创世区块
{
"config": {
"chainId": 20191111,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"ethash": {}
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0xffffffff",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
进入想要新建节点的目录
cd /home/pi/Documents/privatechain
新建账户
因为树莓派的内存比较小,新建账户的时候有可能会出现out of memory 的情况,所以在启动节点前新建账户,减少新建账户时的内存使用量。如果这时仍然出现out of memory,那就重启一下,然后尽量少打开其他的应用,先新建账户
这一步很容易爆内存,执行命令前可以先在树莓派上清理内存:
sudo apt-get autoclean //--清理旧版本的软件缓存
sudo apt-get clean //--清理所有软件缓存
sudo apt-get autoremove //--删除系统不再使用的孤立软件
//如果执行了上面三条命令之后,新建账户还是爆内存,那可以考虑输入这两条命令
lxpanelctl restart //重启桌面面板
sudo service lightdm restart //重启图形服务
新建账户:geth --datadir ./data01 account new
然后按指示输入密码,结果如下:
$ geth --datadir .pichain account new
INFO [12-27|14:48:47.373] Maximum peer count ETH=50 LES=0 total=50
INFO [12-27|14:48:47.381] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
Your new account is locked with a password. Please give a password. Do not forget this password.
Password:
Repeat password:
Your new key was generated
Public address of the key: 0x503132F313dc68A85A400982cb58f913F8E766FC
Path of the secret key file: .pichain/keystore/UTC--2019-12-27T06-48-52.297906913Z--503132f313dc68a85a400982cb58f913f8e766fc
- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
初始化
geth --datadir ./data01 init gen_xxw.json
启动节点
注意这时要指定使用全同步模式(即
--syncmode full
),否则后续和服务器连接同步时可能会出错
geth --syncmode full --datadir ./data01 --identity "my etherum" --ipcdisable --allow-insecure-unlock --rpc --rpccorsdomain "*" --rpcapi "db,eth,net,web3,debug" --rpcport "8545" --port "30303" --networkid 20191111 --nodiscover console 2>>geth01.log
参考链接:
https://www.rs-online.com/designspark/exploring-ethereum-with-raspberry-pi-part-2-creating-a-private-blockchain