Skip to content

Commit

Permalink
perf(hapi-evm): get the partial ATH when the old blocks are synchroni…
Browse files Browse the repository at this point in the history
…zing and when it ends increase the timeout
  • Loading branch information
Torresmorah committed Nov 28, 2023
1 parent 2ebfbc7 commit 7c02161
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 16 deletions.
24 changes: 24 additions & 0 deletions hapi-evm/src/models/stats/queries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { gql } from 'graphql-request'

import { coreUtil } from '../../utils'
import { historicalStatsModel } from '..'

import { Stats } from './interfaces'

interface StatsResponse {
Expand All @@ -22,3 +24,25 @@ export const getPartialATH = async () => {

return state
}

export const updateATH = async (partialATH: Stats) => {
if (!partialATH) return
const currentState = await historicalStatsModel.queries.getState()
if (
currentState.tps_all_time_high.transactions_count >
partialATH.ath_transactions_count
)
return
if (
currentState.tps_all_time_high.transactions_count ||
0 < partialATH.ath_transactions_count
) {
await historicalStatsModel.queries.saveOrUpdate({
tps_all_time_high: {
blocks: partialATH.ath_blocks.split(','),
transactions_count: partialATH.ath_transactions_count,
gas_used: partialATH.ath_gas_used
}
})
}
}
30 changes: 14 additions & 16 deletions hapi-evm/src/services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TransactionHash,
TransactionReceipt
} from 'web3-types'
import moment from 'moment'

import {
defaultModel,
Expand All @@ -15,7 +16,9 @@ import {
StatsModel
} from '../models'
import { networkConfig } from '../config'
import moment from 'moment'
import { timeUtil } from '../utils'

import { getATHInRange } from './partial-ath.service'

const httpProvider = new Web3.providers.HttpProvider(networkConfig.evmEndpoint)
const web3 = new Web3(httpProvider)
Expand Down Expand Up @@ -135,7 +138,10 @@ const getBlock = async () => {
const syncOldBlocks = async (): Promise<void> => {
let blocksInserted = 1
const paramStats = await paramModel.queries.getState()
if (paramStats.isSynced) return
if (paramStats.isSynced) {
await timeUtil.sleep(86400)
return
}
const nextBlock = paramStats.nextBlock
const nextBlockToNumber =
paramStats.completeAt ||
Expand All @@ -160,6 +166,11 @@ const syncOldBlocks = async (): Promise<void> => {
!!isUpToDate,
nextBlockToNumber
)
const partialATH = await getATHInRange(
nextBlock - 1,
nextBlock + blocksInserted - 1
)
await StatsModel.queries.updateATH(partialATH)
}

const blockWorker = async () => {
Expand All @@ -171,21 +182,8 @@ const cleanOldBlocks = async () => {
}

const syncATH = async () => {
const currentState = await historicalStatsModel.queries.getState()
const partialATH = await StatsModel.queries.getPartialATH()
if (!partialATH) return
if (
currentState.tps_all_time_high.transactions_count ||
0 < partialATH.ath_transactions_count
) {
await historicalStatsModel.queries.saveOrUpdate({
tps_all_time_high: {
blocks: partialATH.ath_blocks.split(','),
transactions_count: partialATH.ath_transactions_count,
gas_used: partialATH.ath_gas_used
}
})
}
await StatsModel.queries.updateATH(partialATH)
}

const syncBlockWorker = (): defaultModel.Worker => {
Expand Down
59 changes: 59 additions & 0 deletions hapi-evm/src/services/partial-ath.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Stats } from '../models/stats/interfaces'
import { sequelizeUtil } from '../utils'

export const getATHInRange = async (
lowerBlockNumber: number,
upperBlockNumber: number
) => {
const [rows] = await sequelizeUtil.sequelize.query(`
SELECT
ath_in_range.blocks AS ath_blocks,
COALESCE(
(ath_in_range.max_transaction_sum) :: numeric,
(0) :: numeric
) AS ath_transactions_count,
COALESCE(ath_in_range.gas_used_sum, (0) :: numeric) AS ath_gas_used
FROM
(
WITH subquery AS (
SELECT
array_to_string(array_agg(block.number), ',' :: text) AS blocks,
sum(jsonb_array_length(block.transactions)) AS total_transaction_count,
sum(block.gas_used) AS gas_used_sum
FROM
evm.block
WHERE (block.number >= ${lowerBlockNumber} and block.number <= ${upperBlockNumber})
GROUP BY
block."timestamp"
)
SELECT
q2.blocks,
q1.max_transaction_sum,
q2.gas_used_sum
FROM
(
(
SELECT
max(subquery.total_transaction_count) AS max_transaction_sum
FROM
subquery
) q1
JOIN subquery q2 ON (
(
q1.max_transaction_sum = q2.total_transaction_count
)
)
)
LIMIT
1
) ath_in_range
`)

const row = rows[0] as Stats

return {
ath_blocks: row.ath_blocks,
ath_transactions_count: Number(row.ath_transactions_count) || 0,
ath_gas_used: Number(row.ath_gas_used)
} as Stats
}

0 comments on commit 7c02161

Please sign in to comment.