Skip to content

Commit

Permalink
feat(Health): upsert number of attempts to anchor an IPFS directory h…
Browse files Browse the repository at this point in the history
…ash (#896)

upsert a tracker with the number of attempts for each HashTxId event

References: #832 #896
  • Loading branch information
krobi64 authored Dec 12, 2018
1 parent d7b36ad commit 26afa46
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
23 changes: 14 additions & 9 deletions src/Health/HealthController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@ import * as Pino from 'pino'
import { pick, pipeP } from 'ramda'

import { childWithFileName } from 'Helpers/Logging'
import { IPFSHashFailure, ClaimIPFSHashPair } from 'Interfaces'
import { HealthError, IPFSHashFailure, TransactionAnchorRetryInfo } from 'Interfaces'
import { IPFSHashTxId } from 'Messaging/Messages'

import { BlockchainInfo, WalletInfo, NetworkInfo, IPFSInfo, EstimatedSmartFeeInfo, HealthDAO } from './HealthDAO'
import { TransactionAnchorRetryInfo, IPFSDirectoryHashDAO } from './IPFSDirectoryHashDAO'

import { BlockchainInfo, EstimatedSmartFeeInfo, HealthDAO, IPFSInfo, NetworkInfo, WalletInfo } from './HealthDAO'
import { IPFS } from './IPFS'
import { IPFSDirectoryHashDAO } from './IPFSDirectoryHashDAO'

enum LogTypes {
info = 'info',
trace = 'trace',
error = 'error',
}

export interface HealthError {
readonly error: string
}

export interface HealthControllerConfiguration {
readonly lowWalletBalanceInBitcoin: number
readonly feeEstimateMinTargetBlock: number
Expand Down Expand Up @@ -159,6 +153,11 @@ export class HealthController {
return transactionAnchorRetryInfo
}

private async upsertIpfsDirectoryHashTxId(ipfsHashTxId: IPFSHashTxId): Promise<IPFSHashTxId> {
await this.ipfsDirectoryHashDAO.updateAnchorAttemptsInfo(ipfsHashTxId)
return ipfsHashTxId
}

private async removeIPFSDirectoryHashByTransactionId(transactionIds: ReadonlyArray<string>): Promise<void> {
await this.ipfsDirectoryHashDAO.deleteByTransactionIds(transactionIds)
}
Expand All @@ -173,6 +172,12 @@ export class HealthController {
)
}

public upsertIpfsHashTxId = pipeP(
this.log(LogTypes.trace)('updating ipfsDirectoryHash info'),
this.upsertIpfsDirectoryHashTxId,
this.log(LogTypes.trace)('updated ipfsDirectoryHash info'),
)

public purgeIpfsDirectoryHashByTransactionIds = pipeP(
this.log(LogTypes.trace)('purging IPFSDirectoryHash for transactionIds'),
this.removeIPFSDirectoryHashByTransactionId,
Expand Down
7 changes: 5 additions & 2 deletions src/Health/HealthDAO.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Collection, Db } from 'mongodb'
import { TransactionAnchorRetryInfo } from './IPFSDirectoryHashDAO'

import { TransactionAnchorRetryInfo } from 'Interfaces'

export interface BlockchainInfo {
readonly blocks: number
Expand Down Expand Up @@ -124,7 +125,9 @@ export class HealthDAO {
)
}

readonly updateTransactionAnchorRetryInfo: updateTransactionAnchorRetryInfo = async transactionAnchorRetryInfo => {
readonly updateTransactionAnchorRetryInfo: updateTransactionAnchorRetryInfo = async (
transactionAnchorRetryInfo: TransactionAnchorRetryInfo,
) => {
await this.collection.updateOne(
{ name: 'transactionAnchorRetryInfo' },
{
Expand Down
10 changes: 2 additions & 8 deletions src/Health/IPFSDirectoryHashDAO.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { TransactionAnchorRetryInfo } from 'Interfaces'
import { IPFSHashTxId } from 'Messaging/Messages'
import { Collection } from 'mongodb'
import { isNil } from 'ramda'

import { IPFSHashTxId } from 'Messaging/Messages'

type updateAnchorAttemptsInfo = (x: IPFSHashTxId) => Promise<void>

export interface AnchorRetryDAOResult {
readonly _id: number
readonly count: number
}

export interface TransactionAnchorRetryEntry {
readonly attempts: number
readonly count: number
}

export type TransactionAnchorRetryInfo = ReadonlyArray<TransactionAnchorRetryEntry>
export type getTransactionAnchorRetryInfo = () => Promise<TransactionAnchorRetryInfo>
type deleteByTransactionIds = (transactionIds: ReadonlyArray<string>) => Promise<void>

Expand Down
14 changes: 14 additions & 0 deletions src/Health/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class Router {
async start() {
await this.messaging.consume(this.exchange.getHealth, this.onGetHealth)
await this.messaging.consumeClaimsNotDownloaded(this.onClaimsNotDownloaded)
await this.messaging.consumeIPFSHashTxId(this.ipfsHashTxIdConsumer)
await this.messaging.consumeBlockDownloaded(this.blockDownloadedConsumer)
}

Expand Down Expand Up @@ -70,13 +71,26 @@ export class Router {
}
}

ipfsHashTxIdConsumer = async (hashTxId: IPFSHashTxId): Promise<void> => {
const logger = this.logger.child({ method: 'ipfsHashTxIdConsumer' })

logger.debug({ hashTxId }, 'IPFS Directory Hash assigned a transactionId, updating counts')
try {
await this.controller.upsertIpfsHashTxId(hashTxId)
logger.info({ hashTxId }, 'Updated count for IPFS Directory Hash')
} catch (error) {
logger.error({ error }, 'Failed to upsert IPFSDirectoryHash')
}
}

blockDownloadedConsumer = async (blockDownloaded: BlockDownloaded): Promise<void> => {
const logger = this.logger.child({ method: 'blockDownloadedConsumer' })

logger.debug({ blockDownloaded }, 'Block downloaded, removing associated transactions')
try {
const transactionIds = blockDownloaded.poetBlockAnchors.map(_ => _.transactionId)
await this.controller.purgeIpfsDirectoryHashByTransactionIds(transactionIds)
logger.info({ blockDownloaded }, 'Block downloaded, associated transactions removed')
} catch (error) {
logger.error({ error }, 'Failed to remove transactions')
}
Expand Down
11 changes: 11 additions & 0 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ const hasFailureType = has('failureType')
const hasFailureReason = has('failureReason')
const hasFailureTime = has('failureTime')
export const isIPFSHashFailure = allPass([hasIPFSFileHash, hasFailureReason, hasFailureType, hasFailureTime])

export interface HealthError {
readonly error: string
}

export interface TransactionAnchorRetryEntry {
readonly attempts: number
readonly count: number
}

export type TransactionAnchorRetryInfo = ReadonlyArray<TransactionAnchorRetryEntry>

0 comments on commit 26afa46

Please sign in to comment.