(二)HyperledgerFarbic1.4-創建新的聯盟

增加一個聯盟信息

以官方fabric-samples項目中 的first-network 項目爲例。

1 增加聯盟的原理

因爲fabric的配置信息都是存放在orderer節點的sys_channel中,在byfn.sh的腳本中,可以找打關於sys_channel的配置信息。

# system channel name defaults to "byfn-sys-channel"
# 系統channel的名稱 默認爲byfn-sys-channel
SYS_CHANNEL="byfn-sys-channel"

# 如果排序節點以solo方式啓動,則以TwoOrgsOrdererGenesis這個配置節點下的配置來生成創世區塊 並存記錄到系統channel中
if [ "$CONSENSUS_TYPE" == "solo" ]; then
    configtxgen -profile TwoOrgsOrdererGenesis -channelID $SYS_CHANNEL -outputBlock ./channel-artifacts/genesis.block

上一節時已經查看到了現在orderer節點中存放的配置信息並轉成了json。其中關於聯盟的配置如下。
在這裏插入圖片描述
對應了first-network中的聯盟配置configtx.yaml
在這裏插入圖片描述
所以 要想新增聯盟 就需要在configtx.yaml配置文件中修改聯盟的配置信息,並提交到區塊鏈中做增量數據。

2 修改first-network中的configtx.yaml配置文件

在本地路徑
~/fabric-samples/first-network/configtx.yaml 修改文件內容 增加TestConsortium

默認是用solo模式啓動所以 要修改的配置節點是TwoOrgsOrdererGenesis
修改之前可以先備份

TwoOrgsOrdererGenesis:
  <<: *ChannelDefaults
  Orderer:
     <<: *OrdererDefaults
     Organizations:
        - *OrdererOrg
     Capabilities:
        <<: *OrdererCapabilities
  Consortiums:
     SampleConsortium:
        Organizations:
           - *Org1
           - *Org2
     TestConsortium:   # 增加新的聯盟 名稱爲TestConsortium
        Organizations:  # 組織機構配置節點
           - *Org1  # 將org1配置到該聯盟中

3 將新的配置信息輸出爲json格式

因爲我本地mac環境下沒有jq工具 所以 我在cli容器中執行修改配置文件 生成新區塊的操作

由於生成config區塊信息 需要制定配置文件和orderer節點的證書地址
所以吧
~/fabric-samples/first-network/configtx.yaml文件 複製到
~/fabric-samples/first-network/channel-artifacts/configtx.yaml
然後吧證書 也複製
~/fabric-samples/first-network/crypto-config/
~/fabric-samples/first-network/channel-artifacts/crypto-config/
因爲在docker啓動時 容器已經掛在了這個channel-artifacts文件目錄。所以進入cli容器後,在容器中的channel-artifacts也可以找到複製過去的配置文件和證書

進入到cli容器中 驗證文件是否成功掛載

# 第一步 進入容器
docker exec -it cli base

# 切換到orderder的 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
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/[email protected]/msp


# 第二步 查看當前所在目錄
pwd
# 輸出:/opt/gopath/src/github.com/hyperledger/fabric/peer/

# 第三步 查看目錄下的文件夾
ls
# 輸出:channel-artifacts  crypto  log.txt  mychannel.block  scripts

# 第四步 進入channel-artifacts 
cd channel-artifacts 

# 第五步 查看文件
ls
# 輸出 中包含準備好的 configtx.yaml  crypto-config  驗證完成

#  第六步 進入目錄 執行生成新配置區塊的指令
cd /opt/gopath/src/github.com/hyperledger/fabric/peer

# 第七步 生成包含新聯盟的新配置區塊 覆蓋原來的區塊
configtxgen -configPath=./channel-artifacts/  -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

可以查看輸出的TestConsortium.json文件
在這裏插入圖片描述
因爲在 將區塊內容轉換成json的時候我們只抽取了新的聯盟信息輸出 所以 這個json中只包含新聯盟的配置信息。
這裏可以看到Org1的配置信息已經生成了。
我們要做的就是吧這個json信息 添加到原來的sys_config.josn中 並生成新的區塊 提交到sys_channel 中 給其他節點同步

4 獲取到當前網絡中正在使用的配置節點

也就是 # 七、查看網絡中聯盟的配置數據 最後獲取到的那個sys_config.json
獲取到當前的配置文件,並且生成了新的增量配置 TestConsortium.json。
接下來需要的就是 把增量的配置TestConsortium.json 添加到 原來的sys_config.json中去

