写在最前
在使用Fabric-SDK-Go@1.0.0操作Fabric网络时遇到了bug。Fabric-SDK-GO的当前版本没有办法在没有系统通道的情况下创建应用通道,而Fabric的最新几个版本允许在没有系统通道的情况下搭建应用通道。为了解决这个矛盾并使用Fabric-SDK-GO完成后续的项目开发,所以只能将Fabric做降级。这里使用的Fabric版本为:2.2.10。
本篇博客不会完整地介绍整个系统通道的搭建过程,这里主要是为了配合使用Fabric-SDK-GO。
这里依然使用前序博文中的finance_network
网络示例来展示应用通道的搭建过程。在使用Fabric2.2搭建该网络时,生成证书及链码链接配置文件的过程可以直接参考博客:https://blog.csdn.net/yeshang_lady/article/details/134113296。 这里就不在赘述了。
1. 创建通道配置文件
在有系统通道的情况下搭建应用通道时,在启动相关docker容器之前,一定要先创建系统通道(应用通道可以等到容器启动之后创建)。所以这里要先创建系统通道配置文件configtx.yaml
,其具体内容如下:
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
Organizations:- &OrdererOrgName: OrdererOrgID: OrdererMSPMSPDir: ./organizations/ordererOrganizations/finance.com/mspPolicies:Readers:Type: SignatureRule: "OR('OrdererMSP.member')"Writers:Type: SignatureRule: "OR('OrdererMSP.member')"Admins:Type: SignatureRule: "OR('OrdererMSP.admin')"OrdererEndpoints:- orderer.finance.com:7050- &Org1Name: Org1MSPID: Org1MSPMSPDir: ./organizations/peerOrganizations/org1.finance.com/mspAnchorPeers:- Host: peer0.org2.finance.comPort: 7051Policies:Readers:Type: SignatureRule: "OR('Org1MSP.admin', 'Org1MSP.peer', 'Org1MSP.client')"Writers:Type: SignatureRule: "OR('Org1MSP.admin', 'Org1MSP.client')"Admins:Type: SignatureRule: "OR('Org1MSP.admin')"Endorsement:Type: SignatureRule: "OR('Org1MSP.peer')"- &Org2Name: Org2MSPID: Org2MSPMSPDir: ./organizations/peerOrganizations/org2.finance.com/mspAnchorPeers:- Host: peer0.org2.finance.comPort: 9051Policies:Readers:Type: SignatureRule: "OR('Org2MSP.admin', 'Org2MSP.peer', 'Org2MSP.client')"Writers:Type: SignatureRule: "OR('Org2MSP.admin', 'Org2MSP.client')"Admins:Type: SignatureRule: "OR('Org2MSP.admin')"Endorsement:Type: SignatureRule: "OR('Org2MSP.peer')"Capabilities:Channel: &ChannelCapabilitiesV2_0: trueOrderer: &OrdererCapabilitiesV2_0: trueApplication: &ApplicationCapabilitiesV2_0: trueApplication: &ApplicationDefaultsOrganizations:Policies:Readers:Type: ImplicitMetaRule: "ANY Readers"Writers:Type: ImplicitMetaRule: "ANY Writers"Admins:Type: ImplicitMetaRule: "MAJORITY Admins"LifecycleEndorsement:Type: ImplicitMetaRule: "MAJORITY Endorsement"Endorsement:Type: ImplicitMetaRule: "MAJORITY Endorsement"Capabilities:<<: *ApplicationCapabilities
Orderer: &OrdererDefaultsOrdererType: etcdraftAddresses:- orderer.finance.com:7050EtcdRaft:Consenters:- Host: orderer.finance.comPort: 7050ClientTLSCert: ./organizations/ordererOrganizations/finance.com/orderers/orderer.finance.com/tls/server.crtServerTLSCert: ./organizations/ordererOrganizations/finance.com/orderers/orderer.finance.com/tls/server.crtBatchTimeout: 2sBatchSize:MaxMessageCount: 10AbsoluteMaxBytes: 99 MBPreferredMaxBytes: 512 KBOrganizations:Policies:Readers:Type: ImplicitMetaRule: "ANY Readers"Writers:Type: ImplicitMetaRule: "ANY Writers"Admins:Type: ImplicitMetaRule: "MAJORITY Admins"BlockValidation:Type: ImplicitMetaRule: "ANY Writers"Channel: &ChannelDefaultsPolicies:Readers:Type: ImplicitMetaRule: "ANY Readers"Writers:Type: ImplicitMetaRule: "ANY Writers"Admins:Type: ImplicitMetaRule: "MAJORITY Admins"Capabilities:<<: *ChannelCapabilitiesProfiles:TwoOrgsOrdererGenesis:<<: *ChannelDefaultsOrderer:<<: *OrdererDefaultsOrganizations:- *OrdererOrgCapabilities:<<: *OrdererCapabilitiesConsortiums:SampleConsortium:Organizations:- *Org1- *Org2TwoOrgsChannel:Consortium: SampleConsortium<<: *ChannelDefaultsApplication:<<: *ApplicationDefaultsOrganizations:- *Org1- *Org2Capabilities:<<: *ApplicationCapabilities
接着,便可以创建系统通道了:
#在finance_network目录下执行
configtxgen -profile TwoOrgsOrdererGenesis -channelID system-channel -outputBlock ./system-genesis-block/genesis.block -configPath ./
如果在启动docker容器之前没有创建系统通道的话,可能会提示如下错误:panic: unable to bootstrap orderer. Error reading genesis block file: read /var/hyperledger/orderer/orderer.genesis.block: is a directory。
出现这种错误是因为在docker配置文件中指定了系统通道文件路径,docker容器会自己创建相关文件。
2. 创建docker配置文件
这里创建compose/compose-withsystem.yaml
文件作为docker容器配置文件,这里要增加系统通道相关的环境变量。文件具体内容如下:
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2.1'volumes:orderer.finance.com:peer0.org1.finance.com:peer1.org1.finance.com:peer0.org2.finance.com:networks:test:name: fabric_financeservices:orderer.finance.com:container_name: orderer.finance.comimage: hyperledger/fabric-orderer:latestenvironment:- 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_BOOTSTRAPMETHOD=file- ORDERER_GENERAL_LOCALMSPID=OrdererMSP- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp- ORDERER_OPERATIONS_LISTENADDRESS=orderer.finance.com:9443# 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/fabriccommand: orderervolumes:- ../system-genesis-block/genesis.block:/var/hyperledger/orderer/orderer.genesis.block- ../organizations/ordererOrganizations/finance.com/orderers/orderer.finance.com/msp:/var/hyperledger/orderer/msp- ../organizations/ordererOrganizations/finance.com/orderers/orderer.finance.com/tls/:/var/hyperledger/orderer/tls- orderer.finance.com:/var/hyperledger/production/ordererports:- 7050:7050- 9443:9443networks:- testpeer0.org1.finance.com:container_name: peer0.org1.finance.comimage: hyperledger/fabric-peer:latestenvironment:#Generic peer variables- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabric_finance- 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.finance.com- CORE_PEER_ADDRESS=peer0.org1.finance.com:7051- CORE_PEER_LISTENADDRESS=0.0.0.0:7051- CORE_PEER_CHAINCODEADDRESS=peer0.org1.finance.com:7052- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.finance.com:7051- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.finance.com:7051- CORE_PEER_LOCALMSPID=Org1MSP- CORE_OPERATIONS_LISTENADDRESS=peer0.org1.finance.com:9444volumes:- /var/run/docker.sock:/host/var/run/docker.sock- ../organizations/peerOrganizations/org1.finance.com/peers/peer0.org1.finance.com/msp:/etc/hyperledger/fabric/msp- ../organizations/peerOrganizations/org1.finance.com/peers/peer0.org1.finance.com/tls:/etc/hyperledger/fabric/tls- peer0.org1.finance.com:/var/hyperledger/productionworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: peer node startports:- 7051:7051- 9444:9444networks:- testpeer1.org1.finance.com:container_name: peer1.org1.finance.comimage: hyperledger/fabric-peer:latestenvironment:#Generic peer variables- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabric_finance- 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=peer1.org1.finance.com- CORE_PEER_ADDRESS=peer1.org1.finance.com:8051- CORE_PEER_LISTENADDRESS=0.0.0.0:8051- CORE_PEER_CHAINCODEADDRESS=peer1.org1.finance.com:8052- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:8052- CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org1.finance.com:8051- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.finance.com:8051- CORE_PEER_LOCALMSPID=Org1MSP- CORE_OPERATIONS_LISTENADDRESS=peer1.org1.finance.com:9446volumes:- /var/run/docker.sock:/host/var/run/docker.sock- ../organizations/peerOrganizations/org1.finance.com/peers/peer1.org1.finance.com/msp:/etc/hyperledger/fabric/msp- ../organizations/peerOrganizations/org1.finance.com/peers/peer1.org1.finance.com/tls:/etc/hyperledger/fabric/tls- peer1.org1.finance.com:/var/hyperledger/productionworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: peer node startports:- 8051:8051- 9446:9446networks:- testpeer0.org2.finance.com:container_name: peer0.org2.finance.comimage: hyperledger/fabric-peer:latestenvironment:#Generic peer variables- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabric_test- 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.finance.com- CORE_PEER_ADDRESS=peer0.org2.finance.com:9051- CORE_PEER_LISTENADDRESS=0.0.0.0:9051- CORE_PEER_CHAINCODEADDRESS=peer0.org2.finance.com:9052- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:9052- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.finance.com:9051- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.finance.com:9051- CORE_PEER_LOCALMSPID=Org2MSP- CORE_OPERATIONS_LISTENADDRESS=peer0.org2.finance.com:9445volumes:- /var/run/docker.sock:/host/var/run/docker.sock- ../organizations/peerOrganizations/org2.finance.com/peers/peer0.org2.finance.com/msp:/etc/hyperledger/fabric/msp- ../organizations/peerOrganizations/org2.finance.com/peers/peer0.org2.finance.com/tls:/etc/hyperledger/fabric/tls- peer0.org2.finance.com:/var/hyperledger/productionworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: peer node startports:- 9051:9051- 9445:9445networks:- testcli:container_name: cliimage: hyperledger/fabric-tools:latesttty: truestdin_open: trueenvironment:- GOPATH=/opt/gopath- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- FABRIC_LOGGING_SPEC=INFO#- FABRIC_LOGGING_SPEC=DEBUGworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: /bin/bashvolumes:- /var/run/:/host/var/run/- ../organizations:/opt/gopath/src/github.com/hyperledger/fabric/peer/organizations- ../scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/depends_on:- peer0.org1.finance.com- peer1.org1.finance.com- peer0.org2.finance.comnetworks:- test
然后启动容器,具体命令如下:
#在finance_network目录下执行
sudo DOCKER_SOCK="/var/run/docker.sock" docker-compose -f compose/compose-withsystem.yaml up -d
创建应用通道及配置锚节点
接下来使用如下命令创建应用通道并配置各个组织的锚节点。
#创建应用通道
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
#设置组织Org1的锚节点
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
#设置组织Org2的锚节点
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
接下来的peer节点加入通道及部署链码等的操作将使用Fabric-sdk-go来实现,具体参考博客:https://blog.csdn.net/yeshang_lady/article/details/134921528。
Tips:在使用Fabric-SDK-GO进行后续操作的时候,要对docker容器进行重启(注意,只需要重启docker容器)。因为Fabric-SDK-GO会依据我们生成的文件(channel.tx、Org1MSPanchors.tx、Org2MSPanchors.tx)重新进行创建应用通道及配置锚节点等操作。如果不重启,则会提示如下错误: create channel failed: create channel failed: SendEnvelope failed: calling orderer 'orderer.finance.com:7050' failed: Orderer Server Status Code: (400) BAD_REQUEST. Description: error applying config update to existing channel 'mychannel': error authorizing update: error validating ReadSet: proposed update requires that key [Group] /Channel/Application be at version 0, but it is currently at version 1