hyperledger fabric 1.4 创建联盟

概述

在fabric中联盟不能为空,必须包含一个组织机构,所以在创建联盟的时候必须有一个组织机构,能够添加进去,fabric中的联盟和通道是一对一的关系,联盟必须和通道channel并存,而联盟的所有配置都是记录在系统channel的配置区块中的,包括有哪些联盟,有哪些org,所以要添加联盟就必须修改区块中的数据,更新配置,也就是重新上传配置块。
系统channel的配置块是根据configtx.yamlSection: Profile中的配置文件来创建的,如下所示以TestConsortium联盟为例,如需视频学习,可以参考视频教程.。

TwoOrgsOrdererGenesis:
  <<: *ChannelDefaults
  Orderer:
     <<: *OrdererDefaults
     Organizations:
        - *OrdererOrg
     Capabilities:
        <<: *OrdererCapabilities
  Consortiums:
     SampleConsortium:
        Organizations:
           - *Org1
           - *Org2
     TestConsortium:
        Organizations:
           - *Org1
           ```
以JSON格式输出**新联盟**的配置材料

生成包含新联盟的新创世区块

configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/sys-channel.block

将其内容转换成JSON并抽取出新联盟的配置材料

configtxlator proto_decode --input ./channel-artifacts/sys-channel.block --type common.Block | jq .data.data[0].payload.data.config.channel_group.groups.Consortiums.groups.TestConsortium > ./channel-artifacts/TestConsortium.json

### 获取系统通道的创世区块
- 以JSON格式输出**新联盟**的配置材料

生成包含新联盟的新创世区块

configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/sys-channel.block

将其内容转换成JSON并抽取出新联盟的配置材料

configtxlator proto_decode --input ./channel-artifacts/sys-channel.block --type common.Block | jq .data.data[0].payload.data.config.channel_group.groups.Consortiums.groups.TestConsortium > ./channel-artifacts/TestConsortium.json


1. 获取系统通道的创世区块**

   - (可选)设置了`ORDERER_CA`变量:

     ```
     docker exec -it cli bash
     export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
     ```

   - 切换到`OrdererOrgs`的admin用户

     ```bash
     export CORE_PEER_LOCALMSPID="OrdererMSP"
     export CORE_PEER_TLS_ROOTCERT_FILE=$ORDERER_CA
     export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/users/Admin@example.com/msp
     ```

     原因:系统通道的相关操作必须由`OrdererOrgs`的admin用户来执行

   - 使用`peer channel fetch`命令获取系统通道的创世区块

     ```bash
     peer channel fetch config ./channel-artifacts/sys_config_block.pb -o orderer.example.com:7050 -c byfn-sys-channel --tls --cafile $ORDERER_CA
     ```

     其中`-c`参数是`--channelID`的简写,此处需要使用系统通道ID,即在调用`configtxgen`创建orderer创世区块时所指定的channelID。

   - 将创世区块中的内容转换成JSON并对其进行修剪

     ```bash
     exit
     configtxlator proto_decode --input ./channel-artifacts/sys_config_block.pb --type common.Block | jq .data.data[0].payload.data.config > ./channel-artifacts/sys_config.json
     ```

   - 将新联盟TestConsortium配置定义`TestConsortium.json`添加到channel的`Consortiums`的`TestConsortium`中,并将其写入`sys_updated_config.json`

     ```bash
     jq -s '.[0] * {"channel_group":{"groups":{"Consortiums":{"groups": {"TestConsortium": .[1]}}}}}' ./channel-artifacts/sys_config.json ./channel-artifacts/TestConsortium.json >& ./channel-artifacts/sys_updated_config.json
     ```

   - **创建Config Update**

     - 配置增量计算

       ```bash
       # 将原始的配置sys_config.json编码成protobuf
       configtxlator proto_encode --input ./channel-artifacts/sys_config.json --type common.Config --output ./channel-artifacts/sys_config.pb
       # 将更新后的配置sys_updated_config.json编码成protobuf
       configtxlator proto_encode --input ./channel-artifacts/sys_updated_config.json --type common.Config --output ./channel-artifacts/sys_updated_config.pb
       # 配置增量计算
       configtxlator compute_update --channel_id byfn-sys-channel --original ./channel-artifacts/sys_config.pb --updated ./channel-artifacts/sys_updated_config.pb --output ./channel-artifacts/sys_config_update.pb
       ```

     - Generating config update and wrapping it in an envelope

       ```bash
       # 将sys_config_update.pb编码成json
       configtxlator proto_decode --input ./channel-artifacts/sys_config_update.pb --type common.ConfigUpdate | jq . > ./channel-artifacts/sys_config_update.json
       # 生成sys_config_update_in_envelope.json
       echo '{"payload":{"header":{"channel_header":{"channel_id":"byfn-sys-channel", "type":2}},"data":{"config_update":'$(cat ./channel-artifacts/sys_config_update.json)'}}}' | jq . > ./channel-artifacts/sys_config_update_in_envelope.json
       # 将sys_config_update_in_envelope.json编码成protobuf
       configtxlator proto_encode --input ./channel-artifacts/sys_config_update_in_envelope.json --type common.Envelope --output ./channel-artifacts/sys_config_update_in_envelope.pb
       ```

   - **向orderer发送配置更新(<u>必须使用OrdererOrg的admin用户</u>)**

     ```bash
     docker exec -it cli bash
     
     export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
     export CORE_PEER_LOCALMSPID="OrdererMSP"
     export CORE_PEER_TLS_ROOTCERT_FILE=$ORDERER_CA
     export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/users/Admin@example.com/msp
     
     peer channel update -f ./channel-artifacts/sys_config_update_in_envelope.pb -c byfn-sys-channel -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA
     ```

(可选)测试联盟,创建channel,只要是联盟的成员的admin都可以创建channel

编辑configtx.yaml找到channel创建的配置文件的位置,编写channel配置文件

TestChannel:
Consortium: TestConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
Capabilities:
<<: *ApplicationCapabilities




configtxgen -profile TestChannel -outputCreateChannelTx ./channel-artifacts/testchannel.tx -channelID testchannel


docker exec -it cli bash


此处我们testConsortium里面的是org1,所有无需切换环境变量,如果是其他org,则必须切换到该org的admin用户

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

peer channel create -o orderer.example.com:7050 -c testchannel -f ./channel-artifacts/testchannel.tx --tls --cafile $ORDERER_CA

peer channel fetch 0 testchannel.block -o orderer.example.com:7050 -c testchannel --tls --cafile $ORDERER_CA

peer channel join -b testchannel.block
peer channel list #查看

export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=peer1.org1.example.com:7051

peer channel join -b testchannel.block
peer channel list #查看




©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容