Fabric v2.0 通道(channel)的创建与配置更新——新增组织

推荐阅读:通道(Channel)机制运行原理

1. 创建包含两个组织的channel

1.1 准备configtx.yaml文件

首先需要准备channel配置的configtx.yaml文件,同时需要将环境变量FABRIC_CFG_PATH设置为该文件所在目录。

configtx.yaml中的相关配置如下:

    TwoOrgsChannel:
        Consortium: SampleConsortium
        <<: *ChannelDefaults
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Org1
                - *Org2
            Capabilities:
                <<: *ApplicationCapabilities
  • 初始配置只包含两个组织Org1和Org2,channel创建成功后,我们会尝试在该channel中新增第三个组织Org3。
  • Capabilities引用了ApplicationCapabilities定义,用于确保网络和channel以相同的方式处理交易,使用版本号进行定义。
  • Application引用了ApplicationDefaults定义,控制应用程序channel的配置参数(添加/删除组织):修改这一部分配置需要大部分组织管理管理员的签名。要实现将组织添加到channel必须将组织的MSP等配置参数添加到组织配置。
  • 组织相关参数:可以更改组织特定的任何参数(例如,标识锚点peer或组织管理员的证书)。默认情况下,更改这些值将不需要大多数application组织管理员,而仅需要组织本身的管理员。

1.2 生成创建channel的tx文件

执行下面的命令,生成channel配置的tx文件:

configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel

-outputCreateChannelTx :输出tx文件路径
-channelID: 指定channel ID

1.3 创建channel

进入cli容器,执行下面命令根据上面生成的tx文件,创建channel:

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

-c:channel ID
-o:排序节点的服务域名、端口
-f:channel配置的tx文件所在路径

channel创建成功后,当前目录会产生channel的区块文件mychannel.block。

1.4 节点加入channel

切换到peer0.org1.example.com节点(切换方法见文章末尾),将该节点加入到channel:

peer channel join -b mychannel.block

-b:区块文件路径

使用下面命令可查看当前节点加入的所有channel:

peer channel list

节点加入成功后,查看排序节点的日志,可以看到排序节点写入了新的区块,同时为该channel创建了一个raft集群。

2. channel中新增组织

2.1 生成Org3证书配置

生成Org3的密钥与证书,作为该组织在Fabric网络中的身份。org3-crypto.yaml如下,包括两个节点和一个普通user:

PeerOrgs:
  - Name: Org3
    Domain: org3.example.com
    EnableNodeOUs: true
    Template:
      Count: 2
    Users:
      Count: 1

执行下面命令生成密钥和证书:

cd org3-artifacts
cryptogen generate --config=./org3-crypto.yaml

org3-artifacts路径下将会生成crypto-config证书目录。

还需将orderer的证书配置拷贝到org3-artifacts目录:

cp -r crypto-config/ordererOrganizations org3-artifacts/crypto-config/

2.2 生成Org3组织定义

生成Org3组织定义,配置文件为first-network/org3-artifacts/configtx.yaml:

cd org3-artifacts
export FABRIC_CFG_PATH=$PWD
configtxgen -printOrg Org3MSP > ../channel-artifacts/org3.json

channel-artifacts目录下将会产生org3.json文件。

Fabric网络中已经创建了创世区块与user channel的配置,后面步骤中会将新加的组织配置添加到区块配置中。

2.3 生成新增Org3的channel配置交易

首先获取该channel的最新配置区块:

peer channel fetch config config_block.pb -o orderer.example.com:7050 -c mychannel --tls --cafile $ORDERER_CA

解码获取到的区块文件config_block.pb,得到config.json文件:

configtxlator proto_decode --input config_block.pb --type common.Block | jq .data.data[0].payload.data.config > config.json

在config.json文件基础上增加新组织Org3,得到modified_config.json文件:

jq -s '.[0] * {"channel_group":{"groups":{"Application":{"groups": {"Org3MSP":.[1]}}}}}' config.json ./channel-artifacts/org3.json > modified_config.json

计算出前后的配置更新,得到config_update.json文件:

configtxlator proto_encode --input config.json --type common.Config >original_config.pb
configtxlator proto_encode --input modified_config.json --type common.Config >modified_config.pb
configtxlator compute_update --channel_id mychannel --original original_config.pb --updated modified_config.pb >config_update.pb
configtxlator proto_decode --input config_update.pb --type common.ConfigUpdate >config_update.json

将更新文件config_update.json包装在信封消息中,得到config_update_in_envelope.json文件:

echo '{"payload":{"header":{"channel_header":{"channel_id":"'$CHANNEL'", "type":2}},"data":{"config_update":'$(cat config_update.json)'}}}' | jq . >config_update_in_envelope.json

将该json文件编码为protobuf格式文件,得到org3_update_in_envelope.pb,即为最终的更新对象:

configtxlator proto_encode --input org3_update_in_envelope.json --type common.Envelope --output org3_update_in_envelope.pb

2.4 签名并提交配置更新交易

添加组织需要channel大部分的组织Admin签名(当前环境变量为peer0.org1.example.com):

peer channel signconfigtx -f org3_update_in_envelope.pb

切换环境为peer0.org2.example.com执行更新配置(切换方法见文章末尾),因为update也会为当前组织签名,所以不需要再org2的签名:

peer channel update -f org3_update_in_envelope.pb -c mychannel -o orderer.example.com:7050 --tls --cafile ${ORDERER_CA}

2.5 Org3的节点加入channel

启动Org3相关的节点容器,在first-network目录下执行:

docker-compose -f docker-compose-org3.yaml up -d

获取mychannel的0号创世区块:

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

当前环境变量为peer0.org3.example.com,使用创世区块将org3的节点加入channel:

peer channel join -b mychannel.block

查看当前节点加入的channel:

peer channel list

附录:环境变量

本文命令中使用的环境变量配置:

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
PEER0_ORG1_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
PEER0_ORG2_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
PEER0_ORG3_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt

cli中设置连接不同的节点,需要设置相应的环境变量:

  • peer0.org1.example.com
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/peer0.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=peer0.org1.example.com:7051
  • peer0.org2.example.com
export CORE_PEER_LOCALMSPID="Org2MSP"
export 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
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org2.example.com:9051
  • peer0.org3.example.com
export CORE_PEER_LOCALMSPID=Org3MSP
export 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
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org3.example.com:11051

参考资料:
github.com/hyperledger/fabric-samples/first-network/eyfn.sh

发布了45 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ice_fire_x/article/details/105250090
今日推荐