Skip to content

Commit

Permalink
Remove the legacy fabric connectors (#1235)
Browse files Browse the repository at this point in the history
This is mainly a deletion exercise with the following code changes
1. simplication of which connector to load
2. only create a connector instance if required for example
flow-only-start and flow-only-end don't need to have a connector created

Signed-off-by: D <[email protected]>

Co-authored-by: D <[email protected]>
  • Loading branch information
davidkel and D authored Feb 25, 2022
1 parent 338212d commit 9043fd0
Show file tree
Hide file tree
Showing 25 changed files with 244 additions and 11,280 deletions.
1 change: 0 additions & 1 deletion packages/caliper-core/lib/common/config/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ const keys = {
CountQueryAsLoad: 'caliper-fabric-countqueryasload',
SkipCreateChannelPrefix: 'caliper-fabric-skipcreatechannel-',
Gateway: {
Discovery: 'caliper-fabric-gateway-discovery',
Enabled: 'caliper-fabric-gateway-enabled',
EventStrategy: 'caliper-fabric-gateway-eventstrategy',
LocalHost: 'caliper-fabric-gateway-localhost',
Expand Down
2 changes: 0 additions & 2 deletions packages/caliper-core/lib/common/config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,6 @@ caliper:
enabled: false
# Indicates whether to use the localhost default within the Fabric Gateway API
localhost: true
# Indicates whether to use the Fabric discovery mechanism (via Gateway API)
discovery: false
# Which event strategy to use
eventstrategy: msp_any
# Which query strategy to use
Expand Down
6 changes: 5 additions & 1 deletion packages/caliper-core/lib/manager/caliper-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ class CaliperEngine {
BenchValidator.validateObject(this.benchmarkConfig);

logger.info('Starting benchmark flow');
let connector = await this.adapterFactory(-1);

try {

Expand All @@ -101,10 +100,13 @@ class CaliperEngine {
await this._executeCommand('start', 0);
}

let connector;

// Conditional network initialization
if (!flowOpts.performInit) {
logger.info('Skipping initialization phase due to benchmark flow conditioning');
} else {
connector = connector ? connector : await this.adapterFactory(-1);
let initStartTime = Date.now();
try {
await connector.init();
Expand All @@ -123,6 +125,7 @@ class CaliperEngine {
if (!flowOpts.performInstall) {
logger.info('Skipping install smart contract phase due to benchmark flow conditioning');
} else {
connector = connector ? connector : await this.adapterFactory(-1);
let installStartTime = Date.now();
try {
await connector.installSmartContract();
Expand All @@ -143,6 +146,7 @@ class CaliperEngine {
} else {
let numberSet = this.benchmarkConfig.test && this.benchmarkConfig.test.workers && this.benchmarkConfig.test.workers.number;
let numberOfWorkers = numberSet ? this.benchmarkConfig.test.workers.number : 1;
connector = connector ? connector : await this.adapterFactory(-1);
let workerArguments = await connector.prepareWorkerArguments(numberOfWorkers);

const roundOrchestrator = new RoundOrchestrator(this.benchmarkConfig, this.networkConfig, workerArguments);
Expand Down
73 changes: 19 additions & 54 deletions packages/caliper-fabric/lib/FabricConnectorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

const { CaliperUtils, ConfigUtil } = require('@hyperledger/caliper-core');
const ConnectorConfigurationFactory = require('./connector-configuration/ConnectorConfigurationFactory');
const ConfigValidator = require('./configValidator.js');
const Logger = CaliperUtils.getLogger('fabric-connector');
const Logger = CaliperUtils.getLogger('FabricConnectorFactory');
const semver = require('semver');

const LEGACY_V1_NODE_CONNECTOR = './connector-versions/v1/fabric.js';
const LEGACY_V1_GATEWAY_CONNECTOR = './connector-versions/v1/fabric-gateway.js';
const LEGACY_V2_GATEWAY_CONNECTOR = './connector-versions/v2/fabric-gateway.js';

const NEW_V1_NODE_CONNECTOR = './connector-versions/v1/FabricNonGateway.js';
const NEW_V1_GATEWAY_CONNECTOR = './connector-versions/v1/FabricGateway.js';
const NEW_V1_WALLET_FACADE_FACTORY = './connector-versions/v1/WalletFacadeFactory.js';
Expand All @@ -35,41 +30,29 @@ const NEW_V2_WALLET_FACADE_FACTORY = './connector-versions/v2/WalletFacadeFactor
* @returns {string} version
*/
const _determineInstalledNodeSDKVersion = () => {
let version;

// Caliper can only work if you use bind and it will pull in fabric network even for non gateway 1.4
if (CaliperUtils.moduleIsInstalled('fabric-network')) {
const packageVersion = require('fabric-network/package').version;
version = semver.coerce(packageVersion);
} else if (CaliperUtils.moduleIsInstalled('fabric-client')) {
const packageVersion = require('fabric-client/package').version;
version = semver.coerce(packageVersion);
return semver.coerce(packageVersion);
} else {
const msg = 'Unable to detect required Fabric binding packages';
throw new Error(msg);
throw new Error('Unable to detect required Fabric binding packages');
}
return version;
};

const _loadAppropriateConnectorClass = (installedNodeSDKVersion, useGateway, useLegacyVersion) => {
const _loadAppropriateConnectorClass = (installedNodeSDKVersion, useGateway) => {
let connectorPath;
let walletFacadeFactoryPath;

if (semver.satisfies(installedNodeSDKVersion, '=1.x')) {
if (!useGateway) {
if (useLegacyVersion) {
connectorPath = LEGACY_V1_NODE_CONNECTOR;
} else {
connectorPath = NEW_V1_NODE_CONNECTOR;
walletFacadeFactoryPath = NEW_V1_WALLET_FACADE_FACTORY;
}
connectorPath = NEW_V1_NODE_CONNECTOR;
walletFacadeFactoryPath = NEW_V1_WALLET_FACADE_FACTORY;
} else {
// gateway with default event handlers appears in SDK > 1.4.2
if (semver.satisfies(installedNodeSDKVersion, '>=1.4.2')) {
if (useLegacyVersion) {
connectorPath = LEGACY_V1_GATEWAY_CONNECTOR;
} else {
connectorPath = NEW_V1_GATEWAY_CONNECTOR;
walletFacadeFactoryPath = NEW_V1_WALLET_FACADE_FACTORY;
}
connectorPath = NEW_V1_GATEWAY_CONNECTOR;
walletFacadeFactoryPath = NEW_V1_WALLET_FACADE_FACTORY;
} else {
throw new Error('Caliper currently only supports Fabric gateway based operation using Fabric-SDK 1.4.2 and higher. Please retry with a different SDK binding');
}
Expand All @@ -78,12 +61,8 @@ const _loadAppropriateConnectorClass = (installedNodeSDKVersion, useGateway, use
if (!useGateway) {
throw new Error(`Caliper currently only supports gateway based operation using the ${installedNodeSDKVersion} Fabric-SDK. Please retry with the gateway flag`);
} else {
if (useLegacyVersion) {
connectorPath = LEGACY_V2_GATEWAY_CONNECTOR;
} else {
connectorPath = NEW_V2_GATEWAY_CONNECTOR;
walletFacadeFactoryPath = NEW_V2_WALLET_FACADE_FACTORY;
}
connectorPath = NEW_V2_GATEWAY_CONNECTOR;
walletFacadeFactoryPath = NEW_V2_WALLET_FACADE_FACTORY;
}
} else {
throw new Error(`Installed SDK version ${installedNodeSDKVersion} did not match any compatible Fabric connectors`);
Expand All @@ -108,35 +87,21 @@ const connectorFactory = async (workerIndex) => {

const connectorConfigurationFile = CaliperUtils.resolvePath(ConfigUtil.get(ConfigUtil.keys.NetworkConfig));
const loadedConnectorConfiguration = CaliperUtils.parseYaml(connectorConfigurationFile);
const legacyVersion = loadedConnectorConfiguration.version === '1.0';
if (loadedConnectorConfiguration.version === '1.0') {
throw new Error('Network configuration version 1.0 is not supported anymore, please use version 2');
}

if (!legacyVersion && !semver.satisfies(loadedConnectorConfiguration.version, '=2.0')) {
if (!semver.satisfies(loadedConnectorConfiguration.version, '=2.0')) {
throw new Error(`Unknown network configuration version ${loadedConnectorConfiguration.version} specified`);
}

const installedNodeSDKVersion = _determineInstalledNodeSDKVersion();
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' } connector compatible with installed SDK: ${installedNodeSDKVersion}`);
const {fabricConnectorClass, walletFacadeFactoryClass} = _loadAppropriateConnectorClass(installedNodeSDKVersion, useGateway, legacyVersion);

let fabricConnector;

if (legacyVersion) {
ConfigValidator.validateNetwork(loadedConnectorConfiguration, CaliperUtils.getFlowOptions(), useDiscovery, useGateway);

fabricConnector = new fabricConnectorClass(loadedConnectorConfiguration, workerIndex, 'fabric');
if (workerIndex > -1) {
// These connectors must have init called for both masters and workers
// but for masters it will have already been called
await fabricConnector.init(true);
}
} else {
// use new connectors
const connectorConfiguration = await new ConnectorConfigurationFactory().create(connectorConfigurationFile, new walletFacadeFactoryClass());
fabricConnector = new fabricConnectorClass(connectorConfiguration, workerIndex, 'fabric');
}
const {fabricConnectorClass, walletFacadeFactoryClass} = _loadAppropriateConnectorClass(installedNodeSDKVersion, useGateway);
const connectorConfiguration = await new ConnectorConfigurationFactory().create(connectorConfigurationFile, new walletFacadeFactoryClass());
const fabricConnector = new fabricConnectorClass(connectorConfiguration, workerIndex, 'fabric');

return fabricConnector;
};
Expand Down
Loading

0 comments on commit 9043fd0

Please sign in to comment.