Fabric SDK 配置文件

一、Fabric SDK配置

Fabric区块链网络应用程序需要大量的参数,用于连接Fabric区块链网络。通常将Fabric区块链网络应用程序所需的参数放到一个配置文件进行管理,配置文件定义Fabric SDK Go的配置和用户自定义参数,指定了连接Fabric区块链网络所需的全部信息,例如Fabric区块链网络组件的主机名和端口等。Fabric SDK GO为应用程序提供的配置文件通常使用yaml文件格式编写,并命名为config.yaml,配置文件会在应用程序代码中被读取。
Fabric SDK Go版本提供了config.yaml模板,开发者可以参考
fabric-sdk-go/pkg/core/config/testdata/template/config.yaml,也可以根据fabric-sdk-go/test/fixtures/config/config_e2e.yaml实例进行改写。

二、version定义

version用于定义config.yaml文件内容的版本,Fabric SDK Go会使用version匹配相应的解析规则。
version: 1.0.0

三、channels定义

channels部分描述已经存在的通道信息,每个通道包含哪些orderer、peer 。
peer部分可以定义peer节点的角色属性,角色如下:
endorsingPeer:可选。peer节点节点必须安装链码。peer节点是否会被发送交易提案进行背书。应用程序也可以使用本属性来决定发送链码安装请求到哪个peer节点。默认值:true。
chaincodeQuery:可选。peer节点必须安装链码。peer节点是否会被发送交易提案只进行查询。应用程序还可以使用本属性来决定发送链码安装的请求到哪个peer节点。默认值:true。
ledgerQuery:可选。是否可以向peer节点发送不会进行链码查询的提案,如queryBlock(),queryTransaction()等。默认值:true。
eventSource:可选。peer节点是否为SDK事件监听器注册的目标,所有的peer节点都可以生产事件,但通常只需要连接一个对事件进行监听。默认值:true。

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
# 如果应用程序创建了通道,不需要本部分
channels:
# 如果没有定义channel配置或是定义的channel没有信息,使用默认channel
# 如果channel没有定义peers,使用默认channel的peers
# 如果channel没有定义orderes,使用默认channel的orderes
# 如果channel没有定义背书策略,使用默认channel的背书策略
# 如果channel定义了背书策略,但某些背书策略缺失,缺失的背书策略会被默认channel填充
_default:
# 可选,参与组织的peers列表
peers:
peer1.org1.example.com:
endorsingPeer: true
chaincodeQuery: true
ledgerQuery: true
eventSource: true

# 可选,应用程序使用下列选项执行通道操作,如获取通道配置
policies:
#可选,获取通道配置区块
queryChannelConfig:
#可选,成功响应节点的最小数量
minResponses: 1
# 可选
maxTargets: 1
# 可选,查询配置区块的重试选项
retryOpts:
# 可选,重试次数
attempts: 5
# 可选, 第一次重试的后退间隔
initialBackoff: 500ms
# 可选, 重试的最大后退间隔
maxBackoff: 5s
backoffFactor: 2.0
# 可选,获取发现信息选项
discovery:
maxTargets: 2
# 重试选项
retryOpts:
# 可选,重试次数
attempts: 4
initialBackoff: 500ms
maxBackoff: 5s
backoffFactor: 2.0

# 可选,事件服务选项
eventService:
# 可选 resolverStrategy指定连接到peer节点时选择peer节点的决策策略
# 可选值:PreferOrg(默认), MinBlockHeight, Balanced
# PreferOrg:
# 基于区块高度滞后阀值决定哪些peer节点是合适的, 虽然会在当前组织中优先选择peer节点
# 如果当前组织中没有合适的peer节点,会从其它组织中选取
# MinBlockHeight:
# 根据区块高度滞后阀值选择最好的peer节点,
# 所有peer节点的最大区块高度是确定的,区块高度小于最大区块高度但在滞后阀值范围内的peer节点会被加载,
# 其它peer节点不会被考虑。
# Balanced:
# 使用配置的balancer选择peer节点
resolverStrategy: PreferOrg

# 可选 balancer是选择连接到peer节点的负载均衡器
# 可选值: Random (default), RoundRobin
balancer: Random

# 可选,blockHeightLagThreshold设置区块高度滞后阀值,用于选择连接到的peer节点
# 如果一个peer节点滞后于最新的peer节点给定的区块数,会被排除在选择之外
# 注意:当minBlockHeightResolverMode设置为ResolveByThreshold时,本参数才可用
# 默认: 5
blockHeightLagThreshold: 5

