Building a Hyperledger Fabric Network from Scratch

In this article, we will first describe how to build a Hyperledger Fabric network from scratch on Alibaba Cloud Elastic Compute Service (ECS) or Mac. Then, we will look at how to download fabric-sdk-java from IntelliJ IDEA on the local Mac computer and use End2EndIT to perform unit tests on the blockchain deployed on ECS or Mac. Finally, we will look at the whole development process with a real use case.

Install Dependencies for Fabric

Fabric is dependent on cURL (which comes with Linux and Mac terminals by default), Docker, and Go.

Install Docker CE on ECS. For detailed information, see Install Docker on Alibaba Cloud ECS.

Install docker-compose.

Install Docker on Mac as follows: Run the homebrew command to install Docker (including docker-compose) or directly download and install Docker.

After Docker is installed on Mac, a question mark (?) icon appears on the upper right corner of the Mac desktop.

Install Go on ECS.

Configure Go environment variables.

Install Go on Mac.

Configure Go environment variables.

Install Fabric

Both ECS and Mac can call scripts, such as Fabric binaries, official sample, and all types of Docker images of Fabric.

Note: If the system does not respond when the preceding command is run, terminate the command, click, and paste the web content into a local .sh file.

Git is not installed on ECS by default. Install Git first; otherwise, fabric-samples cannot be checked out.

If an exception occurs during installation and you are prompted to choose installation items, modify the following lines in the script and choose to install the Docker images, samples, and binaries as required.


  1. Because Docker, on which Fabric is dependent, is larger than 10 GB, you will have to wait for it to install.
  2. Configure the accelerator on MAC as follows: Click the question mark (?) icon on the upper right corner of the Mac desktop, choose Preference > Daemon > Registry mirrors, and add an Alibaba Cloud or DaoCloud personal accelerator address.

After the command is run, you can check whether the tools required by Fabric are stored in the /usr/local/bin directory.

View the downloaded Docker images.

Official Fabric sample

(1) Official Sample: Build Your First Network (BYFN)

If you directly run the ./ up command in the cloud ECS environment, the system will break down. For detailed information, see Official Fabric issue.

Hyperledger officially suggests that — GODEBUG=netdns=go be added. However, test results indicate that this method does not work and the peer may still fail to join the channel. This problem still persists even if the timeout duration is prolonged.

According to our cause analysis, modify the /etc/resolv.conf file by commenting out the following line: options timeout:2 attempts:3 rotate single-request-reopen.

(2) BYFN Startup Process

Step 1

Call the script and networkUp method as follows:

  1. Verify the version.
  2. Use the cryptogen tool to generate public and private keys and certificates.
  3. Use the configtxgen tool to generate a genesis block, a channel configuration block, and two organization anchor points.
  4. Call the scripts/ script to perform subsequent operations.

Fabric supports two types of public and private keys and certificates, namely, a TLS certificate for ensuring the communication security between nodes and a user certificate for user logon and permission control. These certificates should have been issued by the CA, but we use the test environment where the CA node is not enabled. Fabric has provided us with the cryptogen tool.

The cryptogen tool generates a crypto-config folder in the current directory based on the crypto-config.yaml file. The folder structure is shown as follows.

The crypto-config.yaml file is parsed as follows.

The configtxgen tool generates four components based on the configtx.yaml file. Before this operation, specify the path to the configtx.yaml file for the configtxgen tool. We need to set an environment variable as the current directory, and the generated files will be stored in the channel-artifacts folder of this directory.

  1. Generate a genesis.block file for the orderer node.
  2. Generate a channel configuration transaction file channel.tx.
  3. Generate an anchor peer for Org1 of the channel.
  4. Generate an anchor peer for Org2 of the channel.

Anchor peer: specifies a peer that can be discovered by all other peers in the same channel. Each member of the channel has an anchor peer (or multiple anchor peers to prevent single-point failures). Peers belonging to different members are allowed to discover all existing peers in the same channel.

The configtx.yaml file contains TwoOrgsOrdererGenesis (the orderer consensus configuration for two organizations) and TwoOrgsChannel (the channel configuration for two organizations). The consensus algorithm can be set to Solo or Kafka for the orderer. The default values of the consensus time block size, timeout duration, and other parameters can be used. The configurations of peer nodes include the configurations of the MSP and anchor nodes. If more organizations or channels are required, you can modify the configurations based on the template.

Step 2

Enable each container of Fabric. The docker-compose tool uses the docker-compose-cli.yaml file in the current directory to initialize the environment. It then generates four fabric-peer transaction node containers, one fabric-order orderer node container, and one fabric-tools Cli container. The Cli container is used to replace the SDK client. Because the Java SDK was not started, a client tool needs to be created to connect to the Fabric network for testing.

