channel one(如何搭建一个多通道Fabric网络)

发布时间:2025-12-11 00:47:17 浏览次数:10

1、Hyperledger Fabric多通道网络实验环境概述

我们将构造一个包含3个机构的Hyperledger Fabric网络:Org1、Org2和Org3,每个机构中包含一个节点Peer0。网络包含两个通道:由Org1、Org2和Org3组成的ChannelAll,以及由Org1和Org2组成的Channel12,因此这个Fabric网络是多通道的配置。在这两个Fabric通道上我们将部署同样的链码,即Fabrc-Samples中提供的Simple Asset链码:

2、Hyperledger Fabric多通道网络实验环境搭建

Step 1:在Hyperledger官方提供的fabric-samples目录下克隆本教程提供的示例代码:

cdfabric-samplesgitclonehttps://github.com/kctam/3org2ch_143.gitcd3org2ch_143

Step 2:为参与Fabric通道的机构生成所需的密码学资料

../bin/cryptogengenerate--config=./crypto-config.yaml

Step 3:生成Fabric通道素材

mkdirchannel-artifacts&&exportFABRIC_CFG_PATH=$PWD../bin/configtxgen-profileOrdererGenesis\-outputBlock./channel-artifacts/genesis.blockexportCHANNEL_ONE_NAME=channelallexportCHANNEL_ONE_PROFILE=ChannelAllexportCHANNEL_TWO_NAME=channel12exportCHANNEL_TWO_PROFILE=Channel12../bin/configtxgen-profile${CHANNEL_ONE_PROFILE}\-outputCreateChannelTx./channel-artifacts/${CHANNEL_ONE_NAME}.tx\-channelID$CHANNEL_ONE_NAME../bin/configtxgen-profile${CHANNEL_TWO_PROFILE}\-outputCreateChannelTx./channel-artifacts/${CHANNEL_TWO_NAME}.tx\-channelID$CHANNEL_TWO_NAME../bin/configtxgen-profile${CHANNEL_ONE_PROFILE}\-outputAnchorPeersUpdate./channel-artifacts/Org1MSPanchors_${CHANNEL_ONE_NAME}.tx\-channelID$CHANNEL_ONE_NAME-asOrgOrg1MSP../bin/configtxgen-profile${CHANNEL_ONE_PROFILE}\-outputAnchorPeersUpdate./channel-artifacts/Org2MSPanchors_${CHANNEL_ONE_NAME}.tx\-channelID$CHANNEL_ONE_NAME-asOrgOrg2MSP../bin/configtxgen-profile${CHANNEL_ONE_PROFILE}\-outputAnchorPeersUpdate./channel-artifacts/Org3MSPanchors_${CHANNEL_ONE_NAME}.tx\-channelID$CHANNEL_ONE_NAME-asOrgOrg3MSP../bin/configtxgen-profile${CHANNEL_TWO_PROFILE}\-outputAnchorPeersUpdate./channel-artifacts/Org1MSPanchors_${CHANNEL_TWO_NAME}.tx\-channelID$CHANNEL_TWO_NAME-asOrgOrg1MSP../bin/configtxgen-profile${CHANNEL_TWO_PROFILE}\-outputAnchorPeersUpdate./channel-artifacts/Org2MSPanchors_${CHANNEL_TWO_NAME}.tx\-channelID$CHANNEL_TWO_NAME-asOrgOrg2MSP

Step 4:启动所有的容器,最后应当看到有5个容器

docker-composeup-ddockerps

Step 5:为了便于演示,开启3个终端,并设置排序节点的CA

Org1

dockerexec-itclibashexportORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Org2

dockerexec-e"CORE_PEER_LOCALMSPID=Org2MSP"\-e"CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt"\-e"CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp"\-e"CORE_PEER_ADDRESS=peer0.org2.example.com:7051"\-itclibashexportORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Org3

dockerexec-e"CORE_PEER_LOCALMSPID=Org3MSP"\-e"CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt"\-e"CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp"\-e"CORE_PEER_ADDRESS=peer0.org3.example.com:7051"\-itclibashexportORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Step 5:在Fabric网络中创建多通道,并将各peer节点分别加入多个通道

首先创建channelall通道,并将3个机构的节点都加入该通道:

Org1

peerchannelcreate-oorderer.example.com:7050-cchannelall\-f/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/channelall.tx\--tls--cafile$ORDERER_CApeerchanneljoin-bchannelall.block--tls--cafile$ORDERER_CApeerchannelupdate-oorderer.example.com:7050-cchannelall\-f/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/Org1MSPanchors_channelall.tx\--tls--cafile$ORDERER_CA

