balance transfer程式碼解析及api深度追蹤(七)查詢交易
一程式碼解析
var queryChaincode = function(peer, channelName, chaincodeName, args, fcn, username, org) {
var channel = helper.getChannelForOrg(org);
var client = helper.getClientForOrg(org);
var target = buildTarget(peer, org);
//1 獲取 jim 使用者( 內部會設定為上下文環境)
return helper.getRegisteredUsers(username, org).then((user) => {
tx_id = client.newTransactionID();
//2 封裝查詢請求
var request = {
chaincodeId: chaincodeName,
txId: tx_id,
fcn: fcn,
args: args
};
//3 傳送請求
return channel.queryByChaincode(request, target);(1)
}, (err) => {
-
@typedef {Object} ChaincodeQueryRequest * @property {Peer[]} targets - Optional. The peers that will receive this request, * when not provided the list of peers added to this channel object will be used. * @property {string} chaincodeId - Required. The id of the chaincode to process the transaction proposal * @property {map} transientMap - Optional. <string, byte[]> map that can be used by the chaincode but not * saved in the ledger, such as cryptographic information for encryption * @property {string} fcn - Optional. The function name to be returned when calling
stub.GetFunctionAndParameters()
* channel.queryByChaincode(request) * .then((response_payloads) => { * for(let i = 0; i < response_payloads.length; i++) { * console.log(util.format('Query result from peer [%s]: %s', i, response_payloads[i].toString('utf8'))); * } * }); */ queryByChaincode(request, useAdmin) { logger.debug('queryByChaincode - start'); if (!request) { throw new Error('Missing request object for this queryByChaincode call.'); } var targets = this._getTargets(request.targets, Constants.NetworkConfig.CHAINCODE_QUERY_ROLE); var signer = this._clientContext._getSigningIdentity(useAdmin); var txId = new TransactionID(signer, useAdmin); // make a new request object so we can add in the txId and not change the user's var trans_request = { targets: targets, chaincodeId: request.chaincodeId, fcn: request.fcn, args: request.args, transientMap: request.transientMap, txId: txId, signer: signer }; return this.sendTransactionProposal(trans_request) .then( function (results) { var responses = results[0]; // var proposal = results[1]; logger.debug('queryByChaincode - results received'); if (responses && Array.isArray(responses)) { let results = []; for (let i = 0; i < responses.length; i++) { let response = responses[i]; if (response instanceof Error) { results.push(response); } else if (response.response && response.response.payload) { results.push(response.response.payload); } else { logger.error('queryByChaincode - unknown or missing results in query ::' + results); results.push(new Error(response)); } } return Promise.resolve(results); } return Promise.reject(new Error('Payload results are missing from the chaincode query')); } ).catch( function (err) { logger.error('Failed Query by chaincode. Error: %s', err.stack ? err.stack : err); return Promise.reject(err); } ); }