Skip to content

Commit 25f905d

Browse files
authored
added logic to pick new Peer Gateway connector in the connector selector in FabriConnectorFactory.js + added 2.4 fabric SUT version with binding packages in .config.yaml + added last fixes for the Peer Gateway connector implementation (#1298)
Signed-off-by: fraVlaca <ocsenarf@outlook.com>
1 parent 7c33aca commit 25f905d

File tree

4 files changed

+120
-45
lines changed

4 files changed

+120
-45
lines changed

packages/caliper-cli/lib/lib/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ sut:
4242
2.2.11: &fabric-v2-lts
4343
packages: ['fabric-network@2.2.11']
4444
2.2: *fabric-v2-lts
45+
2.4:
46+
packages: ['@hyperledger/fabric-gateway@1.0.1']
4547
latest: *fabric-v1-lts
4648
latest-v2-lts: *fabric-v2-lts
4749
latest-v2: *fabric-v2-lts

packages/caliper-fabric/lib/FabricConnectorFactory.js

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,87 @@ const ConnectorConfigurationFactory = require('./connector-configuration/Connect
1919
const Logger = CaliperUtils.getLogger('FabricConnectorFactory');
2020
const semver = require('semver');
2121

22-
const NEW_V1_NODE_CONNECTOR = './connector-versions/v1/FabricNonGateway.js';
23-
const NEW_V1_GATEWAY_CONNECTOR = './connector-versions/v1/FabricGateway.js';
24-
const NEW_V1_WALLET_FACADE_FACTORY = './connector-versions/v1/WalletFacadeFactory.js';
22+
const V1_NODE_CONNECTOR = './connector-versions/v1/FabricNonGateway.js';
23+
const V1_GATEWAY_CONNECTOR = './connector-versions/v1/FabricGateway.js';
24+
const V1_WALLET_FACADE_FACTORY = './connector-versions/v1/WalletFacadeFactory.js';
2525

26-
const NEW_V2_GATEWAY_CONNECTOR = './connector-versions/v2/FabricGateway.js';
27-
const NEW_V2_WALLET_FACADE_FACTORY = './connector-versions/v2/WalletFacadeFactory.js';
26+
const V2_GATEWAY_CONNECTOR = './connector-versions/v2/FabricGateway.js';
27+
const V2_WALLET_FACADE_FACTORY = './connector-versions/v2/WalletFacadeFactory.js';
28+
29+
const PEER_GATEWAY_CONNECTOR = './connector-versions/peer-gateway/PeerGateway.js';
30+
const PEER_WALLET_FACADE_FACTORY = './connector-versions/peer-gateway/WalletFacadeFactory.js';
2831

2932
/**
30-
* @returns {string} version
33+
* @typedef {Object} Sdk
34+
* @property {String} module - The sdk module name
35+
* @property {String} version - The version of the sdk module
3136
*/
32-
const _determineInstalledNodeSDKVersion = () => {
3337

38+
/**
39+
* @returns {Sdk} installedSDKmodule and version
40+
*/
41+
const _determineInstalledNodeSDKandVersion = () => {
3442
// Caliper can only work if you use bind and it will pull in fabric network even for non gateway 1.4
43+
let sdk, packageVersion;
44+
45+
if (CaliperUtils.moduleIsInstalled('@hyperledger/fabric-gateway')) {
46+
packageVersion = semver.coerce(require('@hyperledger/fabric-gateway/package').version);
47+
sdk = 'fabric-gateway';
48+
}
49+
3550
if (CaliperUtils.moduleIsInstalled('fabric-network')) {
36-
const packageVersion = require('fabric-network/package').version;
37-
return semver.coerce(packageVersion);
38-
} else {
51+
if (sdk) {
52+
throw new Error('Multiple bindings for fabric have been detected, you need to unbind one or more to ensure only a single bind is present to continue');
53+
}
54+
packageVersion = semver.coerce(require('fabric-network/package').version);
55+
sdk = 'fabric-network';
56+
}
57+
58+
59+
if (!sdk) {
3960
throw new Error('Unable to detect required Fabric binding packages');
4061
}
62+
63+
return {sdk, packageVersion};
4164
};
4265

43-
const _loadAppropriateConnectorClass = (installedNodeSDKVersion) => {
66+
const _loadAppropriateConnectorClass = (installedNodeSdk, version) => {
4467
let connectorPath;
4568
let walletFacadeFactoryPath;
4669

47-
if (semver.satisfies(installedNodeSDKVersion, '=1.x')) {
48-
const useGateway = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.Enabled, false);
49-
Logger.info(`Initializing ${useGateway ? 'gateway' : 'standard' } connector compatible with installed SDK: ${installedNodeSDKVersion}`);
70+
if (installedNodeSdk === 'fabric-network') {
71+
if (semver.satisfies(version, '=1.x')) {
72+
const useGateway = ConfigUtil.get(ConfigUtil.keys.Fabric.Gateway.Enabled, false);
73+
Logger.info(`Initializing ${useGateway ? 'gateway' : 'standard' } connector compatible with installed fabric-network SDK: ${version}`);
5074

51-
if (!useGateway) {
52-
connectorPath = NEW_V1_NODE_CONNECTOR;
53-
walletFacadeFactoryPath = NEW_V1_WALLET_FACADE_FACTORY;
54-
} else {
55-
// gateway with default event handlers appears in SDK > 1.4.2
56-
if (semver.satisfies(installedNodeSDKVersion, '>=1.4.2')) {
57-
connectorPath = NEW_V1_GATEWAY_CONNECTOR;
58-
walletFacadeFactoryPath = NEW_V1_WALLET_FACADE_FACTORY;
75+
if (!useGateway) {
76+
connectorPath = V1_NODE_CONNECTOR;
77+
walletFacadeFactoryPath = V1_WALLET_FACADE_FACTORY;
5978
} else {
60-
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');
79+
// gateway with default event handlers appears in SDK > 1.4.2
80+
if (semver.satisfies(version, '>=1.4.2')) {
81+
connectorPath = V1_GATEWAY_CONNECTOR;
82+
walletFacadeFactoryPath = V1_WALLET_FACADE_FACTORY;
83+
} else {
84+
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');
85+
}
6186
}
87+
} else if (semver.satisfies(version, '=2.x')) {
88+
Logger.info(`Initializing gateway connector compatible with installed SDK: ${version}`);
89+
connectorPath = V2_GATEWAY_CONNECTOR;
90+
walletFacadeFactoryPath = V2_WALLET_FACADE_FACTORY;
91+
} else {
92+
throw new Error(`Installed fabric-network SDK version ${version} did not match any compatible Fabric connectors`);
6293
}
63-
} else if (semver.satisfies(installedNodeSDKVersion, '=2.x')) {
64-
Logger.info(`Initializing gateway connector compatible with installed SDK: ${installedNodeSDKVersion}`);
65-
connectorPath = NEW_V2_GATEWAY_CONNECTOR;
66-
walletFacadeFactoryPath = NEW_V2_WALLET_FACADE_FACTORY;
6794
} else {
68-
throw new Error(`Installed SDK version ${installedNodeSDKVersion} did not match any compatible Fabric connectors`);
95+
// can only be fabric-gateway binding due to check done in _determineInstalledNodeSDKandVersion
96+
if (semver.satisfies(version, '=1.x')) {
97+
Logger.info(`Initializing peer gateway connector compatible with installed fabric-gateway SDK: ${version}`);
98+
connectorPath = PEER_GATEWAY_CONNECTOR;
99+
walletFacadeFactoryPath = PEER_WALLET_FACADE_FACTORY;
100+
} else {
101+
throw new Error(`Installed fabric-gateway SDK version ${version} did not match any compatible Fabric connectors`);
102+
}
69103
}
70104

71105
const fabricConnectorClass = require(connectorPath);
@@ -95,8 +129,9 @@ const connectorFactory = async (workerIndex) => {
95129
throw new Error(`Unknown network configuration version ${loadedConnectorConfiguration.version} specified`);
96130
}
97131

98-
const installedNodeSDKVersion = _determineInstalledNodeSDKVersion();
99-
const {fabricConnectorClass, walletFacadeFactoryClass} = _loadAppropriateConnectorClass(installedNodeSDKVersion);
132+
const sdk = _determineInstalledNodeSDKandVersion();
133+
134+
const {fabricConnectorClass, walletFacadeFactoryClass} = _loadAppropriateConnectorClass(sdk.sdk, sdk.packageVersion);
100135
const connectorConfiguration = await new ConnectorConfigurationFactory().create(connectorConfigurationFile, new walletFacadeFactoryClass());
101136
const fabricConnector = new fabricConnectorClass(connectorConfiguration, workerIndex, 'fabric');
102137

packages/caliper-fabric/test/FabricConnectorFactory.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const { ConfigUtil } = require('@hyperledger/caliper-core');
2929
const unsupportedConfig = './sample-configs/LegacyNetworkConfig.yaml';
3030
const v2Config = './sample-configs/NoIdentitiesNetworkConfig.yaml';
3131
const unknownVersionConfig = './sample-configs/UnknownVersionConfig.yaml';
32+
const peerGatewayConfig = './sample-configs/BasicConfig.yaml';
3233

3334
const DefaultEventHandlerStrategies = {};
3435
const DefaultQueryHandlerStrategies = {};
@@ -163,20 +164,59 @@ describe('A Fabric Connector Factory', () => {
163164
mockery.deregisterAll();
164165
});
165166

167+
it('should create a Peer Gateway Fabric connector if a 1.x fabric-gateway library is bound', async () => {
168+
mockery.registerMock('@hyperledger/fabric-gateway', {});
169+
mockery.registerMock('@hyperledger/fabric-gateway/package', {version: '1.0.1'});
170+
mockery.registerMock('@grpc/grpc-js', {});
171+
ConfigUtil.set(ConfigUtil.keys.NetworkConfig, path.resolve(__dirname, peerGatewayConfig));
172+
ConfigUtil.set(ConfigUtil.keys.Fabric.Gateway.Enabled, false);
173+
const connector = await ConnectorFactory(1);
174+
connector.constructor.name.should.equal('PeerGateway');
175+
mockery.deregisterAll();
176+
});
177+
166178
it('should throw an error if no fabric library is bound', async () => {
167-
// Can't test this with mockery because really need require to fail trying to
168-
// find `fabric-network` or `fabric-client`
179+
ConfigUtil.set(ConfigUtil.keys.NetworkConfig, path.resolve(__dirname, peerGatewayConfig));
180+
ConfigUtil.set(ConfigUtil.keys.Fabric.Gateway.Enabled, true);
181+
await ConnectorFactory(1).should.be.rejectedWith(/Unable to detect required Fabric binding packages/);
182+
});
183+
184+
it('should throw an error if fabric-gateway library bound is not than v1.x', async () => {
185+
mockery.registerMock('@hyperledger/fabric-gateway', {});
186+
mockery.registerMock('@hyperledger/fabric-gateway/package', {version: '0.5.0'});
187+
ConfigUtil.set(ConfigUtil.keys.NetworkConfig, path.resolve(__dirname, peerGatewayConfig));
188+
ConfigUtil.set(ConfigUtil.keys.Fabric.Gateway.Enabled, true);
189+
await ConnectorFactory(1).should.be.rejectedWith(/Installed fabric-gateway SDK version 0.5.0 did not match any compatible Fabric connectors/);
190+
mockery.deregisterAll();
191+
mockery.registerMock('@hyperledger/fabric-gateway', {});
192+
mockery.registerMock('@hyperledger/fabric-gateway/package', {version: '2.5.0'});
193+
await ConnectorFactory(1).should.be.rejectedWith(/Installed fabric-gateway SDK version 2.5.0 did not match any compatible Fabric connectors/);
194+
mockery.deregisterAll();
195+
});
196+
197+
it('should throw an error if fabric network library bound is not v1.4, v2.x', async () => {
198+
mockery.registerMock('fabric-network', {});
199+
mockery.registerMock('fabric-network/package', {version: '3.0.0'});
200+
ConfigUtil.set(ConfigUtil.keys.NetworkConfig, path.resolve(__dirname, v2Config));
201+
ConfigUtil.set(ConfigUtil.keys.Fabric.Gateway.Enabled, true);
202+
await ConnectorFactory(1).should.be.rejectedWith(/Installed fabric-network SDK version 3.0.0 did not match any compatible Fabric connectors/);
203+
mockery.deregisterAll();
169204
});
170205

171-
it('should throw an error if fabric library bound is not V1 or V2', async () => {
206+
it('should throw an error if both fabric-network and fabric-gateway are bound', async () => {
172207
mockery.registerMock('fabric-network', {});
173208
mockery.registerMock('fabric-network/package', {version: '3.0.0'});
174209
ConfigUtil.set(ConfigUtil.keys.NetworkConfig, path.resolve(__dirname, v2Config));
175210
ConfigUtil.set(ConfigUtil.keys.Fabric.Gateway.Enabled, true);
176-
await ConnectorFactory(1).should.be.rejectedWith(/Installed SDK version 3.0.0 did not match any compatible Fabric connectors/);
211+
mockery.registerMock('@hyperledger/fabric-gateway', {});
212+
mockery.registerMock('@hyperledger/fabric-gateway/package', {version: '1.0.1'});
213+
ConfigUtil.set(ConfigUtil.keys.NetworkConfig, path.resolve(__dirname, peerGatewayConfig));
214+
ConfigUtil.set(ConfigUtil.keys.Fabric.Gateway.Enabled, true);
215+
await ConnectorFactory(1).should.be.rejectedWith(/Multiple bindings for fabric have been detected, you need to unbind one or more to ensure only a single bind is present to continue/);
177216
mockery.deregisterAll();
178217
});
179218

219+
180220
it('should throw a generic error if network configuration version is not 1.0 or 2.0', async () => {
181221
mockery.registerMock('fabric-network', {});
182222
mockery.registerMock('fabric-network/package', {version: '1.4.11'});

packages/caliper-fabric/test/connector-versions/peer-gateway/PeerGateway.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe('A Fabric Peer Gateway sdk gateway', () => {
118118
});
119119

120120

121-
it('should pass a defined grpcClinet in the gateway options', async () => {
121+
it('should pass a defined grpcClient in the gateway options', async () => {
122122
const connectorConfiguration = await new ConnectorConfigurationFactory().create(path.resolve(__dirname, configWith2Orgs1AdminInWalletNotMutual), walletFacadeFactory);
123123
const peerGateway = new PeerGateway(connectorConfiguration, 1, 'fabric');
124124
await peerGateway.getContext();
@@ -137,16 +137,6 @@ describe('A Fabric Peer Gateway sdk gateway', () => {
137137
clock.restore();
138138
});
139139

140-
it('should pass the default timeout in the gateway options', async () => {
141-
const connectorConfiguration = await new ConnectorConfigurationFactory().create(path.resolve(__dirname, configWith2Orgs1AdminInWalletNotMutual), walletFacadeFactory);
142-
const peerGateway = new PeerGateway(connectorConfiguration, 1, 'fabric');
143-
const now = new Date();
144-
const clock = sinon.useFakeTimers(now.getTime());
145-
await peerGateway.getContext();
146-
Gateway.connectArgs[0].evaluateOptions().should.be.deep.equal({deadline: now.getTime() + 60000});
147-
clock.restore();
148-
});
149-
150140
it('should pass the timeout from invokeOrQuery in the gateway options', async () => {
151141
ConfigUtil.set(ConfigUtil.keys.Fabric.Timeout.InvokeOrQuery, 99);
152142
const connectorConfiguration = await new ConnectorConfigurationFactory().create(path.resolve(__dirname, configWith2Orgs1AdminInWalletNotMutual), walletFacadeFactory);
@@ -175,6 +165,14 @@ describe('A Fabric Peer Gateway sdk gateway', () => {
175165
Gateway.constructed.should.equal(4);
176166
});
177167

168+
169+
it('should create a Gateway with the specified created identity', async () => {
170+
const connectorConfiguration = await new ConnectorConfigurationFactory().create(path.resolve(__dirname, configWith2Orgs1AdminInWalletNotMutual), walletFacadeFactory);
171+
const peerGateway = new PeerGateway(connectorConfiguration, 1, 'fabric');
172+
await peerGateway.getContext();
173+
Gateway.connectArgs[0].identity.should.deep.equal({mspId: 'Org1MSP', credentials: Buffer.from('-----BEGIN CERTIFICATE-----\n-----END CERTIFICATE-----')});
174+
});
175+
178176
it('should a attempt to create a grpc client with default grpc options when not provided through the connection profile', async () => {
179177
const connectorConfiguration = await new ConnectorConfigurationFactory().create(path.resolve(__dirname, configWith2Orgs1AdminInWalletNotMutual), walletFacadeFactory);
180178
const peerGateway = new PeerGateway(connectorConfiguration, 1, 'fabric');

0 commit comments

Comments
 (0)