banner
leaf

leaf

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

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 includes 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 how to use NODE SDK and interact with Fabric network based on NODE SDK and Fabric network in a web page.

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: administrator, used to update and manage car data, and ordinary users, 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 enrolling 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, which configures 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 car information to 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 web page, so that users can operate the Hyperledger Fabric through this interactive page.

  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 is to start the underlying 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 located in the fabric-samples/chaincode/fabcar/go folder, and 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, as well as the corresponding processing logic 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 with 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 network startup

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, in this file, call the SDK to create an administrator with the username "admin", as shown in the figure

image

Create admin administrator

To create an administrator, use registerUser.js, in this file, call the SDK 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 use the npm install command to install the SDK, as shown in the figure.

Install SDK

After the installation is complete, register the administrator and new user by running 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.

Then 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, in invoke.js, call the createCar in Chaincode 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 web page, so that users can directly interact with the Fabric network through the web page and perform queries on the Hyperledger Fabric. Here, we use the express framework to develop a simple page 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 part 1. Start the network and initialize the data 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.