Skip to content

Commit

Permalink
BE-748 Change discovery service to make use of DiscoveryService (#111)
Browse files Browse the repository at this point in the history
* BE-748 Update manner to retrieve discovery result

Using Discovery Service class

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>

* BE-748 Fix args to resolove tlsCACerts path

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>
  • Loading branch information
nekia authored Jun 4, 2020
1 parent 87bf02c commit 71026be
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 1,116 deletions.
78 changes: 6 additions & 72 deletions app/platform/fabric/FabricClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,6 @@ class FabricClient {
} else {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpcs');
}
this.asLocalhost =
String(
this.hfc_client.getConfigSetting('discovery-as-localhost', 'true')
) === 'true';
// Enable discover service
await this.defaultChannel.initialize({
discover: true,
target: this.defaultPeer,
asLocalhost: this.asLocalhost
});
} catch (error) {
// TODO in case of the failure, should terminate explorer?
logger.error(error);
Expand Down Expand Up @@ -126,41 +116,6 @@ class FabricClient {
logger.error('Failed to initialize new channel: ', channel.channel_id);
}
}

try {
// Load default channel network details from discovery
const result = await this.defaultChannel.getDiscoveryResults();
logger.debug(
'Channel Discovery, getDiscoveryResults returned result ',
result
);
} catch (e) {
logger.debug('Channel Discovery >> %s', e);
throw new ExplorerError(
explorer_mess.error.ERROR_2001,
this.defaultChannel.getName(),
this.client_name
);
}

/*
* Setting default orderer
* The new channel may not be in the configuration, let's use this.defaultChannel.getName()
*/
const defaultChannelName = this.defaultChannel.getName();
const channel = await this.hfc_client.getChannel(defaultChannelName);
const temp_orderers = await channel.getOrderers();
if (temp_orderers && temp_orderers.length > 0) {
this.defaultOrderer = temp_orderers[0];
} else {
logger.error(' No orderrers found ', temp_orderers);
throw new ExplorerError(explorer_mess.error.ERROR_2002);
}
logger.debug(
'Set client [%s] default orderer as >> %s',
this.client_name,
this.defaultOrderer.getName()
);
} else if (persistence) {
logger.info('********* call to initializeDetachClient **********');
this.initializeDetachClient(this.client_config, persistence);
Expand Down Expand Up @@ -317,14 +272,7 @@ class FabricClient {
// Setting channel_genesis_hash to map
this.setChannelGenHash(channel_name, channel_genesis_hash);
logger.debug(
'Channel genesis hash for channel [%s] >> %s',
channel_name,
channel_genesis_hash
);
logger.debug(
'Channel genesis hash for channel [%s] >> %s',
channel_name,
channel_genesis_hash
`Channel genesis hash for channel [${channel_name}] >> ${channel_genesis_hash}`
);
}

Expand All @@ -348,17 +296,13 @@ class FabricClient {
} else {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpcs');
}
await channel.initialize({
discover: true,
target: this.defaultPeer,
asLocalhost: this.asLocalhost
});
}

const discover_results = await this.getChannelDiscover(channel);
const discover_results = await this.fabricGateway.getDiscoveryResult(
channel_name
);
logger.debug(
'Discover results for client [%s] >> %j',
this.client_name,
`Discover results for channel [${channel_name}] >>`,
discover_results
);

Expand Down Expand Up @@ -388,7 +332,7 @@ class FabricClient {
for (const msp_id in discover_results.orderers) {
const endpoints = discover_results.orderers[msp_id].endpoints;
for (const endpoint of endpoints) {
logger.info(' FabricClient.discover_results endpoint ', endpoint);
logger.info('FabricClient.discover_results endpoint ', endpoint);
const discoveryProtocol = this.hfc_client.getConfigSetting(
'discovery-protocol'
);
Expand Down Expand Up @@ -673,16 +617,6 @@ class FabricClient {
return this.defaultChannel;
}

/**
*
*
* @returns
* @memberof FabricClient
*/
getDefaultOrderer() {
return this.defaultOrderer;
}

/**
*
*
Expand Down
63 changes: 62 additions & 1 deletion app/platform/fabric/gateway/FabricGateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
*/

const { Wallets, Gateway } = require('fabric-network');
const { BlockDecoder, Client } = require('fabric-common');
const {
Discoverer,
DiscoveryService,
Client,
BlockDecoder
} = require('fabric-common');
const fabprotos = require('fabric-protos');

const FabricCAServices = require('fabric-ca-client');
Expand Down Expand Up @@ -285,6 +290,62 @@ class FabricGateway {
return null;
}
}

getPeer_pem(pemPath) {
const data = fs.readFileSync(path.resolve(__dirname, '../../../..', pemPath));
const pem = Buffer.from(data).toString();
return pem;
}

async getDiscoveryResult(channelName) {
try {
const network = await this.gateway.getNetwork(channelName);
const channel = network.getChannel();
const ds = new DiscoveryService('be discovery service', channel);

const client = new Client('discovery client');
client.setTlsClientCertAndKey();

const mspID = this.config.client.organization;
const targets = [];
for (const peer of this.config.organizations[mspID].peers) {
const discoverer = new Discoverer(`be discoverer ${peer}`, client);
const url = this.config.peers[peer].url;
const pemPath = this.config.peers[peer].tlsCACerts.path;
let grpcOpt = {};
if ('grpcOptions' in this.config.peers[peer]) {
grpcOpt = this.config.peers[peer].grpcOptions;
}
const peer_endpoint = client.newEndpoint(
Object.assign(grpcOpt, {
url: url,
pem: this.getPeer_pem(pemPath)
})
);
await discoverer.connect(peer_endpoint);
targets.push(discoverer);
}

const idx = this.gateway.identityContext;
// do the three steps
ds.build(idx);
ds.sign(idx);
await ds.send({
asLocalhost: true,
refreshAge: 15000,
targets: targets
});

const result = await ds.getDiscoveryResults(true);
return result;
} catch (error) {
logger.error(
`Failed to get discovery result from channel ${channelName} : `,
error
);
}
return null;
}
}

module.exports = FabricGateway;
6 changes: 2 additions & 4 deletions app/platform/fabric/sync/SyncPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ class SyncPlatform {
this.client_name
);

// Setting the block synch interval time
await this.setBlocksSyncTime(all_config);

logger.debug('Blocks synch interval time >> %s', this.blocksSyncTime);

// Update the discovery-cache-life as block synch interval time in global config
global.hfc.config.set('discovery-cache-life', this.blocksSyncTime);
global.hfc.config.set('initialize-with-discovery', true);
// global.hfc.config.set('initialize-with-discovery', true);

this.client_configs = network_configs[this.network_name];

Expand Down
3 changes: 3 additions & 0 deletions app/platform/fabric/sync/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ class SyncServices {
' channel_name ',
channel_name
);

const block = await client.getGenesisBlock(channel_name);
const channel_genesis_hash = await FabricUtils.generateBlockHash(
block.header
);
client.setChannelGenHash(channel_name, channel_genesis_hash);

const res = await this.insertNewChannel(
client,
channel,
Expand Down
Loading

0 comments on commit 71026be

Please sign in to comment.