This article describes how to compile an executable from Fabric source code to deploy a Fabric blockchain network application on multiple machines, as well as chain code test samples.
1
Environment introduction
Fabric project is an alliance chain project in hyperledger project. This article introduces how to compile related components and tools from fabric source code, to deploy a set of Fabric blockchain services, to introduce to you, and learn about fabric blockchain.
Fabric-related components and tools:
-
Orderer consensus node. Sort transactions and generate blocks
-
Peer Indicates the consensus node. Endorse transactions and record block information
-
Crypto is used to generate relevant certificate files for corresponding users in the blockchain network
-
Configtxgen is used to generate genesis blocks for the chain code of the blockchain system, profiles for new channels, and profiles for anchor nodes in the organization
Introduction to Fabric Blockchain Network deployment environment:
-
Five Centos 7 VMS with four cores and 8 GB memory
-
1 Orderer node and 4 peer nodes. Solo consensus algorithm 10.121.60.1 is used to deploy orderer node 12.10.121.60.2 — 10.121.60.3 is used to deploy peer nodes
2
Source code compilation, and preparation of fabric blockchain network deployment environment
Fabric is developed based on the GO language, and the test user chain code used in this example is developed based on the GO language, which depends on the compilation and development environment of the GO language. Because this article focuses on the application of Fabric, you need to find the installation method for the relevant environment.
-
Install go language development environment, and set related environment variables GOPATH, GOROOT, GOBIN (please install Go 1.8 or later)
-
X86_64, zlib-devel.x86_64, bzip2-devel.x86_64, libtoo-ltdl-devel.x86_64, libtool (dependent packages are based on centos, Other * NiX and MAC system, please find and install related dependency files)
-
Docker, docker-compose install docker, docker-compose (docker version on yum is too low, please download and install from the official website)
-
Install git (the version of git on yum is too early)
-
To get the fabric source (the example is based on v1.0.0 version), and stored in a directory under $GOPATH/src/github.com/hyperledger
-
Build goTools from Github.com (golang.org)
-
Execute in the directory $GOPATH/ SRC /golang/x/
git clone https://github.com/golang/tools.git
3
Compile fabric blockchain-related executables
Switch to the fabric source directory and use the makefile to compile all the fabric project executables, as shown in the following figure:
Fabric source code compiles a list of executables
In this example, you only need to compile some of the necessary files: Orderer, Peer, ConfigtxGen, cryptogen.
The following command, all in $GOPATH/src/github.com/hyperledger/fabric directory.
1. Compile go related tools
cp -r GOPATH/src/github.com/hyperledger/fabric/gotools/build/gopath/src make gotools
2. Compile the Fabric infrastructure environment
make buildenv
Note: If an error message is encountered during compilation:
cp:build/docker/gotools/bin/protoc-gen-go:No such file or directory
Solution: Install Protocbuf
go get -u github.com/golang/protobuf/protoc-gen-go cp GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin/
3. Compile tools related to blockchain services
-
make orderer
-
make peer
-
make configtxgen
-
make cryptogen
-
The compiled executable is generated in the./fabric/build/bin directory, which is set to the PATH environment variable
4
Multi-host Fabric Network Deployment (SOLO Consensus)
Next, the focus of this article is how to use the compiled executable and related tools to build a Fabric blockchain network and implement the deployment and testing of the chain code.
Create a directory /etc/hyperledger/fabric on the five VMS. Run the following commands in this directory and set environment variables for the fabric network:
$FABRIC_CFG_PATH=/etc/hyperledger/fabric
1. Configure topology relationships among Fabric network users
- Use the crypto-config.yaml configuration file to configure the topology of fabric network users. The contents of crypto-config. yaml are as follows:
Crypto-config.yaml
This configuration file contains an Orderer node and two peer organizations, which in turn contain two peer nodes.
- Use the cryptogen tool to generate the user’s corresponding secret key and certificate file from the crypto-config.yaml configuration file:
cryptogen generate –config=./crypto-config.yaml –output ./crypto-config
After the command is executed, the crypto-config folder is generated in the current directory, which contains the node user’s secret key and certificate file.
- Run the SCP command to distribute the crypto-config folder to the /etc/hyperledger/fabric directory on the other four VMS
2. Configure the startup creation block of the Orderer node and create the related configuration of the channel transaction
- Through a configuration file configtx. Yaml configuration ordere node start need founding block information, as well as the new application channel of trading information. The configuration file is as follows:
configtx.yaml
- Use the configtxGen tool to generate the creation blocks required to start the Orderer node:
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock genesis.block
- Use the configtxgen tool to generate the transaction profile to create the application channel:
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx testchannel.tx -channelID testchannel
- Use the configtxgen tool to generate and update the configuration information file of the organization anchor node:
Configtxgen – profile TwoOrgsChannel – outputAnchorPeersUpdate./ org1mSPANors. Tx – channelID testChannel – asOrg Org1MSP
Configtxgen – profile TwoOrgsChannel – outputAnchorPeersUpdate./ org2mSPANors. Tx – channelID testChannel – asOrg Org2MSP
- After the command is executed, the following files are generated in the current directory:
Genesis. block, Testchannel. tx, org1MSPANors. Tx, org2MSPANors
-
Send the testchannel.yx and org1mspancorons. tx files to 10.121.60.2 using the SCP command
-
Run the SCP command to file org2mSPANcorons. tx to 10.121.60.4
3. Set the Hosts file for the VM
Since the information related to the network address in the configuration file related to the fabric network startup is the domain name, the corresponding relationship between the domain name and IP needs to be configured in the hosts file. In our test environment, orderer node is processed. Configure hosts information for the VMS of the other four peer nodes:
10.121.60.1 orderer.test.com 10.121.60.2 peer0.org1.test.com 10.121.60.3 peer1.org1.test.com 10.121.60.4 peer0.org2.test.com 10.121.60.5 peer1.org2.test.com
The domain name is obtained according to the cryprto-config.yaml configuration information, which will be explained in the subsequent configuration file
4. Configure the Orderer startup environment
The orderer node starts the configuration information orderer.yaml and saves it in /etc/hyperledger/fabric. See the attachment for related configuration files
From cypto-config, copy the orderer node’s key and certificate file to the Fabric startup environment variables directory:
Cp – r. / crypto-config/ordererOrganizations/test.com/orderers/orderer.test.com/msp. / cp – r ./crypto-config/ordererOrganizations/test.com/orderers/orderer.test.com/tls ./
According to the previous operations, the execution environment directory of the Orderer node in the Fabric network must have the following files and folders:
./crypto-config,./ MSP,./ TLS, orderer.yaml, genesis.block
Start the orderer node: orderer start
5. Configure the peer startup environment
Configure peer startup configuration information core.yaml on the vM of the peer node and save it in the /etc/hyperledger/fabric directory. See the attachment for related configuration files.
Copy the key and certificate file of the peer node from the crypto-config folder to the fabric startup environment variable directory:
Cp – r. / crypto-config/peerOrganizations/org1.test.com/peers/peer0.org1.test.com/msp. / cp – r ./crypto-config/peerOrganizations/org1.test.com/peers/peer0.org1.test.com/tls ./
Note that the marked part should copy the corresponding file information in the corresponding directory (refer to the hosts configuration information for distinction).
According to the previous operations, the execution environment directory of the Fabric network peer must contain the following files and folders:
Tx,./ MSP,./ TLS, core.yaml, channel.tx, core.yaml, org1mspanors. Tx (.2 VM), org2mspanors.
Start the peer node: Peer node start
Yaml and core.yaml configuration files. In order to be consistent with the configuration information in the attachment, the MSP and TLS folder can be copied without operation. Therefore, copy the corresponding key and certificate file to the fabric network execution environment variable directory
6. Create an application channel
Set the corresponding environment variables (modify the configuration according to the environment) **
CORE_PEER_LOCALMSPID Org1MSP CORE_PEER_MSPCONFIGPATH “= =”/etc/hyperledger/fabric/crypto – config \
/peerOrganizations/org1.test.com/users/[email protected]/msp
Run the following command to create an application channel
peer channel create -o orderer.test.com:7050 -c testchannel -f ./channel.tx –tls true –cafile /etc/hyperledger/fabric/crypto-config /ordererOrganizations/test.com/orderers /orderer.test.com/msp/tlscacerts /tlsca.test.com-cert/pem
After the command is successfully executed, the creation block testchannel.block file of the application channel is generated in the current directory. Only this file can be used to add the corresponding application channel. Run the SCP command to distribute the data to the fabric execution environment variable (/etc/hyperledger/fabric) of the other three peer VMS.
7. Add the application channel
Run the following command to add the application channel:
Peer Channel Join -b testchannel.block
After the command is executed successfully, the following information is displayed:
Peer joined the channel!
The peer VMS that join the application channel can view the passing information of the peer VMS through terminal commands, as shown in the following figure:
Join channel
8. Update the anchor node configuration
Execute the command to update the anchor node responsible for communicating with other nodes on behalf of the organization:
peer channel update -o orderer.test.com:7050 -c testchannel -f ./Org1MSPanchors.tx –tls true –cafile /etc/hyperledger/fabric/crypto-config /ordererOrganizations/test.com/orderers /orderer.test.com/msp/tlscacerts /tlsca.test.com-cert/pem
The anchor node is responsible for the Gossip communication on behalf of the organization with nodes in other organizations.
5
Test chain code
1. Install chain codes
Set the environment variable (modify the configuration according to the environment) :
CORE_PEER_LOCALMSPID Org1MSP CORE_PEER_MSPCONFIGPATH “= =”/etc/hyperledger/fabric/crypto – config /peerOrganizations/org1.test.com/users/[email protected]/msp
Since this example is a smart contract developed based on the go language, it will rely on the go development environment for installation. If the peer chaincode install command is used to specify the chaincode path for installation, It may cause that the same chaincode installed on other virtual devices cannot be instantiated synchronously (a problem caused by inconsistent go development environment), so the go chaincode package is used here, and then the chaincode is installed by installing the package chaincode file.
Note: the path specified in the fabric source code is automatically prefixed according to the GOPATH environment variable when executing the command, so one path is in the $GOPATH/ SRC/directory: Peer chaincode package – n test – p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_exanple02 – v 1.0 Pak uses the SCP command to distribute the test.pak file to the fabric execution environment variable path of the other three peer VMS.
Install the chain code
peer chaincode install test.pak
The peer node server with chain code installed can view the installed chain code information by terminal command, as shown in the figure:
Write the picture description here
2. Initialize the chain code
To initialize the chain code, you only need to perform the instantiation on the server of one peer node to automatically create the chain code container. The other servers of the peer node will synchronize the instantiated chain code information and create the chain code container
Execute the command to instantiate the chain code:
Peer chainCode instantiate -o orderer.test.com:7050 -c testchannel -n test -v 1.0 -c ‘{” Args “:[” init”, “a”, “100”, “b”,” 200 “]} ‘-p “OR (” Org1MSP. Member’, ‘Org2MSP. Member’)” true — – TLS cafile/etc/hyperledger/fabric/crypto – config /ordererOrganizations/test.com/orderers /orderer.test.com/msp/tlscacerts /tlsca.test.com-cert/pem
After the command is executed, 100 is assigned to user A and 200 tokens are assigned to user B.
After the command is executed successfully, you can view the chain code information that has been instantiated under the service of the peer node through the command, as shown in the following figure:
Write the picture description here
Use the following command to view the container information: docker ps. As shown in figure:
Write the picture description here
3. Test trades
Execute a command to send a transaction ordering A to transfer 10 tokens to B:
Peer chaincode invoke-o orderer.test.com:7050 -c testchannel -n test -c ‘{” Args “:[” Args”, “a”, “b”,” 10 “]} ‘true — – TLS cafile/etc/hyperledger/fabric/crypto – config/ordererOrganizations/test.com/orderers /orderer.test.com/msp/tlscacerts /tlsca.test.com-cert/pem
After the command is executed, run the following command to query the balance information:
peer chaincode query \
– n test – C testchannel – C ‘{” Args “: [” query”, “a”]}’
Content source: Jane Book
Akik Still a trick
The following is our community introduction, welcome all kinds of cooperation, exchange, learning 🙂