@streamr/dht 0.0.1-tatum.6 → 0.0.1-tatum.7
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 +1 -1
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +17 -40
- package/dist/src/connection/ConnectionManager.js +138 -204
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectivityChecker.js +14 -11
- package/dist/src/connection/ConnectivityChecker.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +49 -0
- package/dist/src/connection/ConnectorFacade.js +86 -0
- package/dist/src/connection/ConnectorFacade.js.map +1 -0
- package/dist/src/connection/ManagedConnection.d.ts +1 -4
- package/dist/src/connection/ManagedConnection.js +23 -31
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/RemoteConnectionLocker.js +4 -3
- package/dist/src/connection/RemoteConnectionLocker.js.map +1 -1
- package/dist/src/connection/Simulator/Simulator.d.ts +0 -2
- package/dist/src/connection/Simulator/Simulator.js +0 -5
- package/dist/src/connection/Simulator/Simulator.js.map +1 -1
- package/dist/src/connection/Simulator/SimulatorConnection.js +16 -13
- package/dist/src/connection/Simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/Simulator/SimulatorConnector.d.ts +2 -2
- package/dist/src/connection/Simulator/SimulatorConnector.js +10 -11
- package/dist/src/connection/Simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/Simulator/SimulatorTransport.js +6 -1
- package/dist/src/connection/Simulator/SimulatorTransport.js.map +1 -1
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.d.ts +2 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js +12 -12
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js.map +1 -1
- package/dist/src/connection/WebRTC/WebRtcConnector.d.ts +9 -9
- package/dist/src/connection/WebRTC/WebRtcConnector.js +22 -22
- package/dist/src/connection/WebRTC/WebRtcConnector.js.map +1 -1
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js +2 -1
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js.map +1 -1
- package/dist/src/connection/WebSocket/WebSocketConnector.d.ts +19 -8
- package/dist/src/connection/WebSocket/WebSocketConnector.js +67 -46
- package/dist/src/connection/WebSocket/WebSocketConnector.js.map +1 -1
- package/dist/src/connection/WebSocket/WebSocketServer.d.ts +11 -1
- package/dist/src/connection/WebSocket/WebSocketServer.js +15 -10
- package/dist/src/connection/WebSocket/WebSocketServer.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +15 -51
- package/dist/src/dht/DhtNode.js +107 -129
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/{DhtPeer.d.ts → RemoteDhtNode.d.ts} +2 -3
- package/dist/src/dht/{DhtPeer.js → RemoteDhtNode.js} +21 -19
- package/dist/src/dht/RemoteDhtNode.js.map +1 -0
- package/dist/src/dht/contact/ContactList.d.ts +0 -1
- package/dist/src/dht/contact/ContactList.js +0 -3
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +0 -1
- package/dist/src/dht/contact/RandomContactList.js +0 -3
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +0 -3
- package/dist/src/dht/contact/SortedContactList.js +0 -9
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +5 -7
- package/dist/src/dht/discovery/DiscoverySession.js +9 -10
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +11 -10
- package/dist/src/dht/discovery/PeerDiscovery.js +32 -37
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/find/RecursiveFindSession.d.ts +5 -6
- package/dist/src/dht/find/RecursiveFindSession.js +8 -8
- package/dist/src/dht/find/RecursiveFindSession.js.map +1 -1
- package/dist/src/dht/find/RecursiveFinder.d.ts +2 -4
- package/dist/src/dht/find/RecursiveFinder.js +11 -12
- package/dist/src/dht/find/RecursiveFinder.js.map +1 -1
- package/dist/src/dht/registerExternalApiRpcMethods.d.ts +1 -1
- package/dist/src/dht/routing/DuplicateDetector.d.ts +1 -2
- package/dist/src/dht/routing/DuplicateDetector.js +2 -7
- package/dist/src/dht/routing/DuplicateDetector.js.map +1 -1
- package/dist/src/dht/routing/RemoteRouter.js +1 -1
- package/dist/src/dht/routing/RemoteRouter.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +10 -13
- package/dist/src/dht/routing/Router.js +28 -29
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -4
- package/dist/src/dht/routing/RoutingSession.js +4 -3
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/DataStore.d.ts +2 -2
- package/dist/src/dht/store/DataStore.js +7 -7
- package/dist/src/dht/store/DataStore.js.map +1 -1
- package/dist/src/exports.d.ts +1 -7
- package/dist/src/exports.js +2 -14
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/PeerID.d.ts +0 -1
- package/dist/src/helpers/PeerID.js +0 -6
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/browser/isBrowserEnvironment.d.ts +1 -0
- package/dist/src/helpers/browser/isBrowserEnvironment.js +6 -0
- package/dist/src/helpers/browser/isBrowserEnvironment.js.map +1 -0
- package/dist/src/helpers/browser/isBrowserEnvironment_override.d.ts +1 -0
- package/dist/src/helpers/browser/isBrowserEnvironment_override.js +7 -0
- package/dist/src/helpers/browser/isBrowserEnvironment_override.js.map +1 -0
- package/dist/src/helpers/kademliaId.d.ts +1 -0
- package/dist/src/helpers/kademliaId.js +14 -0
- package/dist/src/helpers/kademliaId.js.map +1 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +1 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +3 -3
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +0 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +1 -2
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/package.json +10 -9
- package/protos/DhtRpc.proto +0 -1
- package/src/connection/ConnectionLockHandler.ts +1 -1
- package/src/connection/ConnectionManager.ts +157 -254
- package/src/connection/ConnectivityChecker.ts +14 -11
- package/src/connection/ConnectorFacade.ts +143 -0
- package/src/connection/ManagedConnection.ts +23 -34
- package/src/connection/RemoteConnectionLocker.ts +4 -3
- package/src/connection/Simulator/Simulator.ts +0 -7
- package/src/connection/Simulator/SimulatorConnection.ts +16 -13
- package/src/connection/Simulator/SimulatorConnector.ts +11 -12
- package/src/connection/Simulator/SimulatorTransport.ts +6 -1
- package/src/connection/WebRTC/NodeWebRtcConnection.ts +14 -13
- package/src/connection/WebRTC/WebRtcConnector.ts +31 -31
- package/src/connection/WebSocket/RemoteWebSocketConnector.ts +2 -1
- package/src/connection/WebSocket/WebSocketConnector.ts +85 -62
- package/src/connection/WebSocket/WebSocketServer.ts +26 -8
- package/src/dht/DhtNode.ts +155 -181
- package/src/dht/{DhtPeer.ts → RemoteDhtNode.ts} +11 -9
- package/src/dht/contact/ContactList.ts +0 -4
- package/src/dht/contact/RandomContactList.ts +0 -4
- package/src/dht/contact/SortedContactList.ts +0 -12
- package/src/dht/discovery/DiscoverySession.ts +20 -23
- package/src/dht/discovery/PeerDiscovery.ts +45 -44
- package/src/dht/find/RecursiveFindSession.ts +12 -13
- package/src/dht/find/RecursiveFinder.ts +16 -19
- package/src/dht/registerExternalApiRpcMethods.ts +1 -1
- package/src/dht/routing/DuplicateDetector.ts +3 -10
- package/src/dht/routing/RemoteRouter.ts +2 -2
- package/src/dht/routing/Router.ts +35 -39
- package/src/dht/routing/RoutingSession.ts +9 -9
- package/src/dht/store/DataStore.ts +11 -11
- package/src/exports.ts +1 -7
- package/src/helpers/PeerID.ts +0 -7
- package/src/helpers/browser/isBrowserEnvironment.ts +1 -0
- package/src/helpers/browser/isBrowserEnvironment_override.ts +3 -0
- package/src/helpers/kademliaId.ts +8 -0
- package/src/helpers/peerIdFromPeerDescriptor.ts +1 -1
- package/src/proto/packages/dht/protos/DhtRpc.ts +1 -6
- package/test/benchmark/KademliaCorrectness.test.ts +4 -2
- package/test/benchmark/RecursiveFind.test.ts +6 -6
- package/test/end-to-end/Layer0-Layer1.test.ts +9 -9
- package/test/end-to-end/Layer0WebRTC-Layer1.test.ts +5 -5
- package/test/end-to-end/Layer0WebRTC.test.ts +5 -6
- package/test/end-to-end/Layer1-Scale-WebRTC.test.ts +13 -8
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +15 -10
- package/test/end-to-end/WebSocketConnectionRequest.test.ts +5 -5
- package/test/integration/ConnectionLocking.test.ts +32 -26
- package/test/integration/ConnectionManager.test.ts +90 -93
- package/test/integration/DhtJoinPeerDiscovery.test.ts +53 -0
- package/test/integration/DhtRpc.test.ts +4 -6
- package/test/integration/Layer1-scale.test.ts +8 -8
- package/test/integration/MigrateData.test.ts +9 -9
- package/test/integration/Mock-Layer1-Layer0.test.ts +1 -2
- package/test/integration/RecursiveFind.test.ts +5 -5
- package/test/integration/{DhtPeer.test.ts → RemoteDhtNode.test.ts} +11 -12
- package/test/integration/RemoteRouter.test.ts +5 -6
- package/test/integration/RemoteStore.test.ts +4 -5
- package/test/integration/RouteMessage.test.ts +7 -9
- package/test/integration/RpcErrors.test.ts +25 -10
- package/test/integration/ScaleDownDht.test.ts +8 -8
- package/test/integration/SimultaneousConnections.test.ts +35 -36
- package/test/integration/Store.test.ts +8 -9
- package/test/integration/StoreAndDelete.test.ts +11 -11
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +7 -7
- package/test/integration/WebRtcConnectionManagement.test.ts +26 -19
- package/test/integration/WebRtcConnectorRpc.test.ts +6 -8
- package/test/integration/WebSocket.test.ts +4 -2
- package/test/integration/WebSocketConnectionManagement.test.ts +30 -17
- package/test/integration/WebSocketConnectorRpc.test.ts +2 -3
- package/test/unit/DuplicateDetector.test.ts +3 -4
- package/test/unit/LocalDataStore.test.ts +6 -8
- package/test/unit/RandomContactList.test.ts +1 -1
- package/test/unit/RecursiveFinder.test.ts +8 -12
- package/test/unit/Router.test.ts +18 -21
- package/test/unit/WebSocketConnector.test.ts +64 -0
- package/test/unit/WebSocketServer.test.ts +24 -12
- package/test/utils/mock/RecursiveFinder.ts +2 -2
- package/test/utils/mock/Router.ts +9 -11
- package/test/utils/mock/Transport.ts +2 -2
- package/test/utils/utils.ts +40 -49
- package/dist/src/dht/DhtPeer.js.map +0 -1
- package/dist/src/helpers/browser/isBrowser.d.ts +0 -1
- package/dist/src/helpers/browser/isBrowser.js +0 -6
- package/dist/src/helpers/browser/isBrowser.js.map +0 -1
- package/dist/src/helpers/browser/isNodeJS.d.ts +0 -1
- package/dist/src/helpers/browser/isNodeJS.js +0 -6
- package/dist/src/helpers/browser/isNodeJS.js.map +0 -1
- package/src/helpers/browser/isBrowser.ts +0 -1
- package/src/helpers/browser/isNodeJS.ts +0 -1
- package/test/integration/DhtWithMockConnectionLatencies.test.ts +0 -46
- package/test/integration/DhtWithMockConnections.test.ts +0 -46
- package/test/integration/DhtWithRealConnectionLatencies.test.ts +0 -47
- /package/test/unit/{webrtcReplaceInternalIpWithExternalIp.ts → webrtcReplaceInternalIpWithExternalIp.test.ts} +0 -0
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { IDhtRpcServiceClient } from '../proto/packages/dht/protos/DhtRpc.client'
|
|
2
2
|
import {
|
|
3
3
|
ClosestPeersRequest,
|
|
4
|
-
LeaveNotice,
|
|
5
4
|
PeerDescriptor,
|
|
6
5
|
PingRequest
|
|
7
6
|
} from '../proto/packages/dht/protos/DhtRpc'
|
|
@@ -9,7 +8,8 @@ import { v4 } from 'uuid'
|
|
|
9
8
|
import { Logger } from '@streamr/utils'
|
|
10
9
|
import { ProtoRpcClient } from '@streamr/proto-rpc'
|
|
11
10
|
import { Remote } from './contact/Remote'
|
|
12
|
-
import { PeerID
|
|
11
|
+
import { PeerID } from '../helpers/PeerID'
|
|
12
|
+
import { keyFromPeerDescriptor, peerIdFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
13
13
|
|
|
14
14
|
const logger = new Logger(module)
|
|
15
15
|
|
|
@@ -19,7 +19,7 @@ export interface KBucketContact {
|
|
|
19
19
|
vectorClock: number
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export class
|
|
22
|
+
export class RemoteDhtNode extends Remote<IDhtRpcServiceClient> implements KBucketContact {
|
|
23
23
|
|
|
24
24
|
private static counter = 0
|
|
25
25
|
public vectorClock: number
|
|
@@ -33,11 +33,11 @@ export class DhtPeer extends Remote<IDhtRpcServiceClient> implements KBucketCont
|
|
|
33
33
|
) {
|
|
34
34
|
super(ownPeerDescriptor, peerDescriptor, serviceId, client)
|
|
35
35
|
this.id = this.getPeerId().value
|
|
36
|
-
this.vectorClock =
|
|
36
|
+
this.vectorClock = RemoteDhtNode.counter++
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
async getClosestPeers(kademliaId: Uint8Array): Promise<PeerDescriptor[]> {
|
|
40
|
-
logger.trace(`Requesting getClosestPeers on ${this.getServiceId()} from ${this.
|
|
40
|
+
logger.trace(`Requesting getClosestPeers on ${this.getServiceId()} from ${keyFromPeerDescriptor(this.getPeerDescriptor())}`)
|
|
41
41
|
const request: ClosestPeersRequest = {
|
|
42
42
|
kademliaId,
|
|
43
43
|
requestId: v4()
|
|
@@ -52,7 +52,7 @@ export class DhtPeer extends Remote<IDhtRpcServiceClient> implements KBucketCont
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
async ping(): Promise<boolean> {
|
|
55
|
-
logger.trace(`Requesting ping on ${this.getServiceId()} from ${this.
|
|
55
|
+
logger.trace(`Requesting ping on ${this.getServiceId()} from ${keyFromPeerDescriptor(this.getPeerDescriptor())}`)
|
|
56
56
|
const request: PingRequest = {
|
|
57
57
|
requestId: v4()
|
|
58
58
|
}
|
|
@@ -65,13 +65,15 @@ export class DhtPeer extends Remote<IDhtRpcServiceClient> implements KBucketCont
|
|
|
65
65
|
return true
|
|
66
66
|
}
|
|
67
67
|
} catch (err) {
|
|
68
|
-
logger.trace(`ping failed on ${this.getServiceId()} to ${this.
|
|
68
|
+
logger.trace(`ping failed on ${this.getServiceId()} to ${keyFromPeerDescriptor(this.getPeerDescriptor())}: ${err}`)
|
|
69
69
|
}
|
|
70
70
|
return false
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
/*
|
|
74
|
+
TODO remove or start using this method in NET-1131
|
|
73
75
|
leaveNotice(): void {
|
|
74
|
-
logger.trace(`Sending leaveNotice on ${this.getServiceId()} from ${this.
|
|
76
|
+
logger.trace(`Sending leaveNotice on ${this.getServiceId()} from ${keyFromPeerDescriptor(this.getPeerDescriptor())}`)
|
|
75
77
|
const request: LeaveNotice = {
|
|
76
78
|
serviceId: this.getServiceId()
|
|
77
79
|
}
|
|
@@ -81,7 +83,7 @@ export class DhtPeer extends Remote<IDhtRpcServiceClient> implements KBucketCont
|
|
|
81
83
|
this.getClient().leaveNotice(request, options).catch((e) => {
|
|
82
84
|
logger.trace('Failed to send leaveNotice' + e)
|
|
83
85
|
})
|
|
84
|
-
}
|
|
86
|
+
}*/
|
|
85
87
|
|
|
86
88
|
getPeerId(): PeerID {
|
|
87
89
|
return peerIdFromPeerDescriptor(this.getPeerDescriptor())
|
|
@@ -39,10 +39,6 @@ export class ContactList<C extends { getPeerId: () => PeerID }> extends EventEmi
|
|
|
39
39
|
return this.contactsById.get(id.toKey())!
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
public hasContact(id: PeerID): boolean {
|
|
43
|
-
return this.contactsById.has(id.toKey())
|
|
44
|
-
}
|
|
45
|
-
|
|
46
42
|
public getSize(): number {
|
|
47
43
|
return this.contactIds.length
|
|
48
44
|
}
|
|
@@ -37,10 +37,6 @@ export class RandomContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
addContacts(contacts: C[]): void {
|
|
41
|
-
contacts.forEach((contact) => this.addContact(contact))
|
|
42
|
-
}
|
|
43
|
-
|
|
44
40
|
removeContact(id: PeerID): boolean {
|
|
45
41
|
if (this.contactsById.has(id.toKey())) {
|
|
46
42
|
const removed = this.contactsById.get(id.toKey())!.contact
|
|
@@ -129,10 +129,6 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
129
129
|
return distance1 - distance2
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
public getStringIds(): string[] {
|
|
133
|
-
return this.contactIds.map((peerId) => peerId.toKey())
|
|
134
|
-
}
|
|
135
|
-
|
|
136
132
|
public removeContact(id: PeerID): boolean {
|
|
137
133
|
if (this.contactsById.has(id.toKey())) {
|
|
138
134
|
const removed = this.contactsById.get(id.toKey())!.contact
|
|
@@ -156,12 +152,4 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
156
152
|
public getAllContacts(): C[] {
|
|
157
153
|
return this.contactIds.map((peerId) => this.contactsById.get(peerId.toKey())!.contact)
|
|
158
154
|
}
|
|
159
|
-
|
|
160
|
-
public getMaxSize(): number {
|
|
161
|
-
return this.maxSize
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
public setAllAsUncontacted(): void {
|
|
165
|
-
this.contactsById.forEach((contact) => contact.contacted = false)
|
|
166
|
-
}
|
|
167
155
|
}
|
|
@@ -7,8 +7,8 @@ import { PeerID } from '../../helpers/PeerID'
|
|
|
7
7
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
8
8
|
import { DhtRpcServiceClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
9
9
|
import { SortedContactList } from '../contact/SortedContactList'
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
10
|
+
import { RemoteDhtNode } from '../RemoteDhtNode'
|
|
11
|
+
import { areEqualPeerDescriptors, keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
12
12
|
|
|
13
13
|
const logger = new Logger(module)
|
|
14
14
|
|
|
@@ -17,16 +17,15 @@ interface DiscoverySessionEvents {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
interface DiscoverySessionConfig {
|
|
20
|
-
bucket: KBucket<
|
|
21
|
-
neighborList: SortedContactList<
|
|
20
|
+
bucket: KBucket<RemoteDhtNode>
|
|
21
|
+
neighborList: SortedContactList<RemoteDhtNode>
|
|
22
22
|
targetId: Uint8Array
|
|
23
23
|
ownPeerDescriptor: PeerDescriptor
|
|
24
24
|
serviceId: string
|
|
25
25
|
rpcCommunicator: RpcCommunicator
|
|
26
26
|
parallelism: number
|
|
27
27
|
noProgressLimit: number
|
|
28
|
-
newContactListener?: (
|
|
29
|
-
nodeName?: string
|
|
28
|
+
newContactListener?: (remoteDhtNode: RemoteDhtNode) => void
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
export class DiscoverySession {
|
|
@@ -38,11 +37,9 @@ export class DiscoverySession {
|
|
|
38
37
|
private noProgressCounter = 0
|
|
39
38
|
private ongoingClosestPeersRequests: Set<string> = new Set()
|
|
40
39
|
private readonly config: DiscoverySessionConfig
|
|
41
|
-
private readonly ownPeerId: PeerID
|
|
42
40
|
|
|
43
41
|
constructor(config: DiscoverySessionConfig) {
|
|
44
42
|
this.config = config
|
|
45
|
-
this.ownPeerId = peerIdFromPeerDescriptor(config.ownPeerDescriptor)
|
|
46
43
|
}
|
|
47
44
|
|
|
48
45
|
private addNewContacts(contacts: PeerDescriptor[]): void {
|
|
@@ -50,28 +47,28 @@ export class DiscoverySession {
|
|
|
50
47
|
return
|
|
51
48
|
}
|
|
52
49
|
contacts.forEach((contact) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
50
|
+
if (!areEqualPeerDescriptors(contact, this.config.ownPeerDescriptor)) {
|
|
51
|
+
const remoteDhtNode = new RemoteDhtNode(
|
|
52
|
+
this.config.ownPeerDescriptor,
|
|
53
|
+
contact,
|
|
54
|
+
toProtoRpcClient(new DhtRpcServiceClient(this.config.rpcCommunicator.getRpcClientTransport())),
|
|
55
|
+
this.config.serviceId
|
|
56
|
+
)
|
|
60
57
|
if (this.config.newContactListener) {
|
|
61
|
-
this.config.newContactListener(
|
|
58
|
+
this.config.newContactListener(remoteDhtNode)
|
|
62
59
|
}
|
|
63
|
-
if (!this.config.neighborList.getContact(
|
|
64
|
-
this.config.neighborList.addContact(
|
|
60
|
+
if (!this.config.neighborList.getContact(remoteDhtNode.getPeerId())) {
|
|
61
|
+
this.config.neighborList.addContact(remoteDhtNode)
|
|
65
62
|
}
|
|
66
63
|
}
|
|
67
64
|
})
|
|
68
65
|
}
|
|
69
66
|
|
|
70
|
-
private async getClosestPeersFromContact(contact:
|
|
67
|
+
private async getClosestPeersFromContact(contact: RemoteDhtNode): Promise<PeerDescriptor[]> {
|
|
71
68
|
if (this.stopped) {
|
|
72
69
|
return []
|
|
73
70
|
}
|
|
74
|
-
logger.trace(`Getting closest peers from contact: ${contact.
|
|
71
|
+
logger.trace(`Getting closest peers from contact: ${keyFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
75
72
|
this.outgoingClosestPeersRequestsCounter++
|
|
76
73
|
this.config.neighborList.setContacted(contact.getPeerId())
|
|
77
74
|
const returnedContacts = await contact.getClosestPeers(this.config.targetId)
|
|
@@ -93,7 +90,7 @@ export class DiscoverySession {
|
|
|
93
90
|
}
|
|
94
91
|
}
|
|
95
92
|
|
|
96
|
-
private onClosestPeersRequestFailed(peer:
|
|
93
|
+
private onClosestPeersRequestFailed(peer: RemoteDhtNode) {
|
|
97
94
|
if (!this.ongoingClosestPeersRequests.has(peer.getPeerId().toKey())) {
|
|
98
95
|
return
|
|
99
96
|
}
|
|
@@ -120,7 +117,7 @@ export class DiscoverySession {
|
|
|
120
117
|
// eslint-disable-next-line promise/catch-or-return
|
|
121
118
|
this.getClosestPeersFromContact(nextPeer)
|
|
122
119
|
.then((contacts) => this.onClosestPeersRequestSucceeded(nextPeer.getPeerId(), contacts))
|
|
123
|
-
.catch((
|
|
120
|
+
.catch(() => this.onClosestPeersRequestFailed(nextPeer))
|
|
124
121
|
.finally(() => {
|
|
125
122
|
this.outgoingClosestPeersRequestsCounter--
|
|
126
123
|
this.findMoreContacts()
|
|
@@ -128,7 +125,7 @@ export class DiscoverySession {
|
|
|
128
125
|
}
|
|
129
126
|
}
|
|
130
127
|
|
|
131
|
-
public async findClosestNodes(timeout: number): Promise<SortedContactList<
|
|
128
|
+
public async findClosestNodes(timeout: number): Promise<SortedContactList<RemoteDhtNode>> {
|
|
132
129
|
if (this.config.neighborList.getUncontactedContacts(this.config.parallelism).length < 1) {
|
|
133
130
|
logger.trace('getUncontactedContacts length was 0 in beginning of discovery, this.neighborList.size: '
|
|
134
131
|
+ this.config.neighborList.getSize())
|
|
@@ -1,25 +1,24 @@
|
|
|
1
1
|
import { DiscoverySession } from './DiscoverySession'
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import { isSamePeerDescriptor, keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
2
|
+
import { RemoteDhtNode } from '../RemoteDhtNode'
|
|
3
|
+
import { areEqualPeerDescriptors, keyFromPeerDescriptor, peerIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
5
4
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
6
5
|
import { Logger, scheduleAtInterval, setAbortableTimeout } from '@streamr/utils'
|
|
7
6
|
import KBucket from 'k-bucket'
|
|
8
7
|
import { SortedContactList } from '../contact/SortedContactList'
|
|
9
8
|
import { ConnectionManager } from '../../connection/ConnectionManager'
|
|
10
|
-
import {
|
|
9
|
+
import { PeerIDKey } from '../../helpers/PeerID'
|
|
11
10
|
import { RoutingRpcCommunicator } from '../../transport/RoutingRpcCommunicator'
|
|
12
11
|
import { RandomContactList } from '../contact/RandomContactList'
|
|
12
|
+
import { createRandomKademliaId } from '../../helpers/kademliaId'
|
|
13
13
|
|
|
14
14
|
interface PeerDiscoveryConfig {
|
|
15
15
|
rpcCommunicator: RoutingRpcCommunicator
|
|
16
16
|
ownPeerDescriptor: PeerDescriptor
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
openInternetPeers: SortedContactList<DhtPeer>
|
|
17
|
+
bucket: KBucket<RemoteDhtNode>
|
|
18
|
+
connections: Map<PeerIDKey, RemoteDhtNode>
|
|
19
|
+
neighborList: SortedContactList<RemoteDhtNode>
|
|
20
|
+
randomPeers: RandomContactList<RemoteDhtNode>
|
|
21
|
+
openInternetPeers: SortedContactList<RemoteDhtNode>
|
|
23
22
|
joinNoProgressLimit: number
|
|
24
23
|
getClosestContactsLimit: number
|
|
25
24
|
serviceId: string
|
|
@@ -32,12 +31,11 @@ interface PeerDiscoveryConfig {
|
|
|
32
31
|
const logger = new Logger(module)
|
|
33
32
|
|
|
34
33
|
export class PeerDiscovery {
|
|
34
|
+
|
|
35
35
|
private readonly config: PeerDiscoveryConfig
|
|
36
36
|
private ongoingDiscoverySessions: Map<string, DiscoverySession> = new Map()
|
|
37
|
-
private stopped = false
|
|
38
37
|
private rejoinOngoing = false
|
|
39
38
|
private joinCalled = false
|
|
40
|
-
|
|
41
39
|
private rejoinTimeoutRef?: NodeJS.Timeout
|
|
42
40
|
private readonly abortController: AbortController
|
|
43
41
|
private recoveryIntervalStarted = false
|
|
@@ -47,8 +45,8 @@ export class PeerDiscovery {
|
|
|
47
45
|
this.abortController = new AbortController()
|
|
48
46
|
}
|
|
49
47
|
|
|
50
|
-
async joinDht(entryPointDescriptor: PeerDescriptor,
|
|
51
|
-
if (this.
|
|
48
|
+
async joinDht(entryPointDescriptor: PeerDescriptor, doAdditionalRandomPeerDiscovery = true, retry = true): Promise<void> {
|
|
49
|
+
if (this.isStopped()) {
|
|
52
50
|
return
|
|
53
51
|
}
|
|
54
52
|
this.joinCalled = true
|
|
@@ -56,44 +54,47 @@ export class PeerDiscovery {
|
|
|
56
54
|
`Joining ${this.config.serviceId === 'layer0' ? 'The Streamr Network' : `Control Layer for ${this.config.serviceId}`}`
|
|
57
55
|
+ ` via entrypoint ${keyFromPeerDescriptor(entryPointDescriptor)}`
|
|
58
56
|
)
|
|
59
|
-
if (
|
|
57
|
+
if (areEqualPeerDescriptors(entryPointDescriptor, this.config.ownPeerDescriptor)) {
|
|
60
58
|
return
|
|
61
59
|
}
|
|
62
60
|
this.config.connectionManager?.lockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
|
|
63
61
|
this.config.addContact(entryPointDescriptor)
|
|
64
|
-
const closest = this.config.bucket.closest(this.config.
|
|
62
|
+
const closest = this.config.bucket.closest(peerIdFromPeerDescriptor(this.config.ownPeerDescriptor).value, this.config.getClosestContactsLimit)
|
|
65
63
|
this.config.neighborList.addContacts(closest)
|
|
64
|
+
const sessions = [this.createSession(peerIdFromPeerDescriptor(this.config.ownPeerDescriptor).value)]
|
|
65
|
+
if (doAdditionalRandomPeerDiscovery) {
|
|
66
|
+
sessions.push(this.createSession(createRandomKademliaId()))
|
|
67
|
+
}
|
|
68
|
+
await this.runSessions(sessions, entryPointDescriptor, retry)
|
|
69
|
+
this.config.connectionManager?.unlockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
|
|
70
|
+
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private createSession(targetId: Uint8Array): DiscoverySession {
|
|
66
74
|
const sessionOptions = {
|
|
67
75
|
bucket: this.config.bucket,
|
|
68
76
|
neighborList: this.config.neighborList,
|
|
69
|
-
targetId
|
|
77
|
+
targetId,
|
|
70
78
|
ownPeerDescriptor: this.config.ownPeerDescriptor,
|
|
71
79
|
serviceId: this.config.serviceId,
|
|
72
80
|
rpcCommunicator: this.config.rpcCommunicator,
|
|
73
81
|
parallelism: this.config.parallelism,
|
|
74
82
|
noProgressLimit: this.config.joinNoProgressLimit,
|
|
75
|
-
newContactListener: (newPeer:
|
|
76
|
-
nodeName: this.config.ownPeerDescriptor.nodeName
|
|
77
|
-
}
|
|
78
|
-
const session = new DiscoverySession(sessionOptions)
|
|
79
|
-
const randomSession = doRandomJoin ? new DiscoverySession({
|
|
80
|
-
...sessionOptions,
|
|
81
|
-
targetId: crypto.randomBytes(8),
|
|
82
|
-
nodeName: this.config.ownPeerDescriptor.nodeName + '-random'
|
|
83
|
-
}) : null
|
|
84
|
-
this.ongoingDiscoverySessions.set(session.sessionId, session)
|
|
85
|
-
if (randomSession) {
|
|
86
|
-
this.ongoingDiscoverySessions.set(randomSession.sessionId, randomSession)
|
|
83
|
+
newContactListener: (newPeer: RemoteDhtNode) => this.config.addContact(newPeer.getPeerDescriptor())
|
|
87
84
|
}
|
|
85
|
+
return new DiscoverySession(sessionOptions)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private async runSessions(sessions: DiscoverySession[], entryPointDescriptor: PeerDescriptor, retry: boolean): Promise<void> {
|
|
88
89
|
try {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
await
|
|
90
|
+
for (const session of sessions) {
|
|
91
|
+
this.ongoingDiscoverySessions.set(session.sessionId, session)
|
|
92
|
+
await session.findClosestNodes(this.config.joinTimeout)
|
|
92
93
|
}
|
|
93
94
|
} catch (_e) {
|
|
94
95
|
logger.debug(`DHT join on ${this.config.serviceId} timed out`)
|
|
95
96
|
} finally {
|
|
96
|
-
if (!this.
|
|
97
|
+
if (!this.isStopped()) {
|
|
97
98
|
if (this.config.bucket.count() === 0) {
|
|
98
99
|
if (retry) {
|
|
99
100
|
setAbortableTimeout(() => this.rejoinDht(entryPointDescriptor), 1000, this.abortController.signal)
|
|
@@ -102,19 +103,15 @@ export class PeerDiscovery {
|
|
|
102
103
|
await this.ensureRecoveryIntervalIsRunning()
|
|
103
104
|
}
|
|
104
105
|
}
|
|
105
|
-
this.ongoingDiscoverySessions.delete(session.sessionId)
|
|
106
|
-
if (randomSession) {
|
|
107
|
-
this.ongoingDiscoverySessions.delete(randomSession.sessionId)
|
|
108
|
-
}
|
|
109
|
-
this.config.connectionManager?.unlockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
|
|
106
|
+
sessions.forEach((session) => this.ongoingDiscoverySessions.delete(session.sessionId))
|
|
110
107
|
}
|
|
111
108
|
}
|
|
112
109
|
|
|
113
110
|
public async rejoinDht(entryPoint: PeerDescriptor): Promise<void> {
|
|
114
|
-
if (this.
|
|
111
|
+
if (this.isStopped() || this.rejoinOngoing) {
|
|
115
112
|
return
|
|
116
113
|
}
|
|
117
|
-
logger.debug(`Rejoining DHT ${this.config.serviceId}
|
|
114
|
+
logger.debug(`Rejoining DHT ${this.config.serviceId}`)
|
|
118
115
|
this.rejoinOngoing = true
|
|
119
116
|
try {
|
|
120
117
|
this.config.neighborList.clear()
|
|
@@ -122,7 +119,7 @@ export class PeerDiscovery {
|
|
|
122
119
|
logger.debug(`Rejoined DHT successfully ${this.config.serviceId}!`)
|
|
123
120
|
} catch (err) {
|
|
124
121
|
logger.warn(`Rejoining DHT ${this.config.serviceId} failed`)
|
|
125
|
-
if (!this.
|
|
122
|
+
if (!this.isStopped()) {
|
|
126
123
|
setAbortableTimeout(() => this.rejoinDht(entryPoint), 5000, this.abortController.signal)
|
|
127
124
|
}
|
|
128
125
|
} finally {
|
|
@@ -138,10 +135,11 @@ export class PeerDiscovery {
|
|
|
138
135
|
}
|
|
139
136
|
|
|
140
137
|
private async fetchClosestPeersFromBucket(): Promise<void> {
|
|
141
|
-
if (this.
|
|
138
|
+
if (this.isStopped()) {
|
|
142
139
|
return
|
|
143
140
|
}
|
|
144
|
-
|
|
141
|
+
const nodes = this.config.bucket.closest(peerIdFromPeerDescriptor(this.config.ownPeerDescriptor).value, this.config.parallelism)
|
|
142
|
+
await Promise.allSettled(nodes.map(async (peer: RemoteDhtNode) => {
|
|
145
143
|
const contacts = await peer.getClosestPeers(this.config.ownPeerDescriptor.kademliaId)
|
|
146
144
|
contacts.forEach((contact) => {
|
|
147
145
|
this.config.addContact(contact)
|
|
@@ -157,8 +155,11 @@ export class PeerDiscovery {
|
|
|
157
155
|
return this.joinCalled
|
|
158
156
|
}
|
|
159
157
|
|
|
158
|
+
private isStopped() {
|
|
159
|
+
return this.abortController.signal.aborted
|
|
160
|
+
}
|
|
161
|
+
|
|
160
162
|
public stop(): void {
|
|
161
|
-
this.stopped = true
|
|
162
163
|
this.abortController.abort()
|
|
163
164
|
if (this.rejoinTimeoutRef) {
|
|
164
165
|
clearTimeout(this.rejoinTimeoutRef)
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
2
1
|
import { Logger } from '@streamr/utils'
|
|
3
2
|
import EventEmitter from 'eventemitter3'
|
|
4
3
|
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
@@ -20,18 +19,18 @@ const logger = new Logger(module)
|
|
|
20
19
|
|
|
21
20
|
export interface RecursiveFindSessionConfig {
|
|
22
21
|
serviceId: string
|
|
23
|
-
|
|
22
|
+
transport: ITransport
|
|
24
23
|
kademliaIdToFind: Uint8Array
|
|
25
|
-
|
|
24
|
+
ownPeerId: PeerID
|
|
26
25
|
waitedRoutingPathCompletions: number
|
|
27
26
|
mode: FindMode
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
export class RecursiveFindSession extends EventEmitter<RecursiveFindSessionEvents> implements IRecursiveFindSessionService {
|
|
31
30
|
private readonly serviceId: string
|
|
32
|
-
private readonly
|
|
31
|
+
private readonly transport: ITransport
|
|
33
32
|
private readonly kademliaIdToFind: Uint8Array
|
|
34
|
-
private readonly
|
|
33
|
+
private readonly ownPeerId: PeerID
|
|
35
34
|
private readonly waitedRoutingPathCompletions: number
|
|
36
35
|
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
37
36
|
private readonly mode: FindMode
|
|
@@ -46,17 +45,17 @@ export class RecursiveFindSession extends EventEmitter<RecursiveFindSessionEvent
|
|
|
46
45
|
constructor(config: RecursiveFindSessionConfig) {
|
|
47
46
|
super()
|
|
48
47
|
this.serviceId = config.serviceId
|
|
49
|
-
this.
|
|
48
|
+
this.transport = config.transport
|
|
50
49
|
this.kademliaIdToFind = config.kademliaIdToFind
|
|
51
|
-
this.
|
|
50
|
+
this.ownPeerId = config.ownPeerId
|
|
52
51
|
this.waitedRoutingPathCompletions = config.waitedRoutingPathCompletions
|
|
53
52
|
this.results = new SortedContactList(PeerID.fromValue(this.kademliaIdToFind), 10, undefined, true)
|
|
54
53
|
this.mode = config.mode
|
|
55
|
-
this.rpcCommunicator = new ListeningRpcCommunicator(this.serviceId, this.
|
|
54
|
+
this.rpcCommunicator = new ListeningRpcCommunicator(this.serviceId, this.transport, {
|
|
56
55
|
rpcRequestTimeout: 15000
|
|
57
56
|
})
|
|
58
57
|
this.rpcCommunicator.registerRpcNotification(RecursiveFindReport, 'reportRecursiveFindResult',
|
|
59
|
-
(req: RecursiveFindReport
|
|
58
|
+
(req: RecursiveFindReport) => this.reportRecursiveFindResult(req))
|
|
60
59
|
}
|
|
61
60
|
|
|
62
61
|
private isFindCompleted(): boolean {
|
|
@@ -90,7 +89,7 @@ export class RecursiveFindSession extends EventEmitter<RecursiveFindSessionEvent
|
|
|
90
89
|
if (routingPath.length >= 1) {
|
|
91
90
|
this.setHopAsReported(routingPath[routingPath.length - 1])
|
|
92
91
|
}
|
|
93
|
-
nodes.
|
|
92
|
+
nodes.forEach((descriptor: PeerDescriptor) => {
|
|
94
93
|
this.results.addContact(new Contact(descriptor))
|
|
95
94
|
})
|
|
96
95
|
this.processFoundData(dataEntries)
|
|
@@ -102,7 +101,7 @@ export class RecursiveFindSession extends EventEmitter<RecursiveFindSessionEvent
|
|
|
102
101
|
private addKnownHops(routingPath: PeerDescriptor[]) {
|
|
103
102
|
routingPath.forEach((desc) => {
|
|
104
103
|
const newPeerId = PeerID.fromValue(desc.kademliaId)
|
|
105
|
-
if (!this.
|
|
104
|
+
if (!this.ownPeerId.equals(newPeerId)) {
|
|
106
105
|
this.allKnownHops.add(newPeerId.toKey())
|
|
107
106
|
}
|
|
108
107
|
})
|
|
@@ -110,7 +109,7 @@ export class RecursiveFindSession extends EventEmitter<RecursiveFindSessionEvent
|
|
|
110
109
|
|
|
111
110
|
private setHopAsReported(desc: PeerDescriptor) {
|
|
112
111
|
const newPeerId = PeerID.fromValue(desc.kademliaId)
|
|
113
|
-
if (!this.
|
|
112
|
+
if (!this.ownPeerId.equals(newPeerId)) {
|
|
114
113
|
this.reportedHops.add(newPeerId.toKey())
|
|
115
114
|
}
|
|
116
115
|
if (this.isFindCompleted()) {
|
|
@@ -157,7 +156,7 @@ export class RecursiveFindSession extends EventEmitter<RecursiveFindSessionEvent
|
|
|
157
156
|
}
|
|
158
157
|
}
|
|
159
158
|
|
|
160
|
-
public async reportRecursiveFindResult(report: RecursiveFindReport
|
|
159
|
+
public async reportRecursiveFindResult(report: RecursiveFindReport): Promise<Empty> {
|
|
161
160
|
logger.trace('recursiveFindReport arrived: ' + JSON.stringify(report))
|
|
162
161
|
this.doReportRecursiveFindResult(report.routingPath, report.nodes, report.dataEntries, report.noCloserNodesFound)
|
|
163
162
|
return {}
|
|
@@ -12,13 +12,13 @@ import {
|
|
|
12
12
|
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
13
13
|
import { createRouteMessageAck, RoutingErrors, IRouter } from '../routing/Router'
|
|
14
14
|
import { RoutingMode } from '../routing/RoutingSession'
|
|
15
|
-
import { keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
15
|
+
import { areEqualPeerDescriptors, keyFromPeerDescriptor, peerIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
16
16
|
import { Logger, runAndWaitForEvents3 } from '@streamr/utils'
|
|
17
17
|
import { RoutingRpcCommunicator } from '../../transport/RoutingRpcCommunicator'
|
|
18
18
|
import { RemoteRecursiveFindSession } from './RemoteRecursiveFindSession'
|
|
19
19
|
import { v4 } from 'uuid'
|
|
20
20
|
import { RecursiveFindSession, RecursiveFindSessionEvents } from './RecursiveFindSession'
|
|
21
|
-
import {
|
|
21
|
+
import { RemoteDhtNode } from '../RemoteDhtNode'
|
|
22
22
|
import { ITransport } from '../../transport/ITransport'
|
|
23
23
|
import { LocalDataStore } from '../store/LocalDataStore'
|
|
24
24
|
import { IRoutingService } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
@@ -30,10 +30,9 @@ import { SortedContactList } from '../contact/SortedContactList'
|
|
|
30
30
|
interface RecursiveFinderConfig {
|
|
31
31
|
rpcCommunicator: RoutingRpcCommunicator
|
|
32
32
|
sessionTransport: ITransport
|
|
33
|
-
connections: Map<PeerIDKey,
|
|
33
|
+
connections: Map<PeerIDKey, RemoteDhtNode>
|
|
34
34
|
router: IRouter
|
|
35
35
|
ownPeerDescriptor: PeerDescriptor
|
|
36
|
-
ownPeerId: PeerID
|
|
37
36
|
serviceId: string
|
|
38
37
|
localDataStore: LocalDataStore
|
|
39
38
|
addContact: (contact: PeerDescriptor, setActive?: boolean) => void
|
|
@@ -54,10 +53,9 @@ export class RecursiveFinder implements IRecursiveFinder {
|
|
|
54
53
|
|
|
55
54
|
private readonly rpcCommunicator: RoutingRpcCommunicator
|
|
56
55
|
private readonly sessionTransport: ITransport
|
|
57
|
-
private readonly connections: Map<PeerIDKey,
|
|
56
|
+
private readonly connections: Map<PeerIDKey, RemoteDhtNode>
|
|
58
57
|
private readonly router: IRouter
|
|
59
58
|
private readonly ownPeerDescriptor: PeerDescriptor
|
|
60
|
-
private readonly ownPeerId: PeerID
|
|
61
59
|
private readonly serviceId: string
|
|
62
60
|
private readonly localDataStore: LocalDataStore
|
|
63
61
|
private readonly addContact: (contact: PeerDescriptor, setActive?: boolean) => void
|
|
@@ -71,7 +69,6 @@ export class RecursiveFinder implements IRecursiveFinder {
|
|
|
71
69
|
this.connections = config.connections
|
|
72
70
|
this.router = config.router
|
|
73
71
|
this.ownPeerDescriptor = config.ownPeerDescriptor
|
|
74
|
-
this.ownPeerId = config.ownPeerId
|
|
75
72
|
this.serviceId = config.serviceId
|
|
76
73
|
this.localDataStore = config.localDataStore
|
|
77
74
|
this.addContact = config.addContact
|
|
@@ -91,9 +88,9 @@ export class RecursiveFinder implements IRecursiveFinder {
|
|
|
91
88
|
const sessionId = v4()
|
|
92
89
|
const recursiveFindSession = new RecursiveFindSession({
|
|
93
90
|
serviceId: sessionId,
|
|
94
|
-
|
|
91
|
+
transport: this.sessionTransport,
|
|
95
92
|
kademliaIdToFind: idToFind,
|
|
96
|
-
|
|
93
|
+
ownPeerId: peerIdFromPeerDescriptor(this.ownPeerDescriptor),
|
|
97
94
|
waitedRoutingPathCompletions: this.connections.size > 1 ? 2 : 1,
|
|
98
95
|
mode: findMode
|
|
99
96
|
})
|
|
@@ -116,7 +113,7 @@ export class RecursiveFinder implements IRecursiveFinder {
|
|
|
116
113
|
15000
|
|
117
114
|
)
|
|
118
115
|
} catch (err) {
|
|
119
|
-
logger.debug(`doFindRecursively failed with error ${
|
|
116
|
+
logger.debug(`doFindRecursively failed with error ${err}`)
|
|
120
117
|
}
|
|
121
118
|
this.findAndReportLocalData(idToFind, findMode, [], this.ownPeerDescriptor, sessionId)
|
|
122
119
|
this.ongoingSessions.delete(sessionId)
|
|
@@ -186,8 +183,8 @@ export class RecursiveFinder implements IRecursiveFinder {
|
|
|
186
183
|
noCloserNodesFound: boolean = false
|
|
187
184
|
): void {
|
|
188
185
|
const dataEntries = data ? Array.from(data.values(), DataEntry.create.bind(DataEntry)) : []
|
|
189
|
-
const
|
|
190
|
-
if (
|
|
186
|
+
const isOwnNode = areEqualPeerDescriptors(this.ownPeerDescriptor, targetPeerDescriptor)
|
|
187
|
+
if (isOwnNode && this.ongoingSessions.has(serviceId)) {
|
|
191
188
|
this.ongoingSessions.get(serviceId)!
|
|
192
189
|
.doReportRecursiveFindResult(routingPath, closestNodes, dataEntries, noCloserNodesFound)
|
|
193
190
|
} else {
|
|
@@ -206,19 +203,19 @@ export class RecursiveFinder implements IRecursiveFinder {
|
|
|
206
203
|
if (this.stopped) {
|
|
207
204
|
return createRouteMessageAck(routedMessage, 'DhtNode Stopped')
|
|
208
205
|
}
|
|
209
|
-
const idToFind =
|
|
206
|
+
const idToFind = peerIdFromPeerDescriptor(routedMessage.destinationPeer!)
|
|
210
207
|
const msg = routedMessage.message
|
|
211
208
|
const recursiveFindRequest = msg?.body.oneofKind === 'recursiveFindRequest' ? msg.body.recursiveFindRequest : undefined
|
|
212
209
|
const closestPeersToDestination = this.getClosestConnections(routedMessage.destinationPeer!.kademliaId, 5)
|
|
213
210
|
const data = this.findLocalData(idToFind.value, recursiveFindRequest!.findMode)
|
|
214
|
-
if (this.
|
|
211
|
+
if (areEqualPeerDescriptors(this.ownPeerDescriptor, routedMessage.destinationPeer!)) {
|
|
215
212
|
this.reportRecursiveFindResult(routedMessage.routingPath, routedMessage.sourcePeer!, recursiveFindRequest!.recursiveFindSessionId,
|
|
216
213
|
closestPeersToDestination, data, true)
|
|
217
214
|
return createRouteMessageAck(routedMessage)
|
|
218
215
|
}
|
|
219
216
|
const ack = this.router.doRouteMessage(routedMessage, RoutingMode.RECURSIVE_FIND, excludedPeer)
|
|
220
217
|
if (ack.error === RoutingErrors.NO_CANDIDATES_FOUND) {
|
|
221
|
-
logger.trace(`findRecursively Node
|
|
218
|
+
logger.trace(`findRecursively Node found no candidates`)
|
|
222
219
|
this.reportRecursiveFindResult(routedMessage.routingPath, routedMessage.sourcePeer!, recursiveFindRequest!.recursiveFindSessionId,
|
|
223
220
|
closestPeersToDestination, data, true)
|
|
224
221
|
} else if (ack.error) {
|
|
@@ -237,7 +234,7 @@ export class RecursiveFinder implements IRecursiveFinder {
|
|
|
237
234
|
|
|
238
235
|
private getClosestConnections(kademliaId: Uint8Array, limit: number): PeerDescriptor[] {
|
|
239
236
|
const connectedPeers = Array.from(this.connections.values())
|
|
240
|
-
const closestPeers = new SortedContactList<
|
|
237
|
+
const closestPeers = new SortedContactList<RemoteDhtNode>(
|
|
241
238
|
PeerID.fromValue(kademliaId),
|
|
242
239
|
limit,
|
|
243
240
|
undefined,
|
|
@@ -252,13 +249,13 @@ export class RecursiveFinder implements IRecursiveFinder {
|
|
|
252
249
|
async findRecursively(routedMessage: RouteMessageWrapper): Promise<RouteMessageAck> {
|
|
253
250
|
if (this.stopped) {
|
|
254
251
|
return createRouteMessageAck(routedMessage, 'findRecursively() service is not running')
|
|
255
|
-
} else if (this.router.
|
|
252
|
+
} else if (this.router.isMostLikelyDuplicate(routedMessage.requestId)) {
|
|
256
253
|
return createRouteMessageAck(routedMessage, 'message given to findRecursively() service is likely a duplicate')
|
|
257
254
|
}
|
|
258
255
|
const senderKey = keyFromPeerDescriptor(routedMessage.previousPeer || routedMessage.sourcePeer!)
|
|
259
|
-
logger.trace(`
|
|
256
|
+
logger.trace(`Received findRecursively call from ${senderKey}`)
|
|
260
257
|
this.addContact(routedMessage.sourcePeer!, true)
|
|
261
|
-
this.router.addToDuplicateDetector(routedMessage.requestId
|
|
258
|
+
this.router.addToDuplicateDetector(routedMessage.requestId)
|
|
262
259
|
return this.doFindRecursevily(routedMessage)
|
|
263
260
|
}
|
|
264
261
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DhtNode } from '../
|
|
1
|
+
import { DhtNode } from '../dht/DhtNode'
|
|
2
2
|
import { ExternalStoreDataRequest, ExternalStoreDataResponse, FindDataRequest, FindDataResponse, FindMode } from '../proto/packages/dht/protos/DhtRpc'
|
|
3
3
|
|
|
4
4
|
export const registerExternalApiRpcMethods = (thisNode: DhtNode): void => {
|