Skip to content

Commit

Permalink
feat: create dao for tracking ipfs directory hashes (#834)
Browse files Browse the repository at this point in the history
References #832
  • Loading branch information
krobi64 authored Nov 30, 2018
1 parent 8d687e7 commit a2b14a7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Health/Health.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -89,6 +93,10 @@ export class Health {
this.container.bind<HealthServiceConfiguration>('HealthServiceConfiguration').toConstantValue({
healthIntervalInSeconds: this.configuration.healthIntervalInSeconds,
})
this.container
.bind<Collection>('IPFSDirectoryHashInfoCollection')
.toConstantValue(this.ipfsDirectoryHashInfoCollection)

this.container.bind<ExchangeConfiguration>('ExchangeConfiguration').toConstantValue(this.configuration.exchanges)
this.container.bind<Messaging>('Messaging').toConstantValue(this.messaging)
this.container.bind<Router>('Router').to(Router)
Expand All @@ -101,5 +109,6 @@ export class Health {
feeEstimateMinTargetBlock: this.configuration.feeEstimateMinTargetBlock,
})
this.container.bind<HealthDAO>('HealthDAO').to(HealthDAO)
this.container.bind<IPFSDirectoryHashDAO>('IPFSDirectoryHashDAO').to(IPFSDirectoryHashDAO )
}
}
35 changes: 35 additions & 0 deletions src/Health/IPFSDirectoryHashDAO.ts
Original file line number Diff line number Diff line change
@@ -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<void>

@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 },
)
}
}

0 comments on commit a2b14a7

Please sign in to comment.