2022-06-18 16:48:02 +02:00
|
|
|
import DB from '../database';
|
|
|
|
import logger from '../logger';
|
2023-03-31 12:08:05 +09:00
|
|
|
import { BlockSummary, TransactionStripped } from '../mempool.interfaces';
|
2022-06-18 16:48:02 +02:00
|
|
|
|
|
|
|
class BlocksSummariesRepository {
|
|
|
|
public async $getByBlockId(id: string): Promise<BlockSummary | undefined> {
|
|
|
|
try {
|
|
|
|
const [summary]: any[] = await DB.query(`SELECT * from blocks_summaries WHERE id = ?`, [id]);
|
|
|
|
if (summary.length > 0) {
|
|
|
|
summary[0].transactions = JSON.parse(summary[0].transactions);
|
|
|
|
return summary[0];
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
logger.err(`Cannot get block summary for block id ${id}. Reason: ` + (e instanceof Error ? e.message : e));
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2023-03-31 12:08:05 +09:00
|
|
|
public async $saveTransactions(blockHeight: number, blockId: string, transactions: TransactionStripped[]): Promise<void> {
|
|
|
|
try {
|
|
|
|
const transactionsStr = JSON.stringify(transactions);
|
|
|
|
await DB.query(`
|
|
|
|
INSERT INTO blocks_summaries
|
|
|
|
SET height = ?, transactions = ?, id = ?
|
|
|
|
ON DUPLICATE KEY UPDATE transactions = ?`,
|
|
|
|
[blockHeight, transactionsStr, blockId, transactionsStr]);
|
|
|
|
} catch (e: any) {
|
|
|
|
logger.debug(`Cannot save block summary transactions for ${blockId}. Reason: ${e instanceof Error ? e.message : e}`);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 10:49:10 +09:00
|
|
|
public async $saveTemplate(params: { height: number, template: BlockSummary}) {
|
|
|
|
const blockId = params.template?.id;
|
|
|
|
try {
|
|
|
|
const transactions = JSON.stringify(params.template?.transactions || []);
|
|
|
|
await DB.query(`
|
2023-05-25 17:39:45 -04:00
|
|
|
INSERT INTO blocks_templates (id, template)
|
|
|
|
VALUE (?, ?)
|
2022-12-03 10:49:10 +09:00
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
template = ?
|
2023-05-25 17:39:45 -04:00
|
|
|
`, [blockId, transactions, transactions]);
|
2022-12-03 10:49:10 +09:00
|
|
|
} catch (e: any) {
|
|
|
|
if (e.errno === 1062) { // ER_DUP_ENTRY - This scenario is possible upon node backend restart
|
|
|
|
logger.debug(`Cannot save block template for ${blockId} because it has already been indexed, ignoring`);
|
|
|
|
} else {
|
2023-05-13 15:28:29 +02:00
|
|
|
logger.warn(`Cannot save block template for ${blockId}. Reason: ${e instanceof Error ? e.message : e}`);
|
2022-12-03 10:49:10 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 16:48:02 +02:00
|
|
|
public async $getIndexedSummariesId(): Promise<string[]> {
|
|
|
|
try {
|
|
|
|
const [rows]: any[] = await DB.query(`SELECT id from blocks_summaries`);
|
|
|
|
return rows.map(row => row.id);
|
|
|
|
} catch (e) {
|
|
|
|
logger.err(`Cannot get block summaries id list. Reason: ` + (e instanceof Error ? e.message : e));
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
2022-06-20 16:35:10 +02:00
|
|
|
|
2023-02-18 11:26:13 +09:00
|
|
|
/**
|
|
|
|
* Get the fee percentiles if the block has already been indexed, [] otherwise
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
*/
|
|
|
|
public async $getFeePercentilesByBlockId(id: string): Promise<number[] | null> {
|
|
|
|
try {
|
|
|
|
const [rows]: any[] = await DB.query(`
|
|
|
|
SELECT transactions
|
|
|
|
FROM blocks_summaries
|
|
|
|
WHERE id = ?`,
|
|
|
|
[id]
|
|
|
|
);
|
|
|
|
if (rows === null || rows.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const transactions = JSON.parse(rows[0].transactions);
|
|
|
|
if (transactions === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
transactions.shift(); // Ignore coinbase
|
|
|
|
transactions.sort((a: any, b: any) => a.fee - b.fee);
|
|
|
|
const fees = transactions.map((t: any) => t.fee);
|
|
|
|
|
|
|
|
return [
|
2023-02-24 11:41:54 +09:00
|
|
|
fees[0] ?? 0, // min
|
|
|
|
fees[Math.max(0, Math.floor(fees.length * 0.1) - 1)] ?? 0, // 10th
|
|
|
|
fees[Math.max(0, Math.floor(fees.length * 0.25) - 1)] ?? 0, // 25th
|
|
|
|
fees[Math.max(0, Math.floor(fees.length * 0.5) - 1)] ?? 0, // median
|
|
|
|
fees[Math.max(0, Math.floor(fees.length * 0.75) - 1)] ?? 0, // 75th
|
|
|
|
fees[Math.max(0, Math.floor(fees.length * 0.9) - 1)] ?? 0, // 90th
|
|
|
|
fees[fees.length - 1] ?? 0, // max
|
2023-02-18 11:26:13 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
logger.err(`Cannot get block summaries transactions. Reason: ` + (e instanceof Error ? e.message : e));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-06-18 16:48:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new BlocksSummariesRepository();
|
|
|
|
|