1. 程式人生 > >balance transfer程式碼解析及api深度追蹤(七)查詢交易

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) => {

logger.info(‘Failed to get submitter ‘’+username+’’’); return ‘Failed to get submitter ‘’+username+’’. Error: ’ + err.stack ? err.stack : err; }).then((response_payloads) => { //4 處理結果 if (response_payloads) { for (let i = 0; i < response_payloads.length; i++) { logger.info(args[0]+’ now has ’ + response_payloads[i].toString(‘utf8’) + ’ after the move’); return args[0]+’ now has ’ + response_payloads[i].toString(‘utf8’) + ’ after the move’; } } else { logger.error(‘response_payloads is null’); return ‘response_payloads is null’; } }, (err) => { logger.error('Failed to send query due to error: ’ + err.stack ? err.stack : err); return 'Failed to send query due to error: ’ + err.stack ? err.stack : err; }).catch((err) => { logger.error(‘Failed to end to end test with error:’ + err.stack ? err.stack : err); return ‘Failed to end to end test with error:’ + err.stack ? err.stack : err; }); }; 二api深度追蹤 (1)

  • @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()

    * in the target chaincode. Default is ‘invoke’ * @property {string[]} args - An array of string arguments specific to the chaincode’s ‘Invoke’ method / /* * Sends a proposal to one or more endorsing peers that will be handled by the chaincode. * In fabric v1.0, there is no difference in how the endorsing peers process a request * to invoke a chaincode for transaction vs. to invoke a chaincode for query. All requests * will be presented to the target chaincode’s ‘Invoke’ method which must be implemented to * understand from the arguments that this is a query request. The chaincode must also return * results in the byte array format and the caller will have to be able to decode * these results. * * @param {ChaincodeQueryRequest} request * @returns {Promise} A Promise for an array of byte array results returned from the chaincode * on all Endorsing Peers * @example * Get the list of query results returned by the chaincode

      * 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);
     			}
     		);
     }