Skip to content

Commit

Permalink
Remove the logic for calculating running_count and total_count in…
Browse files Browse the repository at this point in the history
… the `missed_blocks` (#548)

* remove logic to update running count and total count

* add temp logging

* add temp counter

* comment out calc latency function

* remove tmp logging, uncomment latency calc

* fix formatting

* add issue link to todo

* add change log entry
  • Loading branch information
nullpointer0x00 authored Sep 19, 2024
1 parent 5d860ac commit 447fe17
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 74 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* Add limit of 1 to missing block queries [#544](https://github.com/provenance-io/explorer-service/pull/544)
* Removes the logic for calculating `running_count` and `total_count` in the `missed_blocks` [#548](https://github.com/provenance-io/explorer-service/pull/548)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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 @@ -58,9 +57,7 @@ import org.jetbrains.exposed.sql.jodatime.datetime
import org.jetbrains.exposed.sql.leftJoin
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.statements.BatchUpdateStatement
import org.jetbrains.exposed.sql.sum
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update
import org.joda.time.DateTime
Expand Down Expand Up @@ -263,79 +260,13 @@ class MissedBlocksRecord(id: EntityID<Int>) : IntEntity(id) {
query.exec(arguments).map { it.getString("val_cons_address") }
}

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 calculateMissedAndInsert(height: Int, valconsAddr: String) = transaction {
val startTime = System.currentTimeMillis()

val (running, total, updateFromHeight) = findLatestForVal(valconsAddr)?.let { rec ->
when {
rec.blockHeight == height - 1 -> listOf(rec.runningCount, rec.totalCount, null)
rec.blockHeight > height ->
when (val last = findForValFirstUnderHeight(valconsAddr, height - 1)) {
null -> listOf(0, 0, height)
else -> listOf(
if (last.blockHeight == height - 1) last.runningCount else 0,
last.totalCount,
height
)
}
else -> listOf(0, rec.totalCount, null)
}
} ?: listOf(0, 0, null)

fun insert(height: Int, valconsAddr: String) = transaction {
MissedBlocksTable.insertIgnore {
it[this.blockHeight] = height
it[this.valConsAddr] = valconsAddr
it[this.runningCount] = running!! + 1
it[this.totalCount] = total!! + 1
}

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 {
val records = MissedBlocksRecord
.find { (MissedBlocksTable.valConsAddr eq valconsAddr) and (MissedBlocksTable.blockHeight greater height) }
.orderBy(Pair(MissedBlocksTable.blockHeight, SortOrder.ASC))

BatchUpdateStatement(MissedBlocksTable).apply {
var lastHeight = height
var lastRunning = currRunning
var lastTotal = currTotal
records.forEach {
addBatch(it.id)
val running = if (lastHeight == it.blockHeight - 1) lastRunning + 1 else 1
this[MissedBlocksTable.runningCount] = running
this[MissedBlocksTable.totalCount] = lastTotal + 1
lastHeight = it.blockHeight
lastRunning = running
lastTotal += 1
}
execute(TransactionManager.current())
// TODO: remove these column from database See: https://github.com/provenance-io/explorer-service/issues/549
it[this.runningCount] = -1
it[this.totalCount] = -1
}
}
}
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.calculateMissedAndInsert(lastBlock.height.toInt(), vali.address)
MissedBlocksRecord.insert(lastBlock.height.toInt(), vali.address)
}
}
}
Expand Down

0 comments on commit 447fe17

Please sign in to comment.