5 向sys_config.json中增加增量聯盟信息

將新聯盟TestConsortium配置定義TestConsortium.json添加到channel的Consortiums的TestConsortium中,並將最終生成文件 寫入sys_updated_config.json
依然是在容器中 使用orderer的admin用戶 並在
/opt/gopath/src/github.com/hyperledger/fabric/peer
目錄下執行

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

生成之後sys_updated_config.json
會有以下內容 已經變成了兩個聯盟的信息了
在這裏插入圖片描述

6 計算區塊增量數據

將上一步生成的sys_updated_config.json文件轉爲pb格式 提價增量計算
依然是在容器中 使用orderer的admin用戶 並在
/opt/gopath/src/github.com/hyperledger/fabric/peer下操作

只有pb格式 纔可以計算增量 所以 第一步和第二步先將原始的配置和新的配置轉成pb格式

 # 將原始的配置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

增量計算完成後 都會記錄在sys_config_update.pb 文件中

7 生成發送給orderer節點的envelope數據

先將計算增量完成後的sys_config_update.pb轉成json
依然是在容器中 使用orderer的admin用戶 並在
/opt/gopath/src/github.com/hyperledger/fabric/peer下操作

# 將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.json
其中的write_set 中 包含了我們本次增加的TestConsortium數據

要組裝一個標準的envelope格式的數據 需要封裝一個頭信息

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

該頭信息 標誌着 本次是一次 config_update的操作
完成之後查看該json,頭部信息已經添加進去了
在這裏插入圖片描述
然後 把sys_config_update_in_envelope.json數據 轉成pb格式 準備提交給orderer節點

configtxlator proto_encode --input ./channel-artifacts/sys_config_update_in_envelope.json --type common.Envelope --output ./channel-artifacts/sys_config_update_in_envelope.pb

之後會生成文件./channel-artifacts/sys_config_update_in_envelope.pb

8 向orderer發送envelope

之前使用orderer的admin用戶 組裝好了一個信封,信封的內容是創建一個新的聯盟並將org1添加到該聯盟中
使用一下命令將信封數據提交到orderer節點

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

出現一下的提示 表示已經成功的將增加聯盟的數據提交到了order節點
在這裏插入圖片描述

9 查看修改聯盟結果

在orderer的admin用戶下操作。
可以獲取最新的區塊數據

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

可以看到接收到新區塊的輸出在這裏插入圖片描述
現在會覆蓋掉之前的sys_config_block.pb
然後 將這個pb轉成json查看數據

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

同樣會覆蓋掉之前的數據
查看新的區塊數據
在這裏插入圖片描述
發現新的區塊中 已經包含了新的聯盟信息

10 創建Test聯盟對應的channel

修改容易映射目錄中的configtx.yaml配置文件 添加TestChannel數據

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

此處我們testConsortium裏面的是org1,所有無需切換環境變量,如果是其他org,則必須切換到該org的admin用戶

從新進入 cli容器 cli容器中 默認使用的就是 org1 中的admin用戶

docker exec -it cli bash

使用env命令 可以看到當前使用的用戶信息

CORE_PEER_LOCALMSPID=Org1MSP
CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt

生成testchannel對應的創世區塊的配置文件

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

執行成功後 會生成testchannel.tx這個channel的配置文件

配置好orderer-ca證書的位置

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

執行創建channel的命令

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

拿到testchannel中的第0個區塊 就是創世區塊

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

將org1當前的peer加入到testchannel中

peer channel join -b testchannel.block

提示加入成功
在這裏插入圖片描述
此時 可以查看peer0 已經加入的channel

peer channel list
輸出
Channels peers has joined: 
mychannel
testchannel

查看peer1加入的channel

切換到peer1 的用戶身份

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/[email protected]/msp
export CORE_PEER_ADDRESS=peer1.org1.example.com:8051

最後 export CORE_PEER_ADDRESS=peer1.org1.example.com:8051 的地址以自己容器中啓動的地址爲準

此時

peer channel list
輸出
Channels peers has joined: 
mychannel

peer1 加入到channel中

peer channel join -b testchannel.block
peer channel list #查看
輸出
Channels peers has joined: 
mychannel

到此 完成了 聯盟的創建以及節點加入到聯盟對應的channel中的操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章