Fabric网络搭建流程

本项目基于Fabric2.2进行搭建,暂时不使用CA二用crytogen生成证书文件。

假设我们项目的home是/data/twonodes

目录

  • 配置网络
  • 通道配置
  • 部署节点
  • 节点与通道
  • 链码的安装与执行

配置网络

配置网络主要是由CA负责,基于crypto-config.yaml文件生成orderer和peer节点的证书文件

由于本项目暂时还不采用CA搭建,所以使用官方给定的crytogen命令生成证书文件。

预备条件,参考Fabric测试网络的搭建

  1. 使用cryptogen showtemplate > crypto_config.yaml生成生成密钥材料模板

    其内容如下:

    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
    # ---------------------------------------------------------------------------
    # "OrdererOrgs" - 管理orderer节点的组织的定义
    # ---------------------------------------------------------------------------
    OrdererOrgs:
    # ---------------------------------------------------------------------------
    # Orderer
    # ---------------------------------------------------------------------------
    - Name: Orderer
    Domain: example.com # 域
    EnableNodeOUs: true # 是否开启部门结构,如:组织-部门-节点

    # ---------------------------------------------------------------------------
    # "Specs" - See PeerOrgs below for complete description
    # ---------------------------------------------------------------------------
    Specs:
    - Hostname: orderer

    # ---------------------------------------------------------------------------
    # "PeerOrgs" - 管理peer节点的组织的定义
    # ---------------------------------------------------------------------------
    PeerOrgs:
    # ---------------------------------------------------------------------------
    # Org1
    # ---------------------------------------------------------------------------
    - Name: Org1
    Domain: org1.example.com
    EnableNodeOUs: true

    # ---------------------------------------------------------------------------
    # "CA"
    # ---------------------------------------------------------------------------
    # Uncomment this section to enable the explicit definition of the CA for this
    # organization. This entry is a Spec. See "Specs" section below for details.
    # ---------------------------------------------------------------------------
    # CA:
    # Hostname: ca # implicitly ca.org1.example.com
    # Country: US
    # Province: California
    # Locality: San Francisco
    # OrganizationalUnit: Hyperledger Fabric
    # StreetAddress: address for org # default nil
    # PostalCode: postalCode for org # default nil

    # ---------------------------------------------------------------------------
    # "Specs"
    # ---------------------------------------------------------------------------
    # Uncomment this section to enable the explicit definition of hosts in your
    # configuration. Most users will want to use Template, below
    #
    # Specs is an array of Spec entries. Each Spec entry consists of two fields:
    # - Hostname: (Required) The desired hostname, sans the domain.
    # - CommonName: (Optional) Specifies the template or explicit override for
    # the CN. By default, this is the template:
    #
    # "{{.Hostname}}.{{.Domain}}"
    #
    # which obtains its values from the Spec.Hostname and
    # Org.Domain, respectively.
    # - SANS: (Optional) Specifies one or more Subject Alternative Names
    # to be set in the resulting x509. Accepts template
    # variables {{.Hostname}}, {{.Domain}}, {{.CommonName}}. IP
    # addresses provided here will be properly recognized. Other
    # values will be taken as DNS names.
    # NOTE: Two implicit entries are created for you:
    # - {{ .CommonName }}
    # - {{ .Hostname }}
    # ---------------------------------------------------------------------------
    # Specs:
    # - Hostname: foo # implicitly "foo.org1.example.com"
    # CommonName: foo27.org5.example.com # overrides Hostname-based FQDN set above
    # SANS:
    # - "bar.{{.Domain}}"
    # - "altfoo.{{.Domain}}"
    # - "{{.Hostname}}.org6.net"
    # - 172.16.10.31
    # - Hostname: bar
    # - Hostname: baz

    # -------------------------------------------------------------------------
    # "Template"
    # -------------------------------------------------------------------------
    # 允许定义一个或多个按顺序创建的主机 (组织节点的生成模板)
    # 默认情况下,这看起来像“peer%d”,从0到Count-1。
    # 您可以覆盖节点数(Count)、起始索引(Start)
    # 或用于构造名称(主机名)的模板。
    # 注:模板和规范不是互斥的。你可以定义两者
    # 将为您创建节和聚合节点。小心名称冲突
    # -------------------------------------------------------------------------
    Template:
    Count: 1 #一个组织生成count个节点
    # Start: 5
    # Hostname: {{.Prefix}}{{.Index}} # default
    # SANS:
    # - "{{.Hostname}}.alt.{{.Domain}}"

    # ---------------------------------------------------------------------------
    # "Users"
    # ---------------------------------------------------------------------------
    # Count: 除Admin之外的用户帐户数
    # ---------------------------------------------------------------------------
    Users: # 组织中实际操作人员的个数
    Count: 1

    # ---------------------------------------------------------------------------
    # Org2: 参考组织1的配置
    # ---------------------------------------------------------------------------
    - Name: Org2
    Domain: org2.example.com
    EnableNodeOUs: true
    Template:
    Count: 1
    Users:
    Count: 1
  2. 使用cryptogen generate --config=crypto_config.yaml 指定使用自定义的配置文件在crypto-config文件夹下生成order和peer的证书文件