Org2

peerchanneljoin-bchannelall.block--tls--cafile$ORDERER_CApeerchannelupdate-oorderer.example.com:7050-cchannelall\-f/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/Org2MSPanchors_channelall.tx\--tls--cafile$ORDERER_CA

Org3

peerchanneljoin-bchannelall.block--tls--cafile$ORDERER_CApeerchannelupdate-oorderer.example.com:7050-cchannelall\-f/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/Org3MSPanchors_channelall.tx\--tls--cafile$ORDERER_CA

然后创建channel12,并将Org1和Org2都加入该通道:

Org1

peerchannelcreate-oorderer.example.com:7050-cchannel12\-f/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/channel12.tx\--tls--cafile$ORDERER_CApeerchanneljoin-bchannel12.block--tls--cafile$ORDERER_CApeerchannelupdate-oorderer.example.com:7050-cchannel12\-f/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/Org1MSPanchors_channel12.tx\--tls--cafile$ORDERER_CA

Org2

peerchanneljoin-bchannel12.block--tls--cafile$ORDERER_CApeerchannelupdate-oorderer.example.com:7050-cchannel12\-f/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/Org2MSPanchors_channel12.tx\--tls--cafile$ORDERER_CA

Step 6:检查各节点已经加入的Fabric通道

在各节点对应的终端中使用如下命令查看当前节点加入的通道:

peerchannellist

你应当可以看到org1和org2分别加入了两个通道,而org3则只加入了一个通道。

如果一切顺利,现在你就有了一个包含3个机构的多通道Fabric网络,可以用于测试 任何链码了。

Step 7:在测试完毕后记得清理实验环境,命令如下:

docker-composedown-vdockerrm$(dockerps-aq)dockerrmi$(dockerimagesdev-*-q)

3、Fabric多通道安装Simple Asset链码(SACC)

现在我们的Fabric多通道实验网络已经起来了,可以开始部署链码了。

我们使用fabric-samples内置的SACC链码,其内容如下:

/**CopyrightIBMCorpAllRightsReserved**SPDX-License-Identifier:Apache-2.0*/packagemainimport("fmt""github.com/hyperledger/fabric/core/chaincode/shim""github.com/hyperledger/fabric/protos/peer")//SimpleAssetimplementsasimplechaincodetomanageanassettypeSimpleAssetstruct{}//Initiscalledduringchaincodeinstantiationtoinitializeany//data.Notethatchaincodeupgradealsocallsthisfunctiontoreset//ortomigratedata.func(t*SimpleAsset)Init(stubshim.ChaincodeStubInterface)peer.Response{//Gettheargsfromthetransactionproposalargs:=stub.GetStringArgs()iflen(args)!=2{returnshim.Error("Incorrectarguments.Expectingakeyandavalue")}//Setupanyvariablesorassetsherebycallingstub.PutState()//Westorethekeyandthevalueontheledgererr:=stub.PutState(args[0],[]byte(args[1]))iferr!=nil{returnshim.Error(fmt.Sprintf("Failedtocreateasset:%s",args[0]))}returnshim.Success(nil)}//Invokeiscalledpertransactiononthechaincode.Eachtransactionis//eithera'get'ora'set'ontheassetcreatedbyInitfunction.TheSet//methodmaycreateanewassetbyspecifyinganewkey-valuepair.func(t*SimpleAsset)Invoke(stubshim.ChaincodeStubInterface)peer.Response{//Extractthefunctionandargsfromthetransactionproposalfn,args:=stub.GetFunctionAndParameters()varresultstringvarerrerroriffn=="set"{result,err=set(stub,args)}else{//assume'get'eveniffnisnilresult,err=get(stub,args)}iferr!=nil{returnshim.Error(err.Error())}//Returntheresultassuccesspayloadreturnshim.Success([]byte(result))}//Setstorestheasset(bothkeyandvalue)ontheledger.Ifthekeyexists,//itwilloverridethevaluewiththenewonefuncset(stubshim.ChaincodeStubInterface,args[]string)(string,error){iflen(args)!=2{return"",fmt.Errorf("Incorrectarguments.Expectingakeyandavalue")}err:=stub.PutState(args[0],[]byte(args[1]))iferr!=nil{return"",fmt.Errorf("Failedtosetasset:%s",args[0])}returnargs[1],nil}//Getreturnsthevalueofthespecifiedassetkeyfuncget(stubshim.ChaincodeStubInterface,args[]string)(string,error){iflen(args)!=1{return"",fmt.Errorf("Incorrectarguments.Expectingakey")}value,err:=stub.GetState(args[0])iferr!=nil{return"",fmt.Errorf("Failedtogetasset:%switherror:%s",args[0],err)}ifvalue==nil{return"",fmt.Errorf("Assetnotfound:%s",args[0])}returnstring(value),nil}//mainfunctionstartsupthechaincodeinthecontainerduringinstantiatefuncmain(){iferr:=shim.Start(new(SimpleAsset));err!=nil{fmt.Printf("ErrorstartingSimpleAssetchaincode:%s",err)}}

