Break block templates into their own db table
This commit is contained in:
		
							parent
							
								
									126a75ed45
								
							
						
					
					
						commit
						b171ed6dd0
					
				@ -7,7 +7,7 @@ import cpfpRepository from '../repositories/CpfpRepository';
 | 
				
			|||||||
import { RowDataPacket } from 'mysql2';
 | 
					import { RowDataPacket } from 'mysql2';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class DatabaseMigration {
 | 
					class DatabaseMigration {
 | 
				
			||||||
  private static currentVersion = 60;
 | 
					  private static currentVersion = 61;
 | 
				
			||||||
  private queryTimeout = 3600_000;
 | 
					  private queryTimeout = 3600_000;
 | 
				
			||||||
  private statisticsAddedIndexed = false;
 | 
					  private statisticsAddedIndexed = false;
 | 
				
			||||||
  private uniqueLogs: string[] = [];
 | 
					  private uniqueLogs: string[] = [];
 | 
				
			||||||
@ -521,6 +521,18 @@ class DatabaseMigration {
 | 
				
			|||||||
      await this.$executeQuery('ALTER TABLE `blocks_audits` ADD sigop_txs JSON DEFAULT "[]"');
 | 
					      await this.$executeQuery('ALTER TABLE `blocks_audits` ADD sigop_txs JSON DEFAULT "[]"');
 | 
				
			||||||
      await this.updateToSchemaVersion(60);
 | 
					      await this.updateToSchemaVersion(60);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (databaseSchemaVersion < 61 && isBitcoin === true) {
 | 
				
			||||||
 | 
					      // Break block templates into their own table
 | 
				
			||||||
 | 
					      if (! await this.$checkIfTableExists('blocks_templates')) {
 | 
				
			||||||
 | 
					        await this.$executeQuery('CREATE TABLE blocks_templates AS SELECT id, template FROM blocks_summaries WHERE template != "[]"');
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      await this.$executeQuery('ALTER TABLE blocks_templates MODIFY template JSON DEFAULT "[]"');
 | 
				
			||||||
 | 
					      await this.$executeQuery('ALTER TABLE blocks_templates ADD PRIMARY KEY (id)');
 | 
				
			||||||
 | 
					      await this.$executeQuery('ALTER TABLE blocks_summaries DROP COLUMN template');
 | 
				
			||||||
 | 
					      await this.updateToSchemaVersion(61);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
				
			|||||||
@ -55,6 +55,7 @@ class BlocksAuditRepositories {
 | 
				
			|||||||
        transactions, template, missing_txs as missingTxs, added_txs as addedTxs, fresh_txs as freshTxs, sigop_txs as sigopTxs, match_rate as matchRate
 | 
					        transactions, template, missing_txs as missingTxs, added_txs as addedTxs, fresh_txs as freshTxs, sigop_txs as sigopTxs, match_rate as matchRate
 | 
				
			||||||
        FROM blocks_audits
 | 
					        FROM blocks_audits
 | 
				
			||||||
        JOIN blocks ON blocks.hash = blocks_audits.hash
 | 
					        JOIN blocks ON blocks.hash = blocks_audits.hash
 | 
				
			||||||
 | 
					        JOIN blocks_templates ON blocks_templates.id = blocks_audits.hash
 | 
				
			||||||
        JOIN blocks_summaries ON blocks_summaries.id = blocks_audits.hash
 | 
					        JOIN blocks_summaries ON blocks_summaries.id = blocks_audits.hash
 | 
				
			||||||
        WHERE blocks_audits.hash = "${hash}"
 | 
					        WHERE blocks_audits.hash = "${hash}"
 | 
				
			||||||
      `);
 | 
					      `);
 | 
				
			||||||
 | 
				
			|||||||
@ -36,11 +36,11 @@ class BlocksSummariesRepository {
 | 
				
			|||||||
    try {
 | 
					    try {
 | 
				
			||||||
      const transactions = JSON.stringify(params.template?.transactions || []);
 | 
					      const transactions = JSON.stringify(params.template?.transactions || []);
 | 
				
			||||||
      await DB.query(`
 | 
					      await DB.query(`
 | 
				
			||||||
        INSERT INTO blocks_summaries (height, id, transactions, template)
 | 
					        INSERT INTO blocks_templates (id, template)
 | 
				
			||||||
        VALUE (?, ?, ?, ?)
 | 
					        VALUE (?, ?)
 | 
				
			||||||
        ON DUPLICATE KEY UPDATE
 | 
					        ON DUPLICATE KEY UPDATE
 | 
				
			||||||
          template = ?
 | 
					          template = ?
 | 
				
			||||||
      `, [params.height, blockId, '[]', transactions, transactions]);
 | 
					      `, [blockId, transactions, transactions]);
 | 
				
			||||||
    } catch (e: any) {
 | 
					    } catch (e: any) {
 | 
				
			||||||
      if (e.errno === 1062) { // ER_DUP_ENTRY - This scenario is possible upon node backend restart
 | 
					      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`);
 | 
					        logger.debug(`Cannot save block template for ${blockId} because it has already been indexed, ignoring`);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user