通道配置

通过构建通道创建交易并将交易提交给排序服务来创建通道。通道创建交易指定通道的初始配置,并由排序服务用于写入通道创世块。使用configtxgen工具读取定义通道配置的configtx.yaml文件,然后将相关信息写入通道创建交易中来工作。

需要注意的是Fabric2.2和2.3的 configtx.yaml 的配置不同!!!详情参考

  1. copy 官方实例的 configtx.yaml

  2. 修改为2.2版本的样子

  3. 修改证书路径为刚才生成的路径

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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

---
################################################################################
#
# Section: Organizations
#
# - This section defines the different organizational identities which will
# be referenced later in the configuration.
#
################################################################################
Organizations:

# SampleOrg defines an MSP using the sampleconfig. It should never be used
# in production but may be used as a template for other definitions
- &OrdererOrg
# DefaultOrg defines the organization which is used in the sampleconfig
# of the fabric.git development environment
Name: OrdererOrg

# ID to load the MSP definition as
ID: OrdererMSP

# MSPDir is the filesystem path which contains the MSP configuration
MSPDir: crypto-config/ordererOrganizations/example.com/msp

# Policies defines the set of policies at this level of the config tree
# For organization policies, their canonical path is usually
# /Channel/<Application|Orderer>/<OrgName>/<PolicyName>
Policies:
Readers:
Type: Signature
Rule: "OR('OrdererMSP.member')"
Writers:
Type: Signature
Rule: "OR('OrdererMSP.member')"
Admins:
Type: Signature
Rule: "OR('OrdererMSP.admin')"

OrdererEndpoints:
- orderer.example.com:7050

- &Org1
# DefaultOrg defines the organization which is used in the sampleconfig
# of the fabric.git development environment
Name: Org1MSP

# ID to load the MSP definition as
ID: Org1MSP

MSPDir: crypto-config/peerOrganizations/org1.example.com/msp

# 每个peer节点 配属一个锚节点
AnchorPeers:
# AnchorPeers defines the location of peers which can be used
# for cross org gossip communication. Note, this value is only
# encoded in the genesis block in the Application section context
- Host: peer0.org1.example.com
Port: 7051

# Policies defines the set of policies at this level of the config tree
# For organization policies, their canonical path is usually
# /Channel/<Application|Orderer>/<OrgName>/<PolicyName>
Policies:
Readers:
Type: Signature
Rule: "OR('Org1MSP.admin', 'Org1MSP.peer', 'Org1MSP.client')"
Writers:
Type: Signature
Rule: "OR('Org1MSP.admin', 'Org1MSP.client')"
Admins:
Type: Signature
Rule: "OR('Org1MSP.admin')"
Endorsement:
Type: Signature
Rule: "OR('Org1MSP.peer')"

- &Org2
# DefaultOrg defines the organization which is used in the sampleconfig
# of the fabric.git development environment
Name: Org2MSP

# ID to load the MSP definition as
ID: Org2MSP

MSPDir: crypto-config/peerOrganizations/org2.example.com/msp

# 每个peer节点 配属一个锚节点
AnchorPeers:
# AnchorPeers defines the location of peers which can be used
# for cross org gossip communication. Note, this value is only
# encoded in the genesis block in the Application section context
- Host: peer0.org2.example.com
Port: 9051

# Policies defines the set of policies at this level of the config tree
# For organization policies, their canonical path is usually
# /Channel/<Application|Orderer>/<OrgName>/<PolicyName>
Policies:
Readers:
Type: Signature
Rule: "OR('Org2MSP.admin', 'Org2MSP.peer', 'Org2MSP.client')"
Writers:
Type: Signature
Rule: "OR('Org2MSP.admin', 'Org2MSP.client')"
Admins:
Type: Signature
Rule: "OR('Org2MSP.admin')"
Endorsement:
Type: Signature
Rule: "OR('Org2MSP.peer')"

