import { isIPV4 } from '@liskhq/lisk-validator' ; import { P2P } from '../../src/index' ; import { wait } from '../utils/helpers' ; import { createNetwork , destroyNetwork } from '../utils/network_setup' ; describe ( 'Sanitize peer lists on load' , ( ) => { const initPeerInfoList = ( qty ) => { const peerInfos = [ ] ; for ( let i = 1 ; i <= qty ; i += 1 ) { peerInfos . push ( { peerId : `200:db8:85a3:8d3:1319:8a2e:370:7348:${ 5000 + ( i % 40000 ) }` , ipAddress : '200:db8:85a3:8d3:1319:8a2e:370:7348' , port : 5000 + ( i % 40000 ) , } ) ; } return peerInfos ; } ; describe ( 'Address validation' , ( ) => { let p2pNodeList = [ ] ; beforeAll ( async ( ) => { const customConfig = ( ) => ( { rateCalculationInterval : 100 , previousPeers : initPeerInfoList ( 200 ) , } ) ; p2pNodeList = await createNetwork ( { networkDiscoveryWaitTime : 0 , customConfig , networkSize : 4 , } ) ; await Promise . all ( p2pNodeList . map ( async ( p2p ) => p2p . start ( ) ) ) ; await wait ( 1000 ) ; } ) ; afterAll ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; await wait ( 200 ) ; } ) ; it ( 'should not contain invalid addresses in any of its peer list' , ( ) => { for ( const p2p of p2pNodeList ) { const { allPeers } = p2p [ '_peerBook' ] ; const allPeerAddresses = allPeers . map ( peerInfo => peerInfo . ipAddress ) . sort ( ) ; const connectedPeerAddresses = p2p . getConnectedPeers ( ) . map ( peerInfo => peerInfo . ipAddress ) . sort ( ) ; expect ( [ ... allPeerAddresses , ... connectedPeerAddresses ] . filter ( ipAddress => ! isIPV4 ( ipAddress ) ) ) . toHaveLength ( 0 ) ; } } ) ; } ) ; } ) ;