@streamr/dht 100.0.0-testnet-one.3 → 100.0.0-testnet-two.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/connection/ConnectionLockHandler.d.ts +11 -11
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionLockRpcLocal.js +4 -4
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +3 -5
- package/dist/src/connection/ConnectionLockRpcRemote.js +4 -7
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +4 -6
- package/dist/src/connection/ConnectionManager.js +48 -46
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +2 -2
- package/dist/src/connection/ConnectorFacade.js +2 -1
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +3 -6
- package/dist/src/connection/ManagedConnection.js +6 -10
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/connectivityChecker.js +1 -0
- package/dist/src/connection/connectivityChecker.js.map +1 -1
- package/dist/src/connection/simulator/Simulator.d.ts +0 -3
- package/dist/src/connection/simulator/Simulator.js +3 -51
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.js +5 -5
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +2 -2
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +0 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js +7 -9
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +11 -12
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +2 -5
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +1 -4
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +1 -2
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +3 -4
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +2 -7
- package/dist/src/connection/websocket/WebsocketConnector.js +32 -38
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +3 -2
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +8 -10
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +3 -6
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +0 -3
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketServer.d.ts +1 -4
- package/dist/src/connection/websocket/WebsocketServer.js +11 -12
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +10 -13
- package/dist/src/dht/DhtNode.js +39 -52
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +0 -1
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +7 -7
- package/dist/src/dht/DhtNodeRpcRemote.js +8 -7
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +5 -4
- package/dist/src/dht/ExternalApiRpcLocal.js +3 -2
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +2 -2
- package/dist/src/dht/ExternalApiRpcRemote.js +2 -0
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +13 -14
- package/dist/src/dht/PeerManager.js +45 -60
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/Contact.d.ts +2 -2
- package/dist/src/dht/contact/Contact.js +2 -2
- package/dist/src/dht/contact/Contact.js.map +1 -1
- package/dist/src/dht/contact/ContactList.d.ts +7 -7
- package/dist/src/dht/contact/ContactList.js +3 -3
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +4 -4
- package/dist/src/dht/contact/RandomContactList.js +12 -11
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/RpcRemote.d.ts +7 -4
- package/dist/src/dht/contact/RpcRemote.js +5 -2
- package/dist/src/dht/contact/RpcRemote.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +13 -13
- package/dist/src/dht/contact/SortedContactList.js +33 -29
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +2 -4
- package/dist/src/dht/discovery/DiscoverySession.js +19 -20
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +2 -5
- package/dist/src/dht/discovery/PeerDiscovery.js +12 -20
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +36 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +165 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +14 -0
- package/dist/src/dht/{find/FindRpcLocal.js → recursive-operation/RecursiveOperationRpcLocal.js} +7 -7
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.d.ts +6 -0
- package/dist/src/dht/{routing/FindRpcRemote.js → recursive-operation/RecursiveOperationRpcRemote.js} +11 -11
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +42 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +182 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +12 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +17 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.d.ts +6 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js +21 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js.map +1 -0
- package/dist/src/dht/routing/Router.d.ts +4 -19
- package/dist/src/dht/routing/Router.js +41 -33
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.js +6 -5
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcRemote.d.ts +2 -2
- package/dist/src/dht/routing/RouterRpcRemote.js +7 -7
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +20 -16
- package/dist/src/dht/routing/RoutingSession.js +37 -45
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +11 -13
- package/dist/src/dht/store/LocalDataStore.js +36 -56
- package/dist/src/dht/store/LocalDataStore.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +33 -0
- package/dist/src/dht/store/StoreManager.js +186 -0
- package/dist/src/dht/store/StoreManager.js.map +1 -0
- package/dist/src/dht/store/StoreRpcLocal.d.ts +8 -40
- package/dist/src/dht/store/StoreRpcLocal.js +13 -229
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.d.ts +5 -5
- package/dist/src/dht/store/StoreRpcRemote.js +4 -5
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/exports.d.ts +2 -0
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/MapWithTtl.d.ts +14 -0
- package/dist/src/helpers/MapWithTtl.js +59 -0
- package/dist/src/helpers/MapWithTtl.js.map +1 -0
- package/dist/src/helpers/PeerID.d.ts +2 -0
- package/dist/src/helpers/PeerID.js +4 -0
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/nodeId.d.ts +5 -0
- package/dist/src/helpers/nodeId.js +18 -1
- package/dist/src/helpers/nodeId.js.map +1 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +2 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +2 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/helpers/protoClasses.js +2 -3
- package/dist/src/helpers/protoClasses.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +21 -22
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +18 -18
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +36 -60
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +34 -53
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +11 -12
- package/dist/src/rpc-protocol/DhtCallContext.d.ts +2 -1
- package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -1
- package/dist/src/rpc-protocol/DhtRpcOptions.d.ts +2 -1
- package/dist/src/transport/ITransport.d.ts +9 -1
- package/dist/src/transport/ITransport.js +5 -0
- package/dist/src/transport/ITransport.js.map +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.js +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -1
- package/dist/src/transport/RoutingRpcCommunicator.js +16 -11
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/jest.config.js +4 -1
- package/karma-setup.js +2 -0
- package/karma.config.js +11 -7
- package/package.json +6 -7
- package/protos/DhtRpc.proto +17 -23
- package/src/connection/ConnectionLockHandler.ts +15 -15
- package/src/connection/ConnectionLockRpcLocal.ts +8 -9
- package/src/connection/ConnectionLockRpcRemote.ts +7 -16
- package/src/connection/ConnectionManager.ts +58 -55
- package/src/connection/ConnectorFacade.ts +3 -0
- package/src/connection/ManagedConnection.ts +12 -30
- package/src/connection/connectivityChecker.ts +1 -0
- package/src/connection/simulator/Simulator.ts +5 -36
- package/src/connection/simulator/SimulatorConnector.ts +8 -8
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -3
- package/src/connection/webrtc/NodeWebrtcConnection.ts +3 -4
- package/src/connection/webrtc/WebrtcConnector.ts +11 -13
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +16 -16
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +3 -13
- package/src/connection/websocket/AutoCertifierClientFacade.ts +4 -6
- package/src/connection/websocket/ClientWebsocket.ts +1 -0
- package/src/connection/websocket/WebsocketConnector.ts +43 -56
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +11 -13
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +4 -15
- package/src/connection/websocket/WebsocketServer.ts +14 -17
- package/src/dht/DhtNode.ts +67 -69
- package/src/dht/DhtNodeRpcLocal.ts +0 -1
- package/src/dht/DhtNodeRpcRemote.ts +13 -13
- package/src/dht/ExternalApiRpcLocal.ts +16 -6
- package/src/dht/ExternalApiRpcRemote.ts +5 -3
- package/src/dht/PeerManager.ts +73 -90
- package/src/dht/contact/Contact.ts +5 -4
- package/src/dht/contact/ContactList.ts +9 -9
- package/src/dht/contact/RandomContactList.ts +14 -14
- package/src/dht/contact/RpcRemote.ts +10 -7
- package/src/dht/contact/SortedContactList.ts +48 -45
- package/src/dht/discovery/DiscoverySession.ts +25 -25
- package/src/dht/discovery/PeerDiscovery.ts +16 -37
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +240 -0
- package/src/dht/{find/FindRpcLocal.ts → recursive-operation/RecursiveOperationRpcLocal.ts} +9 -9
- package/src/dht/{routing/FindRpcRemote.ts → recursive-operation/RecursiveOperationRpcRemote.ts} +10 -10
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +224 -0
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +25 -0
- package/src/dht/{find/FindSessionRpcRemote.ts → recursive-operation/RecursiveOperationSessionRpcRemote.ts} +8 -8
- package/src/dht/routing/Router.ts +45 -64
- package/src/dht/routing/RouterRpcLocal.ts +7 -6
- package/src/dht/routing/RouterRpcRemote.ts +9 -10
- package/src/dht/routing/RoutingSession.ts +59 -68
- package/src/dht/store/LocalDataStore.ts +47 -66
- package/src/dht/store/StoreManager.ts +217 -0
- package/src/dht/store/StoreRpcLocal.ts +26 -302
- package/src/dht/store/StoreRpcRemote.ts +9 -11
- package/src/exports.ts +2 -0
- package/src/helpers/MapWithTtl.ts +71 -0
- package/src/helpers/PeerID.ts +5 -0
- package/src/helpers/nodeId.ts +20 -0
- package/src/helpers/peerIdFromPeerDescriptor.ts +5 -3
- package/src/helpers/protoClasses.ts +4 -6
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +31 -32
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +11 -12
- package/src/proto/packages/dht/protos/DhtRpc.ts +49 -80
- package/src/rpc-protocol/DhtCallContext.ts +2 -1
- package/src/rpc-protocol/DhtRpcOptions.ts +2 -1
- package/src/transport/ITransport.ts +10 -1
- package/src/transport/ListeningRpcCommunicator.ts +1 -1
- package/src/transport/RoutingRpcCommunicator.ts +18 -12
- package/test/benchmark/Find.test.ts +8 -21
- package/test/benchmark/KademliaCorrectness.test.ts +11 -20
- package/test/benchmark/SortedContactListBenchmark.test.ts +9 -9
- package/test/benchmark/kademlia-simulation/Contact.ts +9 -8
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +2 -2
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +17 -17
- package/test/end-to-end/Layer0-Layer1.test.ts +10 -14
- package/test/end-to-end/Layer0.test.ts +15 -18
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +7 -9
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +4 -10
- package/test/end-to-end/Layer0Webrtc.test.ts +5 -6
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +8 -16
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +6 -13
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +5 -6
- package/test/end-to-end/memory-leak.test.ts +6 -8
- package/test/integration/ConnectionLocking.test.ts +6 -13
- package/test/integration/ConnectionManager.test.ts +26 -43
- package/test/integration/DhtJoinPeerDiscovery.test.ts +5 -10
- package/test/integration/DhtNodeExternalAPI.test.ts +16 -22
- package/test/integration/DhtNodeRpcRemote.test.ts +13 -22
- package/test/integration/DhtRpc.test.ts +13 -18
- package/test/integration/Find.test.ts +10 -10
- package/test/integration/Layer1-scale.test.ts +17 -28
- package/test/integration/Mock-Layer1-Layer0.test.ts +25 -43
- package/test/integration/MultipleEntryPointJoining.test.ts +7 -7
- package/test/integration/ReplicateData.test.ts +28 -30
- package/test/integration/RouteMessage.test.ts +30 -27
- package/test/integration/RouterRpcRemote.test.ts +12 -18
- package/test/integration/RpcErrors.test.ts +8 -21
- package/test/integration/ScaleDownDht.test.ts +11 -10
- package/test/integration/SimultaneousConnections.test.ts +43 -67
- package/test/integration/Store.test.ts +23 -36
- package/test/integration/StoreAndDelete.test.ts +30 -50
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +16 -37
- package/test/integration/StoreRpcRemote.test.ts +20 -30
- package/test/integration/WebrtcConnectionManagement.test.ts +3 -13
- package/test/integration/WebrtcConnectorRpc.test.ts +3 -9
- package/test/integration/WebsocketConnectionManagement.test.ts +7 -10
- package/test/integration/WebsocketConnectorRpc.test.ts +8 -17
- package/test/unit/ConnectivityHelpers.test.ts +9 -17
- package/test/unit/DuplicateDetector.test.ts +7 -4
- package/test/unit/LocalDataStore.test.ts +66 -77
- package/test/unit/PeerManager.test.ts +31 -0
- package/test/unit/RandomContactList.test.ts +13 -8
- package/test/unit/{Finder.test.ts → RecursiveOperationManager.test.ts} +38 -43
- package/test/unit/RecursiveOperationSession.test.ts +68 -0
- package/test/unit/Router.test.ts +15 -22
- package/test/unit/RoutingSession.test.ts +23 -25
- package/test/unit/SortedContactList.test.ts +41 -26
- package/test/unit/StoreManager.test.ts +132 -0
- package/test/unit/WebsocketConnector.test.ts +27 -35
- package/test/unit/customMatchers.test.ts +16 -0
- package/test/utils/FakeTransport.ts +44 -0
- package/test/utils/customMatchers.ts +70 -0
- package/test/utils/mock/MockRpcCommunicator.ts +7 -0
- package/test/utils/mock/Router.ts +3 -2
- package/test/utils/mock/mockDataEntry.ts +36 -0
- package/test/utils/utils.ts +49 -87
- package/dist/src/dht/find/FindRpcLocal.d.ts +0 -14
- package/dist/src/dht/find/FindRpcLocal.js.map +0 -1
- package/dist/src/dht/find/FindSession.d.ts +0 -44
- package/dist/src/dht/find/FindSession.js +0 -150
- package/dist/src/dht/find/FindSession.js.map +0 -1
- package/dist/src/dht/find/FindSessionRpcLocal.d.ts +0 -12
- package/dist/src/dht/find/FindSessionRpcLocal.js +0 -17
- package/dist/src/dht/find/FindSessionRpcLocal.js.map +0 -1
- package/dist/src/dht/find/FindSessionRpcRemote.d.ts +0 -6
- package/dist/src/dht/find/FindSessionRpcRemote.js +0 -21
- package/dist/src/dht/find/FindSessionRpcRemote.js.map +0 -1
- package/dist/src/dht/find/Finder.d.ts +0 -49
- package/dist/src/dht/find/Finder.js +0 -197
- package/dist/src/dht/find/Finder.js.map +0 -1
- package/dist/src/dht/routing/FindRpcRemote.d.ts +0 -6
- package/dist/src/dht/routing/FindRpcRemote.js.map +0 -1
- package/src/dht/find/FindSession.ts +0 -183
- package/src/dht/find/FindSessionRpcLocal.ts +0 -25
- package/src/dht/find/Finder.ts +0 -295
package/src/dht/PeerManager.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
Logger
|
|
2
|
+
Logger, hexToBinary
|
|
3
3
|
} from '@streamr/utils'
|
|
4
4
|
import KBucket from 'k-bucket'
|
|
5
|
-
import { PeerID, PeerIDKey } from '../helpers/PeerID'
|
|
6
5
|
import {
|
|
7
|
-
getNodeIdFromPeerDescriptor
|
|
8
|
-
keyFromPeerDescriptor,
|
|
9
|
-
peerIdFromPeerDescriptor
|
|
6
|
+
getNodeIdFromPeerDescriptor
|
|
10
7
|
} from '../helpers/peerIdFromPeerDescriptor'
|
|
11
8
|
import {
|
|
12
9
|
PeerDescriptor
|
|
@@ -16,6 +13,7 @@ import { RandomContactList } from './contact/RandomContactList'
|
|
|
16
13
|
import { SortedContactList } from './contact/SortedContactList'
|
|
17
14
|
import { ConnectionManager } from '../connection/ConnectionManager'
|
|
18
15
|
import EventEmitter from 'eventemitter3'
|
|
16
|
+
import { NodeID, areEqualNodeIds } from '../helpers/nodeId'
|
|
19
17
|
|
|
20
18
|
const logger = new Logger(module)
|
|
21
19
|
|
|
@@ -23,7 +21,7 @@ interface PeerManagerConfig {
|
|
|
23
21
|
numberOfNodesPerKBucket: number
|
|
24
22
|
maxContactListSize: number
|
|
25
23
|
peerDiscoveryQueryBatchSize: number
|
|
26
|
-
|
|
24
|
+
localNodeId: NodeID
|
|
27
25
|
connectionManager: ConnectionManager
|
|
28
26
|
isLayer0: boolean
|
|
29
27
|
createDhtNodeRpcRemote: (peerDescriptor: PeerDescriptor) => DhtNodeRpcRemote
|
|
@@ -37,8 +35,8 @@ export interface PeerManagerEvents {
|
|
|
37
35
|
kBucketEmpty: () => void
|
|
38
36
|
}
|
|
39
37
|
|
|
40
|
-
export const getDistance = (
|
|
41
|
-
return KBucket.distance(
|
|
38
|
+
export const getDistance = (nodeId1: NodeID, nodeId2: NodeID): number => {
|
|
39
|
+
return KBucket.distance(hexToBinary(nodeId1), hexToBinary(nodeId2))
|
|
42
40
|
}
|
|
43
41
|
|
|
44
42
|
export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
@@ -50,28 +48,23 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
50
48
|
// * 'contacts' are all non-unresponsive nodes that we know about
|
|
51
49
|
|
|
52
50
|
// The kademlia k-bucket
|
|
53
|
-
private bucket
|
|
51
|
+
private bucket: KBucket<DhtNodeRpcRemote>
|
|
54
52
|
// Nodes that are connected to this node on Layer0
|
|
55
|
-
public readonly connections: Map<
|
|
53
|
+
public readonly connections: Map<NodeID, DhtNodeRpcRemote> = new Map()
|
|
56
54
|
// All nodes that we know about
|
|
57
|
-
private contacts
|
|
58
|
-
private randomPeers
|
|
59
|
-
private readonly config: PeerManagerConfig
|
|
55
|
+
private contacts: SortedContactList<DhtNodeRpcRemote>
|
|
56
|
+
private randomPeers: RandomContactList<DhtNodeRpcRemote>
|
|
60
57
|
private stopped: boolean = false
|
|
58
|
+
private readonly config: PeerManagerConfig
|
|
61
59
|
|
|
62
60
|
constructor(config: PeerManagerConfig) {
|
|
63
61
|
super()
|
|
64
62
|
this.config = config
|
|
65
|
-
this.initKBuckets()
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
private initKBuckets() {
|
|
69
63
|
this.bucket = new KBucket<DhtNodeRpcRemote>({
|
|
70
|
-
localNodeId: this.config.
|
|
64
|
+
localNodeId: hexToBinary(this.config.localNodeId),
|
|
71
65
|
numberOfNodesPerKBucket: this.config.numberOfNodesPerKBucket,
|
|
72
66
|
numberOfNodesToPing: this.config.numberOfNodesPerKBucket
|
|
73
67
|
})
|
|
74
|
-
|
|
75
68
|
this.bucket.on('ping', (oldContacts: DhtNodeRpcRemote[], newContact: DhtNodeRpcRemote) => this.onKBucketPing(oldContacts, newContact))
|
|
76
69
|
this.bucket.on('removed', (contact: DhtNodeRpcRemote) => this.onKBucketRemoved(contact))
|
|
77
70
|
this.bucket.on('added', (contact: DhtNodeRpcRemote) => this.onKBucketAdded(contact))
|
|
@@ -79,7 +72,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
79
72
|
// TODO: Update contact info to the connection manager and reconnect
|
|
80
73
|
})
|
|
81
74
|
this.contacts = new SortedContactList({
|
|
82
|
-
referenceId: this.config.
|
|
75
|
+
referenceId: this.config.localNodeId,
|
|
83
76
|
maxSize: this.config.maxContactListSize,
|
|
84
77
|
allowToContainReferenceId: false,
|
|
85
78
|
emitEvents: true
|
|
@@ -89,12 +82,12 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
89
82
|
return
|
|
90
83
|
}
|
|
91
84
|
this.emit('contactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
92
|
-
this.randomPeers
|
|
85
|
+
this.randomPeers.addContact(this.config.createDhtNodeRpcRemote(removedContact.getPeerDescriptor()))
|
|
93
86
|
})
|
|
94
87
|
this.contacts.on('newContact', (newContact: DhtNodeRpcRemote, activeContacts: DhtNodeRpcRemote[]) =>
|
|
95
88
|
this.emit('newContact', newContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
96
89
|
)
|
|
97
|
-
this.randomPeers = new RandomContactList(this.config.
|
|
90
|
+
this.randomPeers = new RandomContactList(this.config.localNodeId, this.config.maxContactListSize)
|
|
98
91
|
this.randomPeers.on('contactRemoved', (removedContact: DhtNodeRpcRemote, activeContacts: DhtNodeRpcRemote[]) =>
|
|
99
92
|
this.emit('randomContactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
100
93
|
)
|
|
@@ -108,7 +101,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
108
101
|
return
|
|
109
102
|
}
|
|
110
103
|
const sortingList: SortedContactList<DhtNodeRpcRemote> = new SortedContactList({
|
|
111
|
-
referenceId: this.config.
|
|
104
|
+
referenceId: this.config.localNodeId,
|
|
112
105
|
maxSize: 100, // TODO use config option or named constant?
|
|
113
106
|
allowToContainReferenceId: false,
|
|
114
107
|
emitEvents: false
|
|
@@ -116,8 +109,8 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
116
109
|
sortingList.addContacts(oldContacts)
|
|
117
110
|
const sortedContacts = sortingList.getAllContacts()
|
|
118
111
|
this.config.connectionManager?.weakUnlockConnection(sortedContacts[sortedContacts.length - 1].getPeerDescriptor())
|
|
119
|
-
this.bucket
|
|
120
|
-
this.bucket
|
|
112
|
+
this.bucket.remove(hexToBinary(sortedContacts[sortedContacts.length - 1].getNodeId()))
|
|
113
|
+
this.bucket.add(newContact)
|
|
121
114
|
}
|
|
122
115
|
|
|
123
116
|
private onKBucketRemoved(contact: DhtNodeRpcRemote): void {
|
|
@@ -126,7 +119,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
126
119
|
}
|
|
127
120
|
this.config.connectionManager?.weakUnlockConnection(contact.getPeerDescriptor())
|
|
128
121
|
logger.trace(`Removed contact ${getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
129
|
-
if (this.bucket
|
|
122
|
+
if (this.bucket.count() === 0) {
|
|
130
123
|
this.emit('kBucketEmpty')
|
|
131
124
|
}
|
|
132
125
|
}
|
|
@@ -135,10 +128,10 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
135
128
|
if (this.stopped) {
|
|
136
129
|
return
|
|
137
130
|
}
|
|
138
|
-
if (!contact.
|
|
131
|
+
if (!areEqualNodeIds(contact.getNodeId(), this.config.localNodeId)) {
|
|
139
132
|
// Important to lock here, before the ping result is known
|
|
140
133
|
this.config.connectionManager?.weakLockConnection(contact.getPeerDescriptor())
|
|
141
|
-
if (this.connections.has(contact.
|
|
134
|
+
if (this.connections.has(contact.getNodeId())) {
|
|
142
135
|
logger.trace(`Added new contact ${getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
143
136
|
} else { // open connection by pinging
|
|
144
137
|
logger.trace('starting ping ' + getNodeIdFromPeerDescriptor(contact.getPeerDescriptor()))
|
|
@@ -172,21 +165,22 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
172
165
|
}
|
|
173
166
|
|
|
174
167
|
private getClosestActiveContactNotInBucket(): DhtNodeRpcRemote | undefined {
|
|
175
|
-
for (const contactId of this.contacts
|
|
176
|
-
if (!this.bucket
|
|
177
|
-
return this.contacts
|
|
168
|
+
for (const contactId of this.contacts.getContactIds()) {
|
|
169
|
+
if (!this.bucket.get(hexToBinary(contactId)) && this.contacts.isActive(contactId)) {
|
|
170
|
+
return this.contacts.getContact(contactId)!.contact
|
|
178
171
|
}
|
|
179
172
|
}
|
|
180
173
|
return undefined
|
|
181
174
|
}
|
|
182
175
|
|
|
183
176
|
handleConnected(peerDescriptor: PeerDescriptor): void {
|
|
184
|
-
|
|
177
|
+
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
178
|
+
if (areEqualNodeIds(nodeId, this.config.localNodeId)) {
|
|
185
179
|
logger.error('handleConnected() to self')
|
|
186
180
|
}
|
|
187
181
|
const rpcRemote = this.config.createDhtNodeRpcRemote(peerDescriptor)
|
|
188
|
-
if (!this.connections.has(
|
|
189
|
-
this.connections.set(
|
|
182
|
+
if (!this.connections.has(nodeId)) {
|
|
183
|
+
this.connections.set(nodeId, rpcRemote)
|
|
190
184
|
logger.trace('connectionschange add ' + this.connections.size)
|
|
191
185
|
} else {
|
|
192
186
|
logger.trace('new connection not set to connections, there is already a connection with the peer ID')
|
|
@@ -196,9 +190,9 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
196
190
|
|
|
197
191
|
handleDisconnected(peerDescriptor: PeerDescriptor, gracefulLeave: boolean): void {
|
|
198
192
|
logger.trace('disconnected: ' + getNodeIdFromPeerDescriptor(peerDescriptor))
|
|
199
|
-
this.connections.delete(
|
|
193
|
+
this.connections.delete(getNodeIdFromPeerDescriptor(peerDescriptor))
|
|
200
194
|
if (this.config.isLayer0) {
|
|
201
|
-
this.bucket
|
|
195
|
+
this.bucket.remove(peerDescriptor.nodeId)
|
|
202
196
|
if (gracefulLeave === true) {
|
|
203
197
|
logger.trace(getNodeIdFromPeerDescriptor(peerDescriptor) + ' ' + 'onTransportDisconnected with gracefulLeave ' + gracefulLeave)
|
|
204
198
|
this.removeContact(peerDescriptor)
|
|
@@ -217,65 +211,54 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
217
211
|
return
|
|
218
212
|
}
|
|
219
213
|
logger.trace(`Removing contact ${getNodeIdFromPeerDescriptor(contact)}`)
|
|
220
|
-
const
|
|
221
|
-
this.bucket
|
|
222
|
-
this.contacts
|
|
223
|
-
this.randomPeers
|
|
214
|
+
const nodeId = getNodeIdFromPeerDescriptor(contact)
|
|
215
|
+
this.bucket.remove(hexToBinary(nodeId))
|
|
216
|
+
this.contacts.removeContact(nodeId)
|
|
217
|
+
this.randomPeers.removeContact(nodeId)
|
|
224
218
|
}
|
|
225
219
|
|
|
226
220
|
stop(): void {
|
|
227
221
|
this.stopped = true
|
|
228
|
-
this.bucket
|
|
222
|
+
this.bucket.toArray().forEach((rpcRemote: DhtNodeRpcRemote) => {
|
|
229
223
|
rpcRemote.leaveNotice()
|
|
230
|
-
this.bucket
|
|
224
|
+
this.bucket.remove(rpcRemote.id)
|
|
231
225
|
})
|
|
232
|
-
this.bucket
|
|
233
|
-
this.contacts
|
|
234
|
-
this.randomPeers
|
|
226
|
+
this.bucket.removeAllListeners()
|
|
227
|
+
this.contacts.stop()
|
|
228
|
+
this.randomPeers.stop()
|
|
235
229
|
this.connections.clear()
|
|
236
230
|
}
|
|
237
231
|
|
|
238
|
-
getClosestNeighborsTo(
|
|
232
|
+
getClosestNeighborsTo(referenceId: NodeID, limit?: number, excludedNodeIds?: Set<NodeID>): DhtNodeRpcRemote[] {
|
|
239
233
|
const closest = new SortedContactList<DhtNodeRpcRemote>({
|
|
240
|
-
referenceId
|
|
234
|
+
referenceId,
|
|
241
235
|
allowToContainReferenceId: true,
|
|
242
|
-
emitEvents: false
|
|
236
|
+
emitEvents: false,
|
|
237
|
+
excludedNodeIds
|
|
243
238
|
})
|
|
244
|
-
this.bucket
|
|
245
|
-
|
|
246
|
-
return closest.getClosestContacts(limit).filter((contact) => {
|
|
247
|
-
if (!excludeSet) {
|
|
248
|
-
return true
|
|
249
|
-
} else {
|
|
250
|
-
return !excludeSet.has(contact.getPeerId().toKey())
|
|
251
|
-
}
|
|
252
|
-
})
|
|
239
|
+
this.bucket.toArray().map((contact) => closest.addContact(contact))
|
|
240
|
+
return closest.getClosestContacts(limit)
|
|
253
241
|
}
|
|
254
242
|
|
|
255
243
|
// TODO reduce copy-paste?
|
|
256
|
-
getClosestContactsTo(
|
|
244
|
+
getClosestContactsTo(referenceId: NodeID, limit?: number, excludedNodeIds?: Set<NodeID>): DhtNodeRpcRemote[] {
|
|
257
245
|
const closest = new SortedContactList<DhtNodeRpcRemote>({
|
|
258
|
-
referenceId
|
|
246
|
+
referenceId,
|
|
259
247
|
allowToContainReferenceId: true,
|
|
260
|
-
emitEvents: false
|
|
248
|
+
emitEvents: false,
|
|
249
|
+
excludedNodeIds
|
|
261
250
|
})
|
|
262
|
-
this.contacts
|
|
251
|
+
this.contacts.getAllContacts().map((contact) => closest.addContact(contact))
|
|
263
252
|
// TODO should set the excludeSet and limit to SortedContactList constructor and remove these line
|
|
264
|
-
return closest.getClosestContacts(limit)
|
|
265
|
-
if (!excludeSet) {
|
|
266
|
-
return true
|
|
267
|
-
} else {
|
|
268
|
-
return !excludeSet.has(contact.getPeerId().toKey())
|
|
269
|
-
}
|
|
270
|
-
})
|
|
253
|
+
return closest.getClosestContacts(limit)
|
|
271
254
|
}
|
|
272
255
|
|
|
273
|
-
getNumberOfContacts(
|
|
274
|
-
return this.contacts
|
|
275
|
-
if (!
|
|
256
|
+
getNumberOfContacts(excludedNodeIds?: Set<NodeID>): number {
|
|
257
|
+
return this.contacts.getAllContacts().filter((contact) => {
|
|
258
|
+
if (!excludedNodeIds) {
|
|
276
259
|
return true
|
|
277
260
|
} else {
|
|
278
|
-
return !
|
|
261
|
+
return !excludedNodeIds.has(contact.getNodeId())
|
|
279
262
|
}
|
|
280
263
|
}).length
|
|
281
264
|
}
|
|
@@ -285,20 +268,20 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
285
268
|
}
|
|
286
269
|
|
|
287
270
|
getNumberOfNeighbors(): number {
|
|
288
|
-
return this.bucket
|
|
271
|
+
return this.bucket.count()
|
|
289
272
|
}
|
|
290
273
|
|
|
291
274
|
getNeighbors(): PeerDescriptor[] {
|
|
292
|
-
return this.bucket
|
|
275
|
+
return this.bucket.toArray().map((rpcRemote: DhtNodeRpcRemote) => rpcRemote.getPeerDescriptor())
|
|
293
276
|
}
|
|
294
277
|
|
|
295
|
-
handlePeerActive(
|
|
296
|
-
this.contacts
|
|
278
|
+
handlePeerActive(nodeId: NodeID): void {
|
|
279
|
+
this.contacts.setActive(nodeId)
|
|
297
280
|
}
|
|
298
281
|
|
|
299
|
-
handlePeerUnresponsive(
|
|
300
|
-
this.bucket
|
|
301
|
-
this.contacts
|
|
282
|
+
handlePeerUnresponsive(nodeId: NodeID): void {
|
|
283
|
+
this.bucket.remove(hexToBinary(nodeId))
|
|
284
|
+
this.contacts.removeContact(nodeId)
|
|
302
285
|
}
|
|
303
286
|
|
|
304
287
|
handleNewPeers(peerDescriptors: PeerDescriptor[], setActive?: boolean): void {
|
|
@@ -306,23 +289,23 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
306
289
|
return
|
|
307
290
|
}
|
|
308
291
|
peerDescriptors.forEach((contact) => {
|
|
309
|
-
const
|
|
310
|
-
if (!
|
|
311
|
-
logger.trace(`Adding new contact ${
|
|
292
|
+
const nodeId = getNodeIdFromPeerDescriptor(contact)
|
|
293
|
+
if (!areEqualNodeIds(nodeId, this.config.localNodeId)) {
|
|
294
|
+
logger.trace(`Adding new contact ${nodeId}`)
|
|
312
295
|
const remote = this.config.createDhtNodeRpcRemote(contact)
|
|
313
|
-
const isInBucket = (this.bucket
|
|
314
|
-
const
|
|
315
|
-
if (isInBucket ||
|
|
316
|
-
this.randomPeers
|
|
296
|
+
const isInBucket = (this.bucket.get(contact.nodeId) !== null)
|
|
297
|
+
const isInContacts = (this.contacts.getContact(nodeId) !== undefined)
|
|
298
|
+
if (isInBucket || isInContacts) {
|
|
299
|
+
this.randomPeers.addContact(remote)
|
|
317
300
|
}
|
|
318
301
|
if (!isInBucket) {
|
|
319
|
-
this.bucket
|
|
302
|
+
this.bucket.add(remote)
|
|
320
303
|
}
|
|
321
|
-
if (!
|
|
322
|
-
this.contacts
|
|
304
|
+
if (!isInContacts) {
|
|
305
|
+
this.contacts.addContact(remote)
|
|
323
306
|
}
|
|
324
307
|
if (setActive) {
|
|
325
|
-
this.contacts
|
|
308
|
+
this.contacts.setActive(nodeId)
|
|
326
309
|
}
|
|
327
310
|
}
|
|
328
311
|
})
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { PeerID } from '../../helpers/PeerID'
|
|
2
1
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
3
|
-
import {
|
|
2
|
+
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
3
|
+
import { NodeID } from '../../helpers/nodeId'
|
|
4
4
|
|
|
5
5
|
export class Contact {
|
|
6
|
+
|
|
6
7
|
private peerDescriptor: PeerDescriptor
|
|
7
8
|
|
|
8
9
|
constructor(peerDescriptor: PeerDescriptor) {
|
|
@@ -13,7 +14,7 @@ export class Contact {
|
|
|
13
14
|
return this.peerDescriptor
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
public
|
|
17
|
-
return
|
|
17
|
+
public getNodeId(): NodeID {
|
|
18
|
+
return getNodeIdFromPeerDescriptor(this.peerDescriptor)
|
|
18
19
|
}
|
|
19
20
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
2
1
|
import EventEmitter from 'eventemitter3'
|
|
2
|
+
import { NodeID } from '../../helpers/nodeId'
|
|
3
3
|
|
|
4
4
|
export class ContactState<C> {
|
|
5
5
|
public contacted = false
|
|
@@ -16,28 +16,28 @@ export interface Events<C> {
|
|
|
16
16
|
newContact: (newContact: C, closestContacts: C[]) => void
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export class ContactList<C extends {
|
|
19
|
+
export class ContactList<C extends { getNodeId: () => NodeID }> extends EventEmitter<Events<C>> {
|
|
20
20
|
|
|
21
|
-
protected contactsById: Map<
|
|
21
|
+
protected contactsById: Map<NodeID, ContactState<C>> = new Map()
|
|
22
22
|
// TODO move this to SortedContactList
|
|
23
|
-
protected contactIds:
|
|
24
|
-
protected
|
|
23
|
+
protected contactIds: NodeID[] = []
|
|
24
|
+
protected localNodeId: NodeID
|
|
25
25
|
protected maxSize: number
|
|
26
26
|
protected defaultContactQueryLimit
|
|
27
27
|
|
|
28
28
|
constructor(
|
|
29
|
-
|
|
29
|
+
localNodeId: NodeID,
|
|
30
30
|
maxSize: number,
|
|
31
31
|
defaultContactQueryLimit = 20
|
|
32
32
|
) {
|
|
33
33
|
super()
|
|
34
|
-
this.
|
|
34
|
+
this.localNodeId = localNodeId
|
|
35
35
|
this.maxSize = maxSize
|
|
36
36
|
this.defaultContactQueryLimit = defaultContactQueryLimit
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
public getContact(id:
|
|
40
|
-
return this.contactsById.get(id
|
|
39
|
+
public getContact(id: NodeID): ContactState<C> | undefined {
|
|
40
|
+
return this.contactsById.get(id)
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
public getSize(): number {
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NodeID, areEqualNodeIds } from '../../helpers/nodeId'
|
|
2
2
|
import { ContactList, ContactState } from './ContactList'
|
|
3
3
|
|
|
4
|
-
export class RandomContactList<C extends {
|
|
4
|
+
export class RandomContactList<C extends { getNodeId: () => NodeID }> extends ContactList<C> {
|
|
5
5
|
|
|
6
6
|
private randomness: number
|
|
7
7
|
|
|
8
8
|
constructor(
|
|
9
|
-
|
|
9
|
+
localNodeId: NodeID,
|
|
10
10
|
maxSize: number,
|
|
11
11
|
randomness = 0.20,
|
|
12
12
|
defaultContactQueryLimit?: number
|
|
13
13
|
) {
|
|
14
|
-
super(
|
|
14
|
+
super(localNodeId, maxSize, defaultContactQueryLimit)
|
|
15
15
|
this.randomness = randomness
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
addContact(contact: C): void {
|
|
19
|
-
if (this.
|
|
19
|
+
if (areEqualNodeIds(this.localNodeId, contact.getNodeId())) {
|
|
20
20
|
return
|
|
21
21
|
}
|
|
22
|
-
if (!this.contactsById.has(contact.
|
|
22
|
+
if (!this.contactsById.has(contact.getNodeId())) {
|
|
23
23
|
const roll = Math.random()
|
|
24
24
|
if (roll < this.randomness) {
|
|
25
25
|
if (this.getSize() === this.maxSize && this.getSize() > 0) {
|
|
26
26
|
const toRemove = this.contactIds[0]
|
|
27
27
|
this.removeContact(toRemove)
|
|
28
28
|
}
|
|
29
|
-
this.contactIds.push(contact.
|
|
30
|
-
this.contactsById.set(contact.
|
|
29
|
+
this.contactIds.push(contact.getNodeId())
|
|
30
|
+
this.contactsById.set(contact.getNodeId(), new ContactState(contact))
|
|
31
31
|
this.emit(
|
|
32
32
|
'newContact',
|
|
33
33
|
contact,
|
|
@@ -37,12 +37,12 @@ export class RandomContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
removeContact(id:
|
|
41
|
-
if (this.contactsById.has(id
|
|
42
|
-
const removed = this.contactsById.get(id
|
|
43
|
-
const index = this.contactIds.findIndex((
|
|
40
|
+
removeContact(id: NodeID): boolean {
|
|
41
|
+
if (this.contactsById.has(id)) {
|
|
42
|
+
const removed = this.contactsById.get(id)!.contact
|
|
43
|
+
const index = this.contactIds.findIndex((nodeId) => areEqualNodeIds(nodeId, id))
|
|
44
44
|
this.contactIds.splice(index, 1)
|
|
45
|
-
this.contactsById.delete(id
|
|
45
|
+
this.contactsById.delete(id)
|
|
46
46
|
this.emit('contactRemoved', removed, this.getContacts())
|
|
47
47
|
return true
|
|
48
48
|
}
|
|
@@ -52,7 +52,7 @@ export class RandomContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
52
52
|
public getContacts(limit = this.defaultContactQueryLimit): C[] {
|
|
53
53
|
const ret: C[] = []
|
|
54
54
|
this.contactIds.forEach((contactId) => {
|
|
55
|
-
const contact = this.contactsById.get(contactId
|
|
55
|
+
const contact = this.contactsById.get(contactId)
|
|
56
56
|
if (contact) {
|
|
57
57
|
ret.push(contact.contact)
|
|
58
58
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import type { ServiceInfo } from '@protobuf-ts/runtime-rpc'
|
|
2
|
+
import { ClassType, ClientTransport, ProtoRpcClient, RpcCommunicator, toProtoRpcClient } from '@streamr/proto-rpc'
|
|
3
|
+
import { ConnectionType } from '../../connection/IConnection'
|
|
4
|
+
import { expectedConnectionType } from '../../helpers/Connectivity'
|
|
1
5
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
2
|
-
import { ProtoRpcClient } from '@streamr/proto-rpc'
|
|
3
6
|
import { DhtRpcOptions } from '../../rpc-protocol/DhtRpcOptions'
|
|
4
7
|
import { ServiceID } from '../../types/ServiceID'
|
|
5
|
-
import { ConnectionType } from '../../connection/IConnection'
|
|
6
|
-
import { expectedConnectionType } from '../../helpers/Connectivity'
|
|
7
8
|
|
|
8
9
|
// Should connect directly to the server, timeout can be low
|
|
9
10
|
const WEBSOCKET_CLIENT_TIMEOUT = 5000
|
|
@@ -27,7 +28,7 @@ const getTimeout = (localPeerDescriptor: PeerDescriptor, remotePeerDescriptor: P
|
|
|
27
28
|
return WEBRTC_TIMEOUT
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
export abstract class RpcRemote<T> {
|
|
31
|
+
export abstract class RpcRemote<T extends ServiceInfo & ClassType> {
|
|
31
32
|
|
|
32
33
|
private readonly localPeerDescriptor: PeerDescriptor
|
|
33
34
|
private readonly remotePeerDescriptor: PeerDescriptor
|
|
@@ -39,12 +40,14 @@ export abstract class RpcRemote<T> {
|
|
|
39
40
|
localPeerDescriptor: PeerDescriptor,
|
|
40
41
|
remotePeerDescriptor: PeerDescriptor,
|
|
41
42
|
serviceId: ServiceID,
|
|
42
|
-
|
|
43
|
+
rpcCommunicator: RpcCommunicator,
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/prefer-function-type
|
|
45
|
+
clientClass: { new (clientTransport: ClientTransport): T },
|
|
43
46
|
timeout?: number
|
|
44
47
|
) {
|
|
45
48
|
this.localPeerDescriptor = localPeerDescriptor
|
|
46
49
|
this.remotePeerDescriptor = remotePeerDescriptor
|
|
47
|
-
this.client =
|
|
50
|
+
this.client = toProtoRpcClient(new clientClass(rpcCommunicator.getRpcClientTransport()))
|
|
48
51
|
this.serviceId = serviceId
|
|
49
52
|
this.timeout = timeout
|
|
50
53
|
}
|
|
@@ -57,7 +60,7 @@ export abstract class RpcRemote<T> {
|
|
|
57
60
|
return this.localPeerDescriptor
|
|
58
61
|
}
|
|
59
62
|
|
|
60
|
-
getServiceId():
|
|
63
|
+
getServiceId(): ServiceID {
|
|
61
64
|
return this.serviceId
|
|
62
65
|
}
|
|
63
66
|
|