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
- Query System Chaincode (QSCC) — Ledger and other fabric related queries
- Configuration System Chaincode (CSCC) — Regulate Access Control
- Lifecycle System Chaincode (LSCC) — chaincode lifecycle for 1.x fabric releases
- Validation System Chaincode (VSCC) — transaction validation including checking endorsement policy and read-write set versioning
- Endorsement System Chaincode (ESCC) — Runs in endorsing peers to cryptographically sign the transaction response
- _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.
- GetChainInfo — returns BlockchainInfo which contains information about the blockchain ledger such as height, current block hash, and previous block hash.
- GetBlockByNumber — returns the block specified by block number
- GetBlockByHash — returns the block specified by block number
- GetTransactionByID — returns the transaction by transaction id
- 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.