Query System ChainCode (QSCC) in Hyperledger Fabric (HLF)

System chaincodes are special chaincodes running as part of peer process itself instead of user chaincodes(which usually run as separate docker containers).

These chaincodes are registered and deployed by the peer when peer starts up. There is no steps like regular user chaincode way of deployment (proposals submitted via SDK/CLI for install and other steps).

System chaincodes have more access to peer resources and allows us to have features which are difficult or not available in user chaincodes.

System chaincodes can be associated with peer via 2 ways: 1. Static 2. Dynamic using go plugin.

Some of the System Chaincodes are

  1. Query System Chaincode (QSCC) — Ledger and other fabric related queries
  2. Configuration System Chaincode (CSCC) — Regulate Access Control
  3. Lifecycle System Chaincode (LSCC) — chaincode lifecycle for 1.x fabric releases
  4. Validation System Chaincode (VSCC) — transaction validation including checking endorsement policy and read-write set versioning
  5. Endorsement System Chaincode (ESCC) — Runs in endorsing peers to cryptographically sign the transaction response
  6. _lifcycle — chaincode lifecycle in peers (install, approval, commit chaincode definition to channel)

These system chaincodes can be updated by the developers cautiously as they are fundamental for the proper functioning of the fabric network.

In this article, we will go through Query System Chaincode.

QSCC contains following functionality which we can utilize to get ledger data.

  1. GetChainInfo — returns BlockchainInfo which contains information about the blockchain ledger such as height, current block hash, and previous block hash.
  2. GetBlockByNumber — returns the block specified by block number
  3. GetBlockByHash — returns the block specified by block number
  4. GetTransactionByID — returns the transaction by transaction id
  5. GetBlockByTxID — returns theblock specified by transaction id

Query using QSCC

As QSCC is a chaincode, we can follow regular way of querying fabric ledger via evaluateTransaction method using SDK. Use ‘qscc’ as the chaincode name and use any of the function listed above.

In this example, you can see how to get the transaction detail using Transaction ID. Similar approach should be followed to get block data.

const { BlockDecoder } = require('fabric-common');.......
........ <Function call>
const channelName = "mychannel";
const fcn = "GetTransactionByID";
const txid = "<Transaction ID goes here>"
// Get the contract from the network.
const contract = network.getContract(chaincodeName);
let blockchainResponse;blockchainResponse = await contract.evaluateTransaction(fcn, channelName, txId);blockchainResponse = BlockDecoder.decodeTransaction(blockchainResponse);const writes = JSON.parse(JSON.stringify(blockchainResponse)).transactionEnvelope.payload.data.actions[0].payload.action.proposal_response_payload.extension.results.ns_rwset[1].rwset.writes;var records = []for (index in writes) {
const record = JSON.parse(Buffer.from(JSON.parse(JSON.stringify(writes[index].value.data))).toString())
records.push(record);
}
return records

That’s it in this article. I hope this article is useful.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shivaganesh

Blockchain Developer experienced in developing Supply chain, Pharma, Telecom domain solutions using Hyperledger Fabric, R3 Corda, Symbiont Assembly and DAML