Skip to content

Commit

Permalink
fix: Replaces the time-based logic to determine sync progress and ini…
Browse files Browse the repository at this point in the history
…tialisation state

Enabled by having direct access to cardano-node via cardano-cli,
the node tip is used as the base value to determine the sync progress
of cardano-db-sync. In addition, the initialisation state is now
determined by asserting the most recent epoch in the table matches
the tip's epochNo

Fixes #248
  • Loading branch information
rhyslbw committed Jan 25, 2021
1 parent 01ade07 commit b29ef3e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
1 change: 0 additions & 1 deletion packages/api-cardano-db-hasura/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"apollo-server": "^2.15.1",
"bignumber.js": "^9.0.1",
"cross-fetch": "^3.0.4",
"dayjs": "^1.8.29",
"graphql": "14.5.8",
"graphql-bigint": "^1.0.0",
"graphql-scalars": "^1.2.1",
Expand Down
10 changes: 8 additions & 2 deletions packages/api-cardano-db-hasura/src/CardanoNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class CardanoNodeClient {
constructor (
private cardanoCli: CardanoCli,
pollingInterval: number,
private currentEraFirstSlot: number,
readonly currentEraFirstSlot: number,
private logger: Logger = dummyLogger
) {
this.currentEraFirstSlot = currentEraFirstSlot
Expand All @@ -35,10 +35,16 @@ export class CardanoNodeClient {
)
}

public async getTip () {
const tip = this.cardanoCli.getTip()
this.logger.debug('getTip', { module: 'CardanoNodeClient', value: tip })
return tip
}

public async initialize () {
await pRetry(async () => {
await fs.stat(process.env.CARDANO_NODE_SOCKET_PATH)
const { slotNo } = await this.cardanoCli.getTip()
const { slotNo } = await this.getTip()
if (slotNo < this.currentEraFirstSlot) {
this.logger.debug('cardano-node tip', { module: 'CardanoNodeClient', value: slotNo })
this.logger.debug('currentEraFirstSlot', { module: 'CardanoNodeClient', value: this.currentEraFirstSlot })
Expand Down
23 changes: 14 additions & 9 deletions packages/api-cardano-db-hasura/src/HasuraClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { createHttpLink } from 'apollo-link-http'
import util, { DataFetcher } from '@cardano-graphql/util'
import { exec } from 'child_process'
import fetch from 'cross-fetch'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import { DocumentNode, GraphQLSchema, print } from 'graphql'
import { introspectSchema, wrapSchema } from '@graphql-tools/wrap'
import pRetry from 'p-retry'
Expand All @@ -19,8 +17,6 @@ import {
import { dummyLogger, Logger } from 'ts-log'
import BigNumber from 'bignumber.js'

dayjs.extend(utc)

export class HasuraClient {
private client: ApolloClient<NormalizedCacheObject>
private applyingSchemaAndMetadata: boolean
Expand Down Expand Up @@ -252,21 +248,30 @@ export class HasuraClient {
}
}

public async getMeta () {
public async getMeta (nodeTipBlockNumber: number) {
const result = await this.client.query({
query: gql`query {
epochs (limit: 1, order_by: { number: desc }) {
number
}
cardano {
tip {
epoch {
number
}
number
forgedAt
}
}}`
})
const { tip } = result.data?.cardano[0]
const currentUtc = dayjs().utc()
const tipUtc = dayjs.utc(tip.forgedAt)
const lastEpoch = result.data?.epochs[0]
return {
initialized: tipUtc.isAfter(currentUtc.subtract(120, 'second')),
syncPercentage: (tipUtc.valueOf() / currentUtc.valueOf()) * 100
// cardano-db-sync writes the epoch record at the end of each epoch during times of bulk sync
// The initialization state can be determined by comparing the last epoch record against the
// tip
initialized: lastEpoch.number === tip.epoch.number,
syncPercentage: (tip.number / nodeTipBlockNumber) * 100
}
}
}
3 changes: 2 additions & 1 deletion packages/api-cardano-db-hasura/src/executableSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ export async function buildSchema (
},
cardanoDbMeta: async () => {
try {
return hasuraClient.getMeta()
const tip = await cardanoNodeClient.getTip()
return hasuraClient.getMeta(tip.blockNo)
} catch (error) {
throw new ApolloError(error)
}
Expand Down

0 comments on commit b29ef3e

Please sign in to comment.