Skip to content

Commit

Permalink
[FAB-6480] NodeSDK - readable block numbers
Browse files Browse the repository at this point in the history
Change the block decode to use a string for unit64
numbers so that user does not have to see the high
and low of the 'Long' object

Change-Id: Id01e65b7d1afcd960a52b5754f9081fe0160789e
Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
  • Loading branch information
harrisob committed Oct 17, 2017
1 parent 07a6323 commit ab7634b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
19 changes: 10 additions & 9 deletions fabric-client/lib/BlockDecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ rule
var block = {};
try {
block.header = {
number: proto_block.header.number,
number: proto_block.header.number.toString(),
previous_hash: proto_block.header.previous_hash.toString('hex'),
data_hash: proto_block.header.data_hash.toString('hex')
};
Expand Down Expand Up @@ -548,7 +548,7 @@ payload -- {}

function decodeBlockHeader(proto_block_header) {
var block_header = {};
block_header.number = proto_block_header.getNumber();
block_header.number = proto_block_header.getNumber().toString();
block_header.previous_hash = proto_block_header.getPreviousHash().toBuffer().toString('hex');
block_header.data_hash = proto_block_header.getDataHash().toBuffer().toString('hex');

Expand Down Expand Up @@ -614,7 +614,7 @@ function decodeLastConfigSequenceNumber(metadata_bytes) {
if (metadata_bytes) {
var proto_metadata = _commonProto.Metadata.decode(metadata_bytes);
var proto_last_config = _commonProto.LastConfig.decode(proto_metadata.getValue());
last_config.value.index = proto_last_config.getIndex();
last_config.value.index = proto_last_config.getIndex().toString(); //unit64
last_config.signatures = decodeMetadataValueSignatures(proto_metadata.signatures);
}
return last_config;
Expand Down Expand Up @@ -701,7 +701,7 @@ function decodeConfigEnvelope(config_envelope_bytes) {

function decodeConfig(proto_config) {
var config = {};
config.sequence = proto_config.getSequence();
config.sequence = proto_config.getSequence().toString(); //unit64
config.channel_group = decodeConfigGroup(proto_config.getChannelGroup());
config.type = proto_config.getType();

Expand Down Expand Up @@ -811,7 +811,7 @@ function decodeConfigValue(proto_config_value) {
break;
case 'ChannelRestrictions':
var proto_channel_restrictions = _ordererConfigurationProto.ChannelRestrictions.decode(proto_config_value.value.value);
config_value.value.max_count = proto_channel_restrictions.getMaxCount(); //unit64
config_value.value.max_count = proto_channel_restrictions.getMaxCount().toString(); //unit64
break;
case 'CreationPolicy':
var proto_creation_policy = _ordererConfigurationProto.CreationPolicy.decode(proto_config_value.value.value);
Expand Down Expand Up @@ -1092,7 +1092,7 @@ function decodeChannelHeader(header_bytes) {
channel_header.timestamp = timeStampToDate(proto_channel_header.getTimestamp()).toString();
channel_header.channel_id = proto_channel_header.getChannelId();
channel_header.tx_id = proto_channel_header.getTxId();
channel_header.epoch = proto_channel_header.getEpoch().toInt();
channel_header.epoch = proto_channel_header.getEpoch().toString(); //unit64
//TODO need to decode this
channel_header.extension = proto_channel_header.getExtension().toBuffer();

Expand Down Expand Up @@ -1251,8 +1251,8 @@ function decodeKVRead(proto_kv_read) {
let proto_version = proto_kv_read.getVersion();
if (proto_version) {
kv_read.version = {};
kv_read.version.block_num = proto_version.getBlockNum();
kv_read.version.tx_num = proto_version.getTxNum();
kv_read.version.block_num = proto_version.getBlockNum().toString();
kv_read.version.tx_num = proto_version.getTxNum().toString();
} else {
kv_read.version = null;
}
Expand All @@ -1272,7 +1272,8 @@ function decodeRangeQueryInfo(proto_range_query_info) {
if (proto_raw_reads) {
range_query_info.reads_info.kv_reads = [];
for (let i in proto_raw_reads.kv_reads) {
range_query_info.reads_info.kv_reads.push(proto_raw_reads.kv_reads[i]);
let kv_read = decodeKVRead(proto_raw_reads.kv_reads[i]);
range_query_info.reads_info.kv_reads.push(kv_read);
}
}
// or QueryReadsMerkleSummary
Expand Down
34 changes: 19 additions & 15 deletions test/integration/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ test('\n\n***** use the network configuration file *****\n\n', function(t) {
});

async function looper(t) {
global.gc();
heapdump.writeSnapshot();
//global.gc();
//heapdump.writeSnapshot();
var skip = getArg('skipcreate', false);
if(skip === 'true') skip = true;

Expand All @@ -72,12 +72,12 @@ async function looper(t) {
logger.info('*********************************************\n');
await actions(t);
if(i==0) {
global.gc();
heapdump.writeSnapshot();
//global.gc();
//heapdump.writeSnapshot();
}
}
global.gc();
heapdump.writeSnapshot();
//global.gc();
//heapdump.writeSnapshot();
}

function getArg(arg_name, default_value) {
Expand Down Expand Up @@ -496,49 +496,53 @@ async function actions(t) {
t.fail('Failed to find our chaincode in the result list');
}

results = await channel.queryBlock(0);
logger.debug(' queryBlock ::%j',results);
t.equals('0', results.header.number, 'Should be able to find our block number');

results = await channel.queryBlock(1);
logger.debug(' queryBlock ::%j',results);
t.equals(1, results.header.number.low, 'Should be able to find our block number');
t.equals('1', results.header.number, 'Should be able to find our block number');

results = await channel.queryInfo();
logger.debug(' queryInfo ::%j',results);
t.pass('Successfully got the block height :: '+ results.height.low);
t.pass('Successfully got the block height :: '+ results.height);

results = await channel.queryBlockByHash(results.previousBlockHash);
logger.debug(' queryBlockHash ::%j',results);
t.pass('Successfully got block by hash ::'+ results.header.number.low);
t.pass('Successfully got block by hash ::'+ results.header.number);

results = await channel.queryTransaction(query_tx_id);
logger.debug(' queryTransaction ::%j',results);
t.equals(0, results.validationCode, 'Should be able to find our transaction validationCode');

results = await channel.queryBlock(1,'peer0.org1.example.com');
logger.debug(' queryBlock ::%j',results);
t.equals(1, results.header.number.low, 'Should be able to find our block number with string peer name');
t.equals('1', results.header.number, 'Should be able to find our block number with string peer name');

results = await channel.queryInfo('peer0.org1.example.com');
logger.info(' queryInfo ::%j',results);
t.pass('Successfully got the block height :: '+ results.height.low);
t.pass('Successfully got the block height :: '+ results.height);

results = await channel.queryBlockByHash(results.previousBlockHash, 'peer0.org1.example.com');
logger.debug(' queryBlockHash ::%j',results);
t.pass('Successfully got block by hash ::'+ results.header.number.low);
t.pass('Successfully got block by hash ::'+ results.header.number);

results = await channel.queryTransaction(query_tx_id,'peer0.org1.example.com');
logger.debug(' queryTransaction ::%j',results);
t.equals(0, results.validationCode, 'Should be able to find our transaction validationCode with string peer name');

results = await channel.queryBlock(1,'peer0.org1.example.com', true);
logger.debug(' queryBlock ::%j',results);
t.equals(1, results.header.number.low, 'Should be able to find our block number by admin');
t.equals('1', results.header.number, 'Should be able to find our block number by admin');

results = await channel.queryInfo('peer0.org1.example.com', true);
logger.debug(' queryInfo ::%j',results);
t.pass('Successfully got the block height by admin:: '+ results.height.low);
t.pass('Successfully got the block height by admin:: '+ results.height);

results = await channel.queryBlockByHash(results.previousBlockHash, 'peer0.org1.example.com', true);
logger.debug(' queryBlockHash ::%j',results);
t.pass('Successfully got block by hash by admin ::' + results.header.number.low);
t.pass('Successfully got block by hash by admin ::' + results.header.number);

results = await channel.queryTransaction(query_tx_id,'peer0.org1.example.com', true);
logger.debug(' queryTransaction ::%j',results);
Expand Down
12 changes: 6 additions & 6 deletions test/integration/network-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ test('\n\n***** use the network configuration file *****\n\n', function(t) {
return channel.queryBlock(1);
}).then((results) => {
logger.debug(' queryBlock ::%j',results);
t.equals(1, results.header.number.low, 'Should be able to find our block number');
t.equals('1', results.header.number, 'Should be able to find our block number');

return channel.queryInfo();
}).then((results) => {
Expand All @@ -543,7 +543,7 @@ test('\n\n***** use the network configuration file *****\n\n', function(t) {
return channel.queryBlockByHash(results.previousBlockHash);
}).then((results) => {
logger.debug(' queryBlockHash ::%j',results);
t.equals(1, results.header.number.low, 'Should be able to find our block number by hash');
t.equals('1', results.header.number, 'Should be able to find our block number by hash');

return channel.queryTransaction(query_tx_id);
}).then((results) => {
Expand All @@ -553,7 +553,7 @@ test('\n\n***** use the network configuration file *****\n\n', function(t) {
return channel.queryBlock(1,'peer0.org1.example.com');
}).then((results) => {
logger.debug(' queryBlock ::%j',results);
t.equals(1, results.header.number.low, 'Should be able to find our block number with string peer name');
t.equals('1', results.header.number, 'Should be able to find our block number with string peer name');

return channel.queryInfo('peer0.org1.example.com');
}).then((results) => {
Expand All @@ -563,7 +563,7 @@ test('\n\n***** use the network configuration file *****\n\n', function(t) {
return channel.queryBlockByHash(results.previousBlockHash, 'peer0.org1.example.com');
}).then((results) => {
logger.debug(' queryBlockHash ::%j',results);
t.equals(1, results.header.number.low, 'Should be able to find our block number by hash with string peer name');
t.equals('1', results.header.number, 'Should be able to find our block number by hash with string peer name');

return channel.queryTransaction(query_tx_id,'peer0.org1.example.com');
}).then((results) => {
Expand All @@ -573,7 +573,7 @@ test('\n\n***** use the network configuration file *****\n\n', function(t) {
return channel.queryBlock(1,'peer0.org1.example.com', true);
}).then((results) => {
logger.debug(' queryBlock ::%j',results);
t.equals(1, results.header.number.low, 'Should be able to find our block number by admin');
t.equals('1', results.header.number, 'Should be able to find our block number by admin');

return channel.queryInfo('peer0.org1.example.com', true);
}).then((results) => {
Expand All @@ -583,7 +583,7 @@ test('\n\n***** use the network configuration file *****\n\n', function(t) {
return channel.queryBlockByHash(results.previousBlockHash, 'peer0.org1.example.com', true);
}).then((results) => {
logger.debug(' queryBlockHash ::%j',results);
t.equals(1, results.header.number.low, 'Should be able to find our block number by hash by admin');
t.equals('1', results.header.number, 'Should be able to find our block number by hash by admin');

return channel.queryTransaction(query_tx_id,'peer0.org1.example.com', true);
}).then((results) => {
Expand Down
16 changes: 8 additions & 8 deletions test/unit/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ test('\n\n*** BlockDecoder test for readwrite sets', (t) => {

t.equal('test1',results_json.ns_rwset[0].namespace, ' check results_json.ns_rwset[0].namespace');
t.equal('read key',results_json.ns_rwset[0].rwset.reads[0].key, ' check results_json.ns_rwset[0].rwset.reads[0].key');
t.equal(12,results_json.ns_rwset[0].rwset.reads[0].version.block_num.low, ' check results_json.ns_rwset[0].rwset.reads[0].version.block_num');
t.equal(21,results_json.ns_rwset[0].rwset.reads[0].version.tx_num.low, ' check results_json.ns_rwset[0].rwset.reads[0].version.tx_num');
t.equal('12',results_json.ns_rwset[0].rwset.reads[0].version.block_num, ' check results_json.ns_rwset[0].rwset.reads[0].version.block_num');
t.equal('21',results_json.ns_rwset[0].rwset.reads[0].version.tx_num, ' check results_json.ns_rwset[0].rwset.reads[0].version.tx_num');

t.equal('start',results_json.ns_rwset[0].rwset.range_queries_info[0].start_key, ' check results_json.ns_rwset[0].rwset.range_queries_info[0].start_key');
t.equal('end',results_json.ns_rwset[0].rwset.range_queries_info[0].end_key, ' check results_json.ns_rwset[0].rwset.range_queries_info[0].end_key');
Expand Down Expand Up @@ -121,11 +121,11 @@ test('\n\n*** BlockDecoder test for readwrite sets', (t) => {

t.equal('test2',results_json.ns_rwset[0].namespace, ' check results_json.ns_rwset[0].namespace');
t.equal('read key',results_json.ns_rwset[0].rwset.reads[0].key, ' check results_json.ns_rwset[0].rwset.reads[0].key');
t.equal(12,results_json.ns_rwset[0].rwset.reads[0].version.block_num.low, ' check results_json.ns_rwset[0].rwset.reads[0].version.block_num');
t.equal(21,results_json.ns_rwset[0].rwset.reads[0].version.tx_num.low, ' check results_json.ns_rwset[0].rwset.reads[0].version.tx_num');
t.equal('12',results_json.ns_rwset[0].rwset.reads[0].version.block_num, ' check results_json.ns_rwset[0].rwset.reads[0].version.block_num');
t.equal('21',results_json.ns_rwset[0].rwset.reads[0].version.tx_num, ' check results_json.ns_rwset[0].rwset.reads[0].version.tx_num');
t.equal('range query key',results_json.ns_rwset[0].rwset.range_queries_info[0].reads_info.kv_reads[0].key,' check results_json.ns_rwset[0].rwset.reads[0].range_queries_info[0].reads_info.kv_reads[0].key');
t.equal(13,results_json.ns_rwset[0].rwset.range_queries_info[0].reads_info.kv_reads[0].version.block_num.low, ' check results_json.ns_rwset[0].rwset.reads[0].range_queries_info[0].reads_info.kv_reads[0].version.tx_num');
t.equal(31,results_json.ns_rwset[0].rwset.range_queries_info[0].reads_info.kv_reads[0].version.tx_num.low, ' check results_json.ns_rwset[0].rwset.reads[0].range_queries_info[0].reads_info.kv_reads[0].version.tx_num.');
t.equal('13',results_json.ns_rwset[0].rwset.range_queries_info[0].reads_info.kv_reads[0].version.block_num, ' check results_json.ns_rwset[0].rwset.reads[0].range_queries_info[0].reads_info.kv_reads[0].version.block_num');
t.equal('31',results_json.ns_rwset[0].rwset.range_queries_info[0].reads_info.kv_reads[0].version.tx_num, ' check results_json.ns_rwset[0].rwset.reads[0].range_queries_info[0].reads_info.kv_reads[0].version.tx_num.');

// take range query with query reads out and add range query with QueryReadsMerkleSummary
range_query_info_proto.setRawReads(null);
Expand All @@ -150,8 +150,8 @@ test('\n\n*** BlockDecoder test for readwrite sets', (t) => {

t.equal('test3',results_json.ns_rwset[0].namespace, ' check results_json.ns_rwset[0].namespace');
t.equal('read key',results_json.ns_rwset[0].rwset.reads[0].key, ' check results_json.ns_rwset[0].rwset.reads[0].key');
t.equal(12,results_json.ns_rwset[0].rwset.reads[0].version.block_num.low, ' check results_json.ns_rwset[0].rwset.reads[0].version.block_num');
t.equal(21,results_json.ns_rwset[0].rwset.reads[0].version.tx_num.low, ' check results_json.ns_rwset[0].rwset.reads[0].version.tx_num');
t.equal('12',results_json.ns_rwset[0].rwset.reads[0].version.block_num, ' check results_json.ns_rwset[0].rwset.reads[0].version.block_num');
t.equal('21',results_json.ns_rwset[0].rwset.reads[0].version.tx_num, ' check results_json.ns_rwset[0].rwset.reads[0].version.tx_num');
t.equal(44,results_json.ns_rwset[0].rwset.range_queries_info[0].reads_merkle_hashes.max_degree,' check results_json.ns_rwset[0].rwset.range_queries_info[0].reads_merkle_hashes.max_degree');
t.equal(33,results_json.ns_rwset[0].rwset.range_queries_info[0].reads_merkle_hashes.max_level,' check results_json.ns_rwset[0].rwset.range_queries_info[0].reads_merkle_hashes.max_level');
t.equal('some hash',results_json.ns_rwset[0].rwset.range_queries_info[0].reads_merkle_hashes.max_level_hashes[0].toString('utf8'),' check results_json.ns_rwset[0].rwset.range_queries_info[0].reads_merkle_hashes.max_level_hashes[0]');
Expand Down

0 comments on commit ab7634b

Please sign in to comment.