Environment Construction of Hyperledger Fabric v1.0 of Blockchain (Super Detailed Tutorial)

First of all, I would like to thank Shenlanju. This tutorial is based on his tutorial [ http://www.cnblogs.com/studyzy/p/7437157.html ] . The blockchain technology is developing rapidly, and I will do a little on the tutorial of the great god. some additions. In addition, students who are also learning blockchain can contact me, and everyone can learn from each other!

When I first came into contact with Hyperledger, I was very confused and didn't know where to start. The instructor suggested that I don't just look at theoretical knowledge, try to run it on the machine, and learn as I use it. So I started to search on the Internet how to build a Fabric environment. For the first time, a rookie (non-computer undergraduate) installed a virtual machine and used linux. The instructions were also used while checking. It took nearly five days to build it successfully, and the process was difficult. So write this tutorial, so that the students who learn later will avoid detours.


1. Ubuntu 16.04 is recommended for installing Linux system

Regarding whether to use a virtual machine or a dual system, the virtual machine for my laptop and the laboratory desktop computer are installed with dual systems. After experiencing both, it is recommended that you still prefer dual systems. In fact, the interface of Ubuntu is very friendly. Even if you don't know Linux commands at all (such as me), the basic operations can still be completed, and it is more direct to use.
Virtual machine tutorial: (1) Download the latest VirtualBox or VMware. (2) Download the Ubuntu 16.04 image and follow the steps to import it. There are many virtual machine installation tutorials on the Internet. do it right

2. Ensure that apt source is domestic

Enter the following command:
sudo vi /etc/apt/sources.list
Check the apt source list, if you see something like http://us.xxxxx , it is foreign, if you see something like http://cn.xxxxx , then you don’t need to change it .
If it is us, the original tutorial method is in the command mode, enter:
:%s/us./cn./g
batch replacement, and then enter: wq save and exit.

*tips:* I provide a simple method here, open the system settings , select software and updates
write picture description here
, and thenselect aliyun in the download from: as shown below. This source file is also in the country.
write picture description here

Update the sources sudo apt-get update
and install ssh so that you can connect to Ubuntu remotely using a client like Xshell or SecureCRT.

sudo apt-get install ssh

3. Go installation

The go version that comes with Ubuntu's apt-get is too low. Here we reinstall it and enter the command:
wget https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz

Then unzip:
sudo tar -C /usr/local -xzf go1.9.linux-amd64.tar.gz
Next, edit the environment variables of the current user and add the following
vi ~/.profile
at the end

export PATH=$PATH:/usr/local/go/bin 
export GOROOT=/usr/local/go 
export GOPATH=$HOME/go 
export PATH=$PATH:$HOME/go/bin

Figure
write picture description here
: wq save and exit

Finally load environment variables
source ~/.profile

We set the go directory GOPATH under the current user's folder, so remember to create a go folder and go
cd ~back to the root directory to
mkdir gocreate a go folder and the
go environment installation is complete! !

4. Docker installation

According to the Deep Blue Residence tutorial, for the installation of docker, I used the image provided by Alibaba. The installation instructions are as follows.
curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -
Then modify the permissions of the current user,
sudo usermod -aG docker XXX
log out and log in again, and then add the Docker Hub image of Alibaba Cloud:
Create a docker filesudo mkdir -p /etc/docker

sudo gedit /etc/docker/daemon.json 
{
 "registry-mirrors": ["https://obou6wyb.mirror.aliyuncs.com"]
}
保存并退出
sudo systemctl daemon-reload
sudo systemctl restart docker

The final docker versionresult is as follows.

write picture description here

The docker installation is complete! !

*tips* Later, when I installed docker on the desktop and others, I found that there was always a problem in this step, either the connection to the URL failed, or it was not added to the group, or the next few steps suggested that the path was wrong. The solution is to solve the Docker startup server link failure - Job for docker.service failed because the control process exited error code , or give the user root permission to operate, add to the group, log out and log in again. If it still doesn't work, give up Ali and change to DaoCloud.

5. Installation of Docker-Compose

Docker-compose is a component that supports batch creation of Docker containers via template scripts. Before installing Docker-Compose, you need to install Python-pip and run the command:
sudo apt-get install python-pip
Then install docker-compose. Considering the reasons for domestic and foreign walls, in order to download faster, we choose to install Docker-compose from DaoClound and run the command:

curl -L https://get.daocloud.io/docker/compose/releases/download/1.12.0/docker-compose-uname -s-uname -m> ~/docker-compose

sudo mv ~/docker-compose /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

The official recommendation is version 1.8.0 or above, here we download 1.12.0.
See docker-compose versionbelow, docker-compose installation is successful! !

write picture description here

6. Fabric source code download

First, create a folder for storing the source code (or manually), and the path is as follows:
mkdir –p ~/go/src/github.com/hyperledger
cd ~/go/src/github.com/hyperledger
Use the git command to download the complete source code
git clone https://github.com/hyperledger/fabric.git

*tips* Fabric code has been updated all the time. If you only want to download the latest code in the future, you can use the --single-branch -b master --depth 1 command option to get only the latest code of the master branch.
Since we are just setting up the environment. The latest code is not required, so just switch to the v1.0.0 major version.

cd ~/go/src/github.com/hyperledger/fabricEnter the fabric directory
git checkout v1.0.0

The Fabric source code download is complete! !

7. Download the Fabric Docker image

We have already set up the Docker Hub image address before, so the download will also be fast. run command

cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/

source download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0

This will download all the required Fabric Docker images. Since we have set up a domestic mirror, the download should be faster.
After the download is complete, we run the following command to check the downloaded mirror list:
docker images
The result is as follows
write picture description here

The main functions of each image are not listed one by one. For details, please refer to the book "Blockchain Principles, Design and Application", which is still very detailed.

8. Start the Fabric network and complete the ChainCode test

Still stuck in the e2e_cli file (if you haven't entered it first), here is an automated script to start and shut down the Fabric network. To start the Fabric network and automatically run the Example02 ChainCode test, execute a command:
./network_setup.sh up

This instruction specifically performs the following operations:
Compile the program to generate the Fabric public and private key and certificate, the program is in the directory: fabric/release/linux-amd64/bin

Generate genesis block and channel related information based on configtx.yaml and save it in the channel-artifacts folder.

Generate public and private keys and certificate information based on crypto-config.yaml and save them in the crypto-config folder.

Start the Fabric container of 1Orderer+4Peer+1CLI based on docker-compose-cli.yaml.

When the CLI starts, the scripts/script.sh file will be run. This script file includes functions such as creating a Channel, adding a Channel, installing Example02, and running Example02.
After a while, you will see this interface, then congratulations, the entire Fabric network has been connected,
write picture description here

ctrl+c to exit
Start successful! !

9. Manually test the Fabric network

Here is a small example provided by the official. In the official example, the channel name is mychannel and the chain code name is mycc.
First enter the CLI, then reopen a command line window, and enter:
docker exec -it cli bash
At this time, the user is root@748dafdc900b, and in the /opt/gopath/src/github.com/hyperledger/fabric/peer directory, run the following command to query account a Balance:
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
write picture description here

You can see the balance in the box: 90

Next, we can perform the transfer operation, the operation is invoke, from a to b 50:
peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -c '{"Args":["invoke","a","b","50"]}'

Now that the transfer is completed, let's try to check the balance of account a again, repeat the previous query command, the result is: the
write picture description here
result is correct, the balance of a is only 40.
Finally, we need to shut down Fabric, here we first use the exit command to exit the cli container.
exit
Then something like the start command:
cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli
./network_setup.sh down

write picture description here

At this point, our entire Fabric environment has been tested! ! Go for research or development! ! !


Written at the end: This tutorial is also a summary of my own work. Currently, I am slowly learning the operation of the Ubuntu system and researching the architecture and design of Fabric v1.0. Hope to help you!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325392619&siteId=291194637