import { platform } from 'os' ; import { P2P , constants } from '../../src/index' ; import { wait } from './helpers' ; const { DEFAULT_MAX_OUTBOUND_CONNECTIONS , DEFAULT_MAX_INBOUND_CONNECTIONS } = constants ; export const NETWORK_START_PORT = 5000 ; export const NETWORK_PEER_COUNT = 10 ; export const POPULATOR_INTERVAL = 50 ; export const DEFAULT_CONNECTION_TIMEOUT = 500 ; export const DEFAULT_ACK_TIMEOUT = 500 ; export const RATE_CALCULATION_INTERVAL = 10000 ; export const SEED_PEER_IP = '127.0.0.1' ; export const NETWORK_CREATION_WAIT_TIME = 1000 ; export const NETWORK_DESTROY_WAIT_TIME = 1000 ; export const FALLBACK_PEER_DISCOVER_INTERVAL = 800 ; export const nodeInfoConstants = { networkIdentifier : 'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba' , version : '1.0.1' , networkVersion : '1.1' , minVersion : '1.0.0' , os : platform ( ) , height : 0 , nonce : 'O2wTkjqplHII' , } ; interface TestNetworkConfig { networkSize ? : number ; startNodePort ? : number ; networkDiscoveryWaitTime ? : number ; customConfig ? : ( index : number , startPort : number , networkSize : number ) => object ; } export const createNetwork = async ( { networkSize , startNodePort , networkDiscoveryWaitTime , customConfig , } = { } ) : Promise < P2P [ ] > => { const numberOfPeers = networkSize ?? NETWORK_PEER_COUNT ; const startPort = startNodePort ?? NETWORK_START_PORT ; const p2pNodeList = [ ... new Array ( numberOfPeers ) . keys ( ) ] . map ( index => { const defaultSeedPeers = index === 0 ? [ ] : [ { ipAddress : SEED_PEER_IP , port : NETWORK_START_PORT + index - 1 , } , ] ; const nodePort = NETWORK_START_PORT + index ; const { nodeInfo : customNodeInfo , ... customConfigObject } = customConfig ? ( customConfig ( index , startPort , numberOfPeers ) as any ) : { nodeInfo : { } } ; const p2pConfig = { port : nodePort , connectTimeout : DEFAULT_CONNECTION_TIMEOUT , ackTimeout : DEFAULT_ACK_TIMEOUT , rateCalculationInterval : RATE_CALCULATION_INTERVAL , seedPeers : defaultSeedPeers , populatorInterval : POPULATOR_INTERVAL + Math . floor ( ( POPULATOR_INTERVAL / NETWORK_PEER_COUNT ) * index ) , maxOutboundConnections : DEFAULT_MAX_OUTBOUND_CONNECTIONS , maxInboundConnections : DEFAULT_MAX_INBOUND_CONNECTIONS , fallbackSeedPeerDiscoveryInterval : FALLBACK_PEER_DISCOVER_INTERVAL , nodeInfo : { networkIdentifier : nodeInfoConstants . networkIdentifier , networkVersion : nodeInfoConstants . networkVersion , nonce : `${ nodeInfoConstants . nonce }${ nodePort }` , options : { } , ... customNodeInfo , } , ... customConfigObject , } ; return new P2P ( p2pConfig ) ; } ) ; if ( networkDiscoveryWaitTime !== 0 ) { await Promise . all ( p2pNodeList . map ( async ( p2p ) => p2p . start ( ) ) ) ; await wait ( networkDiscoveryWaitTime ?? NETWORK_CREATION_WAIT_TIME ) ; } return p2pNodeList ; } ; export const destroyNetwork = async ( p2pNodeList , networkDestroyWaitTime ? ) : Promise < void > => { await Promise . all ( p2pNodeList . filter ( p2p => p2p . isActive ) . map ( async ( p2p ) => p2p . stop ( ) ) ) ; await wait ( networkDestroyWaitTime ?? NETWORK_DESTROY_WAIT_TIME ) ; } ;