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

Add limit of 1 to missing block queries #544

Merged
merged 3 commits into from
Sep 17, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Unreleased

### Improvements

* Add limit of 1 to missing block queries [#544](https://github.com/provenance-io/explorer-service/pull/544)

## [v5.11.0](https://github.com/provenance-io/explorer-service/releases/tag/v5.11.0) - 2024-08-27

### Improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.provenance.explorer.domain.entities

import cosmos.base.tendermint.v1beta1.Query
import io.provenance.explorer.OBJECT_MAPPER
import io.provenance.explorer.domain.core.logger
import io.provenance.explorer.domain.core.sql.DateTrunc
import io.provenance.explorer.domain.core.sql.Distinct
import io.provenance.explorer.domain.core.sql.ExtractDOW
Expand Down Expand Up @@ -265,24 +266,25 @@ class MissedBlocksRecord(id: EntityID<Int>) : IntEntity(id) {
fun findLatestForVal(valconsAddr: String) = transaction {
MissedBlocksRecord.find { MissedBlocksTable.valConsAddr eq valconsAddr }
.orderBy(Pair(MissedBlocksTable.blockHeight, SortOrder.DESC))
.limit(1)
.firstOrNull()
}

fun findForValFirstUnderHeight(valconsAddr: String, height: Int) = transaction {
MissedBlocksRecord
.find { (MissedBlocksTable.valConsAddr eq valconsAddr) and (MissedBlocksTable.blockHeight lessEq height) }
.orderBy(Pair(MissedBlocksTable.blockHeight, SortOrder.DESC))
.limit(1)
.firstOrNull()
}

fun insert(height: Int, valconsAddr: String) = transaction {
fun calculateMissedAndInsert(height: Int, valconsAddr: String) = transaction {
val startTime = System.currentTimeMillis()

val (running, total, updateFromHeight) = findLatestForVal(valconsAddr)?.let { rec ->
when {
// If current height follows the last height, continue sequences
rec.blockHeight == height - 1 -> listOf(rec.runningCount, rec.totalCount, null)
rec.blockHeight > height ->
// If current height is under the found height, find the last one directly under current
// height, and see if it follows the sequence
when (val last = findForValFirstUnderHeight(valconsAddr, height - 1)) {
null -> listOf(0, 0, height)
else -> listOf(
Expand All @@ -291,7 +293,6 @@ class MissedBlocksRecord(id: EntityID<Int>) : IntEntity(id) {
height
)
}
// Restart running sequence
else -> listOf(0, rec.totalCount, null)
}
} ?: listOf(0, 0, null)
Expand All @@ -303,10 +304,17 @@ class MissedBlocksRecord(id: EntityID<Int>) : IntEntity(id) {
it[this.totalCount] = total!! + 1
}

// Update following height records
if (updateFromHeight != null) {
updateRecords(updateFromHeight, valconsAddr, running!! + 1, total!! + 1)
}

val endTime = System.currentTimeMillis()
val duration = endTime - startTime

// TODO: remove warning if problem is fixed after adding limit to queries See: https://github.com/provenance-io/explorer-service/issues/546
if (duration > 1000) {
logger().warn("Processing calculateMissedAndInsert took ${duration}ms for height: $height and valconsAddr: $valconsAddr")
}
}

fun updateRecords(height: Int, valconsAddr: String, currRunning: Int, currTotal: Int) = transaction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ class ValidatorService(

currentVals.validatorsList.forEach { vali ->
if (!signatures.contains(vali.address)) {
MissedBlocksRecord.insert(lastBlock.height.toInt(), vali.address)
MissedBlocksRecord.calculateMissedAndInsert(lastBlock.height.toInt(), vali.address)
}
}
}
Expand Down
Loading