configtxgen是Fabric提供的用于通道配置的实用程序,configtx.yaml配置文件则是configtxgen工具配置的根据,configtx.yaml文件中的内容直接决定了创世区块、通道配置transaction以及锚点transaction文件中的内容,本文将以first-network案例为例,简单解释configtxgen和configtx.yaml的作用和协同工作过程。
configtxgen是Fabric提供的用于通道配置的实用程序,主要生成以下3种文件:
configtxgen工具参数信息及解释:
Usage of configtxgen:
-asOrg string
作为特定的组织(按名称string)执行配置生成,只包括org(可能)有权设置的写集中的值。如用来指明生成的锚节点所在的组织
-channelCreateTxBaseProfile string
指定一个概要文件作为orderer系统通道当前状态,以允许在通道创建tx生成期间修改非应用程序参数。仅在与“outputCreateChannelTx”组合时有效。
-channelID string
在configtx中使用的通道ID,即通道名称,默认是"testchainid"
-configPath string
包含要使用的配置的路径(如果设置的话)
-inspectBlock string
按指定路径打印块中包含的配置,用于检查和输出通道中创世区块的内容,锚节点在configtx.yaml中的AnchorPeers中指定
-inspectChannelCreateTx string
按指定路径打印交易中包含的配置,用来检查通道的配置交易信息
-outputAnchorPeersUpdate string
创建一个配置更新来更新锚节点(仅在默认通道创建时工作,并且仅在第一次更新时工作)
-outputBlock string
将genesis块写入(如果设置)的路径。configtx.yaml文件中的Profiles要指定Consortiums,否则启动排序服务节点会失败
-outputCreateChannelTx string
将通道配置交易文件写入(如果设置)的路径。configtx.yaml文件中的Profiles必须包含Application,否则创建通道会失败
-printOrg string
将组织的定义打印为JSON。(对于手动向通道添加组织非常有用)
-profile string
指定使用的是configtx.yaml中某个用于生成的Profiles配置项。(默认为“SampleInsecureSolo”)
-version
显示版本信息
configtx.yaml文件分为很多配置段,其中最重要的是profiles配置段,该配置段相当于一个入口,通过configtxgen配置工具的-profiles参数指定选择那个profile生成相应的文件,相当于调用一个函数的感觉,其他配置段都会在profiles配置段根据需要进行引用。
该配置段定义了不同组织的身份,在后面的配置中会用到,first-network案例相应内容如下:
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: crypto-config/ordererOrganizations/example.com/msp
Policies:
Readers:
Type: Signature
Rule: "OR('OrdererMSP.member')"
Writers:
Type: Signature
Rule: "OR('OrdererMSP.member')"
Admins:
Type: Signature
Rule: "OR('OrdererMSP.admin')"
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
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')"
AnchorPeers:
- Host: peer0.org1.example.com
Port: 7051
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: crypto-config/peerOrganizations/org2.example.com/msp
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')"
AnchorPeers:
- Host: peer0.org2.example.com
Port: 9051
capability直接翻译是能力,这里可以理解为对Fabric网络中组件版本的控制,通过版本进而控制相应的特性。新更新的特性旧版本的组件不支持,就可能无法验证或提交transaction从而导致不同版本的节点上有不同的账本,因此使用Capabilities来使不支持特性的旧组件终止处理transaction直到其更新升级。first-network案例中相应配置段如下:
Capabilities:
Channel: &ChannelCapabilities
V2_0: true
Orderer: &OrdererCapabilities
V2_0: true
Application: &ApplicationCapabilities
V2_0: true
Channel表示orderers和peers同时都要满足,Orderer只需要orderers满足,Application只需要peers满足即可。
一些和应用有关的将会编进创世区块或配置transaction的应用相关的参数,其中organizations:在此处不进行配置,在后面profiles配置段中,根据需要生成的文件类型进行配置。first-network案例中相应的配置段如下:
Application: &ApplicationDefaults
Organizations:
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
关于orderer的一些参数,first-network案例中相应配置段如下:
Orderer: &OrdererDefaults
OrdererType: etcdraft
Addresses:
- orderer.example.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 99 MB
PreferredMaxBytes: 512 KB
Organizations:
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
BlockValidation:
Type: ImplicitMeta
Rule: "ANY Writers"
first-network案例中相应配置段如下:
Channel: &ChannelDefaults
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
Capabilities:
<<: *ChannelCapabilities
profiles配置段相当于configtxgen工具的统一入口,通过设置不同的configtxgen -profile参数决定要使用configtxgen生成什么文件,profiles配置段通过使用上面准备好的配置段来根据需要配置不同的文件(虽然可以显示配置但是最好采用引用默认配置的方式,有封装的意思)。first-network案例中相应配置段如下所示:
Profiles:
TwoOrgsChannel:
Consortium: SampleConsortium
<<: *ChannelDefaults
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
Capabilities:
<<: *ApplicationCapabilities
SampleMultiNodeEtcdRaft:
<<: *ChannelDefaults
Capabilities:
<<: *ChannelCapabilities
Orderer:
<<: *OrdererDefaults
OrdererType: etcdraft
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
- Host: orderer2.example.com
Port: 8050
ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/server.crt
ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/server.crt
- Host: orderer3.example.com
Port: 9050
ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer3.example.com/tls/server.crt
ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer3.example.com/tls/server.crt
- Host: orderer4.example.com
Port: 10050
ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer4.example.com/tls/server.crt
ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer4.example.com/tls/server.crt
- Host: orderer5.example.com
Port: 11050
ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer5.example.com/tls/server.crt
ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer5.example.com/tls/server.crt
Addresses:
- orderer.example.com:7050
- orderer2.example.com:8050
- orderer3.example.com:9050
- orderer4.example.com:10050
- orderer5.example.com:11050
Organizations:
- *OrdererOrg
Capabilities:
<<: *OrdererCapabilities
Application:
<<: *ApplicationDefaults
Organizations:
- <<: *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2