Keep re-org'ed block summaries in the database
This commit is contained in:
		
							parent
							
								
									9e4fe40ca3
								
							
						
					
					
						commit
						44bbb472d3
					
				@ -593,7 +593,6 @@ class Blocks {
 | 
				
			|||||||
            // We assume there won't be a reorg with more than 10 block depth
 | 
					            // We assume there won't be a reorg with more than 10 block depth
 | 
				
			||||||
            await BlocksRepository.$deleteBlocksFrom(lastBlock.height - 10);
 | 
					            await BlocksRepository.$deleteBlocksFrom(lastBlock.height - 10);
 | 
				
			||||||
            await HashratesRepository.$deleteLastEntries();
 | 
					            await HashratesRepository.$deleteLastEntries();
 | 
				
			||||||
            await BlocksSummariesRepository.$deleteTransactionsFrom(lastBlock.height - 10); // Will be re-index in formatDbBlockIntoExtendedBlock()
 | 
					 | 
				
			||||||
            await cpfpRepository.$deleteClustersFrom(lastBlock.height - 10);
 | 
					            await cpfpRepository.$deleteClustersFrom(lastBlock.height - 10);
 | 
				
			||||||
            for (let i = 10; i >= 0; --i) {
 | 
					            for (let i = 10; i >= 0; --i) {
 | 
				
			||||||
              const newBlock = await this.$indexBlock(lastBlock.height - i);
 | 
					              const newBlock = await this.$indexBlock(lastBlock.height - i);
 | 
				
			||||||
@ -736,7 +735,7 @@ class Blocks {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Index the response if needed
 | 
					    // Index the response if needed
 | 
				
			||||||
    if (Common.blocksSummariesIndexingEnabled() === true) {
 | 
					    if (Common.blocksSummariesIndexingEnabled() === true) {
 | 
				
			||||||
      await BlocksSummariesRepository.$saveSummary({height: block.height, mined: summary});
 | 
					      await BlocksSummariesRepository.$saveTransactions(block.height, block.hash, summary.transactions);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return summary.transactions;
 | 
					    return summary.transactions;
 | 
				
			||||||
@ -852,7 +851,7 @@ class Blocks {
 | 
				
			|||||||
        if (cleanBlock.fee_amt_percentiles === null) {
 | 
					        if (cleanBlock.fee_amt_percentiles === null) {
 | 
				
			||||||
          const block = await bitcoinClient.getBlock(cleanBlock.hash, 2);
 | 
					          const block = await bitcoinClient.getBlock(cleanBlock.hash, 2);
 | 
				
			||||||
          const summary = this.summarizeBlock(block);
 | 
					          const summary = this.summarizeBlock(block);
 | 
				
			||||||
          await BlocksSummariesRepository.$saveSummary({ height: block.height, mined: summary });
 | 
					          await BlocksSummariesRepository.$saveTransactions(cleanBlock.height, cleanBlock.hash, summary.transactions);
 | 
				
			||||||
          cleanBlock.fee_amt_percentiles = await BlocksSummariesRepository.$getFeePercentilesByBlockId(cleanBlock.hash);
 | 
					          cleanBlock.fee_amt_percentiles = await BlocksSummariesRepository.$getFeePercentilesByBlockId(cleanBlock.hash);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (cleanBlock.fee_amt_percentiles !== null) {
 | 
					        if (cleanBlock.fee_amt_percentiles !== null) {
 | 
				
			||||||
 | 
				
			|||||||
@ -981,7 +981,7 @@ class BlocksRepository {
 | 
				
			|||||||
      if (extras.feePercentiles === null) {
 | 
					      if (extras.feePercentiles === null) {
 | 
				
			||||||
        const block = await bitcoinClient.getBlock(dbBlk.id, 2);
 | 
					        const block = await bitcoinClient.getBlock(dbBlk.id, 2);
 | 
				
			||||||
        const summary = blocks.summarizeBlock(block);
 | 
					        const summary = blocks.summarizeBlock(block);
 | 
				
			||||||
        await BlocksSummariesRepository.$saveSummary({ height: block.height, mined: summary });
 | 
					        await BlocksSummariesRepository.$saveTransactions(dbBlk.height, dbBlk.hash, summary.transactions);
 | 
				
			||||||
        extras.feePercentiles = await BlocksSummariesRepository.$getFeePercentilesByBlockId(dbBlk.id);
 | 
					        extras.feePercentiles = await BlocksSummariesRepository.$getFeePercentilesByBlockId(dbBlk.id);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      if (extras.feePercentiles !== null) {
 | 
					      if (extras.feePercentiles !== null) {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,6 @@
 | 
				
			|||||||
import DB from '../database';
 | 
					import DB from '../database';
 | 
				
			||||||
import logger from '../logger';
 | 
					import logger from '../logger';
 | 
				
			||||||
import { BlockSummary } from '../mempool.interfaces';
 | 
					import { BlockSummary, TransactionStripped } from '../mempool.interfaces';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BlocksSummariesRepository {
 | 
					class BlocksSummariesRepository {
 | 
				
			||||||
  public async $getByBlockId(id: string): Promise<BlockSummary | undefined> {
 | 
					  public async $getByBlockId(id: string): Promise<BlockSummary | undefined> {
 | 
				
			||||||
@ -37,6 +37,20 @@ class BlocksSummariesRepository {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async $saveTemplate(params: { height: number, template: BlockSummary}) {
 | 
					  public async $saveTemplate(params: { height: number, template: BlockSummary}) {
 | 
				
			||||||
    const blockId = params.template?.id;
 | 
					    const blockId = params.template?.id;
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user