# 可选,reconnectBlockHeightLagThreshold - 如果peer节点的区块高度落后于指定的区块数量,
# 事件客户端会从peer节点断开,重新连接到一个性能更好的peer节点
# 如果peerMonitor设置为启用(默认),本参数才可用
# 默认值: 10
# 注意:设置值太低会导致事件客户端频繁断开或重连,影响性能
reconnectBlockHeightLagThreshold: 8

# 可选, peerMonitorPeriod是事件客户端从连接节点断开重新连接到另外一个节点的时间
# 默认: 对于Balanced resolverStrategy禁用,为0; 对于PreferOrg和MinBlockHeight为5s
peerMonitorPeriod: 6s

#如果_default没有定义,必选;如果_default已经定义,可选。
# 通道名称
assetchannel:
# 如果_default peers没有定义,必选;如果_default peers已经定义,可选。
# 参与组织的peer节点列表
peers:
peer0.org1.example.com:
# 可选。peer节点是否会被发送交易提议只进行查询。peer节点必须安装链码。
# 应用程序还可以使用本属性来决定发送链码安装的请求到哪个peer节点。默认值:true
endorsingPeer: true

# 可选。peer节点必须安装链码。peer节点是否会被发送交易提案只进行查询。
# 应用程序还可以使用本属性来决定发送链码安装的请求到哪个peer节点。默认值:true
chaincodeQuery: true

# 可选。是否可以向peer节点发送不会进行链码查询的提案,如queryBlock(),
# queryTransaction()等。默认值:true。
ledgerQuery: true

# 可选。peer节点是否为SDK事件监听器注册的目标,所有的peer节点都可以生产事件,
# 但通常只需要连接一个对事件进行监听。默认值:true。
eventSource: true

四、organizations定义

organizations描述peer节点所属的组织(org),证书颁发机构,以及组织的MSP ID。

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
# Fabric区块链网络中参与的组织列表
organizations:
org1:
mspid: Org1MSP
# 组织的MSP存储位置,绝对路径或相对cryptoconfig的路径
cryptoPath: peerOrganizations/org1.example.com/users/{username}@org1.example.com/msp
peers:
- peer0.org1.example.com
- peer1.org1.example.com
# 可选,证书颁发机构签发×××明,Fabric-CA是一个特殊的证书管理机构,提供REST API支持动态证书管理,如登记、撤销、重新登记
# 下列部分只为Fabric-CA服务器设置
certificateAuthorities:
- ca.org1.example.com

org2:
mspid: Org2MSP

# 组织的MSP存储位置,相对于cryptoconfig的相对位置或绝对路径
cryptoPath: peerOrganizations/org2.example.com/users/{username}@org2.example.com/msp
peers:
- peer0.org2.example.com
certificateAuthorities:
- ca.org2.example.com

# Orderer组织名称
ordererorg:
# 组织的MSPID
mspID: OrdererMSP
# 加载用户需要的密钥和证书,绝对路径或相对路径
cryptoPath: ordererOrganizations/example.com/users/{username}@example.com/msp

五、orderers定义

orderers必须指定要连接的Hyperledger Fabric区块链网络中所有orderder节点的主机名和端口。orderers对象可以包含多个orderder节点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 发送交易请求或通道创建、更新请求到的orderers列表
# 如果定义了超过一个orderer,SDK使用哪一个orderer由代码实现时指定
orderers:
# orderer节点,可以定义多个
orderer.example.com:
url: orderer.example.com:7050
# 以下属性由gRPC库定义,会被传递给gRPC客户端构造函数
grpcOptions:
ssl-target-name-override: orderer.example.com
# 下列参数用于设置服务器上的keepalive策略,不兼容的设置会导致连接关闭
# 当keep-alive-time被设置为0或小于激活客户端的参数,下列参数失效
keep-alive-time: 0s
keep-alive-timeout: 20s
keep-alive-permit: false
fail-fast: false
allow-insecure: false

tlsCACerts:
# 证书的绝对路径
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem

六、peers定义

peers必须指定Hyperledger Fabric区块链网络中所有peer节点的主机名和端口,可能会在其它地方引用,如channels,organizations等部分。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# peers节点列表
peers:
# peer节点定义,可以定义多个
peer0.org1.example.com:
# URL用于发送背书和查询请求
url: peer0.org1.example.com:7051

