Skip to content

Commit fdbb517

Browse files
authored
Merge branch 'main' into BE-345-Search-data-by-block-number-or-transaction-id
Signed-off-by: ArchanaArige <[email protected]>
2 parents 5a9d258 + 2d6bb44 commit fdbb517

32 files changed

+1040
-377
lines changed

Dockerfile

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ RUN apk add --no-cache --virtual npm-deps python3 make g++ curl bash && \
2525
rm -r /root/.cache
2626

2727
# install node-prune (https://github.com/tj/node-prune)
28-
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin
29-
28+
RUN curl -sf https://gobinaries.com/tj/node-prune | sh
3029
# install NPM dependencies
3130
RUN npm install && npm run build && npm prune --production
3231

MAINTAINERS.md

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
Maintainers
55
===========
66

7+
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:
8+
9+
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.
10+
11+
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.
12+
13+
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.
14+
15+
**Who To Contact**
16+
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.
17+
18+
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.
19+
20+
721
**Active Maintainers**
822

923
| Name | GitHub |

app/persistence/fabric/CRUDService.ts

+96-27
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,51 @@ export class CRUDService {
8282
* @param {*} txid
8383
* @param {*} from
8484
* @param {*} to
85+
* @param {*} page
86+
* @param {*} size
8587
* @param {*} orgs
8688
* @returns
8789
* @memberof CRUDService
8890
*/
89-
getTxList(
91+
async getTxList(
9092
network_name: any,
9193
channel_genesis_hash: any,
9294
blockNum: any,
9395
txid: any,
9496
from: any,
9597
to: any,
96-
orgs: string
98+
orgs: string,
99+
page: number,
100+
size: number
97101
) {
102+
var countOfTxns: number;
98103
let sqlTxList = ` select t.creator_msp_id,t.txhash,t.type,t.chaincodename,t.createdt,channel.name as channelName from transactions as t
99104
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
100105
t.channel_genesis_hash = $3 and t.network_name = $4 and t.createdt between $5 and $6 `;
101-
const values = [blockNum, txid, channel_genesis_hash, network_name, from, to];
102-
106+
const values = [blockNum, txid, channel_genesis_hash, network_name, from, to, page, size];
107+
if (page == 1) {
108+
let sqlTxCount: string;
109+
const filterValues = [blockNum, txid, channel_genesis_hash, network_name, from, to];
110+
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
111+
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 `
112+
if (orgs && orgs.length > 0) {
113+
sqlTxCount += ' and t.creator_msp_id = ANY($7)';
114+
filterValues.push(orgs);
115+
}
116+
countOfTxns = await this.sql.getRowsCountBySQlQuery(sqlTxCount, filterValues)
117+
}
103118
if (orgs && orgs.length > 0) {
104-
sqlTxList += ' and t.creator_msp_id = ANY($7)';
119+
sqlTxList += ' and t.creator_msp_id = ANY($9)';
105120
values.push(orgs);
106121
}
107-
sqlTxList += ' order by t.createdt desc';
122+
sqlTxList += ' order by t.createdt desc LIMIT $8 OFFSET (($7 - 1) * $8)';
123+
let txnsData = await this.sql.getRowsBySQlQuery(sqlTxList, values);
124+
let response = {
125+
txnsData: txnsData,
126+
noOfpages: Math.ceil(countOfTxns / size)
127+
}
108128

109-
return this.sql.getRowsBySQlQuery(sqlTxList, values);
129+
return response;
110130
}
111131

112132
/**
@@ -115,40 +135,89 @@ export class CRUDService {
115135
*
116136
* @param {*} channel_genesis_hash
117137
* @param {*} blockNum
138+
* @param {*} txid
118139
* @param {*} from
119140
* @param {*} to
141+
* @param {*} page
142+
* @param {*} size
120143
* @param {*} orgs
121144
* @returns
122145
* @memberof CRUDService
123146
*/
124-
getBlockAndTxList(
147+
async getBlockAndTxList(
125148
network_name: any,
126149
channel_genesis_hash: any,
127150
blockNum: any,
128151
from: any,
129152
to: any,
130-
orgs: string[]
153+
orgs: string[],
154+
page: number,
155+
size: number
131156
) {
132-
const values = [channel_genesis_hash, network_name, from, to];
133-
let byOrgs = '';
157+
var countOfBlocks: number;
158+
let byOrgs = ' ';
159+
const values = [channel_genesis_hash, network_name, from, to, page, size];
134160
if (orgs && orgs.length > 0) {
135161
values.push(orgs);
136-
byOrgs = ' and creator_msp_id = ANY($5)';
162+
byOrgs = ' and creator_msp_id = ANY($7)';
137163
}
138-
139-
logger.debug('getBlockAndTxList.byOrgs ', byOrgs);
140-
141-
const sqlBlockTxList = `select a.* from (
142-
select (select c.name from channel c where c.channel_genesis_hash =$1 and c.network_name = $2)
143-
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (
144-
SELECT array_agg(txhash) as txhash FROM transactions where blockid = blocks.blocknum ${byOrgs} and
145-
channel_genesis_hash = $1 and network_name = $2 and createdt between $3 and $4) from blocks where
146-
blocks.channel_genesis_hash =$1 and blocks.network_name = $2 and blocknum >= 0 and blocks.createdt between $3 and $4
147-
order by blocks.blocknum desc) a where a.txhash IS NOT NULL`;
148-
149-
logger.debug('sqlBlockTxList ', sqlBlockTxList);
150-
151-
return this.sql.getRowsBySQlQuery(sqlBlockTxList, values);
164+
let sqlBlockTxList;
165+
if(orgs == null || orgs.length == 0 ) {
166+
sqlBlockTxList = `SELECT a.* FROM (
167+
SELECT (SELECT c.name FROM channel c WHERE c.channel_genesis_hash =$1 AND c.network_name = $2)
168+
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (
169+
SELECT array_agg(txhash) as txhash FROM transactions WHERE blockid = blocks.blocknum ${byOrgs} AND
170+
channel_genesis_hash = $1 AND network_name = $2 AND createdt between $3 AND $4) FROM blocks WHERE
171+
blocks.channel_genesis_hash =$1 AND blocks.network_name = $2 AND blocknum >= 0 AND blocks.createdt between $3 AND $4
172+
ORDER BY blocks.blocknum desc) a WHERE a.txhash IS NOT NULL LIMIT $6 OFFSET (($5 - 1) * $6)`;
173+
} else {
174+
sqlBlockTxList =`SELECT c.name AS channelname,
175+
b.blocknum, b.txcount, b.datahash, b.blockhash, b.prehash,b.createdt, b.blksize,
176+
array_agg(t.txhash) AS txhash
177+
FROM channel c
178+
INNER JOIN blocks b ON b.channel_genesis_hash = c.channel_genesis_hash AND
179+
b.network_name = c.network_name
180+
INNER JOIN transactions t ON t.blockid = b.blocknum AND t.channel_genesis_hash = c.channel_genesis_hash
181+
AND t.network_name = c.network_name AND t.createdt between $3 and $4 = c.createdt between $3 and $4
182+
AND t.creator_msp_id IS NOT NULL AND t.creator_msp_id != ' ' AND length(t.creator_msp_id) > 0
183+
WHERE c.channel_genesis_hash =$1 AND c.network_name = $2 AND b.blocknum >= 0 ${byOrgs} AND b.createdt between $3 and $4
184+
GROUP BY c.name, b.blocknum, b.txcount, b.datahash, b.blockhash, b.prehash,b.createdt, b.blksize
185+
ORDER BY b.blocknum DESC
186+
LIMIT $6 OFFSET (($5 - 1) * $6)`;
187+
}
188+
if (page == 1) {
189+
let sqlBlockTxCount: string;
190+
let byOrgs = ' ';
191+
const filterValues = [channel_genesis_hash, network_name, from, to];
192+
if (orgs && orgs.length > 0) {
193+
filterValues.push(orgs);
194+
byOrgs = ' and creator_msp_id = ANY($5)';
195+
}
196+
if(orgs == null || orgs.length == 0 ) {
197+
sqlBlockTxCount = `SELECT COUNT(DISTINCT blocks.blocknum) FROM blocks
198+
JOIN transactions ON blocks.blocknum = transactions.blockid
199+
WHERE blockid = blocks.blocknum ${byOrgs} AND
200+
blocknum >= 0 AND blocks.channel_genesis_hash = $1 AND blocks.network_name = $2 AND
201+
blocks.createdt between $3 AND $4`
202+
} else {
203+
sqlBlockTxCount = `SELECT COUNT(DISTINCT blocks.blocknum) FROM blocks
204+
JOIN transactions ON blocks.blocknum = transactions.blockid
205+
WHERE blockid = blocks.blocknum ${byOrgs}
206+
AND blocks.channel_genesis_hash = $1 and blocks.network_name = $2 AND blocks.createdt between $3 AND $4
207+
AND transactions.creator_msp_id IS NOT NULL AND transactions.creator_msp_id != ' ' AND length(creator_msp_id) > 0`
208+
}
209+
countOfBlocks = await this.sql.getRowsCountBySQlQuery(
210+
sqlBlockTxCount,
211+
filterValues
212+
);
213+
}
214+
let blocksData = await this.sql.getRowsBySQlQuery(sqlBlockTxList, values);
215+
let noOfpages = Math.ceil(countOfBlocks / size);
216+
let response = {
217+
blocksData: blocksData,
218+
noOfpages: noOfpages
219+
};
220+
return response;
152221
}
153222

154223
/**
@@ -485,7 +554,7 @@ export class CRUDService {
485554
* @returns
486555
* @memberof CRUDService
487556
*/
488-
async getBlockByBlocknum(network_name:any, channel_genesis_hash:any, blockNo:any) {
557+
async getBlockByBlocknum(network_name: any, channel_genesis_hash: any, blockNo: any) {
489558
const sqlBlockTxList = `select a.* from (
490559
select (select c.name from channel c where c.channel_genesis_hash =$1 and c.network_name = $2)
491560
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (

app/persistence/fabric/MetricService.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class MetricService {
2727
*/
2828
getChaincodeCount(network_name: any, channel_genesis_hash: any) {
2929
return this.sql.getRowsBySQlCase(
30-
'select count(1) c from chaincodes where channel_genesis_hash=$1 and network_name=$2 ',
30+
'select count(DISTINCT name) c from chaincodes where channel_genesis_hash=$1 and network_name=$2 ',
3131
[channel_genesis_hash, network_name]
3232
);
3333
}
@@ -701,7 +701,7 @@ export class MetricService {
701701
getTxByOrgs(network_name: any, channel_genesis_hash: any) {
702702
const sqlPerOrg = ` select count(creator_msp_id), creator_msp_id
703703
from transactions
704-
where channel_genesis_hash =$1 and network_name=$2
704+
where Trim(creator_msp_id) > '' and channel_genesis_hash =$1 and network_name=$2
705705
group by creator_msp_id`;
706706

707707
return this.sql.getRowsBySQlQuery(sqlPerOrg, [

app/persistence/postgreSQL/PgService.ts

+24
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,28 @@ export class PgService {
562562
});
563563
});
564564
}
565+
566+
/**
567+
*
568+
*
569+
* @param {*} sql
570+
* @param {*} values
571+
* @returns
572+
* @memberof PgService
573+
*/
574+
getRowsCountBySQlQuery(sql, values): Promise<number> {
575+
return new Promise((resolve, reject) => {
576+
this.client.query(sql, values, (err, res) => {
577+
if (err) {
578+
reject(err);
579+
return;
580+
}
581+
logger.debug(`the getRowsCountBySQlQuery ${res}`);
582+
if (res) {
583+
resolve(res.rows[0].count);
584+
}
585+
});
586+
});
587+
}
588+
565589
}

app/platform/fabric/sync/SyncService.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -553,15 +553,19 @@ export class SyncServices {
553553
}
554554
let envelope_signature = txObj.signature;
555555
if (envelope_signature !== undefined) {
556-
envelope_signature = Buffer.from(envelope_signature).toString('hex');
556+
envelope_signature = Buffer.from(
557+
JSON.stringify(envelope_signature)
558+
).toString('hex');
557559
}
558560
let payload_extension = txObj.payload.header.channel_header.extension;
559561
if (payload_extension !== undefined) {
560-
payload_extension = Buffer.from(payload_extension).toString('hex');
562+
payload_extension = Buffer.from(JSON.stringify(payload_extension)).toString(
563+
'hex'
564+
);
561565
}
562566
let creator_nonce = txObj.payload.header.signature_header.nonce;
563567
if (creator_nonce !== undefined) {
564-
creator_nonce = Buffer.from(creator_nonce).toString('hex');
568+
creator_nonce = Buffer.from(JSON.stringify(creator_nonce)).toString('hex');
565569
}
566570
/* eslint-disable */
567571
const creator_id_bytes =
@@ -598,14 +602,16 @@ export class SyncServices {
598602
for (const input of chaincode_proposal_input) {
599603
inputs =
600604
(inputs === '' ? inputs : `${inputs},`) +
601-
Buffer.from(input).toString('hex');
605+
Buffer.from(JSON.stringify(input)).toString('hex');
602606
}
603607
chaincode_proposal_input = inputs;
604608
}
605609
endorser_signature =
606610
txObj.payload.data.actions[0].payload.action.endorsements[0].signature;
607611
if (endorser_signature !== undefined) {
608-
endorser_signature = Buffer.from(endorser_signature).toString('hex');
612+
endorser_signature = Buffer.from(
613+
JSON.stringify(endorser_signature)
614+
).toString('hex');
609615
}
610616
payload_proposal_hash = txObj.payload.data.actions[0].payload.action.proposal_response_payload.proposal_hash.toString(
611617
'hex'

app/rest/dbroutes.ts

+37-19
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ export function dbroutes(router: Router, platform: Platform) {
124124
* GET /txList/
125125
* curl -i 'http://<host>:<port>/txList/<channel_genesis_hash>/<blocknum>/<txid>/<limitrows>/<offset>'
126126
* Response:
127-
* {'rows':[{'id':56,'channelname':'mychannel','blockid':24,
127+
* {'rows':{"txnsData": [{'id':56,'channelname':'mychannel','blockid':24,
128128
* 'txhash':'c42c4346f44259628e70d52c672d6717d36971a383f18f83b118aaff7f4349b8',
129-
* 'createdt':'2018-03-09T19:40:59.000Z','chaincodename':'mycc'}]}
129+
* 'createdt':'2018-03-09T19:40:59.000Z','chaincodename':'mycc'}], "noOfpages": 1}
130130
*/
131131
router.get(
132132
'/txList/:channel_genesis_hash/:blocknum/:txid',
@@ -139,22 +139,31 @@ export function dbroutes(router: Router, platform: Platform) {
139139
req.query.from as string,
140140
req.query.to as string
141141
);
142+
const { page, size } = req.query;
142143
if (isNaN(txid)) {
143144
txid = 0;
144145
}
145146
if (channel_genesis_hash) {
146147
const extReq = (req as unknown) as ExtRequest;
147-
dbCrudService
148+
let data = await dbCrudService
148149
.getTxList(
149150
extReq.network,
150151
channel_genesis_hash,
151152
blockNum,
152153
txid,
153154
from,
154155
to,
155-
orgs
156+
orgs,
157+
page,
158+
size
156159
)
157-
.then(handleResult(req, res));
160+
if (data) {
161+
return res.send({
162+
status: 200,
163+
rows: data
164+
});
165+
}
166+
return requtil.notFound(req, res);
158167
} else {
159168
return requtil.invalidRequest(req, res);
160169
}
@@ -229,9 +238,9 @@ export function dbroutes(router: Router, platform: Platform) {
229238
* GET /blockAndTxList
230239
* curl -i 'http://<host>:<port>/blockAndTxList/channel_genesis_hash/<blockNum>/<limitrows>/<offset>'
231240
* Response:
232-
* {'rows':[{'id':51,'blocknum':50,'datahash':'374cceda1c795e95fc31af8f137feec8ab6527b5d6c85017dd8088a456a68dee',
241+
* {'rows': { "blocksData":[{'id':51,'blocknum':50,'datahash':'374cceda1c795e95fc31af8f137feec8ab6527b5d6c85017dd8088a456a68dee',
233242
* 'prehash':'16e76ca38975df7a44d2668091e0d3f05758d6fbd0aab76af39f45ad48a9c295','channelname':'mychannel','txcount':1,
234-
* 'createdt':'2018-03-13T15:58:45.000Z','txhash':['6740fb70ed58d5f9c851550e092d08b5e7319b526b5980a984b16bd4934b87ac']}]}
243+
* 'createdt':'2018-03-13T15:58:45.000Z','txhash':['6740fb70ed58d5f9c851550e092d08b5e7319b526b5980a984b16bd4934b87ac']}], "noOfpages": 1}
235244
*/
236245
router.get(
237246
'/blockAndTxList/:channel_genesis_hash/:blocknum',
@@ -243,20 +252,29 @@ export function dbroutes(router: Router, platform: Platform) {
243252
req.query.from as string,
244253
req.query.to as string
245254
);
246-
if (channel_genesis_hash && !isNaN(blockNum)) {
255+
const { page, size } = req.query;
256+
if (channel_genesis_hash ) {
247257
const extReq = (req as unknown) as ExtRequest;
248-
return dbCrudService
249-
.getBlockAndTxList(
250-
extReq.network,
251-
channel_genesis_hash,
252-
blockNum,
253-
from,
254-
to,
255-
orgs
256-
)
257-
.then(handleResult(req, res));
258+
let data = await dbCrudService.getBlockAndTxList(
259+
extReq.network,
260+
channel_genesis_hash,
261+
blockNum,
262+
from,
263+
to,
264+
orgs,
265+
page,
266+
size
267+
);
268+
if (data) {
269+
return res.send({
270+
status: 200,
271+
rows: data
272+
});
273+
}
274+
return requtil.notFound(req, res);
275+
} else {
276+
return requtil.invalidRequest(req, res);
258277
}
259-
return requtil.invalidRequest(req, res);
260278
}
261279
);
262280

0 commit comments

Comments
 (0)