From 0c1fa2b4aa22a79e674fb99b17ad41b753dad749 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Wed, 16 Feb 2022 15:19:16 +0900 Subject: [PATCH] Cleanup blocks/pools fields data type - Index more block data --- backend/src/api/database-migration.ts | 30 ++++++++++++++++---- backend/src/repositories/BlocksRepository.ts | 15 +++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/backend/src/api/database-migration.ts b/backend/src/api/database-migration.ts index b8557ff65..f83f95034 100644 --- a/backend/src/api/database-migration.ts +++ b/backend/src/api/database-migration.ts @@ -6,7 +6,7 @@ import logger from '../logger'; const sleep = (ms: number) => new Promise(res => setTimeout(res, ms)); class DatabaseMigration { - private static currentVersion = 5; + private static currentVersion = 6; private queryTimeout = 120000; private statisticsAddedIndexed = false; @@ -76,6 +76,7 @@ class DatabaseMigration { private async $createMissingTablesAndIndexes(databaseSchemaVersion: number) { await this.$setStatisticsAddedIndexedFlag(databaseSchemaVersion); + const isBitcoin = ['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK); const connection = await DB.pool.getConnection(); try { await this.$executeQuery(connection, this.getCreateElementsTableQuery(), await this.$checkIfTableExists('elements_pegs')); @@ -90,6 +91,29 @@ class DatabaseMigration { await this.$executeQuery(connection, 'DROP table IF EXISTS blocks;'); await this.$executeQuery(connection, this.getCreateBlocksTableQuery(), await this.$checkIfTableExists('blocks')); } + if (databaseSchemaVersion < 5 && isBitcoin === true) { + await this.$executeQuery(connection, 'ALTER TABLE blocks ADD `reward` double unsigned NOT NULL DEFAULT "0"'); + } + + if (databaseSchemaVersion < 6 && isBitcoin === true) { + // Cleanup original blocks fields type + await this.$executeQuery(connection, 'ALTER TABLE blocks MODIFY `height` integer unsigned NOT NULL DEFAULT "0"'); + await this.$executeQuery(connection, 'ALTER TABLE blocks MODIFY `tx_count` smallint unsigned NOT NULL DEFAULT "0"'); + await this.$executeQuery(connection, 'ALTER TABLE blocks MODIFY `size` integer unsigned NOT NULL DEFAULT "0"'); + await this.$executeQuery(connection, 'ALTER TABLE blocks MODIFY `weight` integer unsigned NOT NULL DEFAULT "0"'); + await this.$executeQuery(connection, 'ALTER TABLE blocks MODIFY `difficulty` double NOT NULL DEFAULT "0"'); + // We also fix the pools.id type so we need to drop/re-create the foreign key + await this.$executeQuery(connection, 'ALTER TABLE blocks DROP FOREIGN KEY IF EXISTS `blocks_ibfk_1`'); + await this.$executeQuery(connection, 'ALTER TABLE pools MODIFY `id` smallint unsigned AUTO_INCREMENT'); + await this.$executeQuery(connection, 'ALTER TABLE blocks MODIFY `pool_id` smallint unsigned NULL'); + await this.$executeQuery(connection, 'ALTER TABLE blocks ADD FOREIGN KEY (`pool_id`) REFERENCES `pools` (`id`)'); + // Add new block indexing fields + await this.$executeQuery(connection, 'ALTER TABLE blocks ADD `version` integer unsigned NOT NULL DEFAULT "0"'); + await this.$executeQuery(connection, 'ALTER TABLE blocks ADD `bits` integer unsigned NOT NULL DEFAULT "0"'); + await this.$executeQuery(connection, 'ALTER TABLE blocks ADD `nonce` bigint unsigned NOT NULL DEFAULT "0"'); + await this.$executeQuery(connection, 'ALTER TABLE blocks ADD `merkle_root` varchar(65) NOT NULL DEFAULT ""'); + await this.$executeQuery(connection, 'ALTER TABLE blocks ADD `previous_block_hash` varchar(65) NULL'); + } connection.release(); } catch (e) { connection.release(); @@ -229,10 +253,6 @@ class DatabaseMigration { } } - if (version < 5 && (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === true)) { - queries.push('ALTER TABLE blocks ADD `reward` double unsigned NOT NULL DEFAULT "0"'); - } - return queries; } diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index 6e74e9435..adc3a1f31 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -20,12 +20,14 @@ class BlocksRepository { height, hash, blockTimestamp, size, weight, tx_count, coinbase_raw, difficulty, pool_id, fees, fee_span, median_fee, - reward + reward, version, bits, nonce, + merkle_root, previous_block_hash ) VALUE ( ?, ?, FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, - ? + ?, ?, ?, ?, + ?, ? )`; const params: any[] = [ @@ -37,11 +39,16 @@ class BlocksRepository { block.tx_count, '', block.difficulty, - block.extras?.pool?.id, // Should always be set to something + block.extras.pool?.id, // Should always be set to something 0, '[]', block.extras.medianFee ?? 0, - block.extras?.reward ?? 0, + block.extras.reward ?? 0, + block.version, + block.bits, + block.nonce, + block.merkle_root, + block.previousblockhash ]; // logger.debug(query);