如何更新一個channel的orderer地址
詳細文件請參考:fabric文檔:Updating a Channel Configuration
假設(shè)CHANNEL_NAME=mychannel
Step 1:得到channel的最新配置塊(config block)
#!/bin/bash
export CHANNEL=mychannel
export CORE_PEER_LOCALMSPID=Org1MSP
export CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
peer channel fetch config config.pb \
-o orderer.example.com:7050 \
-c ${CHANNEL} \
--tls --cafile crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
生成本地配置塊文件config.pb
注意:peer channel fetch config
拿到的最新的配置塊藏澳,而如果使用peer channel fetch 0
則是拿到第一個配置塊赌结,第一個配置塊就是channel的第一個塊哎甲。從log也能看出其中的差異:
如果是0:
2018-08-02 05:30:49.968 UTC [channelCmd] readBlock -> DEBU 00a Received block: 0
如果是config:
2018-08-02 05:30:52.896 UTC [channelCmd] readBlock -> DEBU 00a Received block: 6
這個數(shù)字會根據(jù)配置的更新歷史發(fā)生變化博脑。
Step 2:從config.pb中提取有效的數(shù)據(jù),并轉(zhuǎn)換成可編輯json格式
configtxlator proto_decode --input config.pb --type common.Block | jq .data.data[0].payload.data.config > config.json
Step 3:修改config.json里面的orderer地址
例如修改orderer地址屁倔,或者添加orderer地址脑又。
"OrdererAddresses": {
"mod_policy": "/Channel/Orderer/Admins",
"value": {
"addresses": [
"orderer.example.com:7050"
]
},
"version": "0"
}
修改后保存為新的文件名,例如config_update.json
或者使用jq更新:
jq '.channel_group.values.OrdererAddresses.value.addresses = ["orderer.example.com:7050","orderer2.example.com:7050"]' config.json > config_update.json
Step 4:把前后的兩個json文件重新轉(zhuǎn)換回pb類型文件
configtxlator proto_encode --input config.json --type common.Config --output config.pb
configtxlator proto_encode --input config_update.json --type common.Config --output config_update.pb
注意兩個都要改锐借,替換原來從channel里面直接拿到的config.pb文件问麸,否則無法比較。
Step 5:比較前后兩個pb文件的差異钞翔,即改動部分
configtxlator compute_update \
--channel_id mychannel \
--original config.pb \
--updated config_update.pb \
--output mychannel_update.pb
Step 6:把配置變化部分轉(zhuǎn)換成json格式
configtxlator proto_decode --input mychannel_update.pb --type common.ConfigUpdate | jq . > mychannel_update.json
Step 7:為上述json文件添加頭部信息(Header)
封裝成一個完整的config update請求严卖。
echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel", "type":2}},"data":{"config_update":'$(cat mychannel_update.json)'}}}' | jq . > mychannel_update_in_envelope.json
Step 8:把封裝好的json文件轉(zhuǎn)換回pb格式文件
configtxlator proto_encode --input mychannel_update_in_envelope.json --type common.Envelope --output mychannel_update_in_envelope.pb
Step 9:獲取簽名
#!/bin/bash
export CHANNEL=mychannel
export CORE_PEER_LOCALMSPID=Org1MSP
export CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
peer channel signconfigtx -f ${CHANNEL}config_update_in_envelope.pb \
-o orderer.example.com:7050 \
--tls \
--cafile crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Step 10:提交修改請求到orderer
#!/bin/bash
export CHANNEL=mychannel
# Notice, there must use orderer MSP,otherwise, you will see following error:
# Error: got unexpected status: BAD_REQUEST -- error authorizing update: error validating DeltaSet: policy for [Value] /Channel/OrdererAddresses not satisfied: Failed to reach implicit threshold of 1 sub-policies, required 1 remaining
# and from orderer log, you may find:
# orderer.example.com | 2018-08-02 05:51:23.892 UTC [cauthdsl] func2 -> DEBU 253 0xc420116060 identity 0 does not satisfy principal: the identity is a member of a different MSP (expected OrdererMSP, got Org1MSP)
export CORE_PEER_LOCALMSPID=OrdererMSP
export CORE_PEER_MSPCONFIGPATH=crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp
peer channel update -f mychannel_update_in_envelope.pb \
-o orderer.example.com:7050 \
-c ${CHANNEL} \
--tls \
--cafile crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem