Get the code
by Oliver Rodriguez, Rojan Jose | Published March 27, 2019
In this developer code pattern, we will take a look at how to query the world state of a peer within Hyperledger Fabric. The world state, implemented as a database, contains all of the network history (e.g., transactions issued). Querying the world state is useful for seeing the current state of the assets in the network. We will use the commercial paper use case from the Hyperledger Fabric 1.4 documentation. You’ll go through the process of creating indexes for the CouchDB world state database, update the smart contract to include the logic to query the world state using the newly created indexes, simulate 100 transactions to populate the world state with assets, then run a few queries utilizing the Node.js SDK and view the returned results.
Before we jump into showing how to query the world state, we first need to understand what database indexes are and how they can help us with queries.
In order to understand indexes, let’s take a look at what happens when you query the world state. Say, for example, you want to find all assets owned by the user “Bob.” The database will search through each JSON document in the database one by one and return all documents that match user = bob. This might not seem like a big deal, but consider if you have millions of documents in your database. These queries might take a while to return the results since the database needs to go through each and every document. With indexes, you create a reference that contains all the values of a specific field and which document contains that value. What this means is that instead of searching every document, the database can just search the index for occurrences of the user “bob” and return the documents that are referenced. It’s important to note that every time a document is added to the database, the index needs to be updated. Normally in CouchDB, this is done when a query is received, but in Hyperledger Fabric, the indexes are updated every time a new block is committed, which allows for faster querying. This is a process known as index warming.
The developer creates the query indexes.
The developer adds query logic to the smart contract.
The IBM Blockchain extension for VS Code is then used to package, install, and instantiate the smart contract and indexes on the local Hyperledger Fabric network.
We simulate 100 transactions on the network to populate the world state with data.
The Node.js client application uses the Hyperledger Fabric SDK to send the query to the endorsing nodes to evaluate the query transaction.
The query results are returned.
Ready to get started? Check out the README for detailed instructions.
August 21, 2019
June 22, 2019
API ManagementArtificial intelligence+
Get the Code »
Back to top