Commit ac0488a6 authored by Hugo's avatar Hugo

add 联盟链网络搭建

parent 7d4e3c65
# 1 联盟链网络搭建
# 1 联盟链网络搭建
## 1.1 安装Go环境,Git环境,expect
> 检查是否已经安装了golang和git,如果显示如下内容表示都没有安装.
```shell
ubuntu@ubuntu-VirtualBox:~$ go version
The program 'go' is currently not installed. You can install it by typing:
sudo apt install golang-go
ubuntu@ubuntu-VirtualBox:~$ git
The program 'git' is currently not installed. You can install it by typing:
sudo apt install git
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ expect
The program 'expect' is currently not installed. You can install it by typing:
sudo apt install expect
```
- Go环境安装参考<a hraf="https://chain.33.cn/document/81#1.1%20Go%20%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85">这里</a>
> 1.下载并解压golang1.9.2安装包
> 2.移动go目录到/usr/local
```shell
ubuntu@ubuntu-VirtualBox:~$ curl -O https://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz
ubuntu@ubuntu-VirtualBox:~$ tar xvf go1.9.2.linux-amd64.tar.gz
ubuntu@ubuntu-VirtualBox:~$ sudo mv go /usr/local
```
> 设置环境变量
> 下面的$HOME/work可以是任何目录,该目录将作为你golang工作目录,go get将把所有文件放在该目录下. 如果该目录不存在,需要先创建它.
> 编辑.profile文件,在末尾添加如下几行:
```shell
export GOPATH=$HOME/work
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
```
> 刷新profile通过如下命令:
```shell
source ~/.profile
```
>再次验证golang是否安装成功
```shell
ubuntu@ubuntu-VirtualBox:~$ go version
go version go1.9.2 linux/amd64
```
- Git环境安装参考<a href="https://chain.33.cn/document/81#1.2%20Git%20%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85">这里</a>
```shell
ubuntu@ubuntu-VirtualBox:~$ sudo apt install git
```
> 验证git是否安装成功
```shell
ubuntu@ubuntu-VirtualBox:~$ git --version
git version 2.7.4
```
- expect安装
```shell
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ sudo apt install expect
```
> 验证expect是否安装成功
```shell
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ expect -f
expect: option requires an argument -- 'f'
usage: expect [-div] [-c cmds] [[-f] cmdfile] [args]
```
## 1.2 下载plugin源代码,编译生成可执行文件
> 代码下载参考<a href="https://chain.33.cn/document/81#2%20%E4%BB%A3%E7%A0%81%E4%B8%8B%E8%BD%BD">这里</a>,执行结果演示如下:
```shell
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn$ git clone https://github.com/33cn/plugin.git
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn$ cd plugin
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin$ make
```
编译成功后会在build目录下面生成chain33和chain33-cli两个可执行文件
## 1.3 生成4节点配置文件
> 执行如下命令编译生成pvfile,该程序用于生成制定数量的配置文件
```shell
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin$ cd plugin/consensus/tendermint/tools/
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ go build pvfile.go
```
> 按提示输入4,生成4节点的配置文件
```shell
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ ./pvfile
Please enter the number of key file:4
```
> 查看生成文件
```shell
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ ls
auto_deploy.sh genesis.json nonePerf.go priv_validator_0.json priv_validator_1.json priv_validator_2.json priv_validator_3.json pvfile pvfile.go
```
## 1.4 发送相关文件到指定节点
### 1.4.1 修改配置文件
> 将../tendermint/chain33.test.toml拷贝到当前目录下
```shell
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ cp ../chain33.test.toml .
```
> 修改配置文件,这里我们的4个节点都是seed节点,所以将[p2p]中的isSeed设置为true,并将节点ip:port添加进去[p2p]中的seeds和[consensus.sub.tendermint]中validatorNodes中并保存
```plain
[p2p]
seeds=["192.168.0.105:13802","192.168.0.107:13802","192.168.0.116:13802","192.168.0.137:13802"]
isSeed=true
[consensus.sub.tendermint]
validatorNodes=["192.168.0.105:46656","192.168.0.107:46656","192.168.0.116:46656","192.168.0.137:46656"]
```
### 1.4.1 发送相同文件到各节点
> 拷贝chain33,chain33-cli
```shell
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ cp ~/work/src/github.com/33cn/plugin/build/chain33 .
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ cp ~/work/src/github.com/33cn/plugin/build/chain33-cli .
```
> 执行auto_deploy.sh文件,发送chain33 chain33-cli genesis.json chain33.test.toml四个文件到指定节点
> 注:如果目标节点的目标文件夹不存在,请先创建.
```shell
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ ./auto_deploy.sh
***************************************************************************************************
*
* This tool can send file to specified path.
* And you should input the file first(It doesn't support get file auto-matically now)
* Then it will pack those file into a package and send to the environment.
*
* Note: You should move the file to the current directory, otherwise the packing process will be failed.
*
***************************************************************************************************
Please input the file: (such as "chain33 chain33-cli genesis.json" ...)
chain33 chain33-cli genesis.json chain33.test.toml
Which way to get environment? 1) Input 2) Config file
1
Please input the username, password and path of the destination: (such as "ubuntu 123456 /home/ubuntu/chain33")
ubuntu 123456 /home/ubuntu/chain33
Please input ip list of your destination: (such as "192.168.3.143 192.168.3.144 192.168.3.145 192.168.3.146")
192.168.0.105 192.168.0.107 192.168.0.116 192.168.0.137
servers.1: userName->ubuntu
servers.1: password->123456
servers.1: hostIp->192.168.0.105
servers.1: path->/home/ubuntu/chain33
servers.2: userName->ubuntu
servers.2: password->123456
servers.2: hostIp->192.168.0.107
servers.2: path->/home/ubuntu/chain33
servers.3: userName->ubuntu
servers.3: password->123456
servers.3: hostIp->192.168.0.116
servers.3: path->/home/ubuntu/chain33
servers.4: userName->ubuntu
servers.4: password->123456
servers.4: hostIp->192.168.0.137
servers.4: path->/home/ubuntu/chain33
spawn scp chain33_tendermint_config.tar.gz ubuntu@192.168.0.105:/home/ubuntu/chain33
ubuntu@192.168.0.105's password:
chain33_tendermint_config.tar.gz 100% 29MB 28.9MB/s 00:01
spawn ssh ubuntu@192.168.0.105 tar zxf /home/ubuntu/chain33/chain33_tendermint_config.tar.gz -C /home/ubuntu/chain33
ubuntu@192.168.0.105's password:
tar (child): /home/ubuntu/chain33/chain33_tendermint_config.tar.gz: Cannot open: Not a directory
tar (child): Error is not recoverable: exiting now
gzip: stdin: unexpected end of file
tar: Child returned status 2
tar: Error is not recoverable: exiting now
spawn scp chain33_tendermint_config.tar.gz ubuntu@192.168.0.107:/home/ubuntu/chain33
ubuntu@192.168.0.107's password:
chain33_tendermint_config.tar.gz 100% 29MB 28.9MB/s 00:01
spawn ssh ubuntu@192.168.0.107 tar zxf /home/ubuntu/chain33/chain33_tendermint_config.tar.gz -C /home/ubuntu/chain33
ubuntu@192.168.0.107's password:
spawn scp chain33_tendermint_config.tar.gz ubuntu@192.168.0.116:/home/ubuntu/chain33
ubuntu@192.168.0.116's password:
chain33_tendermint_config.tar.gz 100% 29MB 28.9MB/s 00:01
spawn ssh ubuntu@192.168.0.116 tar zxf /home/ubuntu/chain33/chain33_tendermint_config.tar.gz -C /home/ubuntu/chain33
ubuntu@192.168.0.116's password:
spawn scp chain33_tendermint_config.tar.gz ubuntu@192.168.0.137:/home/ubuntu/chain33
ubuntu@192.168.0.137's password:
chain33_tendermint_config.tar.gz 100% 29MB 28.9MB/s 00:00
spawn ssh ubuntu@192.168.0.137 tar zxf /home/ubuntu/chain33/chain33_tendermint_config.tar.gz -C /home/ubuntu/chain33
ubuntu@192.168.0.137's password:
```
### 1.4.2 发送不同的priv_validator.json到各节点
> 分别将当前目录下的priv_validator_x.json拷贝到对应节点的/home/ubuntu/chain33目录下,并命名为priv_validator.json
```shell
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ scp ./priv_validator_0.json ubuntu@192.168.0.105:/home/ubuntu/chain33/priv_validator.json
ubuntu@192.168.0.105's password:
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ scp ./priv_validator_1.json ubuntu@192.168.0.107:/home/ubuntu/chain33/priv_validator.json
ubuntu@192.168.0.107's password:
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ scp ./priv_validator_2.json ubuntu@192.168.0.116:/home/ubuntu/chain33/priv_validator.json
ubuntu@192.168.0.116's password:
ubuntu@ubuntu-VirtualBox:~/work/src/github.com/33cn/plugin/plugin/consensus/tendermint/tools$ scp ./priv_validator_3.json ubuntu@192.168.0.137:/home/ubuntu/chain33/priv_validator.json
ubuntu@192.168.0.137's password:
```
## 1.5 在每个节点启动程序
> 登录到各个节点输入如下命令,启动区块链程序:
```shell
ubuntu@ubuntu055-1:~/chain33$ nohup ./chain33 -f chain33.test.toml >> log.out 2>&1 &
```
## 1.6 发送交易测试区块链是否运行正常
- 首先创建钱包及账户,具体参考<a href="https://chain.33.cn/document/80#1.3%20%E5%88%9B%E5%BB%BA%E9%92%B1%E5%8C%85%E4%BB%A5%E5%8F%8A%E8%B4%A6%E6%88%B7" >这里</a>,执行结果演示如下:
```shell
ubuntu@ubuntu-VirtualBox:~/chain33$ ./chain33-cli account list
{
"wallets": [
{
"acc": {
"balance": "0.0000",
"frozen": "0.0000",
"addr": "1JfGzjz4ikQWuLyDGPL8DP9hVNUgDXaDLY"
},
"label": "node award"
},
{
"acc": {
"balance": "100000000.0000",
"frozen": "0.0000",
"addr": "1CbEVT9RnM5oZhWMj4fxUrJX94VtRotzvs"
},
"label": "genesis"
},
{
"acc": {
"balance": "0.0000",
"frozen": "0.0000",
"addr": "1EqCTLRGPXYHDDUBrVUuoeqQyTnpSAMgXM"
},
"label": "test"
}
]
}
```
- 从genesis账户向test账户发送转账交易,具体参考<a href="https://chain.33.cn/document/80#1.4.2%20%E5%8F%91%E9%80%81%E8%BD%AC%E8%B4%A6%E4%BA%A4%E6%98%93" >这里</a>,执行结果演示如下:
```shell
ubuntu@ubuntu-VirtualBox:~/chain33$ ./chain33-cli send bty transfer -a 1000 -t 1EqCTLRGPXYHDDUBrVUuoeqQyTnpSAMgXM -n "first transfer" -k 1CbEVT9RnM5oZhWMj4fxUrJX94VtRotzvs
0x040224c373b189ccd7a6e2c5a123a5a9f743e4905ad112f0cf5957145db7bdd4
```
- 查询交易结果,具体参考<a href="https://chain.33.cn/document/80#1.4.3%20%E6%9F%A5%E8%AF%A2%E4%BA%A4%E6%98%93%E7%BB%93%E6%9E%9C">这里</a>,执行结果演示如下:
```shell
ubuntu@ubuntu-VirtualBox:~/chain33$ ./chain33-cli tx query -s 0x040224c373b189ccd7a6e2c5a123a5a9f743e4905ad112f0cf5957145db7bdd4
{
"tx": {
"execer": "coins",
"payload": {
"transfer": {
"cointoken": "",
"amount": "100000000000",
"note": "first transfer",
"to": "1EqCTLRGPXYHDDUBrVUuoeqQyTnpSAMgXM"
},
"ty": 1
},
.........
"fee": "0.0010",
"expire": 1550563096,
"nonce": 3159930318345275859,
"to": "1EqCTLRGPXYHDDUBrVUuoeqQyTnpSAMgXM",
"from": "1CbEVT9RnM5oZhWMj4fxUrJX94VtRotzvs",
"hash": "0x040224c373b189ccd7a6e2c5a123a5a9f743e4905ad112f0cf5957145db7bdd4"
},
............
"height": 1,
"index": 1,
"blocktime": 1550562976,
"amount": "1000.0000",
"fromaddr": "1CbEVT9RnM5oZhWMj4fxUrJX94VtRotzvs",
"actionname": "transfer",
"assets": [
{
"exec": "coins",
"symbol": "BTY",
"amount": 100000000000
}
]
}
```
- 查询节点信息,高度相同表示区块链工作正常.
```shell
ubuntu@ubuntu-VirtualBox:~/chain33$ ./chain33-cli net peer_info
{
"peers": [
{
"addr": "192.168.0.137",
"port": 13802,
"name": "035bd8a1f1aba3ef53cac434530d611d5e8a9527980ab3a5410c5f2fddb1a83194",
"mempoolSize": 0,
"self": false,
"header": {
"version": 0,
"parentHash": "0xc8799befacd9709a5f4dfa68d6fd53a37ccce83352997a132adfed7f46747757",
"txHash": "0xd7dabfb237a791aa9d907d10f4e2d8578943b407a0af2443a7ac50492afcd1ec",
"stateHash": "0xafec60a0a5931b30ddd0cf849fea1a72ef8687d41030a3aa8adb83c947f78129",
"height": 1,
"blockTime": 1550562976,
"txCount": 2,
"hash": "0x9ebd6aebcb764d6d381a948ff4534edd7e3fa214a4361a43311b9bf880be2c08",
"difficulty": 0
}
},
{
"addr": "192.168.0.107",
"port": 13802,
"name": "0294cfe779fbf34225ec89d278f80658d93a4e82075a4ca1ec0b4456b63ecbc6f1",
"mempoolSize": 0,
"self": false,
"header": {
"version": 0,
"parentHash": "0xc8799befacd9709a5f4dfa68d6fd53a37ccce83352997a132adfed7f46747757",
"txHash": "0xd7dabfb237a791aa9d907d10f4e2d8578943b407a0af2443a7ac50492afcd1ec",
"stateHash": "0xafec60a0a5931b30ddd0cf849fea1a72ef8687d41030a3aa8adb83c947f78129",
"height": 1,
"blockTime": 1550562976,
"txCount": 2,
"hash": "0x9ebd6aebcb764d6d381a948ff4534edd7e3fa214a4361a43311b9bf880be2c08",
"difficulty": 0
}
},
{
"addr": "192.168.0.116",
"port": 13802,
"name": "0373d41c98e896bebfe37f7e831e848da1889f88574a7622d112511caf07c7d299",
"mempoolSize": 0,
"self": false,
"header": {
"version": 0,
"parentHash": "0xc8799befacd9709a5f4dfa68d6fd53a37ccce83352997a132adfed7f46747757",
"txHash": "0xd7dabfb237a791aa9d907d10f4e2d8578943b407a0af2443a7ac50492afcd1ec",
"stateHash": "0xafec60a0a5931b30ddd0cf849fea1a72ef8687d41030a3aa8adb83c947f78129",
"height": 1,
"blockTime": 1550562976,
"txCount": 2,
"hash": "0x9ebd6aebcb764d6d381a948ff4534edd7e3fa214a4361a43311b9bf880be2c08",
"difficulty": 0
}
},
{
"addr": "192.168.0.105",
"port": 13802,
"name": "03da6dec2f03958caa104fa79c58c49d95c343118b49ab959791d144d7b69db71b",
"mempoolSize": 0,
"self": true,
"header": {
"version": 0,
"parentHash": "0xc8799befacd9709a5f4dfa68d6fd53a37ccce83352997a132adfed7f46747757",
"txHash": "0xd7dabfb237a791aa9d907d10f4e2d8578943b407a0af2443a7ac50492afcd1ec",
"stateHash": "0xafec60a0a5931b30ddd0cf849fea1a72ef8687d41030a3aa8adb83c947f78129",
"height": 1,
"blockTime": 1550562976,
"txCount": 2,
"hash": "0x9ebd6aebcb764d6d381a948ff4534edd7e3fa214a4361a43311b9bf880be2c08",
"difficulty": 0
}
}
]
}
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment