Skip to content

Commit

Permalink
BE-758 Fix channel switch issue on dashboard (#127)
Browse files Browse the repository at this point in the history
Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>
  • Loading branch information
nekia authored Jun 27, 2020
1 parent f27d97c commit 3d8f088
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 238 deletions.
82 changes: 16 additions & 66 deletions app/persistence/fabric/CRUDService.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,14 @@ class CRUDService {

async saveBlock(network_name, block) {
const c = await this.sql
.getRowByPkOne(`select count(1) as c from blocks where blocknum='${
block.blocknum
}' and txcount='${block.txcount}'
and channel_genesis_hash='${
block.channel_genesis_hash
}' and network_name = '${network_name}' and prehash='${
block.prehash
}' and datahash='${block.datahash}' `);
.getRowByPkOne(`select count(1) as c from blocks where blocknum='${block.blocknum}' and txcount='${block.txcount}'
and channel_genesis_hash='${block.channel_genesis_hash}' and network_name = '${network_name}' and prehash='${block.prehash}' and datahash='${block.datahash}' `);

if (isValidRow(c)) {
block.network_name = network_name;
await this.sql.saveRow('blocks', block);
await this.sql.updateBySql(
`update channel set blocks =blocks+1 where channel_genesis_hash='${
block.channel_genesis_hash
}' and network_name = '${network_name}' `
`update channel set blocks =blocks+1 where channel_genesis_hash='${block.channel_genesis_hash}' and network_name = '${network_name}' `
);
return true;
}
Expand All @@ -240,26 +232,16 @@ class CRUDService {
*/
async saveTransaction(network_name, transaction) {
const c = await this.sql.getRowByPkOne(
`select count(1) as c from transactions where blockid='${
transaction.blockid
}' and txhash='${transaction.txhash}' and channel_genesis_hash='${
transaction.channel_genesis_hash
}' and network_name = '${network_name}' `
`select count(1) as c from transactions where blockid='${transaction.blockid}' and txhash='${transaction.txhash}' and channel_genesis_hash='${transaction.channel_genesis_hash}' and network_name = '${network_name}' `
);

if (isValidRow(c)) {
await this.sql.saveRow('transactions', transaction);
await this.sql.updateBySql(
`update chaincodes set txcount =txcount+1 where channel_genesis_hash='${
transaction.channel_genesis_hash
}' and network_name = '${network_name}' and name='${
transaction.chaincodename
}'`
`update chaincodes set txcount =txcount+1 where channel_genesis_hash='${transaction.channel_genesis_hash}' and network_name = '${network_name}' and name='${transaction.chaincodename}'`
);
await this.sql.updateBySql(
`update channel set trans =trans+1 where channel_genesis_hash='${
transaction.channel_genesis_hash
}' and network_name = '${network_name}' `
`update channel set trans =trans+1 where channel_genesis_hash='${transaction.channel_genesis_hash}' and network_name = '${network_name}' `
);
return true;
}
Expand Down Expand Up @@ -303,14 +285,8 @@ class CRUDService {
*/
async saveChaincode(network_name, chaincode) {
const c = await this.sql
.getRowByPkOne(`select count(1) as c from chaincodes where name='${
chaincode.name
}' and
channel_genesis_hash='${
chaincode.channel_genesis_hash
}' and network_name = '${network_name}' and version='${
chaincode.version
}' and path='${chaincode.path}'`);
.getRowByPkOne(`select count(1) as c from chaincodes where name='${chaincode.name}' and
channel_genesis_hash='${chaincode.channel_genesis_hash}' and network_name = '${network_name}' and version='${chaincode.version}' and path='${chaincode.path}'`);

if (isValidRow(c)) {
chaincode.network_name = network_name;
Expand Down Expand Up @@ -340,13 +316,7 @@ class CRUDService {
*/
async saveChaincodPeerRef(network_name, peers_ref_chaincode) {
const c = await this.sql.getRowByPkOne(
`select count(1) as c from peer_ref_chaincode prc where prc.peerid= '${
peers_ref_chaincode.peerid
}' and prc.chaincodeid='${
peers_ref_chaincode.chaincodeid
}' and cc_version='${peers_ref_chaincode.cc_version}' and channelid='${
peers_ref_chaincode.channelid
}' and network_name = '${network_name}' `
`select count(1) as c from peer_ref_chaincode prc where prc.peerid= '${peers_ref_chaincode.peerid}' and prc.chaincodeid='${peers_ref_chaincode.chaincodeid}' and cc_version='${peers_ref_chaincode.cc_version}' and channelid='${peers_ref_chaincode.channelid}' and network_name = '${network_name}' `
);

if (isValidRow(c)) {
Expand All @@ -363,11 +333,7 @@ class CRUDService {
*/
async saveChannel(network_name, channel) {
const c = await this.sql.getRowByPkOne(
`select count(1) as c from channel where name='${
channel.name
}' and channel_genesis_hash='${
channel.channel_genesis_hash
}' and network_name = '${network_name}' `
`select count(1) as c from channel where name='${channel.name}' and channel_genesis_hash='${channel.channel_genesis_hash}' and network_name = '${network_name}' `
);

if (isValidRow(c)) {
Expand All @@ -382,13 +348,7 @@ class CRUDService {
});
} else {
await this.sql.updateBySql(
`update channel set blocks='${channel.blocks}',trans='${
channel.trans
}',channel_hash='${channel.channel_hash}' where name='${
channel.name
}'and channel_genesis_hash='${
channel.channel_genesis_hash
}' and network_name = '${network_name}' `
`update channel set blocks='${channel.blocks}',trans='${channel.trans}',channel_hash='${channel.channel_hash}' where name='${channel.name}'and channel_genesis_hash='${channel.channel_genesis_hash}' and network_name = '${network_name}' `
);
}
}
Expand All @@ -401,11 +361,7 @@ class CRUDService {
*/
async savePeer(network_name, peer) {
const c = await this.sql.getRowByPkOne(
`select count(1) as c from peer where channel_genesis_hash='${
peer.channel_genesis_hash
}' and network_name = '${network_name}' and server_hostname='${
peer.server_hostname
}' `
`select count(1) as c from peer where channel_genesis_hash='${peer.channel_genesis_hash}' and network_name = '${network_name}' and server_hostname='${peer.server_hostname}' `
);

if (isValidRow(c)) {
Expand All @@ -422,11 +378,7 @@ class CRUDService {
*/
async savePeerChannelRef(network_name, peers_ref_Channel) {
const c = await this.sql.getRowByPkOne(
`select count(1) as c from peer_ref_channel prc where prc.peerid = '${
peers_ref_Channel.peerid
}' and network_name = '${network_name}' and prc.channelid='${
peers_ref_Channel.channelid
}' `
`select count(1) as c from peer_ref_channel prc where prc.peerid = '${peers_ref_Channel.peerid}' and network_name = '${network_name}' and prc.channelid='${peers_ref_Channel.channelid}' `
);

if (isValidRow(c)) {
Expand All @@ -442,10 +394,10 @@ class CRUDService {
* @returns
* @memberof CRUDService
*/
async getChannelsInfo(network_name, peerid) {
async getChannelsInfo(network_name) {
const channels = await this.sql
.getRowsBySQlNoCondition(` select c.id as id,c.name as channelName,c.blocks as blocks ,c.channel_genesis_hash as channel_genesis_hash,c.trans as transactions,c.createdt as createdat,c.channel_hash as channel_hash from channel c,
peer_ref_channel pc where c.channel_genesis_hash = pc.channelid and c.network_name = '${network_name}' and pc.peerid='${peerid}' group by c.id ,c.name ,c.blocks ,c.trans ,c.createdt ,c.channel_hash,c.channel_genesis_hash order by c.name `);
peer_ref_channel pc where c.channel_genesis_hash = pc.channelid and c.network_name = '${network_name}' group by c.id ,c.name ,c.blocks ,c.trans ,c.createdt ,c.channel_hash,c.channel_genesis_hash order by c.name `);

return channels;
}
Expand All @@ -459,9 +411,7 @@ class CRUDService {
*/
async saveOrderer(network_name, orderer) {
const c = await this.sql.getRowByPkOne(
`select count(1) as c from orderer where requests='${
orderer.requests
}' and network_name = '${network_name}' `
`select count(1) as c from orderer where requests='${orderer.requests}' and network_name = '${network_name}' `
);
if (isValidRow(c)) {
orderer.network_name = network_name;
Expand Down
4 changes: 2 additions & 2 deletions app/persistence/fabric/MetricService.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class MetricService {
const c1 = await this.sql
.getRowsBySQlNoCondition(`select channel.name as channelName,c.requests as requests,c.channel_genesis_hash as channel_genesis_hash ,
c.server_hostname as server_hostname, c.mspid as mspid, c.peer_type as peer_type from peer as c inner join channel on
c.channel_genesis_hash=channel.channel_genesis_hash where c.channel_genesis_hash='${channel_genesis_hash}' and c.network_name='${network_name}' `);
c.channel_genesis_hash=channel.channel_genesis_hash and c.network_name=channel.network_name where c.channel_genesis_hash='${channel_genesis_hash}' and c.network_name='${network_name}' `);
for (let i = 0, len = c1.length; i < len; i++) {
const item = c1[i];
peerArray.push({
Expand Down Expand Up @@ -128,7 +128,7 @@ class MetricService {
const txArray = [];
const c = await this.sql
.getRowsBySQlNoCondition(`select c.name as chaincodename,channel.name as channelname ,c.version as version,c.channel_genesis_hash
as channel_genesis_hash,c.path as path ,txcount as c from chaincodes as c inner join channel on c.channel_genesis_hash=channel.channel_genesis_hash where c.channel_genesis_hash='${channel_genesis_hash}' and c.network_name='${network_name}' `);
as channel_genesis_hash,c.path as path ,txcount as c from chaincodes as c inner join channel on c.channel_genesis_hash=channel.channel_genesis_hash and c.network_name=channel.network_name where c.channel_genesis_hash='${channel_genesis_hash}' and c.network_name='${network_name}' `);
if (c) {
c.forEach((item, index) => {
logger.debug(' item ------------> ', item);
Expand Down
116 changes: 2 additions & 114 deletions app/platform/fabric/FabricClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ class FabricClient {
this.client_name = client_name;
this.hfc_client = null;
this.fabricGateway = null;
this.defaultPeer = {};
this.defaultMspId = {};
this.defaultChannel = {};
this.defaultOrderer = null;
this.channelsGenHash = new Map();
this.client_config = null;
Expand Down Expand Up @@ -74,17 +72,9 @@ class FabricClient {
await this.fabricGateway.initialize();
this.hfc_client = Fabric_Client.loadFromConfig(this.fabricGateway.config);
/* eslint-enable */
const channel_name = this.fabricGateway.getDefaultChannelName();
this.defaultPeer = this.fabricGateway.getDefaultPeer();
this.defaultMspId = this.fabricGateway.getDefaultMspId();
this.defaultChannel = this.hfc_client.getChannel(channel_name);
this.tls = this.fabricGateway.getTls();
this.config = this.fabricGateway.getConfig();
logger.debug(
'Set client [%s] default channel as >> %s',
this.client_name,
this.defaultChannel.getName()
);
// Set discovery protocol
if (!this.tls) {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpc');
Expand All @@ -99,7 +89,7 @@ class FabricClient {
// Getting channels from queryChannels
let channels;
try {
logger.debug('this.defaultPeer ', this.defaultPeer);
// logger.debug('this.defaultPeer ', this.defaultPeer);
channels = await this.fabricGateway.queryChannels();
} catch (e) {
logger.error(e);
Expand Down Expand Up @@ -158,13 +148,9 @@ class FabricClient {
logger.info(
'************************************** initializeDetachClient ************************************************'
);
const defaultPeerConfig = fabricConfig.getDefaultPeerConfig();
const default_peer_name = defaultPeerConfig.name;
const channels = await persistence
.getCrudService()
.getChannelsInfo(this.network_name, default_peer_name);

const default_channel_name = fabricConfig.getDefaultChannel();
.getChannelsInfo(this.network_name);

if (channels.length === 0) {
throw new ExplorerError(explorer_mess.error.ERROR_2003);
Expand Down Expand Up @@ -201,29 +187,6 @@ class FabricClient {
logger.error(e);
}
}

try {
newchannel.getPeer(default_peer_name);
} catch (e) {
logger.error(
'Failed to connect to default peer: ',
default_peer_name,
' \n',
e
);
}
}

this.defaultChannel = this.hfc_client.getChannel(default_channel_name);
if (this.defaultChannel.getPeers().length > 0) {
this.defaultPeer = this.defaultChannel.getPeer(default_peer_name);
}

if (this.defaultChannel === undefined) {
throw new ExplorerError(explorer_mess.error.ERROR_2004);
}
if (this.defaultPeer === undefined) {
throw new ExplorerError(explorer_mess.error.ERROR_2005);
}
}

Expand Down Expand Up @@ -353,22 +316,6 @@ class FabricClient {
return newpeer;
}

/**
*
*
* @param {*} channel
* @returns
* @memberof FabricClient
*/
async getChannelDiscover(channel) {
const discover_request = {
target: this.defaultPeer,
config: true
};
const discover_results = await channel._discover(discover_request);
return discover_results;
}

/**
*
*
Expand Down Expand Up @@ -516,16 +463,6 @@ class FabricClient {
this.channelsGenHash.set(name, channel_genesis_hash);
}

/**
*
*
* @returns
* @memberof FabricClient
*/
getDefaultPeer() {
return this.defaultPeer;
}

/**
*
*
Expand All @@ -536,26 +473,6 @@ class FabricClient {
return this.client_name;
}

/**
*
*
* @returns
* @memberof FabricClient
*/
getDefaultChannel() {
return this.defaultChannel;
}

/**
*
*
* @param {*} defaultPeer
* @memberof FabricClient
*/
setDefaultPeer(defaultPeer) {
this.defaultPeer = defaultPeer;
}

/**
*
*
Expand Down Expand Up @@ -597,35 +514,6 @@ class FabricClient {
return this.hfc_client.getChannel(channel_name);
}

/**
*
*
* @param {*} channel_name
* @memberof FabricClient
*/
setDefaultChannel(channel_name) {
this.defaultChannel = this.hfc_client.getChannel(channel_name);
}

/**
*
*
* @param {*} new_channel_genesis_hash
* @returns
* @memberof FabricClient
*/
setDefaultChannelByHash(new_channel_genesis_hash) {
for (const [
channel_name,
channel_genesis_hash
] of this.channelsGenHash.entries()) {
if (new_channel_genesis_hash === channel_genesis_hash) {
this.defaultChannel = this.hfc_client.getChannel(channel_name);
return channel_genesis_hash;
}
}
}

/**
*
*
Expand Down
15 changes: 0 additions & 15 deletions app/platform/fabric/FabricConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,6 @@ class FabricConfig {
return defChannel;
}

/**
*
*
* @returns
* @memberof FabricConfig
*/
getDefaultPeerConfig() {
let defaultPeerConfig = [];
const peers = this.getPeersConfig();
if (peers) {
defaultPeerConfig = peers[0];
}
return defaultPeerConfig;
}

/**
*
*
Expand Down
Loading

0 comments on commit 3d8f088

Please sign in to comment.