title: fabric environment construction
date: 2021-03-24 17:12:42
categories:
- Hyperledger Fabric
tags: - environment
Docker of Hyperledger Fabric basic environment
Docker is an open source application container engine, which is based on Go language and complies with Apache 2.0 0 protocol is open source. Docker allows developers to package their applications and dependency packages into a lightweight and portable container, and then publish them to any popular Linux machine. It can also realize virtualization. Containers completely use sandbox mechanism, and there will be no interface between them. More importantly, the performance overhead of containers is very low. After version 17.03, docker is divided into CE (Community Edition) and EE (Enterprise Edition). We can use the Community Edition.
1.Docker installation and configuration
Use Docker warehouse for installation. Before installing Docker engine community on a new host for the first time, you need to set up Docker warehouse. After that, you can install and update Docker from the warehouse.
Use the apt get command to update the package index.
sudo apt-get update
Use the apt get command to install the dependency package, which is used to obtain the warehouse through HTTPS.
sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ git \ gnupg-agent \ software-properties-common
Use the curl command to add the official GPG key of Docker.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 use the following command to verify that you now have a key with a fingerprint by searching the last 8 characters of the fingerprint.
sudo apt-key fingerprint 0EBFCD88
pub rsa4096 2017-02-22 [SCEA] 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 uid [ unknown] Docker Release (CE deb) <docker@docker.com> sub rsa4096 2017-02-22 [S]
Use the following command to set up the stable warehouse.
sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
Use the following command to install docker engine community and containerd io.
sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io
Start Docker with the following command.
sudo systemctl start docker
Optional: if you want to start the Docker daemon at system startup, use the following command.
sudo systemctl enable docker
Use the following command to add users to the Docker group so that any user has permission to use Docker.
sudo usermod -a -G docker $USER
2.Docker compose installation and configuration
Compose is a tool for defining and running multi container Docker applications. With compose, you can use the YML file to configure all the services required by your application. Then, with one command, you can create and start all services from the YML configuration file.
Install Docker Compose and use the following command to download the latest version of Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Use the chmod command to apply executable permissions to binaries.
sudo chmod +x /usr/local/bin/docker-compose
3. Test
Docker allows you to run an application in the container. Use the docker run command to run an application in the container and output Hello world.
docker run ubuntu:15.10 /bin/echo "Hello world" Hello world
Analysis of each parameter:
- Docker: binary executable of docker.
- Run: run a container in combination with the previous Docker.
- ubuntu:15.10: specify the image to run. Docker first finds out whether the image exists from the local host. If it does not exist, docker will download the public image from the image warehouse Docker Hub.
- /bin/echo "Hello world": the command executed in the launched container
The complete meaning of the above command can be interpreted as: Docker is based on Ubuntu 15 10 create a new container with the image, then execute bin/echo "Hello world" in the container, and then output the result.
Golang of Hyperledger Fabric basic environment
1.Golang installation and configuration
Golang is a programming language developed by Google, which is statically strongly typed, compiled, and hairstyle, and has garbage collection function. The Hyperledger Fabric open source platform was developed with golang.
Use the following command to install Golang. The chain code in Fabric can be written in Golang, JavaScript and Java. We mainly use Golang to write.
sudo add-apt-repository ppa:longsleep/golang-backports sudo apt-get update sudo apt-get install golang-go
Create GOPATH and GOROOT paths.
cd ~ mkdir go
Configure the Go environment variable and open the environment variable configuration file with Vim.
Install vim
sudo apt install vim establish/usr/local/go folder cd /usr/local sudo mkdir go cd ~
sudo vim /etc/profile
Enter the following at the end of the file:
export GOROOT=/usr/local/go export GOPATH=$HOME/go export GOBIN=$GOPATH/bin export PATH=$PATH:$GOROOT/bin:$GOBIN
Press Esc to return to normal mode, then enter the: wq command and press enter to save the exit file. Then use the following command to make the variable take effect immediately.
source /etc/profile
Because the official proxy source of Golang is slow, sometimes the package cannot be downloaded. We use the following command to set the proxy source as the domestic proxy source.
go env -w GOPROXY=https://goproxy.cn,direct
Use the following command to view Golang version information.
go version
go1.16.4 linux/amd64
Use the following command to view the Golang environment variable configuration.
go env
GO111MODULE="on" GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/root/go" GOPRIVATE="" GOPROXY="https://goproxy.cn,direct" GOROOT="/usr/lib/go-1.13" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/lib/go-1.13/pkg/tool/linux_amd64" GCCGO="gccgo" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/dev/null" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build410299436=/tmp/go-build -gno-record-gcc-switches"
2. Test
Use the following command to create the test file test. In the GOPATH path path go
cd $GOPATH vim test.go
The contents of the input test file are as follows:
package main import "fmt" func main() { fmt.Println("Hello, World!") }
Press Esc to return to normal mode, then enter: wq command and press enter to save the exit file, and then use the following command to run the test file.
sudo go run test.go //You must run as an administrator, fool
Output the following results.
Hello, World!
After the test, use the following command to delete the test file.
rm test.go
Installation and testing of Hyperledger Fabric
1. Install Fabric
Use the following command to create a directory under the root directory and enter the created directory.
cd ~ mkdir hyperledger cd hyperledger
Use the curl command to download Fabric without specifying the version number. The latest version will be downloaded by default (please try again if it is unsuccessful due to network problems).
curl -sSL https://bit.ly/2ysbOFE | bash -s
Use the ls command to view the directory under the current path, that is, the download results.
ls
fabric-samples
Use the following command to enter the bin directory to view all the tools of Fabric.
cd fabric-samples/bin ls
configtxgen cryptogen fabric-ca-client idemixgen peer configtxlator discover fabric-ca-server orderer
Use the following command to copy all tools to / usr/local/bin, so that Fabric tools can be used in any directory.
sudo cp * /usr/local/bin
be careful
If the above cannot be downloaded due to network reasons
Use the following steps
cd ~ mkdir hyperledger cd hyperledger
Pull Hyperledger Fabric from git to measure mobile hotspot faster
git clone https://github.com/hyperledger/fabric.git
Determine fabric version - enter the fabric directory
cd fabric git checkout v2.3.0 //Select version branch
View branch version
git branch
Download the fabric samples source code
cd scripts #Run bootstrap sh ./bootstrap.sh //It's fast to climb over the wall here. The permission is not enough. Add sudo in front
If it is found that two image files have not been downloaded, it means that there are files in the bin folder
hyperledger-fabric-ca-darwin-amd64-1.4.9.tar.gz
hyperledger-fabric-darwin-amd64-2.3.0.tar.gz
Download the above two files
Download hyperledger-fabric-darwin-amd64-2.3.0 tar. The GZ package contains two folders: bin and config, hyperledger-fabric-ca-darwin-amd64-1.4.9 tar. There is a bin folder in the GZ compressed package. The binaries in the two bin folders are summarized in one bin folder. Finally, copy the bin and config folders to the fabric samples folder.
If fabric samples are not downloaded
- git clone https://github.com/hyperledger/fabric-samples.git
- $ cd ./fabric-samples
- $ git branch -a
- $ git checkout v2.3.0
- Add the bin and config files to fabric samples
Go to github to download wall climbing
Use the following command to enter the bin directory to view all the tools of Fabric.
cd fabric-samples/bin ls
configtxgen cryptogen fabric-ca-client idemixgen peer configtxlator discover fabric-ca-server orderer
Use the following command to copy all tools to / usr/local/bin, so that Fabric tools can be used in any directory.
sudo cp * /usr/local/bin
2. Test
After successful download, enter the official sample test network directory.
cd ~/hyperledger/fabric/scripts/fabric-samples/test-network
The official sample project is a network structure with two organizations. Run the official sample script to start the network with the following command.
sudo ./network.sh up
Using the following command to view the container, you can see that three nodes have been started.
sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8d0c74b9d6af hyperledger/fabric-orderer:latest "orderer" 4 seconds ago Up Less than a second 0.0.0.0:7050->7050/tcp orderer.example.com ea1cf82b5b99 hyperledger/fabric-peer:latest "peer node start" 4 seconds ago Up Less than a second 0.0.0.0:7051->7051/tcp peer0.org1.example.com cd8d9b23cb56 hyperledger/fabric-peer:latest "peer node start" 4 seconds ago Up 1 second 7051/tcp, 0.0.0.0:9051->9051/tcp peer0.org2.example.com
Use network The SH script creates a channel between Org1 and Org2 and adds its peer node to the channel. Run the following command to create a channel with the default name mychannel.
sudo ./network.sh createChannel
Download the go dependency package. If something goes wrong, first remove the permission restrictions on the entire hyperledger folder
cd ~/hyperledger/fabric/scripts/fabric-samples/chaincode/sacc go mod init go env -w GOPROXY=https://goproxy.cn,direct go mod vendor
In Fabric, smart contracts are deployed on the network in a software package called chaincode. Chaincode is installed on the peer node of the organization and then deployed to the channel, where it can be used to recognize transactions and interact with the blockchain ledger. Before deploying the chain code to the channel, the members of the channel need to agree on the chain code definition for establishing chain code governance. When the required number of organizations is agreed, the chain code definition can be submitted to the channel and ready to use the chain code. Use network SH after creating a channel, you can use the following command to start the chain code on the channel, where - ccl specifies the language version of chaincode.
sudo ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
Ensure that you are operating from the test network directory. If you follow the instructions [to install the samples, binaries, and Docker images, you can find the peer binaries in the bin folder of the fabric samples code library. Use the following command to add these binaries to your CLI path:
export PATH=${PWD}/../bin:$PATH
Use the following command to set up FABRIC_CFG_PATH is to point to the core in the repository Yaml file.
export FABRIC_CFG_PATH=$PWD/../config/
Now use the following commands to set environment variables for the CLI operation of Org1. The specific meaning and setting of environment variables will be described in detail in the following sections.
# Environment variables for Org1 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_ The mspconfigpath environment variable points to the encrypted material in the organizations folder of Org1. If you use/ network.sh deployCC -ccl go installs and starts the asset transfer (basic) chain code. You can call the initledger method of the chain code (Go) to give some initial assets on the ledger (if you use typescript or javascript, such as. / network.sh deployCC -l javascript, you will call the initledger function of the relevant chain code).
Use the following command to initialize the ledger with assets, and invoke is the command to call the chain code.
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, output:
-> INFO 001 Chaincode invoke successful. result: status:200
Run the following command to get a list of assets that have been added to the channel ledger.
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
If successful, you should see the following output:
[ {"ID": "asset1", "color": "blue", "size": 5, "owner": "Tomoko", "appraisedValue": 300}, {"ID": "asset2", "color": "red", "size": 5, "owner": "Brad", "appraisedValue": 400}, {"ID": "asset3", "color": "green", "size": 10, "owner": "Jin Soo", "appraisedValue": 500}, {"ID": "asset4", "color": "yellow", "size": 10, "owner": "Max", "appraisedValue": 600}, {"ID": "asset5", "color": "black", "size": 15, "owner": "Adriana", "appraisedValue": 700}, {"ID": "asset6", "color": "white", "size": 15, "owner": "Michel", "appraisedValue": 800} ]
When a network member wants to transfer some assets or change some assets in the ledger, the chain code will be called. Use the following instructions to change the asset owner on the ledger by calling the asset transfer (basic) chain code:
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":"TransferAsset","Args":["asset6","Christopher"]}'
If the command succeeds, you should see the following response:
2019-12-04 17:38:21.048 EST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
Because the endorsement strategy of the asset transfer (basic) chain code requires that the transaction be signed by Org1 and Org2 at the same time, the chain code call instruction needs to use the -- peeraddresesses tag to point to peer0 Org1. example. COM and peer0 Org2. example. com. Because the network TLS is enabled, the instruction also needs to point to the TLS certificate of each peer node with the -- tlsRootCertFiles tag.
After calling the chain code, we can use another query to see how the call changes the assets of the blockchain ledger. Because we have queried the peer of Org1, we can run this query chain code through the peer of Org2. Set the following environment variables to operate Org2:
# Environment variables for Org2 export CORE_PEER_TLS_ENABLED=true export CORE_PEER_LOCALMSPID="Org2MSP" export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp export CORE_PEER_ADDRESS=localhost:9051
You can query running in peer0 org2. example. Com asset transfer (basic) chain code:
peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
The result shows that "asset6" was transferred to Christopher:
{"ID":"asset6","color":"white","size":15,"owner":"Christopher","appraisedValue":800}
After the test, turn off the network.
sudo ./network.sh down