Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FABCN-408] Separate message handler from client #155

Merged
merged 2 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libraries/fabric-shim/lib/chaincode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Logger = require('./logger');
const utils = require('./utils/utils');

const logger = Logger.getLogger('lib/chaincode.js');
const Handler = require('./handler');
const {ChaincodeSupportClient} = require('./handler');
const Iterators = require('./iterators');
const ChaincodeStub = require('./stub');
const KeyEndorsementPolicy = require('./utils/statebased');
Expand Down Expand Up @@ -122,7 +122,7 @@ class Shim {
}

const chaincodeName = opts['chaincode-id-name'];
const client = new Handler(chaincode, url, optsCpy);
const client = new ChaincodeSupportClient(chaincode, url, optsCpy);
const chaincodeID = {
name: chaincodeName
};
Expand Down
45 changes: 35 additions & 10 deletions libraries/fabric-shim/lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class MsgQueueHandler {


/*
* The ChaincodeSupportClient class represents a the base class for all remote nodes, Peer, Orderer , and MemberServicespeer.
* The ChaincodeSupportClient class represents a chaincode gRPC client to the peer.
*/
class ChaincodeSupportClient {

Expand Down Expand Up @@ -269,11 +269,37 @@ class ChaincodeSupportClient {
this._stream.end();
}

chat(convStarterMsg) {
this._stream = this._client.register();

this._handler = new ChaincodeMessageHandler(this._stream, this.chaincode);
this._handler.chat(convStarterMsg);
}

/*
return a printable representation of this object
*/
toString() {
return 'ChaincodeSupportClient : {' +
'url:' +
this._url +
'}';
}
}

/**
* The ChaincodeMessageHandler class handles messages between peer and chaincode both in the chaincode server and client model.
*/
class ChaincodeMessageHandler {
constructor (stream, chaincode) {
this._stream = stream;
this.chaincode = chaincode;
}

// this is a long-running method that does not return until
// the conversation b/w the chaincode program and the target
// peer has been completed
chat(convStarterMsg) {
this._stream = this._client.register();
this.msgQueueHandler = new MsgQueueHandler(this);

const stream = this._stream;
Expand Down Expand Up @@ -528,15 +554,11 @@ class ChaincodeSupportClient {
});
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer not to remove the toString as this will help with log files and debugging.


/*
* return a printable representation of this object
*/
return a printable representation of this object
*/
toString() {
return 'ChaincodeSupportClient : {' +
'url:' +
this._url +
'}';
return 'ChaincodeMessageHandler : {}';
}
}

Expand Down Expand Up @@ -723,7 +745,10 @@ function parseResponse(handler, res, method) {
}
}

module.exports = ChaincodeSupportClient;
module.exports = {
ChaincodeSupportClient,
ChaincodeMessageHandler
};

//
// The Endpoint class represents a remote grpc or grpcs target
Expand Down
16 changes: 8 additions & 8 deletions libraries/fabric-shim/test/unit/chaincode.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('Chaincode', () => {
});

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

const myYargs = {'argv': {'$0': 'fabric-chaincode-node', 'peer.address': 'localhost:7051', 'chaincode-id-name': 'mycc'}};
Expand Down Expand Up @@ -140,8 +140,8 @@ describe('Chaincode', () => {
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'}};
Chaincode.__set__('yargs', myYargs);

const handlerClass = Chaincode.__get__('Handler');
Chaincode.__set__('Handler', MockHandler);
const handlerClass = Chaincode.__get__('ChaincodeSupportClient');
Chaincode.__set__('ChaincodeSupportClient', MockHandler);

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

Chaincode.__set__('Handler', handlerClass);
Chaincode.__set__('ChaincodeSupportClient', handlerClass);

getArgsStub.restore();
});
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('Chaincode', () => {

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

const handlerClass = Chaincode.__get__('Handler');
const handlerClass = Chaincode.__get__('ChaincodeSupportClient');
const chat = sandbox.stub(handlerClass.prototype, 'chat');

process.env.CORE_TLS_CLIENT_KEY_PATH = keyPath;
Expand Down Expand Up @@ -247,8 +247,8 @@ describe('Chaincode', () => {
}
}

const handlerClass = Chaincode.__get__('Handler');
Chaincode.__set__('Handler', MockHandler);
const handlerClass = Chaincode.__get__('ChaincodeSupportClient');
Chaincode.__set__('ChaincodeSupportClient', MockHandler);

process.env.CORE_TLS_CLIENT_KEY_PATH = keyPath;
process.env.CORE_TLS_CLIENT_CERT_PATH = certPath;
Expand All @@ -262,7 +262,7 @@ describe('Chaincode', () => {
testOpts.cert.should.equal(cert);
testOpts.key.should.equal(key);

Chaincode.__set__('Handler', handlerClass);
Chaincode.__set__('ChaincodeSupportClient', handlerClass);
});
});
});
Expand Down
Loading