From 540b3aa66387f451b037eb0a7ef097a691bf7dd3 Mon Sep 17 00:00:00 2001 From: Thomas Kammerlocher Date: Fri, 29 Nov 2024 11:37:19 +0100 Subject: [PATCH] Fix/non nullable field in token asset (#905) * fix: switching back to direct saving mode when the chainfollower reached the tip * fix: switching back to direct saving mode when the chainfollower reached the tip * fix: switching back to direct saving mode when the chainfollower reached the tip --- .../api-cardano-db-hasura/src/ChainFollower.ts | 17 ++++++++++++----- .../api-cardano-db-hasura/src/background.ts | 15 +-------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/packages/api-cardano-db-hasura/src/ChainFollower.ts b/packages/api-cardano-db-hasura/src/ChainFollower.ts index b1bf1f49..fe1e2d34 100644 --- a/packages/api-cardano-db-hasura/src/ChainFollower.ts +++ b/packages/api-cardano-db-hasura/src/ChainFollower.ts @@ -8,7 +8,7 @@ import util, { assetFingerprint, errors, RunnableModuleState } from '@cardano-gr import PgBoss from 'pg-boss' import { dummyLogger, Logger } from 'ts-log' import { createInteractionContextWithLogger } from './util' -import { PointOrOrigin, BlockPraos, BlockBFT } from '@cardano-ogmios/schema' +import { PointOrOrigin, BlockPraos, BlockBFT, Tip, Origin } from '@cardano-ogmios/schema' import { HasuraBackgroundClient } from './HasuraBackgroundClient' import { DbConfig } from './typeAliases' import { ChainSynchronizationClient } from '@cardano-ogmios/client/dist/ChainSynchronization' @@ -40,6 +40,7 @@ export class ChainFollower { application_name: 'cardano-graphql', ...this.queueConfig }) + this.logger.info({ module: MODULE_NAME }, 'Connecting to queue') await pRetry(async () => { const context = await createInteractionContextWithLogger(ogmiosConfig, this.logger, MODULE_NAME, async () => { await this.shutdown() @@ -63,7 +64,7 @@ export class ChainFollower { } requestNext() }, - rollForward: async ({ block }, requestNext) => { + rollForward: async ({ block, tip }, requestNext) => { try { let b switch (block.type) { @@ -83,7 +84,7 @@ export class ChainFollower { const policyId = entry[0] const assetNames = Object.keys(entry[1]) for (const assetName of assetNames) { - await this.saveAsset(policyId, assetName, b) + await this.saveAsset(policyId, assetName, b, tip) } } } @@ -109,7 +110,7 @@ export class ChainFollower { this.logger.info({ module: MODULE_NAME }, 'Initialized') } - async saveAsset (policyId: string, assetName: string | undefined, b: BlockPraos | BlockBFT) { + async saveAsset (policyId: string, assetName: string | undefined, b: BlockPraos | BlockBFT, tip: Tip | Origin) { const assetId = `${policyId}${assetName !== undefined ? assetName : ''}` const asset = { assetId, @@ -121,7 +122,13 @@ export class ChainFollower { // introducing a caching to speed things up. The GraphQL insertAssets takes a lot of time. // Saving when > 1000 assets in the cache or every minute this.cacheAssets.push(asset) - if (this.cacheAssets.length > 1000 || (Date.now() - this.cacheTimer) / 1000 > 60) { + let isTip = false + try { + isTip = (tip as Tip).slot === b.slot + } catch (e) { + this.logger.debug({ module: MODULE_NAME }, 'Sync is not at tip. Using a cache to save Assets every minute to increase catching up speed.') + } + if (isTip || this.cacheAssets.length > 1000 || (Date.now() - this.cacheTimer) / 1000 > 60) { this.cacheTimer = Date.now() // resetting the timer const response = await this.hasuraClient.insertAssets(this.cacheAssets) this.cacheAssets = [] diff --git a/packages/api-cardano-db-hasura/src/background.ts b/packages/api-cardano-db-hasura/src/background.ts index afccef4f..b92126a7 100644 --- a/packages/api-cardano-db-hasura/src/background.ts +++ b/packages/api-cardano-db-hasura/src/background.ts @@ -6,7 +6,6 @@ import { CustomError } from 'ts-custom-error' import fs from 'fs-extra' import { DbConfig } from './typeAliases' import { PointOrOrigin } from '@cardano-ogmios/schema' -import { Schema } from '@cardano-ogmios/client' // Todo: Hoist to util package next major version export class MissingConfig extends CustomError { public constructor (message: string) { @@ -178,20 +177,8 @@ function filterAndTypecastEnvs (env: any) { ) const db = new Db(config.db, logger) const getChainSyncPoints = async (): Promise => { - const chainSyncPoint = (config.chainfollower) as Schema.Point - logger.info(chainSyncPoint) const mostRecentPoint = await hasuraBackgroundClient.getMostRecentPointWithNewAsset() - if (mostRecentPoint !== null) { - if (chainSyncPoint.slot && chainSyncPoint.slot > mostRecentPoint.slot) { - return [chainSyncPoint, 'origin'] - } else { - return [mostRecentPoint, 'origin'] - } - } else if (chainSyncPoint.slot && chainSyncPoint.id) { - return [chainSyncPoint, 'origin'] - } else { - return ['origin'] - } + return mostRecentPoint !== null ? [mostRecentPoint, 'origin'] : ['origin'] } await db.init({ onDbInit: () => hasuraBackgroundClient.shutdown(),