Querying the chaincode

A chaincode query is somewhat simpler to implement as it involves the entire network, but simply requires communication from client to peer.

Client and channel handles should be created as in the previous stages, but this time, we will select just one or more peers from the caller's (or client's) organization to associate with the channel object. Then, we must create a query request (identical to an invocation proposal request) and submit it to the selected peers:

var request = {
chaincodeId : Constants.CHAINCODE_ID,
fcn: funcName,
args: argList
};
channel.queryByChaincode(request);

The responses to the query can be collected and compared before being returned to the caller. The complete implementation can be found in the queryChaincode function in query-chaincode.js. We test this function by running a getTradeStatus chaincode query in our createTradeApp.js script:

var queryCC = require('./query-chaincode.js'),
queryCC.queryChaincode(Constants.EXPORTER_ORG, Constants.CHAINCODE_VERSION, 'getTradeStatus', ['2ks89j9'], 'Exporter'),

As with an invocation, we specify a user ID (‘Exporter’) and organization: here we want a member of the exporter’s organization to check the status of a trade request.

Since a query is local to the client and its associated peers, the response is returned immediately to the client and does not have to be subscribed to (as in the case of invocation).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.168.214