@streamr/dht 0.0.1-tatum.6 → 0.0.1-tatum.8
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/ConnectionLockRpcRemote.d.ts +10 -0
- package/dist/src/connection/{RemoteConnectionLocker.js → ConnectionLockRpcRemote.js} +21 -35
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -0
- package/dist/src/connection/ConnectionManager.d.ts +17 -41
- package/dist/src/connection/ConnectionManager.js +148 -212
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectivityChecker.js +16 -13
- package/dist/src/connection/ConnectivityChecker.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +49 -0
- package/dist/src/connection/ConnectorFacade.js +83 -0
- package/dist/src/connection/ConnectorFacade.js.map +1 -0
- package/dist/src/connection/Handshaker.d.ts +1 -2
- package/dist/src/connection/Handshaker.js +1 -4
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +2 -6
- package/dist/src/connection/ManagedConnection.js +27 -36
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/ManagedWebRtcConnection.d.ts +1 -1
- package/dist/src/connection/ManagedWebRtcConnection.js +2 -2
- package/dist/src/connection/ManagedWebRtcConnection.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 -3
- package/dist/src/connection/Simulator/SimulatorConnector.js +12 -14
- 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 +3 -1
- 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 → WebRtcConnectorRpcLocal.d.ts} +13 -13
- package/dist/src/connection/WebRTC/{WebRtcConnector.js → WebRtcConnectorRpcLocal.js} +46 -38
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcRemote.d.ts +11 -0
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcRemote.js +55 -0
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcRemote.js.map +1 -0
- package/dist/src/connection/WebRTC/iceServerAsString.d.ts +1 -1
- package/dist/src/connection/WebSocket/{WebSocketConnector.d.ts → WebSocketConnectorRpcLocal.d.ts} +21 -11
- package/dist/src/connection/WebSocket/{WebSocketConnector.js → WebSocketConnectorRpcLocal.js} +80 -59
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcRemote.d.ts +8 -0
- package/dist/src/connection/WebSocket/{RemoteWebSocketConnector.js → WebSocketConnectorRpcRemote.js} +12 -16
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcRemote.js.map +1 -0
- 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 +18 -55
- package/dist/src/dht/DhtNode.js +122 -145
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/{RemoteExternalApi.d.ts → ExternalApiRpcRemote.d.ts} +2 -2
- package/dist/src/dht/{RemoteExternalApi.js → ExternalApiRpcRemote.js} +5 -6
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -0
- 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 +12 -11
- package/dist/src/dht/discovery/PeerDiscovery.js +33 -37
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/find/RecursiveFindSession.d.ts +9 -10
- package/dist/src/dht/find/RecursiveFindSession.js +13 -13
- package/dist/src/dht/find/RecursiveFindSession.js.map +1 -1
- package/dist/src/dht/find/RecursiveFinder.d.ts +9 -11
- package/dist/src/dht/find/RecursiveFinder.js +35 -36
- package/dist/src/dht/find/RecursiveFinder.js.map +1 -1
- package/dist/src/dht/find/RemoteRecursiveFindSession.d.ts +1 -1
- package/dist/src/dht/find/RemoteRecursiveFindSession.js +4 -4
- package/dist/src/dht/find/RemoteRecursiveFindSession.js.map +1 -1
- package/dist/src/dht/registerExternalApiRpcMethods.d.ts +1 -1
- package/dist/src/dht/registerExternalApiRpcMethods.js +4 -3
- package/dist/src/dht/registerExternalApiRpcMethods.js.map +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/Router.d.ts +12 -15
- package/dist/src/dht/routing/Router.js +30 -33
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/{RemoteRouter.d.ts → RouterRpcRemote.d.ts} +2 -2
- package/dist/src/dht/routing/{RemoteRouter.js → RouterRpcRemote.js} +5 -5
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -0
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -4
- package/dist/src/dht/routing/RoutingSession.js +6 -5
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/{DataStore.d.ts → StoreRpcLocal.d.ts} +6 -6
- package/dist/src/dht/store/{DataStore.js → StoreRpcLocal.js} +34 -34
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -0
- package/dist/src/dht/store/{RemoteStore.d.ts → StoreRpcRemote.d.ts} +2 -2
- package/dist/src/dht/store/{RemoteStore.js → StoreRpcRemote.js} +4 -4
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -0
- package/dist/src/exports.d.ts +2 -8
- 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/helpers/protoClasses.js +2 -2
- package/dist/src/helpers/protoClasses.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +29 -29
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +39 -39
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +49 -162
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +47 -88
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +15 -15
- package/dist/src/transport/RoutingRpcCommunicator.js +1 -0
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/package.json +10 -9
- package/protos/DhtRpc.proto +30 -60
- package/src/connection/ConnectionLockHandler.ts +1 -1
- package/src/connection/ConnectionLockRpcRemote.ts +62 -0
- package/src/connection/ConnectionManager.ts +178 -274
- package/src/connection/ConnectivityChecker.ts +15 -12
- package/src/connection/ConnectorFacade.ts +140 -0
- package/src/connection/Handshaker.ts +0 -5
- package/src/connection/ManagedConnection.ts +26 -40
- package/src/connection/ManagedWebRtcConnection.ts +0 -2
- package/src/connection/Simulator/Simulator.ts +0 -7
- package/src/connection/Simulator/SimulatorConnection.ts +16 -13
- package/src/connection/Simulator/SimulatorConnector.ts +13 -19
- package/src/connection/Simulator/SimulatorTransport.ts +6 -1
- package/src/connection/WebRTC/NodeWebRtcConnection.ts +15 -14
- package/src/connection/WebRTC/{WebRtcConnector.ts → WebRtcConnectorRpcLocal.ts} +68 -56
- package/src/connection/WebRTC/WebRtcConnectorRpcRemote.ts +71 -0
- package/src/connection/WebRTC/iceServerAsString.ts +1 -1
- package/src/connection/WebSocket/{WebSocketConnector.ts → WebSocketConnectorRpcLocal.ts} +102 -79
- package/src/connection/WebSocket/WebSocketConnectorRpcRemote.ts +45 -0
- package/src/connection/WebSocket/WebSocketServer.ts +26 -8
- package/src/dht/DhtNode.ts +176 -203
- package/src/dht/{RemoteExternalApi.ts → ExternalApiRpcRemote.ts} +3 -4
- 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 +47 -45
- package/src/dht/find/RecursiveFindSession.ts +21 -22
- package/src/dht/find/RecursiveFinder.ts +45 -49
- package/src/dht/find/RemoteRecursiveFindSession.ts +6 -6
- package/src/dht/registerExternalApiRpcMethods.ts +8 -5
- package/src/dht/routing/DuplicateDetector.ts +3 -10
- package/src/dht/routing/Router.ts +39 -45
- package/src/dht/routing/{RemoteRouter.ts → RouterRpcRemote.ts} +4 -4
- package/src/dht/routing/RoutingSession.ts +15 -15
- package/src/dht/store/{DataStore.ts → StoreRpcLocal.ts} +42 -42
- package/src/dht/store/{RemoteStore.ts → StoreRpcRemote.ts} +2 -2
- package/src/exports.ts +2 -8
- 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/helpers/protoClasses.ts +4 -4
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +54 -54
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +15 -15
- package/src/proto/packages/dht/protos/DhtRpc.ts +77 -216
- package/src/transport/RoutingRpcCommunicator.ts +1 -0
- 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/RouteMessage.test.ts +7 -9
- package/test/integration/{RemoteRouter.test.ts → RouterRpcRemote.test.ts} +13 -14
- 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/{RemoteStore.test.ts → StoreRpcRemote.test.ts} +17 -18
- package/test/integration/WebRtcConnectionManagement.test.ts +26 -19
- package/test/integration/WebRtcConnectorRpc.test.ts +17 -32
- package/test/integration/WebSocket.test.ts +4 -2
- package/test/integration/WebSocketConnectionManagement.test.ts +30 -17
- package/test/integration/WebSocketConnectorRpc.test.ts +10 -15
- 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 +13 -18
- package/test/unit/Router.test.ts +18 -21
- package/test/unit/WebSocketConnectorRpcLocal.test.ts +64 -0
- package/test/unit/WebSocketServer.test.ts +24 -12
- package/test/unit/{webrtcReplaceInternalIpWithExternalIp.ts → webrtcReplaceInternalIpWithExternalIp.test.ts} +1 -1
- 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 +55 -74
- package/dist/src/connection/RemoteConnectionLocker.d.ts +0 -13
- package/dist/src/connection/RemoteConnectionLocker.js.map +0 -1
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.d.ts +0 -12
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js +0 -74
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js.map +0 -1
- package/dist/src/connection/WebRTC/WebRtcConnector.js.map +0 -1
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.d.ts +0 -9
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js.map +0 -1
- package/dist/src/connection/WebSocket/WebSocketConnector.js.map +0 -1
- package/dist/src/dht/DhtPeer.js.map +0 -1
- package/dist/src/dht/RemoteExternalApi.js.map +0 -1
- package/dist/src/dht/routing/RemoteRouter.js.map +0 -1
- package/dist/src/dht/store/DataStore.js.map +0 -1
- package/dist/src/dht/store/RemoteStore.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/connection/RemoteConnectionLocker.ts +0 -84
- package/src/connection/WebRTC/RemoteWebrtcConnector.ts +0 -93
- package/src/connection/WebSocket/RemoteWebSocketConnector.ts +0 -49
- 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
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { Any } from '../proto/google/protobuf/any'
|
|
2
2
|
import { DataEntry, ExternalStoreDataRequest, FindDataRequest, PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
|
|
3
|
-
import {
|
|
3
|
+
import { IExternalApiRpcClient } from '../proto/packages/dht/protos/DhtRpc.client'
|
|
4
4
|
import { Remote } from './contact/Remote'
|
|
5
5
|
|
|
6
|
-
export class
|
|
6
|
+
export class ExternalApiRpcRemote extends Remote<IExternalApiRpcClient> {
|
|
7
7
|
|
|
8
8
|
async findData(idToFind: Uint8Array): Promise<DataEntry[]> {
|
|
9
9
|
const request: FindDataRequest = {
|
|
10
|
-
kademliaId: idToFind
|
|
11
|
-
requestor: this.getLocalPeerDescriptor(),
|
|
10
|
+
kademliaId: idToFind
|
|
12
11
|
}
|
|
13
12
|
const options = this.formDhtRpcOptions({
|
|
14
13
|
timeout: 10000
|
|
@@ -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,27 +1,26 @@
|
|
|
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
|
+
peerDiscoveryQueryBatchSize: number
|
|
25
24
|
serviceId: string
|
|
26
25
|
parallelism: number
|
|
27
26
|
joinTimeout: number
|
|
@@ -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,48 @@ 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
|
|
62
|
+
const targetId = peerIdFromPeerDescriptor(this.config.ownPeerDescriptor).value
|
|
63
|
+
const closest = this.config.bucket.closest(targetId, this.config.peerDiscoveryQueryBatchSize)
|
|
65
64
|
this.config.neighborList.addContacts(closest)
|
|
65
|
+
const sessions = [this.createSession(targetId)]
|
|
66
|
+
if (doAdditionalRandomPeerDiscovery) {
|
|
67
|
+
sessions.push(this.createSession(createRandomKademliaId()))
|
|
68
|
+
}
|
|
69
|
+
await this.runSessions(sessions, entryPointDescriptor, retry)
|
|
70
|
+
this.config.connectionManager?.unlockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
|
|
71
|
+
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private createSession(targetId: Uint8Array): DiscoverySession {
|
|
66
75
|
const sessionOptions = {
|
|
67
76
|
bucket: this.config.bucket,
|
|
68
77
|
neighborList: this.config.neighborList,
|
|
69
|
-
targetId
|
|
78
|
+
targetId,
|
|
70
79
|
ownPeerDescriptor: this.config.ownPeerDescriptor,
|
|
71
80
|
serviceId: this.config.serviceId,
|
|
72
81
|
rpcCommunicator: this.config.rpcCommunicator,
|
|
73
82
|
parallelism: this.config.parallelism,
|
|
74
83
|
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)
|
|
84
|
+
newContactListener: (newPeer: RemoteDhtNode) => this.config.addContact(newPeer.getPeerDescriptor())
|
|
87
85
|
}
|
|
86
|
+
return new DiscoverySession(sessionOptions)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private async runSessions(sessions: DiscoverySession[], entryPointDescriptor: PeerDescriptor, retry: boolean): Promise<void> {
|
|
88
90
|
try {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
await
|
|
91
|
+
for (const session of sessions) {
|
|
92
|
+
this.ongoingDiscoverySessions.set(session.sessionId, session)
|
|
93
|
+
await session.findClosestNodes(this.config.joinTimeout)
|
|
92
94
|
}
|
|
93
95
|
} catch (_e) {
|
|
94
96
|
logger.debug(`DHT join on ${this.config.serviceId} timed out`)
|
|
95
97
|
} finally {
|
|
96
|
-
if (!this.
|
|
98
|
+
if (!this.isStopped()) {
|
|
97
99
|
if (this.config.bucket.count() === 0) {
|
|
98
100
|
if (retry) {
|
|
99
101
|
setAbortableTimeout(() => this.rejoinDht(entryPointDescriptor), 1000, this.abortController.signal)
|
|
@@ -102,19 +104,15 @@ export class PeerDiscovery {
|
|
|
102
104
|
await this.ensureRecoveryIntervalIsRunning()
|
|
103
105
|
}
|
|
104
106
|
}
|
|
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`)
|
|
107
|
+
sessions.forEach((session) => this.ongoingDiscoverySessions.delete(session.sessionId))
|
|
110
108
|
}
|
|
111
109
|
}
|
|
112
110
|
|
|
113
111
|
public async rejoinDht(entryPoint: PeerDescriptor): Promise<void> {
|
|
114
|
-
if (this.
|
|
112
|
+
if (this.isStopped() || this.rejoinOngoing) {
|
|
115
113
|
return
|
|
116
114
|
}
|
|
117
|
-
logger.debug(`Rejoining DHT ${this.config.serviceId}
|
|
115
|
+
logger.debug(`Rejoining DHT ${this.config.serviceId}`)
|
|
118
116
|
this.rejoinOngoing = true
|
|
119
117
|
try {
|
|
120
118
|
this.config.neighborList.clear()
|
|
@@ -122,7 +120,7 @@ export class PeerDiscovery {
|
|
|
122
120
|
logger.debug(`Rejoined DHT successfully ${this.config.serviceId}!`)
|
|
123
121
|
} catch (err) {
|
|
124
122
|
logger.warn(`Rejoining DHT ${this.config.serviceId} failed`)
|
|
125
|
-
if (!this.
|
|
123
|
+
if (!this.isStopped()) {
|
|
126
124
|
setAbortableTimeout(() => this.rejoinDht(entryPoint), 5000, this.abortController.signal)
|
|
127
125
|
}
|
|
128
126
|
} finally {
|
|
@@ -138,10 +136,11 @@ export class PeerDiscovery {
|
|
|
138
136
|
}
|
|
139
137
|
|
|
140
138
|
private async fetchClosestPeersFromBucket(): Promise<void> {
|
|
141
|
-
if (this.
|
|
139
|
+
if (this.isStopped()) {
|
|
142
140
|
return
|
|
143
141
|
}
|
|
144
|
-
|
|
142
|
+
const nodes = this.config.bucket.closest(peerIdFromPeerDescriptor(this.config.ownPeerDescriptor).value, this.config.parallelism)
|
|
143
|
+
await Promise.allSettled(nodes.map(async (peer: RemoteDhtNode) => {
|
|
145
144
|
const contacts = await peer.getClosestPeers(this.config.ownPeerDescriptor.kademliaId)
|
|
146
145
|
contacts.forEach((contact) => {
|
|
147
146
|
this.config.addContact(contact)
|
|
@@ -157,8 +156,11 @@ export class PeerDiscovery {
|
|
|
157
156
|
return this.joinCalled
|
|
158
157
|
}
|
|
159
158
|
|
|
159
|
+
private isStopped() {
|
|
160
|
+
return this.abortController.signal.aborted
|
|
161
|
+
}
|
|
162
|
+
|
|
160
163
|
public stop(): void {
|
|
161
|
-
this.stopped = true
|
|
162
164
|
this.abortController.abort()
|
|
163
165
|
if (this.rejoinTimeoutRef) {
|
|
164
166
|
clearTimeout(this.rejoinTimeoutRef)
|
|
@@ -1,8 +1,7 @@
|
|
|
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'
|
|
5
|
-
import { DataEntry,
|
|
4
|
+
import { DataEntry, PeerDescriptor, FindResponse } from '../../proto/packages/dht/protos/DhtRpc'
|
|
6
5
|
import { IRecursiveFindSessionService } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
7
6
|
import { Empty } from '../../proto/google/protobuf/empty'
|
|
8
7
|
import { ITransport } from '../../transport/ITransport'
|
|
@@ -20,21 +19,21 @@ 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
|
+
fetchData: boolean
|
|
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
|
-
private readonly
|
|
36
|
+
private readonly fetchData: boolean
|
|
38
37
|
private results: SortedContactList<Contact>
|
|
39
38
|
private foundData: Map<string, DataEntry> = new Map()
|
|
40
39
|
private allKnownHops: Set<PeerIDKey> = new Set()
|
|
@@ -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
|
-
this.
|
|
55
|
-
this.rpcCommunicator = new ListeningRpcCommunicator(this.serviceId, this.
|
|
53
|
+
this.fetchData = config.fetchData
|
|
54
|
+
this.rpcCommunicator = new ListeningRpcCommunicator(this.serviceId, this.transport, {
|
|
56
55
|
rpcRequestTimeout: 15000
|
|
57
56
|
})
|
|
58
|
-
this.rpcCommunicator.registerRpcNotification(
|
|
59
|
-
(req:
|
|
57
|
+
this.rpcCommunicator.registerRpcNotification(FindResponse, 'sendFindResponse',
|
|
58
|
+
(req: FindResponse) => this.sendFindResponse(req))
|
|
60
59
|
}
|
|
61
60
|
|
|
62
61
|
private isFindCompleted(): boolean {
|
|
@@ -65,10 +64,10 @@ export class RecursiveFindSession extends EventEmitter<RecursiveFindSessionEvent
|
|
|
65
64
|
unreportedHops.delete(id)
|
|
66
65
|
})
|
|
67
66
|
if (this.noCloserNodesReceivedCounter >= 1 && unreportedHops.size === 0) {
|
|
68
|
-
if (this.
|
|
67
|
+
if (this.fetchData
|
|
69
68
|
&& (this.hasNonStaleData() || this.noCloserNodesReceivedCounter >= this.waitedRoutingPathCompletions)) {
|
|
70
69
|
return true
|
|
71
|
-
} else if (this.
|
|
70
|
+
} else if (this.fetchData) {
|
|
72
71
|
return false
|
|
73
72
|
}
|
|
74
73
|
return true
|
|
@@ -80,7 +79,7 @@ export class RecursiveFindSession extends EventEmitter<RecursiveFindSessionEvent
|
|
|
80
79
|
return Array.from(this.foundData.values()).some((entry) => entry.stale === false)
|
|
81
80
|
}
|
|
82
81
|
|
|
83
|
-
public
|
|
82
|
+
public doSendFindResponse(
|
|
84
83
|
routingPath: PeerDescriptor[],
|
|
85
84
|
nodes: PeerDescriptor[],
|
|
86
85
|
dataEntries: DataEntry[],
|
|
@@ -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,9 +156,9 @@ export class RecursiveFindSession extends EventEmitter<RecursiveFindSessionEvent
|
|
|
157
156
|
}
|
|
158
157
|
}
|
|
159
158
|
|
|
160
|
-
public async
|
|
159
|
+
public async sendFindResponse(report: FindResponse): Promise<Empty> {
|
|
161
160
|
logger.trace('recursiveFindReport arrived: ' + JSON.stringify(report))
|
|
162
|
-
this.
|
|
161
|
+
this.doSendFindResponse(report.routingPath, report.closestConnectedPeers, report.dataEntries, report.noCloserNodesFound)
|
|
163
162
|
return {}
|
|
164
163
|
}
|
|
165
164
|
|