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);
     			}
     		);
     }
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值