Skip to content

Commit a2b14a7

Browse files
authored
feat: create dao for tracking ipfs directory hashes (#834)
References #832
1 parent 8d687e7 commit a2b14a7

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/Health/Health.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Messaging } from 'Messaging/Messaging'
22
import BitcoinCore = require('bitcoin-core')
33
import { Container } from 'inversify'
4-
import { Db, MongoClient } from 'mongodb'
4+
import { Collection, Db, MongoClient } from 'mongodb'
55
import * as Pino from 'pino'
66
import { pick } from 'ramda'
77

@@ -13,6 +13,7 @@ import { HealthController, HealthControllerConfiguration } from './HealthControl
1313
import { HealthDAO } from './HealthDAO'
1414
import { HealthService, HealthServiceConfiguration } from './HealthService'
1515
import { IPFS, IPFSConfiguration } from './IPFS'
16+
import { IPFSDirectoryHashDAO } from './IPFSDirectoryHashDAO'
1617
import { Router } from './Router'
1718

1819
export interface HealthConfiguration
@@ -35,6 +36,7 @@ export class Health {
3536
private cron: HealthService
3637
private messaging: Messaging
3738
private router: Router
39+
private ipfsDirectoryHashInfoCollection: Collection
3840

3941
constructor(configuration: HealthConfiguration) {
4042
this.configuration = configuration
@@ -46,6 +48,8 @@ export class Health {
4648
this.mongoClient = await MongoClient.connect(this.configuration.dbUrl)
4749
this.dbConnection = await this.mongoClient.db()
4850

51+
this.ipfsDirectoryHashInfoCollection = this.dbConnection.collection('ipfsDirectoryHashInfo')
52+
4953
const exchangesMessaging = pick(
5054
['getHealth', 'claimsNotDownloaded'],
5155
this.configuration.exchanges,
@@ -89,6 +93,10 @@ export class Health {
8993
this.container.bind<HealthServiceConfiguration>('HealthServiceConfiguration').toConstantValue({
9094
healthIntervalInSeconds: this.configuration.healthIntervalInSeconds,
9195
})
96+
this.container
97+
.bind<Collection>('IPFSDirectoryHashInfoCollection')
98+
.toConstantValue(this.ipfsDirectoryHashInfoCollection)
99+
92100
this.container.bind<ExchangeConfiguration>('ExchangeConfiguration').toConstantValue(this.configuration.exchanges)
93101
this.container.bind<Messaging>('Messaging').toConstantValue(this.messaging)
94102
this.container.bind<Router>('Router').to(Router)
@@ -101,5 +109,6 @@ export class Health {
101109
feeEstimateMinTargetBlock: this.configuration.feeEstimateMinTargetBlock,
102110
})
103111
this.container.bind<HealthDAO>('HealthDAO').to(HealthDAO)
112+
this.container.bind<IPFSDirectoryHashDAO>('IPFSDirectoryHashDAO').to(IPFSDirectoryHashDAO )
104113
}
105114
}

src/Health/IPFSDirectoryHashDAO.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { inject, injectable } from 'inversify'
2+
import { Collection } from 'mongodb'
3+
4+
export interface UpdateAnchorAttemptInfo {
5+
readonly ipfsDirectoryHash: string
6+
readonly txId: string
7+
}
8+
9+
type updateAnchorAttemptsInfo = (x: UpdateAnchorAttemptInfo) => Promise<void>
10+
11+
@injectable()
12+
export class IPFSDirectoryHashDAO {
13+
private readonly ipfsDirectoryHashInfoCollection: Collection
14+
15+
constructor(
16+
@inject('IPFSDirectoryHashInfoCollection') ipfsDirectoryHashInfoCollection: Collection,
17+
) {
18+
this.ipfsDirectoryHashInfoCollection = ipfsDirectoryHashInfoCollection
19+
}
20+
21+
readonly updateAnchorAttemptsInfo: updateAnchorAttemptsInfo = async anchorAttemptInfo => {
22+
await this.ipfsDirectoryHashInfoCollection.updateOne(
23+
{
24+
ipfsDirectoryHash: anchorAttemptInfo.ipfsDirectoryHash,
25+
txId: { $ne: anchorAttemptInfo.txId },
26+
},
27+
{
28+
$set: { txId: anchorAttemptInfo.txId },
29+
$inc: { attempts: 1 },
30+
$setOnInsert: { attempts: 1 },
31+
},
32+
{ upsert: true },
33+
)
34+
}
35+
}

0 commit comments

Comments
 (0)