1. Description
The core of the blockchain network is the distributed ledger, in which all transaction information that occurs in the network is recorded.
Hyperledger Fabric is an open source, enterprise-grade, permissioned distributed ledger solution platform. Hyperledger Fabric is underpinned by a modular architecture with excellent confidentiality, scalability, flexibility and extensibility. Hyperledger Fabric is designed to support direct plug-in of different modular components, and can adapt to various scenarios intricately in the economic ecosystem.
This article shares how to build a Hyperledger Fabric 2.4 environment under Centos and conduct a simple network test.
2. Environmental preparation
2.1. Environment dependencies
- Git client
- Golang 1.17.5 and above
- Docker 18.03 or later
2.2. Check the environment
Docker version
docker -v
Golang locale
go version
3. Fabric source installation
3.1. Create a directory
Create a Fabric folder in the GOPATH directory:
mkdir -p $GOPATH/src/github.com/hyperledger
3.2. Download the source code
cd $GOPATH/src/github.com/hyperledger
git clone https://gitee.com/hyperledger/fabric.git
Here, the mirror warehouse of the domestic code cloud is used
3.3. Modifying the installation script
If the local network access to github is smooth, you can ignore this step
Edit bootstrap.sh
file
vim $GOPATH/src/github.com/hyperledger/fabric/scripts/bootstrap.sh
https://github.com/hyperledger/fabric-samples.git
tohttps://gitee.com/hyperledger/fabric-samples.git
- Note
pullBinaries
3.4. Execute the installation script
./bootstrap.sh
As shown in the figure below, after the script is successfully executed, a fabric-samples
project and a bunch of fabric docker images will be downloaded:
Manually download the compressed packages compiled by fabric
and fabric-ca
fabric/scripts/
directory:
Since the release package of the code cloud mirror warehouse only has the source code, we need to compile it, so we can only find a way to download it from github.
Compress the compressed package and get two folders: bin
and config
tar -zxvf hyperledger-fabric-linux-amd64-2.4.1.tar.gz
tar -zxvf hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz
Execute the following command to copy the fabric-samples
directory
cp -r bin fabric-samples/
cp -r config fabric-samples/
Fourth, start the test-network test network
Enter the test-network directory
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
Execute the following commands:
./network.sh up
If the following error occurs:
Then you need to modify docker-compose
and edit the following files in the test-network directory:
vim docker/docker-compose-test-net.yaml
vim docker/docker-compose-couch.yaml
vim docker/docker-compose-ca.yaml
vim addOrg3/docker/docker-compose-couch-org3.yaml
vim addOrg3/docker/docker-compose-org3.yaml
Among them, modify version: '3.7'
version: '3.6'
as shown in the figure below:
After modifying the configuration, re-execute:
./network.sh up
As shown in the figure below, one orderer node and two
peer nodes have been successfully started:
So far, a Hyperledger Fabric
has been built.
Five, test network use
The script's help text can be printed by executing:
./network.sh -h
5.1. Creating Channels
Now that our peers and orderers are running on our machines, we can use the script to create a Fabric channel for transactions between Org1 and Org2.
A Fabric channel is a dedicated communication layer between members of a particular network, a channel can only be used by organizations invited to join the channel, and is invisible to other members of the network. Each channel has a separate blockchain ledger, and invited organizations "join" their peers to store their channel ledger and verify transactions. Building a channel is equivalent to establishing a subchain.
To create a channel between Org1 and Org2 and join their peers using the network.sh script, execute the following command to create a channel:
./network.sh createChannel
As shown in the figure below, the default name is mychannel
The -c
, the following command will create a channel channel1
./network.sh createChannel -c channel1
5.2. Starting a chaincode on a channel
After creating a channel, you can start interacting with the channel ledger using smart contracts. Smart contracts contain the business logic for managing assets on the blockchain ledger. A network of applications run by members can invoke smart contracts on the ledger to create, change, and transfer these assets. Applications also query through smart contracts to read on the ledger. fetch data.
In Fabric, smart contracts are deployed on the network in the form of software packages as chaincodes. Chaincode is installed on an organization's peer nodes and then deployed to a channel where it can then be used to endorse transactions and interact with the blockchain ledger. Before deploying a chaincode to a channel, members of the channel need to reach a consensus on the chaincode definition and establish chaincode governance. When the required number of organizations agree, the chaincode definition can be submitted to the channel and the chaincode is ready to use.
After the channel is created, chaincode can be started on the channel using the network.sh script:
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-java -ccl java
- -ccn : specifies the chain code name
- -ccl : specifies the chain code language
deployCC
subcommand will install the asset-transfer-basic
peer0.org2.example.com
peer0.org1.example.com
and 061ee0129ccc7f, if the chaincode is deployed for the first time, the script will install the chaincode's dependencies. By default, the script installs the Go version of the asset-transfer-basic
. You can install the Java or javascript version of the -ccl
5.3. Interacting with the network
After the test network is enabled, the peer cli
client can be used to interact with the network, and the peer cli
client can call deployed smart contracts, update channels, or install and deploy new smart contracts.
First make sure that the operation directory is the test-network directory, for example, my directory is:
The following operations must be performed in the test-network
directory:
Execute the following command to add the cli client to the environment variables:
export PATH=${PWD}/../bin:$PATH
You also need to set the FABRIC_CFG_PATH in the fabric-samples codebase to point to the core.yaml file in it:
export FABRIC_CFG_PATH=$PWD/../config/
Set environment variables that allow org1 to operate peer cli:
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/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
CORE_PEER_TLS_ROOTCERT_FILE
and CORE_PEER_MSPCONFIGPATH
environment variables point to the encrypted material organizations
Execute the following command to initialize the ledger with some assets:
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"InitLedger","Args":[]}'
Successful execution will return Chaincode invoke successful. result: status:200
as shown in the following figure:
Execute the following command to query the list of assets in the channel ledger:
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
6. Turn off the network
After using the test network, you can shut down the network by executing the following command:
./network.sh down
This command will stop and delete the node and chaincode container, delete the organization encryption material, and remove the chaincode image from the Docker Registry, in addition to deleting the previously running channel project:
7. Create a network with a certification authority
Hyperledger Fabric uses Public Key Infrastructure (PKI) to verify the actions of all network participants. Transactions submitted by each node, network administrator and user need to have a public certificate and private key to verify their identity.
By default, the script creates certificates and keys using the cryptogen tool, which is used for development and testing, and can quickly create the required cryptographic material for a Fabric organization with a valid root of trust.
The test network script also provides startup options for networks that use a certificate authority (CA). Each organization in the network operates a CA (or multiple intermediate CAs) to create their own organizational identity, and all identities created by the CAs run by that organization share the same organizational root of trust.
First run the following command to shut down all running networks:
./network.sh down
Start the network with the CA parameters:
./network.sh up -ca
After the command is executed successfully, you can see through the printed docker container that three CAs are started, one for each organization in the network:
You can view the MSP folder structure and files of the Org1 administrator user tree
tree organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/
The signcerts
folder stores the administrator user's certificate, and the keystore
folder stores the private key.
References
Scan the code and follow for a surprise!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。