################################################################################
#
# SECTION: Capabilities
#
# - This section defines the capabilities of fabric network. This is a new
# concept as of v1.1.0 and should not be utilized in mixed networks with
# v1.0.x peers and orderers. Capabilities define features which must be
# present in a fabric binary for that binary to safely participate in the
# fabric network. For instance, if a new MSP type is added, newer binaries
# might recognize and validate the signatures from this type, while older
# binaries without this support would be unable to validate those
# transactions. This could lead to different versions of the fabric binaries
# having different world states. Instead, defining a capability for a channel
# informs those binaries without this capability that they must cease
# processing transactions until they have been upgraded. For v1.0.x if any
# capabilities are defined (including a map with all capabilities turned off)
# then the v1.0.x peer will deliberately crash.
#
################################################################################
Capabilities:
# Channel capabilities apply to both the orderers and the peers and must be
# supported by both.
# Set the value of the capability to true to require it.
Channel: &ChannelCapabilities
# V2_0 capability ensures that orderers and peers behave according
# to v2.0 channel capabilities. Orderers and peers from
# prior releases would behave in an incompatible way, and are therefore
# not able to participate in channels at v2.0 capability.
# Prior to enabling V2.0 channel capabilities, ensure that all
# orderers and peers on a channel are at v2.0.0 or later.
V2_0: true

# Orderer capabilities apply only to the orderers, and may be safely
# used with prior release peers.
# Set the value of the capability to true to require it.
Orderer: &OrdererCapabilities
# V2_0 orderer capability ensures that orderers behave according
# to v2.0 orderer capabilities. Orderers from
# prior releases would behave in an incompatible way, and are therefore
# not able to participate in channels at v2.0 orderer capability.
# Prior to enabling V2.0 orderer capabilities, ensure that all
# orderers on channel are at v2.0.0 or later.
V2_0: true

# Application capabilities apply only to the peer network, and may be safely
# used with prior release orderers.
# Set the value of the capability to true to require it.
Application: &ApplicationCapabilities
# V2_0 application capability ensures that peers behave according
# to v2.0 application capabilities. Peers from
# prior releases would behave in an incompatible way, and are therefore
# not able to participate in channels at v2.0 application capability.
# Prior to enabling V2.0 application capabilities, ensure that all
# peers on channel are at v2.0.0 or later.
V2_0: true

################################################################################
#
# SECTION: Application
#
# - This section defines the values to encode into a config transaction or
# genesis block for application related parameters
#
################################################################################
Application: &ApplicationDefaults

# Organizations is the list of orgs which are defined as participants on
# the application side of the network
Organizations:

# Policies defines the set of policies at this level of the config tree
# For Application policies, their canonical path is
# /Channel/Application/<PolicyName>
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
LifecycleEndorsement:
Type: ImplicitMeta
Rule: "MAJORITY Endorsement"
Endorsement:
Type: ImplicitMeta
Rule: "MAJORITY Endorsement"

Capabilities:
<<: *ApplicationCapabilities
################################################################################
#
# SECTION: Orderer
#
# - This section defines the values to encode into a config transaction or
# genesis block for orderer related parameters
#
################################################################################
Orderer: &OrdererDefaults

# Orderer Type: The orderer implementation to start
OrdererType: etcdraft

# Addresses used to be the list of orderer addresses that clients and peers
# could connect to. However, this does not allow clients to associate orderer
# addresses and orderer organizations which can be useful for things such
# as TLS validation. The preferred way to specify orderer addresses is now
# to include the OrdererEndpoints item in your org definition
Addresses:
- orderer.example.com:7050

EtcdRaft:
Consenters:
- Host: orderer.example.com
Port: 7050
ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.crt
ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.crt

# Batch Timeout: The amount of time to wait before creating a batch
BatchTimeout: 2s

# Batch Size: Controls the number of messages batched into a block
BatchSize:

# Max Message Count: The maximum number of messages to permit in a batch
MaxMessageCount: 10

# Absolute Max Bytes: The absolute maximum number of bytes allowed for
# the serialized messages in a batch.
AbsoluteMaxBytes: 99 MB

