Skip to content

Commit

Permalink
refactor: Move community creation and data to backend (#2305)
Browse files Browse the repository at this point in the history
This commit migrates the Community model to the backend so that the backend is now the source of truth for Community data. In order to accomplish this, for existing communities (existing Quiet install), we move (migrate) the data from the frontend to the backend and then make sure all operations that previously affected the frontend Community state are done on the backend. For new communities (new Quiet install), some of the logic for creating/joining has been moved to the backend and the event-flow is simplified. Whenever the Community model is updated on the backend, the backend now emits a COMMUNITY_UPDATED event so that the frontend can synchronize its Community state.

Solution for: #2329
  • Loading branch information
Lucas Leblow authored Apr 4, 2024
1 parent 71c8b22 commit 29c1c1b
Show file tree
Hide file tree
Showing 59 changed files with 794 additions and 823 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,34 @@ describe('ConnectionsManagerService', () => {
'y7yczmugl2tekami7sbdz5pfaemvx7bahwthrdvcbzw5vex2crsr26qd',
'QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE'
)
const launchCommunityPayload: InitCommunityPayload = {

// Using the factory includes extra properties that affect the assertion
// below
const actualCommunity = {
id: community.id,
peerList: [remotePeer],
}
await localDbService.setCommunity(actualCommunity)
await localDbService.setCurrentCommunityId(community.id)
// TODO: Revisit this when we move the Identity model to the backend, since
// this network data lives in that model.
const network = {
peerId: userIdentity.peerId,
hiddenService: userIdentity.hiddenService,
certs: {
// @ts-expect-error
certificate: userIdentity.userCertificate,
// @ts-expect-error
key: userIdentity.userCsr?.userKey,
CA: [communityRootCa],
},
peers: [remotePeer],
}
await localDbService.setNetworkInfo(network)

await localDbService.put(LocalDBKeys.COMMUNITY, launchCommunityPayload)
await connectionsManagerService.closeAllServices()

const launchCommunitySpy = jest.spyOn(connectionsManagerService, 'launchCommunity').mockResolvedValue()

await connectionsManagerService.init()

const localPeerAddress = createLibp2pAddress(userIdentity.hiddenService.onionAddress, userIdentity.peerId.id)
const updatedLaunchCommunityPayload = { ...launchCommunityPayload, peers: [localPeerAddress, remotePeer] }
const updatedLaunchCommunityPayload = {
community: { ...actualCommunity, peerList: [localPeerAddress, remotePeer] },
network,
}

expect(launchCommunitySpy).toHaveBeenCalledWith(updatedLaunchCommunityPayload)
})
Expand All @@ -129,20 +134,13 @@ describe('ConnectionsManagerService', () => {
expect(launchCommunitySpy).not.toHaveBeenCalled()
})

// At this moment, that test have to be skipped, because checking statues is called before launchCommunity method
it.skip('community is only launched once', async () => {
const launchCommunityPayload: InitCommunityPayload = {
id: community.id,
peerId: userIdentity.peerId,
hiddenService: userIdentity.hiddenService,
certs: {
// @ts-expect-error
certificate: userIdentity.userCertificate,
// @ts-expect-error
key: userIdentity.userCsr?.userKey,
CA: [communityRootCa],
it('community is only launched once', async () => {
const launchCommunityPayload = {
community: community,
network: {
peerId: userIdentity.peerId,
hiddenService: userIdentity.hiddenService,
},
peers: community.peerList,
}

//@ts-ignore
Expand All @@ -157,22 +155,15 @@ describe('ConnectionsManagerService', () => {
})

it('Bug reproduction - Error on startup - Error: TOR: Connection already established - Trigger launchCommunity from backend and state manager', async () => {
const launchCommunityPayload: InitCommunityPayload = {
id: community.id,
await localDbService.setCommunity(community)
await localDbService.setCurrentCommunityId(community.id)
// TODO: Revisit this when we move the Identity model to the backend, since
// this network data lives in that model.
const network = {
peerId: userIdentity.peerId,
hiddenService: userIdentity.hiddenService,
certs: {
// @ts-expect-error
certificate: userIdentity.userCertificate,
// @ts-expect-error
key: userIdentity.userCsr?.userKey,
CA: [communityRootCa],
},
peers: community.peerList,
}

// await connectionsManager.init()
await localDbService.put(LocalDBKeys.COMMUNITY, launchCommunityPayload)
await localDbService.setNetworkInfo(network)

const peerid = 'QmaEvCkpUG7GxhgvMkk8wxurfi1ehjHhSUNRksWTmXN2ix'
await localDbService.put(LocalDBKeys.PEERS, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,6 @@ describe('Connections manager', () => {
}
const emitSpy = jest.spyOn(libp2pService, 'emit')

const launchCommunityPayload: InitCommunityPayload = {
id: community.id,
peerId: userIdentity.peerId,
hiddenService: userIdentity.hiddenService,
certs: {
// @ts-expect-error
certificate: userIdentity.userCertificate,
// @ts-expect-error
key: userIdentity.userCsr?.userKey,
CA: [communityRootCa],
},
peers: community.peerList,
}

await localDbService.put(LocalDBKeys.COMMUNITY, launchCommunityPayload)

// Peer connected
await connectionsManagerService.init()
libp2pService.connectedPeers.set(peerId.toString(), DateTime.utc().valueOf())
Expand Down Expand Up @@ -206,19 +190,15 @@ describe('Connections manager', () => {
)
}

const launchCommunityPayload: InitCommunityPayload = {
id: community.id,
peerId: userIdentity.peerId,
hiddenService: userIdentity.hiddenService,
certs: {
// @ts-expect-error Identity.userCertificate can be null
certificate: userIdentity.userCertificate,
// @ts-expect-error Identity.userCertificate userCsr.userKey can be undefined
key: userIdentity.userCsr?.userKey,
// @ts-expect-error
CA: [community.rootCa],
const launchCommunityPayload = {
community: {
id: community.id,
peerList,
},
network: {
peerId: userIdentity.peerId,
hiddenService: userIdentity.hiddenService,
},
peers: peerList,
}
await connectionsManagerService.init()
await connectionsManagerService.launchCommunity(launchCommunityPayload)
Expand Down Expand Up @@ -248,19 +228,15 @@ describe('Connections manager', () => {
)
}

const launchCommunityPayload: InitCommunityPayload = {
id: community.id,
peerId: userIdentity.peerId,
hiddenService: userIdentity.hiddenService,
certs: {
// @ts-expect-error Identity.userCertificate can be null
certificate: userIdentity.userCertificate,
// @ts-expect-error
key: userIdentity.userCsr?.userKey,
// @ts-expect-error
CA: [community.rootCa],
const launchCommunityPayload = {
community: {
id: community.id,
peerList,
},
network: {
peerId: userIdentity.peerId,
hiddenService: userIdentity.hiddenService,
},
peers: peerList,
}

await connectionsManagerService.launchCommunity(launchCommunityPayload)
Expand Down
Loading

0 comments on commit 29c1c1b

Please sign in to comment.