From 44e84583f7fa2a35ca8f88e0dcba3cfae47754bb Mon Sep 17 00:00:00 2001 From: "nkl199@yahoo.co.uk" Date: Wed, 3 Jun 2020 14:24:35 +0100 Subject: [PATCH] modify fabric config file for gateway Signed-off-by: nkl199@yahoo.co.uk --- package.json | 2 +- .../caliper-core/lib/common/config/Config.js | 4 ++-- .../lib/common/config/default.yaml | 5 ++-- .../adaptor-versions/v1/fabric-gateway-v1.js | 24 +++++-------------- .../lib/adaptor-versions/v1/fabric-v1.js | 22 ++++------------- .../adaptor-versions/v2/fabric-gateway-v2.js | 24 +++++-------------- .../lib/adaptor-versions/v2/fabric-v2.js | 22 ++++------------- packages/caliper-fabric/lib/fabric.js | 11 +++++++-- .../fabric_tests/run.sh | 4 ++-- 9 files changed, 39 insertions(+), 79 deletions(-) diff --git a/package.json b/package.json index 62abc9a2c9..bb3ebcc497 100644 --- a/package.json +++ b/package.json @@ -67,4 +67,4 @@ } }, "license": "Apache-2.0" -} \ No newline at end of file +} diff --git a/packages/caliper-core/lib/common/config/Config.js b/packages/caliper-core/lib/common/config/Config.js index dc3ce7adda..20efe25bf5 100644 --- a/packages/caliper-core/lib/common/config/Config.js +++ b/packages/caliper-core/lib/common/config/Config.js @@ -132,10 +132,10 @@ const keys = { SkipCreateChannelPrefix: 'caliper-fabric-skipcreatechannel-', Gateway: { Discovery: 'caliper-fabric-gateway-discovery', + Enabled: 'caliper-fabric-gateway-enabled', EventStrategy: 'caliper-fabric-gateway-eventstrategy', - GatewayLocalHost: 'caliper-fabric-gateway-gatewaylocalhost', + LocalHost: 'caliper-fabric-gateway-localhost', QueryStrategy: 'caliper-fabric-gateway-querystrategy', - UseGateway: 'caliper-fabric-gateway-usegateway', } } }; diff --git a/packages/caliper-core/lib/common/config/default.yaml b/packages/caliper-core/lib/common/config/default.yaml index 1b66054677..49510fb068 100644 --- a/packages/caliper-core/lib/common/config/default.yaml +++ b/packages/caliper-core/lib/common/config/default.yaml @@ -187,9 +187,10 @@ caliper: countqueryasload: true # Settings for the Fabric Gateway API gateway: - usegateway: false + # Indicates whether to use the Fabric Gateway API + enabled: false # Indicates whether to use the localhost default within the Fabric Gateway API - gatewaylocalhost: true + localhost: true # Indicates whether to use the Fabric discovery mechanism (via Gateway API) discovery: false # Which event strategy to use diff --git a/packages/caliper-fabric/lib/adaptor-versions/v1/fabric-gateway-v1.js b/packages/caliper-fabric/lib/adaptor-versions/v1/fabric-gateway-v1.js index 9f96781e09..9dad9a21a7 100644 --- a/packages/caliper-fabric/lib/adaptor-versions/v1/fabric-gateway-v1.js +++ b/packages/caliper-fabric/lib/adaptor-versions/v1/fabric-gateway-v1.js @@ -21,7 +21,6 @@ const { BlockchainInterface, CaliperUtils, TxStatus, ConfigUtil } = require('@hy const logger = CaliperUtils.getLogger('adapters/fabric'); const FabricNetwork = require('../../fabricNetwork.js'); -const ConfigValidator = require('../../configValidator.js'); const fs = require('fs'); const semver = require('semver'); @@ -132,26 +131,18 @@ const QueryStrategies = { class Fabric extends BlockchainInterface { /** * Initializes the Fabric adapter. - * @param {string|object} networkConfig The relative or absolute file path, or the object itself of the Common Connection Profile settings. + * @param {object} networkObject The parsed network configuration. * @param {string} workspace_root The absolute path to the root location for the application configuration files. * @param {number} clientIndex the client index */ - constructor(networkConfig, workspace_root, clientIndex) { - super(networkConfig); + constructor(networkObject, workspace_root, clientIndex) { + super(networkObject); this.bcType = 'fabric'; this.workspaceRoot = workspace_root; this.version = require('fabric-client/package').version; - this.network = undefined; - if (typeof networkConfig === 'string') { - const configPath = CaliperUtils.resolvePath(networkConfig, workspace_root); - this.network = CaliperUtils.parseYaml(configPath); - } else if (typeof networkConfig === 'object' && networkConfig !== null) { - // clone the object to prevent modification by other objects - this.network = CaliperUtils.parseYamlString(CaliperUtils.stringifyYaml(networkConfig)); - } else { - throw new Error('[FabricNetwork.constructor] Parameter \'networkConfig\' is neither a file path nor an object'); - } + // clone the object to prevent modification by other objects + this.network = CaliperUtils.parseYamlString(CaliperUtils.stringifyYaml(networkObject)); this.clientProfiles = new Map(); this.adminProfiles = new Map(); @@ -179,14 +170,11 @@ class Fabric extends BlockchainInterface { this.configCountQueryAsLoad = ConfigUtil.get(ConfigUtil.keys.Fabric.CountQueryAsLoad, true); // Gateway adaptor - this.configLocalHost = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.GatewayLocalHost, true); + this.configLocalHost = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.LocalHost, true); this.configDiscovery = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.Discovery, false); this.eventStrategy = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.EventStrategy, 'msp_all'); this.queryStrategy = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.QueryStrategy, 'msp_single'); - ConfigValidator.validateNetwork(this.network, CaliperUtils.getFlowOptions(), - this.configDiscovery, true); - this.networkUtil = new FabricNetwork(this.network, workspace_root); this.fileWalletPath = this.networkUtil.getFileWalletPath(); this.defaultInvoker = Array.from(this.networkUtil.getClients())[0]; diff --git a/packages/caliper-fabric/lib/adaptor-versions/v1/fabric-v1.js b/packages/caliper-fabric/lib/adaptor-versions/v1/fabric-v1.js index d82ff573e1..9c1cd4b9fc 100644 --- a/packages/caliper-fabric/lib/adaptor-versions/v1/fabric-v1.js +++ b/packages/caliper-fabric/lib/adaptor-versions/v1/fabric-v1.js @@ -20,7 +20,6 @@ const {BlockchainInterface, CaliperUtils, TxStatus, Version, ConfigUtil} = requi const logger = CaliperUtils.getLogger('adapters/fabric'); const FabricNetwork = require('../../fabricNetwork.js'); -const ConfigValidator = require('../../configValidator.js'); const fs = require('fs'); @@ -126,26 +125,18 @@ const fs = require('fs'); class Fabric extends BlockchainInterface { /** * Initializes the Fabric adapter. - * @param {string|object} networkConfig The relative or absolute file path, or the object itself of the Common Connection Profile settings. + * @param {object} networkObject The parsed network configuration. * @param {string} workspace_root The absolute path to the root location for the application configuration files. * @param {number} clientIndex the client index */ - constructor(networkConfig, workspace_root, clientIndex) { - super(networkConfig); + constructor(networkObject, workspace_root, clientIndex) { + super(networkObject); this.bcType = 'fabric'; this.workspaceRoot = workspace_root; this.version = new Version(require('fabric-client/package').version); - this.network = undefined; - if (typeof networkConfig === 'string') { - const configPath = CaliperUtils.resolvePath(networkConfig, workspace_root); - this.network = CaliperUtils.parseYaml(configPath); - } else if (typeof networkConfig === 'object' && networkConfig !== null) { - // clone the object to prevent modification by other objects - this.network = CaliperUtils.parseYamlString(CaliperUtils.stringifyYaml(networkConfig)); - } else { - throw new Error('[FabricNetwork.constructor] Parameter \'networkConfig\' is neither a file path nor an object'); - } + // clone the object to prevent modification by other objects + this.network = CaliperUtils.parseYamlString(CaliperUtils.stringifyYaml(networkObject)); this.clientProfiles = new Map(); this.adminProfiles = new Map(); @@ -174,9 +165,6 @@ class Fabric extends BlockchainInterface { this.configClientBasedLoadBalancing = ConfigUtil.get(ConfigUtil.keys.Fabric.LoadBalancing, 'client') === 'client'; this.configCountQueryAsLoad = ConfigUtil.get(ConfigUtil.keys.Fabric.CountQueryAsLoad, true); - ConfigValidator.validateNetwork(this.network, CaliperUtils.getFlowOptions(), - this.configDiscovery, false); - this.networkUtil = new FabricNetwork(this.network, workspace_root); this.defaultInvoker = Array.from(this.networkUtil.getClients())[0]; diff --git a/packages/caliper-fabric/lib/adaptor-versions/v2/fabric-gateway-v2.js b/packages/caliper-fabric/lib/adaptor-versions/v2/fabric-gateway-v2.js index 0efbc0efb7..694f7b773f 100644 --- a/packages/caliper-fabric/lib/adaptor-versions/v2/fabric-gateway-v2.js +++ b/packages/caliper-fabric/lib/adaptor-versions/v2/fabric-gateway-v2.js @@ -18,7 +18,6 @@ const { DefaultEventHandlerStrategies, DefaultQueryHandlerStrategies, Gateway, W const { BlockchainInterface, CaliperUtils, TxStatus, Version, ConfigUtil } = require('@hyperledger/caliper-core'); const FabricNetwork = require('../../fabricNetwork.js'); -const ConfigValidator = require('../../configValidator.js'); const RegistrarHelper = require('./registrarHelper'); const logger = CaliperUtils.getLogger('adapters/fabric'); @@ -115,26 +114,18 @@ const QueryStrategies = { class Fabric extends BlockchainInterface { /** * Initializes the Fabric adapter. - * @param {string|object} networkConfig The relative or absolute file path, or the object itself of the Common Connection Profile settings. + * @param {object} networkObject The parsed network configuration. * @param {string} workspace_root The absolute path to the root location for the application configuration files. * @param {number} clientIndex the client index */ - constructor(networkConfig, workspace_root, clientIndex) { - super(networkConfig); + constructor(networkObject, workspace_root, clientIndex) { + super(networkObject); this.bcType = 'fabric'; this.workspaceRoot = workspace_root; this.version = new Version(require('fabric-network/package').version); - this.network = undefined; - if (typeof networkConfig === 'string') { - const configPath = CaliperUtils.resolvePath(networkConfig, workspace_root); - this.network = CaliperUtils.parseYaml(configPath); - } else if (typeof networkConfig === 'object' && networkConfig !== null) { - // clone the object to prevent modification by other objects - this.network = CaliperUtils.parseYamlString(CaliperUtils.stringifyYaml(networkConfig)); - } else { - throw new Error('[FabricNetwork.constructor] Parameter \'networkConfig\' is neither a file path nor an object'); - } + // clone the object to prevent modification by other objects + this.network = CaliperUtils.parseYamlString(CaliperUtils.stringifyYaml(networkObject)); this.clientIndex = clientIndex; this.txIndex = -1; @@ -152,13 +143,10 @@ class Fabric extends BlockchainInterface { this.configCountQueryAsLoad = ConfigUtil.get(ConfigUtil.keys.Fabric.CountQueryAsLoad, true); // Gateway adaptor - this.configLocalHost = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.GatewayLocalHost, true); + this.configLocalHost = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.LocalHost, true); this.configDiscovery = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.Discovery, false); this.eventStrategy = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.EventStrategy, 'msp_all'); this.queryStrategy = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.QueryStrategy, 'msp_single'); - - // validate the network - ConfigValidator.validateNetwork(this.network, CaliperUtils.getFlowOptions(), this.configDiscovery, true); } //////////////////////////////// diff --git a/packages/caliper-fabric/lib/adaptor-versions/v2/fabric-v2.js b/packages/caliper-fabric/lib/adaptor-versions/v2/fabric-v2.js index 69e865041d..bf5aa01b06 100644 --- a/packages/caliper-fabric/lib/adaptor-versions/v2/fabric-v2.js +++ b/packages/caliper-fabric/lib/adaptor-versions/v2/fabric-v2.js @@ -20,7 +20,6 @@ const {BlockchainInterface, CaliperUtils, TxStatus, Version, ConfigUtil} = requi const logger = CaliperUtils.getLogger('adapters/fabric'); const FabricNetwork = require('../../fabricNetwork.js'); -const ConfigValidator = require('../../configValidator.js'); const fs = require('fs'); @@ -126,26 +125,18 @@ const fs = require('fs'); class Fabric extends BlockchainInterface { /** * Initializes the Fabric adapter. - * @param {string|object} networkConfig The relative or absolute file path, or the object itself of the Common Connection Profile settings. + * @param {object} networkObject The parsed network configuration. * @param {string} workspace_root The absolute path to the root location for the application configuration files. * @param {number} clientIndex the client index */ - constructor(networkConfig, workspace_root, clientIndex) { - super(networkConfig); + constructor(networkObject, workspace_root, clientIndex) { + super(networkObject); this.bcType = 'fabric'; this.workspaceRoot = workspace_root; this.version = new Version(require('fabric-client/package').version); - this.network = undefined; - if (typeof networkConfig === 'string') { - const configPath = CaliperUtils.resolvePath(networkConfig, workspace_root); - this.network = CaliperUtils.parseYaml(configPath); - } else if (typeof networkConfig === 'object' && networkConfig !== null) { - // clone the object to prevent modification by other objects - this.network = CaliperUtils.parseYamlString(CaliperUtils.stringifyYaml(networkConfig)); - } else { - throw new Error('[FabricNetwork.constructor] Parameter \'networkConfig\' is neither a file path nor an object'); - } + // clone the object to prevent modification by other objects + this.network = CaliperUtils.parseYamlString(CaliperUtils.stringifyYaml(networkObject)); this.clientProfiles = new Map(); this.adminProfiles = new Map(); @@ -174,9 +165,6 @@ class Fabric extends BlockchainInterface { this.configClientBasedLoadBalancing = ConfigUtil.get(ConfigUtil.keys.Fabric.LoadBalancing, 'client') === 'client'; this.configCountQueryAsLoad = ConfigUtil.get(ConfigUtil.keys.Fabric.CountQueryAsLoad, true); - ConfigValidator.validateNetwork(this.network, CaliperUtils.getFlowOptions(), - this.configDiscovery, false); - this.networkUtil = new FabricNetwork(this.network, workspace_root); this.defaultInvoker = Array.from(this.networkUtil.getClients())[0]; diff --git a/packages/caliper-fabric/lib/fabric.js b/packages/caliper-fabric/lib/fabric.js index 0e44fe725a..7d2c4d11e3 100644 --- a/packages/caliper-fabric/lib/fabric.js +++ b/packages/caliper-fabric/lib/fabric.js @@ -15,6 +15,7 @@ 'use strict'; const { BlockchainInterface, CaliperUtils, ConfigUtil } = require('@hyperledger/caliper-core'); +const ConfigValidator = require('./configValidator.js'); const Logger = CaliperUtils.getLogger('adapters/fabric'); const semver = require('semver'); @@ -42,7 +43,8 @@ const Fabric = class extends BlockchainInterface { throw new Error(msg); } - const useGateway = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.UseGateway, false); + const useGateway = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.Enabled, false); + const useDiscovery = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.Discovery, false); Logger.info(`Initializing ${useGateway ? 'gateway' : 'standard' } adaptor compatible with installed SDK: ${version}`); @@ -72,8 +74,13 @@ const Fabric = class extends BlockchainInterface { const networkConfig = CaliperUtils.resolvePath(ConfigUtil.get(ConfigUtil.keys.NetworkConfig)); const workspaceRoot = path.resolve(ConfigUtil.get(ConfigUtil.keys.Workspace)); + // validate the passed network file before use in underlying adaptor(s) + const configPath = CaliperUtils.resolvePath(networkConfig, workspaceRoot); + const networkObject = CaliperUtils.parseYaml(configPath); + ConfigValidator.validateNetwork(networkObject, CaliperUtils.getFlowOptions(), useDiscovery, useGateway); + const Fabric = require(modulePath); - this.fabric = new Fabric(networkConfig, workspaceRoot, workerIndex); + this.fabric = new Fabric(networkObject, workspaceRoot, workerIndex); } /** diff --git a/packages/caliper-tests-integration/fabric_tests/run.sh b/packages/caliper-tests-integration/fabric_tests/run.sh index bf901dc19a..7e1af52a9e 100755 --- a/packages/caliper-tests-integration/fabric_tests/run.sh +++ b/packages/caliper-tests-integration/fabric_tests/run.sh @@ -87,7 +87,7 @@ if [[ ${rc} != 0 ]]; then fi # PHASE 5: testing through the gateway API -${CALL_METHOD} launch master --caliper-workspace phase5 --caliper-flow-only-test --caliper-fabric-gateway-usegateway +${CALL_METHOD} launch master --caliper-workspace phase5 --caliper-flow-only-test --caliper-fabric-gateway-enabled rc=$? if [[ ${rc} != 0 ]]; then echo "Failed CI step 6"; @@ -107,7 +107,7 @@ if [[ "${BIND_IN_PACKAGE_DIR}" = "true" ]]; then fi # PHASE 6: testing through the gateway API (v2 SDK) -${CALL_METHOD} launch master --caliper-workspace phase5 --caliper-flow-only-test --caliper-fabric-gateway-usegateway +${CALL_METHOD} launch master --caliper-workspace phase5 --caliper-flow-only-test --caliper-fabric-gateway-enabled rc=$? if [[ ${rc} != 0 ]]; then echo "Failed CI step 7";