In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to build a multi-channel Fabric network". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to build a multi-channel Fabric network".
1. Overview of Hyperledger Fabric multi-channel network experimental environment.
We will construct a Hyperledger Fabric network consisting of three institutions: Org1, Org2 and Org3, each containing a node Peer0. The network contains two channels: ChannelAll, which is composed of Org1, Org2 and Org3, and Channel12, which is composed of Org1 and Org2, so this Fabric network is a multi-channel configuration. We will deploy the same chain code on these two Fabric channels, that is, the Simple Asset chain code provided in Fabrc-Samples:
2. The construction of Hyperledger Fabric multi-channel network experimental environment.
Step 1: clone the sample code provided in this tutorial under the fabric-samples directory officially provided by Hyperledger:
Cd fabric-samplesgit clone https://github.com/kctam/3org2ch_143.gitcd 3org2ch_143
Step 2: generate the required cryptographic data for institutions involved in the Fabric channel
.. / bin/cryptogen generate-- config=./crypto-config.yaml
Step 3: generate Fabric channel footage
Mkdir channel-artifacts & & export FABRIC_CFG_PATH=$PWD../bin/configtxgen-profile OrdererGenesis\-outputBlock. / channel-artifacts/genesis.blockexport CHANNEL_ONE_NAME=channelallexport CHANNEL_ONE_PROFILE=ChannelAllexport CHANNEL_TWO_NAME=channel12export CHANNEL_TWO_PROFILE=Channel12../bin/configtxgen-profile ${CHANNEL_ONE_PROFILE}\-outputCreateChannelTx. / channel-artifacts/$ {CHANNEL_ONE_NAME} .tx\-channelID $CHANNEL_ONE_NAME../bin/configtxgen-profile ${CHANNEL_TWO_PROFILE}\- OutputCreateChannelTx. / channel-artifacts/$ {CHANNEL_TWO_NAME} .tx\-channelID $CHANNEL_TWO_NAME../bin/configtxgen-profile ${CHANNEL_ONE_PROFILE}\-outputAnchorPeersUpdate. / channel-artifacts/Org1MSPanchors_$ {CHANNEL_ONE_NAME} .tx\-channelID $CHANNEL_ONE_NAME-asOrg Org1MSP../bin/configtxgen-profile ${CHANNEL_ONE_PROFILE}\-outputAnchorPeersUpdate. / channel-artifacts/Org2MSPanchors_$ {CHANNEL_ONE_NAME} .tx\-channelID $CHANNEL_ONE_NAME-asOrg Org2MSP../bin/configtxgen-profile ${CHANNEL_ONE_PROFILE}\-outputAnchorPeersUpdate. / channel-artifacts/Org3MSPanchors_$ {CHANNEL_ONE_NAME} .tx\-channelID $CHANNEL_ONE_NAME-asOrg Org3MSP../bin/configtxgen-profile ${CHANNEL_TWO_PROFILE}\-outputAnchorPeersUpdate. / channel-artifacts/Org1MSPanchors_$ {CHANNEL_TWO_NAME} .tx\-channelID $CHANNEL_TWO_NAME-asOrg Org1MSP../bin/configtxgen-profile ${ CHANNEL_TWO_PROFILE}\-outputAnchorPeersUpdate. / channel-artifacts/Org2MSPanchors_$ {CHANNEL_TWO_NAME} .tx\-channelID $CHANNEL_TWO_NAME-asOrg Org2MSP
Step 4: start all containers, and finally you should see 5 containers
Docker-compose up-ddocker ps
Step 5: for demonstration purposes, open 3 terminals and set the CA of the sort node
Org1
Docker exec-it cli bash 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
Org2
Docker exec-e "CORE_PEER_LOCALMSPID=Org2MSP"\-e "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"\-e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp" \-e "CORE_PEER_ADDRESS=peer0.org2.example.com:7051"\-it cli bashexport 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
Org3
Docker exec-e "CORE_PEER_LOCALMSPID=Org3MSP"\-e "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"\-e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp" \-e "CORE_PEER_ADDRESS=peer0.org3.example.com:7051"\-it cli bashexport 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
Step 5: create multiple channels in the Fabric network and add each peer node to multiple channels
First, create a channelall channel and add the nodes of the three institutions to the channel:
Org1
Peer channel create-o orderer.example.com:7050-c channelall\-f / opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/channelall.tx\-- tls-- cafile $ORDERER_CApeer channel join-b channelall.block-tls-- cafile $ORDERER_CApeer channel update-o orderer.example.com:7050-c channelall\-f / opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/Org1MSPanchors_channelall.tx\-- tls-- cafile $ORDERER_CA
Org2
Peer channel join-b channelall.block-- tls-- cafile $ORDERER_CApeer channel update-o orderer.example.com:7050-c channelall\-f / opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/Org2MSPanchors_channelall.tx\-- tls-- cafile $ORDERER_CA
Org3
Peer channel join-b channelall.block-- tls-- cafile $ORDERER_CApeer channel update-o orderer.example.com:7050-c channelall\-f / opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/Org3MSPanchors_channelall.tx\-- tls-- cafile $ORDERER_CA
Then create a channel12 and add both Org1 and Org2 to the channel:
Org1
Peer channel create-o orderer.example.com:7050-c channel12\-f / opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/channel12.tx\-- tls-- cafile $ORDERER_CApeer channel join-b channel12.block-tls-- cafile $ORDERER_CApeer channel update-o orderer.example.com:7050-c channel12\-f / opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/Org1MSPanchors_channel12.tx\-- tls-- cafile $ORDERER_CA
Org2
Peer channel join-b channel12.block-- tls-- cafile $ORDERER_CApeer channel update-o orderer.example.com:7050-c channel12\-f / opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/Org2MSPanchors_channel12.tx\-- tls-- cafile $ORDERER_CA
Step 6: check the Fabric channels that have been joined by each node
Use the following command in the corresponding terminal of each node to view the channels joined by the current node:
Peer channel list
You should see that org1 and org2 have each added two channels, while org3 has added only one channel.
If all goes well, you now have a multi-channel Fabric network of three institutions that can be used to test any chain code.
Step 7: remember to clean up the experimental environment after the test. The command is as follows:
Docker-compose down-vdocker rm $(docker ps-aq) docker rmi $(docker images dev-*-Q) 3, Fabric Multichannel installation Simple Asset chain Code (SACC)
Now that our Fabric multi-channel experimental network has been set up, we can begin to deploy chain codes.
We use the built-in SACC chain code of fabric-samples, which is as follows:
/ * * Copyright IBM Corp All Rights Reserved * * SPDX-License-Identifier: Apache-2.0 * / package mainimport ("fmt"github.com/hyperledger/fabric/core/chaincode/shim"github.com/hyperledger/fabric/protos/peer") / / SimpleAsset implements a simple chaincode to manage an assettype SimpleAsset struct {} / / Init is called during chaincode instantiation to initialize any// data. Note that chaincode upgrade also calls this function to reset// or to migrate data.func (t * SimpleAsset) Init (stub shim.ChaincodeStubInterface) peer.Response {/ / Get the args from the transaction proposal args: = stub.GetStringArgs () if len (args)! = 2 {return shim.Error ("Incorrect arguments. Expecting a key and a value ")} / / Set up any variables or assets here by calling stub.PutState () / / We store the key and the value on the ledger err: = stub.PutState (args [0], [] byte (args [1])) if err! = nil {return shim.Error (" Failed to create asset:% s ") Args [0])} return shim.Success (nil)} / / Invoke is called per transaction on the chaincode. Each transaction is// either a 'get' or a 'set' on the asset created by Init function. The Set// method may create a new asset by specifying a new key-value pair.func (t * SimpleAsset) Invoke (stub shim.ChaincodeStubInterface) peer.Response {/ / Extract the function and args from the transaction proposal fn, args: = stub.GetFunctionAndParameters () var result string var err error if fn = = "set" {result, err = set (stub Args)} else {/ / assume 'get' even if fn is nil result, err = get (stub, args)} if err! = nil {return shim.Error (err.Error ())} / / Return the result as success payload return shim.Success ([] byte (result))} / / Set stores the asset (both key and value) on the ledger. If the key exists,// it will override the value with the new onefunc set (stub shim.ChaincodeStubInterface, args [] string) (string, error) {if len (args)! = 2 {return ", fmt.Errorf (" Incorrect arguments. Expecting a key and a value ")} err: = stub.PutState (args [0], [] byte (args [1])) if err! = nil {return", fmt.Errorf ("Failed to set asset:% s", args [0])} return args [1], nil} / / Get returns the value of the specified asset keyfunc get (stub shim.ChaincodeStubInterface, args [] string) (string Error) {if len (args)! = 1 {return ", fmt.Errorf (" Incorrect arguments. Expecting a key ")} value, err: = stub.GetState (args [0]) if err! = nil {return", fmt.Errorf ("Failed to get asset:% s with error:% s", args [0], err)} if value = = nil {return ", fmt.Errorf (" Asset not found:% s " Args [0])} return string (value), nil} / / main function starts up the chaincode in the container during instantiatefunc main () {if err: = shim.Start (new (SimpleAsset)) Err! = nil {fmt.Printf ("Error starting SimpleAsset chaincode:% s", err)}}
The logic of the SACC chain code provided by Fabric Samples is simple:
When the chain code is instantiated, the Init () function is executed, which requires two parameters, corresponding to the key and the value.
Save the key / value pairs passed in the Init () function to the account book using the PutState method
After the chain code is instantiated, the transaction is handled by the Invoke () function. The arguments to this function include a method name and several parameters.
If the method name is set when calling the Invoke () function, you need to pass in two parameters that represent the key and value to be set
If the method name is get when calling the Invoke () function, you need a parameter that represents the key to read
Through the chain code installation operation, the chain code can be started on each node. Note that the chain code is not available until it is instantiated.
Install the chain code in the corresponding terminal of each node using the following command:
Peer chaincode install-n sacc-p github.com/chaincode/sacc-v 1.0
We should see the following output:
Now that all nodes have SACC chain codes installed, we can instantiate this chain code.
4. Instantiation and access of Fabric chain code on Fabric multi-channel experimental 1:ChannelAll channel
First, let's look at the ChannelAll channel that contains all three institutions.
In the terminal corresponding to Org1, instantiate the chain code on the ChannelAll channel:
Peer chaincode instantiate-o orderer.example.com:7050-- tls\-- cafile $ORDERER_CA-C channelall-c'{"Args": ["a", "100"]}'\-n sacc-v 1.0-P "OR ('Org1MSP.peer',' Org2MSP.peer', 'Org3MSP.peer')"
We set the initial key / value pair to aUniver 100. In addition, we have set up an endorsement strategy: OR means that only any one of the three organizations is required to endorse.
Now let's query the value of the key an on the channel ChannelAll.
Enter the terminal corresponding to Org1 and run the following command:
Peer chaincode query-C channelall-n sacc-c'{"Args": ["get", "a"]}'
The results are as follows:
Now run the following command in the terminal corresponding to Org2:
Peer chaincode query-C channelall-n sacc-c'{"Args": ["get", "a"]}'
The results are as follows:
Now run the following command in the terminal corresponding to Org3:
Peer chaincode query-C channelall-n sacc-c'{"Args": ["get", "a"]}'
The results are as follows:
Now we can see that we get the same value on three nodes, which share the same ledger.
5. Fabric multi-channel experiment 2: instantiation and interaction of SACC chain codes on Channel12 channels
Now let's instantiate the SACC chain code on the channel Channel12.
In the terminal corresponding to Org1, run the following command:
Peer chaincode instantiate-o orderer.example.com:7050\-- tls-- cafile $ORDERER_CA-C channel12\-c'{"Args": ["b", "200"]}'- n sacc-v 1.0\-P "OR ('Org1MSP.peer',' Org2MSP.peer')"
This time we set the initial key / value pair to bUnip 200, and the endorsement strategy is for any organization to complete the endorsement.
Or start with Org1:
Peer chaincode query-C channel12-n sacc-c'{"Args": ["get", "b"]}'
The results are as follows:
Then enter the terminal corresponding to Org2:
Peer chaincode query-C channel12-n sacc-c'{"Args": ["get", "b"]}'
The results are as follows:
If we run the same command on the terminal corresponding to Org3, we will see a prompt to disable access. This is because Org3 did not join the channel Channel12:
Peer chaincode query-C channel12-n sacc-c'{"Args": ["get", "b"]}'
The results are as follows:
If we try to read the value of the key an on the channel Channel12, we will find that the prompt does not define a. In Hyperledger Fabric, each channel has its own account book, and the states of different channels are not shared.
Run the following command in the terminals of Org1 and Org2:
Peer chaincode query-C channel12-n sacc-c'{"Args": ["get", "a"]}'
The results are as follows:
The above is all the contents of the article "how to build a multi-channel Fabric network". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un