@streamr/dht 100.2.5-beta.0 → 101.0.0-beta.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/package.json +7 -7
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionLockRpcLocal.js +8 -8
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.js +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +4 -6
- package/dist/src/connection/ConnectionManager.js +128 -103
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +15 -14
- package/dist/src/connection/ConnectorFacade.js +70 -52
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +9 -2
- package/dist/src/connection/Handshaker.js +117 -27
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +13 -38
- package/dist/src/connection/ManagedConnection.js +31 -252
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/OutputBuffer.d.ts +9 -0
- package/dist/src/connection/OutputBuffer.js +26 -0
- package/dist/src/connection/OutputBuffer.js.map +1 -0
- package/dist/src/connection/PendingConnection.d.ts +19 -0
- package/dist/src/connection/PendingConnection.js +59 -0
- package/dist/src/connection/PendingConnection.js.map +1 -0
- package/dist/src/connection/connectivityChecker.js +3 -3
- package/dist/src/connection/connectivityChecker.js.map +1 -1
- package/dist/src/connection/connectivityRequestHandler.js +2 -2
- package/dist/src/connection/connectivityRequestHandler.js.map +1 -1
- package/dist/src/connection/simulator/Simulator.d.ts +1 -3
- package/dist/src/connection/simulator/Simulator.js +1 -4
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +1 -2
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.d.ts +3 -3
- package/dist/src/connection/simulator/SimulatorConnector.js +28 -21
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.d.ts +1 -6
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +3 -20
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +11 -6
- package/dist/src/connection/webrtc/WebrtcConnector.js +57 -42
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +8 -10
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +21 -44
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js +8 -2
- package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +3 -3
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +8 -8
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/NodeWebsocketClientConnection.js +1 -1
- package/dist/src/connection/websocket/NodeWebsocketClientConnection.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketClientConnector.d.ts +26 -0
- package/dist/src/connection/websocket/WebsocketClientConnector.js +86 -0
- package/dist/src/connection/websocket/WebsocketClientConnector.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.d.ts +19 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js +23 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.d.ts +5 -0
- package/dist/src/connection/websocket/{WebsocketConnectorRpcRemote.js → WebsocketClientConnectorRpcRemote.js} +4 -4
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketServer.d.ts +8 -5
- package/dist/src/connection/websocket/WebsocketServer.js +11 -11
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/connection/websocket/{WebsocketConnector.d.ts → WebsocketServerConnector.d.ts} +16 -21
- package/dist/src/connection/websocket/{WebsocketConnector.js → WebsocketServerConnector.js} +112 -160
- package/dist/src/connection/websocket/WebsocketServerConnector.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +4 -4
- package/dist/src/dht/DhtNode.js +85 -84
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +3 -3
- package/dist/src/dht/DhtNodeRpcLocal.js +9 -9
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +3 -3
- package/dist/src/dht/ExternalApiRpcLocal.js +5 -5
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.js +2 -2
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +4 -4
- package/dist/src/dht/PeerManager.js +22 -22
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +3 -3
- package/dist/src/dht/contact/SortedContactList.js +9 -9
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +3 -3
- package/dist/src/dht/discovery/DiscoverySession.js +21 -21
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +3 -3
- package/dist/src/dht/discovery/PeerDiscovery.js +46 -44
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/discovery/RingDiscoverySession.d.ts +3 -3
- package/dist/src/dht/discovery/RingDiscoverySession.js +19 -19
- package/dist/src/dht/discovery/RingDiscoverySession.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +33 -33
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js +8 -8
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +4 -4
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +24 -24
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +4 -4
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +5 -5
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +3 -3
- package/dist/src/dht/routing/Router.js +20 -20
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +3 -3
- package/dist/src/dht/routing/RouterRpcLocal.js +16 -16
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -3
- package/dist/src/dht/routing/RoutingSession.js +24 -24
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +3 -3
- package/dist/src/dht/store/StoreManager.js +25 -25
- package/dist/src/dht/store/StoreManager.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.d.ts +3 -3
- package/dist/src/dht/store/StoreRpcLocal.js +12 -12
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/exports.d.ts +3 -0
- package/dist/src/exports.js +5 -1
- package/dist/src/exports.js.map +1 -1
- package/dist/src/proto/google/protobuf/any.d.ts +5 -8
- package/dist/src/proto/google/protobuf/any.js.map +1 -1
- package/dist/src/proto/google/protobuf/empty.d.ts +1 -0
- package/dist/src/proto/google/protobuf/empty.js.map +1 -1
- package/dist/src/proto/google/protobuf/timestamp.d.ts +1 -10
- package/dist/src/proto/google/protobuf/timestamp.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +4 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +8 -8
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +3 -3
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +4 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +2 -2
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.d.ts +2 -2
- package/dist/src/transport/ListeningRpcCommunicator.js +2 -2
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -2
- package/dist/src/transport/RoutingRpcCommunicator.js +2 -2
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/package.json +7 -7
- package/protos/DhtRpc.proto +1 -1
- package/src/connection/ConnectionLockRpcLocal.ts +9 -9
- package/src/connection/ConnectionLockRpcRemote.ts +1 -1
- package/src/connection/ConnectionManager.ts +153 -111
- package/src/connection/ConnectorFacade.ts +84 -61
- package/src/connection/Handshaker.ts +131 -27
- package/src/connection/ManagedConnection.ts +41 -304
- package/src/connection/OutputBuffer.ts +28 -0
- package/src/connection/PendingConnection.ts +68 -0
- package/src/connection/connectivityChecker.ts +2 -2
- package/src/connection/connectivityRequestHandler.ts +1 -1
- package/src/connection/simulator/Simulator.ts +1 -5
- package/src/connection/simulator/SimulatorConnection.ts +1 -2
- package/src/connection/simulator/SimulatorConnector.ts +34 -33
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -6
- package/src/connection/webrtc/NodeWebrtcConnection.ts +3 -24
- package/src/connection/webrtc/WebrtcConnector.ts +73 -62
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +26 -56
- package/src/connection/websocket/AbstractWebsocketClientConnection.ts +8 -2
- package/src/connection/websocket/AutoCertifierClientFacade.ts +11 -11
- package/src/connection/websocket/NodeWebsocketClientConnection.ts +1 -1
- package/src/connection/websocket/WebsocketClientConnector.ts +119 -0
- package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +39 -0
- package/src/connection/websocket/{WebsocketConnectorRpcRemote.ts → WebsocketClientConnectorRpcRemote.ts} +2 -2
- package/src/connection/websocket/WebsocketServer.ts +18 -14
- package/src/connection/websocket/{WebsocketConnector.ts → WebsocketServerConnector.ts} +128 -205
- package/src/dht/DhtNode.ts +90 -89
- package/src/dht/DhtNodeRpcLocal.ts +11 -11
- package/src/dht/ExternalApiRpcLocal.ts +6 -6
- package/src/dht/ExternalApiRpcRemote.ts +2 -2
- package/src/dht/PeerManager.ts +24 -24
- package/src/dht/contact/SortedContactList.ts +10 -10
- package/src/dht/discovery/DiscoverySession.ts +24 -24
- package/src/dht/discovery/PeerDiscovery.ts +47 -45
- package/src/dht/discovery/RingDiscoverySession.ts +23 -23
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +36 -36
- package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +9 -9
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +25 -25
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +7 -7
- package/src/dht/routing/Router.ts +21 -21
- package/src/dht/routing/RouterRpcLocal.ts +17 -17
- package/src/dht/routing/RoutingSession.ts +26 -26
- package/src/dht/store/StoreManager.ts +27 -27
- package/src/dht/store/StoreRpcLocal.ts +13 -13
- package/src/exports.ts +3 -0
- package/src/proto/google/protobuf/any.ts +6 -9
- package/src/proto/google/protobuf/empty.ts +2 -1
- package/src/proto/google/protobuf/timestamp.ts +2 -11
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +9 -9
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +3 -3
- package/src/proto/packages/dht/protos/DhtRpc.ts +4 -4
- package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +1 -1
- package/src/transport/ListeningRpcCommunicator.ts +3 -3
- package/src/transport/RoutingRpcCommunicator.ts +3 -3
- package/test/end-to-end/Layer0Webrtc.test.ts +0 -10
- package/test/integration/ConnectionManager.test.ts +3 -2
- package/test/integration/GeoIpConnectivityChecking.test.ts +1 -1
- package/test/integration/SimultaneousConnections.test.ts +2 -2
- package/test/integration/WebrtcConnectionManagement.test.ts +2 -10
- package/test/integration/{WebsocketConnectorRpc.test.ts → WebsocketClientConnectorRpc.test.ts} +9 -9
- package/test/integration/WebsocketConnectionManagement.test.ts +11 -29
- package/test/unit/ConnectionManager.test.ts +64 -0
- package/test/unit/DiscoverySession.test.ts +1 -1
- package/test/unit/Handshaker.test.ts +169 -0
- package/test/unit/ManagedConnection.test.ts +58 -0
- package/test/unit/PendingConnection.test.ts +57 -0
- package/test/unit/WebrtcConnector.test.ts +56 -0
- package/test/unit/{WebsocketConnector.test.ts → WebsocketClientConnector.test.ts} +56 -11
- package/test/unit/WebsocketServerConnector.test.ts +102 -0
- package/test/utils/FakeConnectorFacade.ts +41 -0
- package/test/utils/mock/MockConnection.ts +26 -0
- package/test/utils/utils.ts +2 -2
- package/dist/src/connection/IConnectionSource.d.ts +0 -4
- package/dist/src/connection/IConnectionSource.js +0 -3
- package/dist/src/connection/IConnectionSource.js.map +0 -1
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.d.ts +0 -7
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.js +0 -20
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnector.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +0 -19
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +0 -23
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +0 -5
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +0 -1
- package/src/connection/IConnectionSource.ts +0 -6
- package/src/connection/webrtc/ManagedWebrtcConnection.ts +0 -27
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +0 -39
package/src/dht/PeerManager.ts
CHANGED
|
@@ -17,9 +17,9 @@ import { RingIdRaw, getRingIdRawFromPeerDescriptor } from './contact/ringIdentif
|
|
|
17
17
|
|
|
18
18
|
const logger = new Logger(module)
|
|
19
19
|
|
|
20
|
-
interface
|
|
20
|
+
interface PeerManagerOptions {
|
|
21
21
|
numberOfNodesPerKBucket: number
|
|
22
|
-
|
|
22
|
+
maxContactCount: number
|
|
23
23
|
localNodeId: DhtAddress
|
|
24
24
|
localPeerDescriptor: PeerDescriptor
|
|
25
25
|
connectionLocker?: ConnectionLocker
|
|
@@ -71,17 +71,17 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
71
71
|
private ringContacts: RingContactList<DhtNodeRpcRemote>
|
|
72
72
|
private randomContacts: RandomContactList<DhtNodeRpcRemote>
|
|
73
73
|
private stopped: boolean = false
|
|
74
|
-
private readonly
|
|
74
|
+
private readonly options: PeerManagerOptions
|
|
75
75
|
|
|
76
|
-
constructor(
|
|
76
|
+
constructor(options: PeerManagerOptions) {
|
|
77
77
|
super()
|
|
78
|
-
this.
|
|
78
|
+
this.options = options
|
|
79
79
|
this.neighbors = new KBucket<DhtNodeRpcRemote>({
|
|
80
|
-
localNodeId: getRawFromDhtAddress(this.
|
|
81
|
-
numberOfNodesPerKBucket: this.
|
|
82
|
-
numberOfNodesToPing: this.
|
|
80
|
+
localNodeId: getRawFromDhtAddress(this.options.localNodeId),
|
|
81
|
+
numberOfNodesPerKBucket: this.options.numberOfNodesPerKBucket,
|
|
82
|
+
numberOfNodesToPing: this.options.numberOfNodesPerKBucket
|
|
83
83
|
})
|
|
84
|
-
this.ringContacts = new RingContactList<DhtNodeRpcRemote>(getRingIdRawFromPeerDescriptor(this.
|
|
84
|
+
this.ringContacts = new RingContactList<DhtNodeRpcRemote>(getRingIdRawFromPeerDescriptor(this.options.localPeerDescriptor))
|
|
85
85
|
this.ringContacts.on('contactAdded', (contact: DhtNodeRpcRemote) => {
|
|
86
86
|
this.emit('ringContactAdded', contact.getPeerDescriptor())
|
|
87
87
|
})
|
|
@@ -95,8 +95,8 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
95
95
|
// TODO: Update contact info to the connection manager and reconnect
|
|
96
96
|
})
|
|
97
97
|
this.nearbyContacts = new SortedContactList({
|
|
98
|
-
referenceId: this.
|
|
99
|
-
maxSize: this.
|
|
98
|
+
referenceId: this.options.localNodeId,
|
|
99
|
+
maxSize: this.options.maxContactCount,
|
|
100
100
|
allowToContainReferenceId: false
|
|
101
101
|
})
|
|
102
102
|
this.nearbyContacts.on('contactRemoved', (contact: DhtNodeRpcRemote) => {
|
|
@@ -104,13 +104,13 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
104
104
|
return
|
|
105
105
|
}
|
|
106
106
|
this.emit('nearbyContactRemoved', contact.getPeerDescriptor())
|
|
107
|
-
this.randomContacts.addContact(this.
|
|
107
|
+
this.randomContacts.addContact(this.options.createDhtNodeRpcRemote(contact.getPeerDescriptor()))
|
|
108
108
|
})
|
|
109
109
|
this.nearbyContacts.on('contactAdded', (contact: DhtNodeRpcRemote) =>
|
|
110
110
|
this.emit('nearbyContactAdded', contact.getPeerDescriptor())
|
|
111
111
|
)
|
|
112
112
|
this.activeContacts = new Set()
|
|
113
|
-
this.randomContacts = new RandomContactList(this.
|
|
113
|
+
this.randomContacts = new RandomContactList(this.options.localNodeId, this.options.maxContactCount)
|
|
114
114
|
this.randomContacts.on('contactRemoved', (removedContact: DhtNodeRpcRemote) =>
|
|
115
115
|
this.emit('randomContactRemoved', removedContact.getPeerDescriptor())
|
|
116
116
|
)
|
|
@@ -124,12 +124,12 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
124
124
|
return
|
|
125
125
|
}
|
|
126
126
|
const sortingList: SortedContactList<DhtNodeRpcRemote> = new SortedContactList({
|
|
127
|
-
referenceId: this.
|
|
127
|
+
referenceId: this.options.localNodeId,
|
|
128
128
|
allowToContainReferenceId: false
|
|
129
129
|
})
|
|
130
130
|
sortingList.addContacts(oldContacts)
|
|
131
131
|
const removableNodeId = sortingList.getFurthestContacts(1)[0].getNodeId()
|
|
132
|
-
this.
|
|
132
|
+
this.options.connectionLocker?.weakUnlockConnection(removableNodeId, this.options.lockId)
|
|
133
133
|
this.neighbors.remove(getRawFromDhtAddress(removableNodeId))
|
|
134
134
|
this.neighbors.add(newContact)
|
|
135
135
|
}
|
|
@@ -138,7 +138,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
138
138
|
if (this.stopped) {
|
|
139
139
|
return
|
|
140
140
|
}
|
|
141
|
-
this.
|
|
141
|
+
this.options.connectionLocker?.weakUnlockConnection(nodeId, this.options.lockId)
|
|
142
142
|
logger.trace(`Removed contact ${nodeId}`)
|
|
143
143
|
if (this.neighbors.count() === 0) {
|
|
144
144
|
this.emit('kBucketEmpty')
|
|
@@ -149,12 +149,12 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
149
149
|
if (this.stopped) {
|
|
150
150
|
return
|
|
151
151
|
}
|
|
152
|
-
if (contact.getNodeId() !== this.
|
|
152
|
+
if (contact.getNodeId() !== this.options.localNodeId) {
|
|
153
153
|
const peerDescriptor = contact.getPeerDescriptor()
|
|
154
154
|
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
155
155
|
// Important to lock here, before the ping result is known
|
|
156
|
-
this.
|
|
157
|
-
if (this.
|
|
156
|
+
this.options.connectionLocker?.weakLockConnection(nodeId, this.options.lockId)
|
|
157
|
+
if (this.options.hasConnection(contact.getNodeId())) {
|
|
158
158
|
logger.trace(`Added new contact ${nodeId}`)
|
|
159
159
|
} else { // open connection by pinging
|
|
160
160
|
logger.trace('starting ping ' + nodeId)
|
|
@@ -163,12 +163,12 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
163
163
|
logger.trace(`Added new contact ${nodeId}`)
|
|
164
164
|
} else {
|
|
165
165
|
logger.trace('ping failed ' + nodeId)
|
|
166
|
-
this.
|
|
166
|
+
this.options.connectionLocker?.weakUnlockConnection(nodeId, this.options.lockId)
|
|
167
167
|
this.removeContact(nodeId)
|
|
168
168
|
this.addNearbyContactToNeighbors()
|
|
169
169
|
}
|
|
170
170
|
}).catch((_e) => {
|
|
171
|
-
this.
|
|
171
|
+
this.options.connectionLocker?.weakUnlockConnection(nodeId, this.options.lockId)
|
|
172
172
|
this.removeContact(nodeId)
|
|
173
173
|
this.addNearbyContactToNeighbors()
|
|
174
174
|
})
|
|
@@ -246,7 +246,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
246
246
|
): { left: DhtNodeRpcRemote[], right: DhtNodeRpcRemote[] } {
|
|
247
247
|
const closest = new RingContactList<DhtNodeRpcRemote>(ringIdRaw, excludedIds)
|
|
248
248
|
this.ringContacts.getAllContacts().map((contact) => closest.addContact(contact))
|
|
249
|
-
// TODO use
|
|
249
|
+
// TODO use options option or named constant?
|
|
250
250
|
return closest.getClosestContacts(limit ?? 8)
|
|
251
251
|
}
|
|
252
252
|
|
|
@@ -279,9 +279,9 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
279
279
|
return
|
|
280
280
|
}
|
|
281
281
|
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
282
|
-
if (nodeId !== this.
|
|
282
|
+
if (nodeId !== this.options.localNodeId) {
|
|
283
283
|
logger.trace(`Adding new contact ${nodeId}`)
|
|
284
|
-
const remote = this.
|
|
284
|
+
const remote = this.options.createDhtNodeRpcRemote(peerDescriptor)
|
|
285
285
|
const isInNeighbors = (this.neighbors.get(peerDescriptor.nodeId) !== null)
|
|
286
286
|
const isInNearbyContacts = (this.nearbyContacts.getContact(nodeId) !== undefined)
|
|
287
287
|
const isInRingContacts = this.ringContacts.getContact(peerDescriptor) !== undefined
|
|
@@ -8,7 +8,7 @@ import { DhtAddress, getRawFromDhtAddress } from '../../identifiers'
|
|
|
8
8
|
export type ReadonlySortedContactList<C extends { getNodeId: () => DhtAddress }> =
|
|
9
9
|
Pick<SortedContactList<C>, 'getClosestContacts' | 'getAllContactsInUndefinedOrder'>
|
|
10
10
|
|
|
11
|
-
export interface
|
|
11
|
+
export interface SortedContactListOptions {
|
|
12
12
|
referenceId: DhtAddress // all contacts in this list are in sorted by the distance to this ID
|
|
13
13
|
allowToContainReferenceId: boolean
|
|
14
14
|
maxSize?: number
|
|
@@ -20,15 +20,15 @@ export interface SortedContactListConfig {
|
|
|
20
20
|
|
|
21
21
|
export class SortedContactList<C extends { getNodeId: () => DhtAddress }> extends EventEmitter<Events<C>> {
|
|
22
22
|
|
|
23
|
-
private
|
|
23
|
+
private options: SortedContactListOptions
|
|
24
24
|
private contactsById: Map<DhtAddress, C> = new Map()
|
|
25
25
|
private contactIds: DhtAddress[] = []
|
|
26
26
|
|
|
27
27
|
constructor(
|
|
28
|
-
|
|
28
|
+
options: SortedContactListOptions
|
|
29
29
|
) {
|
|
30
30
|
super()
|
|
31
|
-
this.
|
|
31
|
+
this.options = options
|
|
32
32
|
this.compareIds = this.compareIds.bind(this)
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -42,15 +42,15 @@ export class SortedContactList<C extends { getNodeId: () => DhtAddress }> extend
|
|
|
42
42
|
|
|
43
43
|
public addContact(contact: C): void {
|
|
44
44
|
const contactId = contact.getNodeId()
|
|
45
|
-
if (this.
|
|
45
|
+
if (this.options.excludedNodeIds !== undefined && this.options.excludedNodeIds.has(contactId)) {
|
|
46
46
|
return
|
|
47
47
|
}
|
|
48
|
-
if ((!this.
|
|
49
|
-
(this.
|
|
48
|
+
if ((!this.options.allowToContainReferenceId && (this.options.referenceId === contactId)) ||
|
|
49
|
+
(this.options.nodeIdDistanceLimit !== undefined && this.compareIds(this.options.nodeIdDistanceLimit, contactId) < 0)) {
|
|
50
50
|
return
|
|
51
51
|
}
|
|
52
52
|
if (!this.contactsById.has(contactId)) {
|
|
53
|
-
if ((this.
|
|
53
|
+
if ((this.options.maxSize === undefined) || (this.contactIds.length < this.options.maxSize)) {
|
|
54
54
|
this.contactsById.set(contactId, contact)
|
|
55
55
|
const index = sortedIndexBy(this.contactIds, contactId, (id: DhtAddress) => { return this.distanceToReferenceId(id) })
|
|
56
56
|
this.contactIds.splice(index, 0, contactId)
|
|
@@ -60,7 +60,7 @@ export class SortedContactList<C extends { getNodeId: () => DhtAddress }> extend
|
|
|
60
60
|
contact
|
|
61
61
|
)
|
|
62
62
|
}
|
|
63
|
-
} else if (this.compareIds(this.contactIds[this.
|
|
63
|
+
} else if (this.compareIds(this.contactIds[this.options.maxSize - 1], contactId) > 0) {
|
|
64
64
|
const removedId = this.contactIds.pop()
|
|
65
65
|
const removedContact = this.contactsById.get(removedId!)!
|
|
66
66
|
this.contactsById.delete(removedId!)
|
|
@@ -120,7 +120,7 @@ export class SortedContactList<C extends { getNodeId: () => DhtAddress }> extend
|
|
|
120
120
|
// TODO inline this method?
|
|
121
121
|
private distanceToReferenceId(id: DhtAddress): number {
|
|
122
122
|
// TODO maybe this class should store the referenceId also as DhtAddressRaw so that we don't need to convert it here?
|
|
123
|
-
return getDistance(getRawFromDhtAddress(this.
|
|
123
|
+
return getDistance(getRawFromDhtAddress(this.options.referenceId), getRawFromDhtAddress(id))
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
public removeContact(id: DhtAddress): boolean {
|
|
@@ -8,7 +8,7 @@ import { getClosestNodes } from '../contact/getClosestNodes'
|
|
|
8
8
|
|
|
9
9
|
const logger = new Logger(module)
|
|
10
10
|
|
|
11
|
-
interface
|
|
11
|
+
interface DiscoverySessionOptions {
|
|
12
12
|
targetId: DhtAddress
|
|
13
13
|
parallelism: number
|
|
14
14
|
noProgressLimit: number
|
|
@@ -25,31 +25,31 @@ export class DiscoverySession {
|
|
|
25
25
|
private noProgressCounter = 0
|
|
26
26
|
private ongoingRequests: Set<DhtAddress> = new Set()
|
|
27
27
|
private doneGate = new Gate(false)
|
|
28
|
-
private readonly
|
|
28
|
+
private readonly options: DiscoverySessionOptions
|
|
29
29
|
|
|
30
|
-
constructor(
|
|
31
|
-
this.
|
|
30
|
+
constructor(options: DiscoverySessionOptions) {
|
|
31
|
+
this.options = options
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
private addContacts(contacts: PeerDescriptor[]): void {
|
|
35
|
-
if (this.
|
|
35
|
+
if (this.options.abortSignal.aborted || this.doneGate.isOpen()) {
|
|
36
36
|
return
|
|
37
37
|
}
|
|
38
38
|
for (const contact of contacts) {
|
|
39
|
-
this.
|
|
39
|
+
this.options.peerManager.addContact(contact)
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
private async fetchClosestNeighborsFromRemote(peerDescriptor: PeerDescriptor): Promise<PeerDescriptor[]> {
|
|
44
|
-
if (this.
|
|
44
|
+
if (this.options.abortSignal.aborted || this.doneGate.isOpen()) {
|
|
45
45
|
return []
|
|
46
46
|
}
|
|
47
47
|
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
48
48
|
logger.trace(`Getting closest neighbors from remote: ${nodeId}`)
|
|
49
|
-
this.
|
|
50
|
-
const remote = this.
|
|
51
|
-
const returnedContacts = await remote.getClosestPeers(this.
|
|
52
|
-
this.
|
|
49
|
+
this.options.contactedPeers.add(nodeId)
|
|
50
|
+
const remote = this.options.createDhtNodeRpcRemote(peerDescriptor)
|
|
51
|
+
const returnedContacts = await remote.getClosestPeers(this.options.targetId)
|
|
52
|
+
this.options.peerManager.setContactActive(nodeId)
|
|
53
53
|
return returnedContacts
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -58,7 +58,7 @@ export class DiscoverySession {
|
|
|
58
58
|
return
|
|
59
59
|
}
|
|
60
60
|
this.ongoingRequests.delete(nodeId)
|
|
61
|
-
const targetId = getRawFromDhtAddress(this.
|
|
61
|
+
const targetId = getRawFromDhtAddress(this.options.targetId)
|
|
62
62
|
const oldClosestNeighbor = this.getClosestNeighbor()
|
|
63
63
|
const oldClosestDistance = getDistance(targetId, oldClosestNeighbor.nodeId)
|
|
64
64
|
this.addContacts(contacts)
|
|
@@ -71,8 +71,8 @@ export class DiscoverySession {
|
|
|
71
71
|
|
|
72
72
|
private getClosestNeighbor(): PeerDescriptor {
|
|
73
73
|
return getClosestNodes(
|
|
74
|
-
this.
|
|
75
|
-
this.
|
|
74
|
+
this.options.targetId,
|
|
75
|
+
this.options.peerManager.getNeighbors().map((n) => n.getPeerDescriptor()),
|
|
76
76
|
{ maxCount: 1 }
|
|
77
77
|
)[0]
|
|
78
78
|
}
|
|
@@ -82,27 +82,27 @@ export class DiscoverySession {
|
|
|
82
82
|
return
|
|
83
83
|
}
|
|
84
84
|
this.ongoingRequests.delete(nodeId)
|
|
85
|
-
this.
|
|
85
|
+
this.options.peerManager.removeContact(nodeId)
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
private findMoreContacts(): void {
|
|
89
|
-
if (this.
|
|
89
|
+
if (this.options.abortSignal.aborted || this.doneGate.isOpen()) {
|
|
90
90
|
return
|
|
91
91
|
}
|
|
92
92
|
const uncontacted = getClosestNodes(
|
|
93
|
-
this.
|
|
94
|
-
Array.from(this.
|
|
93
|
+
this.options.targetId,
|
|
94
|
+
Array.from(this.options.peerManager.getNearbyContacts().getAllContactsInUndefinedOrder(), (c) => c.getPeerDescriptor()),
|
|
95
95
|
{
|
|
96
|
-
maxCount: this.
|
|
97
|
-
excludedNodeIds: this.
|
|
96
|
+
maxCount: this.options.parallelism,
|
|
97
|
+
excludedNodeIds: this.options.contactedPeers
|
|
98
98
|
}
|
|
99
99
|
)
|
|
100
|
-
if ((uncontacted.length === 0 && this.ongoingRequests.size === 0) || (this.noProgressCounter >= this.
|
|
100
|
+
if ((uncontacted.length === 0 && this.ongoingRequests.size === 0) || (this.noProgressCounter >= this.options.noProgressLimit)) {
|
|
101
101
|
this.doneGate.open()
|
|
102
102
|
return
|
|
103
103
|
}
|
|
104
104
|
for (const node of uncontacted) {
|
|
105
|
-
if (this.ongoingRequests.size >= this.
|
|
105
|
+
if (this.ongoingRequests.size >= this.options.parallelism) {
|
|
106
106
|
break
|
|
107
107
|
}
|
|
108
108
|
const nodeId = getNodeIdFromPeerDescriptor(node)
|
|
@@ -118,12 +118,12 @@ export class DiscoverySession {
|
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
public async findClosestNodes(timeout: number): Promise<void> {
|
|
121
|
-
if (this.
|
|
121
|
+
if (this.options.peerManager.getNearbyContactCount(this.options.contactedPeers) === 0) {
|
|
122
122
|
return
|
|
123
123
|
}
|
|
124
124
|
setImmediate(() => {
|
|
125
125
|
this.findMoreContacts()
|
|
126
126
|
})
|
|
127
|
-
await withTimeout(this.doneGate.waitUntilOpen(), timeout, 'discovery session timed out', this.
|
|
127
|
+
await withTimeout(this.doneGate.waitUntilOpen(), timeout, 'discovery session timed out', this.options.abortSignal)
|
|
128
128
|
}
|
|
129
129
|
}
|
|
@@ -16,8 +16,9 @@ import { getClosestNodes } from '../contact/getClosestNodes'
|
|
|
16
16
|
import { RingIdRaw, getRingIdRawFromPeerDescriptor } from '../contact/ringIdentifiers'
|
|
17
17
|
import { DiscoverySession } from './DiscoverySession'
|
|
18
18
|
import { RingDiscoverySession } from './RingDiscoverySession'
|
|
19
|
+
import { CONTROL_LAYER_NODE_SERVICE_ID } from '../DhtNode'
|
|
19
20
|
|
|
20
|
-
interface
|
|
21
|
+
interface PeerDiscoveryOptions {
|
|
21
22
|
localPeerDescriptor: PeerDescriptor
|
|
22
23
|
joinNoProgressLimit: number
|
|
23
24
|
serviceId: ServiceID
|
|
@@ -45,10 +46,10 @@ export class PeerDiscovery {
|
|
|
45
46
|
private rejoinOngoing = false
|
|
46
47
|
private joinCalled = false
|
|
47
48
|
private recoveryIntervalStarted = false
|
|
48
|
-
private readonly
|
|
49
|
+
private readonly options: PeerDiscoveryOptions
|
|
49
50
|
|
|
50
|
-
constructor(
|
|
51
|
-
this.
|
|
51
|
+
constructor(options: PeerDiscoveryOptions) {
|
|
52
|
+
this.options = options
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
async joinDht(
|
|
@@ -57,12 +58,12 @@ export class PeerDiscovery {
|
|
|
57
58
|
retry = true
|
|
58
59
|
): Promise<void> {
|
|
59
60
|
const contactedPeers = new Set<DhtAddress>()
|
|
60
|
-
const
|
|
61
|
+
const distantJoinOptions = doAdditionalDistantPeerDiscovery
|
|
61
62
|
? { enabled: true, contactedPeers: new Set<DhtAddress>() } : { enabled: false } as const
|
|
62
63
|
await Promise.all(entryPoints.map((entryPoint) => this.joinThroughEntryPoint(
|
|
63
64
|
entryPoint,
|
|
64
65
|
contactedPeers,
|
|
65
|
-
|
|
66
|
+
distantJoinOptions,
|
|
66
67
|
retry
|
|
67
68
|
)))
|
|
68
69
|
}
|
|
@@ -79,39 +80,40 @@ export class PeerDiscovery {
|
|
|
79
80
|
}
|
|
80
81
|
this.joinCalled = true
|
|
81
82
|
logger.debug(
|
|
82
|
-
`Joining ${this.
|
|
83
|
+
`Joining ${this.options.serviceId === CONTROL_LAYER_NODE_SERVICE_ID
|
|
84
|
+
? 'The Streamr Network' : `Control Layer for ${this.options.serviceId}`}`
|
|
83
85
|
+ ` via entrypoint ${getNodeIdFromPeerDescriptor(entryPointDescriptor)}`
|
|
84
86
|
)
|
|
85
|
-
if (areEqualPeerDescriptors(entryPointDescriptor, this.
|
|
87
|
+
if (areEqualPeerDescriptors(entryPointDescriptor, this.options.localPeerDescriptor)) {
|
|
86
88
|
return
|
|
87
89
|
}
|
|
88
|
-
this.
|
|
89
|
-
this.
|
|
90
|
-
const targetId = getNodeIdFromPeerDescriptor(this.
|
|
90
|
+
this.options.connectionLocker?.lockConnection(entryPointDescriptor, `${this.options.serviceId}::joinDht`)
|
|
91
|
+
this.options.peerManager.addContact(entryPointDescriptor)
|
|
92
|
+
const targetId = getNodeIdFromPeerDescriptor(this.options.localPeerDescriptor)
|
|
91
93
|
const sessions = [this.createSession(targetId, contactedPeers)]
|
|
92
94
|
if (additionalDistantJoin.enabled) {
|
|
93
95
|
sessions.push(this.createSession(createDistantDhtAddress(targetId), additionalDistantJoin.contactedPeers))
|
|
94
96
|
}
|
|
95
97
|
await this.runSessions(sessions, entryPointDescriptor, retry)
|
|
96
|
-
this.
|
|
98
|
+
this.options.connectionLocker?.unlockConnection(entryPointDescriptor, `${this.options.serviceId}::joinDht`)
|
|
97
99
|
|
|
98
100
|
}
|
|
99
101
|
|
|
100
102
|
async joinRing(): Promise<void> {
|
|
101
103
|
const contactedPeers = new Set<DhtAddress>()
|
|
102
|
-
const sessions = [this.createRingSession(getRingIdRawFromPeerDescriptor(this.
|
|
104
|
+
const sessions = [this.createRingSession(getRingIdRawFromPeerDescriptor(this.options.localPeerDescriptor), contactedPeers)]
|
|
103
105
|
await this.runRingSessions(sessions)
|
|
104
106
|
}
|
|
105
107
|
|
|
106
108
|
private createSession(targetId: DhtAddress, contactedPeers: Set<DhtAddress>): DiscoverySession {
|
|
107
109
|
const sessionOptions = {
|
|
108
110
|
targetId,
|
|
109
|
-
parallelism: this.
|
|
110
|
-
noProgressLimit: this.
|
|
111
|
-
peerManager: this.
|
|
111
|
+
parallelism: this.options.parallelism,
|
|
112
|
+
noProgressLimit: this.options.joinNoProgressLimit,
|
|
113
|
+
peerManager: this.options.peerManager,
|
|
112
114
|
contactedPeers,
|
|
113
|
-
abortSignal: this.
|
|
114
|
-
createDhtNodeRpcRemote: this.
|
|
115
|
+
abortSignal: this.options.abortSignal,
|
|
116
|
+
createDhtNodeRpcRemote: this.options.createDhtNodeRpcRemote
|
|
115
117
|
}
|
|
116
118
|
return new DiscoverySession(sessionOptions)
|
|
117
119
|
}
|
|
@@ -119,12 +121,12 @@ export class PeerDiscovery {
|
|
|
119
121
|
private createRingSession(targetId: RingIdRaw, contactedPeers: Set<DhtAddress>): RingDiscoverySession {
|
|
120
122
|
const sessionOptions = {
|
|
121
123
|
targetId,
|
|
122
|
-
parallelism: this.
|
|
123
|
-
noProgressLimit: this.
|
|
124
|
-
peerManager: this.
|
|
124
|
+
parallelism: this.options.parallelism,
|
|
125
|
+
noProgressLimit: this.options.joinNoProgressLimit,
|
|
126
|
+
peerManager: this.options.peerManager,
|
|
125
127
|
contactedPeers,
|
|
126
|
-
abortSignal: this.
|
|
127
|
-
createDhtNodeRpcRemote: this.
|
|
128
|
+
abortSignal: this.options.abortSignal,
|
|
129
|
+
createDhtNodeRpcRemote: this.options.createDhtNodeRpcRemote
|
|
128
130
|
}
|
|
129
131
|
return new RingDiscoverySession(sessionOptions)
|
|
130
132
|
}
|
|
@@ -133,17 +135,17 @@ export class PeerDiscovery {
|
|
|
133
135
|
try {
|
|
134
136
|
for (const session of sessions) {
|
|
135
137
|
this.ongoingDiscoverySessions.set(session.id, session)
|
|
136
|
-
await session.findClosestNodes(this.
|
|
138
|
+
await session.findClosestNodes(this.options.joinTimeout)
|
|
137
139
|
}
|
|
138
140
|
} catch (_e) {
|
|
139
|
-
logger.debug(`DHT join on ${this.
|
|
141
|
+
logger.debug(`DHT join on ${this.options.serviceId} timed out`)
|
|
140
142
|
} finally {
|
|
141
143
|
if (!this.isStopped()) {
|
|
142
|
-
if (this.
|
|
144
|
+
if (this.options.peerManager.getNeighborCount() === 0) {
|
|
143
145
|
if (retry) {
|
|
144
146
|
// TODO should we catch possible promise rejection?
|
|
145
|
-
// TODO use
|
|
146
|
-
setAbortableTimeout(() => this.rejoinDht(entryPointDescriptor), 1000, this.
|
|
147
|
+
// TODO use options option or named constant?
|
|
148
|
+
setAbortableTimeout(() => this.rejoinDht(entryPointDescriptor), 1000, this.options.abortSignal)
|
|
147
149
|
}
|
|
148
150
|
} else {
|
|
149
151
|
await this.ensureRecoveryIntervalIsRunning()
|
|
@@ -157,10 +159,10 @@ export class PeerDiscovery {
|
|
|
157
159
|
try {
|
|
158
160
|
for (const session of sessions) {
|
|
159
161
|
this.ongoingRingDiscoverySessions.set(session.id, session)
|
|
160
|
-
await session.findClosestNodes(this.
|
|
162
|
+
await session.findClosestNodes(this.options.joinTimeout)
|
|
161
163
|
}
|
|
162
164
|
} catch (_e) {
|
|
163
|
-
logger.debug(`Ring join on ${this.
|
|
165
|
+
logger.debug(`Ring join on ${this.options.serviceId} timed out`)
|
|
164
166
|
} finally {
|
|
165
167
|
sessions.forEach((session) => this.ongoingDiscoverySessions.delete(session.id))
|
|
166
168
|
}
|
|
@@ -174,17 +176,17 @@ export class PeerDiscovery {
|
|
|
174
176
|
if (this.isStopped() || this.rejoinOngoing) {
|
|
175
177
|
return
|
|
176
178
|
}
|
|
177
|
-
logger.debug(`Rejoining DHT ${this.
|
|
179
|
+
logger.debug(`Rejoining DHT ${this.options.serviceId}`)
|
|
178
180
|
this.rejoinOngoing = true
|
|
179
181
|
try {
|
|
180
182
|
await this.joinThroughEntryPoint(entryPoint, contactedPeers, { enabled: true, contactedPeers: distantJoinContactPeers })
|
|
181
|
-
logger.debug(`Rejoined DHT successfully ${this.
|
|
183
|
+
logger.debug(`Rejoined DHT successfully ${this.options.serviceId}!`)
|
|
182
184
|
} catch (err) {
|
|
183
|
-
logger.warn(`Rejoining DHT ${this.
|
|
185
|
+
logger.warn(`Rejoining DHT ${this.options.serviceId} failed`)
|
|
184
186
|
if (!this.isStopped()) {
|
|
185
187
|
// TODO should we catch possible promise rejection?
|
|
186
|
-
// TODO use
|
|
187
|
-
setAbortableTimeout(() => this.rejoinDht(entryPoint), 5000, this.
|
|
188
|
+
// TODO use options option or named constant?
|
|
189
|
+
setAbortableTimeout(() => this.rejoinDht(entryPoint), 5000, this.options.abortSignal)
|
|
188
190
|
}
|
|
189
191
|
} finally {
|
|
190
192
|
this.rejoinOngoing = false
|
|
@@ -194,8 +196,8 @@ export class PeerDiscovery {
|
|
|
194
196
|
private async ensureRecoveryIntervalIsRunning(): Promise<void> {
|
|
195
197
|
if (!this.recoveryIntervalStarted) {
|
|
196
198
|
this.recoveryIntervalStarted = true
|
|
197
|
-
// TODO use
|
|
198
|
-
await scheduleAtInterval(() => this.fetchClosestAndRandomNeighbors(), 60000, true, this.
|
|
199
|
+
// TODO use options option or named constant?
|
|
200
|
+
await scheduleAtInterval(() => this.fetchClosestAndRandomNeighbors(), 60000, true, this.options.abortSignal)
|
|
199
201
|
}
|
|
200
202
|
}
|
|
201
203
|
|
|
@@ -203,29 +205,29 @@ export class PeerDiscovery {
|
|
|
203
205
|
if (this.isStopped()) {
|
|
204
206
|
return
|
|
205
207
|
}
|
|
206
|
-
const localNodeId = getNodeIdFromPeerDescriptor(this.
|
|
207
|
-
const nodes = this.getClosestNeighbors(localNodeId, this.
|
|
208
|
+
const localNodeId = getNodeIdFromPeerDescriptor(this.options.localPeerDescriptor)
|
|
209
|
+
const nodes = this.getClosestNeighbors(localNodeId, this.options.parallelism)
|
|
208
210
|
const randomNodes = this.getClosestNeighbors(createRandomDhtAddress(), 1)
|
|
209
211
|
await Promise.allSettled([
|
|
210
212
|
...nodes.map(async (node: PeerDescriptor) => {
|
|
211
|
-
const remote = this.
|
|
213
|
+
const remote = this.options.createDhtNodeRpcRemote(node)
|
|
212
214
|
const contacts = await remote.getClosestPeers(localNodeId)
|
|
213
215
|
for (const contact of contacts) {
|
|
214
|
-
this.
|
|
216
|
+
this.options.peerManager.addContact(contact)
|
|
215
217
|
}
|
|
216
218
|
}),
|
|
217
219
|
...randomNodes.map(async (node: PeerDescriptor) => {
|
|
218
|
-
const remote = this.
|
|
220
|
+
const remote = this.options.createDhtNodeRpcRemote(node)
|
|
219
221
|
const contacts = await remote.getClosestPeers(createRandomDhtAddress())
|
|
220
222
|
for (const contact of contacts) {
|
|
221
|
-
this.
|
|
223
|
+
this.options.peerManager.addContact(contact)
|
|
222
224
|
}
|
|
223
225
|
})
|
|
224
226
|
])
|
|
225
227
|
}
|
|
226
228
|
|
|
227
229
|
private getClosestNeighbors(referenceId: DhtAddress, maxCount: number): PeerDescriptor[] {
|
|
228
|
-
return getClosestNodes(referenceId, this.
|
|
230
|
+
return getClosestNodes(referenceId, this.options.peerManager.getNeighbors().map((n) => n.getPeerDescriptor()), { maxCount })
|
|
229
231
|
}
|
|
230
232
|
|
|
231
233
|
public isJoinOngoing(): boolean {
|
|
@@ -237,6 +239,6 @@ export class PeerDiscovery {
|
|
|
237
239
|
}
|
|
238
240
|
|
|
239
241
|
private isStopped() {
|
|
240
|
-
return this.
|
|
242
|
+
return this.options.abortSignal.aborted
|
|
241
243
|
}
|
|
242
244
|
}
|