Hyperledger Fabric v1.1.0 Demo

Hyperledger Fabric v1.1.0 Demo

十月 23, 2018

1. 虚拟机准备

操作系统选择:Ubuntu 14.04

内存:8G

CPU:4核

硬盘:20G

2.(可选)更改ssh配置使得root可以通过SSH登录

为root设置密码

1
sudo passwd root

编辑/etc/ssh/sshd_config配置文件,允许root用户通过SSH登录

1
2
3
4
5
sudo vi /etc/ssh/sshd_config

修改内容

PermitRootLogin yes

重启sshd服务

1
2
3
4
5
6
7
sudo service ssh restart
```
## 3. 安装必要的工具
```sh
sudo apt-get update

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common git make gcc

4. 安装Docker(17.06.2-ce or greater is required)

4.1 使用国内的阿里源安装Docker

1
2
3
4
5
6
7
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

sudo apt-get -y update

sudo apt-get install docker-ce

4.2 添加阿里云的Docker hub镜像

1
2
3
sudo mkdir -p /etc/docker

vi /etc/docker/daemon.json

在daemon.json中添加

1
2
3
{
"registry-mirrors" : ["https://xzmzop8s.mirror.aliyuncs.com"]
}

4.3 为用户添加免sudo执行docker命令

将用户加入该 group 内。然后退出并重新登录

1
2
sudo gpasswd -a ${USER} docker
newgrp - docker

4.3 重新启动Docker服务

1
sudo service docker restart

5. 安装docker-compose(version 1.14.0 or greater)

1
2
3
4
5
curl -L https://get.daocloud.io/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m`>/usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

docker-compose version

6. 安装Golang环境(Go version 1.10.x is required

1
2
3
4
5
6
7
cd ~

wget https://studygolang.com/dl/golang/go1.10.1.linux-amd64.tar.gz

tar -C /usr/local -xzf go1.10.1.linux-amd64.tar.gz

mkdir go

Go环境变量设置

vi /etc/profile

添加

1
2
3
export GOPATH=~/go

export PATH=$PATH:/usr/local/go/bin

使环境变量生效

source /etc/profile

go version

7. 安装Node.js和NPM(version 8.9.x or greater,但目前不支持9.x)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
cd ~

wget https://npm.taobao.org/mirrors/node/v8.11.2/node-v8.11.2-linux-x64.tar.xz

mkdir node

tar xvf node-v*.tar.?z --strip-components=1 -C ./node

rm -rf node-v*

mkdir node/etc

echo 'prefix=/usr/local' > node/etc/npmrc

sudo mv node /opt/

sudo chown -R root: /opt/node

sudo ln -s /opt/node/bin/node /usr/local/bin/node

sudo ln -s /opt/node/bin/npm /usr/local/bin/npm

node -v

npm -v

8. python版本确认(2.7, npm install需要)

python --version

9. hyperledger fabric源代码下载

1
2
3
4
5
6
7
8
9
mkdir -p ~/go/src/github.com/hyperledger

cd ~/go/src/github.com/hyperledger/

git clone https://github.com/hyperledger/fabric.git

cd ~/go/src/github.com/hyperledger/fabric/

git checkout v1.1.0

10. hyperledger fabric docker镜像下载

1
2
3
4
5
6
7
8
9
cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/

source download-dockerimages.sh -c x86_64-1.1.0 -f x86_64-1.1.0

docker pull hyperledger/fabric-kafka:latest

docker pull hyperledger/fabric-zookeeper:latest

docker images

11. 启动Fabric网络完成chaincode测试(运行例子:e2e_cli)

  • 注意:修改示例源代码中的一个错误(少了一个下划线)
    vim ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/base/peer-base.yaml
    将以下内容:

- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=e2ecli_default

修改为:

- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=e2e_cli_default

运行示例

1
2
3
cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/

./network_setup.sh up

成功运行后显示:

1
2
3
4
5
6
7
8
9
10
11
12
Query Result: 90
2018-09-05 06:25:46.293 UTC [main] main -> INFO 008 Exiting.....
===================== Query on PEER3 on channel 'mychannel' is successful =====================

===================== All GOOD, End-2-End execution completed =====================


_____ _ _ ____ _____ ____ _____
| ____| | \ | | | _ \ | ____| |___ \ | ____|
| _| | \| | | | | | _____ | _| __) | | _|
| |___ | |\ | | |_| | |_____| | |___ / __/ | |___
|_____| |_| \_| |____/ |_____| |_____| |_____|

查看启动的容器

docker ps

