banner
leaf

leaf

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

Superledger Development Example 2 - fabcar Blockchain Application

Built a Fabric network using Hyperledger Fabric and interacted with this Fabric network through CLI command line. In this section, we will learn another example in Fabric Samples called fabcar. Fabcar is an example based on NODE SDK and comes with smart contracts (where NODE_SDK is the JavaScript software development toolkit provided by Hyperledger Fabric, developers can interact with the Fabric network through NODE_SDK, which is another way of interaction besides CLI), through this example, we can understand the usage of NODE SDK and how to interact with the Fabric network based on NODE SDK and Fabric network.

Before entering the development of this example, let's summarize the interaction process between a blockchain application and a blockchain network.

In the Fabric network, an application (Application) first needs to be confirmed by the developer certificate (Application Developer Identity), and after confirmation, it executes smart contracts (Run smart contracts), smart contracts can query and update the blockchain network (Receive ledger updates). After the blockchain network is successfully updated, it notifies the application, as shown in the figure

image

Fabcar implements the function of car data management, which includes two roles, one is the administrator, used to update and manage car data, and the other is the ordinary user, who can query all car data. The specific functions implemented by this example are as follows:

● Define a structure called Car to store car data and owner information.

● Initialize and start a test network.

● Have the function of registering and registering administrators.

● Have the function of registering new users.

● Can query and update car information.

Entering the fabcar folder, you can see a shell script and several js files, as shown in the figure

image

Among them, package.json defines the JavaScript modules required for this project, as well as the configuration information of the project (such as name, version, license, etc.). When executing the npm install command, the required modules will be automatically downloaded based on this configuration file, that is, configuring the required runtime and development environment for the project. The startFabric.sh script is used to initialize the Fabric network, start nodes, create channels, instantiate Chaincode, and write the initial information of the car into the blockchain. enrollAdmin.js is used to register the administrator. registerUser.js is used to register new users. query.js can query all car information. invoke.js is used to call Chaincode and execute its functions.

Let's start developing and deploying this fabcar application.

Development and Deployment of fabcar#

In this example, we will first start and initialize a Fabric network, then create an administrator and a new user for testing, and then interact with the network through NODE SDK and integrate NODE SDK into a webpage, so that users can operate the Hyperledger Fabric through this interactive webpage.

  1. Start the network and initialize the data

The first step is to execute the startFabric.sh script in the fabcar folder. The purpose of this script file is to start this basic network, then start the CLI container, instantiate Chaincode, and load the initial car information, as shown in the figure

In the above process, the functionality of Chaincode is important. Let's take a closer look at the implementation details of Chaincode. The code of Chaincode is placed in the fabric-samples/chaincode/fabcar/go folder. The fabcar.go file defines the structure of the car, as shown in the figure

image

In the Chaincode code, the Init() method and Invoke() method of Chaincode need to be implemented. In the Init() method, the initialization operation of Chaincode is implemented, and corresponding processing logic is implemented for different events such as querying car information (queryCar), initializing ledger (initLedger), creating car information (createCar), querying all car information (queryAllcars), and updating car owner (changeCarOwner), as shown in Figure 6-31.

Figure 6-31 Implementing the Chaincode() method

In the Invoke() method, the functionality of calling queryCar (query car information), initLedger (initialize ledger), createCar (create car information), queryAllCars (query all car information), and changeCarOwner (update car owner) is supported.

image

After starting the network using startFabric.sh, if everything goes well, you can see the successful output information, the time consumed by the entire startup, and some help information, as shown in the figure.

Successful output information after the network starts

After successfully starting the network, you also need to create an administrator and a new user.

  1. Create an administrator and a new user

To create an administrator, use enrollAdmin.js, which calls the SDK in this file to create an administrator with the username "admin", as shown in the figure

image

Create admin administrator

To create a new user, use registerUser.js, which calls the SDK in this file to create a new user with the username "user1", as shown in the figure

image

Create user1 new user

Before executing these two scripts, you need to install the SDK using the npm install command, as shown in the figure.

Install SDK

After the installation is complete, register the administrator and new user using node enrollAdmin.js and node registerUser, as shown in the figure

image

Then query the information of a car (such as CAR3), modify query.js, as shown in Figure 6-38.

Execute the command "node query.js", you can see the information of CAR3 returned, as shown in Figure 6-39.

Then try to call Chaincode to update the content of the Hyperledger Fabric, call the createCar in invoke.js to create a new car information, as shown in the figure.

image

Execute the command "node invoke.js", as shown in the figure. Then you can see that there is one more car when querying all car information,

image

Finally, integrate NODE SDK into a webpage, allowing users to directly interact with the Fabric network through the webpage and perform queries on the Hyperledger Fabric. Here, we use the express framework to develop a simple webpage to display and call the SDK. Express is a concise and flexible node.js web application framework that provides a series of powerful features to help users create various web and mobile applications.

The website's functionality is to return all car information when accessing the homepage, and return the car details page when accessing the corresponding car page. First, execute npm install express in the terminal to install the express framework, and then create a file named server.js. In this file, load and create an express application and listen on port 3000. When accessing this port 3000, query all car information loaded in the 1. Start the network and initialize the data part and return it.

The main code is as follows:

image

After completing the code, run the command node app.js in the terminal to run the application. After running, you can access 127.0.0.1:3000 in the browser to view all car information, as shown in the figure

image

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