Skip to content

Commit 1b2768b

Browse files
shimosmbwhite
andauthored
[FABCN-408] Separate message handler from client (#155)
This patch divides the ChaincodeSupportClient class into two classes: ChaincodeSupportClient, which represents a chaincode client that connects to a peer, and ChaincodeMessageHandler, which handles messages in communciation with the peer through a stream. Because the latter part is common in both client and server model, the ChaincodeMessageHandler class will be used in the future implementation for a chaincode gRPC server. Signed-off-by: Taku Shimosawa <taku.shimosawa@hal.hitachi.com> Co-authored-by: Matthew B White <mbwhite@users.noreply.github.com>
1 parent 5acea60 commit 1b2768b

5 files changed

Lines changed: 172 additions & 83 deletions

File tree

libraries/fabric-shim/lib/chaincode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const Logger = require('./logger');
1414
const utils = require('./utils/utils');
1515

1616
const logger = Logger.getLogger('lib/chaincode.js');
17-
const Handler = require('./handler');
17+
const {ChaincodeSupportClient} = require('./handler');
1818
const Iterators = require('./iterators');
1919
const ChaincodeStub = require('./stub');
2020
const KeyEndorsementPolicy = require('./utils/statebased');
@@ -122,7 +122,7 @@ class Shim {
122122
}
123123

124124
const chaincodeName = opts['chaincode-id-name'];
125-
const client = new Handler(chaincode, url, optsCpy);
125+
const client = new ChaincodeSupportClient(chaincode, url, optsCpy);
126126
const chaincodeID = {
127127
name: chaincodeName
128128
};

libraries/fabric-shim/lib/handler.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class MsgQueueHandler {
205205

206206

207207
/*
208-
* The ChaincodeSupportClient class represents a the base class for all remote nodes, Peer, Orderer , and MemberServicespeer.
208+
* The ChaincodeSupportClient class represents a chaincode gRPC client to the peer.
209209
*/
210210
class ChaincodeSupportClient {
211211

@@ -269,11 +269,37 @@ class ChaincodeSupportClient {
269269
this._stream.end();
270270
}
271271

272+
chat(convStarterMsg) {
273+
this._stream = this._client.register();
274+
275+
this._handler = new ChaincodeMessageHandler(this._stream, this.chaincode);
276+
this._handler.chat(convStarterMsg);
277+
}
278+
279+
/*
280+
return a printable representation of this object
281+
*/
282+
toString() {
283+
return 'ChaincodeSupportClient : {' +
284+
'url:' +
285+
this._url +
286+
'}';
287+
}
288+
}
289+
290+
/**
291+
* The ChaincodeMessageHandler class handles messages between peer and chaincode both in the chaincode server and client model.
292+
*/
293+
class ChaincodeMessageHandler {
294+
constructor (stream, chaincode) {
295+
this._stream = stream;
296+
this.chaincode = chaincode;
297+
}
298+
272299
// this is a long-running method that does not return until
273300
// the conversation b/w the chaincode program and the target
274301
// peer has been completed
275302
chat(convStarterMsg) {
276-
this._stream = this._client.register();
277303
this.msgQueueHandler = new MsgQueueHandler(this);
278304

279305
const stream = this._stream;
@@ -528,15 +554,11 @@ class ChaincodeSupportClient {
528554
});
529555
}
530556

531-
532557
/*
533-
* return a printable representation of this object
534-
*/
558+
return a printable representation of this object
559+
*/
535560
toString() {
536-
return 'ChaincodeSupportClient : {' +
537-
'url:' +
538-
this._url +
539-
'}';
561+
return 'ChaincodeMessageHandler : {}';
540562
}
541563
}
542564

@@ -723,7 +745,10 @@ function parseResponse(handler, res, method) {
723745
}
724746
}
725747

726-
module.exports = ChaincodeSupportClient;
748+
module.exports = {
749+
ChaincodeSupportClient,
750+
ChaincodeMessageHandler
751+
};
727752

728753
//
729754
// The Endpoint class represents a remote grpc or grpcs target

libraries/fabric-shim/test/unit/chaincode.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('Chaincode', () => {
9797
});
9898

9999
it ('should start when passed init and invoke', () => {
100-
const handlerClass = Chaincode.__get__('Handler');
100+
const handlerClass = Chaincode.__get__('ChaincodeSupportClient');
101101
const chat = sandbox.stub(handlerClass.prototype, 'chat');
102102

103103
const myYargs = {'argv': {'$0': 'fabric-chaincode-node', 'peer.address': 'localhost:7051', 'chaincode-id-name': 'mycc'}};
@@ -140,8 +140,8 @@ describe('Chaincode', () => {
140140
const myYargs = {'argv': {'$0': 'fabric-chaincode-node', 'peer.address': 'localhost:7051', 'chaincode-id-name': 'mycc', 'some-other-arg': 'another-arg', 'yet-another-bad-arg': 'arg'}};
141141
Chaincode.__set__('yargs', myYargs);
142142

143-
const handlerClass = Chaincode.__get__('Handler');
144-
Chaincode.__set__('Handler', MockHandler);
143+
const handlerClass = Chaincode.__get__('ChaincodeSupportClient');
144+
Chaincode.__set__('ChaincodeSupportClient', MockHandler);
145145

146146
const getArgsStub = sandbox.stub(StartCommand, 'getArgs').returns({
147147
'peer.address': 'localhost:7051',
@@ -162,7 +162,7 @@ describe('Chaincode', () => {
162162
expect(testOpts.hasOwnProperty('module-path')).to.be.false;
163163
expect(testOpts.hasOwnProperty('peer.address')).to.be.true;
164164

165-
Chaincode.__set__('Handler', handlerClass);
165+
Chaincode.__set__('ChaincodeSupportClient', handlerClass);
166166

167167
getArgsStub.restore();
168168
});
@@ -213,7 +213,7 @@ describe('Chaincode', () => {
213213

214214
it ('should call handler.chat() with the correct object and output a message', () => {
215215

216-
const handlerClass = Chaincode.__get__('Handler');
216+
const handlerClass = Chaincode.__get__('ChaincodeSupportClient');
217217
const chat = sandbox.stub(handlerClass.prototype, 'chat');
218218

219219
process.env.CORE_TLS_CLIENT_KEY_PATH = keyPath;
@@ -247,8 +247,8 @@ describe('Chaincode', () => {
247247
}
248248
}
249249

250-
const handlerClass = Chaincode.__get__('Handler');
251-
Chaincode.__set__('Handler', MockHandler);
250+
const handlerClass = Chaincode.__get__('ChaincodeSupportClient');
251+
Chaincode.__set__('ChaincodeSupportClient', MockHandler);
252252

253253
process.env.CORE_TLS_CLIENT_KEY_PATH = keyPath;
254254
process.env.CORE_TLS_CLIENT_CERT_PATH = certPath;
@@ -262,7 +262,7 @@ describe('Chaincode', () => {
262262
testOpts.cert.should.equal(cert);
263263
testOpts.key.should.equal(key);
264264

265-
Chaincode.__set__('Handler', handlerClass);
265+
Chaincode.__set__('ChaincodeSupportClient', handlerClass);
266266
});
267267
});
268268
});

0 commit comments

Comments
 (0)