樹梅派上搭建以太坊私有鏈並運行輕節點
安裝最新版的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