fabric-samples/balance-transfer/app/query.js
Yuki Kondo 948e2372b8 [FAB-5913]balance-transfer:Fix query response message
"queryChaincode" doesn’t output responses from all
peers in log message. It exits the loop processing after
outputting the first response.
We should print all responses from all peers. Also, we should
return a response (e.g. the response_payloads[0]) after the loop.

This patch fixes the problems above.

Change-Id: I92a0c5a663d6da0854d89bf76c5ba36da86754ef
Signed-off-by: Yuki Kondo <yuki.kondo@hal.hitachi.com>
2017-11-10 22:51:20 +00:00

234 lines
8.3 KiB
JavaScript

/**
* Copyright 2017 IBM All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var path = require('path');
var fs = require('fs');
var util = require('util');
var hfc = require('fabric-client');
var helper = require('./helper.js');
var logger = helper.getLogger('Query');
var queryChaincode = async function(peer, channelName, chaincodeName, args, fcn, username, org_name) {
try {
// first setup the client for this org
var client = await helper.getClientForOrg(org_name, username);
logger.debug('Successfully got the fabric client for the organization "%s"', org_name);
var channel = client.getChannel(channelName);
if(!channel) {
let message = util.format('Channel %s was not defined in the connection profile', channelName);
logger.error(message);
throw new Error(message);
}
// send query
var request = {
targets : [peer], //queryByChaincode allows for multiple targets
chaincodeId: chaincodeName,
fcn: fcn,
args: args
};
let response_payloads = await channel.queryByChaincode(request);
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[0].toString('utf8') +
' after the move';
} else {
logger.error('response_payloads is null');
return 'response_payloads is null';
}
} catch(error) {
logger.error('Failed to query due to error: ' + error.stack ? error.stack : error);
return error.toString();
}
};
var getBlockByNumber = async function(peer, channelName, blockNumber, username, org_name) {
try {
// first setup the client for this org
var client = await helper.getClientForOrg(org_name, username);
logger.debug('Successfully got the fabric client for the organization "%s"', org_name);
var channel = client.getChannel(channelName);
if(!channel) {
let message = util.format('Channel %s was not defined in the connection profile', channelName);
logger.error(message);
throw new Error(message);
}
let response_payload = await channel.queryBlock(parseInt(blockNumber, peer));
if (response_payload) {
logger.debug(response_payload);
return response_payload;
} else {
logger.error('response_payload is null');
return 'response_payload is null';
}
} catch(error) {
logger.error('Failed to query due to error: ' + error.stack ? error.stack : error);
return error.toString();
}
};
var getTransactionByID = async function(peer, channelName, trxnID, username, org_name) {
try {
// first setup the client for this org
var client = await helper.getClientForOrg(org_name, username);
logger.debug('Successfully got the fabric client for the organization "%s"', org_name);
var channel = client.getChannel(channelName);
if(!channel) {
let message = util.format('Channel %s was not defined in the connection profile', channelName);
logger.error(message);
throw new Error(message);
}
let response_payload = await channel.queryTransaction(trxnID, peer);
if (response_payload) {
logger.debug(response_payload);
return response_payload;
} else {
logger.error('response_payload is null');
return 'response_payload is null';
}
} catch(error) {
logger.error('Failed to query due to error: ' + error.stack ? error.stack : error);
return error.toString();
}
};
var getBlockByHash = async function(peer, channelName, hash, username, org_name) {
try {
// first setup the client for this org
var client = await helper.getClientForOrg(org_name, username);
logger.debug('Successfully got the fabric client for the organization "%s"', org_name);
var channel = client.getChannel(channelName);
if(!channel) {
let message = util.format('Channel %s was not defined in the connection profile', channelName);
logger.error(message);
throw new Error(message);
}
let response_payload = await channel.queryBlockByHash(Buffer.from(hash), peer);
if (response_payload) {
logger.debug(response_payload);
return response_payload;
} else {
logger.error('response_payload is null');
return 'response_payload is null';
}
} catch(error) {
logger.error('Failed to query due to error: ' + error.stack ? error.stack : error);
return error.toString();
}
};
var getChainInfo = async function(peer, channelName, username, org_name) {
try {
// first setup the client for this org
var client = await helper.getClientForOrg(org_name, username);
logger.debug('Successfully got the fabric client for the organization "%s"', org_name);
var channel = client.getChannel(channelName);
if(!channel) {
let message = util.format('Channel %s was not defined in the connection profile', channelName);
logger.error(message);
throw new Error(message);
}
let response_payload = await channel.queryInfo(peer);
if (response_payload) {
logger.debug(response_payload);
return response_payload;
} else {
logger.error('response_payload is null');
return 'response_payload is null';
}
} catch(error) {
logger.error('Failed to query due to error: ' + error.stack ? error.stack : error);
return error.toString();
}
};
//getInstalledChaincodes
var getInstalledChaincodes = async function(peer, channelName, type, username, org_name) {
try {
// first setup the client for this org
var client = await helper.getClientForOrg(org_name, username);
logger.debug('Successfully got the fabric client for the organization "%s"', org_name);
let response = null
if (type === 'installed') {
response = await client.queryInstalledChaincodes(peer, true); //use the admin identity
} else {
var channel = client.getChannel(channelName);
if(!channel) {
let message = util.format('Channel %s was not defined in the connection profile', channelName);
logger.error(message);
throw new Error(message);
}
response = await channel.queryInstantiatedChaincodes(peer, true); //use the admin identity
}
if (response) {
if (type === 'installed') {
logger.debug('<<< Installed Chaincodes >>>');
} else {
logger.debug('<<< Instantiated Chaincodes >>>');
}
var details = [];
for (let i = 0; i < response.chaincodes.length; i++) {
logger.debug('name: ' + response.chaincodes[i].name + ', version: ' +
response.chaincodes[i].version + ', path: ' + response.chaincodes[i].path
);
details.push('name: ' + response.chaincodes[i].name + ', version: ' +
response.chaincodes[i].version + ', path: ' + response.chaincodes[i].path
);
}
return details;
} else {
logger.error('response is null');
return 'response is null';
}
} catch(error) {
logger.error('Failed to query due to error: ' + error.stack ? error.stack : error);
return error.toString();
}
};
var getChannels = async function(peer, username, org_name) {
try {
// first setup the client for this org
var client = await helper.getClientForOrg(org_name, username);
logger.debug('Successfully got the fabric client for the organization "%s"', org_name);
let response = await client.queryChannels(peer);
if (response) {
logger.debug('<<< channels >>>');
var channelNames = [];
for (let i = 0; i < response.channels.length; i++) {
channelNames.push('channel id: ' + response.channels[i].channel_id);
}
logger.debug(channelNames);
return response;
} else {
logger.error('response_payloads is null');
return 'response_payloads is null';
}
} catch(error) {
logger.error('Failed to query due to error: ' + error.stack ? error.stack : error);
return error.toString();
}
};
exports.queryChaincode = queryChaincode;
exports.getBlockByNumber = getBlockByNumber;
exports.getTransactionByID = getTransactionByID;
exports.getBlockByHash = getBlockByHash;
exports.getChainInfo = getChainInfo;
exports.getInstalledChaincodes = getInstalledChaincodes;
exports.getChannels = getChannels;