From ab1e3712ad0bfa54b66dc7a623151407c1af7778 Mon Sep 17 00:00:00 2001 From: "Mark S. Lewis" Date: Wed, 12 Dec 2018 11:17:01 +0000 Subject: [PATCH] FABN-1060: Handle error responses in comparison Proposal responses returned from Channel.sendTransactionProposal() can be any of: ProposalResponse, Error, or Error augmented with response element of a ProposalResponse. Changing Channel.compareProposalResponseResults() so that it can handle any responses returned by Channel.sendTransactionProposal() without error. Where any of the responses are not good (i.e. instances of Error), compareProposalResponseResults() will return false, since they don't contain valid write sets. Similar change to Channel.verifyProposalResponse() to return false for error responses. If client code wants to compare only the successful proposal responses, they can do so with: const goodResponses = responses.filter((response) => !(response instanceof Error)); channel.compareProposalResponseResults(goodResponses); Change-Id: Id1af8b3c5a1cb51eb2f4e9b50897671634848a8a Signed-off-by: Mark S. Lewis --- fabric-client/lib/Channel.js | 7 +++++++ fabric-client/test/Channel.js | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/fabric-client/lib/Channel.js b/fabric-client/lib/Channel.js index 64c20fd5b8..16d05f38d5 100755 --- a/fabric-client/lib/Channel.js +++ b/fabric-client/lib/Channel.js @@ -3313,6 +3313,9 @@ const Channel = class { if (!proposal_response) { throw new Error('Missing proposal response'); } + if (proposal_response instanceof Error) { + return false; + } if (!proposal_response.endorsement) { throw new Error('Parameter must be a ProposalResponse Object'); } @@ -3382,6 +3385,10 @@ const Channel = class { throw new Error('proposal_responses is empty'); } + if (proposal_responses.some((response) => response instanceof Error)) { + return false; + } + const first_one = _getProposalResponseResults(proposal_responses[0]); for (let i = 1; i < proposal_responses.length; i++) { const next_one = _getProposalResponseResults(proposal_responses[i]); diff --git a/fabric-client/test/Channel.js b/fabric-client/test/Channel.js index 57dc9c7fa5..2fd19f147a 100644 --- a/fabric-client/test/Channel.js +++ b/fabric-client/test/Channel.js @@ -618,6 +618,13 @@ describe('Channel', () => { const result = channel.compareProposalResponseResults([proposalResponse1, proposalResponse2]); expect(result).to.be.false; }); + + it('returns false if any proposal responses are Error objects', () => { + const proposalResponse1 = createProposalResponse('foo'); + const proposalResponse2 = new Error('bah'); + const result = channel.compareProposalResponseResults([proposalResponse1, proposalResponse2]); + expect(result).to.be.false; + }); }); describe('#generateUnsignedProposal', () => { @@ -760,6 +767,12 @@ describe('Channel', () => { const result = channel.verifyProposalResponse(proposalResponse); expect(result).to.be.true; }); + + it('returns false if the proposal response is an error', () => { + const proposalResponse = new Error('sadface'); + const result = channel.verifyProposalResponse(proposalResponse); + expect(result).to.be.false; + }); }); describe('#generateUnsignedTransaction', () => {