Erase old blocks and transactions from database at interval.

This commit is contained in:
Simon Lindh 2019-08-29 01:17:31 +02:00
parent 53269a805b
commit 61c6345f75

View File

@ -7,6 +7,11 @@ import memPool from './mempool';
class Blocks { class Blocks {
private blocks: IBlock[] = []; private blocks: IBlock[] = [];
private newBlockCallback: Function | undefined; private newBlockCallback: Function | undefined;
private currentBlockHeight = 0;
constructor() {
setInterval(this.$clearOldTransactionsAndBlocksFromDatabase.bind(this), 86400000);
}
public setNewBlockCallback(fn: Function) { public setNewBlockCallback(fn: Function) {
this.newBlockCallback = fn; this.newBlockCallback = fn;
@ -35,23 +40,22 @@ class Blocks {
try { try {
const blockCount = await bitcoinApi.getBlockCount(); const blockCount = await bitcoinApi.getBlockCount();
let currentBlockHeight = 0;
if (this.blocks.length === 0) { if (this.blocks.length === 0) {
currentBlockHeight = blockCount - config.INITIAL_BLOCK_AMOUNT; this.currentBlockHeight = blockCount - config.INITIAL_BLOCK_AMOUNT;
} else { } else {
currentBlockHeight = this.blocks[this.blocks.length - 1].height; this.currentBlockHeight = this.blocks[this.blocks.length - 1].height;
} }
while (currentBlockHeight < blockCount) { while (this.currentBlockHeight < blockCount) {
currentBlockHeight++; this.currentBlockHeight++;
let block: IBlock | undefined; let block: IBlock | undefined;
const storedBlock = await this.$getBlockFromDatabase(currentBlockHeight); const storedBlock = await this.$getBlockFromDatabase(this.currentBlockHeight);
if (storedBlock) { if (storedBlock) {
block = storedBlock; block = storedBlock;
} else { } else {
const blockHash = await bitcoinApi.getBlockHash(currentBlockHeight); const blockHash = await bitcoinApi.getBlockHash(this.currentBlockHeight);
block = await bitcoinApi.getBlock(blockHash, 1); block = await bitcoinApi.getBlock(blockHash, 1);
const coinbase = await memPool.getRawTransaction(block.tx[0], true); const coinbase = await memPool.getRawTransaction(block.tx[0], true);
@ -85,13 +89,15 @@ class Blocks {
block.maxFee = transactions[0] ? transactions[0].feePerVsize : 0; block.maxFee = transactions[0] ? transactions[0].feePerVsize : 0;
block.medianFee = this.median(transactions.map((tx) => tx.feePerVsize)); block.medianFee = this.median(transactions.map((tx) => tx.feePerVsize));
if (this.newBlockCallback) { console.log(`New block found (#${this.currentBlockHeight})! `
+ `${found} of ${block.tx.length} found in mempool. ${notFound} not found.`);
if (this.newBlockCallback) {
this.newBlockCallback(block); this.newBlockCallback(block);
} }
await this.$saveBlockToDatabase(block); this.$saveBlockToDatabase(block);
await this.$saveTransactionsToDatabase(block.height, transactions); this.$saveTransactionsToDatabase(block.height, transactions);
console.log(`New block found (#${currentBlockHeight})! ${found} of ${block.tx.length} found in mempool. ${notFound} not found.`);
} }
this.blocks.push(block); this.blocks.push(block);
@ -173,13 +179,25 @@ class Blocks {
await connection.query(query, params); await connection.query(query, params);
} }
connection.release(); connection.release();
} catch (e) { } catch (e) {
console.log('$create() transaction error', e); console.log('$create() transaction error', e);
} }
} }
private async $clearOldTransactionsAndBlocksFromDatabase() {
try {
const connection = await DB.pool.getConnection();
let query = `DELETE FROM blocks WHERE height < ?`;
await connection.query<any>(query, [this.currentBlockHeight - config.KEEP_BLOCK_AMOUNT]);
query = `DELETE FROM transactions WHERE blockheight < ?`;
await connection.query<any>(query, [this.currentBlockHeight - config.KEEP_BLOCK_AMOUNT]);
connection.release();
} catch (e) {
console.log('$clearOldTransactionsFromDatabase() error', e);
}
}
private median(numbers: number[]) { private median(numbers: number[]) {
if (!numbers.length) { return 0; } if (!numbers.length) { return 0; }
let medianNr = 0; let medianNr = 0;