@@ -19,53 +19,87 @@ const ConnectorConfigurationFactory = require('./connector-configuration/Connect
19
19
const Logger = CaliperUtils . getLogger ( 'FabricConnectorFactory' ) ;
20
20
const semver = require ( 'semver' ) ;
21
21
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' ;
25
25
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' ;
28
31
29
32
/**
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
31
36
*/
32
- const _determineInstalledNodeSDKVersion = ( ) => {
33
37
38
+ /**
39
+ * @returns {Sdk } installedSDKmodule and version
40
+ */
41
+ const _determineInstalledNodeSDKandVersion = ( ) => {
34
42
// 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
+
35
50
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 ) {
39
60
throw new Error ( 'Unable to detect required Fabric binding packages' ) ;
40
61
}
62
+
63
+ return { sdk, packageVersion} ;
41
64
} ;
42
65
43
- const _loadAppropriateConnectorClass = ( installedNodeSDKVersion ) => {
66
+ const _loadAppropriateConnectorClass = ( installedNodeSdk , version ) => {
44
67
let connectorPath ;
45
68
let walletFacadeFactoryPath ;
46
69
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 } ` ) ;
50
74
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 ;
59
78
} 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
+ }
61
86
}
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` ) ;
62
93
}
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 ;
67
94
} 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
+ }
69
103
}
70
104
71
105
const fabricConnectorClass = require ( connectorPath ) ;
@@ -95,8 +129,9 @@ const connectorFactory = async (workerIndex) => {
95
129
throw new Error ( `Unknown network configuration version ${ loadedConnectorConfiguration . version } specified` ) ;
96
130
}
97
131
98
- const installedNodeSDKVersion = _determineInstalledNodeSDKVersion ( ) ;
99
- const { fabricConnectorClass, walletFacadeFactoryClass} = _loadAppropriateConnectorClass ( installedNodeSDKVersion ) ;
132
+ const sdk = _determineInstalledNodeSDKandVersion ( ) ;
133
+
134
+ const { fabricConnectorClass, walletFacadeFactoryClass} = _loadAppropriateConnectorClass ( sdk . sdk , sdk . packageVersion ) ;
100
135
const connectorConfiguration = await new ConnectorConfigurationFactory ( ) . create ( connectorConfigurationFile , new walletFacadeFactoryClass ( ) ) ;
101
136
const fabricConnector = new fabricConnectorClass ( connectorConfiguration , workerIndex , 'fabric' ) ;
102
137
0 commit comments