Skip to content

Commit

Permalink
feat(API): retrieve transactionAnchor info in health report (#901)
Browse files Browse the repository at this point in the history
Resolves: #832, #707
References: #901
  • Loading branch information
krobi64 authored Dec 12, 2018
1 parent 26afa46 commit 893d08a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/API/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class API {
const healthController = new HealthController({
dependencies: {
db: this.dbConnection,
logger: this.logger,
},
})

Expand Down
44 changes: 43 additions & 1 deletion src/API/HealthController.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
import { Db, Collection } from 'mongodb'
import * as Pino from 'pino'

import { TransactionAnchorRetryInfo } from 'Interfaces'

export const isOkOne = ({ ok }: { ok: number }) => ok === 1

interface HealthObject {
export interface HealthObject {
readonly mongoIsConnected: boolean
readonly ipfsInfo: object
readonly walletInfo: object
readonly blockchainInfo: object
readonly networkInfo: object
readonly estimatedSmartFeeInfo: object
readonly ipfsRetryInfo: object
readonly transactionAnchorRetryInfo: TransactionAnchorRetryInfo
}

export interface Dependencies {
readonly db: Db
readonly logger: Pino.Logger
}

export interface Arguments {
readonly dependencies: Dependencies
}

interface EmptyTransactionAnchorRetryInfo {
transactionAnchorRetryInfo: TransactionAnchorRetryInfo
}

const emptyTransactionAnchorRetryInfo: EmptyTransactionAnchorRetryInfo = {
transactionAnchorRetryInfo: [],
}

export class HealthController {
private readonly db: Db
private readonly collection: Collection
private readonly logger: Pino.Logger

constructor({
dependencies: {
db,
logger,
},
}: Arguments) {
this.db = db
this.collection = this.db.collection('health')
this.logger = logger
}

private async checkMongo(): Promise<boolean> {
Expand Down Expand Up @@ -100,6 +116,30 @@ export class HealthController {
}
}

private async getTransactionRetryInfo(): Promise<TransactionAnchorRetryInfo> {
this.logger.child({ message: 'getTransactionRetryInfo' })
this.logger.trace('retrieving TransactionAnchorRetryInfo')
try {
const transactionAnchorRetryResults = await this.collection.findOne(
{
name: 'transactionAnchorRetryInfo',
},
{
fields:
{
_id: false,
name: false,
},
},
) || emptyTransactionAnchorRetryInfo
this.logger.trace({ transactionAnchorRetryResults }, 'getTransactionRetryInfo results')
return transactionAnchorRetryResults.transactionAnchorRetryInfo
} catch (error) {
this.logger.error({ error }, 'error retrieving TransactionAnchorRetryInfo')
return []
}
}

async getHealth(): Promise<HealthObject> {
const mongoIsConnected = await this.checkMongo()
const ipfsInfo = await this.getIPFSInfo()
Expand All @@ -108,6 +148,7 @@ export class HealthController {
const networkInfo = await this.getNetworkInfo()
const estimatedSmartFeeInfo = await this.getEstimatedSmartFeeInfo()
const ipfsRetryInfo = await this.getIPFSRetryInfo()
const transactionAnchorRetryInfo = await this.getTransactionRetryInfo()
return {
mongoIsConnected,
ipfsInfo,
Expand All @@ -116,6 +157,7 @@ export class HealthController {
networkInfo,
estimatedSmartFeeInfo,
ipfsRetryInfo,
transactionAnchorRetryInfo,
}
}
}
1 change: 1 addition & 0 deletions src/Health/HealthController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { pick, pipeP } from 'ramda'
import { childWithFileName } from 'Helpers/Logging'
import { HealthError, IPFSHashFailure, TransactionAnchorRetryInfo } from 'Interfaces'
import { IPFSHashTxId } from 'Messaging/Messages'

import { BlockchainInfo, EstimatedSmartFeeInfo, HealthDAO, IPFSInfo, NetworkInfo, WalletInfo } from './HealthDAO'
import { IPFS } from './IPFS'
import { IPFSDirectoryHashDAO } from './IPFSDirectoryHashDAO'
Expand Down
11 changes: 11 additions & 0 deletions tests/functional/transaction_timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { configureCreateVerifiableClaim, getVerifiableClaimSigner, isSignedVerifiableClaim } from '@po.et/poet-js'
import { allPass, is, isNil, lensPath, not, path, pipe, pipeP, view } from 'ramda'
import { describe } from 'riteway'
import { HealthObject } from '../../src/API/HealthController'

import { issuer, privateKey } from '../helpers/Keys'
import {
Expand All @@ -11,6 +12,7 @@ import {
waitForBlockchainSync,
waitForBlockchainsToSync,
} from '../helpers/bitcoin'
import { getHealth } from '../helpers/endpoints'
import { delayInSeconds, runtimeId, setUpServerAndDb } from '../helpers/utils'
import { getWork, postWork } from '../helpers/works'

Expand Down Expand Up @@ -119,6 +121,8 @@ describe('Transaction timout will reset the transaction id for the claim', async
const secondResponse = await getWorkFromNode(claim.id)
const secondGet = await secondResponse.json()
const secondTxId = getTransactionId(secondGet)
const healthResponse = await getHealth(NODE_PORT)
const { transactionAnchorRetryInfo }: HealthObject = await healthResponse.json()

assert({
given: 'transaction age max reached',
Expand All @@ -134,6 +138,13 @@ describe('Transaction timout will reset the transaction id for the claim', async
expected: true,
})

assert({
given: 'a reset transaction id',
should: 'show in the health report',
actual: transactionAnchorRetryInfo.length > 0,
expected: true,
})

await bitcoinCoreClientA.generate(1)
await delayInSeconds(
blockchainSettings.BATCH_CREATION_INTERVAL_IN_SECONDS +
Expand Down

0 comments on commit 893d08a

Please sign in to comment.