Skip to content

Commit

Permalink
Implement queryBlock by using system chaincode for query (qscc) (#109)
Browse files Browse the repository at this point in the history
* BE-749 Implement queryBlock by querying QSCC

Invoke "GetBlockByNumber" func in QSCC

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>

* BE-749 Remove unnecessary logs

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>
  • Loading branch information
nekia authored May 28, 2020
1 parent e60a7a4 commit 8e5af16
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
15 changes: 15 additions & 0 deletions app/platform/fabric/FabricClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,21 @@ class FabricClient {
}
}

/**
*
*
* @param {*} channel_genesis_hash
* @returns
* @memberof FabricClient
*/
getChannelNameByHash(channel_genesis_hash) {
for (const [channel_name, hash_name] of this.channelsGenHash.entries()) {
if (channel_genesis_hash === hash_name) {
return channel_name;
}
}
}

/**
*
*
Expand Down
8 changes: 2 additions & 6 deletions app/platform/fabric/Proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,11 @@ class Proxy {
*/
async getBlockByNumber(network_name, channel_genesis_hash, number) {
const client = this.platform.getClient(network_name);
const channel = client.getChannelByHash(channel_genesis_hash);
const channelName = client.getChannelNameByHash(channel_genesis_hash);
let block;

try {
block = await channel.queryBlock(
parseInt(number),
client.getDefaultPeer(),
true
);
block = await client.fabricGateway.queryBlock(channelName, parseInt(number));
} catch (e) {
logger.debug('queryBlock >> ', e);
}
Expand Down
17 changes: 15 additions & 2 deletions app/platform/fabric/gateway/FabricGateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

const { Wallets, Gateway } = require('fabric-network');
const { Client } = require('fabric-common');
const { BlockDecoder, Client } = require('fabric-common');
const fabprotos = require('fabric-protos');

const FabricCAServices = require('fabric-ca-client');
Expand Down Expand Up @@ -261,7 +261,20 @@ class FabricGateway {
const contract = network.getContract('cscc');
const result = await contract.evaluateTransaction('GetChannels');
const resultJson = fabprotos.protos.ChannelQueryResponse.decode(result);
logger.info('queryChannels :', resultJson);
return resultJson;
}

async queryBlock(channelName, blockNum) {
const network = await this.gateway.getNetwork(this.defaultChannelName);

// Get the contract from the network.
const contract = network.getContract('qscc');
const resultByte = await contract.evaluateTransaction(
'GetBlockByNumber',
channelName,
String(blockNum)
);
const resultJson = BlockDecoder.decode(resultByte);
return resultJson;
}
}
Expand Down
8 changes: 4 additions & 4 deletions app/platform/fabric/sync/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,10 @@ class SyncServices {
if (results) {
for (const result of results) {
// Get block by number
const block = await client
.getHFC_Client()
.getChannel(channel_name)
.queryBlock(result.missing_id, client.getDefaultPeer(), true);
const block = await client.fabricGateway.queryBlock(
channel_name,
result.missing_id
);
await this.processBlockEvent(client, block);
}
} else {
Expand Down

0 comments on commit 8e5af16

Please sign in to comment.