Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 5953dad

Browse files
authored
[0.19.x] Experimental override chaincode version (#4541)
This is an experimental feature and meant for advanced and knowledgable composer users who have multiple channels running the same bna and are looking to provide scaling through multiple chaincode instances due to lack of capability currently in hyperledger fabric. Signed-off-by: Dave Kelsey <d_kelsey@uk.ibm.com>
1 parent 3eb172c commit 5953dad

File tree

3 files changed

+63
-20
lines changed

3 files changed

+63
-20
lines changed

packages/composer-connector-hlfv1/lib/hlfconnection.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,21 @@ class HLFConnection extends Connection {
479479
this.fs.writeFileSync(indexFile, JSON.stringify(index));
480480
});
481481

482-
// copy over a .npmrc file, should be part of the business network definition.
483-
if (installOptions && installOptions.npmrcFile) {
484-
try {
485-
await this.fs.copy(installOptions.npmrcFile, path.join(installDir, '.npmrc'));
486-
} catch(error) {
487-
const newError = new Error(`Failed to copy specified npmrc file ${installOptions.npmrcFile} during install. ${error}`);
488-
LOG.error(method, newError);
489-
throw newError;
482+
let chaincodeVersion = businessNetworkDefinition.getVersion();
483+
if (installOptions) {
484+
if (installOptions.npmrcFile) {
485+
try {
486+
// copy over a .npmrc file, should be part of the business network definition.
487+
await this.fs.copy(installOptions.npmrcFile, path.join(installDir, '.npmrc'));
488+
} catch(error) {
489+
const newError = new Error(`Failed to copy specified npmrc file ${installOptions.npmrcFile} during install. ${error}`);
490+
LOG.error(method, newError);
491+
throw newError;
492+
}
493+
}
494+
if (installOptions.chaincodeVersion) {
495+
chaincodeVersion = installOptions.chaincodeVersion;
496+
LOG.info(method, `overriding chaincode version to be ${installOptions.chaincodeVersion}`);
490497
}
491498
}
492499

@@ -496,7 +503,7 @@ class HLFConnection extends Connection {
496503
chaincodeType: 'node',
497504
chaincodePath: installDir,
498505
metadataPath: installDir,
499-
chaincodeVersion: businessNetworkDefinition.getVersion(),
506+
chaincodeVersion,
500507
chaincodeId: businessNetworkDefinition.getName(),
501508
txId: txId,
502509
targets: this.getChannelPeersInOrg([FABRIC_CONSTANTS.NetworkConfig.ENDORSING_PEER_ROLE, FABRIC_CONSTANTS.NetworkConfig.CHAINCODE_QUERY_ROLE])

packages/composer-connector-hlfv1/test/hlfconnection.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,42 @@ describe('HLFConnection', () => {
692692
return connection.install(mockSecurityContext, mockBusinessNetwork, {npmrcFile: '/some/file'})
693693
.then(() => {
694694
sinon.assert.calledWith(connection.fs.copy, '/some/file', sinon.match(/\/.npmrc$/));
695+
sinon.assert.calledOnce(mockClient.installChaincode);
696+
sinon.assert.calledWith(mockClient.installChaincode, {
697+
chaincodeType: 'node',
698+
chaincodePath: sinon.match.string,
699+
metadataPath: sinon.match.string,
700+
chaincodeVersion: mockBusinessNetwork.getVersion(),
701+
chaincodeId: mockBusinessNetwork.getName(),
702+
txId: mockTransactionID,
703+
targets: [mockPeer1]
704+
});
705+
});
706+
});
707+
708+
it('should change the chaincode version if requested', () => {
709+
// This is the install proposal and response (from the peers).
710+
const proposalResponses = [{
711+
response: {
712+
status: 200
713+
}
714+
}];
715+
const proposal = { proposal: 'i do' };
716+
const header = { header: 'gooooal' };
717+
mockClient.installChaincode.resolves([ proposalResponses, proposal, header ]);
718+
sandbox.stub(connection, '_validatePeerResponses').returns({ignoredErrors: 0, validResponses: proposalResponses});
719+
return connection.install(mockSecurityContext, mockBusinessNetwork, {chaincodeVersion: '123.456'})
720+
.then(() => {
721+
sinon.assert.calledOnce(mockClient.installChaincode);
722+
sinon.assert.calledWith(mockClient.installChaincode, {
723+
chaincodeType: 'node',
724+
chaincodePath: sinon.match.string,
725+
metadataPath: sinon.match.string,
726+
chaincodeVersion: '123.456',
727+
chaincodeId: mockBusinessNetwork.getName(),
728+
txId: mockTransactionID,
729+
targets: [mockPeer1]
730+
});
695731
});
696732
});
697733

packages/composer-connector-hlfv1/test/hlftxeventhandler.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,32 +165,32 @@ describe('HLFTxEventHandler', () => {
165165
});
166166

167167
describe('#waitForEvents', () => {
168-
it('Should do nothing if no events hubs', () => {
168+
it('Should do nothing if no events hubs', async () => {
169169
let evHandler = new HLFTxEventHandler(null, null, null);
170-
evHandler.waitForEvents().should.eventually.be.resolved;
170+
await evHandler.waitForEvents().should.be.resolved;
171171
sinon.assert.calledOnce(logWarnSpy);
172172
evHandler = new HLFTxEventHandler([], '1234', 100);
173-
evHandler.waitForEvents().should.eventually.be.resolved;
173+
evHandler.waitForEvents().should.be.resolved;
174174
sinon.assert.calledTwice(logWarnSpy);
175175
});
176176

177-
it('Should do wait for 1 event', () => {
177+
it('Should do wait for 1 event', async () => {
178178
sandbox.stub(global, 'setTimeout');
179179
eventhub1.isconnected.returns(true);
180180
let evHandler = new HLFTxEventHandler([eventhub1], '1234', 31);
181181
evHandler.startListening();
182182
evHandler.listenerPromises[0].should.be.instanceOf(Promise);
183183
evHandler.listenerPromises[0] = Promise.resolve();
184-
evHandler.waitForEvents().should.eventually.be.resolved;
184+
await evHandler.waitForEvents().should.be.resolved;
185185

186186
evHandler = new HLFTxEventHandler([eventhub1], '1234', 31);
187187
evHandler.startListening();
188188
evHandler.listenerPromises[0].should.be.instanceOf(Promise);
189189
evHandler.listenerPromises[0] = Promise.reject();
190-
evHandler.waitForEvents().should.eventually.be.rejected;
190+
await evHandler.waitForEvents().should.be.rejected;
191191
});
192192

193-
it('Should do wait more than 1 event', () => {
193+
it('Should do wait more than 1 event', async () => {
194194
sandbox.stub(global, 'setTimeout');
195195
eventhub1.isconnected.returns(true);
196196
eventhub2.isconnected.returns(true);
@@ -200,7 +200,7 @@ describe('HLFTxEventHandler', () => {
200200
evHandler.listenerPromises[0] = Promise.resolve();
201201
evHandler.listenerPromises[1].should.be.instanceOf(Promise);
202202
evHandler.listenerPromises[1] = Promise.reject();
203-
evHandler.waitForEvents().should.eventually.be.rejected;
203+
await evHandler.waitForEvents().should.be.rejected;
204204
});
205205

206206
it('Should handle timeout for an event', () => {
@@ -209,7 +209,7 @@ describe('HLFTxEventHandler', () => {
209209
eventhub2.isconnected.returns(true);
210210
let evHandler = new HLFTxEventHandler([eventhub1, eventhub2], '1234', 31);
211211
evHandler.startListening();
212-
evHandler.waitForEvents().should.eventually.be.rejectedWith(/commit notification/)
212+
return evHandler.waitForEvents().should.eventually.be.rejectedWith(/commit notification/)
213213
.then(() => {
214214
sinon.assert.calledOnce(eventhub1.unregisterTxEvent);
215215
sinon.assert.calledWith(eventhub1.unregisterTxEvent, '1234');
@@ -229,7 +229,7 @@ describe('HLFTxEventHandler', () => {
229229
eventhub2.isconnected.returns(true);
230230
let evHandler = new HLFTxEventHandler([eventhub1, eventhub2], '1234', 31);
231231
evHandler.startListening();
232-
evHandler.waitForEvents()
232+
return evHandler.waitForEvents()
233233
.then(() => {
234234
sinon.assert.calledOnce(eventhub1.unregisterTxEvent);
235235
sinon.assert.calledWith(eventhub1.unregisterTxEvent, '1234');
@@ -253,7 +253,7 @@ describe('HLFTxEventHandler', () => {
253253
eventhub2.isconnected.returns(true);
254254
let evHandler = new HLFTxEventHandler([eventhub1, eventhub2], '1234', 31);
255255
evHandler.startListening();
256-
evHandler.waitForEvents().should.eventually.be.rejectedWith(/rejected transaction/)
256+
return evHandler.waitForEvents().should.eventually.be.rejectedWith(/rejected transaction/)
257257
.then(() => {
258258
sinon.assert.calledOnce(eventhub1.unregisterTxEvent);
259259
sinon.assert.calledWith(eventhub1.unregisterTxEvent, '1234');

0 commit comments

Comments
 (0)