Creating a Hyperledger Fabric network from scratch — Part III Chaincode

0
84

Creating a Hyperledger Fabric network from scratch — Part III Chaincode

Photo by Fré Sonneveld on Unsplash

In this new post we will create the “smart contract” chaincode of Hyperledger to define the logic of our application For simplicity, to always use the same language, both the chaincodes and the SDK we will use NodeJs for writing the chaincode.

Chaincode

To create our chaincode, we will make a new nodejs project. This project is what will be instanciated in the network to deploy the chaincode, so we will add the necessary dependencies on it, our package.json should look like this:

Once created, we see that its main is defined by pointing to the index.js, this file, retrieves the contract that we define within lib and exposes it so that it is accessible, in the form:

use strict’;
const SupplyChain = require(‘./lib/supplyChain’);
module.exports.supplyChain = SupplyChain;module.exports.contracts = [ SupplyChain ];

The real part of logic is inside the file that we define inside lib. In this chaincode, we will define the necessary methods to interact with the blockchain:

  • addAsset: Insert the object passed by parameter in the ledger, to be able to insert two types of assets, tuna as raw material and Sushi already prepared, we will make a common method to add the asset, and we will pass an object so that this code is the most simple as possible and only the data in the ledger persists.
  • queryAsset: Method that will allow us to access the status of the asset in the blockchain, in this case, this method will not access the ledger, so that searches do not penalize performance, this method will return only the current state of the asset, reading the world state.
  • setPosition: It will help us to establish the position of the asset, by passing the id of the asset and its coordinates, it will retrieve the asset from the ledger and update the new coordinates to plot its position.
  • getHistory: It will return all the history of the asset, all the changes that was done before in the ledger, except the current one, this history query the blockchain to see all the modifications that this asset has had.

The complete chaincode code will be as follows:

Now, you just need to install the necessary dependencies for the project and deploy it on the network. To instantiate it, it will be necessary to execute within the directory:

npm i

Installing it on the network is somewhat more complex, the chaincode must be installed on all nodes, and instantiated only in one of them, it is only necessary to instantiate it in one of the organizations, doing so will create a docker container that will be accessible to all.

To facilitate installation we create a script that installs this contract in all organizations through the CLI.

This script will call the installation of each of the organizations, passing the data of each one (peer, name, MSPid, port and version of the chaincode) and once installed it will instantiate it.

The script responsible for installing the chaincode is of the form

As we can see, it receives the data passed by parameter and uses it to set the necessary variables when calling install, then use the install instruction to install the code in the peer and display the result by console.

Once installed, we only have to instantiate, so the last script is responsible for instantiating the contract on the network, with the version that we have specified, this procedure may take more than installation.

With this, we will have instantiated the contract in our network, when can check doing:

docker ps

We will see that there is a new container, with the name of the contract and the version that we have deployed.
In the next post, we will create the identities with the SDK and the rest API to access the methods.
The repository with the entire application can be found on Github

Previous post can be found here:

Creating a Hyperledger Fabric network from scratch — Part II Starting the network


Creating a Hyperledger Fabric network from scratch — Part III Chaincode was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.