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: Replaces the time-based logic to determine sync progress and initialisation state #407

Merged
merged 1 commit into from
Jan 25, 2021
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
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