grpcOptions:
ssl-target-name-override: peer0.org1.example.com
keep-alive-time: 0s
keep-alive-timeout: 20s
keep-alive-permit: false
fail-fast: false
allow-insecure: false

tlsCACerts:
# 证书的绝对路径
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem

七、certificateAuthorities定义

certificateAuthorities必须在Hyperledger Fabric区块链网络中指定证书颁发机构(CA)的主机名和端口,以便用于注册现有用户和注册新用户。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Fabric-CA是Hyperledger Fabric提供了特殊的证书颁发机构,可以通过REST API管理证书。
# 应用程序可以选择使用一个标准的证书颁发机构代替Fabric-CA,此时本部分不需要指定
certificateAuthorities:
# CA机构,可以定义多个
ca.org1.example.com:
url: https://ca.org1.example.com:7054
tlsCACerts:
# Comma-Separated list of paths
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem
# 客户端和Fabric CA进行SSL握手的密钥和证书
client:
key:
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/tls.example.com/users/User1@tls.example.com/tls/client.key
cert:
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/tls.example.com/users/User1@tls.example.com/tls/client.crt

# Fabric-CA支持通过REST API进行动态用户注册
registrar:
enrollId: admin
enrollSecret: adminpw
# 可选,CA机构名称
caName: ca.org1.example.com

八、client

client部分必需定义,客户端应用程序代表谁来和Fabric区块链网络来交互,可以定义超时选项。

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
#客户端定义
client:
# 客户端所属的组织,必须是organizations定义的组织
organization: org1
#定义日志服务
logging:
level: debug #debug级别

# peer、事件服务、orderer超时的全局配置
# 本部分如果忽略,使用下列的值作为默认值
peer:
timeout:
connection: 10s
response: 180s
discovery:
greylistExpiry: 10s
eventService:
timeout:
registrationResponse: 15s
orderer:
timeout:
connection: 15s
response: 15s
global:
timeout:
query: 180s
execute: 180s
resmgmt: 180s
cache:
connectionIdle: 30s
eventServiceIdle: 2m
channelConfig: 30m
channelMembership: 30s
discovery: 10s
selection: 10m

# MSP根目录
cryptoconfig:
path: ${GOPATH}/src/github.com/hyperledger/fabric/AssetExchange/deploy/crypto-config

# 某些SDK支持插件化的KV数据库, 通过指定credentialStore属性实现
credentialStore:
# 可选,用于用户证书材料存储,如果所有的证书材料被嵌入到配置文件,则不需要
path: "/tmp/state-store"

# 可选,指定Go SDK实现的CryptoSuite实现
cryptoStore:
# 指定用于加密密钥存储的底层KV数据库
path: /tmp/msp

# 客户端的BCCSP模块配置
BCCSP:
security:
enabled: true
default:
provider: "SW"
hashAlgorithm: "SHA2"
softVerify: true
level: 256

tlsCerts:
# 可选,当连接到peers,orderes时使用系统证书池,默认为false
systemCertPool: true

# 可选,客户端和peers与orderes进行TLS握手的密钥和证书
client:
key:
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/tls.example.com/users/User1@tls.example.com/tls/client.key
cert:
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/tls.example.com/users/User1@tls.example.com/tls/client.crt

九、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
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
# Copyright SecureKey Technologies Inc. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
#
# The network connection profile provides client applications the information about the target
# blockchain network that are necessary for the applications to interact with it. These are all
# knowledge that must be acquired from out-of-band sources. This file provides such a source.

name: "assetchannel"

#
# Describe what the target network is/does.
#
description: "asset exchange network"

#指定版本
version: 1.0.0

#客户端定义
client:
# 客户端所属的组织,必须是organizations定义的组织
organization: org1
#定义日志服务
logging:
level: debug #debug级别

# MSP根目录
cryptoconfig:
path: ${GOPATH}/src/github.com/hyperledger/fabric/AssetExchange/deploy/crypto-config

# 某些SDK支持插件化的KV数据库, 通过指定credentialStore属性实现
credentialStore:
# 可选,用于用户证书材料存储,如果所有的证书材料被嵌入到配置文件,则不需要
path: "/tmp/state-store"

# 可选,指定Go SDK实现的CryptoSuite实现
cryptoStore:
# 指定用于加密密钥存储的底层KV数据库
path: /tmp/msp

# 客户端的BCCSP模块配置
BCCSP:
security:
enabled: true
default:
provider: "SW"
hashAlgorithm: "SHA2"
softVerify: true
level: 256

tlsCerts:
# 可选,当连接到peers,orderes时使用系统证书池,默认为false
systemCertPool: true