# Preferred Max Bytes: The preferred maximum number of bytes allowed for
# the serialized messages in a batch. A message larger than the preferred
# max bytes will result in a batch larger than preferred max bytes.
PreferredMaxBytes: 512 KB

# Organizations is the list of orgs which are defined as participants on
# the orderer side of the network
Organizations:

# Policies defines the set of policies at this level of the config tree
# For Orderer policies, their canonical path is
# /Channel/Orderer/<PolicyName>
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
# BlockValidation specifies what signatures must be included in the block
# from the orderer for the peer to validate it.
BlockValidation:
Type: ImplicitMeta
Rule: "ANY Writers"

################################################################################
#
# CHANNEL
#
# 本节定义要编码到配置事务或通道相关参数的genesis区块。
#
################################################################################
Channel: &ChannelDefaults
# Policies defines the set of policies at this level of the config tree
# For Channel policies, their canonical path is
# /Channel/<PolicyName>
Policies:
# Who may invoke the 'Deliver' API
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
# Who may invoke the 'Broadcast' API
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
# By default, who may modify elements at this config level
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"

# Capabilities describes the channel level capabilities, see the
# dedicated Capabilities section elsewhere in this file for a full
# description
Capabilities:
<<: *ChannelCapabilities

################################################################################
#
# Profile
#
# - 这里可以对不同的配置概要文件进行编码以进行指定,作为configtxgen工具的参数
#
################################################################################
Profiles:

TwoOrgsOrdererGenesis:
<<: *ChannelDefaults
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Capabilities:
<<: *OrdererCapabilities
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TwoOrgsChannel:
Consortium: SampleConsortium
<<: *ChannelDefaults
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
Capabilities:
<<: *ApplicationCapabilities
  1. 使用configtxgen 命令,基于 configtx.yaml 创建和查看通道配置相关构件。

    • 输出初始区块: 将通道 orderer-system-channel 和轮廓(Profile) TwoOrgsOrdererGenesis 的创世区块写入 genesis.block

      1
      configtxgen -outputBlock ./channel-artifacts/genesis.block -profile TwoOrgsOrdererGenesis -channelID orderer-system-channel
    • 输出创建通道的交易 将轮廓 TwoOrgsChannel 的通道创建交易写入 channel.tx

      1
      configtxgen -outputCreateChannelTx ./channel-artifacts/channel.tx -profile TwoOrgsChannel -channelID mychannel
    • 输出组织1和2的锚节点

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      configtxgen -outputAnchorPeersUpdate ./channel-artifacts/org1_anchor_peer.tx -profile TwoOrgsChannel -asOrg Org1MSP -channelID mychannel

      configtxgen -outputAnchorPeersUpdate ./channel-artifacts/org2_anchor_peer.tx -profile TwoOrgsChannel -asOrg Org2MSP -channelID mychannel

      若出错,Org1,即peer节点的配置少了下面的属性:
      # 每个peer节点 配属一个锚节点
      AnchorPeers:
      # AnchorPeers defines the location of peers which can be used
      # for cross org gossip communication. Note, this value is only
      # encoded in the genesis block in the Application section context
      - Host: peer0.org1.example.com
      Port: 7051
    • 查看创世区块 将通道创建交易 create_chan_tx.pb 以 JSON 的格式打印到屏幕上。

      1
      configtxgen -inspectBlock ./channel-artifacts/genesis.block
    • 查看创建通道的交易 将创世区块 channel.tx 以 JSON 格式打印到屏幕上。

      1
      configtxgen -inspectChannelCreateTx ./channel-artifacts/channel.tx
    • 打印组织定义 基于 configtx.yaml 的配置项(比如 MSPdir)来构建组织并以 JSON 格式打印到屏幕。(常用于创建通道时的重新配置,例如添加成员)

      1
      configtxgen -printOrg Org1MSP

部署节点

创建一个 peer 节点

在创建 peer 节点之前,你需要为 peer 节点定制配置文件core.yaml

方法一:直接启动

方法二:启动docker容器(推荐)

创建一个orderer 节点

在创建 orderer 节点之前,你需要为 orderer 节点定制配置文件orderer.yaml

启动docker容器(推荐)

基于docker-compose创建节点容器

参考Fabric-sample的官方配置

docker-compose.yaml文件如下:

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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

version: '2.2'

volumes:
orderer.example.com:
peer0.org1.example.com:
peer0.org2.example.com:

networks:
test:
name: twonodes_test # 自定义网络名

