Skip to content

Commit 0bd21b7

Browse files
mbwhitejt-nti
authored andcommitted
Update to use arrow functions
- removed possible issue with stream referring to the correct value, due to difference in scoping between function() and ()=>{} - ensured that in server mode, the promise was awaited for - fixed the register message being sent as the wrong type Signed-off-by: Matthew B White <[email protected]>
1 parent 30125f6 commit 0bd21b7

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

libraries/fabric-shim/lib/contract-spi/bootstrap.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ class Bootstrap {
2929
* @ignore
3030
* @param {Contract} contracts contract to register to use
3131
*/
32-
static register(contracts, serializers, fileMetadata, title, version, opts, serverMode = false) {
32+
static async register(contracts, serializers, fileMetadata, title, version, opts, serverMode = false) {
3333
// load up the meta data that the user may have specified
3434
// this will need to passed in and rationalized with the
3535
// code as implemented
3636
const chaincode = new ChaincodeFromContract(contracts, serializers, fileMetadata, title, version);
3737

3838
if (serverMode) {
3939
const server = shim.server(chaincode, opts);
40-
server.start();
40+
await server.start();
4141
} else {
4242
// say hello to the peer
4343
shim.start(chaincode);
@@ -53,7 +53,7 @@ class Bootstrap {
5353
const opts = serverMode ? ServerCommand.getArgs(yargs) : StartCommand.getArgs(yargs);
5454
const {contracts, serializers, title, version} = this.getInfoFromContract(opts['module-path']);
5555
const fileMetadata = await Bootstrap.getMetadata(opts['module-path']);
56-
Bootstrap.register(contracts, serializers, fileMetadata, title, version, opts, serverMode);
56+
await Bootstrap.register(contracts, serializers, fileMetadata, title, version, opts, serverMode);
5757
}
5858

5959
static getInfoFromContract(modulePath) {

libraries/fabric-shim/lib/handler.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,13 @@ class ChaincodeMessageHandler {
286286
this.msgQueueHandler = new MsgQueueHandler(this);
287287

288288
const stream = this._stream;
289-
const self = this;
289+
290290
// the conversation is supposed to follow a certain protocol,
291291
// if either side spoke out of turn, then we error out and
292292
// reject that response. The initial state is "created"
293293
let state = 'created';
294294

295-
stream.on('data', function (msgpb) {
295+
stream.on('data', (msgpb) => {
296296
const msg = mapFromChaincodeMessage(msgpb);
297297
logger.debug(util.format('Received chat message from peer: %s, state: %s, type: %s', msg.txid, state, msg.type));
298298
if (state === STATES.Ready) {
@@ -304,13 +304,13 @@ class ChaincodeMessageHandler {
304304

305305
if (type === MSG_TYPE.RESPONSE || type === MSG_TYPE.ERROR) {
306306
logger.debug(util.format('%s Received %s, handling good or error response', loggerPrefix, msg.type));
307-
self.msgQueueHandler.handleMsgResponse(msg);
307+
this.msgQueueHandler.handleMsgResponse(msg);
308308
} else if (type === MSG_TYPE.INIT) {
309309
logger.debug(util.format('%s Received %s, initializing chaincode', loggerPrefix, msg.type));
310-
self.handleInit(msg);
310+
this.handleInit(msg);
311311
} else if (type === MSG_TYPE.TRANSACTION) {
312312
logger.debug(util.format('%s Received %s, invoking transaction on chaincode(state:%s)', loggerPrefix, msg.type, state));
313-
self.handleTransaction(msg);
313+
this.handleTransaction(msg);
314314
} else {
315315
logger.error('Received unknown message from the peer. Exiting.');
316316
// TODO: Should we really do this ?
@@ -351,12 +351,12 @@ class ChaincodeMessageHandler {
351351
}
352352
});
353353

354-
stream.on('end', function () {
354+
stream.on('end', () => {
355355
logger.debug('Chat stream ending');
356356
stream.cancel();
357357
});
358358

359-
stream.on('error', function (err) {
359+
stream.on('error', (err) => {
360360
logger.error('Chat stream with peer - on error: %j', err.stack ? err.stack : err);
361361
stream.end();
362362
});

libraries/fabric-shim/lib/server.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ class ChaincodeServer {
9595
const msgPb = new peer.ChaincodeID();
9696
msgPb.setName(this._serverOpts.ccid);
9797

98+
const registerMsg = new peer.ChaincodeMessage();
99+
registerMsg.setType(peer.ChaincodeMessage.Type.REGISTER);
100+
registerMsg.setPayload(msgPb.serializeBinary());
101+
98102
logger.debug('Start chatting with a peer through a new stream. Chaincode ID = ' + this._serverOpts.ccid);
99-
client.chat({
100-
type: peer.ChaincodeMessage.Type.REGISTER,
101-
payload: msgPb.serializeBinary()
102-
});
103+
client.chat(registerMsg);
103104
} catch (e) {
104105
logger.warn('connection from peer failed: ' + e);
105106
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,11 @@ describe('ChaincodeServer', () => {
198198
const payloadPb = new peer.ChaincodeID();
199199
payloadPb.setName('example-chaincode-id');
200200

201-
expect(mockHandler.chat.firstCall.args).to.deep.equal([{
202-
type: peer.ChaincodeMessage.Type.REGISTER,
203-
payload: payloadPb.serializeBinary()
204-
}]);
201+
const expectedResponse = new peer.ChaincodeMessage();
202+
expectedResponse.setType(peer.ChaincodeMessage.Type.REGISTER);
203+
expectedResponse.setPayload(payloadPb.serializeBinary());
204+
205+
expect(mapFromChaincodeMessage(mockHandler.chat.firstCall.args[0])).to.deep.equal(mapFromChaincodeMessage(expectedResponse));
205206
});
206207

207208
it('should not throw even if chat fails', () => {
@@ -220,3 +221,14 @@ describe('ChaincodeServer', () => {
220221
});
221222
});
222223
});
224+
225+
function mapFromChaincodeMessage(msgPb) {
226+
return {
227+
type: msgPb.getType(),
228+
payload: Buffer.from(msgPb.getPayload_asU8()),
229+
txid: msgPb.getTxid(),
230+
channel_id: msgPb.getChannelId(),
231+
proposal: msgPb.getProposal(),
232+
chaincode_event: msgPb.getChaincodeEvent()
233+
};
234+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ describe('Stub', () => {
524524
});
525525

526526
it ('should throw Error if MSPID is not available', () => {
527+
delete process.env.CORE_PEER_LOCALMSPID;
527528
const stub = new Stub('dummyClient', 'dummyChannelId', 'dummyTxid', chaincodeInput);
528529

529530
expect(() => {

0 commit comments

Comments
 (0)