banner
leaf

leaf

It is better to manage the army than to manage the people. And the enemy.
follow
substack
tg_channel

Hyperledger Development Example 1 - Establishing a Fabric Network

● 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 smart contract functionality, with smart contracts 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, and Burrow.

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 supporting 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 evolved through two versions. 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, most functions were concentrated in peer nodes, lacking scalability, security, and isolation. In the later released official 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.

Changes in Hyperledger Fabric Architecture

● Membership: Responsible for identity and permission management, also known as Member Service 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 orderer nodes, selecting valid transactions for execution, and writing them to storage.

● Enrollment Certificate Authority (ECA): The certification center responsible for managing member identity-related certificates.

● Transaction Certificate Authority (TCA): The certification center responsible for maintaining transaction-related certificate management.

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, and ledger mechanisms, 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 functionalities.

● 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 performed 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. Once the download is complete, you can start development. Here, we will briefly introduce Go and Docker tools; readers who are familiar with these 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 suitable for developing distributed systems like blockchain.

To install Go, you can visit the Go download page at https://golang.org/dl/ and 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 with 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:

image

Go Download Page

Then open the system's command line terminal and execute the command go run hello.go, and you should see the output "Hello, Golang!" as shown in the figure.

image

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 very conveniently and quickly, completing project deployment and updates in just a few minutes.

Docker uses a client/server (C/S) architecture model, consisting of two parts: 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.

Go Online Learning Page

Docker Client and Docker Daemon

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 using a Dockerfile (a text file that Docker uses to generate an image) or downloaded from a 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 safe 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 users can also set up their own private Docker repositories.

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 installer 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.

image

Output Hello from Docker

This indicates that Docker is now functioning correctly. You can enter the docker command to view common usage methods of the Docker client, such as:

Docker Client Usage Methods

Here are a few commonly used Docker commands.

● Start a container and launch the command line tool bash.

docker run -i -t <image_name/container_id> /bin/bash

● Enter the 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

Regarding Docker, it is also important to know how to generate custom image files. To generate a custom image file, you need to write a Dockerfile. A Dockerfile consists of command statements line by line and supports comment lines starting with #. Generally, a Dockerfile is divided into four parts: base image information, maintainer information, image operation instructions, and commands executed when the container starts. Here is an example using nginx (a high-performance HTTP and reverse proxy server) as the base image. 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.html

Commands executed when the container starts#

!! This example is relatively simple and does not require commands to be executed 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 should see a new image named hello_docker locally, as shown in the figure.

image

This is a brief introduction to Docker. For further learning, you can purchase dedicated Docker tutorials for self-study, such as "Docker Introduction and Practice" or "The First Book on Docker."

After gaining some 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 operations.

  1. Check if the Fabric code for version 1.2.1 exists.

  2. Download the programs and configuration files from the Fabric project version 1.2.1 to the current folder, as shown in the figure.

image

  1. Download the Fabric docker images for version 1.2.1 to the current system, as shown in the figure.

Downloaded Image Files

You can also download example code from the Fabric project to your local machine. These example codes are projects implemented based on Fabric, and this book uses these examples to explain how to use Fabric. The command to download the example 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 Fabric official website for installation steps, with the webpage link being https://hyperledger-fabric.readthedocs.io/en/latest/getting_started.html.

Officially entering the development of Hyperledger, we will first understand the concept of Chaincode, then introduce how to develop Chaincode, how to deploy Chaincode to Fabric, and how to use Chaincode.#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.