Skip to content

Commit

Permalink
BE-814 Fix inconsistency of network key (#172)
Browse files Browse the repository at this point in the history
Unified to use only network ID (network Name is used only for showing
network name on Login view)

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>
  • Loading branch information
nekia authored Aug 24, 2020
1 parent d8a1d3e commit b020de2
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 297 deletions.
1 change: 1 addition & 0 deletions app/persistence/fabric/CRUDService.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class CRUDService {
);

if (isValidRow(c)) {
transaction.network_name = network_name;
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}'`
Expand Down
71 changes: 28 additions & 43 deletions app/platform/fabric/FabricClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

const path = require('path');
const includes = require('lodash/includes');
const ExplorerError = require('../../common/ExplorerError');
const FabricUtils = require('./utils/FabricUtils.js');
const FabricGateway = require('../../platform/fabric/gateway/FabricGateway');
const FabricConfig = require('../fabric/FabricConfig');
const helper = require('../../common/helper');

const logger = helper.getLogger('FabricClient');
Expand All @@ -22,44 +20,33 @@ const explorer_mess = require('../../common/ExplorerMessage').explorer;
class FabricClient {
/**
* Creates an instance of FabricClient.
* @param {*} client_name
* @param {FabricConfig} config
* @memberof FabricClient
*/
constructor(network_name, client_name) {
this.network_name = network_name;
this.client_name = client_name;
constructor(config) {
this.network_id = config.getNetworkId();
this.fabricGateway = null;
this.channelsGenHash = new Map();
this.client_config = null;
this.config = config;
this.status = false;
this.channels = [];
}

/**
*
*
* @param {*} client_config
* @param {*} persistence
* @memberof FabricClient
*/
async initialize(client_config, persistence) {
this.client_config = client_config;

async initialize(persistence) {
// Before initializing a channel

// Loading client from network configuration file
logger.debug(
'Client configuration [%s] ...',
this.client_name,
' this.client_config ',
this.client_config
);
logger.debug('Client configuration [%s] ...', this.config.getNetworkId());

const profileConnection = this.client_config.profile;
const configPath = path.resolve(__dirname, profileConnection);
try {
// Use Gateway to connect to fabric network
this.fabricGateway = new FabricGateway(configPath);
this.fabricGateway = new FabricGateway(this.config);
await this.fabricGateway.initialize();
} catch (error) {
// TODO in case of the failure, should terminate explorer?
Expand Down Expand Up @@ -90,7 +77,7 @@ class FabricClient {
}
} else if (persistence) {
logger.info('********* call to initializeDetachClient **********');
this.initializeDetachClient(this.client_config, persistence);
this.initializeDetachClient(persistence);
} else {
logger.error('Not found any channels');
}
Expand All @@ -99,27 +86,15 @@ class FabricClient {
/**
*
*
* @param {*} client_config
* @param {*} persistence
* @memberof FabricClient
*/
async initializeDetachClient(client_config, persistence) {
const name = client_config.name;
logger.debug(
'initializeDetachClient --> client_config ',
client_config,
' name ',
name
);
const profileConnection = client_config.profile;
const configPath = path.resolve(__dirname, profileConnection);
const fabricConfig = new FabricConfig();
fabricConfig.initialize(configPath);
const config = fabricConfig.getConfig();
this.userName = fabricConfig.getAdminUser();
const peers = fabricConfig.getPeersConfig();
async initializeDetachClient(persistence) {
logger.debug('initializeDetachClient', this.config.getNetworkId());
const network_config = this.config.getConfig();
const peers = this.config.getPeersConfig();

logger.info('initializeDetachClient, network config) ', config);
logger.info('initializeDetachClient, network config) ', network_config);
logger.info(
'************************************* initializeDetachClient *************************************************'
);
Expand All @@ -130,7 +105,7 @@ class FabricClient {
);
const channels = await persistence
.getCrudService()
.getChannelsInfo(this.network_name);
.getChannelsInfo(this.network_id);

if (channels.length === 0) {
throw new ExplorerError(explorer_mess.error.ERROR_2003);
Expand All @@ -140,7 +115,7 @@ class FabricClient {
this.setChannelGenHash(channel.channelname, channel.channel_genesis_hash);
const nodes = await persistence
.getMetricService()
.getPeerList(this.network_name, channel.channel_genesis_hash);
.getPeerList(this.network_id, channel.channel_genesis_hash);
for (const node of nodes) {
const peer_config = peers[node.server_hostname];
let pem;
Expand Down Expand Up @@ -170,7 +145,7 @@ class FabricClient {
async initializeNewChannel(channel_name) {
// Get genesis block for the channel
const block = await this.getGenesisBlock(channel_name);
logger.debug('Genesis Block for client [%s]', this.client_name);
logger.debug('Genesis Block for client [%s]', this.network_id);

const channel_genesis_hash = await FabricUtils.generateBlockHash(
block.header
Expand Down Expand Up @@ -284,8 +259,8 @@ class FabricClient {
* @returns
* @memberof FabricClient
*/
getClientName() {
return this.client_name;
getNetworkId() {
return this.network_id;
}

/**
Expand All @@ -312,6 +287,16 @@ class FabricClient {
getStatus() {
return this.status;
}

/**
*
*
* @returns
* @memberof FabricClient
*/
getNetworkConfig() {
return this.config.getConfig();
}
}

module.exports = FabricClient;
10 changes: 6 additions & 4 deletions app/platform/fabric/FabricConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ class FabricConfig {
* @memberof FabricConfig
*/

initialize(configPath) {
const configJson = fs.readFileSync(configPath, 'utf8');
initialize(network_id, network_config) {
this.network_id = network_id;
const profile_path = path.resolve(__dirname, network_config.profile);
const configJson = fs.readFileSync(profile_path, 'utf8');
this.config = JSON.parse(configJson);
}

Expand Down Expand Up @@ -175,8 +177,8 @@ class FabricConfig {
* @returns
* @memberof FabricConfig
*/
getNetworkName() {
return this.config.name;
getNetworkId() {
return this.network_id;
}

/**
Expand Down
77 changes: 33 additions & 44 deletions app/platform/fabric/Platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ class Platform {
);
await this.buildClients(network_configs);

if (
this.networks.size === 0 &&
this.networks.get(this.defaultNetwork).size === 0
) {
if (this.networks.size === 0) {
logger.error(
'************* There is no client found for Hyperledger fabric platform *************'
);
Expand All @@ -104,58 +101,49 @@ class Platform {
logger.debug('Setting admin organization enrolment files');
this.network_configs = network_configs;

for (const network_name in this.network_configs) {
// this.networks.set(network_name, new Map());
const client_configs = this.network_configs[network_name];
// Console.log('network_name ', network_name, ' client_configs ', client_configs)
for (const network_id in this.network_configs) {
const network_config = this.network_configs[network_id];
if (!this.defaultNetwork) {
this.defaultNetwork = network_name;
this.defaultNetwork = network_id;
}

/*
* Create fabric explorer client for each
* Each client is connected to only a single peer and monitor that particular peer only
*/
logger.info(
' client_configs.name ',
client_configs.name,
' client_configs.profile ',
client_configs.profile
' network_config.id ',
network_id,
' network_config.profile ',
network_config.profile
);
const client_name = client_configs.name;

// Create client instance
logger.debug('Creating client [%s] >> ', client_name, client_configs);
logger.debug('Creating network client [%s] >> ', network_id, network_config);

const signupResult = await this.registerAdmin(
client_configs.name,
client_configs.profile
);
const config = new FabricConfig();
config.initialize(network_id, network_config);

const signupResult = await this.registerAdmin(config);
if (!signupResult) {
logger.error(`Failed to register admin user : ${network_name}`);
logger.error(`Failed to register admin user : ${network_id}`);
continue;
}

const client = await FabricUtils.createFabricClient(
client_configs,
network_name,
client_name,
config,
this.persistence
);
if (client) {
// Set client into clients map
const clientObj = { name: client_name, instance: client };
this.networks.set(network_name, clientObj);
const clientObj = { name: network_config.name, instance: client };
this.networks.set(network_id, clientObj);
}
// }
}
}

async registerAdmin(network, network_profile_path) {
const configPath = path.resolve(__dirname, network_profile_path);
const config = new FabricConfig();
config.initialize(configPath);

async registerAdmin(config) {
if (!config.getEnableAuthentication()) {
logger.info('Disabled authentication');
return true;
Expand All @@ -168,13 +156,14 @@ class Platform {
return false;
}

const network_id = config.getNetworkId();
const reqUser = await User.createInstanceWithParam(
user,
password,
network,
network_id,
'admin'
).asJson();
if (await this.userService.isExist(user, network)) {
if (await this.userService.isExist(user, network_id)) {
logger.info('Already registered : admin');
return true;
}
Expand All @@ -194,17 +183,17 @@ class Platform {
*/
initializeListener(syncconfig) {
/* eslint-disable */
for (const [network_name, clientObj] of this.networks.entries()) {
const client_name = clientObj.name;
const client = clientObj.instance;
for (const [network_id, clientObj] of this.networks.entries()) {
const network_name = clientObj.name;
const network_client = clientObj.instance;
logger.info(
'initializeListener, client_name, client ',
client_name,
client.client_config
'initializeListener, network_id, network_client ',
network_id,
network_client.getNetworkConfig()
);
if (this.getClient(network_name).getStatus()) {
if (network_client.getStatus()) {
const explorerListener = new ExplorerListener(this, syncconfig);
explorerListener.initialize([network_name, client_name, '1']);
explorerListener.initialize([network_id, network_name, '1']);
explorerListener.send('Successfully send a message to child process');
this.explorerListeners.push(explorerListener);
}
Expand Down Expand Up @@ -243,13 +232,13 @@ class Platform {
/**
*
*
* @param {*} network_name
* @param {*} client_name
* @param {*} network_id
* @returns
* @memberof Platform
*/
getClient(network_name) {
const clientObj = this.networks.get(network_name || this.defaultNetwork);
getClient(network_id) {
logger.info(`getClient (id:${network_id})`);
const clientObj = this.networks.get(network_id || this.defaultNetwork);
return clientObj.instance;
}

Expand Down
Loading

0 comments on commit b020de2

Please sign in to comment.