Skip to content

Commit

Permalink
Fix/non nullable field in token asset (#905)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Kammerlo authored Nov 29, 2024
1 parent 854987a commit 540b3aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
17 changes: 12 additions & 5 deletions packages/api-cardano-db-hasura/src/ChainFollower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()
Expand All @@ -63,7 +64,7 @@ export class ChainFollower {
}
requestNext()
},
rollForward: async ({ block }, requestNext) => {
rollForward: async ({ block, tip }, requestNext) => {
try {
let b
switch (block.type) {
Expand All @@ -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)
}
}
}
Expand All @@ -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,
Expand All @@ -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 = []
Expand Down
15 changes: 1 addition & 14 deletions packages/api-cardano-db-hasura/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -178,20 +177,8 @@ function filterAndTypecastEnvs (env: any) {
)
const db = new Db(config.db, logger)
const getChainSyncPoints = async (): Promise<PointOrOrigin[]> => {
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(),
Expand Down

0 comments on commit 540b3aa

Please sign in to comment.