概述
在fabric中聯(lián)盟不能為空,必須包含一個組織機構(gòu),所以在創(chuàng)建聯(lián)盟的時候必須有一個組織機構(gòu),能夠添加進去,fabric中的聯(lián)盟和通道是一對一的關(guān)系,聯(lián)盟必須和通道channel并存,而聯(lián)盟的所有配置都是記錄在系統(tǒng)channel的配置區(qū)塊中的,包括有哪些聯(lián)盟,有哪些org,所以要添加聯(lián)盟就必須修改區(qū)塊中的數(shù)據(jù),更新配置,也就是重新上傳配置塊。
系統(tǒng)channel的配置塊是根據(jù)configtx.yaml的Section: Profile中的配置文件來創(chuàng)建的,如下所示以TestConsortium聯(lián)盟為例,如需視頻學習,可以參考視頻教程.。
TwoOrgsOrdererGenesis:
<<: *ChannelDefaults
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Capabilities:
<<: *OrdererCapabilities
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TestConsortium:
Organizations:
- *Org1
```
以JSON格式輸出**新聯(lián)盟**的配置材料
生成包含新聯(lián)盟的新創(chuàng)世區(qū)塊
configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/sys-channel.block
將其內(nèi)容轉(zhuǎn)換成JSON并抽取出新聯(lián)盟的配置材料
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
### 獲取系統(tǒng)通道的創(chuàng)世區(qū)塊
- 以JSON格式輸出**新聯(lián)盟**的配置材料
生成包含新聯(lián)盟的新創(chuàng)世區(qū)塊
configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/sys-channel.block
將其內(nèi)容轉(zhuǎn)換成JSON并抽取出新聯(lián)盟的配置材料
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. 獲取系統(tǒng)通道的創(chuàng)世區(qū)塊**
- (可選)設(shè)置了`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
```
原因:系統(tǒng)通道的相關(guān)操作必須由`OrdererOrgs`的admin用戶來執(zhí)行
- 使用`peer channel fetch`命令獲取系統(tǒng)通道的創(chuàng)世區(qū)塊
```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`參數(shù)是`--channelID`的簡寫,此處需要使用系統(tǒng)通道ID,即在調(diào)用`configtxgen`創(chuàng)建orderer創(chuàng)世區(qū)塊時所指定的channelID。
- 將創(chuàng)世區(qū)塊中的內(nèi)容轉(zhuǎn)換成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
```
- 將新聯(lián)盟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
```
- **創(chuàng)建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發(fā)送配置更新(<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
```
(可選)測試聯(lián)盟,創(chuàng)建channel,只要是聯(lián)盟的成員的admin都可以創(chuàng)建channel
編輯configtx.yaml找到channel創(chuàng)建的配置文件的位置,編寫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,所有無需切換環(huán)境變量,如果是其他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 #查看