The docker-compose-cli.yaml file inherits all the content parsed from the docker-compose-base.yaml file. The docker-compose-base.yaml file is parsed as follows.

The peer-base.yaml file is parsed as follows.

Step 3

After containers are created, create a channel, add peer nodes to the channel, and configure an anchor point for each organization. The channel is similar to that used in Netty and can be used to isolate transactions.

Run the docker exec -it cli bash command to enter the Bash of the simulated client Cli container. Then, run the channel creation command to create a channel in the container.

Parameter description:

-o: specifies the orderer node In the docker-compose-base.yaml file, port 7050 is specified for the orderer node.

-c mychannel -f ./channel-artifacts/channel.tx: specifies the channel name and uses the channel.tx configuration file that has been generated to initialize the channel.

— tls true: indicates that TLS is used for encrypted transmission on the network.

— cafile: specifies the CA certificate path.

Set the environment variables and add peers to the channel.

Note: peer0.org1 is connected to the Cli container by default. This means that peer0.org1 can use the peer channel join -b mychannel.block command directly. Other nodes can connect to the Cli container only if their environment variables have been modified correctly.

Configure an anchor point for the two organizations respectively: and

Step 4

Install the chain code, and create an instance. The chain code needs to be installed on each peer node. As described in step 3, peer0.org1 is connected to the Cli container by default, and its environment variables do not need to be set.

Install the chain code.

Instantiate the chain code.

This operation is used to pack the previously installed chain code on the terminal where the peer is located, and generate the Docker image and container corresponding to the channel. An endorsement strategy can be specified during the instantiation.

The chain code needs to be instantiated only once.

Start another terminal to view the corresponding logs and the started Docker container.

Step 5

Query and verify data.

Simulate a scenario in which a transfers 10 dollars to b.

Obtain fabric-sdk-java

This project requires an IntelliJ IDEA environment and JDK 1.8 or later.


Fabric uses Google Protocol Buffer (Protobuf) as a serialization tool but JSON does not support Protobuf. Therefore, you must install the Protobuf plug-in on IntelliJ IDEA first.

On Mac, use homebrew to install Libtool; otherwise, Mac will report the error “ltdl.h: no such file or directory” when a unit test is started.

Pull the branch code and switch to version 1.1. The latest version is 1.2, but it has not been released yet and therefore cannot be started.

Get Started with Mac and End2EndIT

The Mac terminal functions as both the server and client, on the premise that Fabric has been installed on it by following the instructions in sections 1 and 2.

Server: Run the following two commands to start the Fabric blockchain network.

Client: Open and run it to conduct a simple blockchain transfer transaction.

The script is described as follows.

Run the following commands to view logs:

Use Case: Running the fabric-sdk-java Project

Detailed Project Directory

End2EndIT Code Details

The SDK uses Protobuf as the transport protocol, uses gRPC to communicate with the server, and uses YAML as the configuration file format. If you are not familiar with these concepts, get started by following the links below.

We recommend that you follow the official example of Fabric to perform operations. In the SDK process, the preceding command lines of the simulated Cli client are simply replaced with Java code.

Run the checkConfig() method to check configuration items.

// Construct a channel and add Org1 to this channel.

Channel fooChannel = constructChannel(FOO_CHANNEL_NAME, client, sampleOrg);

// Install and instantiate the chain code, and perform a query test.

runChannel(client, fooChannel, true, sampleOrg, 0);

The two core methods are described as follows in detail:

Constructing a Channel

Set the user context of the operation.

Get the admin account of the peer. In End2endIT.enrollUsersSetup, this field is set as shown in the following code. This field is actually the default admin account configured by the cryptogen tool based on the crypto-config.yaml file. The read files are those in the v1.1 folder generated by the cryptogen tool:


Initialize the orderer node objects.

sampleOrg.getOrdererLocation obtains the URL of the orderer gRPC configured in testConfig. The following code shows the default value.

Create a channel. The process is similar to that described in the preceding official sample of Fabric. As mentioned above, the configtxgen tool generates four types of files. The channel configuration file src/test/fixture/sdkintegration/e2e-2Orgs/V1.1/foo.tx generated by the configtxgen tool is used here.

Run the client.newPeer method to create a peer node, and then run the joinPeer method to add the peer to the channel.

The joinPeer logic is complex. The peer needs to be signed first, and then the channel join proposal needs to be sent to all peer nodes. Details are not provided here.

peerLocation obtains the URL of the peer gRPC configured in testConfig. The following code shows the default value.

Configure a gRPC port as the event listener port for the channel, and initialize the channel.

Running the Channel

Install the chain code. The chain code file is example_cc.go in Java. This file is compiled by using the Go language.

Initialize the chain code instance and set the endorsement strategy.

The following code is only used for testing.

Finally, query the block height and other information.


Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website: