● Understand the architectural design of Hyperledger.
● Learn to set up the Hyperledger development environment.
● Master the development process of Chaincode.
● Master the Fabric framework development through examples.
Hyperledger is an open-source project initiated by the Linux Foundation in December 2015, aimed at providing a reliable, stable, and high-performance blockchain framework for enterprises to create custom distributed ledger solutions, thereby promoting the application of blockchain technology in business.
The architecture of Hyperledger includes four major modules: Membership, Blockchain, Transactions, and Chaincode. The Membership module mainly provides membership services, including user registration, identity management, and auditability; the Blockchain and Transactions modules provide blockchain-related services, responsible for consensus management, distributed ledger, P2P protocol, and ledger storage; the smart contract service is responsible for providing the functionality of smart contracts, which are placed in a secure registry and executed in a secure container.
In the actual development process of Hyperledger, it has been developed based on the above architecture and has incubated many blockchain projects, mainly including Sawtooth, Iroha, Fabric, Burrow, etc.
Among them, Sawtooth is a modular platform for building, deploying, and running distributed ledgers; Iroha is a commercial blockchain framework designed to be simple and easy to integrate into infrastructure projects that require distributed ledger technology; Fabric is an open-source blockchain development framework that provides a modular architecture, including nodes, smart contracts, and configurable consensus and membership services; Burrow can be seen as a framework that supports Ethereum smart contracts, built according to the Ethereum Virtual Machine (EVM) specification. Among these Hyperledger-based projects, Fabric is the most famous, allowing for the rapid construction of enterprise-level blockchain systems. Generally, Hyperledger mainly refers to Hyperledger Fabric, and the Hyperledger discussed in this chapter also refers to Hyperledger Fabric.
Next, let's understand the architecture of Hyperledger Fabric.
The architecture of Fabric has undergone two versions of evolution. The initially released version 0.6 was only used for commercial validation and could not be applied in real scenarios. Due to its simple structure, almost all functions were concentrated in peer nodes, lacking scalability, security, and isolation. In the later released version 1.0, the functions of peer nodes were split, separating consensus services from peer nodes into independent orderer nodes, providing pluggable consensus services. More importantly, multi-channel functionality was introduced to achieve business isolation.
Several core concepts that need to be understood in Fabric 1.0 are as follows.
● SDK: Software Development Kit.
● Membership: Responsible for identity and permission management, also known as MemberService or Identity Service.
● Chaincode: Application code on the blockchain, extending the concept of "smart contracts," supporting programming languages such as Go and Java, and running in an isolated container environment.
● Orderer: The consensus service role in the Fabric 1.0 architecture, responsible for ordering transactions, batching them, generating blocks, and sending them to peer nodes. There are multiple orderer nodes in a blockchain network, which collectively provide ordering services.
● Endorser: A type of peer node role in the Fabric 1.0 architecture, responsible for verifying whether a transaction is valid and whether it is willing to endorse it.
● Committer: Another type of peer node role in the Fabric 1.0 architecture, responsible for checking transactions ordered by the orderer, selecting valid transactions for execution, and writing them to storage.
● Enrollment Certificate Authority (ECA): The certification center responsible for managing member-related certificates.
● Transaction Certificate Authority (TCA): The certification center responsible for maintaining transaction-related certificates.
In Fabric, there are mainly two types of nodes: peer nodes and orderer nodes. A blockchain network will have multiple peer nodes, and a peer node can serve multiple roles, such as acting as an endorser or a committer. Chaincode is deployed on peer nodes, which are used to execute smart contracts. Orderer nodes act as proxies in the network, with multiple orderer nodes connecting to a Kafka cluster, utilizing Kafka's consensus functionality to complete the ordering and packaging of transactions into blocks. Additionally, a peer node can join multiple channels, which are completely isolated from each other, with each channel only receiving and processing blocks related to that channel, achieving data isolation and confidentiality.
Features of Hyperledger Fabric#
The architecture of Hyperledger provides it with the following features.
● Modular design, including modules for consensus, permission management, encryption and decryption, ledger mechanisms, etc., allowing for flexible selection and replacement.
● Multiple types of nodes. Different nodes are assigned different functions, improving transaction processing efficiency.
● Enhanced identity certificate management services, providing identity certificates, digital signatures, verification algorithms, and several functions to determine the validity of identities.
● Support for multi-channel features, with data isolation between different channels, enhancing isolation security.
● Introduction of chaincode to implement smart contract functionality, achieving programmability and supporting third-party custom functions.
● Full utilization of Docker container technology, allowing for flexible deployment based on load.
Setting Up the Fabric Development Environment#
Hyperledger is primarily developed in Go and deployed using Docker container technology. The configuration process for the Hyperledger development environment is relatively simple and can be operated on Windows, Linux, and Mac systems. To develop a blockchain using Hyperledger, you first need to install the Go development environment and Docker tools. Then, you can use the official installation script to automatically download and install the required files and Docker images, and once the download is complete, you can start development. Here, we will briefly introduce Go and Docker tools; readers familiar with these two parts can skip ahead to the Fabric local development environment installation section.
Introduction to Go and Its Development Environment Installation#
Go is a strongly typed general-purpose programming language launched by Google. Its good language design, efficient performance, and powerful concurrent programming capabilities make it well-suited for developing distributed systems like blockchain.
To install and use Go, you can go to the Go download page at https://golang.org/dl/ to select the installation file for the corresponding system (access may require appropriate settings). The Go download page is shown in Figure 6-7.
After completing the installation of the Go development environment, you need to add the Go directory /usr/local/go/bin to the system's PATH environment variable, using the following command:
$ export PATH=$PATH:/usr/local/go/bin
Then, write a program that outputs "Hello, Golang!" to test whether the Go development environment is installed correctly.
Create a file named hello.go and enter the following content:
Then open the system's command line terminal and execute the command go run hello.go. You should see the output "Hello, Golang!" as shown in the figure.
Go's syntax is relatively simple, and the Go official website also provides online learning tutorials. Interested readers can visit https://tour.golang.org/welcome/1 for learning.
Docker is an open-source application container engine developed based on Go. It is currently the most popular container solution. Docker is a packaging of Linux containers, providing a simple and easy-to-use interface for container usage.
Docker allows developers to package their applications and dependencies into a portable container, which can then be deployed on any popular Linux machine to start and run applications. Docker enables developers to manage their projects conveniently and quickly, completing project deployment and updates in just a few minutes.
Docker uses a client/server (C/S) architecture, consisting of the Docker client and the Docker daemon. The Docker daemon runs on the host machine, handling complex and heavy tasks such as creating, running, and publishing Docker containers. The Docker client (or command-line tool) is the primary way users interact with Docker, communicating with the Docker daemon and returning results to the user. The Docker client and Docker daemon can run on the same system, but the Docker client can also connect to a remote Docker daemon. Communication between the Docker client and Docker daemon occurs via socket or RESTful API, as shown in the figure.
Having understood the basic components of Docker, let's look at three main concepts of Docker.
● Docker Image: Docker images are read-only and contain the files needed to run. Images are used to create containers, and one image can run multiple containers; images can be created via Dockerfile (a text file from which Docker generates an image) or downloaded from the Docker repository.
● Docker Container: Docker containers are the running components of Docker. Starting an image creates a container, which is an isolated environment where multiple containers do not affect each other, ensuring that programs within the container run in a relatively secure environment.
● Docker Repository: Docker repositories are used to share and manage Docker images, allowing users to upload or download images.
The official address for the Docker repository is https://registry.hub.docker.com/, and you can also set up your own private Docker repository.
Next, we will proceed with the installation of Docker. On Mac and Windows, you can download the installation package from the official website, while on other Linux systems, you can install it via command line.
The download address for the Docker installation package is: https://www.docker.com/get-started#
After downloading the file, double-click the Docker installation program to install it. Once installed, you can double-click the Docker icon to start the Docker daemon, and then use the Docker client to interact with the Docker daemon. If everything is installed correctly, entering the command docker run hello-world in the terminal will automatically download the hello-world image and start a container that outputs "Hello from Docker," as shown in the figure.
This indicates that Docker is functioning correctly. You can enter the docker command to view common usage methods of the Docker client, such as:
Here are a few commonly used Docker commands.
● Start a container and launch the bash command line tool.
docker run -i -t <image_name/container_id> /bin/bash
● Enter an already running container while executing bash.
docker exec -t -i <id/container_name> /bin/bash
● View container logs.
docker logs <id/container_name>
● List all currently running containers.
docker ps
● Delete a single container.
docker rm Name/ID
● Stop, start, terminate, or restart a container.
docker stop Name/ID
docker start Name/ID
docker kill Name/ID
docker restart Name/ID
● List images.
docker images
● Search for images.
docker search image_name
● Download an image.
docker pull image_name
● Delete one or more images.
docker rmi image_name
Finally, regarding Docker, you also need to know how to generate custom image files. To generate a custom image file, you need to write a Dockerfile. A Dockerfile consists of a series of command statements and supports comment lines starting with #. Generally, a Dockerfile is divided into four parts: base image information, maintainer information, image operation instructions, and instructions to execute when the container starts. Here, we take nginx (a high-performance HTTP and reverse proxy server) as the base image as an example. Create a blank Dockerfile and enter the following content:
Dockerfile Example#
The first line must specify the base image#
FROM nginx
Maintainer information#
MAINTAINER XXX [email protected]
Image operation instructions#
RUN echo '
Hello, Docker!#
' > /usr/share/nginx/html/index.htmlInstructions to execute when the container starts#
!! This example is relatively simple and does not require instructions to execute when the container starts !!#
After saving the file, execute the command docker build -t hello_docker . in the terminal to build the image. After execution, you will see that a local image named hello_docker has been created, as shown in the figure.
This is a brief introduction to Docker. For further learning, you can purchase specialized Docker tutorials for self-study, such as "Docker Introduction and Practice" or "The First Book on Docker."
After gaining a certain understanding of Go and Docker, you can officially set up the Fabric development environment.
The current latest version of Fabric is 1.2.1. Open the command line tool and use the following command to install it:
curl -sSL http://bit.ly/2ysbOFE | bash -s 1.2.1
This command will perform the following actions:
-
Check if the Fabric code for version 1.2.1 exists.
-
Download the programs and configuration files from the 1.2.1 version of the Fabric project to the current folder, as shown in the figure.
- Download the 1.2.1 Fabric Docker images to the current system, as shown in the figure.
You can also download the sample code of the Fabric project to your local machine. These sample codes are projects implemented based on Fabric, and this book also uses these examples to explain how to use Fabric. The command to download the sample code is as follows:
git clone https://github.com/hyperledger/fabric-samples.git
At this point, the Fabric development environment is set up. You can visit the official Fabric website for installation steps, with the web link being https://hyperledger-fabric.readthedocs.io/en/latest/getting_started.html.