Skip to content

Commit

Permalink
chore: rename sortPeers
Browse files Browse the repository at this point in the history
  • Loading branch information
EmiM committed Nov 20, 2023
1 parent 2a5f801 commit 97a5652
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/backend/src/nest/local-db/local-db.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inject, Injectable } from '@nestjs/common'
import { Level } from 'level'
import { NetworkStats } from '@quiet/types'
import { sortPeers } from '@quiet/common'
import { filterAndSortPeers } from '@quiet/common'
import { LEVEL_DB } from '../const'
import { LocalDBKeys, LocalDbStatus } from './local-db.types'
import Logger from '../common/logger'
Expand Down Expand Up @@ -75,6 +75,6 @@ export class LocalDbService {
console.log('getSortedPeers peers got', peers)
const peersStats = (await this.get(LocalDBKeys.PEERS)) || {}
const stats: NetworkStats[] = Object.values(peersStats)
return sortPeers(peers, stats)
return filterAndSortPeers(peers, stats)
}
}
20 changes: 20 additions & 0 deletions packages/common/src/libp2p.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { filterAndSortPeers } from './sortPeers'

describe('filterValidAddresses', () => {
it('filters out invalid addresses', () => {
const valid = [
'/dns4/gloao6h5plwjy4tdlze24zzgcxll6upq2ex2fmu2ohhyu4gtys4nrjad.onion/tcp/443/ws/p2p/QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE',
'/dns4/gloao6h5plwjy4tdlze24zzgcxll6upq2ex2fmu2ohhyu4gtys4nrjad.onion/tcp/80/ws/p2p/QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE',
]
const addresses = [
'/dns4/gloao6h5plwjy4tdlze24zzgcxll6upq2ex2fmu2ohhyu4gtys4nrjad.onion/tcp/443/wss/p2p/QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE',
...valid,
'invalidAddress',
'/dns4/somethingElse.onion/tcp/443/wss/p2p/QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSA',
'/dns4/gloao6h5plwjy4tdlze24zzgcxll6upq2ex2fmu2ohhyu4gtys4nrjad.onion/tcp/443/ws/p2p/QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbK',
'/dns4/gloao6h5plwjy4tdlze24zzgcxll6upq2ex2fmu2ohhyu4gtys4nrj.onion/tcp/443/ws/p2p/QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE',
'QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbK',
]
expect(filterAndSortPeers(addresses, [])).toEqual(valid)
})
})
4 changes: 4 additions & 0 deletions packages/common/src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ export const isPSKcodeValid = (psk: string): boolean => {
const _psk = psk.trim()
return validator.isBase64(_psk) && _psk.length === PSK_LENGTH
}

export const filterValidAddresses = (addresses: string[]) => {
return addresses.filter(add => add.match(/^\/dns4\/[a-z0-9]{56}.onion\/tcp\/(443|80)\/ws\/p2p\/[a-zA-Z0-9]{46}$/g))
}
8 changes: 4 additions & 4 deletions packages/common/src/sortPeers.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { type NetworkStats } from '@quiet/types'
import { isDefined } from './helpers'
import { filterValidAddresses } from './libp2p'

/**
This is the very simple algorithm for evaluating the most wanted peers.
0. Filters out invalid peers addresses
1. It takes the peers stats list that contains statistics for every peer our node was ever connected to.
2. Two sorted arrays are created - one sorted by last seen and other by most uptime shared.
3. Arrays are merged taking one element from list one and one element from the second list. Duplicates are ommited
4. We end up with mix of last seen and most uptime descending array of peers, the it is enchanced to libp2p address.
*/
export const sortPeers = (peersAddresses: string[], stats: NetworkStats[]): string[] => {
peersAddresses = peersAddresses.filter(add =>
add.match(/^\/dns4\/[a-z0-9]{56}.onion\/tcp\/(443|80)\/ws\/p2p\/[a-zA-Z0-9]{46}$/g)
)
export const filterAndSortPeers = (peersAddresses: string[], stats: NetworkStats[]): string[] => {
peersAddresses = filterValidAddresses(peersAddresses)
const lastSeenSorted = [...stats].sort((a, b) => {
return b.lastSeen - a.lastSeen
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { peersStatsAdapter } from './connection.adapter'
import { connectedPeers } from '../network/network.selectors'
import { type NetworkStats } from './connection.types'
import { type User } from '../users/users.types'
import { sortPeers } from '@quiet/common'
import { filterAndSortPeers } from '@quiet/common'

const connectionSlice: CreatedSelectors[StoreKeys.Connection] = (state: StoreState) => state[StoreKeys.Connection]

Expand All @@ -33,7 +33,7 @@ export const peerList = createSelector(
stats = peersStatsAdapter.getSelectors().selectAll(reducerState.peersStats)
}

return sortPeers(arr, stats)
return filterAndSortPeers(arr, stats)
}
)

Expand Down

0 comments on commit 97a5652

Please sign in to comment.