# 可选,客户端和peers与orderes进行TLS握手的密钥和证书
client:
key:
# path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/tls.example.com/users/User1@tls.example.com/tls/client.key
cert:
#path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/tls.example.com/users/User1@tls.example.com/tls/client.crt

# 如果应用程序创建了通道,不需要本部分
channels:
# 如果没有定义channel配置或是定义的channel没有信息,使用默认channel
# 如果channel没有定义peers,使用默认channel的peers
# 如果channel没有定义orderes,使用默认channel的orderes
# 如果channel没有定义背书策略,使用默认channel的背书策略
# 如果channel定义了背书策略,但某些背书策略缺失,缺失的背书策略会被默认channel填充

#如果_default没有定义,必选;如果_default已经定义,可选。
# 通道名称
assetchannel:
# 如果_default peers没有定义,必选;如果_default peers已经定义,可选。
# 参与组织的peer节点列表
peers:
peer0.org1.example.com:
# 可选。peer节点是否会被发送交易提议只进行查询。peer节点必须安装链码。
# 应用程序还可以使用本属性来决定发送链码安装的请求到哪个peer节点。默认值:true
endorsingPeer: true

# 可选。peer节点必须安装链码。peer节点是否会被发送交易提案只进行查询。
# 应用程序还可以使用本属性来决定发送链码安装的请求到哪个peer节点。默认值:true
chaincodeQuery: true

# 可选。是否可以向peer节点发送不会进行链码查询的提案,如queryBlock(),
# queryTransaction()等。默认值:true。
ledgerQuery: true

# 可选。peer节点是否为SDK事件监听器注册的目标,所有的peer节点都可以生产事件,
# 但通常只需要连接一个对事件进行监听。默认值:true。
eventSource: true
# 可选,应用程序使用下列选项执行通道操作,如获取通道配置
policies:
#可选,获取通道配置区块
queryChannelConfig:
#可选,成功响应节点的最小数量
minResponses: 1
# 可选
maxTargets: 1
# 可选,查询配置区块的重试选项
retryOpts:
# 可选,重试次数
attempts: 5
# 可选, 第一次重试的后退间隔
initialBackoff: 500ms
# 可选, 重试的最大后退间隔
maxBackoff: 5s
backoffFactor: 2.0

# Fabric区块链网络中参与的组织列表
organizations:
org1:
mspid: Org1MSP
# 组织的MSP存储位置,绝对路径或相对cryptoconfig的路径
cryptoPath: peerOrganizations/org1.example.com/users/{username}@org1.example.com/msp
peers:
- peer0.org1.example.com
- peer1.org1.example.com
# 可选,证书颁发机构签发×××明,Fabric-CA是一个特殊的证书管理机构,提供REST API支持动态证书管理,如登记、撤销、重新登记
# 下列部分只为Fabric-CA服务器设置
certificateAuthorities:
#- ca.org1.example.com

# Orderer组织名称
ordererorg:
# 组织的MSPID
mspID: OrdererMSP
# 加载用户需要的密钥和证书,绝对路径或相对路径
cryptoPath: ordererOrganizations/example.com/users/{username}@example.com/msp

# 发送交易请求或通道创建、更新请求到的orderers列表
# 如果定义了超过一个orderer,SDK使用哪一个orderer由代码实现时指定
orderers:
# orderer节点,可以定义多个
orderer.example.com:
url: orderer.example.com:7050
# 以下属性由gRPC库定义,会被传递给gRPC客户端构造函数
grpcOptions:
ssl-target-name-override: orderer.example.com
# 下列参数用于设置服务器上的keepalive策略,不兼容的设置会导致连接关闭
# 当keep-alive-time被设置为0或小于激活客户端的参数,下列参数失效
keep-alive-time: 0s
keep-alive-timeout: 20s
keep-alive-permit: false
fail-fast: false
allow-insecure: false

tlsCACerts:
# 证书的绝对路径
# path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem

# peers节点列表
peers:
# peer节点定义,可以定义多个
peer0.org1.example.com:
# URL用于发送背书和查询请求
url: peer0.org1.example.com:7051

grpcOptions:
ssl-target-name-override: peer0.org1.example.com
keep-alive-time: 0s
keep-alive-timeout: 20s
keep-alive-permit: false
fail-fast: false
allow-insecure: false

tlsCACerts:
# 证书的绝对路径
#path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem
刘小恺(Kyle) wechat
如有疑问可联系博主