Published on

fabric 在旧网络中加入新channel

Authors

前置条件

  1. 已搭建好fabric网络
  2. 对整个fabric网络结构有一定了解

需求

在原来的网络中,加入一个新的channel作为一个溯源业务的链。

  • 3个org
  • 3个CA Server,为不同组织注册新用户

1. 创建channel

echo "-----------创建一个新channel---------------"                                                            
echo "-------------------------------------------"                                                       

echo "设置通道名称,通道tx文件目录,锚节点目录"                                                                                                         
export CHANNEL_NAME=zhujiangtest                                                                         
CHANNEL_ARTIFACTS_DIR=zhujiang-channel-artifacts                                                         
rm -rf $CHANNEL_ARTIFACTS_DIR                                                                            
mkdir $CHANNEL_ARTIFACTS_DIR                                                                             
                                                                                                         
echo "根据configtx.yaml 生成通道tx文件"
configtxgen -profile AllOrgsChannel -outputCreateChannelTx ./${CHANNEL_ARTIFACTS_DIR}/${CHANNEL_NAME}.tx 
channelID $CHANNEL_NAME                                                                                  

echo "根据实际需要,生成锚节点,锚节点的组织个数对应该channel中加入的组织数"                                                                                                         
configtxgen -profile AllOrgsChannel -outputAnchorPeersUpdate ./${CHANNEL_ARTIFACTS_DIR}/Org1MSPanchors.tx
-channelID $CHANNEL_NAME -asOrg Org1MSP                                                                  
configtxgen -profile AllOrgsChannel -outputAnchorPeersUpdate ./${CHANNEL_ARTIFACTS_DIR}/Org2MSPanchors.tx
-channelID $CHANNEL_NAME -asOrg Org2MSP                                                                  
configtxgen -profile AllOrgsChannel -outputAnchorPeersUpdate ./${CHANNEL_ARTIFACTS_DIR}/Org3MSPanchors.tx
-channelID $CHANNEL_NAME -asOrg Org3MSP                                                                  

在这里我只需要三个组织加入。 路径都是相对路径,自己根据情况微调。

2. 创建通道区块

先写好一个cli的docker-compose文件

version: '2'

services:
  cli-operate:
    container_name: cli-operate
    image: hyperledger/fabric-tools:1.4.0
    extends:
      file: ../base/cli-base.yaml
      service: cli-base
    tty: true
    environment:
      - CORE_PEER_ID=cli-operate

      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - 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_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
      - 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
      - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: /bin/bash -c 'sleep 999999999'
    volumes:
        - /var/run/:/host/var/run/
        - ../../artifacts/crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
        - ../../artifacts/zhujiang-channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts

再写好一个脚本,创建channel.block,加入区块,更新锚节点。

#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
set -e

CAFILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer1.example.com/tls/ca.crt
CHANNEL_NAME=zhujiangtest

function operate() {
        echo "=================start test offical cc=================="
        echo "============ peer channel create:peer channel create -o orderer1.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls true --cafile $CAFILE"
        peer channel create -o orderer1.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/$CHANNEL_NAME.tx --tls true --cafile $CAFILE
        echo "============ peer channel join -b $CHANNEL_NAME.block"
        peer channel join -b $CHANNEL_NAME.block
        sleep 2
        echo "============ peer channel update -o orderer1.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile $CAFILE"
        peer channel update -o orderer1.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile $CAFILE
        sleep 2

        echo "============ 切换到Org2MSP"
        CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.crt
        CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.key
        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
        CORE_PEER_ADDRESS=peer0.org2.example.com:7051
        CORE_PEER_LOCALMSPID=Org2MSP
        CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
        echo "============ peer channel join -b $CHANNEL_NAME.block"
        peer channel join -b $CHANNEL_NAME.block
        sleep 2
        echo "============ peer channel update -o orderer1.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile $CAFILE"
        peer channel update -o orderer1.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile $CAFILE
        sleep 2

        echo "============ 切换到Org3MSP"
        CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/server.crt
        CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/server.key
        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
        CORE_PEER_ADDRESS=peer0.org3.example.com:7051
        CORE_PEER_LOCALMSPID=Org3MSP
        CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp
        echo "============ peer channel join -b $CHANNEL_NAME.block"
        peer channel join -b $CHANNEL_NAME.block
        sleep 2
        echo "============ peer channel update -o orderer1.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile $CAFILE"
        peer channel update -o orderer1.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org3MSPanchors.tx --tls --cafile $CAFILE

}
operate
exit 0

只需要一个peer的admin 创建channel.block即可,后面的peer直接加入block,更新锚节点即可。 以上命令都可以手动敲入 在cli中运行bash script/xxx.sh 注意自己的环境变量,运行路径设置

注意事项

  • 遇到一个问题,创建的时候漏了创建一个锚节点,可以敲命令手动创建。
  • 手贱删了channel.bock,可以通过channel fetch 0 channel.block 获取,记得指定orderer和tls证书
  • 加入channel.block一定要是创世区块,所以上面获取的是0号区块,然后join,更新锚节点。
  • 在每个peer用 peer channel list查看是否加入成功。