Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/119 #2026

Merged
merged 8 commits into from
Nov 7, 2023
Merged

fix/119 #2026

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
if (this.storageService) {
this.logger('Stopping orbitdb')
await this.storageService?.stopOrbitDb()
this.logger('reset CsrReplicated map and id')
this.storageService.resetCsrReplicatedMapAndId()
}
// if (this.storageService.ipfs) {
// this.storageService.ipfs = null
Expand Down Expand Up @@ -398,6 +400,12 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
this.registrationService.on(RegistrationEvents.NEW_USER, async payload => {
await this.storageService?.saveCertificate(payload)
})

this.registrationService.on(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, payload => {
if (payload.id) {
this.storageService.resolveCsrReplicatedPromise(payload.id)
}
})
}
private attachsocketServiceListeners() {
// Community
Expand Down Expand Up @@ -561,7 +569,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
})
this.storageService.on(
StorageEvents.REPLICATED_CSR,
async (payload: { csrs: string[]; certificates: string[] }) => {
async (payload: { csrs: string[]; certificates: string[]; id: string }) => {
console.log(`On ${StorageEvents.REPLICATED_CSR}`)
this.serverIoProvider.io.emit(SocketActionTypes.RESPONSE_GET_CSRS, { csrs: payload.csrs })
this.registrationService.emit(RegistrationEvents.REGISTER_USER_CERTIFICATE, payload)
Expand Down
11 changes: 8 additions & 3 deletions packages/backend/src/nest/registration/registration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ export class RegistrationService extends EventEmitter implements OnModuleInit {
onModuleInit() {
this.on(
RegistrationEvents.REGISTER_USER_CERTIFICATE,
async (payload: { csrs: string[]; certificates: string[] }) => {
async (payload: { csrs: string[]; certificates: string[]; id: string }) => {
// Lack of permsData means that we are not the owner of the community in the official model of the app, however anyone can modify the source code, put malicious permsData here, issue false certificates and try to trick other users.
await this.issueCertificates(payload)
}
)
}

private async issueCertificates(payload: { csrs: string[]; certificates: string[] }) {
if (!this._permsData) return
private async issueCertificates(payload: { csrs: string[]; certificates: string[]; id?: string }) {
if (!this._permsData) {
if (payload.id) this.emit(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, { id: payload.id })
return
}
const pendingCsrs = await extractPendingCsrs(payload)
pendingCsrs.forEach(async csr => {
await this.registerUserCertificate(csr)
})

if (payload.id) this.emit(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, { id: payload.id })
}

public set permsData(perms: PermsData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum RegistrationEvents {
ERROR = 'error',
NEW_USER = 'newUser',
REGISTER_USER_CERTIFICATE = 'registerUserCertificate',
FINISHED_ISSUING_CERTIFICATES_FOR_ID = 'FINISHED_ISSUING_CERTIFICATES_FOR_ID',
}
60 changes: 60 additions & 0 deletions packages/backend/src/nest/storage/storage.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { LocalDbModule } from '../local-db/local-db.module'
import { LocalDbService } from '../local-db/local-db.service'
import { IPFS_REPO_PATCH, ORBIT_DB_DIR, QUIET_DIR } from '../const'
import { LocalDBKeys } from '../local-db/local-db.types'
import { RegistrationEvents } from '../registration/registration.types'

const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
Expand Down Expand Up @@ -674,4 +675,63 @@ describe('StorageService', () => {
await expect(storageService.deleteFilesFromChannel(messages)).resolves.not.toThrowError()
})
})

describe('replicate certificatesRequests event', () => {
const replicatedEvent = async () => {
// @ts-ignore - Property 'certificates' is private
storageService.certificatesRequests.events.emit('replicated')
await new Promise<void>(resolve => setTimeout(() => resolve(), 2000))
}

it('replicated event ', async () => {
await storageService.init(peerId)
const spyOnUpdatePeersList = jest.spyOn(storageService, 'updatePeersList')
await replicatedEvent()
expect(spyOnUpdatePeersList).toBeCalledTimes(1)
})

it('2 replicated events - first not resolved ', async () => {
await storageService.init(peerId)
const spyOnUpdatePeersList = jest.spyOn(storageService, 'updatePeersList')
await replicatedEvent()
await replicatedEvent()
expect(spyOnUpdatePeersList).toBeCalledTimes(1)
})

it('2 replicated events - first resolved ', async () => {
await storageService.init(peerId)
const spyOnUpdatePeersList = jest.spyOn(storageService, 'updatePeersList')
await replicatedEvent()
await replicatedEvent()
storageService.resolveCsrReplicatedPromise(1)
await new Promise<void>(resolve => setTimeout(() => resolve(), 500))
expect(spyOnUpdatePeersList).toBeCalledTimes(2)
})

it('3 replicated events - no resolved promises', async () => {
await storageService.init(peerId)
const spyOnUpdatePeersList = jest.spyOn(storageService, 'updatePeersList')

await replicatedEvent()
await replicatedEvent()
await replicatedEvent()

expect(spyOnUpdatePeersList).toBeCalledTimes(1)
})

it('3 replicated events - two resolved promises ', async () => {
await storageService.init(peerId)
const spyOnUpdatePeersList = jest.spyOn(storageService, 'updatePeersList')

await replicatedEvent()
await replicatedEvent()
storageService.resolveCsrReplicatedPromise(1)
await new Promise<void>(resolve => setTimeout(() => resolve(), 500))
await replicatedEvent()
storageService.resolveCsrReplicatedPromise(2)
await new Promise<void>(resolve => setTimeout(() => resolve(), 500))

expect(spyOnUpdatePeersList).toBeCalledTimes(3)
})
})
})
55 changes: 54 additions & 1 deletion packages/backend/src/nest/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ import Logger from '../common/logger'
import { DirectMessagesRepo, PublicChannelsRepo } from '../common/types'
import { removeFiles, removeDirs, createPaths, getUsersAddresses } from '../common/utils'
import { StorageEvents } from './storage.types'
import { RegistrationEvents } from '../registration/registration.types'

interface DBOptions {
replicate: boolean
}

interface CsrReplicatedPromiseValues {
promise: Promise<unknown>
resolveFunction: any
}

@Injectable()
export class StorageService extends EventEmitter {
public channels: KeyValueStore<PublicChannel>
Expand All @@ -71,6 +77,8 @@ export class StorageService extends EventEmitter {
private filesManager: IpfsFileManagerService
private peerId: PeerId | null = null
private ipfsStarted: boolean
public csrReplicatedPromiseMap: Map<number, CsrReplicatedPromiseValues> = new Map()
private csrReplicatedPromiseId: number = 0

private readonly logger = Logger(StorageService.name)
constructor(
Expand Down Expand Up @@ -148,6 +156,11 @@ export class StorageService extends EventEmitter {
})
}

public resetCsrReplicatedMapAndId() {
this.csrReplicatedPromiseMap = new Map()
this.csrReplicatedPromiseId = 0
}

private async startReplicate() {
const dbs = []

Expand Down Expand Up @@ -390,6 +403,26 @@ 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) {
const csrReplicatedPromiseMapId = this.csrReplicatedPromiseMap.get(id)
if (csrReplicatedPromiseMapId) {
csrReplicatedPromiseMapId?.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 @@ -398,13 +431,32 @@ export class StorageService extends EventEmitter {
write: ['*'],
},
})

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

this.csrReplicatedPromiseId++

const filteredCsrs = await this.getCsrs()

const allCertificates = this.getAllEventLogEntries(this.certificates)
this.emit(StorageEvents.REPLICATED_CSR, { csrs: filteredCsrs, certificates: allCertificates })

this.createCsrReplicatedPromise(this.csrReplicatedPromiseId)

if (this.csrReplicatedPromiseId > 1) {
const csrReplicatedPromiseMapId = this.csrReplicatedPromiseMap.get(this.csrReplicatedPromiseId - 1)

if (csrReplicatedPromiseMapId?.promise) {
await csrReplicatedPromiseMapId.promise
}
}

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

await this.updatePeersList()
})
this.certificatesRequests.events.on('write', async (_address, entry) => {
Expand Down Expand Up @@ -1002,5 +1054,6 @@ export class StorageService extends EventEmitter {
// @ts-ignore
this.filesManager = null
this.peerId = null
this.resetCsrReplicatedMapAndId()
}
}
6 changes: 1 addition & 5 deletions packages/e2e-tests/src/tests/backwardsCompatibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ describe('Backwards Compatibility', () => {
await registerModal.typeUsername(ownerUsername)
await registerModal.submit()
})
it('Connecting to peers modal', async () => {
const loadingPanelCommunity = new JoiningLoadingPanel(ownerAppOldVersion.driver)
const isLoadingPanelCommunity = await loadingPanelCommunity.element.isDisplayed()
expect(isLoadingPanelCommunity).toBeTruthy()
})

it('Close update modal', async () => {
console.log('waiting for update modal')
const updateModal = new UpdateModal(ownerAppOldVersion.driver)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { FC, useState, useRef, useEffect } from 'react'

import { Keyboard, KeyboardAvoidingView, TextInput, View, Image } from 'react-native'

import { defaultTheme } from '../../styles/themes/default.theme'
Expand Down
Loading