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

Fix/syncing old blocks 1385 #1386

Merged
merged 4 commits into from
Nov 11, 2023
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
12 changes: 8 additions & 4 deletions hapi-evm/src/models/block/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,39 @@ const internal_get = async <T>(
// such as limit, order, etc
where: object,
order: object | null,
limit: number | null,
attributes: string,
operation?: string
): Promise<T> => {
const query = gql`
${type} (${parameters}) {
${table}${
operation ? `_${operation}` : ''
}(where: $where, order_by: $order) {
}(where: $where, order_by: $order, limit: $limit) {
${attributes}
}
}
`

return await coreUtil.hasura.default.request<T>(query, {
where,
order
order,
limit
})
}

export const exist = async (hashOrNumber: string | number) => {
const result = await internal_get<BlockAggregateResponse>(
'query',
'evm_block',
'$where: evm_block_bool_exp!, $order: [evm_block_order_by!]',
'$where: evm_block_bool_exp!, $order: [evm_block_order_by!], $limit: Int',
{
[typeof hashOrNumber === 'string' ? 'hash' : 'number']: {
_eq: hashOrNumber
}
},
null,
null,
'aggregate { count }',
'aggregate'
)
Expand All @@ -83,9 +86,10 @@ const get = async (
const result = await internal_get<BlockResponse>(
'query',
'evm_block',
'$where: evm_block_bool_exp!, $order: [evm_block_order_by!]',
'$where: evm_block_bool_exp!, $order: [evm_block_order_by!], $limit: Int',
where,
order,
!many ? 1 : null,
'hash, gas_used, transactions, number, timestamp'
)

Expand Down
5 changes: 2 additions & 3 deletions hapi-evm/src/models/historical-stats/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ export const getState = async () => {
}
}
`
const data = await coreUtil.hasura.default.request<HistoricalStatsResponse>(
query
)
const data =
await coreUtil.hasura.default.request<HistoricalStatsResponse>(query)

if (!data.evm_historical_stats.length) {
return defaultHistoricalStats
Expand Down
1 change: 1 addition & 0 deletions hapi-evm/src/models/param/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export interface Param {
id: string
next_block: number
is_synced: boolean
complete_at: number
}
29 changes: 20 additions & 9 deletions hapi-evm/src/models/param/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ interface ParamOneResponse {
const defaultParam = {
id: '00000000-0000-0000-0000-000000000000',
nextBlock: 0,
isSynced: false
isSynced: false,
completeAt: null
}

export const save = async (nextBlock: number, isSynced: boolean = false) => {
export const save = async (
nextBlock: number,
isSynced: boolean = false,
completeAt: number
) => {
const mutation = gql`
mutation ($payload: evm_param_insert_input!) {
insert_evm_param_one(object: $payload) {
Expand All @@ -33,7 +38,8 @@ export const save = async (nextBlock: number, isSynced: boolean = false) => {
{
payload: {
next_block: nextBlock,
is_synced: isSynced
is_synced: isSynced,
complete_at: completeAt
}
}
)
Expand All @@ -44,7 +50,8 @@ export const save = async (nextBlock: number, isSynced: boolean = false) => {
export const update = async (
id: string,
nextBlock: number,
isSynced: boolean = false
isSynced: boolean = false,
completeAt: number
) => {
const mutation = gql`
mutation ($id: uuid!, $payload: evm_param_set_input) {
Expand All @@ -60,7 +67,8 @@ export const update = async (
id,
payload: {
next_block: nextBlock,
is_synced: isSynced
is_synced: isSynced,
complete_at: completeAt
}
})
}
Expand All @@ -75,6 +83,7 @@ export const getState = async () => {
id
next_block
is_synced
complete_at
}
}
`
Expand All @@ -89,21 +98,23 @@ export const getState = async () => {
return {
id: state.id,
nextBlock: state.next_block,
isSynced: state.is_synced
isSynced: state.is_synced,
completeAt: state.complete_at
}
}

export const saveOrUpdate = async (
nextBlock: number,
isSynced: boolean
isSynced: boolean,
completeAt: number
): Promise<void> => {
const currentState = await getState()

if (currentState === defaultParam) {
await save(nextBlock, isSynced)
await save(nextBlock, isSynced, completeAt)

return
}

await update(currentState.id, nextBlock, isSynced)
await update(currentState.id, nextBlock, isSynced, completeAt)
}
16 changes: 11 additions & 5 deletions hapi-evm/src/models/transaction/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,33 @@ const internal_get = async <T>(
// TODO: not only accept where but also additional content
// such as limit, order, etc
where: object,
limit: number | null,
attributes: string,
operation?: string
): Promise<T> => {
const gqlObj = gql`
${type} (${parameters}) {
${table}${operation ? `_${operation}` : ''}(where: $where) {
${table}${
operation ? `_${operation}` : ''
}(where: $where, limit: $limit) {
${attributes}
}
}
`

return await coreUtil.hasura.default.request<T>(gqlObj, {
where
where,
limit
})
}

export const exist = async (hash: string) => {
const result = await internal_get<TransactionAggregateResponse>(
Operation.query,
'evm_transaction',
'$where: evm_transaction_bool_exp!',
'$where: evm_transaction_bool_exp!, $limit: Int',
{ hash: { _eq: hash } },
null,
'aggregate { count }',
'aggregate'
)
Expand All @@ -63,9 +68,10 @@ const get = async (where: object, many = false) => {
const result = await internal_get<TransactionResponse>(
'query',
'evm_transaction',
'$where: evm_transaction_bool_exp!',
'$where: evm_transaction_bool_exp!, $limit: Int',
where,
'hash, gas_used, transactions, number, timestamp'
!many ? 1 : null,
'hash, block_hash, block_number, gas, gas_price'
)

return many ? result.evm_transaction : result.evm_transaction[0]
Expand Down
31 changes: 19 additions & 12 deletions hapi-evm/src/services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,32 @@ const getBlock = async () => {
}

const syncOldBlocks = async (): Promise<void> => {
let blocksInserted = 1
const paramStats = await paramModel.queries.getState()
if (paramStats.isSynced) return
const nextBlock = paramStats.nextBlock
const isUpToDate = await blockModel.queries.default.get({
number: { _eq: nextBlock }
})
const nextBlockToNumber =
paramStats.completeAt ||
(await blockModel.queries.default.getNextBlock(nextBlock))[0]?.number
if (!nextBlockToNumber) return
const isUpToDate = nextBlock >= nextBlockToNumber
if (!isUpToDate) {
const nextBlockTo = await blockModel.queries.default.getNextBlock(nextBlock)
const nextBlockToNumber = nextBlockTo[0]?.number || 0
if (nextBlockToNumber > nextBlock) {
console.log(
`🚦 Syncing blocks behind, pending ${nextBlockToNumber - nextBlock} `
)
console.log(
`🚦 Syncing blocks behind, pending ${nextBlockToNumber - nextBlock} `
)
blocksInserted = Math.min(100, nextBlockToNumber - nextBlock)
const blockPromises = []
for (let index = 0; index < blocksInserted; index++) {
blockPromises.push(syncFullBlock(nextBlock + index))
}
await syncFullBlock(nextBlock)
await Promise.allSettled(blockPromises)
} else {
console.log(`Syncing old blocks complete at ${moment().format()}`)
}
await paramModel.queries.saveOrUpdate(
nextBlock + 1 * Number(!isUpToDate),
!!isUpToDate
nextBlock + blocksInserted * Number(!isUpToDate),
!!isUpToDate,
nextBlockToNumber
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "evm"."param" add column "complete_at" numeric
-- null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "evm"."param" add column "complete_at" numeric
null;