Skip to content

Commit

Permalink
FAB-10745 NodeSDK Handle missing ledger_height
Browse files Browse the repository at this point in the history
Added code to handle ledger height value that is not
a Long.

Change-Id: I5b0f851cc205ddb1aaa0ede3defeadc2d15d6886
Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
  • Loading branch information
harrisob committed Jun 21, 2018
1 parent 2954fcf commit 71d4ef4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
8 changes: 7 additions & 1 deletion fabric-client/lib/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const Orderer = require('./Orderer.js');
const BlockDecoder = require('./BlockDecoder.js');
const TransactionID = require('./TransactionID.js');
const grpc = require('grpc');
const Long = require('long');
const logger = sdk_utils.getLogger('Channel.js');
const MSPManager = require('./msp/msp-manager.js');
const Policy = require('./Policy.js');
Expand Down Expand Up @@ -1071,7 +1072,12 @@ const Channel = class {
// STATE
if (q_peer.state_info) {
const message_s = _gossipProto.GossipMessage.decode(q_peer.state_info.payload);
peer.ledger_height = message_s.state_info.properties.ledger_height;
if(message_s && message_s.state_info && message_s.state_info.properties && message_s.state_info.properties.ledger_height) {
peer.ledger_height = Long.fromValue(message_s.state_info.properties.ledger_height);
} else {
logger.debug('%s - did not find ledger_height', method);
peer.ledger_height =Long.fromValue(0);
}
logger.debug('%s - found ledger_height :%s', method, peer.ledger_height);
peer.chaincodes = [];
for (let index in message_s.state_info.properties.chaincodes) {
Expand Down
32 changes: 31 additions & 1 deletion fabric-client/lib/impl/DiscoveryEndorsementHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,42 @@ class DiscoveryEndorsementHandler extends api.EndorsementHandler {
found = preferred[peer.endpoint];
if(found) {
peer.ledger_height = Long.MAX_VALUE;
} else {
peer.ledger_height = new Long(peer.ledger_height);
}
un_sorted.push(peer);
}
}
logger.debug('%s - about to sort');
const sorted = un_sorted.sort((a,b)=>{
return -1 * a.ledger_height.compare(b.ledger_height);
logger.debug('%s - sorting descending');
if(!a || !b) {
return 0;
}
if(a.ledger_height && !b.ledger_height) {
logger.debug('%s - a exist (%s) - b does not exist', method, a.ledger_height);

return -1;
}
if( !a.ledger_height && b.ledger_height ) {
logger.debug('%s - a does not exist - b exist (%s)', method, b.ledger_height);

return 1;
}
if(!a && !b) {
logger.debug('%s - a does not exist - b does not exist', method);

return 0;
}
if(a.ledger_height && a.ledger_height.compare) {
const result = -1 * a.ledger_height.compare(b.ledger_height);
logger.debug('%s - compare result: %s for a:(%s) b:(%s) ', method, result, a.ledger_height.toString(), b.ledger_height.toString());

return result;
}
logger.debug('%s - compare not available (%s) (%s)', method, typeof(a.ledger_height), typeof(b.ledger_height));

return 1;
});
group.peers = sorted;
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/endorser-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const org3 = [
];

const chaincodes = [{name: 'example',version: 'v2'}];
const ledger_height = Long.fromValue(4);
const ledger_height = {low: 4, high: 0, unsigned: true};

const discovery_plan = {
msps: {
Expand Down

0 comments on commit 71d4ef4

Please sign in to comment.