Skip to content

Commit

Permalink
feat(Health): delete anchored transactions from ipfsDirectoryHash col…
Browse files Browse the repository at this point in the history
…lection (#891)

remove a directoryHash from Health tracking once it has been anchored to the bitcoin blockchain

References: #832, #891
  • Loading branch information
krobi64 authored Dec 11, 2018
1 parent feba211 commit 0d55f27
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Health/HealthController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { pick, pipeP } from 'ramda'

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

import { BlockchainInfo, WalletInfo, NetworkInfo, IPFSInfo, EstimatedSmartFeeInfo, HealthDAO } from './HealthDAO'
import { TransactionAnchorRetryInfo, IPFSDirectoryHashDAO } from './IPFSDirectoryHashDAO'
Expand Down Expand Up @@ -77,6 +78,7 @@ export class HealthController {
this.logger = childWithFileName(logger, __filename)
this.configuration = configuration
this.healthDAO = healthDAO
this.ipfsDirectoryHashDAO = ipfsDirectoryHashDAO
this.bitcoinCore = bitcoinCore
this.ipfs = ipfs
}
Expand Down Expand Up @@ -157,6 +159,10 @@ export class HealthController {
return transactionAnchorRetryInfo
}

private async removeIPFSDirectoryHashByTransactionId(transactionIds: ReadonlyArray<string>): Promise<void> {
await this.ipfsDirectoryHashDAO.deleteByTransactionIds(transactionIds)
}

public async updateIPFSFailures(ipfsHashFailures: ReadonlyArray<IPFSHashFailure>) {
this.logger.debug({ ipfsHashFailures }, 'Updating IPFS Failure Count by failureType')
await ipfsHashFailures.map(
Expand All @@ -167,6 +173,12 @@ export class HealthController {
)
}

public purgeIpfsDirectoryHashByTransactionIds = pipeP(
this.log(LogTypes.trace)('purging IPFSDirectoryHash for transactionIds'),
this.removeIPFSDirectoryHashByTransactionId,
this.log(LogTypes.trace)('purged IPFSDirectoryHash for transactionIds'),
)

public refreshBlockchainInfo = pipeP(
this.log(LogTypes.trace)('refreshing blockchain info'),
this.getBlockchainInfo,
Expand Down
9 changes: 9 additions & 0 deletions src/Health/IPFSDirectoryHashDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface TransactionAnchorRetryEntry {

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

export interface Dependencies {
readonly ipfsDirectoryHashInfoCollection: Collection
Expand Down Expand Up @@ -56,6 +57,14 @@ export class IPFSDirectoryHashDAO {
)
}

readonly deleteByTransactionIds: deleteByTransactionIds = async transactionIds => {
await this.ipfsDirectoryHashInfoCollection.deleteMany(
{
txId: { $in: transactionIds },
},
)
}

readonly getTransactionAnchorRetryInfo: getTransactionAnchorRetryInfo = async () => {
const cursorArray = await this.ipfsDirectoryHashInfoCollection.aggregate([
{ $match: { attempts: { $ne: 1 } } },
Expand Down
14 changes: 14 additions & 0 deletions src/Health/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Pino from 'pino'

import { childWithFileName } from 'Helpers/Logging'
import { IPFSHashFailure } from 'Interfaces'
import { BlockDownloaded, IPFSHashTxId } from 'Messaging/Messages'
import { Messaging } from 'Messaging/Messaging'

import { ExchangeConfiguration } from './ExchangeConfiguration'
Expand Down Expand Up @@ -41,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.consumeBlockDownloaded(this.blockDownloadedConsumer)
}

onGetHealth = async () => {
Expand All @@ -67,4 +69,16 @@ export class Router {
logger.error({ error }, 'Failed to update ipfsHashFailures on health')
}
}

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)
} catch (error) {
logger.error({ error }, 'Failed to remove transactions')
}
}
}

0 comments on commit 0d55f27

Please sign in to comment.