fabric-samples基本使用(3)- 合约部署及操作

前面两个章节,已经搭建好了一个基本的fabric测试网络,在已有基础上,本文在其中加入智能合约,也就是chaincode,如此一条用于测试的fabric联盟链就算完整了。后续就可以在该测试链基础上研究fabric的具体细节了。

前述

  1. 请先看fabric-samples基本使用(1)- 下载准备以及fabric-samples基本使用(2)- 启动测试网络
  2. chaincode目前支持goalng、java来实现,考虑到技术栈和远期规划,这里我还是以golang为主,本文测试网络将会调用官方提供的demo合约。

使用方式

先确保网络已经正常启动

1. 发布合约

1
2
# 这里使用官方测试案例fabcar
./network.sh deployCC -ccn fabcar -ccp ../chaincode/fabcar/go/ -ccl go

在官方测试网络提供的脚本中,只有这一条命令即可发布一条合约,早前版本的脚本,如果不传入参数,会默认调用官方的fabCar合约来发布,但当前最新版脚本强制要求指定合约,
应该是为了让脚本更加通用。
这里强制要求最少传入三个参数:合约名称 -ccn合约路径 -ccp合约开发语言 -ccl,其余可选参数,如策略等,若不传入,则使用官方提供的默认值。

2. 合约发布期间产生的日志

在fabric2.0,对合约的生命周期有了更细的划分,以下是fabcar合约发布整个流程的日志(无删减),通过日志可以很快了解一个合约发布需要涉及到的主要步骤,这里每个步骤我在其中加入了描述

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
1. 合约发布的环境描述,在'mychannel'中发布
deploying chaincode on channel 'mychannel'
executing with the following
- CHANNEL_NAME: mychannel
- CC_NAME: fabcar
- CC_SRC_PATH: ../chaincode/fabcar/go/
- CC_SRC_LANGUAGE: go
- CC_VERSION: 1.0
- CC_SEQUENCE: 1
- CC_END_POLICY: NA
- CC_COLL_CONFIG: NA
- CC_INIT_FCN: NA
- DELAY: 3
- MAX_RETRY: 5
- VERBOSE: false

2. 根据合约代码中go.mod描述的依赖,下载相关依赖
Vendoring Go dependencies at ../chaincode/fabcar/go/
~/go/src/github.com/hyperledger/fabric-samples/chaincode/fabcar/go ~/go/src/github.com/hyperledger/fabric-samples/test-network
go: downloading github.com/hyperledger/fabric-contract-api-go v1.1.0
go: downloading github.com/hyperledger/fabric-chaincode-go v0.0.0-20200424173110-d7076418f212
go: downloading github.com/hyperledger/fabric-protos-go v0.0.0-20200424173316-dd554ba3746e
go: downloading github.com/golang/protobuf v1.3.2
go: downloading google.golang.org/grpc v1.23.0
go: downloading github.com/xeipuuv/gojsonschema v1.2.0
go: downloading github.com/go-openapi/spec v0.19.4
go: downloading github.com/gobuffalo/packr v1.30.1
go: downloading github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415
go: downloading github.com/go-openapi/jsonpointer v0.19.3
go: downloading github.com/go-openapi/jsonreference v0.19.2
go: downloading github.com/go-openapi/swag v0.19.5
go: downloading github.com/gobuffalo/envy v1.7.0
go: downloading github.com/gobuffalo/packd v0.3.0
go: downloading golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
go: downloading google.golang.org/genproto v0.0.0-20180831171423-11092d34479b
go: downloading github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f
go: downloading github.com/PuerkitoBio/purell v1.1.1
go: downloading github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/joho/godotenv v1.3.0
go: downloading github.com/rogpeppe/go-internal v1.3.0
go: downloading golang.org/x/sys v0.0.0-20190710143415-6ec70d6a5542
go: downloading github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578
go: downloading golang.org/x/text v0.3.2
~/go/src/github.com/hyperledger/fabric-samples/test-network
Finished vendoring Go dependencies

3. 打包合约,将合约打包为*.tar.gz压缩包,里面是包含合约源码和上一步骤下载的依赖
+ peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label fabcar_1.0
+ res=0
Chaincode is packaged

4-1. 为org1的peer0安装合约
Installing chaincode on peer0.org1...
Using organization 1
+ peer lifecycle chaincode install fabcar.tar.gz
+ res=0
2021-04-22 12:37:21.229 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nKfabcar_1.0:b41ab9af76a078ae8af2b9897c51f6a0c401e4c6c777414d766698b1b7fac00a\022\nfabcar_1.0" >
2021-04-22 12:37:21.229 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: fabcar_1.0:b41ab9af76a078ae8af2b9897c51f6a0c401e4c6c777414d766698b1b7fac00a
Chaincode is installed on peer0.org1

4-2. 为org2的peer0安装合约
Install chaincode on peer0.org2...
Using organization 2
+ peer lifecycle chaincode install fabcar.tar.gz
+ res=0
2021-04-22 12:37:43.834 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nKfabcar_1.0:b41ab9af76a078ae8af2b9897c51f6a0c401e4c6c777414d766698b1b7fac00a\022\nfabcar_1.0" >
2021-04-22 12:37:43.850 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: fabcar_1.0:b41ab9af76a078ae8af2b9897c51f6a0c401e4c6c777414d766698b1b7fac00a
Chaincode is installed on peer0.org2

5. 检查org1的peer0安装了哪些合约,这里可以看出成功安装了fabcar_1.0
Using organization 1
+ peer lifecycle chaincode queryinstalled
+ res=0
Installed chaincodes on peer:
Package ID: fabcar_1.0:b41ab9af76a078ae8af2b9897c51f6a0c401e4c6c777414d766698b1b7fac00a, Label: fabcar_1.0
Query installed successful on peer0.org1 on channel

