import { Test } from 'tape' ; import { DPT , ETH , RLPx , genPrivateKey } from '../../src' ; import Common , { Chain } from '@ethereumjs/common' ; import testdata from '../testdata.json' ; export const localhost = '127.0.0.1' ; export const basePort = 30306 ; export function getTestDPTs ( numDPTs ) { const dpts = [ ] ; for ( let i = 0 ; i < numDPTs ; ++ i ) { const dpt = new DPT ( genPrivateKey ( ) , { endpoint : { address : localhost , udpPort : basePort + i , tcpPort : basePort + i , } , timeout : 100 , } ) ; dpt . bind ( basePort + i ) ; dpts . push ( dpt ) ; } return dpts ; } export function getTestDPTsWithDns ( numDPTs ) { const dpts = [ ] ; for ( let i = 0 ; i < numDPTs ; ++ i ) { const dpt = new DPT ( genPrivateKey ( ) , { endpoint : { address : localhost , udpPort : basePort + i , tcpPort : basePort + i , } , timeout : 100 , refreshInterval : 10 , dnsNetworks : [ testdata . dns . enrTree ] , shouldFindNeighbours : false , shouldGetDnsPeers : true , } ) ; dpt . bind ( basePort + i ) ; dpts . push ( dpt ) ; } return dpts ; } export function initTwoPeerDPTSetup ( ) { const dpts = getTestDPTs ( 2 ) ; const peer = { address : localhost , udpPort : basePort + 1 } ; dpts [ 0 ] . addPeer ( peer ) ; return dpts ; } export function destroyDPTs ( dpts ) { for ( const dpt of dpts ) dpt . destroy ( ) ; } export function getTestRLPXs ( numRLPXs , maxPeers = 10 , capabilities ? , common ? ) { const rlpxs = [ ] ; if ( ! capabilities ) { capabilities = [ ETH . eth66 , ETH . eth65 , ETH . eth64 , ETH . eth63 , ETH . eth62 ] ; } if ( ! common ) { common = new Common ( { chain : Chain . Mainnet } ) ; } const dpts = getTestDPTs ( numRLPXs ) ; for ( let i = 0 ; i < numRLPXs ; ++ i ) { const rlpx = new RLPx ( dpts [ i ] . privateKey , { dpt : dpts [ i ] , maxPeers : maxPeers , capabilities : capabilities , common : common . constructor === Array ? common [ i ] : ( common as Common ) , listenPort : basePort + i , } ) ; rlpx . listen ( basePort + i ) ; rlpxs . push ( rlpx ) ; } return rlpxs ; } export function initTwoPeerRLPXSetup ( maxPeers ? , capabilities ? , common ? ) { const rlpxs = getTestRLPXs ( 2 , maxPeers , capabilities , common ) ; const peer = { address : localhost , udpPort : basePort + 1 , tcpPort : basePort + 1 } ; rlpxs [ 0 ] . _dpt ! . addPeer ( peer ) ; return rlpxs ; } param { Test } t param { Array } capabilities param opts param { Dictionary } opts . status0 param { Dictionary } opts . status1 param { Function } opts . onOnceStatus0 param { Function } opts . onPeerError0 param { Function } opts . onPeerError1 param { Function } opts . onOnMsg0 param { Function } opts . onOnMsg1 export function twoPeerMsgExchange ( t , opts , capabilities ? , common ? ) { const rlpxs = initTwoPeerRLPXSetup ( null , capabilities , common ) ; rlpxs [ 0 ] . on ( 'peer:added' , function ( peer ) { const protocol = peer . getProtocols ( ) [ 0 ] ; protocol . sendStatus ( opts . status0 ) ; protocol . once ( 'status' , ( ) => { if ( opts . onOnceStatus0 ) opts . onOnceStatus0 ( rlpxs , protocol ) ; } ) ; protocol . on ( 'message' , async ( code , payload ) => { if ( opts . onOnMsg0 ) opts . onOnMsg0 ( rlpxs , protocol , code , payload ) ; } ) ; peer . on ( 'error' , ( err ) => { if ( opts . onPeerError0 ) { opts . onPeerError0 ( err , rlpxs ) ; } else { t . fail ( `Unexpected peer 0 error: ${ err }` ) ; } } ) ; } ) ; rlpxs [ 1 ] . on ( 'peer:added' , function ( peer ) { const protocol = peer . getProtocols ( ) [ 0 ] ; protocol . on ( 'message' , async ( code , payload ) => { switch ( code ) { case 0x00 : t . pass ( 'should receive initial status message' ) ; protocol . sendStatus ( opts . status1 ) ; break ; } if ( opts . onOnMsg1 ) opts . onOnMsg1 ( rlpxs , protocol , code , payload ) ; } ) ; peer . on ( 'error' , ( err ) => { if ( opts . onPeerError1 ) { opts . onPeerError1 ( err , rlpxs ) ; } else { t . fail ( `Unexpected peer 1 error: ${ err }` ) ; } } ) ; } ) ; } export function destroyRLPXs ( rlpxs ) { for ( const rlpx of rlpxs ) { rlpx . _dpt . destroy ( ) ; rlpx . destroy ( ) ; } } export function twoPeerMsgExchange2 ( t , opts , capabilities ? , common ? ) { const rlpxs = initTwoPeerRLPXSetup ( null , capabilities , common ) ; rlpxs [ 0 ] . on ( 'peer:added' , function ( peer ) { const protocol = peer . getProtocols ( ) [ 0 ] ; const v4Hello = { protocolVersion : 4 , clientId : 'fakePeer' , capabilities : [ ETH . eth66 ] , port : 30303 , id : Buffer . alloc ( 12 ) , } ; protocol . _peer . _hello = v4Hello ; protocol . sendStatus ( opts . status0 ) ; peer . on ( 'error' , ( err ) => { t . fail ( `Unexpected peer 0 error: ${ err }` ) ; } ) ; } ) ; rlpxs [ 1 ] . on ( 'peer:added' , function ( peer ) { const protocol = peer . getProtocols ( ) [ 0 ] ; protocol . once ( 'message' , async ( code , _payload ) => { switch ( code ) { case ETH . MESSAGE_CODES . STATUS : t . fail ( 'should not have been able to process status message' ) ; break ; } } ) ; peer . once ( 'error' , ( err ) => { t . equal ( err . message , 'Invalid Snappy bitstream' , 'unable to process snappy compressed message' ) ; t . end ( ) ; destroyRLPXs ( rlpxs ) ; } ) ; } ) ; }