import { MemoryPeer } from 'shared/model/Peer/memoryPeer' ; import { PeerAddress } from 'shared/model/types' ; type MemoryHeight = { MAX_HEIGHT : number ; MIN_HEIGHT : number ; } ; export const blockHeightMemoryConsensus = ( memoryPeers ) : MemoryHeight => { const heightBlockPeers = new Map ( ) ; memoryPeers . forEach ( ( memoryPeer ) => { [ ... memoryPeer . headers . blocksIds . keys ( ) ] . forEach ( ( height ) => { if ( ! heightBlockPeers . has ( height ) ) { heightBlockPeers . set ( height , new Set ( [ memoryPeer . peerAddress ] ) ) ; } else { heightBlockPeers . get ( height ) . add ( memoryPeer . peerAddress ) ; } } ) ; } ) ; const PeersMatchesCount = [ ... heightBlockPeers . values ( ) ] . map ( ( peers ) : number => { return peers . size ; } ) ; const MaxPeersMatches = Math . max ( ... PeersMatchesCount ) ; const MaxMatchesHeight = [ ] ; [ ... heightBlockPeers . entries ( ) ] . forEach ( ( [ height , peers ] ) => { if ( peers . size === MaxPeersMatches ) { MaxMatchesHeight . push ( height ) ; } } ) ; return { MAX_HEIGHT : Math . max ( ... MaxMatchesHeight ) , MIN_HEIGHT : Math . min ( ... MaxMatchesHeight ) } ; } ; export const peerAddressToString = ( peerAddress ) : string => { return `${ peerAddress . ip }:${ peerAddress . port }` ; } ; export const diffArrayPeers = ( arrayA , arrayB ) : Array < PeerAddress > => { const setIp = new Set ( arrayB . map ( peer => peer . ip ) ) ; return arrayA . filter ( str => ! setIp . has ( str . ip ) ) ; } ;