Skip to content

Commit

Permalink
chore: fixed get missing epochs
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Nov 19, 2024
1 parent 640ad09 commit d0cbc3c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
9 changes: 0 additions & 9 deletions server/api/[version]/epochs/missing.get.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import { getRpcClient } from '~~/server/lib/client'
import { consola } from 'consola'
import { getRange } from 'nimiq-validators-trustscore'

function err(error: any) {
consola.error(error)
return createError(error)
}

export default defineEventHandler(async () => {
const rpcClient = getRpcClient()

const range = await getRange(rpcClient)
if (!range)
return err('Could not get range')

const missingEpochs = await findMissingEpochs(range)
return missingEpochs
})
13 changes: 7 additions & 6 deletions server/utils/activities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Activity, EpochsActivities, Range } from 'nimiq-validators-trustscore'
import type { NewActivity } from './drizzle'
import { eq, not } from 'drizzle-orm'
import { eq, gte, lte, not } from 'drizzle-orm'
import { storeValidator } from './validators'

/**
Expand All @@ -11,11 +11,12 @@ export async function findMissingEpochs(range: Range) {
.selectDistinct({ epochBlockNumber: tables.activity.epochNumber })
.from(tables.activity)
.where(and(
eq(tables.activity.epochNumber, range.toEpoch),
// If every entry of the same epoch contains a likelihood of -1, then we consider it as missing
gte(tables.activity.epochNumber, range.fromEpoch),
lte(tables.activity.epochNumber, range.toEpoch),
// If any entry of the same epoch contains a likelihood of -1, then we consider it as missing
not(eq(tables.activity.likelihood, -1)),
))
.execute()
.all()
.then(r => r.map(r => r.epochBlockNumber))

const missingEpochs = []
Expand Down Expand Up @@ -52,7 +53,7 @@ export async function storeSingleActivity({ address, activity, epochNumber }: St
return
// If we ever move out of cloudflare we could use transactions to avoid inconsistencies and improve performance
// Cloudflare D1 does not support transactions: https://github.com/cloudflare/workerd/blob/e78561270004797ff008f17790dae7cfe4a39629/src/workerd/api/sql-test.js#L252-L253
const { likelihood, rewarded, missed, dominanceRatioViaBalance: _dominanceRatioViaBalance, dominanceRatioViaSlots: _dominanceRatioViaSlots, balance: _balance } = await useDrizzle()
const { dominanceRatioViaBalance: _dominanceRatioViaBalance, dominanceRatioViaSlots: _dominanceRatioViaSlots, balance: _balance } = await useDrizzle()
.select({
likelihood: tables.activity.likelihood,
rewarded: tables.activity.rewarded,
Expand All @@ -76,6 +77,6 @@ export async function storeSingleActivity({ address, activity, epochNumber }: St
eq(tables.activity.epochNumber, epochNumber),
eq(tables.activity.validatorId, validatorId),
))
const activityDb: NewActivity = { likelihood, rewarded, missed, epochNumber, validatorId, dominanceRatioViaSlots, dominanceRatioViaBalance, balance }
const activityDb: NewActivity = { ...activity!, epochNumber, validatorId, dominanceRatioViaSlots, dominanceRatioViaBalance, balance }
await useDrizzle().insert(tables.activity).values(activityDb)
}

0 comments on commit d0cbc3c

Please sign in to comment.