From fa90eb84fc8ef304a87b4beb41b539a0ec4f3665 Mon Sep 17 00:00:00 2001 From: natsee Date: Mon, 22 Jan 2024 13:59:11 +0100 Subject: [PATCH 1/2] Truncate elements_pegs and add primary key instead of drop/create --- backend/src/api/database-migration.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/backend/src/api/database-migration.ts b/backend/src/api/database-migration.ts index 9d0a0a0d1..911576f2d 100644 --- a/backend/src/api/database-migration.ts +++ b/backend/src/api/database-migration.ts @@ -561,8 +561,8 @@ class DatabaseMigration { if (databaseSchemaVersion < 67 && config.MEMPOOL.NETWORK === "liquid") { // Drop and re-create the elements_pegs table - await this.$executeQuery('DROP table IF EXISTS elements_pegs;'); - await this.$executeQuery(this.getCreateElementsTableQuery(), await this.$checkIfTableExists('elements_pegs')); + await this.$executeQuery('TRUNCATE TABLE elements_pegs'); + await this.$executeQuery('ALTER TABLE elements_pegs ADD PRIMARY KEY (txid, txindex);'); await this.$executeQuery(`UPDATE state SET number = 0 WHERE name = 'last_elements_block';`); // Create the federation_addresses table and add the two Liquid Federation change addresses in await this.$executeQuery(this.getCreateFederationAddressesTableQuery(), await this.$checkIfTableExists('federation_addresses')); @@ -816,8 +816,7 @@ class DatabaseMigration { bitcoinaddress varchar(100) NOT NULL, bitcointxid varchar(65) NOT NULL, bitcoinindex int(11) NOT NULL, - final_tx int(11) NOT NULL, - PRIMARY KEY (txid, txindex) + final_tx int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`; } From cef060be2d6efc6df05f543f12d779f79f3c10eb Mon Sep 17 00:00:00 2001 From: natsee Date: Tue, 23 Jan 2024 09:46:15 +0100 Subject: [PATCH 2/2] Only include count in federation address and UTXO queries --- backend/src/api/liquid/elements-parser.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/backend/src/api/liquid/elements-parser.ts b/backend/src/api/liquid/elements-parser.ts index 427779898..0b6840b7b 100644 --- a/backend/src/api/liquid/elements-parser.ts +++ b/backend/src/api/liquid/elements-parser.ts @@ -356,27 +356,30 @@ class ElementsParser { // Get all of the federation addresses one month ago, most balances first public async $getFederationAddressesOneMonthAgo(): Promise { const query = ` - SELECT bitcoinaddress, SUM(amount) AS balance FROM federation_txos - WHERE - (blocktime < UNIX_TIMESTAMP(TIMESTAMPADD(DAY, -30, CURRENT_TIMESTAMP()))) - AND - ((unspent = 1) OR (unspent = 0 AND lasttimeupdate > UNIX_TIMESTAMP(TIMESTAMPADD(DAY, -30, CURRENT_TIMESTAMP())))) - GROUP BY bitcoinaddress ORDER BY balance DESC;`; + SELECT COUNT(*) AS addresses_count_one_month FROM ( + SELECT bitcoinaddress, SUM(amount) AS balance + FROM federation_txos + WHERE + (blocktime < UNIX_TIMESTAMP(TIMESTAMPADD(DAY, -30, CURRENT_TIMESTAMP()))) + AND + ((unspent = 1) OR (unspent = 0 AND lasttimeupdate > UNIX_TIMESTAMP(TIMESTAMPADD(DAY, -30, CURRENT_TIMESTAMP())))) + GROUP BY bitcoinaddress + ) AS result;`; const [rows] = await DB.query(query); - return rows; + return rows[0]; } // Get all of the UTXOs held by the federation one month ago, most recent first public async $getFederationUtxosOneMonthAgo(): Promise { const query = ` - SELECT txid, txindex, bitcoinaddress, amount, blocknumber, blocktime, pegtxid, pegindex FROM federation_txos + SELECT COUNT(*) AS utxos_count_one_month FROM federation_txos WHERE (blocktime < UNIX_TIMESTAMP(TIMESTAMPADD(DAY, -30, CURRENT_TIMESTAMP()))) AND ((unspent = 1) OR (unspent = 0 AND lasttimeupdate > UNIX_TIMESTAMP(TIMESTAMPADD(DAY, -30, CURRENT_TIMESTAMP())))) ORDER BY blocktime DESC;`; const [rows] = await DB.query(query); - return rows; + return rows[0]; } }