At the heart of a blockchain network is a distributed ledger that records all the transactions that take place on the network. A blockchain ledger is often described as decentralized because it is replicated across many network participants, each of whom collaborate in its maintenance.
Queries are how you read data from the ledger. This data is stored as a series of key-value pairs, and you can query for the value of a single key, multiple keys, or – if the ledger is written in a rich data storage format like JSON – perform complex searches against it (looking for all assets that contain certain keywords, for example).
This is a representation of how a query works:
Querying a ledger (
Source: Hyperledger Fabric
)
Pre Requisites
We will be using the official fabcar chaincode of the Hyperledger Fabric Samples in this article. Before proceeding with the article, I will assume that you have set up the Dev Environment , downloaded the fabric-samples , and a network is running on your machine.
After downloading the fabric samples, navigate to the fabcar subdirectory
within the fabric-samples directory. Run npm install
to install the required fabric dependencies and launch the network by running the shell script against the chaincode written in NodeJS (./startFabric.sh node
)
Setting up the project
As the first step in any project, we will be creating the folder and the file structures.
The packages fabric-ca-client
& fabric-client
are the ones which help us to interact with the Fabric network and express
is to create the web server for RESTFul API .
Since we will be mainly dealing with querying the ledger. I recommend you to refer Varun’s article . To make it to work with our fabcar chaincode, make sure that you edit the model class file and add the following function,
The above function will call the initLedger
fuction in the chaincode. This function will insert a set of record into the Ledger which contains the details about cars.
Now, set up the server in index.js
file and write the following end point:
The above endpoint will make a call to the invoke function which will insert the record into the ledger.
Querying the Ledger
Set up the endpoint in order to query the Ledger:
After setting up the endpoint in the index.js file, let’s make a call to the Ledger by invoking the function in the chaincode. Thus the endpoint will call the query method in the model class which will in turn call the function of the chaincode to query data. Add the following function in the model class file,
In the Config/FabricClient.js
file, we have written a function to clean the data that were retrieved from the Ledger. The above function will call the following function which resides in the FabricClient.js
file,
The above function will first get the corresponding channel and makes a call to the chaincode function. The function will return the payload. The payload is the list of all the cars that are available in the ledger. Since the payload will be in the form of a string, we will parse it using JSON.parse
to convert it to a javascript object. If the payload is an array, we retrieve the data and store it in the resultData
variable. If it is not an array, we directly return the retrieved payload instead.
And that’s all there is to it! While there are many JS clients you can use to expose Hyperledger Fabric’s APIs, I felt ExpressJS was the most straightforward option. If you’ve used anything else, let me know in the comments section below!