Hyperledger Fabricチュートリアル(14)-組織を動的に追加する手順

1.新しい組織の証明書と新しい組織のチャネル構成jsonファイルを生成します

§1証明書を生成する

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

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

# ---------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs:
  # ---------------------------------------------------------------------------
  # Org3
  # ---------------------------------------------------------------------------
  - Name: Org3
    Domain: org3.example.com
    EnableNodeOUs: true
    Template:
      Count: 2
    Users:
      Count: 1
shijianfeng@ubuntu:~/fabric-samples/first-network/org3-artifacts$ ../../bin/cryptogen generate --config=./org3-crypto.yaml
org3.example.com
shijianfeng@ubuntu:~/fabric-samples/first-network/org3-artifacts$ ls -lrt
total 12
-rw-rw-r-- 1 shijianfeng shijianfeng  607 Jan  7 18:44 org3-crypto.yaml
-rw-rw-r-- 1 shijianfeng shijianfeng 1042 Jan  7 18:44 configtx.yaml
drwxr-xr-x 3 shijianfeng shijianfeng 4096 Jan  8 21:53 crypto-config
shijianfeng@ubuntu:~/fabric-samples/first-network/org3-artifacts$ ls -lrt crypto-config/peerOrganizations/org3.example.com/
total 20
drwxr-xr-x 2 shijianfeng shijianfeng 4096 Jan  8 21:53 tlsca
drwxr-xr-x 2 shijianfeng shijianfeng 4096 Jan  8 21:53 ca
drwxr-xr-x 4 shijianfeng shijianfeng 4096 Jan  8 21:53 peers
drwxr-xr-x 4 shijianfeng shijianfeng 4096 Jan  8 21:53 users
drwxr-xr-x 5 shijianfeng shijianfeng 4096 Jan  8 21:53 msp
shijianfeng@ubuntu:~/fabric-samples/first-network/org3-artifacts$ 

 

§2org3チャネル構成のJSONファイルを生成します

../../bin/configtxgen -printOrg Org3MSP > ../channel-artifacts/org3.json

まず、チャネルファイルがどのように書き込まれるかを確認します

#

---
################################################################################
#
#   Section: Organizations
#
#   - This section defines the different organizational identities which will
#   be referenced later in the configuration.
#
################################################################################
Organizations:
    - &Org3
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org3MSP

        # ID to load the MSP definition as
        ID: Org3MSP

        MSPDir: crypto-config/peerOrganizations/org3.example.com/msp

        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org3.example.com
              Port: 7051
shijianfeng@ubuntu:~/fabric-samples/first-network/org3-artifacts$ ../../bin/configtxgen -printOrg Org3MSP > ../channel-artifacts/org3.json
2021-01-08 22:20:05.349 PST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2021-01-08 22:20:05.351 PST [msp] getMspConfig -> INFO 002 Loading NodeOUs
shijianfeng@ubuntu:~/fabric-samples/first-network/org3-artifacts$ vim ../channel-artifacts/org3.json

チャネルに対する組織の読み取りおよび書き込み権限

 

 

 

§3元のクラスターの注文組織のID証明書ファイルをorg3-artifacts / crypto-config /にコピーします

cd .. && cp -r crypto-config/ordererOrganizations org3-artifacts/crypto-config/

 

 

 

 

2.新しい構成を使用してチャネルを更新します

チャネル構成を更新し、コンテナーcliでscripts /step1org3.shを実行します

§1CLIコンテナを入力します

docker exec -it cli bash

 

§2jq--JSONセレクターをインストールします

echo "nameserver 8.8.8.8" | tee -a /etc/resolv.conf&&apt-get -y update&&apt-get -y install jq

 

 

§3注文者の環境変数を設定します

CORE_PEER_LOCALMSPID="OrdererMSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/users/[email protected]/msp

 

 

 

§4既存のチャネルを構成するトランザクションブロックを取得します

peer channel fetch config config_block.pb -o orderer.example.com:7050 -c mychannel --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

 

 

 

§5既存のチャネルのトランザクションブロックをJSONに変換するように構成し、トランザクションのコンテンツを抽出します

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

jqはトランザクション部分のコンテンツのみを取得し、カプセル化部分を削除しました

 

 

 

 

§6Org3の従来の構成、および新しいフォーマット構成を生成するために組み合わさたチャネルJson

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

 

 

 

§7既存のチャネルのjson構成をバイナリに変換します

configtxlator proto_encode --input config.json --type common.Config > original_config.pb

 

 

§8org3をバイナリにマージした後に構成ファイルを変換します

configtxlator proto_encode --input modified_config.json --type common.Config > modified_config.pb

 

 

§9org3をマージした後の構成ファイルと既存のチャネルの構成ファイルの差を計算します

configtxlator compute_update --channel_id mychannel --original original_config.pb --updated modified_config.pb > config_update.pb

 

 

§10。org3をマージした後の構成ファイルと既存のチャネルの構成ファイルの違いはjson形式に変換されます

configtxlator proto_decode --input config_update.pb --type common.ConfigUpdate > config_update.json

 

 

§11。新しいチャネル構成と古いチャネル構成の違いに基づいてトランザクションjsonを構築します

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

 

 

§12。新旧のチャネル構成の異なるトランザクションjsonをバイナリに変換します

configtxlator proto_encode --input config_update_in_envelope.json --type common.Envelope > org3_update_in_envelope.pb

 

 

§13。peer0.org1の環境変数を設定します

CORE_PEER_LOCALMSPID="Org1MSP"
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/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051

 

 

§14。org3_update_in_envelope.pbに署名します

peer channel signconfigtx -f org3_update_in_envelope.pb

 更新するには、チャネルのメンバーが署名する必要があります

 

 

