Skip to content

Commit

Permalink
Merge branch 'main' into BE-345-Search-data-by-block-number-or-transa…
Browse files Browse the repository at this point in the history
…ction-id

Signed-off-by: ArchanaArige <nigam_archana@yahoo.co.in>
  • Loading branch information
ArchanaArige authored May 29, 2023
2 parents 5a9d258 + 2d6bb44 commit fdbb517
Show file tree
Hide file tree
Showing 32 changed files with 1,040 additions and 377 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ RUN apk add --no-cache --virtual npm-deps python3 make g++ curl bash && \
rm -r /root/.cache

# install node-prune (https://github.com/tj/node-prune)
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin

RUN curl -sf https://gobinaries.com/tj/node-prune | sh
# install NPM dependencies
RUN npm install && npm run build && npm prune --production

Expand Down
14 changes: 14 additions & 0 deletions MAINTAINERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
Maintainers
===========

You can find out who's contributed recently just by looking at GitHub's [contributors list](https://github.com/hyperledger-labs/blockchain-explorer/graphs/contributors). But there are a few more things you ought to know about who maintains this code, and how they do it:

Make sure you read our contributor guidelines so you understand how we work and how to collaborate effectively. This includes instructions about pull request and code review protocols, and it explains what we mean by calling someone a "maintainer" in this file.

Be aware that individual folders in the project may have more specific maintainers; if you see another MAINTAINERS.md in a subfolder, that governs the tree below it.

A lot of interactions with maintainers take place on Discord. You'll need [Linux Foundation credentials](https://identity.linuxfoundation.org/) to comment there; creating them is self-service. The project name you want, once in Discord, is "[blockchain-explorer](https://discord.com/channels/905194001349627914/1039606111654920255)". Most of the team hangs out there during their work day; look for #Blockchain-explorer.

**Who To Contact**
For ordinary questions, we suggest you contact [active contributors](https://github.com/hyperledger-labs/blockchain-explorer/graphs/contributors) generically, on Discord [#blockchain-explorer](https://discord.com/channels/905194001349627914/1039606111654920255). If that doesn't get someone's attention, feel free to contact the contributors individually.

Maintainers are busy and delegate many decisions to other trusted contributors. However, it is appropriate to contact them if you have a complex design decision or a controversial PR.


**Active Maintainers**

| Name | GitHub |
Expand Down
123 changes: 96 additions & 27 deletions app/persistence/fabric/CRUDService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,51 @@ export class CRUDService {
* @param {*} txid
* @param {*} from
* @param {*} to
* @param {*} page
* @param {*} size
* @param {*} orgs
* @returns
* @memberof CRUDService
*/
getTxList(
async getTxList(
network_name: any,
channel_genesis_hash: any,
blockNum: any,
txid: any,
from: any,
to: any,
orgs: string
orgs: string,
page: number,
size: number
) {
var countOfTxns: number;
let sqlTxList = ` select t.creator_msp_id,t.txhash,t.type,t.chaincodename,t.createdt,channel.name as channelName from transactions as t
inner join channel on t.channel_genesis_hash=channel.channel_genesis_hash and t.network_name = channel.network_name where t.blockid >= $1 and t.id >= $2 and
t.channel_genesis_hash = $3 and t.network_name = $4 and t.createdt between $5 and $6 `;
const values = [blockNum, txid, channel_genesis_hash, network_name, from, to];

const values = [blockNum, txid, channel_genesis_hash, network_name, from, to, page, size];
if (page == 1) {
let sqlTxCount: string;
const filterValues = [blockNum, txid, channel_genesis_hash, network_name, from, to];
sqlTxCount = ` select count(*) from transactions as t inner join channel on t.channel_genesis_hash=channel.channel_genesis_hash and t.network_name = channel.network_name
where t.blockid >= $1 and t.id >= $2 and t.channel_genesis_hash = $3 and t.network_name = $4 and t.createdt between $5 and $6 `
if (orgs && orgs.length > 0) {
sqlTxCount += ' and t.creator_msp_id = ANY($7)';
filterValues.push(orgs);
}
countOfTxns = await this.sql.getRowsCountBySQlQuery(sqlTxCount, filterValues)
}
if (orgs && orgs.length > 0) {
sqlTxList += ' and t.creator_msp_id = ANY($7)';
sqlTxList += ' and t.creator_msp_id = ANY($9)';
values.push(orgs);
}
sqlTxList += ' order by t.createdt desc';
sqlTxList += ' order by t.createdt desc LIMIT $8 OFFSET (($7 - 1) * $8)';
let txnsData = await this.sql.getRowsBySQlQuery(sqlTxList, values);
let response = {
txnsData: txnsData,
noOfpages: Math.ceil(countOfTxns / size)
}

return this.sql.getRowsBySQlQuery(sqlTxList, values);
return response;
}

/**
Expand All @@ -115,40 +135,89 @@ export class CRUDService {
*
* @param {*} channel_genesis_hash
* @param {*} blockNum
* @param {*} txid
* @param {*} from
* @param {*} to
* @param {*} page
* @param {*} size
* @param {*} orgs
* @returns
* @memberof CRUDService
*/
getBlockAndTxList(
async getBlockAndTxList(
network_name: any,
channel_genesis_hash: any,
blockNum: any,
from: any,
to: any,
orgs: string[]
orgs: string[],
page: number,
size: number
) {
const values = [channel_genesis_hash, network_name, from, to];
let byOrgs = '';
var countOfBlocks: number;
let byOrgs = ' ';
const values = [channel_genesis_hash, network_name, from, to, page, size];
if (orgs && orgs.length > 0) {
values.push(orgs);
byOrgs = ' and creator_msp_id = ANY($5)';
byOrgs = ' and creator_msp_id = ANY($7)';
}

logger.debug('getBlockAndTxList.byOrgs ', byOrgs);

const sqlBlockTxList = `select a.* from (
select (select c.name from channel c where c.channel_genesis_hash =$1 and c.network_name = $2)
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (
SELECT array_agg(txhash) as txhash FROM transactions where blockid = blocks.blocknum ${byOrgs} and
channel_genesis_hash = $1 and network_name = $2 and createdt between $3 and $4) from blocks where
blocks.channel_genesis_hash =$1 and blocks.network_name = $2 and blocknum >= 0 and blocks.createdt between $3 and $4
order by blocks.blocknum desc) a where a.txhash IS NOT NULL`;

logger.debug('sqlBlockTxList ', sqlBlockTxList);

return this.sql.getRowsBySQlQuery(sqlBlockTxList, values);
let sqlBlockTxList;
if(orgs == null || orgs.length == 0 ) {
sqlBlockTxList = `SELECT a.* FROM (
SELECT (SELECT c.name FROM channel c WHERE c.channel_genesis_hash =$1 AND c.network_name = $2)
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (
SELECT array_agg(txhash) as txhash FROM transactions WHERE blockid = blocks.blocknum ${byOrgs} AND
channel_genesis_hash = $1 AND network_name = $2 AND createdt between $3 AND $4) FROM blocks WHERE
blocks.channel_genesis_hash =$1 AND blocks.network_name = $2 AND blocknum >= 0 AND blocks.createdt between $3 AND $4
ORDER BY blocks.blocknum desc) a WHERE a.txhash IS NOT NULL LIMIT $6 OFFSET (($5 - 1) * $6)`;
} else {
sqlBlockTxList =`SELECT c.name AS channelname,
b.blocknum, b.txcount, b.datahash, b.blockhash, b.prehash,b.createdt, b.blksize,
array_agg(t.txhash) AS txhash
FROM channel c
INNER JOIN blocks b ON b.channel_genesis_hash = c.channel_genesis_hash AND
b.network_name = c.network_name
INNER JOIN transactions t ON t.blockid = b.blocknum AND t.channel_genesis_hash = c.channel_genesis_hash
AND t.network_name = c.network_name AND t.createdt between $3 and $4 = c.createdt between $3 and $4
AND t.creator_msp_id IS NOT NULL AND t.creator_msp_id != ' ' AND length(t.creator_msp_id) > 0
WHERE c.channel_genesis_hash =$1 AND c.network_name = $2 AND b.blocknum >= 0 ${byOrgs} AND b.createdt between $3 and $4
GROUP BY c.name, b.blocknum, b.txcount, b.datahash, b.blockhash, b.prehash,b.createdt, b.blksize
ORDER BY b.blocknum DESC
LIMIT $6 OFFSET (($5 - 1) * $6)`;
}
if (page == 1) {
let sqlBlockTxCount: string;
let byOrgs = ' ';
const filterValues = [channel_genesis_hash, network_name, from, to];
if (orgs && orgs.length > 0) {
filterValues.push(orgs);
byOrgs = ' and creator_msp_id = ANY($5)';
}
if(orgs == null || orgs.length == 0 ) {
sqlBlockTxCount = `SELECT COUNT(DISTINCT blocks.blocknum) FROM blocks
JOIN transactions ON blocks.blocknum = transactions.blockid
WHERE blockid = blocks.blocknum ${byOrgs} AND
blocknum >= 0 AND blocks.channel_genesis_hash = $1 AND blocks.network_name = $2 AND
blocks.createdt between $3 AND $4`
} else {
sqlBlockTxCount = `SELECT COUNT(DISTINCT blocks.blocknum) FROM blocks
JOIN transactions ON blocks.blocknum = transactions.blockid
WHERE blockid = blocks.blocknum ${byOrgs}
AND blocks.channel_genesis_hash = $1 and blocks.network_name = $2 AND blocks.createdt between $3 AND $4
AND transactions.creator_msp_id IS NOT NULL AND transactions.creator_msp_id != ' ' AND length(creator_msp_id) > 0`
}
countOfBlocks = await this.sql.getRowsCountBySQlQuery(
sqlBlockTxCount,
filterValues
);
}
let blocksData = await this.sql.getRowsBySQlQuery(sqlBlockTxList, values);
let noOfpages = Math.ceil(countOfBlocks / size);
let response = {
blocksData: blocksData,
noOfpages: noOfpages
};
return response;
}

/**
Expand Down Expand Up @@ -485,7 +554,7 @@ export class CRUDService {
* @returns
* @memberof CRUDService
*/
async getBlockByBlocknum(network_name:any, channel_genesis_hash:any, blockNo:any) {
async getBlockByBlocknum(network_name: any, channel_genesis_hash: any, blockNo: any) {
const sqlBlockTxList = `select a.* from (
select (select c.name from channel c where c.channel_genesis_hash =$1 and c.network_name = $2)
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (
Expand Down
4 changes: 2 additions & 2 deletions app/persistence/fabric/MetricService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class MetricService {
*/
getChaincodeCount(network_name: any, channel_genesis_hash: any) {
return this.sql.getRowsBySQlCase(
'select count(1) c from chaincodes where channel_genesis_hash=$1 and network_name=$2 ',
'select count(DISTINCT name) c from chaincodes where channel_genesis_hash=$1 and network_name=$2 ',
[channel_genesis_hash, network_name]
);
}
Expand Down Expand Up @@ -701,7 +701,7 @@ export class MetricService {
getTxByOrgs(network_name: any, channel_genesis_hash: any) {
const sqlPerOrg = ` select count(creator_msp_id), creator_msp_id
from transactions
where channel_genesis_hash =$1 and network_name=$2
where Trim(creator_msp_id) > '' and channel_genesis_hash =$1 and network_name=$2
group by creator_msp_id`;

return this.sql.getRowsBySQlQuery(sqlPerOrg, [
Expand Down
24 changes: 24 additions & 0 deletions app/persistence/postgreSQL/PgService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,28 @@ export class PgService {
});
});
}

/**
*
*
* @param {*} sql
* @param {*} values
* @returns
* @memberof PgService
*/
getRowsCountBySQlQuery(sql, values): Promise<number> {
return new Promise((resolve, reject) => {
this.client.query(sql, values, (err, res) => {
if (err) {
reject(err);
return;
}
logger.debug(`the getRowsCountBySQlQuery ${res}`);
if (res) {
resolve(res.rows[0].count);
}
});
});
}

}
16 changes: 11 additions & 5 deletions app/platform/fabric/sync/SyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,15 +553,19 @@ export class SyncServices {
}
let envelope_signature = txObj.signature;
if (envelope_signature !== undefined) {
envelope_signature = Buffer.from(envelope_signature).toString('hex');
envelope_signature = Buffer.from(
JSON.stringify(envelope_signature)
).toString('hex');
}
let payload_extension = txObj.payload.header.channel_header.extension;
if (payload_extension !== undefined) {
payload_extension = Buffer.from(payload_extension).toString('hex');
payload_extension = Buffer.from(JSON.stringify(payload_extension)).toString(
'hex'
);
}
let creator_nonce = txObj.payload.header.signature_header.nonce;
if (creator_nonce !== undefined) {
creator_nonce = Buffer.from(creator_nonce).toString('hex');
creator_nonce = Buffer.from(JSON.stringify(creator_nonce)).toString('hex');
}
/* eslint-disable */
const creator_id_bytes =
Expand Down Expand Up @@ -598,14 +602,16 @@ export class SyncServices {
for (const input of chaincode_proposal_input) {
inputs =
(inputs === '' ? inputs : `${inputs},`) +
Buffer.from(input).toString('hex');
Buffer.from(JSON.stringify(input)).toString('hex');
}
chaincode_proposal_input = inputs;
}
endorser_signature =
txObj.payload.data.actions[0].payload.action.endorsements[0].signature;
if (endorser_signature !== undefined) {
endorser_signature = Buffer.from(endorser_signature).toString('hex');
endorser_signature = Buffer.from(
JSON.stringify(endorser_signature)
).toString('hex');
}
payload_proposal_hash = txObj.payload.data.actions[0].payload.action.proposal_response_payload.proposal_hash.toString(
'hex'
Expand Down
56 changes: 37 additions & 19 deletions app/rest/dbroutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ export function dbroutes(router: Router, platform: Platform) {
* GET /txList/
* curl -i 'http://<host>:<port>/txList/<channel_genesis_hash>/<blocknum>/<txid>/<limitrows>/<offset>'
* Response:
* {'rows':[{'id':56,'channelname':'mychannel','blockid':24,
* {'rows':{"txnsData": [{'id':56,'channelname':'mychannel','blockid':24,
* 'txhash':'c42c4346f44259628e70d52c672d6717d36971a383f18f83b118aaff7f4349b8',
* 'createdt':'2018-03-09T19:40:59.000Z','chaincodename':'mycc'}]}
* 'createdt':'2018-03-09T19:40:59.000Z','chaincodename':'mycc'}], "noOfpages": 1}
*/
router.get(
'/txList/:channel_genesis_hash/:blocknum/:txid',
Expand All @@ -139,22 +139,31 @@ export function dbroutes(router: Router, platform: Platform) {
req.query.from as string,
req.query.to as string
);
const { page, size } = req.query;
if (isNaN(txid)) {
txid = 0;
}
if (channel_genesis_hash) {
const extReq = (req as unknown) as ExtRequest;
dbCrudService
let data = await dbCrudService
.getTxList(
extReq.network,
channel_genesis_hash,
blockNum,
txid,
from,
to,
orgs
orgs,
page,
size
)
.then(handleResult(req, res));
if (data) {
return res.send({
status: 200,
rows: data
});
}
return requtil.notFound(req, res);
} else {
return requtil.invalidRequest(req, res);
}
Expand Down Expand Up @@ -229,9 +238,9 @@ export function dbroutes(router: Router, platform: Platform) {
* GET /blockAndTxList
* curl -i 'http://<host>:<port>/blockAndTxList/channel_genesis_hash/<blockNum>/<limitrows>/<offset>'
* Response:
* {'rows':[{'id':51,'blocknum':50,'datahash':'374cceda1c795e95fc31af8f137feec8ab6527b5d6c85017dd8088a456a68dee',
* {'rows': { "blocksData":[{'id':51,'blocknum':50,'datahash':'374cceda1c795e95fc31af8f137feec8ab6527b5d6c85017dd8088a456a68dee',
* 'prehash':'16e76ca38975df7a44d2668091e0d3f05758d6fbd0aab76af39f45ad48a9c295','channelname':'mychannel','txcount':1,
* 'createdt':'2018-03-13T15:58:45.000Z','txhash':['6740fb70ed58d5f9c851550e092d08b5e7319b526b5980a984b16bd4934b87ac']}]}
* 'createdt':'2018-03-13T15:58:45.000Z','txhash':['6740fb70ed58d5f9c851550e092d08b5e7319b526b5980a984b16bd4934b87ac']}], "noOfpages": 1}
*/
router.get(
'/blockAndTxList/:channel_genesis_hash/:blocknum',
Expand All @@ -243,20 +252,29 @@ export function dbroutes(router: Router, platform: Platform) {
req.query.from as string,
req.query.to as string
);
if (channel_genesis_hash && !isNaN(blockNum)) {
const { page, size } = req.query;
if (channel_genesis_hash ) {
const extReq = (req as unknown) as ExtRequest;
return dbCrudService
.getBlockAndTxList(
extReq.network,
channel_genesis_hash,
blockNum,
from,
to,
orgs
)
.then(handleResult(req, res));
let data = await dbCrudService.getBlockAndTxList(
extReq.network,
channel_genesis_hash,
blockNum,
from,
to,
orgs,
page,
size
);
if (data) {
return res.send({
status: 200,
rows: data
});
}
return requtil.notFound(req, res);
} else {
return requtil.invalidRequest(req, res);
}
return requtil.invalidRequest(req, res);
}
);

Expand Down
Loading

0 comments on commit fdbb517

Please sign in to comment.