Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/transaction pending fixes #138

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions prisma/rsk-explorer-database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/servicesUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`)
Expand Down
10 changes: 6 additions & 4 deletions src/repositories/blocks.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/classes/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Block extends BcThing {
}
}

async save () {
async save (tipBlock = false) {
const { number, forceSaveBcStats } = this
let data
try {
Expand All @@ -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) {
Expand Down