import { BlockData , PeerAddress , ShortPeerInfo } from 'shared/model/types' ; import { MemoryPeer } from 'shared/model/Peer/memoryPeer' ; import IPeerRepository from 'core/repository/peer/index' ; import { SerializedFullHeaders } from 'shared/model/Peer/fullHeaders' ; import PeerNetworkRepository from 'core/repository/peer/peerNetwork' ; import SystemRepository from 'core/repository/system' ; import { PEER_SOCKET_TYPE } from 'shared/model/types' ; import { peerAddressToString } from 'core/util/peer' ; class PeerMemoryRepository implements IPeerRepository < PeerAddress , MemoryPeer > { private peers : Map < string , MemoryPeer > ; constructor ( ) { this . peers = new Map ( ) ; } add ( peerAddress , headers , connectionType ) { this . peers . set ( peerAddressToString ( peerAddress ) , new MemoryPeer ( { peerAddress , headers , connectionType } ) ) ; SystemRepository . update ( { peerCount : this . count } ) ; } remove ( peerAddress ) { this . peers . delete ( peerAddressToString ( peerAddress ) ) ; SystemRepository . update ( { peerCount : this . count } ) ; } removeAll ( ) { this . peers . clear ( ) ; SystemRepository . update ( { peerCount : this . count } ) ; } get ( peerAddress ) { return this . peers . get ( peerAddressToString ( peerAddress ) ) ; } getVersion ( peerAddress ) { if ( ! this . has ( peerAddress ) ) { return ; } return this . get ( peerAddress ) . headers . version ; } getManyByAddress ( peerAddresses ) { return peerAddresses . filter ( ( peerAddress ) => this . has ( peerAddress ) ) . map ( ( peerAddress ) => this . get ( peerAddress ) ) ; } getAll ( ) { return [ ... this . peers . values ( ) ] ; } getPeerAddresses ( ) { return this . getAll ( ) . map ( ( peer ) => ( { ... peer . peerAddress , peerCount : peer . headers . peerCount , } ) ) ; } has ( peerAddress ) { return this . peers . has ( peerAddressToString ( peerAddress ) ) ; } getHigherPeersByFilter ( height , broadhash ) { return [ ... this . peers . values ( ) ] . filter ( ( peer ) => { return peer . headers . height >= height && peer . headers . broadhash !== broadhash && ! PeerNetworkRepository . isBanned ( peer . peerAddress ) ; } ) ; } getLessPeersByFilter ( height , broadhash ) { return [ ... this . peers . values ( ) ] . filter ( ( peer ) => { return peer . headers . height <= height && peer . headers . broadhash !== broadhash ; } ) ; } get count ( ) : number { return this . peers . size ; } } export default new PeerMemoryRepository ( ) ;