Skip to content

Commit

Permalink
fix: move current era check to be lazy operation
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyslbw committed Jun 24, 2021
1 parent 7054a59 commit 7830c91
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*.npmrc
*.nyc_output
*coverage
*d.ts
*.d.ts
!packages/api-cardano-db-hasura/src/blake2b.d.ts
*dist/
lerna-debug.log
Expand Down
18 changes: 8 additions & 10 deletions packages/api-cardano-db-hasura/src/CardanoNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export class CardanoNodeClient {
if (this.state !== 'initialized') {
throw new errors.ModuleIsNotInitialized(MODULE_NAME, 'getProtocolParams')
}
if (!(await this.isInCurrentEra())) {
throw new errors.OperationRequiresNodeInCurrentEra('getProtocolParams')
}
const protocolParams = await this.stateQueryClient.currentProtocolParameters()
this.logger.debug({ module: MODULE_NAME, protocolParams }, 'getProtocolParams')
return protocolParams
Expand All @@ -55,20 +58,15 @@ export class CardanoNodeClient {
if (this.state !== null) return
this.state = 'initializing'
this.logger.info({ module: MODULE_NAME }, 'Initializing')
this.isInitialized = true
await pRetry(async () => {
const options = ogmiosConnectionConfig ? { connection: ogmiosConnectionConfig } : {}
this.stateQueryClient = await createStateQueryClient(options)
this.txSubmissionClient = await createTxSubmissionClient(options)
if (!(await this.isInCurrentEra())) {
this.logger.warn({ module: MODULE_NAME }, 'cardano-node is still synchronizing')
throw new Error()
}
this.stateQueryClient = await createStateQueryClient(this.logger.error, options)
this.txSubmissionClient = await createTxSubmissionClient(this.logger.error, options)
}, {
factor: 1.5,
retries: 39,
factor: 1.2,
retries: 100,
onFailedAttempt: util.onFailedAttemptFor(
'Establishing connection to cardano-node and ensuring state is in the expected era',
'Establishing connection to cardano-node',
this.logger
)
})
Expand Down
1 change: 0 additions & 1 deletion packages/api-cardano-db-hasura/src/ChainFollower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
isMaryBlock,
Schema
} from '@cardano-ogmios/client'
import { errors } from '@cardano-graphql/util'
import { Config } from './Config'
import { errors, RunnableModuleState } from '@cardano-graphql/util'
import { Asset } from './graphql_types'
Expand Down
1 change: 0 additions & 1 deletion packages/api-cardano-db-hasura/src/HasuraClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import util, { DataFetcher } from '@cardano-graphql/util'
import { Schema } from '@cardano-ogmios/client'
import { exec } from 'child_process'
import util, { DataFetcher, ModuleState } from '@cardano-graphql/util'
Expand Down
8 changes: 8 additions & 0 deletions packages/util/src/errors/OperationRequiresNodeInCurrentEra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { CustomError } from 'ts-custom-error'

export class OperationRequiresNodeInCurrentEra extends CustomError {
public constructor (operation: string) {
super()
this.message = `${operation} cannot be called until cardano-node's block sync is in the current era`
}
}
1 change: 1 addition & 0 deletions packages/util/src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './HostDoesNotExist'
export * from './MissingConfig'
export * from './ModuleIsNotInitialized'
export * from './OperationRequiresNodeInCurrentEra'

0 comments on commit 7830c91

Please sign in to comment.