Commit 12545c36 authored by Cédric Moreau's avatar Cédric Moreau

[enh] Speed up "blockCount"

parent c1563c62
import {Server} from 'duniter/server'
import {DBBlock} from 'duniter/app/lib/db/DBBlock'
export class DataFinder {
......@@ -26,8 +27,8 @@ export class DataFinder {
'UNION ALL ' + 'SELECT hash, uid, pubkey as pub, (SELECT NULL) AS wotb_id FROM idty WHERE uid = ?', [uid, uid])
}
async getBlockMedianTimeAndHash(block_number: number): Promise<{ hash: string, medianTime: number }|undefined> {
return (await this.getFromCacheOrDB('getBlockMedianTimeAndHash', String(block_number),() => this.duniterServer.dal.getBlock(block_number))) || undefined
async getBlock(block_number: number): Promise<DBBlock|undefined> {
return (await this.getFromCacheOrDB('getBlock', String(block_number),() => this.duniterServer.dal.getBlock(block_number))) || undefined
}
getUidOfPub(pub: string): Promise<{ uid: string }[]> {
......@@ -98,4 +99,14 @@ export class DataFinder {
invalidateCache() {
this.memCache = {}
}
getBlockWhereMedianTimeGt(previousBlockchainTime: number) {
return this.getFromCacheOrDB('getBlockWhereMedianTimeGt', String(previousBlockchainTime),
() => this.query('SELECT `issuer`,`membersCount`,`medianTime`,`dividend`,`number`,`nonce` FROM block WHERE `fork`=0 AND `medianTime` > '+previousBlockchainTime+' ORDER BY `medianTime` ASC'))
}
getBlockWhereMedianTimeLteAnd(medianTime: number, previousBlockchainTime: number) {
return this.getFromCacheOrDB('getBlockWhereMedianTimeLteAnd', [medianTime, previousBlockchainTime].join('-'),
() => this.query('SELECT `issuer`,`membersCount`,`medianTime`,`dividend`,`number`,`nonce` FROM block WHERE `fork`=0 AND `medianTime` <= '+medianTime+' AND `medianTime` > '+previousBlockchainTime+' ORDER BY `medianTime` ASC'))
}
}
This diff is collapsed.
This diff is collapsed.
......@@ -135,7 +135,7 @@ app.get('/gaussianWotQuality',
* Lister les block en graphe
***************************************/
app.get('/blockCount',
require(__dirname + '/blockCount.js'),
require(__dirname + '/blockCount2.js'),
(req, res)=> res.render('Chart.html', {
printMenu,
pageName: 'BLOCK_COUNT',
......
......@@ -192,7 +192,7 @@ module.exports = async (req: any, res: any, next: any) => {
let blockstampIdtyWritten = membersList[m].written_on.split("-"); // Separate blockNumber and blockHash
// Récupérer le champ medianTime du bloc d'écriture de l'identité du membre
let resultQueryTimeWrittenIdty = await dataFinder.getBlockMedianTimeAndHash(blockstampIdtyWritten[0])
let resultQueryTimeWrittenIdty = await dataFinder.getBlock(blockstampIdtyWritten[0])
// Vérifier si le membre est référent
let currentMemberIsSentry = false;
......@@ -317,7 +317,7 @@ module.exports = async (req: any, res: any, next: any) => {
for (var i=0;i<tmpQueryPendingCertifsList.length;i++)
{
// Récupérer le medianTime et le hash du bloc d'émission de la certification
let emittedBlock = await dataFinder.getBlockMedianTimeAndHash(tmpQueryPendingCertifsList[i].block_number)
let emittedBlock = await dataFinder.getBlock(tmpQueryPendingCertifsList[i].block_number)
let tmpPub = (mode=='emitted') ? tmpQueryPendingCertifsList[i].to:tmpQueryPendingCertifsList[i].from;
let tmpQueryGetUidProtagonistPendingCert = await dataFinder.getUidOfPub(tmpPub)
......@@ -370,7 +370,7 @@ module.exports = async (req: any, res: any, next: any) => {
// Convertir chaque blockNumber (de membership) en timestamp
for (const membershipBlockNumber of membershipsBlockNumberList)
{
membershipsTimeList.push(await dataFinder.getBlockMedianTimeAndHash(membershipBlockNumber) as { medianTime: number })
membershipsTimeList.push(await dataFinder.getBlock(membershipBlockNumber) as { medianTime: number })
}
// Traiter les cas ou expires_on est indéfini
......
......@@ -97,7 +97,7 @@ module.exports = async (req: any, res: any, next: any) => {
let idtyBlockNumber = idtyBlockStamp[0];
// récupérer le medianTime et le hash du bloc d'émission de l'identité
let idtyEmittedBlock = await dataFinder.getBlockMedianTimeAndHash(parseInt(idtyBlockNumber));
let idtyEmittedBlock = await dataFinder.getBlock(parseInt(idtyBlockNumber));
// Récupérer l'identifiant wotex de l'identité (en cas d'identité multiple)
let idties = await dataFinder.getWotexInfos(resultQueryIdtys[i].uid);
......@@ -149,7 +149,7 @@ module.exports = async (req: any, res: any, next: any) => {
for (let j=0;j<tmpQueryPendingCertifsList.length;j++)
{
// Récupérer le medianTime et le hash du bloc d'émission de la certification
let emittedBlock = await dataFinder.getBlockMedianTimeAndHash(tmpQueryPendingCertifsList[j].block_number)
let emittedBlock = await dataFinder.getBlock(tmpQueryPendingCertifsList[j].block_number)
// Vérifier que l'émetteur de la certification correspond à une identité inscrite en blockchain
let tmpQueryGetUidIssuerPendingCert = await dataFinder.getUidOfPub(tmpQueryPendingCertifsList[j].from)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment