Skip to content

Commit

Permalink
FABN-1259 fix timeout when debugging chaincode
Browse files Browse the repository at this point in the history
If the commitTimeout property is larger than the ‘request-timeout’
then invoke ‘sendTransactionProposal’ with this larger timeout

Change-Id: Ic1596cfb93368c430d51afafce01f5adc3601a8c
Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
  • Loading branch information
andrew-coleman committed Jun 5, 2019
1 parent faccd76 commit 670989a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
18 changes: 16 additions & 2 deletions fabric-network/lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,19 @@ class Transaction {
const network = this._contract.getNetwork();
const channel = network.getChannel();
const txId = this._transactionId.getTransactionID();
const eventHandler = this._createTxEventHandler(this, network, this._contract.getEventHandlerOptions());
const options = this._contract.getEventHandlerOptions();
const eventHandler = this._createTxEventHandler(this, network, options);

const request = this._buildRequest(args);

const commitTimeout = options.commitTimeout * 1000; // in ms
let timeout = this._contract.gateway.getClient().getConfigSetting('request-timeout', commitTimeout);
if (timeout < commitTimeout) {
timeout = commitTimeout;
}

// node sdk will target all peers on the channel that are endorsingPeer or do something special for a discovery environment
const results = await channel.sendTransactionProposal(request);
const results = await channel.sendTransactionProposal(request, timeout);
const proposalResponses = results[0];
const proposal = results[1];

Expand Down Expand Up @@ -239,6 +246,13 @@ class Transaction {

const channel = this._contract.getNetwork().getChannel();
const request = this._buildRequest(args);

const commitTimeout = this._contract.getEventHandlerOptions().commitTimeout * 1000; // in ms
const timeout = this._contract.gateway.getClient().getConfigSetting('request-timeout', commitTimeout);
if (timeout < commitTimeout) {
request.request_timeout = commitTimeout;
}

const query = new Query(channel, request);

return this._queryHandler.evaluate(query);
Expand Down
41 changes: 41 additions & 0 deletions fabric-network/test/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const chai = require('chai');
const expect = chai.expect;
chai.use(require('chai-as-promised'));

const Client = require('fabric-client');
const Channel = require('fabric-client/lib/Channel');
const Contract = require('fabric-network/lib/contract');
const Network = require('fabric-network/lib/network');
const Gateway = require('fabric-network/lib/gateway');
const Query = require('fabric-network/lib/impl/query/query');
const Transaction = require('fabric-network/lib/transaction');
const TransactionEventHandler = require('fabric-network/lib/impl/event/transactioneventhandler');
Expand Down Expand Up @@ -92,6 +94,12 @@ describe('Transaction', () => {
stubContract.getChaincodeId.returns(chaincodeId);
stubContract.getEventHandlerOptions.returns({commitTimeout: 418});

const mockClient = sinon.createStubInstance(Client);
const mockGateway = sinon.createStubInstance(Gateway);
mockGateway.getClient.returns(mockClient);
mockClient.getConfigSetting.returns(45000);
stubContract.gateway = mockGateway;

transaction = new Transaction(stubContract, transactionName);
});

Expand Down Expand Up @@ -262,6 +270,18 @@ describe('Transaction', () => {
const promise = transaction.submit();
return expect(promise).to.be.rejectedWith('Transaction has already been invoked');
});

it('sends proposal with long timeout', async () => {
stubContract.getEventHandlerOptions.returns({commitTimeout: 999});
await transaction.submit();
sinon.assert.calledWith(channel.sendTransactionProposal, sinon.match(expectedProposal), 999000);
});

it('sends proposal with short timeout', async () => {
stubContract.getEventHandlerOptions.returns({commitTimeout: 3});
await transaction.submit();
sinon.assert.calledWith(channel.sendTransactionProposal, sinon.match(expectedProposal), 45000);
});
});

describe('#evaluate', () => {
Expand Down Expand Up @@ -335,6 +355,27 @@ describe('Transaction', () => {
const promise = transaction.evaluate();
return expect(promise).to.be.rejectedWith('Transaction has already been invoked');
});

it('builds correct request for invocation with long timeout', async () => {
stubContract.getEventHandlerOptions.returns({commitTimeout: 999});

await transaction.evaluate();

const query = stubQueryHandler.evaluate.lastArg;
expect(query._request).to.deep.include({
request_timeout: 999000
});
});

it('builds correct request for invocation with short timeout', async () => {
stubContract.getEventHandlerOptions.returns({commitTimeout: 3});

await transaction.evaluate();

const query = stubQueryHandler.evaluate.lastArg;
expect(query._request.request_timeout).to.be.undefined;
});

});

describe('#addCommitListener', () => {
Expand Down

0 comments on commit 670989a

Please sign in to comment.