可以看到启动了16个容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ubuntu@i-3d6c3f60:~/go/src/github.com/hyperledger/fabric/examples/e2e_cli$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a04213d7410a dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab "chaincode -peer.add…" 54 seconds ago Up 54 seconds dev-peer1.org2.example.com-mycc-1.0
ad9491cb991a dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9 "chaincode -peer.add…" About a minute ago Up About a minute dev-peer0.org1.example.com-mycc-1.0
e5681194e2ef dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b "chaincode -peer.add…" About a minute ago Up About a minute dev-peer0.org2.example.com-mycc-1.0
e01087f038ce hyperledger/fabric-tools "/bin/bash -c './scr…" 2 minutes ago Up 2 minutes cli
c12d14e72754 hyperledger/fabric-orderer "orderer" 2 minutes ago Up 2 minutes 0.0.0.0:7050->7050/tcp orderer.example.com
bbc867fee694 hyperledger/fabric-kafka "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 9093/tcp, 0.0.0.0:32797->9092/tcp kafka3
f780180a5001 hyperledger/fabric-kafka "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 9093/tcp, 0.0.0.0:32796->9092/tcp kafka2
fcd37d70d806 hyperledger/fabric-kafka "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 9093/tcp, 0.0.0.0:32795->9092/tcp kafka0
2536fa485516 hyperledger/fabric-kafka "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 9093/tcp, 0.0.0.0:32794->9092/tcp kafka1
84783e5b1e79 hyperledger/fabric-peer "peer node start" 2 minutes ago Up 2 minutes 0.0.0.0:7051-7053->7051-7053/tcp peer0.org1.example.com
77c6ea61aaa3 hyperledger/fabric-peer "peer node start" 2 minutes ago Up 2 minutes 0.0.0.0:10051->7051/tcp, 0.0.0.0:10052->7052/tcp, 0.0.0.0:10053->7053/tcp peer1.org2.example.com
8867981827da hyperledger/fabric-zookeeper "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:32793->2181/tcp, 0.0.0.0:32792->2888/tcp, 0.0.0.0:32790->3888/tcp zookeeper0
54a571ecc961 hyperledger/fabric-peer "peer node start" 2 minutes ago Up 2 minutes 0.0.0.0:9051->7051/tcp, 0.0.0.0:9052->7052/tcp, 0.0.0.0:9053->7053/tcp peer0.org2.example.com
899df272085a hyperledger/fabric-zookeeper "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:32791->2181/tcp, 0.0.0.0:32789->2888/tcp, 0.0.0.0:32788->3888/tcp zookeeper2
2bd4de5f8d6e hyperledger/fabric-peer "peer node start" 2 minutes ago Up 2 minutes 0.0.0.0:8051->7051/tcp, 0.0.0.0:8052->7052/tcp, 0.0.0.0:8053->7053/tcp peer1.org1.example.com
35d3b6d83652 hyperledger/fabric-zookeeper "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:32787->2181/tcp, 0.0.0.0:32786->2888/tcp, 0.0.0.0:32785->3888/tcp zookeeper1
ubuntu@i-3d6c3f60:~/go/src/github.com/hyperledger/fabric/examples/e2e_cli$

查询测试

打开一个命令行窗口,关联cli节点

docker exec -it cli bash

查询a账户余额:

peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

查询结果如下:

1
2
3
4
5
6
7
8
9
10
2018-09-05 07:58:10.161 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-09-05 07:58:10.161 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-09-05 07:58:10.162 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-09-05 07:58:10.164 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-09-05 07:58:10.164 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-09-05 07:58:10.165 UTC [msp/identity] Sign -> DEBU 006 Sign: plaintext: 0ABE070A6608031A0B089299BEDC0510...6D7963631A0A0A0571756572790A0161
2018-09-05 07:58:10.165 UTC [msp/identity] Sign -> DEBU 007 Sign: digest: DDB5E812D2C40C2716B4403A54668C66C3B799D79B15E97B5E72EA19D8825847
Query Result: 90
2018-09-05 07:58:10.177 UTC [main] main -> INFO 008 Exiting.....
root@e01087f038ce:/opt/gopath/src/github.com/hyperledger/fabric/peer#

可以看到账户a中余额为:90

查询b账户余额:

peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'

结果:

1
2
3
4
5
6
7
8
9
2018-09-05 08:00:02.723 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-09-05 08:00:02.723 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-09-05 08:00:02.723 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-09-05 08:00:02.723 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-09-05 08:00:02.723 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-09-05 08:00:02.724 UTC [msp/identity] Sign -> DEBU 006 Sign: plaintext: 0ABF070A6708031A0C08829ABEDC0510...6D7963631A0A0A0571756572790A0162
2018-09-05 08:00:02.724 UTC [msp/identity] Sign -> DEBU 007 Sign: digest: DA97BF218DE059B6C77A7EC6628B41B506589172456D3B72C1D27129DCFF66B6
Query Result: 210
2018-09-05 08:00:02.736 UTC [main] main -> INFO 008 Exiting.....

b账户余额为210

转账测试(b向a转账30):

1
2

`peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -c '{"Args":["invoke","b","a","30"]}'`

返回结果

