一、准备阶段
1、系统软件源改为 aliyun
步骤:setting -> system settings -> Software&Updates -> Download from -> China -> aliyun
结果如下:
2、必备软件安装
1 | 安装vim sudo apt-get install vim |
二、环境准备
安装go1.11 以及以上版本
安装docker 18.09.3-ce 以及以上版本
安装docker-compose 1.23.2 以及以上版本
git 拉下 fabric-sample 项目
下载docker image镜像
1、go安装
下载并安装
1
2
3
4cd ~
wget https://studygolang.com/dl/golang/go1.11.linux-amd64.tar.gz #下载
tar -xzf go1.11.linux-amd64.tar.gz #解压
sudo mv go /usr/local #移动路径配置
1
2
3
4export PATH=$PATH:/usr/local/go/bin #环境变量
export GOROOT=/usr/local/go #安装根路径
export GOPATH=$HOME/go #为后续安装fabric作准备
export PATH=$PATH:$HOME/go/bin #环境变量创建go目录
1
2
3cd ~
mkdir -p go/src/github.com/hyperledger/fabric #fabric安装目录
sudo chmod -R 777 go2、docker安装(使用docker仓库)
卸载旧版本docker,未安装docker可忽略此步
1
sudo apt-get remove docker docker-engine docker.io
更新apt-get
1
sudo apt-get update
下载安装工具
1
2
3
4sudo apt-get install \
apt-transport-https \
ca-certificates \
software-properties-common添加官方密钥
1
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
将加入apt仓库
1
2
3
4sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"下载docker-ce
1
2sudo apt-get update
sudo apt-get install docker-ce完成上面操作之后输入
1
docker version
出现下面内容:
Client: Version: 18.09.3 API version: 1.39 Go version: go1.10.8 Git commit: 774a1f4 Built: Thu Feb 28 06:40:58 2019 OS/Arch: linux/amd64 Experimental: false Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/version: dial unix /var/run/docker.sock: connect: permission denied
此时已经安装好Docker了,但是还需要一些小操作。因为普通用户没有权限 。
1
2sudo groupadd docker #创建docker组
sudo usermod -aG docker #XXX(XXX是当前用户名)下面添加阿里云的Docker镜像:
1
2sudo mkdir -p /etc/docker #创建docker目录
sudo vim /etc/docker/daemon.json将以下内容写入daemon.json
1
2
3{
"registry-mirrors": ["https://obou6wyb.mirror.aliyuncs.com"]
}接下来输入:
1
2
3sudo systemctl daemon-reload
sudo systemctl restart docker
docker version如果还有权限不足提示,输入:
1
sudo chmod -R 777 /var/run/docker.sock
正确的结果:
Client: Version: 18.09.3 API version: 1.39 Go version: go1.10.8 Git commit: 774a1f4 Built: Thu Feb 28 06:40:58 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 18.09.3 API version: 1.39 (minimum version 1.12) Go version: go1.10.8 Git commit: 774a1f4 Built: Thu Feb 28 05:59:55 2019 OS/Arch: linux/amd64 Experimental: false
到此docker安装完毕
3、docker-compose安装
安装pip
1
2sudo apt-get install python-pip
sudo pip install docker-compose查看docker-compose版本
1
docker-compose -version
出现
1
docker-compose version 1.23.2, build 1110ad0
安装成功!
4、fabric-sample安装
git下载
1
2
3cd go/src/github.com/hyperledger/fabric/
sudo git clone https://github.com/hyperledger/fabric-samples.git
sudo chmod -R 777 fabric-sample #修改fabric-sample权限5、下载docker镜像
进入fabric目录,输入:
1
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 1.4.0
下载等待时间稍久,完成后输入:
1
docker images
出现以下镜像,则说明下载成功!
三、构建第一个fabric网络
1、进入fabric-sample/first-network中
1
cd fabric-samples/first-network/
2、生成各种配置文件及工具
1
./byfn.sh -m generate -c mytest1 #mytest为channel名称
成功后显示:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
+ res=0
+ set +x
/home/kangel/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/../bin/configtxgen
##########################################################
######### Generating Orderer Genesis block ##############
##########################################################
CONSENSUS_TYPE=solo
+ '[' solo == solo ']'
+ configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
2019-03-13 17:02:39.913 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-03-13 17:02:40.017 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2019-03-13 17:02:40.018 CST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /home/kangel/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2019-03-13 17:02:40.101 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2019-03-13 17:02:40.103 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /home/kangel/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2019-03-13 17:02:40.108 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2019-03-13 17:02:40.109 CST [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block
+ res=0
+ set +x
#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mytest1
2019-03-13 17:02:40.206 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-03-13 17:02:40.295 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/kangel/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2019-03-13 17:02:40.371 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-03-13 17:02:40.371 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/kangel/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2019-03-13 17:02:40.372 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2019-03-13 17:02:40.391 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 006 Writing new channel tx
+ res=0
+ set +x
#################################################################
####### Generating anchor peer update for Org1MSP ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mytest1 -asOrg Org1MSP
2019-03-13 17:02:40.468 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-03-13 17:02:40.539 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/kangel/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2019-03-13 17:02:40.609 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-03-13 17:02:40.609 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/kangel/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2019-03-13 17:02:40.609 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-03-13 17:02:40.609 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x
#################################################################
####### Generating anchor peer update for Org2MSP ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mytest1 -asOrg Org2MSP
2019-03-13 17:02:40.696 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-03-13 17:02:40.773 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/kangel/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2019-03-13 17:02:40.846 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-03-13 17:02:40.846 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/kangel/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2019-03-13 17:02:40.846 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-03-13 17:02:40.847 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x3、启动网络
1
./byfn.sh -m up -c mytest1
成功后显示:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144Creating network "net_byfn" with the default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating volume "net_peer1.org2.example.com" with default driver
Creating volume "net_peer1.org1.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_orderer.example.com" with default driver
Creating peer1.org2.example.com ... done
Creating peer0.org2.example.com ... done
Creating peer1.org1.example.com ... done
Creating peer0.org1.example.com ... done
Creating orderer.example.com ... done
Creating cli ... done
____ _____ _ ____ _____
/ ___| |_ _| / \ | _ \ |_ _|
\___ \ | | / _ \ | |_) | | |
___) | | | / ___ \ | _ < | |
|____/ |_| /_/ \_\ |_| \_\ |_|
Build your first network (BYFN) end-to-end test
+ peer channel create -o orderer.example.com:7050 -c mytest1 -f ./channel-artifacts/channel.tx --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
Channel name : mytest1
Creating channel...
+ res=0
+ set +x
2019-03-13 09:03:35.602 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-13 09:03:35.801 UTC [cli.common] readBlock -> INFO 002 Received block: 0
===================== Channel 'mytest1' created =====================
Having all peers join the channel...
+ peer channel join -b mytest1.block
+ res=0
+ set +x
2019-03-13 09:03:36.041 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-13 09:03:36.177 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer0.org1 joined channel 'mytest1' =====================
+ peer channel join -b mytest1.block
+ res=0
+ set +x
2019-03-13 09:03:39.396 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-13 09:03:39.473 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer1.org1 joined channel 'mytest1' =====================
+ peer channel join -b mytest1.block
+ res=0
+ set +x
2019-03-13 09:03:42.656 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-13 09:03:42.729 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer0.org2 joined channel 'mytest1' =====================
+ peer channel join -b mytest1.block
+ res=0
+ set +x
2019-03-13 09:03:45.890 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-13 09:03:45.990 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer1.org2 joined channel 'mytest1' =====================
Updating anchor peers for org1...
+ peer channel update -o orderer.example.com:7050 -c mytest1 -f ./channel-artifacts/Org1MSPanchors.tx --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
+ res=0
+ set +x
2019-03-13 09:03:49.159 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-13 09:03:49.208 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
===================== Anchor peers updated for org 'Org1MSP' on channel 'mytest1' =====================
Updating anchor peers for org2...
+ peer channel update -o orderer.example.com:7050 -c mytest1 -f ./channel-artifacts/Org2MSPanchors.tx --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
+ res=0
+ set +x
2019-03-13 09:03:52.409 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-13 09:03:52.441 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
===================== Anchor peers updated for org 'Org2MSP' on channel 'mytest1' =====================
Installing chaincode on peer0.org1...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2019-03-13 09:03:55.668 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-03-13 09:03:55.668 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-03-13 09:03:58.029 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
===================== Chaincode is installed on peer0.org1 =====================
Install chaincode on peer0.org2...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2019-03-13 09:03:58.241 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-03-13 09:03:58.241 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-03-13 09:03:58.597 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
===================== Chaincode is installed on peer0.org2 =====================
Instantiating chaincode on peer0.org2...
+ peer chaincode instantiate -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 mytest1 -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')'
+ res=0
+ set +x
2019-03-13 09:03:58.812 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-03-13 09:03:58.812 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
===================== Chaincode is instantiated on peer0.org2 on channel 'mytest1' =====================
Querying chaincode on peer0.org1...
===================== Querying on peer0.org1 on channel 'mytest1'... =====================
Attempting to Query peer0.org1 ...3 secs
+ peer chaincode query -C mytest1 -n mycc -c '{"Args":["query","a"]}'
+ res=0
+ set +x
100
===================== Query successful on peer0.org1 on channel 'mytest1' =====================
Sending invoke transaction on peer0.org1 peer0.org2...
+ 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 mytest1 -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'
+ res=0
+ set +x
2019-03-13 09:06:36.914 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mytest1' =====================
Installing chaincode on peer1.org2...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2019-03-13 09:06:37.117 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-03-13 09:06:37.117 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-03-13 09:06:37.506 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
===================== Chaincode is installed on peer1.org2 =====================
Querying chaincode on peer1.org2...
===================== Querying on peer1.org2 on channel 'mytest1'... =====================
Attempting to Query peer1.org2 ...3 secs
+ peer chaincode query -C mytest1 -n mycc -c '{"Args":["query","a"]}'
+ res=0
+ set +x
90
===================== Query successful on peer1.org2 on channel 'mytest1' =====================
========= All GOOD, BYFN execution completed ===========
_____ _ _ ____
| ____| | \ | | | _ \
| _| | \| | | | | |
| |___ | |\ | | |_| |
|_____| |_| \_| |____/4、关闭网络
1
./byfn.sh -m up -c mytest1
成功后显示:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40Stopping cli ... done
Stopping peer0.org1.example.com ... done
Stopping orderer.example.com ... done
Stopping peer0.org2.example.com ... done
Stopping peer1.org1.example.com ... done
Stopping peer1.org2.example.com ... done
Removing cli ... done
Removing peer0.org1.example.com ... done
Removing orderer.example.com ... done
Removing peer0.org2.example.com ... done
Removing peer1.org1.example.com ... done
Removing peer1.org2.example.com ... done
Removing network net_byfn
Removing volume net_peer0.org3.example.com
WARNING: Volume net_peer0.org3.example.com not found.
Removing volume net_peer1.org3.example.com
WARNING: Volume net_peer1.org3.example.com not found.
Removing volume net_orderer.example.com
Removing volume net_peer0.org2.example.com
Removing volume net_peer0.org1.example.com
Removing volume net_peer1.org1.example.com
Removing volume net_peer1.org2.example.com
bf5553092b5a
0198acc0e83e
5c0dad0e5b89
Untagged: dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab:latest
Deleted: sha256:b550da58d9213e0d82d7f362212eb84f8bffa0d7751cdfcbaf3258ef75c3f24a
Deleted: sha256:d7dced24e5ba3373a3c3bd736be0a351481a21df9209503a3fa93635e4aa7f46
Deleted: sha256:36739293e8852c3feb37dd3e08eb20f3004494d8eec3cba05c134454178323b1
Deleted: sha256:7190f5476edd042aa64c39fcf7909f194bdc10e85e02ee1cecf21add3756d8a1
Untagged: dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9:latest
Deleted: sha256:4db24b70e9fbd10b3b20b6255011d45ac7fb92a7ee9731b7f8cc6e681e1a0eb6
Deleted: sha256:38880d76d92c07ca6477ab3a899d21f00f76801f05b4adb3aba58d5bcf224bde
Deleted: sha256:23a054642ba1b7f84991df2611f22b2a0894ea81676ecc9bc124f50832d04f12
Deleted: sha256:f1811e1e4784e9e4537e98ba4a1bb86e56ee070ea443a026dc61c1375d6f8c30
Untagged: dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b:latest
Deleted: sha256:0c1d56951ae35a8d9db90dc3decda420ca2cf0fcbaa8ccbc4aa580de9fdd58f0
Deleted: sha256:4ccfcafb3212b0448c0a106f25f91c59c56d709fd374658d814ba853bc315991
Deleted: sha256:51b7b0c6e633a94040cb799596a52a13480b3b90819594d5f5c5f17b1ecdc632
Deleted: sha256:11473124d5b1ddfe7136f913791002059bbe8d466e2a931a87caf50e6166996f