From 176c38feb8dc6967668ac03270e57bf73256c81d Mon Sep 17 00:00:00 2001 From: IOVGomezdn Date: Wed, 27 Mar 2024 20:05:48 -0300 Subject: [PATCH 1/2] feat: add index to tx_pending timestamp --- prisma/rsk-explorer-database.sql | 1 + prisma/schema.prisma | 2 ++ 2 files changed, 3 insertions(+) diff --git a/prisma/rsk-explorer-database.sql b/prisma/rsk-explorer-database.sql index dcc23bf0..bfc89274 100644 --- a/prisma/rsk-explorer-database.sql +++ b/prisma/rsk-explorer-database.sql @@ -142,6 +142,7 @@ input VARCHAR NOT NULL, status VARCHAR NOT NULL, timestamp VARCHAR NOT NULL DEFAULT CAST(DATE_PART('epoch', NOW()) AS VARCHAR) ); +CREATE INDEX ON transaction_pending(timestamp); CREATE TABLE transaction_in_pool ( hash VARCHAR(66), diff --git a/prisma/schema.prisma b/prisma/schema.prisma index daa699b2..d1fbe8f9 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -304,6 +304,8 @@ model transaction_pending { input String @db.VarChar status String @db.VarChar timestamp String @default(dbgenerated("(date_part('epoch'::text, now()))::character varying")) @db.VarChar + + @@index([timestamp]) } model tx_pool { From 01c2c78a9c3efa2d90507a1ee78c0044786097ae Mon Sep 17 00:00:00 2001 From: IOVGomezdn Date: Wed, 27 Mar 2024 20:16:23 -0300 Subject: [PATCH 2/2] feat: make removed txPending status update every 1 hour --- src/lib/servicesUtils.js | 2 +- src/repositories/blocks.repository.js | 10 ++++++---- src/services/classes/Block.js | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lib/servicesUtils.js b/src/lib/servicesUtils.js index cc438fee..bda411f4 100644 --- a/src/lib/servicesUtils.js +++ b/src/lib/servicesUtils.js @@ -19,7 +19,7 @@ export async function insertBlock (number, blocksBase, { log, tipBlock = false } // insert block let savingTime = Date.now() - await block.save() + await block.save(tipBlock) savingTime = Date.now() - savingTime log.info(`Block ${number} saved. Fetched in ${fetchingTime} ms. Saved in ${savingTime} ms.`) diff --git a/src/repositories/blocks.repository.js b/src/repositories/blocks.repository.js index b1ce5007..b9bb750d 100644 --- a/src/repositories/blocks.repository.js +++ b/src/repositories/blocks.repository.js @@ -35,7 +35,7 @@ export function getBlocksRepository (prismaClient) { insertOne (data) { return prismaClient.block.createMany({ data: rawBlockToEntity(data), skipDuplicates: true }) }, - async saveBlockData (data) { + async saveBlockData (data, tipBlock) { const { block, transactions, internalTransactions, events, tokenAddresses, addresses, balances, latestBalances, status } = data if (!transactions.length && block.number > 0) throw new Error(`Invalid block ${block.number}. Missing transactions`) @@ -59,9 +59,11 @@ export function getBlocksRepository (prismaClient) { queries.push(txPendingRepository.deleteOne({ hash: tx.hash })) } - // Set status 'REMOVED' to any old transactions stuck on database - const oneHourAgo = String(Math.floor(new Date().getTime() / 1000) - 3600) - queries.push(txPendingRepository.updateMany({ timestamp: { lte: oneHourAgo } }, { status: 'REMOVED' })) + // Set status 'REMOVED' to any old transactions stuck on database every 120 blocks + if (tipBlock && block.number % 120 === 0) { + const oneHourAgo = String(Math.floor(new Date().getTime() / 1000) - 3600) + queries.push(txPendingRepository.updateMany({ timestamp: { lte: oneHourAgo } }, { status: 'REMOVED' })) + } return queries } diff --git a/src/services/classes/Block.js b/src/services/classes/Block.js index f86a0336..8ff989c5 100644 --- a/src/services/classes/Block.js +++ b/src/services/classes/Block.js @@ -37,7 +37,7 @@ export class Block extends BcThing { } } - async save () { + async save (tipBlock = false) { const { number, forceSaveBcStats } = this let data try { @@ -54,7 +54,7 @@ export class Block extends BcThing { data.status = this.status // save block and all related data - await this.repository.saveBlockData(data) + await this.repository.saveBlockData(data, tipBlock) // save blockchain stats. Only for tip blocks (requires block and addresses inserted) if (this.isTipBlock || forceSaveBcStats) {