1
2
3
4
5
6
7
8
9
10
11
12
2018-09-05 08:01:08.254 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-09-05 08:01:08.254 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-09-05 08:01:08.259 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-09-05 08:01:08.259 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-09-05 08:01:08.259 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-09-05 08:01:08.260 UTC [msp/identity] Sign -> DEBU 006 Sign: plaintext: 0ABE070A6608031A0B08C49ABEDC0510...696E766F6B650A01620A01610A023330
2018-09-05 08:01:08.260 UTC [msp/identity] Sign -> DEBU 007 Sign: digest: 84F388DE1B18BC84BC127430CD973ABE42B861918B53470F6AB56A9A1259311B
2018-09-05 08:01:08.281 UTC [msp/identity] Sign -> DEBU 008 Sign: plaintext: 0ABE070A6608031A0B08C49ABEDC0510...BFCB3536D3D984FA6FC3961DA83D3F52
2018-09-05 08:01:08.281 UTC [msp/identity] Sign -> DEBU 009 Sign: digest: DDFB1A52F36C46B313D2DB749285AC3825988F97CBB1A953932C7AD92A38CC6F
2018-09-05 08:01:08.297 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> DEBU 00a ESCC invoke result: version:1 response:<status:200 message:"OK" > payload:"\n f\217\332~\0052u\353[\355\001\3002\013g\251E\311\037\220\316\"\020Z\216\305E\266w\271\004\371\022Z\nF\022\024\n\004lscc\022\014\n\n\n\004mycc\022\002\010\003\022.\n\004mycc\022&\n\007\n\001a\022\002\010\004\n\007\n\001b\022\002\010\004\032\010\n\001a\032\003120\032\010\n\001b\032\003180\032\003\010\310\001\"\013\022\004mycc\032\0031.0" endorsement:<endorser:"\n\007Org1MSP\022\252\006-----BEGIN CERTIFICATE-----\nMIICKDCCAc+gAwIBAgIRANVk5L+uPGqiCXXsXvBtlXkwCgYIKoZIzj0EAwIwczEL\nMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBG\ncmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHDAaBgNVBAMTE2Nh\nLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgwOTA1MDc0OTE2WhcNMjgwOTAyMDc0OTE2\nWjBqMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMN\nU2FuIEZyYW5jaXNjbzENMAsGA1UECxMEcGVlcjEfMB0GA1UEAxMWcGVlcjAub3Jn\nMS5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABMTIUKdKHeMt\nY2rUJk85XXW7lJX1PaZG/kv+19XDybEHLVdkcs4qB35c4uwyhWb6eQ/Nk4F4TndW\nRSrp9jkx5rGjTTBLMA4GA1UdDwEB/wQEAwIHgDAMBgNVHRMBAf8EAjAAMCsGA1Ud\nIwQkMCKAIKa7ZPiaKjOvEWrEc5GR3LdUUfMzfdjdHR/cPn2/84WOMAoGCCqGSM49\nBAMCA0cAMEQCIGiN9fRDQMC5JCGQN34fMmGD3zx022KNxio4qtTshG4jAiAf6nyp\nql0vSV48/s1DRqdMxAg5aPaguVsJYPMUjXQPrg==\n-----END CERTIFICATE-----\n" signature:"0D\002 \020T%\317K\020(Q\237Q\223D\030\203a\003\231>\357bB\222\217\336\251\023\311\026v\370\371n\002 R\010\363\"\262\251\353R8\355\252\217\003J\034~\277\31356\323\331\204\372o\303\226\035\250=?R" >
2018-09-05 08:01:08.297 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 00b Chaincode invoke successful. result: status:200
2018-09-05 08:01:08.297 UTC [main] main -> INFO 00c Exiting.....

返回成功,再查询一下a账户和b账户的余额:

1
2
3
4
5
6
7
8
9
10
root@e01087f038ce:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
2018-09-05 08:02:28.774 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-09-05 08:02:28.774 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-09-05 08:02:28.774 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-09-05 08:02:28.774 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-09-05 08:02:28.774 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-09-05 08:02:28.775 UTC [msp/identity] Sign -> DEBU 006 Sign: plaintext: 0ABF070A6708031A0C08949BBEDC0510...6D7963631A0A0A0571756572790A0161
2018-09-05 08:02:28.775 UTC [msp/identity] Sign -> DEBU 007 Sign: digest: 7E30FE5ADFBFC13D700107349BCE9C34EED0E951EA54D67A1D6235C55CEE0C1A
Query Result: 120
2018-09-05 08:02:28.791 UTC [main] main -> INFO 008 Exiting.....

1
2
3
4
5
6
7
8
9
10
root@e01087f038ce:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'
2018-09-05 08:04:09.532 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-09-05 08:04:09.532 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-09-05 08:04:09.533 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-09-05 08:04:09.533 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-09-05 08:04:09.534 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-09-05 08:04:09.534 UTC [msp/identity] Sign -> DEBU 006 Sign: plaintext: 0ABF070A6708031A0C08F99BBEDC0510...6D7963631A0A0A0571756572790A0162
2018-09-05 08:04:09.535 UTC [msp/identity] Sign -> DEBU 007 Sign: digest: B2BC423CCBFF62B0A754FA69B3C21BB32DD42EEA92D9F1CB4533C4207F7DB3FD
Query Result: 180
2018-09-05 08:04:09.555 UTC [main] main -> INFO 008 Exiting.....

a账号剩余120,b账号剩余180元,结果正确

12. 关闭fabric网络,结束测试

先从cli节点的bash退出到宿主系统下

exit

关闭fabric网络:

1
2
cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/
./network_setup.sh down