import { P2P , events } from '../../../src/index' ; import { InvalidNodeInfoError } from '../../../src/errors' ; import { wait } from '../../utils/helpers' ; import { createNetwork , destroyNetwork } from '../../utils/network_setup' ; import { P2PConfig } from '../../../src/types' ; const { EVENT_MESSAGE_RECEIVED , REMOTE_EVENT_POST_NODE_INFO } = events ; const customNodeInfoSchema = { $id : '/junk' , type : 'object' , properties : { height : { dataType : 'uint32' , fieldNumber : 1 , } , junk : { dataType : 'string' , fieldNumber : 2 , } , } , } ; describe ( 'P2P.applyNodeInfo' , ( ) => { let p2pNodeList = [ ] ; let collectedMessages = [ ] ; beforeAll ( async ( ) => { const customConfig = ( ) : Partial < P2PConfig > => ( { customNodeInfoSchema , nodeInfo : { options : { height : 1 , } , } as any , } ) ; p2pNodeList = await createNetwork ( { customConfig , networkSize : 4 } ) ; collectedMessages = [ ] ; for ( const p2p of p2pNodeList ) { p2p . on ( EVENT_MESSAGE_RECEIVED , request => { if ( request . event === REMOTE_EVENT_POST_NODE_INFO && request . peerId === '127.0.0.1:5000' ) { collectedMessages . push ( { nodePort : p2p . config . port , request , } ) ; } } ) ; } const firstP2PNode = p2pNodeList [ 0 ] ; firstP2PNode . applyNodeInfo ( { networkIdentifier : 'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba' , networkVersion : '1.1' , advertiseAddress : true , options : { height : 10 , } , } ) ; await wait ( 200 ) ; } ) ; afterAll ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; } ) ; it ( 'should throw error when applying too large NodeInfo' , ( ) => { const firstP2PNode = p2pNodeList [ 0 ] ; expect ( ( ) => firstP2PNode . applyNodeInfo ( { networkIdentifier : 'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba' , networkVersion : '1.1' , advertiseAddress : true , options : { junk : '1.' . repeat ( 130000 ) , } , } ) ) . toThrow ( InvalidNodeInfoError ) ; } ) ; it ( 'should send the node info to peers' , ( ) => { const firstP2PNode = p2pNodeList [ 0 ] ; const nodePortToMessagesMap = { } ; const connectedPeerCount = firstP2PNode . getConnectedPeers ( ) . length ; expect ( collectedMessages ) . toHaveLength ( connectedPeerCount ) ; for ( const receivedMessageData of collectedMessages ) { if ( ! nodePortToMessagesMap [ receivedMessageData . nodePort ] ) { nodePortToMessagesMap [ receivedMessageData . nodePort ] = [ ] ; } nodePortToMessagesMap [ receivedMessageData . nodePort ] . push ( receivedMessageData ) ; } Object . values ( nodePortToMessagesMap ) . filter ( ( receivedMessages ) => receivedMessages ?. [ 0 ] && receivedMessages [ 0 ] . nodePort !== firstP2PNode . config . port ) . forEach ( ( receivedMessages ) => { expect ( receivedMessages ) . toHaveLength ( 1 ) ; expect ( receivedMessages [ 0 ] . request ) . toMatchObject ( { data : { networkIdentifier : 'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba' , networkVersion : '1.1' , nonce : firstP2PNode . nodeInfo . nonce , advertiseAddress : true , } , } ) ; } ) ; for ( const p2pNode of p2pNodeList . slice ( 1 ) ) { const firstP2PNodePeerInfo = p2pNode . getConnectedPeers ( ) . find ( peerInfo => peerInfo . port === firstP2PNode . config . port ) ; expect ( firstP2PNodePeerInfo ) . toMatchObject ( { options : { } , ipAddress : '127.0.0.1' , networkIdentifier : 'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba' , peerId : '127.0.0.1:5000' , port : 5000 , } ) ; } } ) ; it ( 'should update itself and reflect new node info' , ( ) => { const firstP2PNode = p2pNodeList [ 0 ] ; for ( const p2pNode of p2pNodeList . slice ( 1 ) ) { const firstNodeInConnectedPeer = p2pNode . getConnectedPeers ( ) . find ( peerInfo => peerInfo . port === firstP2PNode . config . port ) ; const allPeersList = p2pNode [ '_peerBook' ] . allPeers ; const firstNodeInAllPeersList = allPeersList . find ( peerInfo => peerInfo . port === firstP2PNode . config . port ) ; if ( firstNodeInAllPeersList ) { expect ( firstNodeInAllPeersList ) . toMatchObject ( { sharedState : { networkIdentifier : 'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba' , nonce : expect . any ( String ) , } , ipAddress : '127.0.0.1' , port : 5000 , peerId : '127.0.0.1:5000' , } ) ; } if ( firstNodeInConnectedPeer ) { expect ( firstNodeInConnectedPeer ) . toMatchObject ( { networkIdentifier : 'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba' , nonce : expect . any ( String ) , ipAddress : '127.0.0.1' , port : 5000 , peerId : '127.0.0.1:5000' , } ) ; } } } ) ; } ) ;