This post introduces Fabric's application development model and the Node SDK, and runs a simple Fabric application sample.

Introduction

The Fabric samples covered earlier (such as first-network and e2e-cli) are only simple tests on a single machine. They start a cli container to interact with the network's member nodes, and every operation is carried out by typing commands into that container by hand.

In real development, a Fabric blockchain application should have a complete application program that handles requests from the frontend, calls the SDK to interact with the nodes, and ultimately relies on the underlying Fabric features to store data on the blockchain.

Application Model

Architecture diagram

A Fabric application can be divided into three layers: the app layer, the SDK layer, and the underlying Fabric layer. What developers need to build is the app and the chaincode. The application usually runs on a client node. It handles requests and calls the corresponding SDK to communicate with peers, orderers, and CA nodes. The chaincode executes the business logic, querying data from the ledger or writing updates to it.

Fabric Node SDK

Hyperledger Fabric offers SDKs in several languages, including Go, Node.js, Java, and Python. The Node SDK is officially maintained, the most stable, and the best documented.

The main modules of the Node SDK and their key methods are as follows:
1. Client

  • getUserContext() / setUserContext(): read/write user information locally
  • create instances of the other classes

2. CAClient

  • register(): registration
  • enroll(): enrollment

3. Channel

  • sendTransactionProposal(): send a proposal
  • sendTransaction(): send a transaction
  • queryByChaincode(): query through the chaincode
  • various queries related to blocks and transactions

4. Peer

  • sendProposal(): send a transaction proposal to a peer

5. Orderer

  • sendBroadcast(): send data to the orderer
  • sendDeliver(): fetch data from the orderer

Balance Transfer

Balance transfer is a sample application for the Fabric Node SDK. Built on the Node.js express framework, it implements a complete web service with RESTful APIs for network initialization, user registration with certificate generation, transfer transactions, queries, and more.

1. Download the sample
The required environment has to be set up before downloading. That was covered in detail in an earlier post, so I won't repeat it here. The fabric-samples version should match the version of the Fabric images, otherwise you may run into all kinds of errors. You can switch versions with git checkout and the corresponding version tag.

git clone https://github.com/hyperledger/fabric-samples.git
cd fabric-samples/balance-transfer

2. Start the network
In terminal 1, run:

./runApp.sh

The script does the following:

  • starts the Fabric network nodes (docker-compose up)
  • installs the required node modules (npm install)
  • starts the application listening on port 4000 (node app.js)

3. Test the network
Install jq before testing:

sudo apt-get install jq

In terminal 2, run the test script, which mainly uses curl to send requests:

./testAPIs.sh

The script does the following:

  • creates the channel and joins the peers to it
  • installs and instantiates the chaincode
  • invokes transfer transactions
  • runs various queries

4. Bring down the network

# remove all containers and the docker network
cd artifacts
docker-compose down
# remove the chaincode images
docker rmi -f $(docker images | grep dev | awk '{print $3}')
# remove user data, including private keys and certificates
rm -rf fabric-client-kv-org[1-2]

Tags: fabric

4 comments

  1. 这个搭建只是为了测试,我想用到web上,那我这个怎么搞,大神有推荐网址吗?我才学,不太懂。不过感谢大神的分享。

    1. zyj zyj

      参考上面的应用模型图,可以在应用层之上再封装一个web层。用户进行前端交互,调用后台的app层接口。

  2. YUX YUX

    你好,请问有弄过fabric-java-sdk 的吗?还有你github上面那个fabric-tool主要做什么的呢?

    1. zyj zyj

      只用过node-sdk,不过两者功能差不多。fabric-tools主要是一些基于node-sdk的 动态增删组织 和 多机部署 的自动化脚本。

Leave a Comment