§15。peer0.org2の環境変数を設定します

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

別のノードがコミットします

 

§16。チャネルを更新します(ステップ2の終わり)

peer channel update -f org3_update_in_envelope.pb -c mychannel -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

コンテナを終了します

 

3.新しい組織のノードが更新されたチャネルに参加し、チェーンコードをインストールします

新しい組織のノードが更新されたチャネルに参加し、チェーンコードをインストールします

§1新しい組織ノードのコンテナを起動します

IMAGE_TAG=latest docker-compose -f docker-compose-org3.yaml up -d

 

 

 

 

§2コンテナOrg3cliを入力します

docker exec -it Org3cli bash

 

§3新しいチャネルの構成を取得します

peer channel fetch 0 mychannel.block -o orderer.example.com:7050 -c mychannel --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

 

 

 

§4setpeer0.org3環境変数

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

 

 

§5peer0.org3がチャネルに参加します

peer channel join -b mychannel.block

 

 

§6setpeer1.org3環境変数

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer1.org3.example.com/tls/ca.crt
CORE_PEER_ADDRESS=peer1.org3.example.com:7051

 

 

§7。peer1.org3がチャネルに参加します

peer channel join -b mychannel.block

 

 

§8。peer0.org3の環境変数を設定します

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

 

 

§9インストールチェーンコード(ステップ3の終わり)

peer chaincode install -n mycc -v 2.0 -l golang -p github.com/chaincode/chaincode_example02/go/

チェーンコードのバージョンは元のバージョンよりも新しい必要があるため、2.0に設定されていることに注意してください

正常にインストールされないはずです。後で理由を見てみましょう。

 

 

第4に、元の組織ノードがチェーンコードバージョンを更新します

元の組織の各ノードのチェーンコードバージョンを更新します

§1CLIコンテナを入力します

docker exec -it cli bash

 

§2peer0.org1の環境を設定します

CORE_PEER_LOCALMSPID="Org1MSP"
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/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051

 

§3peer0.org1インストールチェーンコード

peer chaincode install -n mycc -v 2.0 -l golang -p github.com/chaincode/chaincode_example02/go/

 

 

§4。peer0.org2の環境を設定する

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

 

 

§5peer0.org2インストールチェーンコード

peer chaincode install -n mycc -v 2.0 -l golang -p github.com/chaincode/chaincode_example02/go/

各組織には、チェーンコードをインストールするためのノードが必要です

 

§6。peer0.org1の環境をセットアップする

CORE_PEER_LOCALMSPID="Org1MSP"
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/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051

 

 

§7チェーンコードバージョンの更新、承認戦略(ステップ4の終わり)

peer chaincode upgrade -o orderer.example.com:7050 --tls false --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -v 2.0 -c '{"Args":["init","a","90","b","210"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer','Org3MSP.peer')"

これがインスタンス化チェーンコードです。インスタンス化するのに必要な場所は1つだけです。

脱落

 

 

5、ノードを追加した後のチェーンコードテストの新しいバージョン

コンテナOrg3cli./scripts/testorg3.shで実行します

docker exec Org3cli ./scripts/testorg3.sh mychannel 3 golang 10
shijianfeng@ubuntu:~/fabric-samples/first-network$ docker exec Org3cli ./scripts/testorg3.sh mychannel 3 golang 10

 ____    _____      _      ____    _____ 
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |  
 ___) |   | |    / ___ \  |  _ <    | |  
|____/    |_|   /_/   \_\ |_| \_\   |_|  

Extend your first network (EYFN) test

Channel name : mychannel
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_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_LOCALMSPID=Org3MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
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_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/[email protected]/msp
CORE_PEER_ID=Org3cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org3.example.com:7051
===================== Querying on peer0.org3 on channel 'mychannel'... ===================== 
Attempting to Query peer0.org3 ...3 secs
+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
+ res=0
+ set +x

2021-01-09 09:54:54.023 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2021-01-09 09:54:54.024 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 90
2021-01-09 09:54:55.119 UTC [main] main -> INFO 003 Exiting.....
===================== Query on peer0.org3 on channel 'mychannel' is successful ===================== 
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_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_LOCALMSPID=Org3MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
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_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/[email protected]/msp
CORE_PEER_ID=Org3cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org3.example.com:7051
+ peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
+ res=0
+ set +x
2021-01-09 09:54:55.160 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2021-01-09 09:54:55.160 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2021-01-09 09:54:55.165 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 003 Chaincode invoke successful. result: status:200 
2021-01-09 09:54:55.165 UTC [main] main -> INFO 004 Exiting.....
===================== Invoke transaction on peer0.org3 on channel 'mychannel' is successful ===================== 

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_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_LOCALMSPID=Org3MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
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_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/[email protected]/msp
CORE_PEER_ID=Org3cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org3.example.com:7051
===================== Querying on peer0.org3 on channel 'mychannel'... ===================== 
Attempting to Query peer0.org3 ...3 secs
+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
+ res=0
+ set +x

2021-01-09 09:54:58.234 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2021-01-09 09:54:58.234 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 80
2021-01-09 09:54:58.238 UTC [main] main -> INFO 003 Exiting.....
===================== Query on peer0.org3 on channel 'mychannel' is successful ===================== 

========= All GOOD, EYFN test execution completed =========== 


 _____   _   _   ____   
| ____| | \ | | |  _ \  
|  _|   |  \| | | | | | 
| |___  | |\  | | |_| | 
|_____| |_| \_| |____/  

shijianfeng@ubuntu:~/fabric-samples/first-network$ 

実行に失敗した場合は、後で再試行します

 

 

おすすめ

転載: blog.csdn.net/u013288190/article/details/112384700