services:

orderer.example.com:
container_name: orderer.example.com
image: hyperledger/fabric-orderer:latest
environment:
- FABRIC_LOGGING_SPEC=INFO
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_LISTENPORT=7050
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
# enabled TLS
- ORDERER_GENERAL_TLS_ENABLED=true
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
- ORDERER_KAFKA_TOPIC_REPLICATIONFACTOR=1
- ORDERER_KAFKA_VERBOSE=true
- ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes: # 修改为自定义文件路径
- ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
- orderer.example.com:/var/hyperledger/production/orderer
ports:
- 7050:7050
networks:
- test

peer0.org1.example.com:
container_name: peer0.org1.example.com
image: hyperledger/fabric-peer:latest
environment:
#Generic peer variables
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# the following setting starts chaincode containers on the same
# bridge network as the peers
# https://docs.docker.com/compose/networking/
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=twonodes_test #项目名_xxx
- FABRIC_LOGGING_SPEC=INFO
#- FABRIC_LOGGING_SPEC=DEBUG
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
# Peer specific variabes
- CORE_PEER_ID=peer0.org1.example.com
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_LISTENADDRESS=0.0.0.0:7051
- CORE_PEER_CHAINCODEADDRESS=peer0.org1.example.com:7052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.example.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
volumes: # 修改为自定义文件路径
- /var/run/docker.sock:/host/var/run/docker.sock
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
- peer0.org1.example.com:/var/hyperledger/production
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
- 7051:7051
networks:
- test

peer0.org2.example.com:
container_name: peer0.org2.example.com
image: hyperledger/fabric-peer:latest
environment:
#Generic peer variables
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# the following setting starts chaincode containers on the same
# bridge network as the peers
# https://docs.docker.com/compose/networking/
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=twonodes_test #项目名_xxx
- FABRIC_LOGGING_SPEC=INFO
#- FABRIC_LOGGING_SPEC=DEBUG
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
# Peer specific variabes
- CORE_PEER_ID=peer0.org2.example.com
- CORE_PEER_ADDRESS=peer0.org2.example.com:9051
- CORE_PEER_LISTENADDRESS=0.0.0.0:9051
- CORE_PEER_CHAINCODEADDRESS=peer0.org2.example.com:9052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:9052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:9051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.example.com:9051
- CORE_PEER_LOCALMSPID=Org2MSP
volumes: # 修改为自定义文件路径
- /var/run/docker.sock:/host/var/run/docker.sock
- ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp:/etc/hyperledger/fabric/msp
- ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tls
- peer0.org2.example.com:/var/hyperledger/production
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
- 9051:9051
networks:
- test



# 将原有的cli分peer创建
cli1:
container_name: cli1
image: hyperledger/fabric-tools:latest
tty: true
stdin_open: true
#- FABRIC_LOGGING_SPEC=DEBUG
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- FABRIC_LOGGING_SPEC=INFO
# 参考Fabric 测试网络的command命令
# Environment variables for Org1
# export CORE_PEER_TLS_ENABLED=true
# export CORE_PEER_LOCALMSPID="Org1MSP"
# export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
# export CORE_PEER_ADDRESS=localhost:7051
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
command: /bin/bash
volumes: # 修改为映射路径
- /var/run/:/host/var/run/
- ./chaincode/go:/opt/gopath/src/github.com/hyperledger/fabric/fabric-cluster/chaincode/go #预留链码路径
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
networks:
- test



cli2:
container_name: cli2
image: hyperledger/fabric-tools:latest
tty: true
stdin_open: true
#- FABRIC_LOGGING_SPEC=DEBUG
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- FABRIC_LOGGING_SPEC=INFO
# 参考Fabric 测试网络的command命令
# Environment variables for Org1
# export CORE_PEER_TLS_ENABLED=true
# export CORE_PEER_LOCALMSPID="Org1MSP"
# export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
# export CORE_PEER_ADDRESS=localhost:7051
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_LOCALMSPID=Org2MSP
- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
- CORE_PEER_ADDRESS=peer0.org2.example.com:9051 # 注意上面的配置和端口与实际路径对应


command: /bin/bash
volumes: # 修改为映射路径
- /var/run/:/host/var/run/
- ./chaincode/go:/opt/gopath/src/github.com/hyperledger/fabric/fabric-cluster/chaincode/go
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
networks:
- test

运行docker-compose生成容器:

1
docker-compose up -d

节点与通道

peer节点需要创建、加入、更新channel

参考命令:peer channel

  • 在cli1中创建新通道

使用 orderer.example.com:7050 创建新的通道 mychannel,配置交易同样定义在 ./channel-artifacts/channel.tx 文件中,CA证书文件路径,注意尽量不要使用相对路径!注意此处的CA证书地址是order的msp证书地址!

1
peer channel create -c mychannel --orderer orderer.example.com:7050 -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
  • 将cli1创建的mychannel.block 分发给需要加入此通道的容器

    在其他需要加入的容器中运行,如从cli1将block 复制到 cli2:

Fabric2.2官方文档

1
2
3
docker cp cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block ./

docker cp ./mychannel.block cli2:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block
  • 节点加入channel

    在每个需要加入的容器中运行:

1
peer channel join -b mychannel.block

链码的安装与运行

本次以Fabric sample中的 fabcar 为例

  1. 将fabcar的代码文件放到docker的映射文件夹中

    1
    cp fabcar.go /chaincode/go
  2. 进入peer节点容器

    1
    docker exec -it cli1 bash
  3. 为链码文件安装依赖

    1
    2
    3
    4
    5
    6
    7
    cd /opt/gopath/src/github.com/hyperledger/fabric/fabric-cluster/chaincode/go

    go env -w GOPROXY=https://goproxy.cn,direct

    go mod init

    go mod vendor
  4. 打包链码,参考 peer lifecycle chaincode 命令

    1
    peer lifecycle chaincode package fabcar.tar.gz --path /opt/gopath/src/github.com/hyperledger/fabric/fabric-cluster/chaincode/go --label fabcar
  5. 同理,在需要安装的peer节点上打包此链码。

  6. 在每一个需要安装的节点上安装链码

    1
    peer lifecycle chaincode install fabcar.tar.gz
  7. 每一个需要安装的节点都批准该链码,只有每一个组织都批准过后,该链码才能被安装在通道上。注意此处的CA证书地址是order的msp证书地址!

    1
    2
    3
    4
    peer lifecycle chaincode approveformyorg --channelId mychannel --name fabcar --version 1.0 --init-required --package-id xxx由安装时系统产生 --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

    如:
    peer lifecycle chaincode approveformyorg --channelID mychannel --name fabcar --version 1.0 --init-required --package-id fabcar:3c3ee1baf40bcc1dc01148a53b7ff5192daed0de4514729020668f826864a119 --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

    其中package-id是安装链码时产生的,相同的链码产生的id是相同的。如:fabcar:5af17c1dfebb54dfd8f7a58f1a3a12283b39132ce4273c6ae3b6fbfb994eb9b6

  8. 查询是否已经批准

    1
    peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1.0 --init-required  --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json
  9. 选择一个Peer节点提交链码即可

    1
    2
    3
    4
    5
    peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name fabcar --version 1.0 --init-required  --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
    --peerAddresses peer0.org1.example.com:7051 \
    --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/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-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt

    注意此处的参数包括:order证书、各peer的地址和ca证书

  10. 测试链码

    • invoke 执行,会对区块数据产生影响,需要审核验证

      1
      2
      3
      4
      5
      6
      peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n fabcar --isInit --ordererTLSHostnameOverride orderer.example.com --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
      --peerAddresses peer0.org1.example.com:7051 \
      --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto-config/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-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
      -c '{"Args":["InitLedger"]}'
* query 查询,单纯查询,不对数据产生影响

  
1
2
3
peer chaincode query -C mychannel -n fabcar -c '{"Args":["QueryAllCars"]}'

peer chaincode query -C mychannel -n fabcar -c '{"Args":["QueryCar","CAR4"]}'

总结:

  1. Fabric基于配置文件crypt_config.yaml生成ca、orderer、peer等节点的公私钥和CA颁发的证书。
  2. Fabric基于配置文件configtx.yaml生成包含系统channel配置信息的区块和组织节点的交易记录。
  3. Docker基于环境变量生成相应容器
  4. CLI节点创建通道并加入通道中
  5. CLI节点打包、安装、批准、提交、运行链码

参考:

https://tinywell.com/2019/12/04/chaincode-lifecycle/

https://www.bilibili.com/video/BV1EK4y1o73U/?spm_id_from=trigger_reload

https://hyperledger-fabric.readthedocs.io/zh_CN/release-2.2/commands/peerlifecycle.html