Replace cached blocks on reorg, serve stale blocks
This commit is contained in:
		
							parent
							
								
									408c86963b
								
							
						
					
					
						commit
						385cb087d3
					
				@ -29,6 +29,7 @@ class BitcoinApi implements AbstractBitcoinApi {
 | 
			
		||||
      weight: block.weight,
 | 
			
		||||
      previousblockhash: block.previousblockhash,
 | 
			
		||||
      mediantime: block.mediantime,
 | 
			
		||||
      stale: block.confirmations === -1,
 | 
			
		||||
    };
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -89,6 +89,7 @@ export namespace IEsploraApi {
 | 
			
		||||
    weight: number;
 | 
			
		||||
    previousblockhash: string;
 | 
			
		||||
    mediantime: number;
 | 
			
		||||
    stale: boolean;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  export interface Address {
 | 
			
		||||
 | 
			
		||||
@ -656,10 +656,6 @@ class Blocks {
 | 
			
		||||
      const blockSummary: BlockSummary = this.summarizeBlockTransactions(block.id, cpfpSummary.transactions);
 | 
			
		||||
      this.updateTimerProgress(timer, `got block data for ${this.currentBlockHeight}`);
 | 
			
		||||
 | 
			
		||||
      // start async callbacks
 | 
			
		||||
      this.updateTimerProgress(timer, `starting async callbacks for ${this.currentBlockHeight}`);
 | 
			
		||||
      const callbackPromises = this.newAsyncBlockCallbacks.map((cb) => cb(blockExtended, txIds, transactions));
 | 
			
		||||
 | 
			
		||||
      if (Common.indexingEnabled()) {
 | 
			
		||||
        if (!fastForwarded) {
 | 
			
		||||
          const lastBlock = await blocksRepository.$getBlockByHeight(blockExtended.height - 1);
 | 
			
		||||
@ -671,9 +667,11 @@ class Blocks {
 | 
			
		||||
            await BlocksRepository.$deleteBlocksFrom(lastBlock.height - 10);
 | 
			
		||||
            await HashratesRepository.$deleteLastEntries();
 | 
			
		||||
            await cpfpRepository.$deleteClustersFrom(lastBlock.height - 10);
 | 
			
		||||
            this.blocks = this.blocks.slice(0, -10);
 | 
			
		||||
            this.updateTimerProgress(timer, `rolled back chain divergence from ${this.currentBlockHeight}`);
 | 
			
		||||
            for (let i = 10; i >= 0; --i) {
 | 
			
		||||
              const newBlock = await this.$indexBlock(lastBlock.height - i);
 | 
			
		||||
              this.blocks.push(newBlock);
 | 
			
		||||
              this.updateTimerProgress(timer, `reindexed block`);
 | 
			
		||||
              let cpfpSummary;
 | 
			
		||||
              if (config.MEMPOOL.CPFP_INDEXING) {
 | 
			
		||||
@ -722,6 +720,10 @@ class Blocks {
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      // start async callbacks
 | 
			
		||||
      this.updateTimerProgress(timer, `starting async callbacks for ${this.currentBlockHeight}`);
 | 
			
		||||
      const callbackPromises = this.newAsyncBlockCallbacks.map((cb) => cb(blockExtended, txIds, transactions));
 | 
			
		||||
 | 
			
		||||
      if (block.height % 2016 === 0) {
 | 
			
		||||
        if (Common.indexingEnabled()) {
 | 
			
		||||
          await DifficultyAdjustmentsRepository.$saveAdjustments({
 | 
			
		||||
@ -814,6 +816,16 @@ class Blocks {
 | 
			
		||||
    return blockExtended;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public async $indexStaleBlock(hash: string): Promise<BlockExtended> {
 | 
			
		||||
    const block: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(hash);
 | 
			
		||||
    const transactions = await this.$getTransactionsExtended(hash, block.height, true);
 | 
			
		||||
    const blockExtended = await this.$getBlockExtended(block, transactions);
 | 
			
		||||
 | 
			
		||||
    blockExtended.canonical = await bitcoinApi.$getBlockHash(block.height);
 | 
			
		||||
 | 
			
		||||
    return blockExtended;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Get one block by its hash
 | 
			
		||||
   */
 | 
			
		||||
@ -831,7 +843,11 @@ class Blocks {
 | 
			
		||||
 | 
			
		||||
    // Bitcoin network, add our custom data on top
 | 
			
		||||
    const block: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(hash);
 | 
			
		||||
    return await this.$indexBlock(block.height);
 | 
			
		||||
    if (block.stale) {
 | 
			
		||||
      return await this.$indexStaleBlock(hash);
 | 
			
		||||
    } else {
 | 
			
		||||
      return await this.$indexBlock(block.height);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public async $getStrippedBlockTransactions(hash: string, skipMemoryCache = false,
 | 
			
		||||
 | 
			
		||||
@ -62,8 +62,7 @@ class BlocksAuditRepositories {
 | 
			
		||||
  public async $getBlockAudit(hash: string): Promise<any> {
 | 
			
		||||
    try {
 | 
			
		||||
      const [rows]: any[] = await DB.query(
 | 
			
		||||
        `SELECT blocks.height, blocks.hash as id, UNIX_TIMESTAMP(blocks.blockTimestamp) as timestamp, blocks.size,
 | 
			
		||||
        blocks.weight, blocks.tx_count,
 | 
			
		||||
        `SELECT blocks_audits.height, blocks_audits.hash as id, UNIX_TIMESTAMP(blocks_audits.time) as timestamp,
 | 
			
		||||
        template,
 | 
			
		||||
        missing_txs as missingTxs,
 | 
			
		||||
        added_txs as addedTxs,
 | 
			
		||||
@ -73,7 +72,6 @@ class BlocksAuditRepositories {
 | 
			
		||||
        expected_fees as expectedFees,
 | 
			
		||||
        expected_weight as expectedWeight
 | 
			
		||||
        FROM blocks_audits
 | 
			
		||||
        JOIN blocks ON blocks.hash = blocks_audits.hash
 | 
			
		||||
        JOIN blocks_templates ON blocks_templates.id = blocks_audits.hash
 | 
			
		||||
        WHERE blocks_audits.hash = "${hash}"
 | 
			
		||||
      `);
 | 
			
		||||
 | 
			
		||||
@ -120,6 +120,7 @@ export interface Block {
 | 
			
		||||
  size: number;
 | 
			
		||||
  weight: number;
 | 
			
		||||
  previousblockhash: string;
 | 
			
		||||
  stale?: boolean;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export interface Address {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user