Skip to content

Commit

Permalink
BE-754 Update block listener (#117)
Browse files Browse the repository at this point in the history
* BE-754 Update block listener to new syntax

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

* BE-754 Update test scenarios to get deterministic result

synchBlocks runs every minute for filling any blocks missed listening.
We need to guarantee ensure that some tests are run after the
synchBlocks has been executed at least once.

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

* BE-754 Remove use of fabric-client Channel class

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

* BE-754 Fix an code issue detected by SonarCloud Quality Gate

Deleted unused variable

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>
  • Loading branch information
nekia authored Jun 12, 2020
1 parent 158ec00 commit d00ee7c
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 304 deletions.
7 changes: 6 additions & 1 deletion app/Synchronizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ class Synchronizer {

this.platform.setPersistenceService();

this.platform.setBlocksSyncTime(syncconfig.sync.blocksSyncTime);
// For overriding sync interval(min) via environment variable(sec)
const syncIntervalSec = process.env.EXPLORER_SYNC_BLOCKSYNCTIME_SEC
? parseInt(process.env.EXPLORER_SYNC_BLOCKSYNCTIME_SEC, 10)
: parseInt(syncconfig.sync.blocksSyncTime, 10) * 60;
this.platform.setBlocksSyncTime(syncIntervalSec);
logger.info('initialize :', syncIntervalSec);

await this.platform.initialize(this.args);
}
Expand Down
121 changes: 22 additions & 99 deletions app/platform/fabric/FabricClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const Fabric_Client = require('fabric-client');
const { User } = require('fabric-common');
const path = require('path');
const includes = require('lodash/includes');

const ExplorerError = require('../../common/ExplorerError');
const FabricUtils = require('./utils/FabricUtils.js');
Expand Down Expand Up @@ -42,6 +43,7 @@ class FabricClient {
this.status = false;
this.tls = false;
this.asLocalhost = false;
this.channels = [];
}

/**
Expand Down Expand Up @@ -209,16 +211,6 @@ class FabricClient {
' \n',
e
);

/*
* Const url = 'grpc://localhost:7051';
* const newpeer = this.hfc_client.newPeer(url, {
* 'ssl-target-name-override': default_peer_name,
* name: default_peer_name
* });
* newchannel.addPeer(newpeer);
*
*/
}
}

