diff --git a/src/Health/Health.ts b/src/Health/Health.ts index 7a4143de..335a1a69 100644 --- a/src/Health/Health.ts +++ b/src/Health/Health.ts @@ -1,7 +1,7 @@ import { Messaging } from 'Messaging/Messaging' import BitcoinCore = require('bitcoin-core') import { Container } from 'inversify' -import { Db, MongoClient } from 'mongodb' +import { Collection, Db, MongoClient } from 'mongodb' import * as Pino from 'pino' import { pick } from 'ramda' @@ -13,6 +13,7 @@ import { HealthController, HealthControllerConfiguration } from './HealthControl import { HealthDAO } from './HealthDAO' import { HealthService, HealthServiceConfiguration } from './HealthService' import { IPFS, IPFSConfiguration } from './IPFS' +import { IPFSDirectoryHashDAO } from './IPFSDirectoryHashDAO' import { Router } from './Router' export interface HealthConfiguration @@ -35,6 +36,7 @@ export class Health { private cron: HealthService private messaging: Messaging private router: Router + private ipfsDirectoryHashInfoCollection: Collection constructor(configuration: HealthConfiguration) { this.configuration = configuration @@ -46,6 +48,8 @@ export class Health { this.mongoClient = await MongoClient.connect(this.configuration.dbUrl) this.dbConnection = await this.mongoClient.db() + this.ipfsDirectoryHashInfoCollection = this.dbConnection.collection('ipfsDirectoryHashInfo') + const exchangesMessaging = pick( ['getHealth', 'claimsNotDownloaded'], this.configuration.exchanges, @@ -89,6 +93,10 @@ export class Health { this.container.bind('HealthServiceConfiguration').toConstantValue({ healthIntervalInSeconds: this.configuration.healthIntervalInSeconds, }) + this.container + .bind('IPFSDirectoryHashInfoCollection') + .toConstantValue(this.ipfsDirectoryHashInfoCollection) + this.container.bind('ExchangeConfiguration').toConstantValue(this.configuration.exchanges) this.container.bind('Messaging').toConstantValue(this.messaging) this.container.bind('Router').to(Router) @@ -101,5 +109,6 @@ export class Health { feeEstimateMinTargetBlock: this.configuration.feeEstimateMinTargetBlock, }) this.container.bind('HealthDAO').to(HealthDAO) + this.container.bind('IPFSDirectoryHashDAO').to(IPFSDirectoryHashDAO ) } } diff --git a/src/Health/IPFSDirectoryHashDAO.ts b/src/Health/IPFSDirectoryHashDAO.ts new file mode 100644 index 00000000..8c2c8b1a --- /dev/null +++ b/src/Health/IPFSDirectoryHashDAO.ts @@ -0,0 +1,35 @@ +import { inject, injectable } from 'inversify' +import { Collection } from 'mongodb' + +export interface UpdateAnchorAttemptInfo { + readonly ipfsDirectoryHash: string + readonly txId: string +} + +type updateAnchorAttemptsInfo = (x: UpdateAnchorAttemptInfo) => Promise + +@injectable() +export class IPFSDirectoryHashDAO { + private readonly ipfsDirectoryHashInfoCollection: Collection + + constructor( + @inject('IPFSDirectoryHashInfoCollection') ipfsDirectoryHashInfoCollection: Collection, + ) { + this.ipfsDirectoryHashInfoCollection = ipfsDirectoryHashInfoCollection + } + + readonly updateAnchorAttemptsInfo: updateAnchorAttemptsInfo = async anchorAttemptInfo => { + await this.ipfsDirectoryHashInfoCollection.updateOne( + { + ipfsDirectoryHash: anchorAttemptInfo.ipfsDirectoryHash, + txId: { $ne: anchorAttemptInfo.txId }, + }, + { + $set: { txId: anchorAttemptInfo.txId }, + $inc: { attempts: 1 }, + $setOnInsert: { attempts: 1 }, + }, + { upsert: true }, + ) + } +}