Build Hyperledger Fabric under Linux environment (with process code)

Build Hyperledger Fabric under Linux environment (with process code)

1

Introduction to Hyperledger Fabric

The Hyperledger project is a consortium blockchain project initiated by the Linux Foundation, which has attracted many big-name companies to enter and develop. The Hyperledger group of projects contains quite a few sub-projects. Each sub-project is led by one or two commercial companies, providing different functions and support for Hyperledger.

In this article, we will introduce the development environment of the Fabric sub-project under the Hyperledger group project, based on the latest 1.1 version. This sub-project can be said to be the core project of the Hyperledger project. It is the implementation framework of the blockchain. It provides a programming interface for chaincode (chaincode, against the smart contract in the Ethereum project). It can be said to be the top priority of the entire Hyperledger. . At present, the project is led by IBM.

2

Build a development environment under Linux

The Linux system is relatively friendly to the development of Fabric, and docker is natively supported under Linux. The Linux distribution we use in this article is Ubuntu 16.04 (or a distribution based on Ubuntu 16.04, such as Linux mint 18.3), to build a Fabric 1.1 development environment. This article mainly refers to official documents, and makes a few additions and modifications to the official documents (https://hyperledger-fabric.readthedocs.io/en/release-1.1/), so that the development environment can be successfully set up in the domestic environment .

3

Installation dependencies

Docker

If you have an old version of docker, you need to uninstall it first:

sudo apt purge docker docker-engine docker.io
 

Then install the dependencies:

sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common
 

Add the docker mirror repository (Tsinghua University):

curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

echo "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee/etc/apt/sources.list.d/docker-ce.list
 

Install the docker-ce package:

sudo apt update && sudo apt install -y docker-ce
 

Modify the image of the docker hub, edit the/etc/docker/daemon.json file, create one if you don't have one, you need the administrator to raise the rights, write the following:

{ 

 "registry-mirrors": ["https://registry.docker-cn.com"]
}
 

Restart the docker service to take effect: sudo service docker restart, so that docker can pull from the domestic mirror station, which greatly improves the speed of the pull image.

GO

Download GO:

wget https://dl.google.com/go/go1.10.2.linux-amd64.tar.gz
 

Golang official website is not accessible, you can also get other versions of go from the mirror site (https://mirrors.ustc.edu.cn/golang/)

Unzip and move go to/usr/local/go:

tar xvf go1.10.2.linux-amd64.tar.gz sudo mv go/usr/local sudo ln -s/usr/local/go/bin/usr/local/bin

This way go is installed. By default, GOPATH is/usr/local/go, and GOROOT is ~/go. If you don't need to modify it, you don't need to set these two environment variables.

4

Install Fabirc executable program and Docker image

This step is relatively simple. After the previous dependencies are installed, you only need to execute the following command:

curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s 1.1.0
 

The whole process takes a long time. Because Fabric's binary package is not fast in China, and the docker image capacity is also large, the entire time will be long. And this script does not do idempotent processing, once it is interrupted in the middle, it may not be retried, and it may be necessary to clean up temporary files and rerun. So don't interrupt script execution easily, just wait patiently.

The final output of the script is similar to the following to indicate that the development environment is installed:

===> List out hyperledger docker imageshyperledger/fabric-ca          latest              72617b4fa9b4        8 weeks ago         299MBhyperledger/fabric-ca          x86_64-1.1.0        72617b4fa9b4        8 weeks ago         299MBhyperledger/fabric-tools       latest              b7bfddf508bc        8 weeks ago         1.46GBhyperledger/fabric-tools       x86_64-1.1.0        b7bfddf508bc        8 weeks ago         1.46GBhyperledger/fabric-orderer     latest              ce0c810df36a        8 weeks ago         180MBhyperledger/fabric-orderer     x86_64-1.1.0        ce0c810df36a        8 weeks ago         180MBhyperledger/fabric-peer        latest              b023f9be0771        8 weeks ago         187MBhyperledger/fabric-peer        x86_64-1.1.0        b023f9be0771        8 weeks ago         187MBhyperledger/fabric-javaenv     latest              82098abb1a17        8 weeks ago         1.52GBhyperledger/fabric-javaenv     x86_64-1.1.0        82098abb1a17        8 weeks ago         1.52GBhyperledger/fabric-ccenv       latest              c8b4909d8d46        8 weeks ago         1.39GBhyperledger/fabric-ccenv       x86_64-1.1.0        c8b4909d8d46        8 weeks ago         1.39GBhyperledger/fabric-zookeeper   latest              92cbb952b6f8        2 months ago        1.39GBhyperledger/fabric-zookeeper   x86_64-0.4.6        92cbb952b6f8        2 months ago        1.39GBhyperledger/fabric-kafka       latest              554c591b86a8        2 months ago        1.4GBhyperledger/fabric-kafka       x86_64-0.4.6        554c591b86a8        2 months ago        1.4GBhyperledger/fabric-couchdb     latest              7e73c828fc5b        2 months ago        1.56GBhyperledger/fabric-couchdb     x86_64-0.4.6        7e73c828fc5b        2 months ago        1.56GB
 

At the same time, this script will clone a fabric-samples/project in the current directory. The downloaded fabric binary toolkit will be stored in the fabric-samples/bin/directory. Add the fabric-samples/bin/directory to the environment variable. Run the node program directly.

export PATH=${PATH}:/path/to/fabric-samples/bin/
 

5

Run Sample

cd fabric-samples/first-network
 

Enter the first-network directory, this directory has a ./byfn.sh script, responsible for quickly generating a blockchain network, directly use this script to generate network configuration, and run the docker container:

./byfn.sh -m generate
 

Quickly generate network node configuration items, such as generating certificates, etc., generate a genesis block, and generate a Channel named mychannel (https://hyperledger-fabric.readthedocs.io/en/release-1.1/glossary.html#channel) .

Start the network with the following command:

./byfn.sh up
 

This command will start the Fabric network in the docker environment, run the chaincode test (Golang's chaincode), and exit the container after running.

After running the test successfully, you can use the following command to shut down the Fabric network and clear all containers and data volumes:

./byfn.sh down
 

At this point, the entire Fabric development environment has been set up, and we can use its API coding code to deploy in the Docker environment for testing. Before entering the development link formally, it is necessary to understand the entire operating principle of Fabric in order to develop better.

It is recommended that readers first understand some basic concepts of blockchain technology, including block, chain, distributed consensus algorithm, smart contract, etc., in order to better understand how Fabric implements these theoretical knowledge through technical means. Next, I will introduce the model concept in Fabric and the various functions of the binary toolkit.

6

Fabric model

In order to adapt to the enterprise's customized blockchain needs, Fabric abstracts the following model concepts.

Assets

Assets refer to all items that can be exchanged at equal value in the network, which can be tangible or intangible assets. Fabric provides the ability to modify assets through chaincode.

Assets are abstracted into kv pairs in Fabric, which can be in JSON or binary format. Assets can be easily defined through Hyperledger Composer (https://github.com/hyperledger/composer).

Chaincode

Directly against the concept of smart contract (https://en.wikipedia.org/wiki/Smart_contract) is a program running in the Fabric network, which can provide the function of modifying assets, and at the same time can obtain various statuses in the current transaction.

Ledger Features

The ledger is a shared, immutable storage unit that records the history of each transaction channel and provides SQL-like query functions.

The concept of the ledger actually covers the two parts of the concept of block and chain. The transaction status is stored in the state database. Currently, Fabric uses LevelDB as the default state database and provides optional CouchDB.

Privacy through Channels

The ledger works on each channel, which can be shared with the entire network, or privatized, and shared with only a few participants.

If you want to privatize the ledger, all participants are required to join the same channel, and transaction transactions and ledger are isolated through channel isolation. Before accounting, transaction data can be obfuscated and protected by encryption algorithms, such as the AES algorithm.

Security & Membership Services

In Fabric's transaction network, all participants have known identities, and encryption certificates are generated through PKI (Public Key Infrastructure) to bind organizations, network components, end users or client programs. Therefore, data access control can be effectively managed in a wide range of network or channel levels.

Fabric introduces the concept of "permissioned" and combines the characteristics of channels to work in scenarios where privacy and confidentiality are paramount.

Consensus

It can be said that it is the primary problem that various blockchains need to solve. There are a lot of mentions in other blockchain projects, and Fabric is similar here. At present, Fabric allows developers to freely choose a variety of consensus algorithms through a plug-in architecture. The original version supports three consensus algorithms:

  • Ignore consensus (No-op)

  • Classic Byzantine Fault Tolerance (Classic PBFT)

  • SIEVE (enhanced version of PBFT)

  • For the complete transaction process, please refer to the transaction process in the Fabric official document (https://hyperledger-fabric.readthedocs.io/en/release-1.1/txflow.html).

7

Fabric tool introduction

If you have run the official bootstrap script mentioned above, you will find that some of Fabric's binary tools are stored in the fabric-samples/bin/directory:

  • configtxgen

  • configtxlator

  • cryptogen

  • orderer

  • peer

After understanding the basic model of Fabric, now briefly understand the role of each tool.

configtxgen can create and check channel configuration-related elements. The generated elements are determined by the content of the configtx.yaml configuration file. Mainly produced for the following elements:

  • Generate a genesis block

  • Create channel deal

  • Anchor Peer (https://hyperledger-fabric.readthedocs.io/en/release-1.1/glossary.html#anchor-peer) transaction between two organizations

The configtxlator is used to convert fabric data structures in protobuf and JSON format, and can also create configuration updates. This tool can be run as an HTTP service in REST mode, or it can be run like a normal command line tool.

Cryptogen is used to generate encryption certificates and keys for transmission encryption and identity verification.

Orderer and peer are used to run orderer node and peer node respectively. In the Fabric network, there are three types of nodes (Nodes):

  • client

  • peer

  • orderer

The client represents an actual end user behavior and must be connected to a node of the blockchain (it can be a peer or an orderer, or both, depending on the actual scenario requirements) to create a transaction. The concept of the client node is no different from the client in other C/S architectures.

The peer node receives the ordered status update and does not maintain the state of the ledger (actually, it acts as a copy node of the ledger). In addition, peer nodes can also exist as endorsers of special roles.

The orderer node provides ordering services, such as delivery guarantees. The orderer node provides a shared communication channel for the client and peer nodes. The sorting service supports multiple channels, and the client can connect to the specified channel to send and get messages.

Author: HiBlock block chain technology evangelism group - Feng Yu Ops

The original text was published in the brief book

Add WeChat baobaotalk_com, join the technical evangelistic group

The following is an introduction to our community, all kinds of cooperation, exchanges, and learning are welcome:)