mirror of
https://github.com/mempool/mempool.git
synced 2025-02-22 14:22:44 +01:00
Add indexer task to backfill audit fee/weight stats
This commit is contained in:
parent
3013386ca5
commit
5b62966863
4 changed files with 90 additions and 0 deletions
|
@ -457,6 +457,41 @@ class Blocks {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* [INDEXING] Index expected fees & weight for all audited blocks
|
||||
*/
|
||||
public async $generateAuditStats(): Promise<void> {
|
||||
const blockIds = await BlocksAuditsRepository.$getBlocksWithoutSummaries();
|
||||
if (!blockIds?.length) {
|
||||
return;
|
||||
}
|
||||
let timer = Date.now();
|
||||
let indexedThisRun = 0;
|
||||
let indexedTotal = 0;
|
||||
logger.debug(`Indexing ${blockIds.length} block audit details`);
|
||||
for (const hash of blockIds) {
|
||||
const summary = await BlocksSummariesRepository.$getTemplate(hash);
|
||||
let totalFees = 0;
|
||||
let totalWeight = 0;
|
||||
for (const tx of summary?.transactions || []) {
|
||||
totalFees += tx.fee;
|
||||
totalWeight += (tx.vsize * 4);
|
||||
}
|
||||
await BlocksAuditsRepository.$setSummary(hash, totalFees, totalWeight);
|
||||
|
||||
indexedThisRun++;
|
||||
indexedTotal++;
|
||||
const elapsedSeconds = (Date.now() - timer) / 1000;
|
||||
if (elapsedSeconds > 5) {
|
||||
const blockPerSeconds = indexedThisRun / elapsedSeconds;
|
||||
logger.debug(`Indexed ${indexedTotal} / ${blockIds.length} block audit details (${blockPerSeconds.toFixed(1)}/s)`);
|
||||
timer = Date.now();
|
||||
indexedThisRun = 0;
|
||||
}
|
||||
}
|
||||
logger.debug(`Indexing block audit details completed`);
|
||||
}
|
||||
|
||||
/**
|
||||
* [INDEXING] Index all blocks metadata for the mining dashboard
|
||||
*/
|
||||
|
|
|
@ -134,6 +134,7 @@ class Indexer {
|
|||
await mining.$generatePoolHashrateHistory();
|
||||
await blocks.$generateBlocksSummariesDatabase();
|
||||
await blocks.$generateCPFPDatabase();
|
||||
await blocks.$generateAuditStats();
|
||||
} catch (e) {
|
||||
this.indexerRunning = false;
|
||||
logger.err(`Indexer failed, trying again in 10 seconds. Reason: ` + (e instanceof Error ? e.message : e));
|
||||
|
|
|
@ -18,6 +18,19 @@ class BlocksAuditRepositories {
|
|||
}
|
||||
}
|
||||
|
||||
public async $setSummary(hash: string, expectedFees: number, expectedWeight: number) {
|
||||
try {
|
||||
await DB.query(`
|
||||
UPDATE blocks_audits SET
|
||||
expected_fees = ?,
|
||||
expected_weight = ?
|
||||
WHERE hash = ?
|
||||
`, [expectedFees, expectedWeight, hash]);
|
||||
} catch (e: any) {
|
||||
logger.err(`Cannot update block audit in db. Reason: ` + (e instanceof Error ? e.message : e));
|
||||
}
|
||||
}
|
||||
|
||||
public async $getBlocksHealthHistory(div: number, interval: string | null): Promise<any> {
|
||||
try {
|
||||
let query = `SELECT UNIX_TIMESTAMP(time) as time, height, match_rate FROM blocks_audits`;
|
||||
|
@ -113,6 +126,32 @@ class BlocksAuditRepositories {
|
|||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public async $getBlocksWithoutSummaries(): Promise<string[]> {
|
||||
try {
|
||||
const [fromRows]: any[] = await DB.query(`
|
||||
SELECT height
|
||||
FROM blocks_audits
|
||||
WHERE expected_fees IS NULL
|
||||
ORDER BY height DESC
|
||||
LIMIT 1
|
||||
`);
|
||||
if (!fromRows?.length) {
|
||||
return [];
|
||||
}
|
||||
const fromHeight = fromRows[0].height;
|
||||
const [idRows]: any[] = await DB.query(`
|
||||
SELECT hash
|
||||
FROM blocks_audits
|
||||
WHERE height <= ?
|
||||
ORDER BY height DESC
|
||||
`, [fromHeight]);
|
||||
return idRows.map(row => row.hash);
|
||||
} catch (e: any) {
|
||||
logger.err(`Cannot fetch block audit from db. Reason: ` + (e instanceof Error ? e.message : e));
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new BlocksAuditRepositories();
|
||||
|
|
|
@ -50,6 +50,21 @@ class BlocksSummariesRepository {
|
|||
}
|
||||
}
|
||||
|
||||
public async $getTemplate(id: string): Promise<BlockSummary | undefined> {
|
||||
try {
|
||||
const [templates]: any[] = await DB.query(`SELECT * from blocks_templates WHERE id = ?`, [id]);
|
||||
if (templates.length > 0) {
|
||||
return {
|
||||
id: templates[0].id,
|
||||
transactions: JSON.parse(templates[0].template),
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
logger.err(`Cannot get block template for block id ${id}. Reason: ` + (e instanceof Error ? e.message : e));
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public async $getIndexedSummariesId(): Promise<string[]> {
|
||||
try {
|
||||
const [rows]: any[] = await DB.query(`SELECT id from blocks_summaries`);
|
||||
|
|
Loading…
Add table
Reference in a new issue