2022-01-05 07:41:14 +01:00
|
|
|
import { BlockExtended, PoolTag } from "../mempool.interfaces";
|
|
|
|
import { DB } from "../database";
|
|
|
|
import logger from "../logger";
|
2022-01-06 11:59:33 +01:00
|
|
|
|
|
|
|
export interface EmptyBlocks {
|
|
|
|
emptyBlocks: number,
|
|
|
|
poolId: number,
|
|
|
|
}
|
2022-01-05 07:41:14 +01:00
|
|
|
|
|
|
|
class BlocksRepository {
|
|
|
|
/**
|
|
|
|
* Save indexed block data in the database
|
|
|
|
*/
|
|
|
|
public async $saveBlockInDatabase(
|
|
|
|
block: BlockExtended,
|
|
|
|
blockHash: string,
|
|
|
|
coinbaseHex: string | undefined,
|
|
|
|
poolTag: PoolTag
|
|
|
|
) {
|
|
|
|
const connection = await DB.pool.getConnection();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const query = `INSERT INTO blocks(
|
|
|
|
height, hash, timestamp, size,
|
|
|
|
weight, tx_count, coinbase_raw, difficulty,
|
|
|
|
pool_id, fees, fee_span, median_fee
|
|
|
|
) VALUE (
|
2022-01-06 11:59:33 +01:00
|
|
|
?, ?, FROM_UNIXTIME(?), ?,
|
2022-01-05 07:41:14 +01:00
|
|
|
?, ?, ?, ?,
|
|
|
|
?, ?, ?, ?
|
|
|
|
)`;
|
|
|
|
|
|
|
|
const params: any[] = [
|
|
|
|
block.height, blockHash, block.timestamp, block.size,
|
|
|
|
block.weight, block.tx_count, coinbaseHex ? coinbaseHex : "", block.difficulty,
|
|
|
|
poolTag.id, 0, "[]", block.medianFee,
|
|
|
|
];
|
|
|
|
|
|
|
|
await connection.query(query, params);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
logger.err('$updateBlocksDatabase() error' + (e instanceof Error ? e.message : e));
|
|
|
|
}
|
|
|
|
|
|
|
|
connection.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a block has already been indexed in the database. Query the databse directly.
|
|
|
|
* This can be cached/optimized if required later on to avoid too many db queries.
|
|
|
|
*/
|
|
|
|
public async $isBlockAlreadyIndexed(blockHeight: number) {
|
|
|
|
const connection = await DB.pool.getConnection();
|
|
|
|
let exists = false;
|
|
|
|
|
2022-01-06 11:59:33 +01:00
|
|
|
const query = `SELECT height from blocks where blocks.height = ${blockHeight}`;
|
|
|
|
const [rows]: any[] = await connection.query(query);
|
|
|
|
exists = rows.length === 1;
|
2022-01-05 07:41:14 +01:00
|
|
|
connection.release();
|
|
|
|
|
|
|
|
return exists;
|
|
|
|
}
|
2022-01-06 11:59:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Count empty blocks for all pools
|
|
|
|
*/
|
|
|
|
public async $countEmptyBlocks(interval: string = "100 YEAR") : Promise<EmptyBlocks[]> {
|
|
|
|
const connection = await DB.pool.getConnection();
|
|
|
|
const [rows] = await connection.query(`
|
|
|
|
SELECT pool_id as poolId
|
|
|
|
FROM blocks
|
|
|
|
WHERE timestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()
|
|
|
|
AND tx_count = 1;
|
|
|
|
`);
|
|
|
|
connection.release();
|
|
|
|
|
|
|
|
return <EmptyBlocks[]>rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get blocks count for a period
|
|
|
|
*/
|
|
|
|
public async $blockCount(interval: string = "100 YEAR") : Promise<number> {
|
|
|
|
const connection = await DB.pool.getConnection();
|
|
|
|
const [rows] = await connection.query(`
|
|
|
|
SELECT count(height) as blockCount
|
|
|
|
FROM blocks
|
|
|
|
WHERE timestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW();
|
|
|
|
`);
|
|
|
|
connection.release();
|
|
|
|
|
|
|
|
return <number>rows[0].blockCount;
|
|
|
|
}
|
2022-01-05 07:41:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new BlocksRepository();
|