Hyperledger Fabric builds a test network

Fabric version used in this article: V2.5.4
Ubuntu system: 16.04LTS

  The previous article has already introduced in detail how to install and deploy the Hyperledger Fabric system, so I won’t go into details here. This article mainly introduces how to use Fabric's test network. Before we officially start, there is one thing that needs to be explained:

  • The complete installation path of Hyperledger Fabric is as follows:
$HOME/go/src/github.com/hyperledger/fabric

For convenience,the path in the following cd command starts from fabric, and the part before fabric is omitted.

1 Start/stop the test network

  In order to facilitate developers to learn and test smart contracts and DApps, a test network test-network is provided in the fabric-samples file.

1.1 Start the test network

Enter fabric/scripts/fabric-samples/test-network and execute the following command to start the test network:

cd fabric/scripts/fabric-samples/test-network
./network.sh up

If the command encounters the "permission denied" prompt during execution, use sudo permissions to execute the second command: a>

sudo ./network.sh up

You will see the following prompt message if the execution is successful:
Insert image description here
As you can see from the figure, this test network includes an Orderer node, two Peer nodes and a CLI client (these are Deployed in a docker container), the two Peer nodes belong to two organizations: Org1 and Org2.

1.2 Close the test network

The test network can be shut down using the following command:

sudo ./network.sh down

Tips: After the test network is closed, the created channel, deployed chain code and configured environment variables will become invalid and must be reset.

1.3 Restart the test network

You can use the following command to restart the test network:

sudo ./network.sh restart

2 Create channel

  Channels provide a communication mechanism that connects designated Peers and Orderers together to form a confidential communication link for data isolation (block data can only be stored among members who have joined the channel within the node).
After the test network is successfully started, the channel can be created. The creation command is as follows:

sudo ./network.sh createChannel

The command will create a channel named "mychannel" by default and add two Peer nodes to the channel.

Anchor peer set for org ‘Org1MSP’ on channel ‘mychannel’
Anchor peer set for org ‘Org2MSP’ on channel ‘mychannel’
Channel ‘mychannel’ joined

When creating a channel, you can also specify a name for the channel. The command is as follows:

//创建一个名为first_channel的通道
sudo ./network.sh crateChannel -c first_channel

Channel names follow the following rules:

  • It consists of lowercase letters, dots (.) and dashes (-); and begins with a letter;
  • Length less than 250 characters

The relationship between channels, nodes, and organizations in Hyperledger Fabric:

The relationship between channels, nodes, and organizations can be summarized in the following way: each participating organization can own one or more nodes, nodes can belong to one or more organizations, and different organizations can be isolated by one or more channels and interact. Participants in each channel can achieve data consistency and transaction privacy through the shared ledger between Peer nodes. At the same time, the Orderer node serves as the coordinator of the entire network and is responsible for the ordering and consensus of transactions.

3 Install and deploy chaincode

  Chaincode is a smart contract in Hyperledger Fabric. It is a developable component program that can operate on ledger data. You need to install and deploy the chain code before calling it. The chain code is also deployed in the container. The deployment command is as follows:

./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

This command will deploy a chaincode written in go language. in:

  • ccn: represents the name of the chain code;
  • ccp: indicates the address of the chain code file; this is the chain code example provided by fabric-samples.
  • ccl: indicates the language used by the chain code file;

In addition to the above parameters, you can also use -c to specify the installed channel. Examples are as follows:

./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go -c channel1

Chaincode deployment commands must not be executed with sudo. But if the following error occurs when executing the above command:
go executable file not found in $PATH
Insert image description here
Insufficient permissions error
Insert image description hereIt can be solved by modifying the permissions of the ~/go file. The details are as follows:

sudo chmod -R 777 ~/go

4 peer CLI

  After creating the network, you can use the peer CLI to operate the network, including installing and deploying smart contracts, calling smart contracts, and updating channels. You only need to configure environment variables to use the peer CLI. details as follows:

4.1 Configure peer CLI

(1) Add the /fabric/scripts/fabric-samples/bin directory to the environment variable PATH.
The first method: configure through the /etc/profile file
Insert image description here
The second method: through the export command
There are many on the network The command given in the data to use export to configure environment variables is as follows:

export PATH=${
    
    PWD}/../bin:$PATH

But this way of writing is not recommended here. There are two reasons:

  • This command uses the PWD variable, which is the user's current working path. If your current path is not /fabric/scripts/fabric-samples/test-network, then the path finally added to PATH will be wrong.
  • Even though the current working path is /fabric/scripts/fabric-samples/test-network, some systems cannot map to the correct path. for example:
    Insert image description here

If you want to use the export method to configure the environment variable, it is recommended to use the following command

//先跳转到fabric-samples文件所在的地址
cd /fabric/scripts/fabric-samples
export PATH=$PWD/bin:$PATH

(2) Configure FABRIC_CFG_PATH

//先跳转到fabric-samples文件所在的地址
cd /fabric/scripts/fabric-samples
export FABRIC_CFG_PATH=${
    
    PWD}/config/

(3) Configure Org1 environment variables

You must first enter the test-network file directory:

cd /fabric/scripts/fabric-samples/test-network

Then use the command line to deploy environment variables:

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${
    
    PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${
    
    PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051

Next you can use the peer CLI. You can enter the following command to verify whether the configuration is correct:

peer channel list

If the "mychannel" information just created can be returned (as shown below), it means that the peer CLI has been configured correctly.
Insert image description here
If the following error occurs:

Error: error getting endorser client for invoke: endorser client failed to connect to localhost:7051: failed to create new connection: context deadline exceeded.

This is because the Orderer and Peer nodes created previously no longer exist. You can use docker ps -a to check. Requires network restart, channel creation, chaincode deployment, and environment variables configuration.

4.2 Description

The following points need to be explained about the peer CLI (to be added later):

  • The peer CLI is a command line tool used to interact with peer nodes in the Fabric network. The peer CLI in the above example is bound to the peer node in Org1. If you need to bind the peer node in Org2, you need to reconfigure the environment variables.

  So far, the test network has been set up. As for how to use peer CLI to call smart contracts, leave it to the next blog.

References

  1. https://hyperledger-fabric.readthedocs.io/en/release-2.5/test_network.html
  2. https://blog.51cto.com/zhuxianzhong/7241974?articleABtest=0
  3. https://blog.51cto.com/zhuxianzhong/7242582?articleABtest=0
  4. https://blog.51cto.com/zhuxianzhong/7246193?articleABtest=0
  5. https://blog.csdn.net/qq_45179762/article/details/122217058
  6. https://blog.csdn.net/wybnmsl/article/details/113743268
  7. https://blog.csdn.net/Blockchain210/article/details/127707353

Guess you like

Origin blog.csdn.net/yeshang_lady/article/details/133854278