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

Commit c20b2b7

Browse files
authored
[Master] Experimental override chaincode version (#4540)
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 8827e64 commit c20b2b7

File tree

3 files changed

+65
-20
lines changed

3 files changed

+65
-20
lines changed

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

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

528-
// copy over a .npmrc file, should be part of the business network definition.
529-
if (installOptions && installOptions.npmrcFile) {
530-
try {
531-
await this.fs.copy(installOptions.npmrcFile, path.join(installDir, '.npmrc'));
532-
} catch(error) {
533-
const newError = new Error(`Failed to copy specified npmrc file ${installOptions.npmrcFile} during install. ${error}`);
534-
LOG.error(method, newError);
535-
throw newError;
528+
let chaincodeVersion = businessNetworkDefinition.getVersion();
529+
if (installOptions) {
530+
if (installOptions.npmrcFile) {
531+
try {
532+
// copy over a .npmrc file, should be part of the business network definition.
533+
await this.fs.copy(installOptions.npmrcFile, path.join(installDir, '.npmrc'));
534+
} catch(error) {
535+
const newError = new Error(`Failed to copy specified npmrc file ${installOptions.npmrcFile} during install. ${error}`);
536+
LOG.error(method, newError);
537+
throw newError;
538+
}
539+
}
540+
if (installOptions.chaincodeVersion) {
541+
chaincodeVersion = installOptions.chaincodeVersion;
542+
LOG.info(method, `overriding chaincode version to be ${installOptions.chaincodeVersion}`);
536543
}
537544
}
538545

@@ -542,7 +549,7 @@ class HLFConnection extends Connection {
542549
chaincodeType: 'node',
543550
chaincodePath: installDir,
544551
metadataPath: installDir,
545-
chaincodeVersion: businessNetworkDefinition.getVersion(),
552+
chaincodeVersion,
546553
chaincodeId: businessNetworkDefinition.getName(),
547554
txId: txId,
548555
channelNames: this.channel.getName()

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,42 @@ describe('HLFConnection', () => {
873873
return connection.install(mockSecurityContext, mockBusinessNetwork, {npmrcFile: '/some/file'})
874874
.then(() => {
875875
sinon.assert.calledWith(connection.fs.copy, '/some/file', sinon.match(/\/.npmrc$/));
876+
sinon.assert.calledOnce(mockClient.installChaincode);
877+
sinon.assert.calledWith(mockClient.installChaincode, {
878+
chaincodeType: 'node',
879+
chaincodePath: sinon.match.string,
880+
metadataPath: sinon.match.string,
881+
chaincodeVersion: mockBusinessNetwork.getVersion(),
882+
chaincodeId: mockBusinessNetwork.getName(),
883+
txId: mockTransactionID,
884+
channelNames: mockChannel.getName()
885+
});
886+
});
887+
});
888+
889+
it('should change the chaincode version if requested', () => {
890+
// This is the install proposal and response (from the peers).
891+
const proposalResponses = [{
892+
response: {
893+
status: 200
894+
}
895+
}];
896+
const proposal = { proposal: 'i do' };
897+
const header = { header: 'gooooal' };
898+
mockClient.installChaincode.resolves([ proposalResponses, proposal, header ]);
899+
sandbox.stub(connection, '_validatePeerResponses').returns({ignoredErrors: 0, validResponses: proposalResponses});
900+
return connection.install(mockSecurityContext, mockBusinessNetwork, {chaincodeVersion: '123.456'})
901+
.then(() => {
902+
sinon.assert.calledOnce(mockClient.installChaincode);
903+
sinon.assert.calledWith(mockClient.installChaincode, {
904+
chaincodeType: 'node',
905+
chaincodePath: sinon.match.string,
906+
metadataPath: sinon.match.string,
907+
chaincodeVersion: '123.456',
908+
chaincodeId: mockBusinessNetwork.getName(),
909+
txId: mockTransactionID,
910+
channelNames: mockChannel.getName()
911+
});
876912
});
877913
});
878914

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,32 +241,33 @@ describe('HLFTxEventHandler', () => {
241241
});
242242

243243
describe('#waitForEvents', () => {
244-
it('Should do nothing if no events hubs', () => {
244+
it('Should do nothing if no events hubs', async () => {
245245
let evHandler = new HLFTxEventHandler(null, null, null);
246-
evHandler.waitForEvents().should.eventually.be.resolved;
246+
await evHandler.waitForEvents().should.be.resolved;
247247
sinon.assert.calledOnce(logWarnSpy);
248248
evHandler = new HLFTxEventHandler([], '1234', 100);
249-
evHandler.waitForEvents().should.eventually.be.resolved;
249+
await evHandler.waitForEvents().should.be.resolved;
250250
sinon.assert.calledTwice(logWarnSpy);
251251
});
252252

253-
it('Should do wait for 1 event', () => {
253+
it('Should do wait for 1 event', async () => {
254254
sandbox.stub(global, 'setTimeout');
255255
sandbox.stub(HLFUtil, 'eventHubConnected').withArgs(eventhub1).returns(true);
256256
let evHandler = new HLFTxEventHandler([eventhub1], '1234', 31);
257257
evHandler.startListening();
258258
evHandler.listenerPromises[0].should.be.instanceOf(Promise);
259259
evHandler.listenerPromises[0] = Promise.resolve();
260-
evHandler.waitForEvents().should.eventually.be.resolved;
260+
evHandler.responseCount++;
261+
await evHandler.waitForEvents().should.be.resolved;
261262

262263
evHandler = new HLFTxEventHandler([eventhub1], '1234', 31);
263264
evHandler.startListening();
264265
evHandler.listenerPromises[0].should.be.instanceOf(Promise);
265266
evHandler.listenerPromises[0] = Promise.reject(new Error('some error'));
266-
evHandler.waitForEvents().should.eventually.be.rejectedWith(/some error/);
267+
await evHandler.waitForEvents().should.be.rejectedWith(/some error/);
267268
});
268269

269-
it('Should do wait more than 1 event', () => {
270+
it('Should do wait more than 1 event', async () => {
270271
sandbox.stub(global, 'setTimeout');
271272
const ehc = sandbox.stub(HLFUtil, 'eventHubConnected');
272273
ehc.withArgs(eventhub1).returns(true);
@@ -275,9 +276,10 @@ describe('HLFTxEventHandler', () => {
275276
evHandler.startListening();
276277
evHandler.listenerPromises[0].should.be.instanceOf(Promise);
277278
evHandler.listenerPromises[0] = Promise.resolve();
279+
evHandler.responseCount++;
278280
evHandler.listenerPromises[1].should.be.instanceOf(Promise);
279281
evHandler.listenerPromises[1] = Promise.reject(new Error('some error'));
280-
evHandler.waitForEvents().should.eventually.be.rejectedWith(/some error/);
282+
await evHandler.waitForEvents().should.be.rejectedWith(/some error/);
281283
});
282284

283285
it('Should handle timeout for an event', () => {
@@ -287,7 +289,7 @@ describe('HLFTxEventHandler', () => {
287289
ehc.withArgs(eventhub2).returns(true);
288290
let evHandler = new HLFTxEventHandler([eventhub1, eventhub2], '1234', 31);
289291
evHandler.startListening();
290-
evHandler.waitForEvents().should.eventually.be.rejectedWith(/commit notification/)
292+
return evHandler.waitForEvents().should.eventually.be.rejectedWith(/commit notification/)
291293
.then(() => {
292294
sinon.assert.calledOnce(eventhub1.unregisterTxEvent);
293295
sinon.assert.calledWith(eventhub1.unregisterTxEvent, '1234');
@@ -308,7 +310,7 @@ describe('HLFTxEventHandler', () => {
308310
ehc.withArgs(eventhub2).returns(true);
309311
let evHandler = new HLFTxEventHandler([eventhub1, eventhub2], '1234', 31);
310312
evHandler.startListening();
311-
evHandler.waitForEvents()
313+
return evHandler.waitForEvents()
312314
.then(() => {
313315
sinon.assert.calledOnce(eventhub1.unregisterTxEvent);
314316
sinon.assert.calledWith(eventhub1.unregisterTxEvent, '1234');
@@ -332,7 +334,7 @@ describe('HLFTxEventHandler', () => {
332334
ehc.withArgs(eventhub2).returns(true);
333335
let evHandler = new HLFTxEventHandler([eventhub1, eventhub2], '1234', 31);
334336
evHandler.startListening();
335-
evHandler.waitForEvents().should.eventually.be.rejectedWith(/rejected transaction/)
337+
return evHandler.waitForEvents().should.eventually.be.rejectedWith(/rejected transaction/)
336338
.then(() => {
337339
sinon.assert.calledOnce(eventhub1.unregisterTxEvent);
338340
sinon.assert.calledWith(eventhub1.unregisterTxEvent, '1234');

0 commit comments

Comments
 (0)