import { P2P , events } from '../../src/index' ; import { createNetwork , destroyNetwork , NETWORK_CREATION_WAIT_TIME , SEED_PEER_IP , } from '../utils/network_setup' ; import { wait } from '../utils/helpers' ; import { constructPeerId } from '../../src/utils' ; import { Peer } from '../../src/peer' ; describe ( 'Reject control messages' , ( ) => { let p2pNodeList = [ ] ; let bannedPeers = [ ] ; beforeEach ( async ( ) => { p2pNodeList = await createNetwork ( { networkDiscoveryWaitTime : 0 , networkSize : 3 , } ) ; bannedPeers = [ ] ; for ( const p2p of p2pNodeList ) { p2p . on ( events . EVENT_BAN_PEER , peerId => { bannedPeers . push ( peerId ) ; } ) ; } await Promise . all ( p2pNodeList . map ( async ( p2p ) => p2p . start ( ) ) ) ; await wait ( NETWORK_CREATION_WAIT_TIME ) ; } ) ; afterEach ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; } ) ; describe ( '#ping' , ( ) => { it ( 'should ban peer when receive ping message on an outgoing connection' , async ( ) => { const firstNode = p2pNodeList [ 0 ] ; const secondNode = p2pNodeList [ 1 ] ; const peer = firstNode [ '_peerPool' ] . getPeer ( constructPeerId ( SEED_PEER_IP , secondNode . config . port ) ) as Peer ; ( peer [ '_socket' ] as any ) . socket . ping ( ) ; await wait ( 100 ) ; expect ( secondNode . getConnectedPeers ( ) . map ( p => p . port ) ) . not . toContain ( firstNode . config . port ) ; expect ( bannedPeers . length ) . toBeGreaterThan ( 0 ) ; } ) ; it ( 'should ban peer when receive ping message on an incoming connection' , async ( ) => { const secondNode = p2pNodeList [ 1 ] ; const thirdNode = p2pNodeList [ 2 ] ; const peer = thirdNode [ '_peerPool' ] [ '_peerMap' ] . get ( `${ SEED_PEER_IP }:${ secondNode . config . port }` ) as Peer ; ( peer [ '_socket' ] as any ) . transport . socket . ping ( ) ; await wait ( 100 ) ; expect ( secondNode . getConnectedPeers ( ) . map ( p => p . port ) ) . not . toContain ( thirdNode . config . port ) ; expect ( bannedPeers . length ) . toBeGreaterThan ( 0 ) ; } ) ; } ) ; describe ( '#pong' , ( ) => { it ( 'should ban peer when receive pong message on an outgoing connection' , async ( ) => { const firstNode = p2pNodeList [ 0 ] ; const secondNode = p2pNodeList [ 1 ] ; const peer = firstNode [ '_peerPool' ] . getPeer ( constructPeerId ( SEED_PEER_IP , secondNode . config . port ) ) as Peer ; ( peer [ '_socket' ] as any ) . socket . pong ( ) ; await wait ( 200 ) ; expect ( secondNode . getConnectedPeers ( ) . map ( p => p . port ) ) . not . toContain ( firstNode . config . port ) ; expect ( bannedPeers . length ) . toBeGreaterThan ( 0 ) ; } ) ; it ( 'should ban peer when receive pong message on an incoming connection' , async ( ) => { const secondNode = p2pNodeList [ 1 ] ; const thirdNode = p2pNodeList [ 2 ] ; const peer = thirdNode [ '_peerPool' ] [ '_peerMap' ] . get ( `${ SEED_PEER_IP }:${ secondNode . config . port }` ) as Peer ; ( peer [ '_socket' ] as any ) . transport . socket . pong ( ) ; await wait ( 100 ) ; expect ( secondNode . getConnectedPeers ( ) . map ( p => p . port ) ) . not . toContain ( thirdNode . config . port ) ; expect ( bannedPeers . length ) . toBeGreaterThan ( 0 ) ; } ) ; } ) ; } ) ;