Skip to content

Commit b1c74d2

Browse files
committed
feat: add ChaincodeMessageHandler methods for getMultipleStates
1 parent e8f20a0 commit b1c74d2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

libraries/fabric-shim/lib/handler.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ class ChaincodeMessageHandler {
277277
constructor(stream, chaincode) {
278278
this._stream = stream;
279279
this.chaincode = chaincode;
280+
this.usePeerGetMultipleKeys = false;
281+
this.maxSizeGetMultipleKeys = 0;
280282
}
281283

282284
// this is a long-running method that does not return until
@@ -388,6 +390,65 @@ class ChaincodeMessageHandler {
388390
return await this._askPeerAndListen(msg, 'GetState');
389391
}
390392

393+
async handleGetMultipleStates(collection, keys, channel_id, txId) {
394+
if (keys.length === 0) {
395+
return [];
396+
}
397+
398+
const responses = [];
399+
400+
if (!this.usePeerGetMultipleKeys) {
401+
for (const key of keys) {
402+
const resp = await this.handleGetState(collection, key, channel_id, txId);
403+
responses.push(resp);
404+
}
405+
return responses;
406+
}
407+
408+
let remainingKeys = [...keys];
409+
while (remainingKeys.length > this.maxSizeGetMultipleKeys) {
410+
const batch = remainingKeys.slice(0, this.maxSizeGetMultipleKeys);
411+
const resp = await this.handleOneSendGetMultipleStates(collection, batch, channel_id, txId);
412+
responses.push(...resp);
413+
remainingKeys = remainingKeys.slice(this.maxSizeGetMultipleKeys);
414+
}
415+
416+
if (remainingKeys.length > 0) {
417+
const resp = await this.handleOneSendGetMultipleStates(collection, remainingKeys, channel_id, txId);
418+
responses.push(...resp);
419+
}
420+
421+
return responses.map(r => (r.length === 0 ? null : r));
422+
}
423+
424+
async handleOneSendGetMultipleStates(collection, keys, channel_id, txId) {
425+
const msgPb = new peer.GetStateMultiple();
426+
msgPb.setCollection(collection);
427+
msgPb.setKeysList(keys);
428+
429+
const msg = mapToChaincodeMessage({
430+
type: peer.ChaincodeMessage.Type.GET_STATE_MULTIPLE,
431+
payload: msgPb.serializeBinary(),
432+
txid: txId,
433+
channel_id: channel_id
434+
});
435+
436+
logger.debug('handleOneSendGetMultipleStates - keys:', keys);
437+
438+
const responseMsg = await this._askPeerAndListen(msg, 'GetMultipleStates');
439+
440+
if (responseMsg.getType() === peer.ChaincodeMessage.Type.RESPONSE) {
441+
const result = peer.GetStateMultipleResult.deserializeBinary(responseMsg.getPayload());
442+
return result.getValuesList();
443+
}
444+
445+
if (responseMsg.getType() === peer.ChaincodeMessage.Type.ERROR) {
446+
throw new Error(Buffer.from(responseMsg.getPayload()).toString());
447+
}
448+
449+
throw new Error(`Unexpected message type ${responseMsg.getType()} received`);
450+
}
451+
391452
async handlePutState(collection, key, value, channel_id, txId) {
392453
const msgPb = new peer.PutState();
393454
msgPb.setKey(key);

0 commit comments

Comments
 (0)