目的
搭建都拥有投票权的两节点联盟链
一、编译环境准备
1. 配置主机时间、时区、系统语言
#查看时区
date -R或者timedatectl
#修改时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#修改系统语言环境
sudo echo 'LANG="en_US.UTF-8"' >> /etc/profile
source /etc/profile
#配置主机NTP时间同步
##yum安装ntp
yum install -y ntp
service ntpd start
chkconfig ntpd on
##定时同步阿里ntp
echo '*/30 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' > /tmp/crontab2.tmp
crontab /tmp/crontab2.tmp
2. 编译环境搭建
2.1 Go语言环境安装
#下载并解压go二进制包
mkdir -p /geth
cd /geth
wget https://studygolang.com/dl/golang/go1.13.linux-amd64.tar.gz
tar -C /geth -zxvf go1.13.linux-amd64.tar.gz
#写入环境变量
cat >> /etc/profile <<EOF
export GOROOT=/geth/go
export PATH=$PATH:$GOROOT/bin
EOF
source /etc/profile
#验证,查看go语言环境版本
[root@jztosaas1 ~]# go version
go version go1.13 linux/amd64
二、编译以太坊go-ethereum源码
1.下载go-ethereum
2.编译go-ethereum源码包
#解压安装包
cd /geth
unzip go-ethereum-1.8.27.zip
cd go-ethereum-1.8.27
chmod 764 build/env.sh
#安装make和gcc
yum install -y make gcc-c++ gcc
make all
三、双节点规划
1.创建节点1和节点2所需目录
mkdir -p /geth/node1 /geth/node1_data /geth/node2 /geth/node2_data
目录结构如下:
目录说明:
node1 ——节点1执行脚本目录
node1_data ——节点1数据存储目录
node2 ——节点2执行脚本目录
node2_data ——节点2数据存储目录
2.创建以太坊新用户
#节点1创建新用户
/geth/node1/go-ethereum-1.8.27/build/bin/geth --datadir /geth/node1_data/ account new
#节点2创建新用户
/geth/node2/go-ethereum-1.8.27/build/bin/geth --datadir /geth/node2_data/ account new
下面为节点1创建例子,节点2同理
密码与Address(相当于用户名)都保存好
3.创世块初始化
3.1 通过puppeth生成初始化json文件
cd /geth/node1_data
/geth/node1/go-ethereum-1.8.27/build/bin/puppeth
然后crtl+c退出
执行完上面的设置会生成文件gethdev.json,gethdev-harmony.json文件
3.2初始化节点
cd /geth/node1_data
cp -rp gethdev.json /geth/node2_data
#节点1初始化
cd /geth/node1_data
/geth/node1/go-ethereum-1.8.27/build/bin/geth --datadir "/geth/node1_data/" init gethtest.json
#节点2初始化
cd /geth/node2_data
/geth/node2/go-ethereum-1.8.27/build/bin/geth --datadir "/geth/node2_data/" init gethtest.json
初始化成功现象
3.3 启动脚本和控制台脚本添加并启动以太坊
3.3.1 节点1
/geth/node1
#将创建用户时设置的密码写入文件password.txt
echo “123456” > password.txt
#启动命令添加进脚本
echo "nohup /geth/node1/go-ethereum-1.8.27/build/bin/geth --datadir /geth/node1_data --networkid 88806--nodiscover --port "30306" --rpc --rpcaddr "0.0.0.0" --rpcport "9545" --rpccorsdomain '*' --unlock '0x034dcf498b38f9078b6ba35ef07d06efeb74ccea8' --password '/geth/node1/password.txt' --etherbase=0x034dcf498b38f9078b6ba35ef07d06efeb74ccea8 --rpcapi 'personal,eth,net,web3' --syncmode "full" 2>&1 >> /geth/node1/geth.log &
" > start.sh
#进入管理界面脚本添加
echo "/geth/node1/go-ethereum-1.8.27/build/bin/geth attach /geth/node1_data/geth.ipc" > in.sh
#启动节点1
sh start.sh
ps -ef|grep geth 查看进程是否启动 或者
netstat -lnpt|grep 9545 查看服务监听端口是否启动
#进入节点1
sh in.sh
>eth.mining #查询矿工是否启动true为启动
>miner.start(1) #启动矿工
>eth.blockNumber #节点挖矿的块数
>admin.nodeInfo.enode #查询ennode
**start.sh文件参数说明:**
/geth/node1/go-ethereum-1.8.27/build/bin/geth
--datadir /geth/node1_data #go-ethereum数据存储目录
--networkid 88806 #初始化时设置的network id
--nodiscover
--port "30306"
--rpc
--rpcaddr "0.0.0.0"
--rpcport "9545" #其它工程调用go-ethereum的端口
--rpccorsdomain '*'
--unlock '0x034dcf498b38f9078b6ba35ef07d06efeb74ccea8' #创建新用户时生成的address
--password '/geth/node1/password.txt' #用户密码存储文件
--etherbase=0x034dcf498b38f9078b6ba35ef07d06efeb74ccea8 #创建新用户时生成的address
--rpcapi 'personal,eth,net,web3' #该节点控制台拥有权限
--syncmode "full"
3.3.2 节点2
节点2同节点1一样设置,启动,进入管理台查询enode值
3.3.3 节点间通信
上面3.3.1和3.3.2分别获取了节点1和节点2的enode值
添加进文件
[root@wanglai-134 node1_data]# cat /geth/node1_data/static-nodes.json
[
"enode://5c2a1f3fd6e6ef90a24ed49f63bf34df04e52f1c9b9ae34d6e1cb2ea502ff09d31871efb8834b0916e12865547a32eaa6d55c09dcd04c9231@127.0.0.1:30306?discport=0",
"enode://538c3c807451037e39378394df613f4cf886c680290b53ef50af67e1960b4517dfa1fc1031be2bbcc1a1b27c5486b04dfd6e4902887dafd435@127.0.0.1:30307?discport=0"
]
cp static-nodes.json /geth/node2_data
#重启节点1和节点2
ps -ef|grep geth
kill -9 ${pid} #杀掉geth进程
cd /geth/node1
sh start.sh #启动节点1
sh in.sh #进入节点1控制台
>miner.start(1) #启动矿工
cd /geth/node2
sh start.sh #启动节点2
sh in.sh #进入节点2控制台
>miner.start(1) #启动矿工
> admin.peers #查看节点连接情况,可以看到其他节点情况,node2上可看到node1的enode
[{
caps: ["eth/62", "eth/63"],
enode: "enode://5d849a6020b86761afaa0634d1bb0c0f5733dbc068ae3e61700f86fa854e8942eb5db6a97c5a5a012c3f757b5c12a05402b89a8ac512e1fab04e22df@127.0.0.1:59696",
id: "d3114c21c96a4b33c1e9175327cf231ef530828f74364574dad0de6d123003be",
name: "Geth/v1.8.27-stable/linux-amd64/go1.13",
network: {
inbound: true,
localAddress: "127.0.0.1:30306",
remoteAddress: "127.0.0.1:59696",
static: false,
trusted: false
},
protocols: {
eth: {
difficulty: 4,
head: "0xcbb974b9b1f3b9daff308cf3e406fac740de51bfe11f34e1a3753cf487fa0c62",
version: 63
}
}
}]
在节点2控制台执行admin.peers能看到节点1的enode信息,就表示两节点连接成功