6-1. 在org1的peer0授权合约,就是说合约规则默认要求多个组织确认后,才可以上链,可以理解为多签。
Using organization 1
+ peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /root/go/src/github.com/hyperledger/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID mychannel --name fabcar --version 1.0 --package-id fabcar_1.0:b41ab9af76a078ae8af2b9897c51f6a0c401e4c6c777414d766698b1b7fac00a --sequence 1
+ res=0
2021-04-22 12:37:46.658 CST [chaincodeCmd] ClientWait -> INFO 001 txid [cbc8814b90142b71153fba5cd3aa276b25365d43bfbc07ec05f573094889933a] committed with status (VALID) at localhost:7051
Chaincode definition approved on peer0.org1 on channel 'mychannel'

6-2. org1的peer0授权合约后,在org1的peer0检查合约在各组织的授权是否成功
Using organization 1
Checking the commit readiness of the chaincode definition on peer0.org1 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1.0 --sequence 1 --output json
+ res=0
{
"approvals": {
"Org1MSP": true,
"Org2MSP": false
}
}
Checking the commit readiness of the chaincode definition successful on peer0.org1 on channel 'mychannel'

6-3. org1的peer0授权合约后,在org2的peer0检查合约在各组织的授权是否成功
Using organization 2
Checking the commit readiness of the chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1.0 --sequence 1 --output json
+ res=0
{
"approvals": {
"Org1MSP": true,
"Org2MSP": false
}
}
Checking the commit readiness of the chaincode definition successful on peer0.org2 on channel 'mychannel'

6-4. 在org2的peer0授权合约,就是说合约规则默认要求多个组织确认后,才可以上链,可以理解为多签。
Using organization 2
+ peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /root/go/src/github.com/hyperledger/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID mychannel --name fabcar --version 1.0 --package-id fabcar_1.0:b41ab9af76a078ae8af2b9897c51f6a0c401e4c6c777414d766698b1b7fac00a --sequence 1
+ res=0
2021-04-22 12:37:55.389 CST [chaincodeCmd] ClientWait -> INFO 001 txid [42c8e7326c69c7859d77ccdb9509d71ff3a3fbeeb98619972c3633b9dfff338e] committed with status (VALID) at localhost:9051
Chaincode definition approved on peer0.org2 on channel 'mychannel'

6-5. org2的peer0授权合约后,在org1的peer0检查合约在各组织的授权是否成功
Using organization 1
Checking the commit readiness of the chaincode definition on peer0.org1 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1.0 --sequence 1 --output json
+ res=0
{
"approvals": {
"Org1MSP": true,
"Org2MSP": true
}
}
Checking the commit readiness of the chaincode definition successful on peer0.org1 on channel 'mychannel'

6-6. org2的peer0授权合约后,在org2的peer0检查合约在各组织的授权是否成功
Using organization 2
Checking the commit readiness of the chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1.0 --sequence 1 --output json
+ res=0
{
"approvals": {
"Org1MSP": true,
"Org2MSP": true
}
}
Checking the commit readiness of the chaincode definition successful on peer0.org2 on channel 'mychannel'

7. 前面的合约已安装、授权(多签)完毕,这里开始在org1的peer0和org2的peer0提交定义合约,可以理解为初始化出一个合约模板,这也是fabric2.0的一个特性,同一个合约可以重复使用
Using organization 1
Using organization 2
+ peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /root/go/src/github.com/hyperledger/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID mychannel --name fabcar --peerAddresses localhost:7051 --tlsRootCertFiles /root/go/src/github.com/hyperledger/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles /root/go/src/github.com/hyperledger/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt --version 1.0 --sequence 1
+ res=0
2021-04-22 12:38:04.422 CST [chaincodeCmd] ClientWait -> INFO 001 txid [abfb67fbeaff552237a982020350063d98ad9620ad0a72e1505aa650fd55eb2c] committed with status (VALID) at localhost:9051
2021-04-22 12:38:04.422 CST [chaincodeCmd] ClientWait -> INFO 002 txid [abfb67fbeaff552237a982020350063d98ad9620ad0a72e1505aa650fd55eb2c] committed with status (VALID) at localhost:7051
Chaincode definition committed on channel 'mychannel'

8-1 在org1的peer0检查合约定义成功与否
Using organization 1
Querying chaincode definition on peer0.org1 on channel 'mychannel'...
Attempting to Query committed status on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID mychannel --name fabcar
+ res=0
Committed chaincode definition for chaincode 'fabcar' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org1 on channel 'mychannel'

8-2 在org2的peer0检查合约定义成功与否
Using organization 2
Querying chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to Query committed status on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID mychannel --name fabcar
+ res=0
Committed chaincode definition for chaincode 'fabcar' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org2 on channel 'mychannel'

8-3 如果脚本传入了初始化方法,则调用合约初始化函数,如果没传入,则不执行
Chaincode initialization is not required

总结

至此,《fabric-samples基本使用》三部曲已经完成,走完整个流程,对fabric整体会有一个大概的了解,明显能感觉到,联盟链对理念和架构明显比公链复杂很多。
当然,fabric-samples内容远远不止于此,如果有时间,建议最好详细读一下整套脚本,配合官方文档,先对fabric有个全局观。
后续将会梳理一些fabric偏原理、架构类的内容,逐步深入。

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2017-2023 Jason
  • Visitors: | Views:

谢谢打赏~

微信