Go SDK搭建多通道多orderer节点的Fabric环境
书接上回,之前利用fabric-go-sdk的项目进行修改,成功创建起了多组织多对等节点的Fabric网络,但这远远不能满足正常开发使用的需求。本文将基于之前的工作内容,逐步实现多通道多排序节点的Fabric网络,并对创建以及踩坑过程做一个简单记录。
多通道搭建
组织结构
在之前已经搭建出3个组织,每个组织有2个peer节点的Fabric网络的基础上,多通道的方案计划使用3条通道进行划分,其中Org1的peer0、peer1和Org3的peer0加入Channel1,Org2的peer0、peer1和Org3的peer1加入Channel2,Org3的peer0、peer1加入Channel3,具体示意图如下
配置文件修改
fixtures
网络结构发生了变化,只需对生成通道以及相关锚节点的配置文件configtx.yaml
进行修改。对于crypto-config.yaml
和docker-compose.yaml
这两个文件与通道的配置没有关系,故不用作任何改动。
在configtx.yaml
中只需要按照网络结构修改Profiles
选项即可,这里直接贴出修改后的相关配置
1 | Profiles: |
修改完成后需要重新生成通道文件和对应的锚节点文件,直接修改脚本文件(脚本相关操作可以参考之前的介绍)。修改后的脚本会生成三个通道文件和五个锚节点文件,其中三个通道对应的channelID
分别为mychannel1、mychannel2和mychannel3
1 | create.sh |
config.yaml和main.go
回到项目根目录,main.go
和config.yaml
就是实现通道创建、安装链码、执行智能合约的客户端代码及其配置文件,每个通道由一个go文件和yaml配置文件来控制操作,所以实现三个通道的测试需要有3份go代码和yaml配置文件,对应通道123分别将go文件命名为main1.go、main2.go、main3.go,yaml文件命名为config1.yaml、config2.yaml、config3.yaml,这三套文件的起始状态都是原先main.go和config.yaml的副本
config.yaml
**client:**这里我将所有的client组织都改为org3,即将原先的org1整体替换为org3
Channels: config1.yaml
对应mychannel1
,相应的peers节点分别为peer0.org1.example.com
、peer1.org1.example.com
、peer0.org3.example.com
,另外两个配置文件中的对应部分以此类推
Organizations: 只需要保留通道1所对应的Org1和Org3两个组织,并且Org3中的节点只保留peer0,其他配置文件以此类推
Peers: 同上,只保留通道1中所加入的3各节点的信息
entityMatchers: 只保留org1和org3的相关信息
至此config1.yaml就修改完成了,别忘了参照上述步骤继续修改2和3的配置文件
main.go
以下步骤没有特别说明均指对
main1.go
的操作,其他go文件以此类推
orgs: 只保留对应通道的组织,并更改组织锚节点文件的文件名(OrgAnchorFile)和加入对应通道的节点数量(OrgPeerNum) ,比如main1.go
中的Org3改为
1 | { |
info: 修改ChannelID
和ChannelConfig
为对应的通道号(以main1.go为例)
1 | ChannelID: "mychannel1", |
sdk: 修改sdk, err := sdkInit.Setup("config.yaml", &info)
配置文件的文件名,即sdk, err := sdkInit.Setup("config1.yaml", &info)
ccpPath: 全部修改为org3的连接配置文件路径,由于默认生成的ccp文件使用的都是peer0,而在channel2中并没有org3的peer0,所以可以拷贝一份connection-org3.yaml
的副本,并修改其中的节点名和对应端口供main2.go使用
network: network, err := gw.GetNetwork("mychannel")
改为network, err := gw.GetNetwork("mychannel1")
populateWallet()函数: 将函数中所有的org1都改为 org3
报错踩坑记录
以下列出的报错及解决方案只作为我自己在操作时的踩坑记录,如遇同样报错,不保证解决方案对所有类似问题通用
>> 开始创建通道…
>> Create channel and join error: Create channel error: error should be nil for SaveChannel of orgchannel: create channel failed: create channel failed: SendEnvelope failed: calling orderer ‘orderer.example.com:7050’ failed: Orderer Server Status Code: (400) BAD_REQUEST. Description: Failing initial channel config creation: mismatched channel IDs: ‘mychannel1’ != ‘channel1’
这里是由于对channelID和channelName没做好区分造成的,只需注意配置文件和代码中用到的通道名都是channelID就行了(如mychannel1),只有在文件路径中才会出现channel*(如channel1.tx)
>>>> 使用每个org的管理员身份更新锚节点配置…
>> Create channel and join error: Create channel error: SaveChannel for anchor org Org3 error: create channel failed: create channel failed: SendEnvelope failed: calling orderer ‘orderer.example.com:7050’ failed: Orderer Server Status Code: (400) BAD_REQUEST. Description: error applying config update to existing channel ‘mychannel1’: error authorizing update: ConfigUpdate for channel ‘mychannel3’ but envelope for channel ‘mychannel1’
这里是由于把所有的通道配置都放在了一个config.yaml
中,将其他不必要的配置删掉即可
>> 开始安装链码…
panic: runtime error: index out of range [0] with length 0goroutine 1 [running]:
fabric-go-fabcar/sdkInit.installCC({0xc000132180?, 0xc00012e008?}, {0xc0011d0000?, 0x1?, 0x1?}, {0xc0001fbaa0, 0x2, 0x1?})
/home/baymrx/go/src/fabric-go-fabcar/sdkInit/sdkSetting.go:216 +0x305
fabric-go-fabcar/sdkInit.CreateCCLifecycle(0xc000198840, 0xc00022dd10?, 0x40?, 0x1271920?)
/home/baymrx/go/src/fabric-go-fabcar/sdkInit/sdkSetting.go:136 +0x233
main.main()
/home/baymrx/go/src/fabric-go-fabcar/main1.go:76 +0x36a
exit status 2
报错问题在于数组出界,跟踪一下代码可以发现问题出在main.go中的orgs数组变量中,由于Org3只有一个peer节点加入Channel1,而数组中对应Org3的OrgPeerNum
设置为2,造成运行的时候数组越界,数量改成1就好了
>> 组织认可智能合约定义…
>>> chaincode approved by Org1 peers:
peer0.org1.example.com:7051
peer1.org1.example.com:8051
>>> chaincode approved by Org3 peers:
peer0.org3.example.com:11051
[fabsdk/util] 2022/08/11 07:40:17 UTC - lazyref.(*Reference).refreshValue -> WARN Error - initializer returned error: QueryBlockConfig failed: QueryBlockConfig failed: target(s) required. Will retry again later
>> create chaincode lifecycle error: %v queryApprovedCC error: Org Org3 Peer peer0.org3.example.com:11051 NewInvoker error: Test status Code: (12) UNKNOWN. Description: LifecycleQueryApprovedCC returned error: querying for installed chaincode failed: Transaction processing for endorser [peer0.org3.example.com:11051]: Chaincode status Code: (500) UNKNOWN. Description: failed to invoke backing implementation of ‘QueryApprovedChaincodeDefinition’: could not fetch approved chaincode definition (name: ‘fabcar’, sequence: ‘1’) on channel ‘mychannel1’exit status 255
>> 组织认可智能合约定义…
>>> chaincode approved by Org3 peers:
peer0.org3.example.com:11051
peer1.org3.example.com:12051
[fabsdk/util] 2022/08/11 07:44:48 UTC - lazyref.(*Reference).refreshValue -> WARN Error - initializer returned error: QueryBlockConfig failed: no channel peers configured for channel [mychannel3]. Will retry again later
>> create chaincode lifecycle error: %v queryApprovedCC error: Org Org3 Peer peer0.org3.example.com:11051 NewInvoker error: Test status Code: (12) UNKNOWN. Description: LifecycleQueryApprovedCC returned error: querying for installed chaincode failed: Transaction processing for endorser [peer0.org3.example.com:11051]: Chaincode status Code: (500) UNKNOWN. Description: failed to invoke backing implementation of ‘QueryApprovedChaincodeDefinition’: could not fetch approved chaincode definition (name: ‘fabcar’, sequence: ‘1’) on channel ‘mychannel3’
exit status 255
这两个错误都是搞混了channelID与channel名造成的,在config.yaml
中channels:
下面的选项错误地使用了channel1
,改成mychannel1
报错消失
>> 设置链码状态完成
Failed to get network: Failed to create new channel client: event service creation failed: could not get chConfig cache reference: QueryBlockConfig failed: QueryBlockConfig failed: queryChaincode failed: Transaction processing for endorser [localhost:7051]: Chaincode status Code: (500) UNKNOWN. Description: channel ‘mychannel’ not found
exit status 1
还有未更改的channelID,检索全文,在main.go
中有一行代码
1 | network, err := gw.GetNetwork("mychannel") |
将mychannel
改为mychannel1
之后解决问题
1 | network, err := gw.GetNetwork("mychannel1") |
>> 设置链码状态完成
Failed to get network: Failed to create new channel client: event service creation failed: could not get chConfig cache reference: QueryBlockConfig failed: QueryBlockConfig failed: queryChaincode failed: Transaction processing for endorser [localhost:7051]: Chaincode status Code: (500) UNKNOWN. Description: channel ‘mychannel3’ not found
exit status 1
报错是在执行main3.go
,报错信息分析一下,localhost:7051
对应的节点找不到mychannel3
,7051端口在配置中对应的是Org1的peer0节点,而通道3中只有Org3的peer节点,所以找不到是必然。排查config.yaml
,其中的ccpPath
路径没有修改,连接配置依然是使用的org1,将其改为org3的路径。同样,将populateWallet()
函数中的组织也都改为org3,问题解决
>> 设置链码状态完成
Failed to get network: Failed to create new channel client: event service creation failed: could not get chConfig cache reference: QueryBlockConfig failed: QueryBlockConfig failed: queryChaincode failed: Transaction processing for endorser [peer0.org3.example:11051]: Endorser Client Status Code: (2) CONNECTION_FAILED. Description: dialing connection on target [peer0.org3.example:11051]: connection is in TRANSIENT_FAILURE
exit status 1
重新生成证书及公私钥文件后,没有清除钱包文件夹,使用rm -rf wallet keystore
删除之前的钱包文件即可
多排序节点搭建
多排序节点方案计划使用三个orderer节点和EtcdRaft排序共识算法进行搭建,对应的三个orderer节点的地址及端口号如下
配置文件修改
configtx.yaml
Organizations选项:
需要修改OrdererOrg
中的OrdererEndpoints
1 | OrdererEndpoints: |
Orderer选项:
1 | # Orderer Type: The orderer implementation to start |
crypto-config.yaml
OrdererOrgs选项:
1 | OrdererOrgs: |
docker-compose.yaml
在volumes中添加新的orderer节点
1 | volumes: |
service中同样添加新节点配置
添加后记得修改节点端口和文件路径的映射,以orderer1为例:
1 | orderer1.example.com: |
config.yaml
orderers选项
三个config文件中将3个排序节点的配置文件都添加上
1 | orderers: |
main.go
info参数
修改其中的OrdererEndpoint
为orderer0.example.com
sdkInit/sdkSetting.go
与上一条相对应,在sdkInit/sdkSetting.go
中有几处函数参数原先使用的是固定字符串,需要对应修改为变量
将代码中的resmgmt.WithOrdererEndpoint("orderer.example.com")
改为resmgmt.WithOrdererEndpoint(info.OrdererEndpoint)
(直接搜索全部替换)
报错踩坑记录
>> 开始创建通道…
>> Create channel and join error: Create channel error: error should be nil for SaveChannel of orgchannel: create channel failed: create channel failed: SendEnvelope failed: calling orderer ‘orderer0.example.com:7050’ failed: Orderer Client Status Code: (2) CONNECTION_FAILED. Description: dialing connection on target [orderer0.example.com:7050]: connection is in TRANSIENT_FAILURE>> exit status 255
报错原因是之前误将crypto-config.yaml
中的OrdererOrgs
改成了下面的配置,按照上面所使用的正确配置即可解决
1 | OrdererOrgs: |
源码
最后把整个项目源码附上,下载链接
为了减少压缩包大小,项目中移除了Tape性能测试工具,需要时自行配置。代码使用时需要将所有文件中与描述文件路径相关的源码全部替换为自己的工作目录,并且fabric-samples
需要与本项目在统一根目录下。
本文主要资料参考:
Fantasy大佬的博客 (Github)FabricLearn-实现一个工作室联盟链