Skip to content

Commit

Permalink
Fix connections manager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
islathehut committed Dec 30, 2024
1 parent 3901695 commit b262707
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe('Connections manager', () => {

// add 7 random peers to the list
for (let pCount = 0; pCount < MANY_PEERS_COUNT; pCount++) {
peerAddress = createLibp2pAddress(generateRandomOnionAddress(56), (await createPeerId()).toString())
peerAddress = createLibp2pAddress(generateRandomOnionAddress(56), (await createPeerId()).peerId.toString())
logger.info(`pushing peer ${pCount}: ${peerAddress}`)
peerList.push(peerAddress)
}
Expand All @@ -203,13 +203,15 @@ describe('Connections manager', () => {
localDbService.setCurrentCommunityId(community.id)
logger.info('Launching community', community.id, 'with peer list', peerList)
await connectionsManagerService.init()
await sleep(5000)

expect(connectionsManagerService.communityState).toBe(ServiceState.LAUNCHED)
await waitForExpect(async () => {
expect(connectionsManagerService.communityState).toBe(ServiceState.LAUNCHED)
}, 20_000)

await waitForExpect(async () => {
expect(connectionsManagerService.libp2pService.dialedPeers.size).toBe(MANY_PEERS_COUNT)
}, 15_000)

await waitForExpect(async () => {
// expect to dial all peers except self
expect(spyOnDial).toHaveBeenCalledTimes(MANY_PEERS_DIALS)
Expand All @@ -226,7 +228,7 @@ describe('Connections manager', () => {
peerList.push(createLibp2pAddress(userIdentity.hiddenService.onionAddress, userIdentity.peerId.id))
// add 7 random peers to the list
for (let pCount = 0; pCount < MANY_PEERS_COUNT; pCount++) {
peerAddress = createLibp2pAddress(generateRandomOnionAddress(56), (await createPeerId()).toString())
peerAddress = createLibp2pAddress(generateRandomOnionAddress(56), (await createPeerId()).peerId.toString())
logger.info(`pushing peer ${pCount}: ${peerAddress}`)
peerList.push(peerAddress)
}
Expand All @@ -240,11 +242,15 @@ describe('Connections manager', () => {
expect(connectionsManagerService.communityState).toBe(undefined)
// community will launch from storage
await connectionsManagerService.init()
expect(connectionsManagerService.communityState).toBe(ServiceState.LAUNCHED)

await waitForExpect(async () => {
expect(connectionsManagerService.communityState).toBe(ServiceState.LAUNCHED)
}, 20_000)

await waitForExpect(async () => {
expect(connectionsManagerService.libp2pService.dialedPeers.size).toBe(MANY_PEERS_COUNT)
}, 15_000)

await waitForExpect(async () => {
// expect to dial all peers except self
expect(spyOnDial).toHaveBeenCalledTimes(MANY_PEERS_DIALS)
Expand All @@ -261,12 +267,12 @@ describe('Connections manager', () => {
peerList.push(createLibp2pAddress(userIdentity.hiddenService.onionAddress, userIdentity.peerId.id))
// add 7 random peers to the list
for (let pCount = 0; pCount < MANY_PEERS_COUNT; pCount++) {
peerAddress = createLibp2pAddress(generateRandomOnionAddress(56), (await createPeerId()).toString())
peerAddress = createLibp2pAddress(generateRandomOnionAddress(56), (await createPeerId()).peerId.toString())
logger.info(`pushing peer ${pCount}: ${peerAddress}`)
peerList.push(peerAddress)
}
// add invalid peer address (too short)
peerList.push(createLibp2pAddress(generateRandomOnionAddress(50), (await createPeerId()).toString()))
peerList.push(createLibp2pAddress(generateRandomOnionAddress(50), (await createPeerId()).peerId.toString()))
// all addresses are valid
expect(peerList.length).toBe(filterValidAddresses(peerList).length + 1)

Expand All @@ -278,11 +284,14 @@ describe('Connections manager', () => {
// community will launch from storage
await connectionsManagerService.init()

expect(connectionsManagerService.communityState).toBe(ServiceState.LAUNCHED)
await waitForExpect(async () => {
expect(connectionsManagerService.communityState).toBe(ServiceState.LAUNCHED)
}, 20_000)

await waitForExpect(async () => {
expect(connectionsManagerService.libp2pService.dialedPeers.size).toBe(MANY_PEERS_COUNT)
}, 15_000)

await waitForExpect(async () => {
// expect to dial all peers except self
expect(spyOnDial).toHaveBeenCalledTimes(MANY_PEERS_DIALS)
Expand All @@ -303,12 +312,12 @@ describe('Connections manager', () => {
peerList.push(createLibp2pAddress(userIdentity.hiddenService.onionAddress, userIdentity.peerId.id))
// add 7 random peers to the list
for (let pCount = 0; pCount < MANY_PEERS_COUNT; pCount++) {
peerAddress = createLibp2pAddress(generateRandomOnionAddress(56), (await createPeerId()).toString())
peerAddress = createLibp2pAddress(generateRandomOnionAddress(56), (await createPeerId()).peerId.toString())
logger.info(`pushing peer ${pCount}: ${peerAddress}`)
peerList.push(peerAddress)
}
// add invalid peer address (too short)
peerList.push(createLibp2pAddress(generateRandomOnionAddress(50), (await createPeerId()).toString()))
peerList.push(createLibp2pAddress(generateRandomOnionAddress(50), (await createPeerId()).peerId.toString()))
// all addresses are valid
expect(peerList.length).toBe(filterValidAddresses(peerList).length + 1)
// update level db store of identity to sim saga registration
Expand All @@ -319,9 +328,14 @@ describe('Connections manager', () => {
localDbService.setCurrentCommunityId(community.id)
await connectionsManagerService.init()

await waitForExpect(async () => {
expect(connectionsManagerService.communityState).toBe(ServiceState.LAUNCHED)
}, 20_000)

await waitForExpect(async () => {
expect(connectionsManagerService.libp2pService.dialedPeers.size).toBe(MANY_PEERS_COUNT)
}, 15_000)

await waitForExpect(async () => {
// expect to dial all peers except self
expect(spyOnDial).toHaveBeenCalledTimes(MANY_PEERS_DIALS)
Expand All @@ -335,7 +349,9 @@ describe('Connections manager', () => {
const peerList: string[] = []
const peersCount = 8
for (let pCount = 0; pCount < peersCount; pCount++) {
peerList.push(createLibp2pAddress(userIdentity.hiddenService.onionAddress, (await createPeerId()).toString()))
peerList.push(
createLibp2pAddress(userIdentity.hiddenService.onionAddress, (await createPeerId()).peerId.toString())
)
}

await connectionsManagerService.launchCommunity({ ...community, peerList: peerList })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
const peerId = await createPeerId()
const peerIdJson: QuietPeerId = {
id: peerId.peerId.toString(),
pubKey: peerId.peerId.publicKey?.toString(),
privKey: uint8ArrayToString(peerId.privKey!.raw, 'base64'),
}
this.logger.info(`Created network for peer ${peerId.toString()}. Address: ${hiddenService.onionAddress}`)
Expand Down
1 change: 0 additions & 1 deletion packages/state-manager/src/utils/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const notAfterDate = new Date(Date.UTC(2030, 11, 28, 10, 10, 10))
export const createPeerIdTestHelper = (): PeerId => {
return {
id: '12D3KooWRga8g1J1oiH7UYnSQ8YMPRDfVuehuiuAd7PMkcXSxRsp',
pubKey: '12D3KooWRga8g1J1oiH7UYnSQ8YMPRDfVuehuiuAd7PMkcXSxRsp',
privKey: 'jAXL3ZK13AWR9WcwbX8nM/qgQqdaApPDqWj6dK9IPwHru99WpGniLouugCv2+t7QN4xnYLMoAFPRP40xTUTrCw',
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/types/src/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface HiddenService {

export interface PeerId {
id: string
pubKey?: string
privKey?: string
}

Expand Down

0 comments on commit b262707

Please sign in to comment.