Expand All @@ -242,26 +234,6 @@ class FabricClient {
* @memberof FabricClient
*/
async initializeNewChannel(channel_name) {
// If the new channel is not defined in configuration, then use default channel configuration as new channel configuration
if (!this.config.channels[channel_name]) {
this.hfc_client._network_config._network_config.channels[
channel_name
] = this.config.channels[this.defaultChannel.getName()];
}

/*
* Get channel, if the channel is not exist in the hfc client context,
* Then it will create new channel from the network configuration
*/
try {
// Enable discover
await this.initializeChannelFromDiscover(channel_name);
} catch (error) {
logger.error(
'Failed to initialize Channel From Discover, channel ',
channel_name
);
}
// Get genesis block for the channel
const block = await this.getGenesisBlock(channel_name);
logger.debug('Genesis Block for client [%s] >> %j', this.client_name, block);
Expand All @@ -271,6 +243,7 @@ class FabricClient {
);
// Setting channel_genesis_hash to map
this.setChannelGenHash(channel_name, channel_genesis_hash);
this.addChannel(channel_name);
logger.debug(
`Channel genesis hash for channel [${channel_name}] >> ${channel_genesis_hash}`
);
Expand All @@ -285,17 +258,14 @@ class FabricClient {
*/
async initializeChannelFromDiscover(channel_name) {
logger.debug('initializeChannelFromDiscover ', channel_name);
let channel = this.hfc_client.getChannel(channel_name, false);
if (!channel) {
if (!includes(this.getChannels(), channel_name)) {
await this.initializeNewChannel(channel_name);
channel = this.getChannel(channel_name);
}
if (channel) {
if (!this.tls) {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpc');
} else {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpcs');
}

if (!this.tls) {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpc');
} else {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpcs');
}

const discover_results = await this.fabricGateway.getDiscoveryResult(
Expand All @@ -306,65 +276,7 @@ class FabricClient {
discover_results
);

// Creating users for admin peers
if (discover_results) {
if (discover_results.msps) {
for (const msp_name in discover_results.msps) {
const msp = discover_results.msps[msp_name];

if (!channel._msp_manager.getMSP(msp.id)) {
const config = {
rootCerts: msp.rootCerts,
intermediateCerts: msp.intermediateCerts,
admins: msp.admins,
cryptoSuite: channel._clientContext._crytoSuite,
id: msp.id,
orgs: msp.orgs,
tls_root_certs: msp.tls_root_certs,
tls_intermediate_certs: msp.tls_intermediate_certs
};
channel._msp_manager.addMSP(config);
}
}
}
// Creating orderers
if (discover_results.orderers) {
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);
const discoveryProtocol = this.hfc_client.getConfigSetting(
'discovery-protocol'
);
const requesturl =
`${discoveryProtocol}://${endpoint.host}:` + endpoint.port;
logger.debug(
'initializeChannelFromDiscover.discoveryProtocol ',
discoveryProtocol,
' requesturl ',
requesturl
);

this.newOrderer(
channel,
requesturl,
msp_id,
endpoint.host,
discover_results.msps
);
logger.debug(
'Successfully created orderer [%s:%s] for client [%s]',
endpoint.host,
endpoint.port,
this.client_name
);
}
}
}

channel._discovery_results = discover_results;
return discover_results;
}
return discover_results;
}

/**
Expand Down Expand Up @@ -556,13 +468,24 @@ class FabricClient {
}

/**
*
*
*
* @returns
* @memberof FabricClient
*/
getChannels() {
return this.hfc_client._channels; // Return Map
return this.channels; // Return Array
}

/**
*
*
* @param {*} channelName
* @memberof FabricClient
*/
addChannel(channelName) {
this.channels.push(channelName);
}

/**
Expand Down
26 changes: 6 additions & 20 deletions app/platform/fabric/Platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class Platform {
/* eslint-disable */
const _self = this;
/* eslint-enable */
let clientstatus = true;

// Setting organization enrolment files
logger.debug('Setting admin organization enrolment files');
Expand Down Expand Up @@ -124,25 +123,12 @@ class Platform {
// Create client instance
logger.debug('Creating client [%s] >> ', client_name, client_configs);

let client;

if (clientstatus) {
logger.info('FabricUtils.createFabricClient ');
client = await FabricUtils.createFabricClient(
client_configs,
network_name,
client_name,
this.persistence
);
} else {
logger.info('FabricUtils.createDetachClient ');
client = await FabricUtils.createDetachClient(
client_configs,
network_name,
client_name,
this.persistence
);
}
const client = await FabricUtils.createFabricClient(
client_configs,
network_name,
client_name,
this.persistence
);
if (client) {
// Set client into clients map
const clientObj = { name: client_name, instance: client };
Expand Down
10 changes: 4 additions & 6 deletions app/platform/fabric/Proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ class Proxy {
logger.debug('getCurrentChannel: network_name', network_name);

const client = await this.platform.getClient(network_name);
const channel = client.getDefaultChannel();
const channel_genesis_hash = client.getChannelGenHash(channel.getName());
const channel_name = Object.keys(client.fabricGateway.config.channels)[0];
const channel_genesis_hash = client.getChannelGenHash(channel_name);
let respose;
if (channel_genesis_hash) {
respose = {
Expand All @@ -105,16 +105,14 @@ class Proxy {
*/
async getPeersStatus(network_name, channel_genesis_hash) {
const client = await this.platform.getClient(network_name);
const channel = client.getDefaultChannel();
const channel_name = client.getChannelNameByHash(channel_genesis_hash);
const nodes = await this.persistence
.getMetricService()
.getPeerList(network_name, channel_genesis_hash);
let discover_results;
if (client.status) {
try {
discover_results = await client.initializeChannelFromDiscover(
channel._name
);
discover_results = await client.initializeChannelFromDiscover(channel_name);
} catch (e) {
logger.debug('getPeersStatus >> ', e);
}
Expand Down
15 changes: 14 additions & 1 deletion app/platform/fabric/e2e-test/specs/apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ var _ = Describe("REST API Test Suite - Single profile", func() {
err := cmd.Start()
Expect(err).NotTo(HaveOccurred())
Eventually(isExplorerReady, 60, 5).Should(Equal(true))

time.Sleep(waitSyncInterval * time.Second)
})

It("get network list", func() {
Expand Down Expand Up @@ -341,6 +343,8 @@ var _ = Describe("REST API Test Suite - Multiple profile", func() {
err := cmd.Start()
Expect(err).NotTo(HaveOccurred())
Eventually(isExplorerReady, 60, 5).Should(Equal(true))

time.Sleep(waitSyncInterval * time.Second)
})

Context("/auth/networklist", func() {
Expand Down Expand Up @@ -404,6 +408,7 @@ var _ = Describe("REST API Test Suite - Multiple profile", func() {
Context("/api/channels/info", func() {

It("get channels info for org1", func() {

resp1 := restPost("/auth/login", map[string]interface{}{"user": "admin", "password": "adminpw", "network": "org1-network"}, &LoginResponse{})
result1 := resp1.Result().(*LoginResponse)
token := result1.Token
Expand All @@ -416,7 +421,7 @@ var _ = Describe("REST API Test Suite - Multiple profile", func() {
chList := result2.getChannelList()
Expect(chList).Should(ContainElements([]string{"commonchannel", "org1channel"}))
Expect(len(chList)).Should(Equal(2))

fmt.Fprintf(GinkgoWriter, "Info: result2 %+v\n", result2)
action := "invoke"
inputSpecPath = "apitest-input-multiprofile-invoke-org1.yml"
err := testclient.Testclient(action, inputSpecPath)
Expand All @@ -426,6 +431,7 @@ var _ = Describe("REST API Test Suite - Multiple profile", func() {

resp3 := restGetWithToken("/api/channels/info", &ChannelsInfoResp{}, token)
result3 := resp3.Result().(*ChannelsInfoResp)
fmt.Fprintf(GinkgoWriter, "Info: result3 %+v\n", result3)

compareChannelsInfoBlockCount(result2, result3, "commonchannel", 0)
compareChannelsInfoBlockCount(result2, result3, "org1channel", 1)
Expand All @@ -441,6 +447,7 @@ var _ = Describe("REST API Test Suite - Multiple profile", func() {

resp4 := restGetWithToken("/api/channels/info", &ChannelsInfoResp{}, token)
result4 := resp4.Result().(*ChannelsInfoResp)
fmt.Fprintf(GinkgoWriter, "Info: result4 %+v\n", result4)

compareChannelsInfoBlockCount(result3, result4, "commonchannel", 0)
compareChannelsInfoBlockCount(result3, result4, "org1channel", 0)
Expand All @@ -456,6 +463,7 @@ var _ = Describe("REST API Test Suite - Multiple profile", func() {

resp5 := restGetWithToken("/api/channels/info", &ChannelsInfoResp{}, token)
result5 := resp5.Result().(*ChannelsInfoResp)
fmt.Fprintf(GinkgoWriter, "Info: result5 %+v\n", result5)

compareChannelsInfoBlockCount(result4, result5, "commonchannel", 1)
compareChannelsInfoBlockCount(result4, result5, "org1channel", 0)
Expand All @@ -464,6 +472,7 @@ var _ = Describe("REST API Test Suite - Multiple profile", func() {
})

It("get channels info for org2", func() {

resp1 := restPost("/auth/login", map[string]interface{}{"user": "admin", "password": "adminpw", "network": "org2-network"}, &LoginResponse{})
result1 := resp1.Result().(*LoginResponse)
token := result1.Token
Expand All @@ -476,6 +485,7 @@ var _ = Describe("REST API Test Suite - Multiple profile", func() {
chList := result2.getChannelList()
Expect(chList).Should(ContainElements([]string{"commonchannel", "org2channel"}))
Expect(len(chList)).Should(Equal(2))
fmt.Fprintf(GinkgoWriter, "Info: result2 %+v\n", result2)

action := "invoke"
inputSpecPath = "apitest-input-multiprofile-invoke-org1.yml"
Expand All @@ -486,6 +496,7 @@ var _ = Describe("REST API Test Suite - Multiple profile", func() {

resp3 := restGetWithToken("/api/channels/info", &ChannelsInfoResp{}, token)
result3 := resp3.Result().(*ChannelsInfoResp)
fmt.Fprintf(GinkgoWriter, "Info: result3 %+v\n", result3)

compareChannelsInfoBlockCount(result2, result3, "commonchannel", 0)
compareChannelsInfoBlockCount(result2, result3, "org2channel", 0)
Expand All @@ -501,6 +512,7 @@ var _ = Describe("REST API Test Suite - Multiple profile", func() {

resp4 := restGetWithToken("/api/channels/info", &ChannelsInfoResp{}, token)
result4 := resp4.Result().(*ChannelsInfoResp)
fmt.Fprintf(GinkgoWriter, "Info: result4 %+v\n", result4)

compareChannelsInfoBlockCount(result3, result4, "commonchannel", 0)
compareChannelsInfoBlockCount(result3, result4, "org2channel", 1)
Expand All @@ -516,6 +528,7 @@ var _ = Describe("REST API Test Suite - Multiple profile", func() {

resp5 := restGetWithToken("/api/channels/info", &ChannelsInfoResp{}, token)
result5 := resp5.Result().(*ChannelsInfoResp)
fmt.Fprintf(GinkgoWriter, "Info: result5 %+v\n", result5)

compareChannelsInfoBlockCount(result4, result5, "commonchannel", 1)
compareChannelsInfoBlockCount(result4, result5, "org2channel", 0)
Expand Down
1 change: 1 addition & 0 deletions app/platform/fabric/e2e-test/specs/runexplorer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ echo "#### Started DB container"
rm -rf logs wallet

export LOG_LEVEL_CONSOLE=debug
export EXPLORER_SYNC_BLOCKSYNCTIME_SEC=5
./start.sh
echo "#### Starting Explorer process ..."

Expand Down
Loading

0 comments on commit d00ee7c

Please sign in to comment.