Skip to content

Commit

Permalink
feat: working solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacper-RF committed Nov 3, 2023
1 parent 920ce24 commit 0f3f7dc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
})

this.registrationService.on(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, payload => {
this.storageService.emit(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, { id: payload.id })
this.storageService.resolveCsrReplicatedPromise(payload.id)
})
}
private attachsocketServiceListeners() {
Expand Down
53 changes: 35 additions & 18 deletions packages/backend/src/nest/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export class StorageService extends EventEmitter {
private filesManager: IpfsFileManagerService
private peerId: PeerId | null = null
private ipfsStarted: boolean
private replicatedPromises: Promise<string | void>[] = []
public csrReplicatedPromiseMap: Map<number, any> = new Map()
private csrReplicatedPromiseId: number = 0

private readonly logger = Logger(StorageService.name)
constructor(
Expand Down Expand Up @@ -392,6 +393,27 @@ export class StorageService extends EventEmitter {
this.logger('STORAGE: Finished createDbForCertificates')
}

private createCsrReplicatedPromise(id: number) {
let resolveFunction
const promise = new Promise(resolve => {
resolveFunction = resolve
})

this.csrReplicatedPromiseMap.set(id, { promise, resolveFunction })
}

public resolveCsrReplicatedPromise(id: number) {
console.log('rsolve promsie id', id)
if (this.csrReplicatedPromiseMap.has(id)) {
const { resolveFunction } = this.csrReplicatedPromiseMap.get(id)
resolveFunction(id)
this.csrReplicatedPromiseMap.delete(id)
} else {
console.log(`No promise with ID ${id} found.`)
return
}
}

public async createDbForCertificatesRequests() {
this.logger('certificatesRequests db init')
this.certificatesRequests = await this.orbitDb.log<string>('csrs', {
Expand All @@ -403,29 +425,24 @@ export class StorageService extends EventEmitter {

this.certificatesRequests.events.on('replicated', async () => {
this.logger('REPLICATED: CSRs')

const id = Math.random().toString(36).slice(2, 7)

const promiseForId = new Promise<string>(resolve => {
const listener = async (payload: { id: string }) => {
if (id === payload.id) {
this.removeListener(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, listener)
resolve(id)
}
}

this.on(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, listener)
})

this.replicatedPromises.push(promiseForId)
this.csrReplicatedPromiseId++

const filteredCsrs = await this.getCsrs()

const allCertificates = this.getAllEventLogEntries(this.certificates)

await Promise.all(this.replicatedPromises)
this.createCsrReplicatedPromise(this.csrReplicatedPromiseId)

if (this.csrReplicatedPromiseId > 1) {
const { promise } = this.csrReplicatedPromiseMap.get(this.csrReplicatedPromiseId - 1)
await promise
}

this.emit(StorageEvents.REPLICATED_CSR, { csrs: filteredCsrs, certificates: allCertificates, id })
this.emit(StorageEvents.REPLICATED_CSR, {
csrs: filteredCsrs,
certificates: allCertificates,
id: this.csrReplicatedPromiseId,
})

await this.updatePeersList()
})
Expand Down

0 comments on commit 0f3f7dc

Please sign in to comment.