import { P2P , events , constants } 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' ; describe ( 'P2P protocol message limit' , ( ) => { let p2pNodeList = [ ] ; let bannedPeer = '' ; beforeEach ( async ( ) => { const customConfig = ( index ) => ( { maxOutboundConnections : index % 2 === 1 ? 3 : 20 , fallbackSeedPeerDiscoveryInterval : index === 2 ? 100 : 10000 , rateCalculationInterval : 1000 , wsMaxMessageRatePenalty : constants . DEFAULT_WS_MAX_MESSAGE_RATE , populatorInterval : index === 2 ? 100 : 10000 , } ) ; p2pNodeList = await createNetwork ( { networkDiscoveryWaitTime : 0 , networkSize : 3 , customConfig , } ) ; for ( const p2p of p2pNodeList ) { p2p . on ( events . EVENT_BAN_PEER , peerId => { bannedPeer = peerId ; } ) ; } await Promise . all ( p2pNodeList . map ( async ( p2p ) => p2p . start ( ) ) ) ; await wait ( NETWORK_CREATION_WAIT_TIME ) ; } ) ; afterEach ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; } ) ; it ( 'should apply penalty for limit exceed' , async ( ) => { await wait ( 100 ) ; expect ( bannedPeer ) . toBe ( constructPeerId ( SEED_PEER_IP , p2pNodeList [ 2 ] . config . port ) ) ; } ) ; } ) ;