import { P2P , events } from '../../src/index' ; import { wait } from '../utils/helpers' ; import { createNetwork , destroyNetwork } from '../utils/network_setup' ; const { EVENT_MESSAGE_RECEIVED , EVENT_BAN_PEER , EVENT_REMOVE_PEER , EVENT_CLOSE_OUTBOUND } = events ; describe ( 'Backup and Restore' , ( ) => { let p2pNodeList = [ ] ; let firstNode ; let secondNode ; let messageCounter = 0 ; const bannedMessages = [ ] ; const removedPeer = [ ] ; const disconnectMessages = [ ] ; beforeEach ( async ( ) => { const customConfig = ( ) => ( { wsMaxMessageRate : 110 , rateCalculationInterval : 300 , wsMaxMessageRatePenalty : 100 , } ) ; p2pNodeList = await createNetwork ( { customConfig , networkSize : 2 } ) ; [ firstNode , secondNode ] = p2pNodeList ; secondNode . on ( EVENT_MESSAGE_RECEIVED , _message => { messageCounter += 1 ; } ) ; secondNode . on ( EVENT_BAN_PEER , peerId => { bannedMessages . push ( peerId ) ; } ) ; secondNode . on ( EVENT_REMOVE_PEER , peerId => { removedPeer . push ( peerId ) ; } ) ; secondNode . on ( EVENT_CLOSE_OUTBOUND , closePacket => { disconnectMessages . push ( closePacket ) ; } ) ; } ) ; afterEach ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; } ) ; it ( 'send messages to second peer' , async ( ) => { const targetPeerId = `127.0.0.1:${ secondNode . config . port }` ; const TOTAL_SENDS = 5 ; const CUSTOM_DISCONNECT_MESSAGE = 'Intentional disconnect **' ; for ( let i = 0 ; i < TOTAL_SENDS ; i += 1 ) { await wait ( 2 ) ; try { firstNode . sendToPeer ( { event : 'foo' , data : i , } , targetPeerId ) ; } catch ( error ) { } } await wait ( 50 ) ; const getFirstConnectedPeer = secondNode [ '_peerPool' ] . getConnectedPeers ( ) . find ( peerInfo => peerInfo . id === `127.0.0.1:${ firstNode . config . port }` ) ; if ( getFirstConnectedPeer ) { getFirstConnectedPeer . disconnect ( 4009 , CUSTOM_DISCONNECT_MESSAGE ) ; expect ( messageCounter ) . toBeGreaterThan ( TOTAL_SENDS - 2 ) ; const disconnectFirstPeer = secondNode [ '_peerBook' ] . getPeer ( { ipAddress : '127.0.0.1' , port : 5000 , peerId : '127.0.0.1:5000' , } ) ; if ( disconnectFirstPeer ) { expect ( ( disconnectFirstPeer . internalState as any ) . messageCounter . get ( 'foo' ) ) . toBe ( TOTAL_SENDS ) ; } await wait ( 10 ) ; expect ( disconnectMessages . map ( msg => msg . reason ) ) . toContain ( CUSTOM_DISCONNECT_MESSAGE ) ; const getFirstNodeSecondTime = secondNode [ '_peerPool' ] [ '_peerMap' ] . get ( `127.0.0.1:${ firstNode . config . port }` ) ; if ( getFirstNodeSecondTime ) { for ( let i = 0 ; i < TOTAL_SENDS ; i += 1 ) { await wait ( 2 ) ; try { firstNode . sendToPeer ( { event : 'foo' , data : i , } , targetPeerId ) ; } catch ( error ) { } } await wait ( 20 ) ; expect ( ( getFirstNodeSecondTime . peerInfo . internalState as any ) . messageCounter . get ( 'foo' ) ) . toBe ( TOTAL_SENDS * 2 ) ; } expect ( removedPeer . length ) . toBeGreaterThan ( 0 ) ; for ( let i = 0 ; i < 200 ; i ++ ) { await wait ( 1 ) ; try { firstNode . sendToPeer ( { event : 'foo' , data : i , } , targetPeerId ) ; } catch ( error ) { } } await wait ( 200 ) ; expect ( bannedMessages ) . toHaveLength ( 1 ) ; } } ) ; } ) ;