@streamr/dht 100.0.0-testnet-one.4 → 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
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
2
1
|
import { ContactState, Events } from './ContactList'
|
|
3
2
|
import { sortedIndexBy } from 'lodash'
|
|
4
3
|
import EventEmitter from 'eventemitter3'
|
|
5
4
|
import { getDistance } from '../PeerManager'
|
|
5
|
+
import { NodeID, areEqualNodeIds } from '../../helpers/nodeId'
|
|
6
6
|
|
|
7
7
|
export interface SortedContactListConfig {
|
|
8
|
-
referenceId:
|
|
8
|
+
referenceId: NodeID // all contacts in this list are in sorted by the distance to this ID
|
|
9
9
|
allowToContainReferenceId: boolean
|
|
10
10
|
// TODO could maybe optimize this by removing the flag and then we'd check whether we have
|
|
11
11
|
// any listeners before we emit the event
|
|
12
12
|
emitEvents: boolean
|
|
13
13
|
maxSize?: number
|
|
14
14
|
// if set, the list can't contain any contacts which are futher away than this limit
|
|
15
|
-
|
|
15
|
+
nodeIdDistanceLimit?: NodeID
|
|
16
16
|
// if set, the list can't contain contacts with these ids
|
|
17
|
-
|
|
17
|
+
excludedNodeIds?: Set<NodeID>
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export class SortedContactList<C extends {
|
|
20
|
+
export class SortedContactList<C extends { getNodeId: () => NodeID }> extends EventEmitter<Events<C>> {
|
|
21
21
|
|
|
22
22
|
private config: SortedContactListConfig
|
|
23
|
-
private contactsById: Map<
|
|
24
|
-
private contactIds:
|
|
23
|
+
private contactsById: Map<NodeID, ContactState<C>> = new Map()
|
|
24
|
+
private contactIds: NodeID[] = []
|
|
25
25
|
|
|
26
26
|
constructor(
|
|
27
27
|
config: SortedContactListConfig
|
|
@@ -31,38 +31,39 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Ev
|
|
|
31
31
|
this.compareIds = this.compareIds.bind(this)
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
public getClosestContactId():
|
|
34
|
+
public getClosestContactId(): NodeID {
|
|
35
35
|
return this.contactIds[0]
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
public getContactIds():
|
|
38
|
+
public getContactIds(): NodeID[] {
|
|
39
39
|
return this.contactIds
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
public addContact(contact: C): void {
|
|
43
|
-
if (this.config.
|
|
44
|
-
&& this.config.excludedPeerIDs.some((peerId) => contact.getPeerId().equals(peerId))) {
|
|
43
|
+
if (this.config.excludedNodeIds !== undefined && this.config.excludedNodeIds.has(contact.getNodeId())) {
|
|
45
44
|
return
|
|
46
45
|
}
|
|
47
46
|
|
|
48
|
-
if ((!this.config.allowToContainReferenceId && this.config.referenceId
|
|
49
|
-
(this.config.
|
|
47
|
+
if ((!this.config.allowToContainReferenceId && areEqualNodeIds(this.config.referenceId, contact.getNodeId())) ||
|
|
48
|
+
(this.config.nodeIdDistanceLimit !== undefined && this.compareIds(this.config.nodeIdDistanceLimit, contact.getNodeId()) < 0)) {
|
|
50
49
|
return
|
|
51
50
|
}
|
|
52
|
-
if (!this.contactsById.has(contact.
|
|
51
|
+
if (!this.contactsById.has(contact.getNodeId())) {
|
|
53
52
|
if ((this.config.maxSize === undefined) || (this.contactIds.length < this.config.maxSize)) {
|
|
54
|
-
this.contactsById.set(contact.
|
|
53
|
+
this.contactsById.set(contact.getNodeId(), new ContactState(contact))
|
|
55
54
|
|
|
56
|
-
|
|
57
|
-
this.contactIds.
|
|
58
|
-
|
|
55
|
+
// eslint-disable-next-line max-len
|
|
56
|
+
const index = sortedIndexBy(this.contactIds, contact.getNodeId(), (id: NodeID) => { return this.distanceToReferenceId(id) })
|
|
57
|
+
this.contactIds.splice(index, 0, contact.getNodeId())
|
|
58
|
+
} else if (this.compareIds(this.contactIds[this.config.maxSize - 1], contact.getNodeId()) > 0) {
|
|
59
59
|
const removedId = this.contactIds.pop()
|
|
60
|
-
const removedContact = this.contactsById.get(removedId
|
|
61
|
-
this.contactsById.delete(removedId
|
|
62
|
-
this.contactsById.set(contact.
|
|
60
|
+
const removedContact = this.contactsById.get(removedId!)!.contact
|
|
61
|
+
this.contactsById.delete(removedId!)
|
|
62
|
+
this.contactsById.set(contact.getNodeId(), new ContactState(contact))
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
this.contactIds.
|
|
64
|
+
// eslint-disable-next-line max-len
|
|
65
|
+
const index = sortedIndexBy(this.contactIds, contact.getNodeId(), (id: NodeID) => { return this.distanceToReferenceId(id) })
|
|
66
|
+
this.contactIds.splice(index, 0, contact.getNodeId())
|
|
66
67
|
if (this.config.emitEvents) {
|
|
67
68
|
this.emit(
|
|
68
69
|
'contactRemoved',
|
|
@@ -85,26 +86,26 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Ev
|
|
|
85
86
|
contacts.forEach((contact) => this.addContact(contact))
|
|
86
87
|
}
|
|
87
88
|
|
|
88
|
-
public getContact(id:
|
|
89
|
-
return this.contactsById.get(id
|
|
89
|
+
public getContact(id: NodeID): ContactState<C> | undefined {
|
|
90
|
+
return this.contactsById.get(id)
|
|
90
91
|
}
|
|
91
92
|
|
|
92
|
-
public setContacted(contactId:
|
|
93
|
-
if (this.contactsById.has(contactId
|
|
94
|
-
this.contactsById.get(contactId
|
|
93
|
+
public setContacted(contactId: NodeID): void {
|
|
94
|
+
if (this.contactsById.has(contactId)) {
|
|
95
|
+
this.contactsById.get(contactId)!.contacted = true
|
|
95
96
|
}
|
|
96
97
|
}
|
|
97
98
|
|
|
98
|
-
public setActive(contactId:
|
|
99
|
-
if (this.contactsById.has(contactId
|
|
100
|
-
this.contactsById.get(contactId
|
|
99
|
+
public setActive(contactId: NodeID): void {
|
|
100
|
+
if (this.contactsById.has(contactId)) {
|
|
101
|
+
this.contactsById.get(contactId)!.active = true
|
|
101
102
|
}
|
|
102
103
|
}
|
|
103
104
|
|
|
104
105
|
public getClosestContacts(limit?: number): C[] {
|
|
105
106
|
const ret: C[] = []
|
|
106
107
|
this.contactIds.forEach((contactId) => {
|
|
107
|
-
const contact = this.contactsById.get(contactId
|
|
108
|
+
const contact = this.contactsById.get(contactId)
|
|
108
109
|
if (contact) {
|
|
109
110
|
ret.push(contact.contact)
|
|
110
111
|
}
|
|
@@ -119,7 +120,7 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Ev
|
|
|
119
120
|
public getUncontactedContacts(num: number): C[] {
|
|
120
121
|
const ret: C[] = []
|
|
121
122
|
for (const contactId of this.contactIds) {
|
|
122
|
-
const contact = this.contactsById.get(contactId
|
|
123
|
+
const contact = this.contactsById.get(contactId)
|
|
123
124
|
if (contact && !contact.contacted) {
|
|
124
125
|
ret.push(contact.contact)
|
|
125
126
|
if (ret.length >= num) {
|
|
@@ -133,7 +134,7 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Ev
|
|
|
133
134
|
public getActiveContacts(limit?: number): C[] {
|
|
134
135
|
const ret: C[] = []
|
|
135
136
|
this.contactIds.forEach((contactId) => {
|
|
136
|
-
const contact = this.contactsById.get(contactId
|
|
137
|
+
const contact = this.contactsById.get(contactId)
|
|
137
138
|
if (contact && contact.active) {
|
|
138
139
|
ret.push(contact.contact)
|
|
139
140
|
}
|
|
@@ -145,23 +146,25 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Ev
|
|
|
145
146
|
}
|
|
146
147
|
}
|
|
147
148
|
|
|
148
|
-
public compareIds(id1:
|
|
149
|
+
public compareIds(id1: NodeID, id2: NodeID): number {
|
|
149
150
|
const distance1 = this.distanceToReferenceId(id1)
|
|
150
151
|
const distance2 = this.distanceToReferenceId(id2)
|
|
151
152
|
return distance1 - distance2
|
|
152
153
|
}
|
|
153
154
|
|
|
154
155
|
// TODO inline this method?
|
|
155
|
-
private distanceToReferenceId(id:
|
|
156
|
-
|
|
156
|
+
private distanceToReferenceId(id: NodeID): number {
|
|
157
|
+
// TODO maybe this class should store the referenceId also as UInt8Array so that we don't need to convert it here?
|
|
158
|
+
return getDistance(this.config.referenceId, id)
|
|
157
159
|
}
|
|
158
160
|
|
|
159
|
-
public removeContact(id:
|
|
160
|
-
if (this.contactsById.has(id
|
|
161
|
-
const removed = this.contactsById.get(id
|
|
162
|
-
|
|
161
|
+
public removeContact(id: NodeID): boolean {
|
|
162
|
+
if (this.contactsById.has(id)) {
|
|
163
|
+
const removed = this.contactsById.get(id)!.contact
|
|
164
|
+
// TODO use sortedIndexBy?
|
|
165
|
+
const index = this.contactIds.findIndex((nodeId) => areEqualNodeIds(nodeId, id))
|
|
163
166
|
this.contactIds.splice(index, 1)
|
|
164
|
-
this.contactsById.delete(id
|
|
167
|
+
this.contactsById.delete(id)
|
|
165
168
|
if (this.config.emitEvents) {
|
|
166
169
|
this.emit(
|
|
167
170
|
'contactRemoved',
|
|
@@ -174,12 +177,12 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Ev
|
|
|
174
177
|
return false
|
|
175
178
|
}
|
|
176
179
|
|
|
177
|
-
public isActive(id:
|
|
178
|
-
return this.contactsById.has(id
|
|
180
|
+
public isActive(id: NodeID): boolean {
|
|
181
|
+
return this.contactsById.has(id) ? this.contactsById.get(id)!.active : false
|
|
179
182
|
}
|
|
180
183
|
|
|
181
184
|
public getAllContacts(): C[] {
|
|
182
|
-
return this.contactIds.map((
|
|
185
|
+
return this.contactIds.map((nodeId) => this.contactsById.get(nodeId)!.contact)
|
|
183
186
|
}
|
|
184
187
|
|
|
185
188
|
public getSize(): number {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Logger, runAndWaitForEvents3 } from '@streamr/utils'
|
|
2
2
|
import EventEmitter from 'eventemitter3'
|
|
3
3
|
import { v4 } from 'uuid'
|
|
4
|
-
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
5
4
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
6
5
|
import { PeerManager, getDistance } from '../PeerManager'
|
|
7
6
|
import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
|
|
8
7
|
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
8
|
+
import { NodeID, getNodeIdFromBinary } from '../../helpers/nodeId'
|
|
9
9
|
|
|
10
10
|
const logger = new Logger(module)
|
|
11
11
|
|
|
@@ -18,18 +18,16 @@ interface DiscoverySessionConfig {
|
|
|
18
18
|
parallelism: number
|
|
19
19
|
noProgressLimit: number
|
|
20
20
|
peerManager: PeerManager
|
|
21
|
-
// Note that contacted peers will be mutated by the DiscoverySession or other parallel sessions
|
|
22
|
-
contactedPeers: Set<PeerIDKey>
|
|
23
21
|
}
|
|
24
22
|
|
|
25
23
|
export class DiscoverySession {
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
|
|
25
|
+
public readonly id = v4()
|
|
28
26
|
private stopped = false
|
|
29
27
|
private emitter = new EventEmitter<DiscoverySessionEvents>()
|
|
30
|
-
private outgoingClosestPeersRequestsCounter = 0
|
|
31
28
|
private noProgressCounter = 0
|
|
32
|
-
private ongoingClosestPeersRequests: Set<
|
|
29
|
+
private ongoingClosestPeersRequests: Set<NodeID> = new Set()
|
|
30
|
+
private contactedPeers: Set<NodeID> = new Set()
|
|
33
31
|
private readonly config: DiscoverySessionConfig
|
|
34
32
|
|
|
35
33
|
constructor(config: DiscoverySessionConfig) {
|
|
@@ -48,23 +46,22 @@ export class DiscoverySession {
|
|
|
48
46
|
return []
|
|
49
47
|
}
|
|
50
48
|
logger.trace(`Getting closest peers from contact: ${getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
51
|
-
this.
|
|
52
|
-
this.config.contactedPeers.add(contact.getPeerId().toKey())
|
|
49
|
+
this.contactedPeers.add(contact.getNodeId())
|
|
53
50
|
const returnedContacts = await contact.getClosestPeers(this.config.targetId)
|
|
54
|
-
this.config.peerManager.handlePeerActive(contact.
|
|
51
|
+
this.config.peerManager.handlePeerActive(contact.getNodeId())
|
|
55
52
|
return returnedContacts
|
|
56
53
|
}
|
|
57
54
|
|
|
58
|
-
private onClosestPeersRequestSucceeded(
|
|
59
|
-
if (!this.ongoingClosestPeersRequests.has(
|
|
55
|
+
private onClosestPeersRequestSucceeded(nodeId: NodeID, contacts: PeerDescriptor[]) {
|
|
56
|
+
if (!this.ongoingClosestPeersRequests.has(nodeId)) {
|
|
60
57
|
return
|
|
61
58
|
}
|
|
62
|
-
this.ongoingClosestPeersRequests.delete(
|
|
63
|
-
const oldClosestNeighbor = this.config.peerManager.getClosestNeighborsTo(this.config.targetId, 1)[0]
|
|
64
|
-
const oldClosestDistance = getDistance(this.config.targetId, oldClosestNeighbor.
|
|
59
|
+
this.ongoingClosestPeersRequests.delete(nodeId)
|
|
60
|
+
const oldClosestNeighbor = this.config.peerManager.getClosestNeighborsTo(getNodeIdFromBinary(this.config.targetId), 1)[0]
|
|
61
|
+
const oldClosestDistance = getDistance(getNodeIdFromBinary(this.config.targetId), oldClosestNeighbor.getNodeId())
|
|
65
62
|
this.addNewContacts(contacts)
|
|
66
|
-
const newClosestNeighbor = this.config.peerManager.getClosestNeighborsTo(this.config.targetId, 1)[0]
|
|
67
|
-
const newClosestDistance = getDistance(this.config.targetId, newClosestNeighbor.
|
|
63
|
+
const newClosestNeighbor = this.config.peerManager.getClosestNeighborsTo(getNodeIdFromBinary(this.config.targetId), 1)[0]
|
|
64
|
+
const newClosestDistance = getDistance(getNodeIdFromBinary(this.config.targetId), newClosestNeighbor.getNodeId())
|
|
68
65
|
if (newClosestDistance >= oldClosestDistance) {
|
|
69
66
|
this.noProgressCounter++
|
|
70
67
|
} else {
|
|
@@ -73,18 +70,22 @@ export class DiscoverySession {
|
|
|
73
70
|
}
|
|
74
71
|
|
|
75
72
|
private onClosestPeersRequestFailed(peer: DhtNodeRpcRemote) {
|
|
76
|
-
if (!this.ongoingClosestPeersRequests.has(peer.
|
|
73
|
+
if (!this.ongoingClosestPeersRequests.has(peer.getNodeId())) {
|
|
77
74
|
return
|
|
78
75
|
}
|
|
79
|
-
this.ongoingClosestPeersRequests.delete(peer.
|
|
80
|
-
this.config.peerManager.handlePeerUnresponsive(peer.
|
|
76
|
+
this.ongoingClosestPeersRequests.delete(peer.getNodeId())
|
|
77
|
+
this.config.peerManager.handlePeerUnresponsive(peer.getNodeId())
|
|
81
78
|
}
|
|
82
79
|
|
|
83
80
|
private findMoreContacts(): void {
|
|
84
81
|
if (this.stopped) {
|
|
85
82
|
return
|
|
86
83
|
}
|
|
87
|
-
const uncontacted = this.config.peerManager.getClosestContactsTo(
|
|
84
|
+
const uncontacted = this.config.peerManager.getClosestContactsTo(
|
|
85
|
+
getNodeIdFromBinary(this.config.targetId),
|
|
86
|
+
this.config.parallelism,
|
|
87
|
+
this.contactedPeers
|
|
88
|
+
)
|
|
88
89
|
if (uncontacted.length === 0 || this.noProgressCounter >= this.config.noProgressLimit) {
|
|
89
90
|
this.emitter.emit('discoveryCompleted')
|
|
90
91
|
this.stopped = true
|
|
@@ -94,20 +95,19 @@ export class DiscoverySession {
|
|
|
94
95
|
if (this.ongoingClosestPeersRequests.size >= this.config.parallelism) {
|
|
95
96
|
break
|
|
96
97
|
}
|
|
97
|
-
this.ongoingClosestPeersRequests.add(nextPeer.
|
|
98
|
+
this.ongoingClosestPeersRequests.add(nextPeer.getNodeId())
|
|
98
99
|
// eslint-disable-next-line promise/catch-or-return
|
|
99
100
|
this.getClosestPeersFromContact(nextPeer)
|
|
100
|
-
.then((contacts) => this.onClosestPeersRequestSucceeded(nextPeer.
|
|
101
|
+
.then((contacts) => this.onClosestPeersRequestSucceeded(nextPeer.getNodeId(), contacts))
|
|
101
102
|
.catch(() => this.onClosestPeersRequestFailed(nextPeer))
|
|
102
103
|
.finally(() => {
|
|
103
|
-
this.outgoingClosestPeersRequestsCounter--
|
|
104
104
|
this.findMoreContacts()
|
|
105
105
|
})
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
public async findClosestNodes(timeout: number): Promise<void> {
|
|
110
|
-
if (this.config.peerManager.getNumberOfContacts(this.
|
|
110
|
+
if (this.config.peerManager.getNumberOfContacts(this.contactedPeers) === 0) {
|
|
111
111
|
return
|
|
112
112
|
}
|
|
113
113
|
// TODO add abortController and signal it in stop()
|
|
@@ -7,7 +7,6 @@ import { ConnectionManager } from '../../connection/ConnectionManager'
|
|
|
7
7
|
import { PeerManager } from '../PeerManager'
|
|
8
8
|
import { createRandomNodeId } from '../../helpers/nodeId'
|
|
9
9
|
import { ServiceID } from '../../types/ServiceID'
|
|
10
|
-
import { PeerIDKey } from '../../helpers/PeerID'
|
|
11
10
|
|
|
12
11
|
interface PeerDiscoveryConfig {
|
|
13
12
|
localPeerDescriptor: PeerDescriptor
|
|
@@ -24,40 +23,19 @@ const logger = new Logger(module)
|
|
|
24
23
|
|
|
25
24
|
export class PeerDiscovery {
|
|
26
25
|
|
|
27
|
-
private readonly config: PeerDiscoveryConfig
|
|
28
26
|
private ongoingDiscoverySessions: Map<string, DiscoverySession> = new Map()
|
|
29
27
|
private rejoinOngoing = false
|
|
30
28
|
private joinCalled = false
|
|
31
|
-
private rejoinTimeoutRef?: NodeJS.Timeout
|
|
32
29
|
private readonly abortController: AbortController
|
|
33
30
|
private recoveryIntervalStarted = false
|
|
31
|
+
private readonly config: PeerDiscoveryConfig
|
|
34
32
|
|
|
35
33
|
constructor(config: PeerDiscoveryConfig) {
|
|
36
34
|
this.config = config
|
|
37
35
|
this.abortController = new AbortController()
|
|
38
36
|
}
|
|
39
37
|
|
|
40
|
-
async joinDht(
|
|
41
|
-
entryPoints: PeerDescriptor[],
|
|
42
|
-
doAdditionalRandomPeerDiscovery = true,
|
|
43
|
-
retry = true
|
|
44
|
-
): Promise<void> {
|
|
45
|
-
const contactedPeers = new Set<PeerIDKey>()
|
|
46
|
-
await Promise.all(entryPoints.map((entryPoint) => this.joinThroughEntryPoint(
|
|
47
|
-
entryPoint,
|
|
48
|
-
contactedPeers,
|
|
49
|
-
doAdditionalRandomPeerDiscovery,
|
|
50
|
-
retry
|
|
51
|
-
)))
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async joinThroughEntryPoint(
|
|
55
|
-
entryPointDescriptor: PeerDescriptor,
|
|
56
|
-
// Note that this set is mutated by DiscoverySession
|
|
57
|
-
contactedPeers: Set<PeerIDKey>,
|
|
58
|
-
doAdditionalRandomPeerDiscovery = true,
|
|
59
|
-
retry = true
|
|
60
|
-
): Promise<void> {
|
|
38
|
+
async joinDht(entryPointDescriptor: PeerDescriptor, doAdditionalRandomPeerDiscovery = true, retry = true): Promise<void> {
|
|
61
39
|
if (this.isStopped()) {
|
|
62
40
|
return
|
|
63
41
|
}
|
|
@@ -72,22 +50,21 @@ export class PeerDiscovery {
|
|
|
72
50
|
this.config.connectionManager?.lockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
|
|
73
51
|
this.config.peerManager.handleNewPeers([entryPointDescriptor])
|
|
74
52
|
const targetId = peerIdFromPeerDescriptor(this.config.localPeerDescriptor).value
|
|
75
|
-
const sessions = [this.createSession(targetId
|
|
53
|
+
const sessions = [this.createSession(targetId)]
|
|
76
54
|
if (doAdditionalRandomPeerDiscovery) {
|
|
77
|
-
sessions.push(this.createSession(createRandomNodeId()
|
|
55
|
+
sessions.push(this.createSession(createRandomNodeId()))
|
|
78
56
|
}
|
|
79
57
|
await this.runSessions(sessions, entryPointDescriptor, retry)
|
|
80
58
|
this.config.connectionManager?.unlockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
|
|
81
59
|
|
|
82
60
|
}
|
|
83
61
|
|
|
84
|
-
private createSession(targetId: Uint8Array
|
|
62
|
+
private createSession(targetId: Uint8Array): DiscoverySession {
|
|
85
63
|
const sessionOptions = {
|
|
86
64
|
targetId,
|
|
87
65
|
parallelism: this.config.parallelism,
|
|
88
66
|
noProgressLimit: this.config.joinNoProgressLimit,
|
|
89
|
-
peerManager: this.config.peerManager
|
|
90
|
-
contactedPeers
|
|
67
|
+
peerManager: this.config.peerManager
|
|
91
68
|
}
|
|
92
69
|
return new DiscoverySession(sessionOptions)
|
|
93
70
|
}
|
|
@@ -95,7 +72,7 @@ export class PeerDiscovery {
|
|
|
95
72
|
private async runSessions(sessions: DiscoverySession[], entryPointDescriptor: PeerDescriptor, retry: boolean): Promise<void> {
|
|
96
73
|
try {
|
|
97
74
|
for (const session of sessions) {
|
|
98
|
-
this.ongoingDiscoverySessions.set(session.
|
|
75
|
+
this.ongoingDiscoverySessions.set(session.id, session)
|
|
99
76
|
await session.findClosestNodes(this.config.joinTimeout)
|
|
100
77
|
}
|
|
101
78
|
} catch (_e) {
|
|
@@ -105,13 +82,14 @@ export class PeerDiscovery {
|
|
|
105
82
|
if (this.config.peerManager.getNumberOfNeighbors() === 0) {
|
|
106
83
|
if (retry) {
|
|
107
84
|
// TODO should we catch possible promise rejection?
|
|
85
|
+
// TODO use config option or named constant?
|
|
108
86
|
setAbortableTimeout(() => this.rejoinDht(entryPointDescriptor), 1000, this.abortController.signal)
|
|
109
87
|
}
|
|
110
88
|
} else {
|
|
111
89
|
await this.ensureRecoveryIntervalIsRunning()
|
|
112
90
|
}
|
|
113
91
|
}
|
|
114
|
-
sessions.forEach((session) => this.ongoingDiscoverySessions.delete(session.
|
|
92
|
+
sessions.forEach((session) => this.ongoingDiscoverySessions.delete(session.id))
|
|
115
93
|
}
|
|
116
94
|
}
|
|
117
95
|
|
|
@@ -122,12 +100,13 @@ export class PeerDiscovery {
|
|
|
122
100
|
logger.debug(`Rejoining DHT ${this.config.serviceId}`)
|
|
123
101
|
this.rejoinOngoing = true
|
|
124
102
|
try {
|
|
125
|
-
await this.
|
|
103
|
+
await this.joinDht(entryPoint)
|
|
126
104
|
logger.debug(`Rejoined DHT successfully ${this.config.serviceId}!`)
|
|
127
105
|
} catch (err) {
|
|
128
106
|
logger.warn(`Rejoining DHT ${this.config.serviceId} failed`)
|
|
129
107
|
if (!this.isStopped()) {
|
|
130
108
|
// TODO should we catch possible promise rejection?
|
|
109
|
+
// TODO use config option or named constant?
|
|
131
110
|
setAbortableTimeout(() => this.rejoinDht(entryPoint), 5000, this.abortController.signal)
|
|
132
111
|
}
|
|
133
112
|
} finally {
|
|
@@ -138,6 +117,7 @@ export class PeerDiscovery {
|
|
|
138
117
|
private async ensureRecoveryIntervalIsRunning(): Promise<void> {
|
|
139
118
|
if (!this.recoveryIntervalStarted) {
|
|
140
119
|
this.recoveryIntervalStarted = true
|
|
120
|
+
// TODO use config option or named constant?
|
|
141
121
|
await scheduleAtInterval(() => this.fetchClosestPeersFromBucket(), 60000, true, this.abortController.signal)
|
|
142
122
|
}
|
|
143
123
|
}
|
|
@@ -146,7 +126,10 @@ export class PeerDiscovery {
|
|
|
146
126
|
if (this.isStopped()) {
|
|
147
127
|
return
|
|
148
128
|
}
|
|
149
|
-
const nodes = this.config.peerManager.getClosestNeighborsTo(
|
|
129
|
+
const nodes = this.config.peerManager.getClosestNeighborsTo(
|
|
130
|
+
getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor),
|
|
131
|
+
this.config.parallelism
|
|
132
|
+
)
|
|
150
133
|
await Promise.allSettled(
|
|
151
134
|
nodes.map(async (peer: DhtNodeRpcRemote) => {
|
|
152
135
|
const contacts = await peer.getClosestPeers(this.config.localPeerDescriptor.nodeId!)
|
|
@@ -169,10 +152,6 @@ export class PeerDiscovery {
|
|
|
169
152
|
|
|
170
153
|
public stop(): void {
|
|
171
154
|
this.abortController.abort()
|
|
172
|
-
if (this.rejoinTimeoutRef) {
|
|
173
|
-
clearTimeout(this.rejoinTimeoutRef)
|
|
174
|
-
this.rejoinTimeoutRef = undefined
|
|
175
|
-
}
|
|
176
155
|
this.ongoingDiscoverySessions.forEach((session) => {
|
|
177
156
|
session.stop()
|
|
178
157
|
})
|