Fabric Samples提供的SACC链码的逻辑很简单:

  • 当链码实例化时就会执行Init()函数,该函数需要两个参数,分别对应键和值

  • 将传入Init()函数的键/值对使用PutState方法保存到账本中

  • 在链码实例化之后,对交易的处理是由Invoke()函数来负责的。 该函数的参数 包括一个方法名以及若干参数。

  • 如果调用Invoke()函数时方法名为set,那么就需要传入两个参数,分别表示要 设置的键和值

  • 如果调用Invoke()函数时方法名为get,那么就需要一个参数,表示要读取的键

通过链码安装操作,就可以在各节点上启动链码。注意在链码实例化之前还不可用。

在各节点对应的终端中使用如下命令安装链码:

peerchaincodeinstall-nsacc-pgithub.com/chaincode/sacc-v1.0

我们应当可以看到如下的输出结果:

现在所有的节点上都安装了SACC链码,我们可以实例化这个链码了。

4、Fabric多通道实验1:ChannelAll通道上Fabric链码的实例化与访问

首先我们看包含所有三个机构的ChannelAll通道。

在Org1对应的终端中,在ChannelAll通道上实例化链码:

peerchaincodeinstantiate-oorderer.example.com:7050--tls\--cafile$ORDERER_CA-Cchannelall-c'{"Args":["a","100"]}'\-nsacc-v1.0-P"OR('Org1MSP.peer','Org2MSP.peer','Org3MSP.peer')"

我们设置了初始的键/值对为a/100。此外我们设置了背书策略:OR表示只需要3个机构中的任何一个背书即可。

现在让我们在通道ChannelAll上查询键a的值。

进入Org1对应的终端,运行如下命令:

peerchaincodequery-Cchannelall-nsacc-c'{"Args":["get","a"]}'

结果如下:

现在在Org2对应的终端中运行如下命令:

peerchaincodequery-Cchannelall-nsacc-c'{"Args":["get","a"]}'

结果如下:

现在在Org3对应的终端中运行如下命令:

peerchaincodequery-Cchannelall-nsacc-c'{"Args":["get","a"]}'

结果如下:

现在我们可以看到在三个节点上得到了相同的值,它们共享同一个账本。

5、Fabric多通道实验2:在Channel12通道上SACC链码的实例化与交互

现在让我们在通道Channel12上实例化这个SACC链码。

在Org1对应的终端中,运行如下命令:

peerchaincodeinstantiate-oorderer.example.com:7050\--tls--cafile$ORDERER_CA-Cchannel12\-c'{"Args":["b","200"]}'-nsacc-v1.0\-P"OR('Org1MSP.peer','Org2MSP.peer')"

这次我们将初始的键/值对设置为b/200,背书策略为任一机构完成背书即可。

还是从Org1开始:

peerchaincodequery-Cchannel12-nsacc-c'{"Args":["get","b"]}'

结果如下:

然后进入Org2对应的终端:

peerchaincodequery-Cchannel12-nsacc-c'{"Args":["get","b"]}'

结果如下:

如果我们在Org3对应的终端运行同样的命令,就会看到提示禁止访问。这是 因为Org3没有加入通道Channel12:

peerchaincodequery-Cchannel12-nsacc-c'{"Args":["get","b"]}'

结果如下:

如果我们尝试在通道Channel12上读取键a的值,会发现提示没有定义a。 在Hyperledger Fabric中,每个通道都有自己的账本,不同通道的状态是不共享的。

在Org1和Org2的终端中运行如下命令:

peerchaincodequery-Cchannel12-nsacc-c'{"Args":["get","a"]}'

结果如下:

channel one
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477