Skip to content

Commit 413e7f5

Browse files
committed
BE-447 | fetch latest block time to get channels updation time
Signed-off-by: manishdas12 <[email protected]>
1 parent 476b8f3 commit 413e7f5

File tree

2 files changed

+161
-56
lines changed

2 files changed

+161
-56
lines changed

app/persistence/fabric/CRUDService.ts

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import { PgService } from '../postgreSQL/PgService';
55
import { helper } from '../../common/helper';
6+
import { queryDatevalidator } from '../../rest/requestutils';
67

78
const logger = helper.getLogger('CRUDService');
89

@@ -103,17 +104,36 @@ export class CRUDService {
103104
let sqlTxList = ` select t.creator_msp_id,t.txhash,t.type,t.chaincodename,t.createdt,channel.name as channelName from transactions as t
104105
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
105106
t.channel_genesis_hash = $3 and t.network_name = $4 and t.createdt between $5 and $6 `;
106-
const values = [blockNum, txid, channel_genesis_hash, network_name, from, to, page, size];
107+
const values = [
108+
blockNum,
109+
txid,
110+
channel_genesis_hash,
111+
network_name,
112+
from,
113+
to,
114+
page,
115+
size
116+
];
107117
if (page == 1) {
108118
let sqlTxCount: string;
109-
const filterValues = [blockNum, txid, channel_genesis_hash, network_name, from, to];
119+
const filterValues = [
120+
blockNum,
121+
txid,
122+
channel_genesis_hash,
123+
network_name,
124+
from,
125+
to
126+
];
110127
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 `
128+
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 `;
112129
if (orgs && orgs.length > 0) {
113130
sqlTxCount += ' and t.creator_msp_id = ANY($7)';
114131
filterValues.push(orgs);
115132
}
116-
countOfTxns = await this.sql.getRowsCountBySQlQuery(sqlTxCount, filterValues)
133+
countOfTxns = await this.sql.getRowsCountBySQlQuery(
134+
sqlTxCount,
135+
filterValues
136+
);
117137
}
118138
if (orgs && orgs.length > 0) {
119139
sqlTxList += ' and t.creator_msp_id = ANY($9)';
@@ -124,7 +144,7 @@ export class CRUDService {
124144
let response = {
125145
txnsData: txnsData,
126146
noOfpages: Math.ceil(countOfTxns / size)
127-
}
147+
};
128148

129149
return response;
130150
}
@@ -162,7 +182,7 @@ export class CRUDService {
162182
byOrgs = ' and creator_msp_id = ANY($7)';
163183
}
164184
let sqlBlockTxList;
165-
if(orgs == null || orgs.length == 0 ) {
185+
if (orgs == null || orgs.length == 0) {
166186
sqlBlockTxList = `SELECT a.* FROM (
167187
SELECT (SELECT c.name FROM channel c WHERE c.channel_genesis_hash =$1 AND c.network_name = $2)
168188
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (
@@ -188,18 +208,18 @@ export class CRUDService {
188208
filterValues.push(orgs);
189209
byOrgs = ' and creator_msp_id = ANY($5)';
190210
}
191-
if(orgs == null || orgs.length == 0 ) {
211+
if (orgs == null || orgs.length == 0) {
192212
sqlBlockTxCount = `SELECT COUNT(DISTINCT blocks.blocknum) FROM blocks
193213
JOIN transactions ON blocks.blocknum = transactions.blockid
194214
WHERE blockid = blocks.blocknum ${byOrgs} AND
195215
blocknum >= 0 AND blocks.channel_genesis_hash = $1 AND blocks.network_name = $2 AND
196-
blocks.createdt between $3 AND $4`
216+
blocks.createdt between $3 AND $4`;
197217
} else {
198218
sqlBlockTxCount = `SELECT COUNT(DISTINCT blocks.blocknum) FROM blocks
199219
JOIN transactions ON blocks.blocknum = transactions.blockid
200220
WHERE blockid = blocks.blocknum ${byOrgs}
201221
AND blocks.channel_genesis_hash = $1 and blocks.network_name = $2 AND blocks.createdt between $3 AND $4
202-
AND transactions.creator_msp_id IS NOT NULL AND transactions.creator_msp_id != ' ' AND length(creator_msp_id) > 0`
222+
AND transactions.creator_msp_id IS NOT NULL AND transactions.creator_msp_id != ' ' AND length(creator_msp_id) > 0`;
203223
}
204224
countOfBlocks = await this.sql.getRowsCountBySQlQuery(
205225
sqlBlockTxCount,
@@ -326,7 +346,12 @@ export class CRUDService {
326346
await this.sql.saveRow('transactions', transaction);
327347
await this.sql.updateBySql(
328348
'update chaincodes set txcount =txcount+1 where channel_genesis_hash=$1 and network_name = $2 and name=$3 and version=$4',
329-
[transaction.channel_genesis_hash, network_name, transaction.chaincodename, chaincodeversion]
349+
[
350+
transaction.channel_genesis_hash,
351+
network_name,
352+
transaction.chaincodename,
353+
chaincodeversion
354+
]
330355
);
331356
await this.sql.updateBySql(
332357
'update channel set trans =trans+1 where channel_genesis_hash=$1 and network_name = $2 ',
@@ -511,14 +536,20 @@ export class CRUDService {
511536
* @returns
512537
* @memberof CRUDService
513538
*/
514-
async getChannelsInfo(network_name) {
539+
async getChannelsInfo(network_name, channel_genesis_hash) {
540+
const currentTime = queryDatevalidator(network_name, channel_genesis_hash);
515541
const channels = await this.sql.getRowsBySQlNoCondition(
516-
` select c.id as id,c.name as channelName,c.blocks as blocks ,c.channel_genesis_hash as channel_genesis_hash,c.trans as transactions,c.createdt as createdat,c.channel_hash as channel_hash from channel c,
517-
peer_ref_channel pc where c.channel_genesis_hash = pc.channelid and c.network_name = $1 group by c.id ,c.name ,c.blocks ,c.trans ,c.createdt ,c.channel_hash,c.channel_genesis_hash order by c.name `,
542+
` SELECT c.id as id, c.name as channelName, c.blocks as blocks, c.channel_genesis_hash as channel_genesis_hash,
543+
c.trans as transactions, c.createdt as createdat, c.channel_hash as channel_hash, MAX(blocks.createdt)
544+
as latestDate FROM channel c
545+
INNER JOIN blocks ON c.channel_genesis_hash = blocks.channel_genesis_hash AND c.network_name = blocks.network_name
546+
INNER JOIN peer_ref_channel pc ON c.channel_genesis_hash = blocks.channel_genesis_hash AND c.network_name = pc.network_name
547+
WHERE c.network_name = $1
548+
GROUP BY c.id, c.name, c.blocks, c.trans, c.createdt, c.channel_hash, c.channel_genesis_hash
549+
ORDER BY c.name `,
518550
[network_name]
519551
);
520-
521-
return channels;
552+
return { channels, currentTime };
522553
}
523554

524555
// Orderer BE-303
@@ -549,20 +580,25 @@ export class CRUDService {
549580
* @returns
550581
* @memberof CRUDService
551582
*/
552-
async getBlockByBlocknum(network_name: any, channel_genesis_hash: any, blockNo: any) {
583+
async getBlockByBlocknum(
584+
network_name: any,
585+
channel_genesis_hash: any,
586+
blockNo: any
587+
) {
553588
const sqlBlockTxList = `select a.* from (
554589
select (select c.name from channel c where c.channel_genesis_hash =$1 and c.network_name = $2)
555590
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (
556591
SELECT array_agg(txhash) as txhash FROM transactions where blockid = $3 and
557592
channel_genesis_hash = $1 and network_name = $2) from blocks where
558593
blocks.channel_genesis_hash =$1 and blocks.network_name = $2 and blocknum = $3) a where a.txhash IS NOT NULL`;
559594

560-
const row: any = await this.sql.getRowsBySQlCase(
561-
sqlBlockTxList,
562-
[channel_genesis_hash, network_name, blockNo]);
595+
const row: any = await this.sql.getRowsBySQlCase(sqlBlockTxList, [
596+
channel_genesis_hash,
597+
network_name,
598+
blockNo
599+
]);
563600
return row;
564601
}
565-
566602
}
567603

568604
/**

0 commit comments

Comments
 (0)