@streamr/dht 100.0.0-pretestnet.6 → 100.0.0-testnet-one.1
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.js +2 -2
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +2 -2
- package/dist/src/connection/ConnectionLockRpcRemote.js +3 -27
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +0 -1
- package/dist/src/connection/ConnectionManager.js +11 -7
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +2 -2
- package/dist/src/connection/ConnectorFacade.js +1 -2
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/ManagedConnection.js +1 -0
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/connectivityChecker.d.ts +9 -0
- package/dist/src/connection/connectivityChecker.js +122 -0
- package/dist/src/connection/connectivityChecker.js.map +1 -0
- package/dist/src/connection/connectivityRequestHandler.d.ts +2 -0
- package/dist/src/connection/connectivityRequestHandler.js +79 -0
- package/dist/src/connection/connectivityRequestHandler.js.map +1 -0
- package/dist/src/connection/simulator/Simulator.js +3 -2
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js.map +1 -1
- package/dist/src/connection/websocket/ClientWebsocket.d.ts +1 -0
- package/dist/src/connection/websocket/ClientWebsocket.js +7 -3
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/ServerWebsocket.d.ts +4 -0
- package/dist/src/connection/websocket/ServerWebsocket.js +32 -21
- package/dist/src/connection/websocket/ServerWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +0 -2
- package/dist/src/connection/websocket/WebsocketConnector.js +61 -16
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +8 -11
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +4 -4
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +5 -39
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketServer.js +21 -4
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +13 -23
- package/dist/src/dht/DhtNode.js +97 -226
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +1 -4
- package/dist/src/dht/DhtNodeRpcLocal.js +1 -5
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +3 -3
- package/dist/src/dht/DhtNodeRpcRemote.js +4 -4
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +4 -4
- package/dist/src/dht/ExternalApiRpcLocal.js +5 -12
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +3 -3
- package/dist/src/dht/ExternalApiRpcRemote.js +5 -5
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +52 -0
- package/dist/src/dht/PeerManager.js +273 -0
- package/dist/src/dht/PeerManager.js.map +1 -0
- package/dist/src/dht/contact/ContactList.d.ts +1 -1
- package/dist/src/dht/contact/ContactList.js +1 -0
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/{Remote.d.ts → RpcRemote.d.ts} +3 -3
- package/dist/src/dht/contact/{Remote.js → RpcRemote.js} +8 -8
- package/dist/src/dht/contact/RpcRemote.js.map +1 -0
- package/dist/src/dht/contact/SortedContactList.d.ts +20 -6
- package/dist/src/dht/contact/SortedContactList.js +55 -24
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +4 -14
- package/dist/src/dht/discovery/DiscoverySession.js +15 -26
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +2 -9
- package/dist/src/dht/discovery/PeerDiscovery.js +11 -19
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/find/FindRpcLocal.js +2 -1
- package/dist/src/dht/find/FindRpcLocal.js.map +1 -1
- package/dist/src/dht/find/FindSession.d.ts +6 -6
- package/dist/src/dht/find/FindSession.js +18 -13
- package/dist/src/dht/find/FindSession.js.map +1 -1
- package/dist/src/dht/find/FindSessionRpcLocal.d.ts +1 -1
- package/dist/src/dht/find/FindSessionRpcRemote.d.ts +2 -2
- package/dist/src/dht/find/FindSessionRpcRemote.js +2 -2
- package/dist/src/dht/find/FindSessionRpcRemote.js.map +1 -1
- package/dist/src/dht/find/Finder.d.ts +4 -4
- package/dist/src/dht/find/Finder.js +55 -42
- package/dist/src/dht/find/Finder.js.map +1 -1
- package/dist/src/dht/routing/FindRpcRemote.d.ts +2 -2
- package/dist/src/dht/routing/FindRpcRemote.js +7 -5
- package/dist/src/dht/routing/FindRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +3 -7
- package/dist/src/dht/routing/Router.js +29 -22
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +2 -2
- package/dist/src/dht/routing/RouterRpcLocal.js +4 -3
- 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 +13 -8
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +1 -1
- package/dist/src/dht/routing/RoutingSession.js +23 -11
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/routing/getPreviousPeer.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +3 -3
- package/dist/src/dht/store/LocalDataStore.js +18 -17
- package/dist/src/dht/store/LocalDataStore.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.d.ts +10 -9
- package/dist/src/dht/store/StoreRpcLocal.js +108 -102
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.d.ts +4 -5
- package/dist/src/dht/store/StoreRpcRemote.js +6 -15
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/exports.d.ts +1 -1
- package/dist/src/exports.js +4 -4
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/PeerID.d.ts +1 -0
- package/dist/src/helpers/PeerID.js +9 -4
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/UUID.js +1 -1
- package/dist/src/helpers/UUID.js.map +1 -1
- package/dist/src/helpers/nodeId.d.ts +1 -0
- package/dist/src/helpers/{kademliaId.js → nodeId.js} +4 -4
- package/dist/src/helpers/nodeId.js.map +1 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +4 -4
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +5 -16
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +2 -9
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +80 -95
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +67 -66
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +3 -10
- package/dist/src/transport/RoutingRpcCommunicator.js +2 -0
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/karma.config.js +2 -2
- package/package.json +5 -5
- package/protos/DhtRpc.proto +36 -36
- package/src/connection/ConnectionLockHandler.ts +2 -2
- package/src/connection/ConnectionLockRpcRemote.ts +3 -4
- package/src/connection/ConnectionManager.ts +19 -17
- package/src/connection/ConnectorFacade.ts +5 -7
- package/src/connection/ManagedConnection.ts +1 -0
- package/src/connection/connectivityChecker.ts +102 -0
- package/src/connection/connectivityRequestHandler.ts +79 -0
- package/src/connection/simulator/Simulator.ts +3 -2
- package/src/connection/simulator/SimulatorConnection.ts +1 -1
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +10 -10
- package/src/connection/webrtc/NodeWebrtcConnection.ts +1 -1
- package/src/connection/webrtc/WebrtcConnector.ts +1 -1
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +1 -1
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +2 -2
- package/src/connection/webrtc/iceServerAsString.ts +1 -1
- package/src/connection/websocket/ClientWebsocket.ts +6 -2
- package/src/connection/websocket/ServerWebsocket.ts +40 -25
- package/src/connection/websocket/WebsocketConnector.ts +43 -22
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +9 -11
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +7 -16
- package/src/connection/websocket/WebsocketServer.ts +20 -5
- package/src/dht/DhtNode.ts +123 -280
- package/src/dht/DhtNodeRpcLocal.ts +2 -9
- package/src/dht/DhtNodeRpcRemote.ts +4 -4
- package/src/dht/ExternalApiRpcLocal.ts +8 -13
- package/src/dht/ExternalApiRpcRemote.ts +5 -5
- package/src/dht/PeerManager.ts +330 -0
- package/src/dht/contact/ContactList.ts +3 -2
- package/src/dht/contact/{Remote.ts → RpcRemote.ts} +7 -6
- package/src/dht/contact/SortedContactList.ts +87 -44
- package/src/dht/discovery/DiscoverySession.ts +19 -44
- package/src/dht/discovery/PeerDiscovery.ts +16 -28
- package/src/dht/find/FindRpcLocal.ts +2 -2
- package/src/dht/find/FindSession.ts +25 -20
- package/src/dht/find/FindSessionRpcLocal.ts +1 -1
- package/src/dht/find/FindSessionRpcRemote.ts +2 -2
- package/src/dht/find/Finder.ts +84 -64
- package/src/dht/routing/FindRpcRemote.ts +7 -5
- package/src/dht/routing/Router.ts +30 -25
- package/src/dht/routing/RouterRpcLocal.ts +5 -5
- package/src/dht/routing/RouterRpcRemote.ts +13 -10
- package/src/dht/routing/RoutingSession.ts +22 -17
- package/src/dht/routing/getPreviousPeer.ts +1 -1
- package/src/dht/store/LocalDataStore.ts +18 -17
- package/src/dht/store/StoreRpcLocal.ts +118 -113
- package/src/dht/store/StoreRpcRemote.ts +7 -23
- package/src/exports.ts +1 -1
- package/src/helpers/PeerID.ts +8 -4
- package/src/helpers/UUID.ts +1 -1
- package/src/helpers/{kademliaId.ts → nodeId.ts} +1 -1
- package/src/helpers/peerIdFromPeerDescriptor.ts +6 -6
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +6 -20
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +3 -10
- package/src/proto/packages/dht/protos/DhtRpc.ts +103 -135
- package/src/transport/RoutingRpcCommunicator.ts +2 -0
- package/test/benchmark/Find.test.ts +5 -5
- package/test/benchmark/KademliaCorrectness.test.ts +3 -3
- package/test/benchmark/SortedContactListBenchmark.test.ts +150 -0
- package/test/benchmark/WebsocketServerMemoryLeak.test.ts +41 -0
- package/test/benchmark/kademlia-simulation/Contact.ts +1 -1
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +1 -1
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +6 -1
- package/test/end-to-end/Layer0-Layer1.test.ts +1 -1
- package/test/end-to-end/Layer0.test.ts +4 -4
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +11 -11
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +6 -6
- package/test/end-to-end/Layer0Webrtc.test.ts +2 -2
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +3 -3
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +3 -3
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +1 -1
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +1 -1
- package/test/end-to-end/memory-leak.test.ts +9 -12
- package/test/integration/ConnectionLocking.test.ts +2 -2
- package/test/integration/ConnectionManager.test.ts +14 -14
- package/test/integration/DhtJoinPeerDiscovery.test.ts +3 -3
- package/test/integration/DhtNodeExternalAPI.test.ts +10 -7
- package/test/integration/DhtNodeRpcRemote.test.ts +4 -4
- package/test/integration/DhtRpc.test.ts +6 -6
- package/test/integration/Find.test.ts +3 -3
- package/test/integration/Layer1-scale.test.ts +3 -3
- package/test/integration/Mock-Layer1-Layer0.test.ts +16 -16
- package/test/integration/MultipleEntryPointJoining.test.ts +7 -7
- package/test/integration/{MigrateData.test.ts → ReplicateData.test.ts} +15 -10
- package/test/integration/RouteMessage.test.ts +2 -2
- package/test/integration/RouterRpcRemote.test.ts +2 -2
- package/test/integration/RpcErrors.test.ts +2 -2
- package/test/integration/ScaleDownDht.test.ts +4 -2
- package/test/integration/SimultaneousConnections.test.ts +89 -57
- package/test/integration/Store.test.ts +33 -13
- package/test/integration/StoreAndDelete.test.ts +19 -17
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +21 -21
- package/test/integration/StoreRpcRemote.test.ts +3 -3
- package/test/integration/WebrtcConnectionManagement.test.ts +2 -2
- package/test/integration/WebrtcConnectorRpc.test.ts +1 -1
- package/test/integration/WebsocketConnectionManagement.test.ts +41 -3
- package/test/integration/WebsocketConnectorRpc.test.ts +5 -7
- package/test/unit/ConnectivityHelpers.test.ts +4 -4
- package/test/unit/Finder.test.ts +69 -23
- package/test/unit/LocalDataStore.test.ts +60 -43
- package/test/unit/RandomContactList.test.ts +2 -2
- package/test/unit/Router.test.ts +19 -11
- package/test/unit/RoutingSession.test.ts +76 -0
- package/test/unit/SortedContactList.test.ts +17 -12
- package/test/unit/WebsocketConnector.test.ts +1 -1
- package/test/unit/connectivityRequestHandler.test.ts +71 -0
- package/test/utils/mock/Router.ts +1 -1
- package/test/utils/utils.ts +24 -22
- package/dist/src/connection/ConnectivityChecker.d.ts +0 -17
- package/dist/src/connection/ConnectivityChecker.js +0 -208
- package/dist/src/connection/ConnectivityChecker.js.map +0 -1
- package/dist/src/dht/contact/Remote.js.map +0 -1
- package/dist/src/helpers/kademliaId.d.ts +0 -1
- package/dist/src/helpers/kademliaId.js.map +0 -1
- package/src/connection/ConnectivityChecker.ts +0 -199
|
@@ -1,26 +1,34 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
2
|
+
import { ContactState, Events } from './ContactList'
|
|
3
|
+
import { sortedIndexBy } from 'lodash'
|
|
4
|
+
import EventEmitter from 'eventemitter3'
|
|
5
|
+
import { getDistance } from '../PeerManager'
|
|
6
|
+
|
|
7
|
+
export interface SortedContactListConfig {
|
|
8
|
+
referenceId: PeerID // all contacts in this list are in sorted by the distance to this ID
|
|
9
|
+
allowToContainReferenceId: boolean
|
|
10
|
+
// TODO could maybe optimize this by removing the flag and then we'd check whether we have
|
|
11
|
+
// any listeners before we emit the event
|
|
12
|
+
emitEvents: boolean
|
|
13
|
+
maxSize?: number
|
|
14
|
+
// if set, the list can't contain any contacts which are futher away than this limit
|
|
15
|
+
peerIdDistanceLimit?: PeerID
|
|
16
|
+
// if set, the list can't contain contacts with these ids
|
|
17
|
+
excludedPeerIDs?: PeerID[]
|
|
18
|
+
}
|
|
4
19
|
|
|
5
|
-
export class SortedContactList<C extends { getPeerId: () => PeerID }> extends
|
|
20
|
+
export class SortedContactList<C extends { getPeerId: () => PeerID }> extends EventEmitter<Events<C>> {
|
|
6
21
|
|
|
7
|
-
private
|
|
8
|
-
private
|
|
9
|
-
private
|
|
22
|
+
private config: SortedContactListConfig
|
|
23
|
+
private contactsById: Map<PeerIDKey, ContactState<C>> = new Map()
|
|
24
|
+
private contactIds: PeerID[] = []
|
|
10
25
|
|
|
11
26
|
constructor(
|
|
12
|
-
|
|
13
|
-
maxSize: number,
|
|
14
|
-
defaultContactQueryLimit?: number,
|
|
15
|
-
allowLocalPeerId = false,
|
|
16
|
-
peerIdDistanceLimit?: PeerID,
|
|
17
|
-
excludedPeerIDs?: PeerID[]
|
|
27
|
+
config: SortedContactListConfig
|
|
18
28
|
) {
|
|
19
|
-
super(
|
|
29
|
+
super()
|
|
30
|
+
this.config = config
|
|
20
31
|
this.compareIds = this.compareIds.bind(this)
|
|
21
|
-
this.allowLocalPeerId = allowLocalPeerId
|
|
22
|
-
this.peerIdDistanceLimit = peerIdDistanceLimit
|
|
23
|
-
this.excludedPeerIDs = excludedPeerIDs
|
|
24
32
|
}
|
|
25
33
|
|
|
26
34
|
public getClosestContactId(): PeerID {
|
|
@@ -32,38 +40,44 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
32
40
|
}
|
|
33
41
|
|
|
34
42
|
public addContact(contact: C): void {
|
|
35
|
-
if (this.excludedPeerIDs
|
|
36
|
-
&& this.excludedPeerIDs.some((peerId) => contact.getPeerId().equals(peerId))) {
|
|
43
|
+
if (this.config.excludedPeerIDs !== undefined
|
|
44
|
+
&& this.config.excludedPeerIDs.some((peerId) => contact.getPeerId().equals(peerId))) {
|
|
37
45
|
return
|
|
38
46
|
}
|
|
39
|
-
|
|
40
|
-
if ((!this.
|
|
41
|
-
(this.peerIdDistanceLimit !== undefined && this.compareIds(this.peerIdDistanceLimit, contact.getPeerId()) < 0)) {
|
|
47
|
+
|
|
48
|
+
if ((!this.config.allowToContainReferenceId && this.config.referenceId.equals(contact.getPeerId())) ||
|
|
49
|
+
(this.config.peerIdDistanceLimit !== undefined && this.compareIds(this.config.peerIdDistanceLimit, contact.getPeerId()) < 0)) {
|
|
42
50
|
return
|
|
43
51
|
}
|
|
44
52
|
if (!this.contactsById.has(contact.getPeerId().toKey())) {
|
|
45
|
-
if (this.contactIds.length < this.maxSize) {
|
|
53
|
+
if ((this.config.maxSize === undefined) || (this.contactIds.length < this.config.maxSize)) {
|
|
46
54
|
this.contactsById.set(contact.getPeerId().toKey(), new ContactState(contact))
|
|
47
|
-
|
|
48
|
-
this.contactIds.
|
|
49
|
-
|
|
55
|
+
|
|
56
|
+
const index = sortedIndexBy(this.contactIds, contact.getPeerId(), (id: PeerID) => { return this.distanceToReferenceId(id) })
|
|
57
|
+
this.contactIds.splice(index, 0, contact.getPeerId())
|
|
58
|
+
} else if (this.compareIds(this.contactIds[this.config.maxSize - 1], contact.getPeerId()) > 0) {
|
|
50
59
|
const removedId = this.contactIds.pop()
|
|
51
60
|
const removedContact = this.contactsById.get(removedId!.toKey())!.contact
|
|
52
61
|
this.contactsById.delete(removedId!.toKey())
|
|
53
62
|
this.contactsById.set(contact.getPeerId().toKey(), new ContactState(contact))
|
|
54
|
-
|
|
55
|
-
this.contactIds.
|
|
63
|
+
|
|
64
|
+
const index = sortedIndexBy(this.contactIds, contact.getPeerId(), (id: PeerID) => { return this.distanceToReferenceId(id) })
|
|
65
|
+
this.contactIds.splice(index, 0, contact.getPeerId())
|
|
66
|
+
if (this.config.emitEvents) {
|
|
67
|
+
this.emit(
|
|
68
|
+
'contactRemoved',
|
|
69
|
+
removedContact,
|
|
70
|
+
this.getClosestContacts()
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (this.config.emitEvents) {
|
|
56
75
|
this.emit(
|
|
57
|
-
'
|
|
58
|
-
|
|
76
|
+
'newContact',
|
|
77
|
+
contact,
|
|
59
78
|
this.getClosestContacts()
|
|
60
79
|
)
|
|
61
80
|
}
|
|
62
|
-
this.emit(
|
|
63
|
-
'newContact',
|
|
64
|
-
contact,
|
|
65
|
-
this.getClosestContacts()
|
|
66
|
-
)
|
|
67
81
|
}
|
|
68
82
|
}
|
|
69
83
|
|
|
@@ -71,6 +85,10 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
71
85
|
contacts.forEach((contact) => this.addContact(contact))
|
|
72
86
|
}
|
|
73
87
|
|
|
88
|
+
public getContact(id: PeerID): ContactState<C> | undefined {
|
|
89
|
+
return this.contactsById.get(id.toKey())
|
|
90
|
+
}
|
|
91
|
+
|
|
74
92
|
public setContacted(contactId: PeerID): void {
|
|
75
93
|
if (this.contactsById.has(contactId.toKey())) {
|
|
76
94
|
this.contactsById.get(contactId.toKey())!.contacted = true
|
|
@@ -83,7 +101,7 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
83
101
|
}
|
|
84
102
|
}
|
|
85
103
|
|
|
86
|
-
public getClosestContacts(limit
|
|
104
|
+
public getClosestContacts(limit?: number): C[] {
|
|
87
105
|
const ret: C[] = []
|
|
88
106
|
this.contactIds.forEach((contactId) => {
|
|
89
107
|
const contact = this.contactsById.get(contactId.toKey())
|
|
@@ -91,7 +109,11 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
91
109
|
ret.push(contact.contact)
|
|
92
110
|
}
|
|
93
111
|
})
|
|
94
|
-
|
|
112
|
+
if (limit === undefined) {
|
|
113
|
+
return ret
|
|
114
|
+
} else {
|
|
115
|
+
return ret.slice(0, limit)
|
|
116
|
+
}
|
|
95
117
|
}
|
|
96
118
|
|
|
97
119
|
public getUncontactedContacts(num: number): C[] {
|
|
@@ -124,22 +146,29 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
124
146
|
}
|
|
125
147
|
|
|
126
148
|
public compareIds(id1: PeerID, id2: PeerID): number {
|
|
127
|
-
const distance1 =
|
|
128
|
-
const distance2 =
|
|
149
|
+
const distance1 = this.distanceToReferenceId(id1)
|
|
150
|
+
const distance2 = this.distanceToReferenceId(id2)
|
|
129
151
|
return distance1 - distance2
|
|
130
152
|
}
|
|
131
153
|
|
|
154
|
+
// TODO inline this method?
|
|
155
|
+
private distanceToReferenceId(id: PeerID): number {
|
|
156
|
+
return getDistance(this.config.referenceId.value, id.value)
|
|
157
|
+
}
|
|
158
|
+
|
|
132
159
|
public removeContact(id: PeerID): boolean {
|
|
133
160
|
if (this.contactsById.has(id.toKey())) {
|
|
134
161
|
const removed = this.contactsById.get(id.toKey())!.contact
|
|
135
162
|
const index = this.contactIds.findIndex((element) => element.equals(id))
|
|
136
163
|
this.contactIds.splice(index, 1)
|
|
137
164
|
this.contactsById.delete(id.toKey())
|
|
138
|
-
this.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
165
|
+
if (this.config.emitEvents) {
|
|
166
|
+
this.emit(
|
|
167
|
+
'contactRemoved',
|
|
168
|
+
removed,
|
|
169
|
+
this.getClosestContacts()
|
|
170
|
+
)
|
|
171
|
+
}
|
|
143
172
|
return true
|
|
144
173
|
}
|
|
145
174
|
return false
|
|
@@ -152,4 +181,18 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
152
181
|
public getAllContacts(): C[] {
|
|
153
182
|
return this.contactIds.map((peerId) => this.contactsById.get(peerId.toKey())!.contact)
|
|
154
183
|
}
|
|
184
|
+
|
|
185
|
+
public getSize(): number {
|
|
186
|
+
return this.contactIds.length
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public clear(): void {
|
|
190
|
+
this.contactsById.clear()
|
|
191
|
+
this.contactIds = []
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
public stop(): void {
|
|
195
|
+
this.removeAllListeners()
|
|
196
|
+
this.clear()
|
|
197
|
+
}
|
|
155
198
|
}
|
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
import { RpcCommunicator, toProtoRpcClient } from '@streamr/proto-rpc'
|
|
2
1
|
import { Logger, runAndWaitForEvents3 } from '@streamr/utils'
|
|
3
2
|
import EventEmitter from 'eventemitter3'
|
|
4
|
-
import KBucket from 'k-bucket'
|
|
5
3
|
import { v4 } from 'uuid'
|
|
6
|
-
import { PeerID } from '../../helpers/PeerID'
|
|
4
|
+
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
7
5
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
8
|
-
import {
|
|
9
|
-
import { SortedContactList } from '../contact/SortedContactList'
|
|
6
|
+
import { PeerManager, getDistance } from '../PeerManager'
|
|
10
7
|
import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
|
|
11
|
-
import {
|
|
12
|
-
import { ServiceID } from '../../types/ServiceID'
|
|
8
|
+
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
13
9
|
|
|
14
10
|
const logger = new Logger(module)
|
|
15
11
|
|
|
@@ -18,16 +14,10 @@ interface DiscoverySessionEvents {
|
|
|
18
14
|
}
|
|
19
15
|
|
|
20
16
|
interface DiscoverySessionConfig {
|
|
21
|
-
bucket: KBucket<DhtNodeRpcRemote>
|
|
22
|
-
neighborList: SortedContactList<DhtNodeRpcRemote>
|
|
23
17
|
targetId: Uint8Array
|
|
24
|
-
localPeerDescriptor: PeerDescriptor
|
|
25
|
-
serviceId: ServiceID
|
|
26
|
-
rpcCommunicator: RpcCommunicator
|
|
27
18
|
parallelism: number
|
|
28
19
|
noProgressLimit: number
|
|
29
|
-
|
|
30
|
-
rpcRequestTimeout?: number
|
|
20
|
+
peerManager: PeerManager
|
|
31
21
|
}
|
|
32
22
|
|
|
33
23
|
export class DiscoverySession {
|
|
@@ -39,6 +29,7 @@ export class DiscoverySession {
|
|
|
39
29
|
private noProgressCounter = 0
|
|
40
30
|
private ongoingClosestPeersRequests: Set<string> = new Set()
|
|
41
31
|
private readonly config: DiscoverySessionConfig
|
|
32
|
+
private contactedPeers: Set<PeerIDKey> = new Set()
|
|
42
33
|
|
|
43
34
|
constructor(config: DiscoverySessionConfig) {
|
|
44
35
|
this.config = config
|
|
@@ -48,23 +39,7 @@ export class DiscoverySession {
|
|
|
48
39
|
if (this.stopped) {
|
|
49
40
|
return
|
|
50
41
|
}
|
|
51
|
-
|
|
52
|
-
if (!areEqualPeerDescriptors(contact, this.config.localPeerDescriptor)) {
|
|
53
|
-
const rpcRemote = new DhtNodeRpcRemote(
|
|
54
|
-
this.config.localPeerDescriptor,
|
|
55
|
-
contact,
|
|
56
|
-
toProtoRpcClient(new DhtNodeRpcClient(this.config.rpcCommunicator.getRpcClientTransport())),
|
|
57
|
-
this.config.serviceId,
|
|
58
|
-
this.config.rpcRequestTimeout
|
|
59
|
-
)
|
|
60
|
-
if (this.config.newContactListener) {
|
|
61
|
-
this.config.newContactListener(rpcRemote)
|
|
62
|
-
}
|
|
63
|
-
if (!this.config.neighborList.getContact(rpcRemote.getPeerId())) {
|
|
64
|
-
this.config.neighborList.addContact(rpcRemote)
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
})
|
|
42
|
+
this.config.peerManager.handleNewPeers(contacts)
|
|
68
43
|
}
|
|
69
44
|
|
|
70
45
|
private async getClosestPeersFromContact(contact: DhtNodeRpcRemote): Promise<PeerDescriptor[]> {
|
|
@@ -73,9 +48,9 @@ export class DiscoverySession {
|
|
|
73
48
|
}
|
|
74
49
|
logger.trace(`Getting closest peers from contact: ${getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
75
50
|
this.outgoingClosestPeersRequestsCounter++
|
|
76
|
-
this.
|
|
51
|
+
this.contactedPeers.add(contact.getPeerId().toKey())
|
|
77
52
|
const returnedContacts = await contact.getClosestPeers(this.config.targetId)
|
|
78
|
-
this.config.
|
|
53
|
+
this.config.peerManager.handlePeerActive(contact.getPeerId())
|
|
79
54
|
return returnedContacts
|
|
80
55
|
}
|
|
81
56
|
|
|
@@ -84,9 +59,12 @@ export class DiscoverySession {
|
|
|
84
59
|
return
|
|
85
60
|
}
|
|
86
61
|
this.ongoingClosestPeersRequests.delete(peerId.toKey())
|
|
87
|
-
const
|
|
62
|
+
const oldClosestNeighbor = this.config.peerManager.getClosestNeighborsTo(this.config.targetId, 1)[0]
|
|
63
|
+
const oldClosestDistance = getDistance(this.config.targetId, oldClosestNeighbor.getPeerId().value)
|
|
88
64
|
this.addNewContacts(contacts)
|
|
89
|
-
|
|
65
|
+
const newClosestNeighbor = this.config.peerManager.getClosestNeighborsTo(this.config.targetId, 1)[0]
|
|
66
|
+
const newClosestDistance = getDistance(this.config.targetId, newClosestNeighbor.getPeerId().value)
|
|
67
|
+
if (newClosestDistance >= oldClosestDistance) {
|
|
90
68
|
this.noProgressCounter++
|
|
91
69
|
} else {
|
|
92
70
|
this.noProgressCounter = 0
|
|
@@ -98,15 +76,14 @@ export class DiscoverySession {
|
|
|
98
76
|
return
|
|
99
77
|
}
|
|
100
78
|
this.ongoingClosestPeersRequests.delete(peer.getPeerId().toKey())
|
|
101
|
-
this.config.
|
|
102
|
-
this.config.neighborList.removeContact(peer.getPeerId())
|
|
79
|
+
this.config.peerManager.handlePeerUnresponsive(peer.getPeerId())
|
|
103
80
|
}
|
|
104
81
|
|
|
105
82
|
private findMoreContacts(): void {
|
|
106
83
|
if (this.stopped) {
|
|
107
84
|
return
|
|
108
85
|
}
|
|
109
|
-
const uncontacted = this.config.
|
|
86
|
+
const uncontacted = this.config.peerManager.getClosestContactsTo(this.config.targetId, this.config.parallelism, this.contactedPeers)
|
|
110
87
|
if (uncontacted.length === 0 || this.noProgressCounter >= this.config.noProgressLimit) {
|
|
111
88
|
this.emitter.emit('discoveryCompleted')
|
|
112
89
|
this.stopped = true
|
|
@@ -128,18 +105,16 @@ export class DiscoverySession {
|
|
|
128
105
|
}
|
|
129
106
|
}
|
|
130
107
|
|
|
131
|
-
public async findClosestNodes(timeout: number): Promise<
|
|
132
|
-
if (this.config.
|
|
133
|
-
|
|
134
|
-
+ this.config.neighborList.getSize())
|
|
135
|
-
return this.config.neighborList
|
|
108
|
+
public async findClosestNodes(timeout: number): Promise<void> {
|
|
109
|
+
if (this.config.peerManager.getNumberOfContacts(this.contactedPeers) === 0) {
|
|
110
|
+
return
|
|
136
111
|
}
|
|
112
|
+
// TODO add abortController and signal it in stop()
|
|
137
113
|
await runAndWaitForEvents3<DiscoverySessionEvents>(
|
|
138
114
|
[this.findMoreContacts.bind(this)],
|
|
139
115
|
[[this.emitter, 'discoveryCompleted']],
|
|
140
116
|
timeout
|
|
141
117
|
)
|
|
142
|
-
return this.config.neighborList
|
|
143
118
|
}
|
|
144
119
|
|
|
145
120
|
public stop(): void {
|
|
@@ -3,26 +3,20 @@ import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
|
|
|
3
3
|
import { areEqualPeerDescriptors, getNodeIdFromPeerDescriptor, peerIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
4
4
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
5
5
|
import { Logger, scheduleAtInterval, setAbortableTimeout } from '@streamr/utils'
|
|
6
|
-
import KBucket from 'k-bucket'
|
|
7
|
-
import { SortedContactList } from '../contact/SortedContactList'
|
|
8
6
|
import { ConnectionManager } from '../../connection/ConnectionManager'
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
7
|
+
import { PeerManager } from '../PeerManager'
|
|
8
|
+
import { createRandomNodeId } from '../../helpers/nodeId'
|
|
11
9
|
import { ServiceID } from '../../types/ServiceID'
|
|
12
10
|
|
|
13
11
|
interface PeerDiscoveryConfig {
|
|
14
|
-
rpcCommunicator: RoutingRpcCommunicator
|
|
15
12
|
localPeerDescriptor: PeerDescriptor
|
|
16
|
-
bucket: KBucket<DhtNodeRpcRemote>
|
|
17
|
-
neighborList: SortedContactList<DhtNodeRpcRemote>
|
|
18
13
|
joinNoProgressLimit: number
|
|
19
14
|
peerDiscoveryQueryBatchSize: number
|
|
20
15
|
serviceId: ServiceID
|
|
21
16
|
parallelism: number
|
|
22
17
|
joinTimeout: number
|
|
23
|
-
addContact: (contact: PeerDescriptor, setActive?: boolean) => void
|
|
24
18
|
connectionManager?: ConnectionManager
|
|
25
|
-
|
|
19
|
+
peerManager: PeerManager
|
|
26
20
|
}
|
|
27
21
|
|
|
28
22
|
const logger = new Logger(module)
|
|
@@ -55,13 +49,11 @@ export class PeerDiscovery {
|
|
|
55
49
|
return
|
|
56
50
|
}
|
|
57
51
|
this.config.connectionManager?.lockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
|
|
58
|
-
this.config.
|
|
52
|
+
this.config.peerManager.handleNewPeers([entryPointDescriptor])
|
|
59
53
|
const targetId = peerIdFromPeerDescriptor(this.config.localPeerDescriptor).value
|
|
60
|
-
const closest = this.config.bucket.closest(targetId, this.config.peerDiscoveryQueryBatchSize)
|
|
61
|
-
this.config.neighborList.addContacts(closest)
|
|
62
54
|
const sessions = [this.createSession(targetId)]
|
|
63
55
|
if (doAdditionalRandomPeerDiscovery) {
|
|
64
|
-
sessions.push(this.createSession(
|
|
56
|
+
sessions.push(this.createSession(createRandomNodeId()))
|
|
65
57
|
}
|
|
66
58
|
await this.runSessions(sessions, entryPointDescriptor, retry)
|
|
67
59
|
this.config.connectionManager?.unlockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
|
|
@@ -70,15 +62,10 @@ export class PeerDiscovery {
|
|
|
70
62
|
|
|
71
63
|
private createSession(targetId: Uint8Array): DiscoverySession {
|
|
72
64
|
const sessionOptions = {
|
|
73
|
-
bucket: this.config.bucket,
|
|
74
|
-
neighborList: this.config.neighborList,
|
|
75
65
|
targetId,
|
|
76
|
-
localPeerDescriptor: this.config.localPeerDescriptor,
|
|
77
|
-
serviceId: this.config.serviceId,
|
|
78
|
-
rpcCommunicator: this.config.rpcCommunicator,
|
|
79
66
|
parallelism: this.config.parallelism,
|
|
80
67
|
noProgressLimit: this.config.joinNoProgressLimit,
|
|
81
|
-
|
|
68
|
+
peerManager: this.config.peerManager
|
|
82
69
|
}
|
|
83
70
|
return new DiscoverySession(sessionOptions)
|
|
84
71
|
}
|
|
@@ -93,8 +80,9 @@ export class PeerDiscovery {
|
|
|
93
80
|
logger.debug(`DHT join on ${this.config.serviceId} timed out`)
|
|
94
81
|
} finally {
|
|
95
82
|
if (!this.isStopped()) {
|
|
96
|
-
if (this.config.
|
|
83
|
+
if (this.config.peerManager.getNumberOfNeighbors() === 0) {
|
|
97
84
|
if (retry) {
|
|
85
|
+
// TODO should we catch possible promise rejection?
|
|
98
86
|
setAbortableTimeout(() => this.rejoinDht(entryPointDescriptor), 1000, this.abortController.signal)
|
|
99
87
|
}
|
|
100
88
|
} else {
|
|
@@ -112,12 +100,12 @@ export class PeerDiscovery {
|
|
|
112
100
|
logger.debug(`Rejoining DHT ${this.config.serviceId}`)
|
|
113
101
|
this.rejoinOngoing = true
|
|
114
102
|
try {
|
|
115
|
-
this.config.neighborList.clear()
|
|
116
103
|
await this.joinDht(entryPoint)
|
|
117
104
|
logger.debug(`Rejoined DHT successfully ${this.config.serviceId}!`)
|
|
118
105
|
} catch (err) {
|
|
119
106
|
logger.warn(`Rejoining DHT ${this.config.serviceId} failed`)
|
|
120
107
|
if (!this.isStopped()) {
|
|
108
|
+
// TODO should we catch possible promise rejection?
|
|
121
109
|
setAbortableTimeout(() => this.rejoinDht(entryPoint), 5000, this.abortController.signal)
|
|
122
110
|
}
|
|
123
111
|
} finally {
|
|
@@ -136,13 +124,13 @@ export class PeerDiscovery {
|
|
|
136
124
|
if (this.isStopped()) {
|
|
137
125
|
return
|
|
138
126
|
}
|
|
139
|
-
const nodes = this.config.
|
|
140
|
-
await Promise.allSettled(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
this.config.
|
|
127
|
+
const nodes = this.config.peerManager.getClosestNeighborsTo(this.config.localPeerDescriptor.nodeId, this.config.parallelism)
|
|
128
|
+
await Promise.allSettled(
|
|
129
|
+
nodes.map(async (peer: DhtNodeRpcRemote) => {
|
|
130
|
+
const contacts = await peer.getClosestPeers(this.config.localPeerDescriptor.nodeId!)
|
|
131
|
+
this.config.peerManager.handleNewPeers(contacts)
|
|
144
132
|
})
|
|
145
|
-
|
|
133
|
+
)
|
|
146
134
|
}
|
|
147
135
|
|
|
148
136
|
public isJoinOngoing(): boolean {
|
|
@@ -163,7 +151,7 @@ export class PeerDiscovery {
|
|
|
163
151
|
clearTimeout(this.rejoinTimeoutRef)
|
|
164
152
|
this.rejoinTimeoutRef = undefined
|
|
165
153
|
}
|
|
166
|
-
this.ongoingDiscoverySessions.forEach((session
|
|
154
|
+
this.ongoingDiscoverySessions.forEach((session) => {
|
|
167
155
|
session.stop()
|
|
168
156
|
})
|
|
169
157
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Logger } from '@streamr/utils'
|
|
2
|
-
import { PeerDescriptor, RouteMessageAck, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
2
|
+
import { PeerDescriptor, RouteMessageAck, RouteMessageError, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
3
3
|
import { IFindRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
4
4
|
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
5
5
|
import { createRouteMessageAck } from '../routing/RouterRpcLocal'
|
|
@@ -24,7 +24,7 @@ export class FindRpcLocal implements IFindRpc {
|
|
|
24
24
|
|
|
25
25
|
async routeFindRequest(routedMessage: RouteMessageWrapper): Promise<RouteMessageAck> {
|
|
26
26
|
if (this.config.isMostLikelyDuplicate(routedMessage.requestId)) {
|
|
27
|
-
return createRouteMessageAck(routedMessage,
|
|
27
|
+
return createRouteMessageAck(routedMessage, RouteMessageError.DUPLICATE)
|
|
28
28
|
}
|
|
29
29
|
const senderId = getNodeIdFromPeerDescriptor(getPreviousPeer(routedMessage) ?? routedMessage.sourcePeer!)
|
|
30
30
|
logger.trace(`Received routeFindRequest call from ${senderId}`)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import EventEmitter from 'eventemitter3'
|
|
2
2
|
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
3
|
-
import { DataEntry, PeerDescriptor, FindResponse } from '../../proto/packages/dht/protos/DhtRpc'
|
|
3
|
+
import { DataEntry, PeerDescriptor, FindResponse, FindAction } from '../../proto/packages/dht/protos/DhtRpc'
|
|
4
4
|
import { ITransport } from '../../transport/ITransport'
|
|
5
5
|
import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
|
|
6
6
|
import { Contact } from '../contact/Contact'
|
|
@@ -17,20 +17,20 @@ export interface FindSessionEvents {
|
|
|
17
17
|
export interface FindSessionConfig {
|
|
18
18
|
serviceId: ServiceID
|
|
19
19
|
transport: ITransport
|
|
20
|
-
|
|
20
|
+
nodeIdToFind: Uint8Array
|
|
21
21
|
localPeerId: PeerID
|
|
22
22
|
waitedRoutingPathCompletions: number
|
|
23
|
-
|
|
23
|
+
action: FindAction
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
27
27
|
private readonly serviceId: ServiceID
|
|
28
28
|
private readonly transport: ITransport
|
|
29
|
-
private readonly
|
|
29
|
+
private readonly nodeIdToFind: Uint8Array
|
|
30
30
|
private readonly localPeerId: PeerID
|
|
31
31
|
private readonly waitedRoutingPathCompletions: number
|
|
32
32
|
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
33
|
-
private readonly
|
|
33
|
+
private readonly action: FindAction
|
|
34
34
|
private results: SortedContactList<Contact>
|
|
35
35
|
private foundData: Map<string, DataEntry> = new Map()
|
|
36
36
|
private allKnownHops: Set<PeerIDKey> = new Set()
|
|
@@ -43,11 +43,16 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
43
43
|
super()
|
|
44
44
|
this.serviceId = config.serviceId
|
|
45
45
|
this.transport = config.transport
|
|
46
|
-
this.
|
|
46
|
+
this.nodeIdToFind = config.nodeIdToFind
|
|
47
47
|
this.localPeerId = config.localPeerId
|
|
48
48
|
this.waitedRoutingPathCompletions = config.waitedRoutingPathCompletions
|
|
49
|
-
this.results = new SortedContactList(
|
|
50
|
-
|
|
49
|
+
this.results = new SortedContactList({
|
|
50
|
+
referenceId: PeerID.fromValue(this.nodeIdToFind),
|
|
51
|
+
maxSize: 10, // TODO use config option or named constant?
|
|
52
|
+
allowToContainReferenceId: true,
|
|
53
|
+
emitEvents: false
|
|
54
|
+
})
|
|
55
|
+
this.action = config.action
|
|
51
56
|
this.rpcCommunicator = new ListeningRpcCommunicator(this.serviceId, this.transport, {
|
|
52
57
|
rpcRequestTimeout: 15000
|
|
53
58
|
})
|
|
@@ -56,7 +61,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
56
61
|
|
|
57
62
|
private registerLocalRpcMethods() {
|
|
58
63
|
const rpcLocal = new FindSessionRpcLocal({
|
|
59
|
-
doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound
|
|
64
|
+
doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound: boolean) => {
|
|
60
65
|
this.doSendFindResponse(routingPath, nodes, dataEntries, noCloserNodesFound)
|
|
61
66
|
}
|
|
62
67
|
})
|
|
@@ -70,10 +75,10 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
70
75
|
unreportedHops.delete(id)
|
|
71
76
|
})
|
|
72
77
|
if (this.noCloserNodesReceivedCounter >= 1 && unreportedHops.size === 0) {
|
|
73
|
-
if (this.
|
|
78
|
+
if (this.action === FindAction.FETCH_DATA
|
|
74
79
|
&& (this.hasNonStaleData() || this.noCloserNodesReceivedCounter >= this.waitedRoutingPathCompletions)) {
|
|
75
80
|
return true
|
|
76
|
-
} else if (this.
|
|
81
|
+
} else if (this.action === FindAction.FETCH_DATA) {
|
|
77
82
|
return false
|
|
78
83
|
}
|
|
79
84
|
return true
|
|
@@ -89,7 +94,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
89
94
|
routingPath: PeerDescriptor[],
|
|
90
95
|
nodes: PeerDescriptor[],
|
|
91
96
|
dataEntries: DataEntry[],
|
|
92
|
-
noCloserNodesFound
|
|
97
|
+
noCloserNodesFound: boolean
|
|
93
98
|
): void {
|
|
94
99
|
this.addKnownHops(routingPath)
|
|
95
100
|
if (routingPath.length >= 1) {
|
|
@@ -106,7 +111,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
106
111
|
|
|
107
112
|
private addKnownHops(routingPath: PeerDescriptor[]) {
|
|
108
113
|
routingPath.forEach((desc) => {
|
|
109
|
-
const newPeerId = PeerID.fromValue(desc.
|
|
114
|
+
const newPeerId = PeerID.fromValue(desc.nodeId)
|
|
110
115
|
if (!this.localPeerId.equals(newPeerId)) {
|
|
111
116
|
this.allKnownHops.add(newPeerId.toKey())
|
|
112
117
|
}
|
|
@@ -114,7 +119,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
114
119
|
}
|
|
115
120
|
|
|
116
121
|
private setHopAsReported(desc: PeerDescriptor) {
|
|
117
|
-
const newPeerId = PeerID.fromValue(desc.
|
|
122
|
+
const newPeerId = PeerID.fromValue(desc.nodeId)
|
|
118
123
|
if (!this.localPeerId.equals(newPeerId)) {
|
|
119
124
|
this.reportedHops.add(newPeerId.toKey())
|
|
120
125
|
}
|
|
@@ -132,11 +137,11 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
132
137
|
|
|
133
138
|
private processFoundData(dataEntries: DataEntry[]): void {
|
|
134
139
|
dataEntries.forEach((entry) => {
|
|
135
|
-
const
|
|
136
|
-
const existingEntry = this.foundData.get(
|
|
137
|
-
if (!existingEntry || existingEntry.
|
|
138
|
-
|| (existingEntry.
|
|
139
|
-
this.foundData.set(
|
|
140
|
+
const creatorKey = keyFromPeerDescriptor(entry.creator!)
|
|
141
|
+
const existingEntry = this.foundData.get(creatorKey)
|
|
142
|
+
if (!existingEntry || existingEntry.createdAt! < entry.createdAt!
|
|
143
|
+
|| (existingEntry.createdAt! <= entry.createdAt! && entry.deleted)) {
|
|
144
|
+
this.foundData.set(creatorKey, entry)
|
|
140
145
|
}
|
|
141
146
|
})
|
|
142
147
|
}
|
|
@@ -164,7 +169,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
164
169
|
|
|
165
170
|
public getResults = (): FindResult => ({
|
|
166
171
|
closestNodes: this.results.getAllContacts().map((contact) => contact.getPeerDescriptor()),
|
|
167
|
-
dataEntries:
|
|
172
|
+
dataEntries: Array.from(this.foundData.values())
|
|
168
173
|
})
|
|
169
174
|
|
|
170
175
|
public stop(): void {
|
|
@@ -6,7 +6,7 @@ import { Logger } from '@streamr/utils'
|
|
|
6
6
|
const logger = new Logger(module)
|
|
7
7
|
|
|
8
8
|
interface FindSessionRpcLocalConfig {
|
|
9
|
-
doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound
|
|
9
|
+
doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound: boolean) => void
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export class FindSessionRpcLocal implements IFindSessionRpc {
|
|
@@ -5,11 +5,11 @@ import {
|
|
|
5
5
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
6
6
|
import { IFindSessionRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
7
7
|
import { Logger } from '@streamr/utils'
|
|
8
|
-
import {
|
|
8
|
+
import { RpcRemote } from '../contact/RpcRemote'
|
|
9
9
|
|
|
10
10
|
const logger = new Logger(module)
|
|
11
11
|
|
|
12
|
-
export class FindSessionRpcRemote extends
|
|
12
|
+
export class FindSessionRpcRemote extends RpcRemote<IFindSessionRpcClient> {
|
|
13
13
|
|
|
14
14
|
sendFindResponse(
|
|
15
15
|
routingPath: PeerDescriptor[],
|