@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
package/src/dht/find/Finder.ts
CHANGED
|
@@ -6,13 +6,15 @@ import {
|
|
|
6
6
|
PeerDescriptor,
|
|
7
7
|
FindRequest,
|
|
8
8
|
RouteMessageAck,
|
|
9
|
-
RouteMessageWrapper
|
|
9
|
+
RouteMessageWrapper,
|
|
10
|
+
RouteMessageError,
|
|
11
|
+
FindAction
|
|
10
12
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
11
13
|
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
12
|
-
import {
|
|
14
|
+
import { IRouter } from '../routing/Router'
|
|
13
15
|
import { RoutingMode } from '../routing/RoutingSession'
|
|
14
16
|
import { areEqualPeerDescriptors, peerIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
15
|
-
import { Logger, runAndWaitForEvents3 } from '@streamr/utils'
|
|
17
|
+
import { Logger, runAndWaitForEvents3, wait } from '@streamr/utils'
|
|
16
18
|
import { RoutingRpcCommunicator } from '../../transport/RoutingRpcCommunicator'
|
|
17
19
|
import { FindSessionRpcRemote } from './FindSessionRpcRemote'
|
|
18
20
|
import { v4 } from 'uuid'
|
|
@@ -37,12 +39,12 @@ interface FinderConfig {
|
|
|
37
39
|
localPeerDescriptor: PeerDescriptor
|
|
38
40
|
serviceId: ServiceID
|
|
39
41
|
localDataStore: LocalDataStore
|
|
40
|
-
addContact: (contact: PeerDescriptor
|
|
42
|
+
addContact: (contact: PeerDescriptor) => void
|
|
41
43
|
isPeerCloserToIdThanSelf: (peer1: PeerDescriptor, compareToId: PeerID) => boolean
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
export interface IFinder {
|
|
45
|
-
startFind(idToFind: Uint8Array,
|
|
47
|
+
startFind(idToFind: Uint8Array, action?: FindAction): Promise<FindResult>
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
export interface FindResult { closestNodes: Array<PeerDescriptor>, dataEntries?: Array<DataEntry> }
|
|
@@ -77,7 +79,7 @@ export class Finder implements IFinder {
|
|
|
77
79
|
private registerLocalRpcMethods(config: FinderConfig) {
|
|
78
80
|
const rpcLocal = new FindRpcLocal({
|
|
79
81
|
doRouteFindRequest: (routedMessage: RouteMessageWrapper) => this.doRouteFindRequest(routedMessage),
|
|
80
|
-
addContact: (contact: PeerDescriptor
|
|
82
|
+
addContact: (contact: PeerDescriptor) => config.addContact(contact),
|
|
81
83
|
isMostLikelyDuplicate: (requestId: string) => this.router.isMostLikelyDuplicate(requestId),
|
|
82
84
|
addToDuplicateDetector: (requestId: string) => this.router.addToDuplicateDetector(requestId)
|
|
83
85
|
})
|
|
@@ -87,7 +89,7 @@ export class Finder implements IFinder {
|
|
|
87
89
|
'routeFindRequest',
|
|
88
90
|
async (routedMessage: RouteMessageWrapper) => {
|
|
89
91
|
if (this.stopped) {
|
|
90
|
-
return createRouteMessageAck(routedMessage,
|
|
92
|
+
return createRouteMessageAck(routedMessage, RouteMessageError.STOPPED)
|
|
91
93
|
} else {
|
|
92
94
|
return rpcLocal.routeFindRequest(routedMessage)
|
|
93
95
|
}
|
|
@@ -97,8 +99,9 @@ export class Finder implements IFinder {
|
|
|
97
99
|
|
|
98
100
|
public async startFind(
|
|
99
101
|
idToFind: Uint8Array,
|
|
100
|
-
|
|
101
|
-
excludedPeer?: PeerDescriptor
|
|
102
|
+
action: FindAction = FindAction.NODE,
|
|
103
|
+
excludedPeer?: PeerDescriptor,
|
|
104
|
+
waitForCompletion = true
|
|
102
105
|
): Promise<FindResult> {
|
|
103
106
|
if (this.stopped) {
|
|
104
107
|
return { closestNodes: [] }
|
|
@@ -107,46 +110,57 @@ export class Finder implements IFinder {
|
|
|
107
110
|
const session = new FindSession({
|
|
108
111
|
serviceId: sessionId,
|
|
109
112
|
transport: this.sessionTransport,
|
|
110
|
-
|
|
113
|
+
nodeIdToFind: idToFind,
|
|
111
114
|
localPeerId: peerIdFromPeerDescriptor(this.localPeerDescriptor),
|
|
112
115
|
waitedRoutingPathCompletions: this.connections.size > 1 ? 2 : 1,
|
|
113
|
-
|
|
116
|
+
action
|
|
114
117
|
})
|
|
115
118
|
if (this.connections.size === 0) {
|
|
116
119
|
const data = this.localDataStore.getEntry(PeerID.fromValue(idToFind))
|
|
117
120
|
session.doSendFindResponse(
|
|
118
121
|
[this.localPeerDescriptor],
|
|
119
122
|
[this.localPeerDescriptor],
|
|
120
|
-
|
|
123
|
+
Array.from(data.values()),
|
|
121
124
|
true
|
|
122
125
|
)
|
|
123
126
|
return session.getResults()
|
|
124
127
|
}
|
|
125
|
-
const routeMessage = this.wrapFindRequest(idToFind, sessionId,
|
|
128
|
+
const routeMessage = this.wrapFindRequest(idToFind, sessionId, action)
|
|
126
129
|
this.ongoingSessions.set(sessionId, session)
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
130
|
+
if (waitForCompletion === true) {
|
|
131
|
+
try {
|
|
132
|
+
await runAndWaitForEvents3<FindSessionEvents>(
|
|
133
|
+
[() => this.doRouteFindRequest(routeMessage, excludedPeer)],
|
|
134
|
+
[[session, 'findCompleted']],
|
|
135
|
+
15000
|
|
136
|
+
)
|
|
137
|
+
} catch (err) {
|
|
138
|
+
logger.debug(`doRouteFindRequest failed with error ${err}`)
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
this.doRouteFindRequest(routeMessage, excludedPeer)
|
|
142
|
+
// Wait for delete operation to be sent out by the router
|
|
143
|
+
// TODO: Add a feature to wait for the router to pass the message?
|
|
144
|
+
await wait(50)
|
|
145
|
+
}
|
|
146
|
+
if (action === FindAction.FETCH_DATA) {
|
|
147
|
+
this.findAndReportLocalData(idToFind, [], this.localPeerDescriptor, sessionId)
|
|
148
|
+
} else if (action === FindAction.DELETE_DATA) {
|
|
149
|
+
this.localDataStore.markAsDeleted(idToFind, peerIdFromPeerDescriptor(this.localPeerDescriptor))
|
|
135
150
|
}
|
|
136
|
-
this.findAndReportLocalData(idToFind, fetchData, [], this.localPeerDescriptor, sessionId)
|
|
137
151
|
this.ongoingSessions.delete(sessionId)
|
|
138
152
|
session.stop()
|
|
139
153
|
return session.getResults()
|
|
140
154
|
}
|
|
141
155
|
|
|
142
|
-
private wrapFindRequest(idToFind: Uint8Array, sessionId: string,
|
|
156
|
+
private wrapFindRequest(idToFind: Uint8Array, sessionId: string, action: FindAction): RouteMessageWrapper {
|
|
143
157
|
const targetDescriptor: PeerDescriptor = {
|
|
144
|
-
|
|
158
|
+
nodeId: idToFind,
|
|
145
159
|
type: NodeType.VIRTUAL
|
|
146
160
|
}
|
|
147
161
|
const request: FindRequest = {
|
|
148
162
|
sessionId,
|
|
149
|
-
|
|
163
|
+
action
|
|
150
164
|
}
|
|
151
165
|
const msg: Message = {
|
|
152
166
|
messageType: MessageType.FIND_REQUEST,
|
|
@@ -170,19 +184,14 @@ export class Finder implements IFinder {
|
|
|
170
184
|
|
|
171
185
|
private findAndReportLocalData(
|
|
172
186
|
idToFind: Uint8Array,
|
|
173
|
-
fetchData: boolean,
|
|
174
187
|
routingPath: PeerDescriptor[],
|
|
175
188
|
sourcePeer: PeerDescriptor,
|
|
176
189
|
sessionId: string
|
|
177
|
-
):
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
this.sendFindResponse(routingPath, sourcePeer, sessionId, [], data, true)
|
|
182
|
-
return true
|
|
183
|
-
}
|
|
190
|
+
): void {
|
|
191
|
+
const data = this.localDataStore.getEntry(PeerID.fromValue(idToFind))
|
|
192
|
+
if (data.size > 0) {
|
|
193
|
+
this.sendFindResponse(routingPath, sourcePeer, sessionId, [], data, true)
|
|
184
194
|
}
|
|
185
|
-
return false
|
|
186
195
|
}
|
|
187
196
|
|
|
188
197
|
private findLocalData(idToFind: Uint8Array, fetchData: boolean): Map<PeerIDKey, DataEntry> | undefined {
|
|
@@ -221,46 +230,57 @@ export class Finder implements IFinder {
|
|
|
221
230
|
|
|
222
231
|
private doRouteFindRequest(routedMessage: RouteMessageWrapper, excludedPeer?: PeerDescriptor): RouteMessageAck {
|
|
223
232
|
if (this.stopped) {
|
|
224
|
-
return createRouteMessageAck(routedMessage,
|
|
233
|
+
return createRouteMessageAck(routedMessage, RouteMessageError.STOPPED)
|
|
225
234
|
}
|
|
226
235
|
const idToFind = peerIdFromPeerDescriptor(routedMessage.destinationPeer!)
|
|
227
236
|
const msg = routedMessage.message
|
|
228
237
|
const findRequest = msg?.body.oneofKind === 'findRequest' ? msg.body.findRequest : undefined
|
|
229
|
-
const closestPeersToDestination = this.getClosestConnections(routedMessage.destinationPeer!.
|
|
230
|
-
const data = this.findLocalData(idToFind.value, findRequest!.
|
|
238
|
+
const closestPeersToDestination = this.getClosestConnections(routedMessage.destinationPeer!.nodeId, 5)
|
|
239
|
+
const data = this.findLocalData(idToFind.value, findRequest!.action === FindAction.FETCH_DATA)
|
|
240
|
+
if (findRequest!.action === FindAction.DELETE_DATA) {
|
|
241
|
+
this.localDataStore.markAsDeleted(idToFind.value, peerIdFromPeerDescriptor(routedMessage.sourcePeer!))
|
|
242
|
+
}
|
|
231
243
|
if (areEqualPeerDescriptors(this.localPeerDescriptor, routedMessage.destinationPeer!)) {
|
|
232
|
-
this
|
|
233
|
-
|
|
244
|
+
// TODO this is also very similar case to what we do at line 255, could simplify the code paths?
|
|
245
|
+
this.sendFindResponse(
|
|
246
|
+
routedMessage.routingPath,
|
|
247
|
+
routedMessage.sourcePeer!,
|
|
248
|
+
findRequest!.sessionId,
|
|
249
|
+
closestPeersToDestination,
|
|
250
|
+
data,
|
|
251
|
+
true
|
|
252
|
+
)
|
|
234
253
|
return createRouteMessageAck(routedMessage)
|
|
235
|
-
}
|
|
236
|
-
const ack = this.router.doRouteMessage(routedMessage, RoutingMode.FIND, excludedPeer)
|
|
237
|
-
if (ack.error === RoutingErrors.NO_CANDIDATES_FOUND) {
|
|
238
|
-
logger.trace(`routeFindRequest Node found no candidates`)
|
|
239
|
-
this.sendFindResponse(routedMessage.routingPath, routedMessage.sourcePeer!, findRequest!.sessionId,
|
|
240
|
-
closestPeersToDestination, data, true)
|
|
241
|
-
} else if (ack.error) {
|
|
242
|
-
return ack
|
|
243
254
|
} else {
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
255
|
+
const ack = this.router.doRouteMessage(routedMessage, RoutingMode.FIND, excludedPeer)
|
|
256
|
+
if ((ack.error === undefined) || (ack.error === RouteMessageError.NO_TARGETS)) {
|
|
257
|
+
const noCloserContactsFound = (ack.error === RouteMessageError.NO_TARGETS) ||
|
|
258
|
+
(
|
|
259
|
+
closestPeersToDestination.length > 0
|
|
260
|
+
&& getPreviousPeer(routedMessage)
|
|
261
|
+
&& !this.isPeerCloserToIdThanSelf(closestPeersToDestination[0], idToFind)
|
|
262
|
+
)
|
|
263
|
+
this.sendFindResponse(
|
|
264
|
+
routedMessage.routingPath,
|
|
265
|
+
routedMessage.sourcePeer!,
|
|
266
|
+
findRequest!.sessionId,
|
|
267
|
+
closestPeersToDestination,
|
|
268
|
+
data,
|
|
269
|
+
noCloserContactsFound
|
|
270
|
+
)
|
|
271
|
+
}
|
|
272
|
+
return ack
|
|
273
|
+
}
|
|
253
274
|
}
|
|
254
275
|
|
|
255
|
-
private getClosestConnections(
|
|
276
|
+
private getClosestConnections(nodeId: Uint8Array, limit: number): PeerDescriptor[] {
|
|
256
277
|
const connectedPeers = Array.from(this.connections.values())
|
|
257
|
-
const closestPeers = new SortedContactList<DhtNodeRpcRemote>(
|
|
258
|
-
PeerID.fromValue(
|
|
259
|
-
limit,
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
)
|
|
278
|
+
const closestPeers = new SortedContactList<DhtNodeRpcRemote>({
|
|
279
|
+
referenceId: PeerID.fromValue(nodeId),
|
|
280
|
+
maxSize: limit,
|
|
281
|
+
allowToContainReferenceId: true,
|
|
282
|
+
emitEvents: false
|
|
283
|
+
})
|
|
264
284
|
closestPeers.addContacts(connectedPeers)
|
|
265
285
|
return closestPeers.getClosestContacts(limit).map((peer) => peer.getPeerDescriptor())
|
|
266
286
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
2
2
|
import { v4 } from 'uuid'
|
|
3
3
|
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
4
|
-
import {
|
|
4
|
+
import { RpcRemote } from '../contact/RpcRemote'
|
|
5
5
|
import { Logger } from '@streamr/utils'
|
|
6
6
|
import { IFindRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
7
7
|
import { getPreviousPeer } from './getPreviousPeer'
|
|
8
8
|
|
|
9
9
|
const logger = new Logger(module)
|
|
10
10
|
|
|
11
|
-
export class FindRpcRemote extends
|
|
11
|
+
export class FindRpcRemote extends RpcRemote<IFindRpcClient> {
|
|
12
12
|
|
|
13
13
|
async routeFindRequest(params: RouteMessageWrapper): Promise<boolean> {
|
|
14
14
|
const message: RouteMessageWrapper = {
|
|
@@ -19,11 +19,13 @@ export class FindRpcRemote extends Remote<IFindRpcClient> {
|
|
|
19
19
|
reachableThrough: params.reachableThrough ?? [],
|
|
20
20
|
routingPath: params.routingPath
|
|
21
21
|
}
|
|
22
|
-
const options = this.formDhtRpcOptions(
|
|
22
|
+
const options = this.formDhtRpcOptions({
|
|
23
|
+
doNotConnect: true
|
|
24
|
+
})
|
|
23
25
|
try {
|
|
24
26
|
const ack = await this.getClient().routeFindRequest(message, options)
|
|
25
|
-
if (ack.error
|
|
26
|
-
logger.
|
|
27
|
+
if (ack.error !== undefined) {
|
|
28
|
+
logger.trace('Next hop responded with error ' + ack.error)
|
|
27
29
|
return false
|
|
28
30
|
}
|
|
29
31
|
} catch (err) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Message, PeerDescriptor, RouteMessageAck, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
1
|
+
import { Message, PeerDescriptor, RouteMessageAck, RouteMessageError, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
2
2
|
import {
|
|
3
3
|
areEqualPeerDescriptors,
|
|
4
4
|
getNodeIdFromPeerDescriptor,
|
|
@@ -16,11 +16,6 @@ import { v4 } from 'uuid'
|
|
|
16
16
|
import { RouterRpcLocal, createRouteMessageAck } from './RouterRpcLocal'
|
|
17
17
|
import { ServiceID } from '../../types/ServiceID'
|
|
18
18
|
|
|
19
|
-
export enum RoutingErrors {
|
|
20
|
-
NO_CANDIDATES_FOUND = 'No routing candidates found',
|
|
21
|
-
STOPPED = 'DhtNode Stopped'
|
|
22
|
-
}
|
|
23
|
-
|
|
24
19
|
export interface RouterConfig {
|
|
25
20
|
rpcCommunicator: RoutingRpcCommunicator
|
|
26
21
|
localPeerDescriptor: PeerDescriptor
|
|
@@ -39,7 +34,7 @@ interface ForwardingTableEntry {
|
|
|
39
34
|
|
|
40
35
|
export interface IRouter {
|
|
41
36
|
doRouteMessage(routedMessage: RouteMessageWrapper, mode: RoutingMode, excludedPeer?: PeerDescriptor): RouteMessageAck
|
|
42
|
-
send(msg: Message, reachableThrough: PeerDescriptor[]):
|
|
37
|
+
send(msg: Message, reachableThrough: PeerDescriptor[]): void
|
|
43
38
|
isMostLikelyDuplicate(requestId: string): boolean
|
|
44
39
|
addToDuplicateDetector(requestId: string): void
|
|
45
40
|
addRoutingSession(session: RoutingSession): void
|
|
@@ -82,7 +77,7 @@ export class Router implements IRouter {
|
|
|
82
77
|
'routeMessage',
|
|
83
78
|
async (routedMessage: RouteMessageWrapper) => {
|
|
84
79
|
if (this.stopped) {
|
|
85
|
-
return createRouteMessageAck(routedMessage,
|
|
80
|
+
return createRouteMessageAck(routedMessage, RouteMessageError.STOPPED)
|
|
86
81
|
}
|
|
87
82
|
return rpcLocal.routeMessage(routedMessage)
|
|
88
83
|
}
|
|
@@ -93,7 +88,7 @@ export class Router implements IRouter {
|
|
|
93
88
|
'forwardMessage',
|
|
94
89
|
async (forwardMessage: RouteMessageWrapper) => {
|
|
95
90
|
if (this.stopped) {
|
|
96
|
-
return createRouteMessageAck(forwardMessage,
|
|
91
|
+
return createRouteMessageAck(forwardMessage, RouteMessageError.STOPPED)
|
|
97
92
|
}
|
|
98
93
|
return rpcLocal.forwardMessage(forwardMessage)
|
|
99
94
|
}
|
|
@@ -101,7 +96,7 @@ export class Router implements IRouter {
|
|
|
101
96
|
|
|
102
97
|
}
|
|
103
98
|
|
|
104
|
-
public
|
|
99
|
+
public send(msg: Message, reachableThrough: PeerDescriptor[]): void {
|
|
105
100
|
msg.sourceDescriptor = this.localPeerDescriptor
|
|
106
101
|
const targetPeerDescriptor = msg.targetDescriptor!
|
|
107
102
|
const forwardingEntry = this.forwardingTable.get(keyFromPeerDescriptor(targetPeerDescriptor))
|
|
@@ -115,7 +110,12 @@ export class Router implements IRouter {
|
|
|
115
110
|
reachableThrough,
|
|
116
111
|
routingPath: []
|
|
117
112
|
}
|
|
118
|
-
this.doRouteMessage(forwardedMessage, RoutingMode.FORWARD)
|
|
113
|
+
const ack = this.doRouteMessage(forwardedMessage, RoutingMode.FORWARD)
|
|
114
|
+
if (ack.error !== undefined) {
|
|
115
|
+
const error = 'Could not forward message with error ' + ack.error
|
|
116
|
+
logger.debug(error)
|
|
117
|
+
throw new Error(error)
|
|
118
|
+
}
|
|
119
119
|
} else {
|
|
120
120
|
const routedMessage: RouteMessageWrapper = {
|
|
121
121
|
message: msg,
|
|
@@ -125,23 +125,29 @@ export class Router implements IRouter {
|
|
|
125
125
|
reachableThrough,
|
|
126
126
|
routingPath: []
|
|
127
127
|
}
|
|
128
|
-
this.doRouteMessage(routedMessage, RoutingMode.ROUTE)
|
|
128
|
+
const ack = this.doRouteMessage(routedMessage, RoutingMode.ROUTE)
|
|
129
|
+
if (ack.error !== undefined) {
|
|
130
|
+
const error = 'Could not route message with error ' + ack.error
|
|
131
|
+
logger.debug(error)
|
|
132
|
+
throw new Error(error)
|
|
133
|
+
}
|
|
129
134
|
}
|
|
130
135
|
}
|
|
131
136
|
|
|
132
|
-
public doRouteMessage(routedMessage: RouteMessageWrapper, mode = RoutingMode.ROUTE): RouteMessageAck {
|
|
137
|
+
public doRouteMessage(routedMessage: RouteMessageWrapper, mode = RoutingMode.ROUTE, excludedPeer?: PeerDescriptor): RouteMessageAck {
|
|
133
138
|
if (this.stopped) {
|
|
134
|
-
return createRouteMessageAck(routedMessage,
|
|
139
|
+
return createRouteMessageAck(routedMessage, RouteMessageError.STOPPED)
|
|
135
140
|
}
|
|
136
141
|
logger.trace(`Routing message ${routedMessage.requestId} from ${getNodeIdFromPeerDescriptor(routedMessage.sourcePeer!)} `
|
|
137
142
|
+ `to ${getNodeIdFromPeerDescriptor(routedMessage.destinationPeer!)}`)
|
|
138
|
-
const session = this.createRoutingSession(routedMessage, mode)
|
|
139
|
-
const contacts = session.
|
|
143
|
+
const session = this.createRoutingSession(routedMessage, mode, excludedPeer)
|
|
144
|
+
const contacts = session.updateAndGetRoutablePeers()
|
|
140
145
|
if (contacts.length > 0) {
|
|
141
146
|
this.addRoutingSession(session)
|
|
142
147
|
// eslint-disable-next-line promise/catch-or-return
|
|
143
148
|
logger.trace('starting to raceEvents from routingSession: ' + session.sessionId)
|
|
144
149
|
let eventReceived: Promise<unknown>
|
|
150
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
145
151
|
executeSafePromise(async () => {
|
|
146
152
|
eventReceived = raceEvents3<RoutingSessionEvents>(
|
|
147
153
|
session,
|
|
@@ -162,17 +168,16 @@ export class Router implements IRouter {
|
|
|
162
168
|
session.sendMoreRequests(contacts)
|
|
163
169
|
return createRouteMessageAck(routedMessage)
|
|
164
170
|
} else {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
`Failed to send (routeMessage: ${this.serviceId}) to ${getNodeIdFromPeerDescriptor(routedMessage.destinationPeer!)}`
|
|
168
|
-
)
|
|
169
|
-
}
|
|
170
|
-
logger.trace('noCandidatesFound sessionId: ' + session.sessionId)
|
|
171
|
-
return createRouteMessageAck(routedMessage, RoutingErrors.NO_CANDIDATES_FOUND)
|
|
171
|
+
logger.trace('no targets', { sessionId: session.sessionId })
|
|
172
|
+
return createRouteMessageAck(routedMessage, RouteMessageError.NO_TARGETS)
|
|
172
173
|
}
|
|
173
174
|
}
|
|
174
175
|
|
|
175
|
-
private createRoutingSession(routedMessage: RouteMessageWrapper, mode: RoutingMode): RoutingSession {
|
|
176
|
+
private createRoutingSession(routedMessage: RouteMessageWrapper, mode: RoutingMode, excludedPeer?: PeerDescriptor): RoutingSession {
|
|
177
|
+
const excludedPeers = routedMessage.routingPath.map((descriptor) => peerIdFromPeerDescriptor(descriptor))
|
|
178
|
+
if (excludedPeer) {
|
|
179
|
+
excludedPeers.push(peerIdFromPeerDescriptor(excludedPeer))
|
|
180
|
+
}
|
|
176
181
|
logger.trace('routing session created with connections: ' + this.connections.size)
|
|
177
182
|
return new RoutingSession(
|
|
178
183
|
this.rpcCommunicator,
|
|
@@ -181,7 +186,7 @@ export class Router implements IRouter {
|
|
|
181
186
|
this.connections,
|
|
182
187
|
areEqualPeerDescriptors(this.localPeerDescriptor, routedMessage.sourcePeer!) ? 2 : 1,
|
|
183
188
|
mode,
|
|
184
|
-
|
|
189
|
+
excludedPeers
|
|
185
190
|
)
|
|
186
191
|
}
|
|
187
192
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Logger } from '@streamr/utils'
|
|
2
2
|
import { ConnectionManager } from '../../connection/ConnectionManager'
|
|
3
3
|
import { areEqualPeerDescriptors, getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
4
|
-
import { PeerDescriptor, RouteMessageAck, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
4
|
+
import { PeerDescriptor, RouteMessageAck, RouteMessageError, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
5
5
|
import { IRouterRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
6
6
|
import { DuplicateDetector } from './DuplicateDetector'
|
|
7
7
|
import { RoutingMode } from './RoutingSession'
|
|
@@ -17,10 +17,10 @@ interface RouterRpcLocalConfig {
|
|
|
17
17
|
|
|
18
18
|
const logger = new Logger(module)
|
|
19
19
|
|
|
20
|
-
export const createRouteMessageAck = (routedMessage: RouteMessageWrapper, error?:
|
|
20
|
+
export const createRouteMessageAck = (routedMessage: RouteMessageWrapper, error?: RouteMessageError): RouteMessageAck => {
|
|
21
21
|
const ack: RouteMessageAck = {
|
|
22
22
|
requestId: routedMessage.requestId,
|
|
23
|
-
error
|
|
23
|
+
error
|
|
24
24
|
}
|
|
25
25
|
return ack
|
|
26
26
|
}
|
|
@@ -37,7 +37,7 @@ export class RouterRpcLocal implements IRouterRpc {
|
|
|
37
37
|
if (this.config.duplicateRequestDetector.isMostLikelyDuplicate(routedMessage.requestId)) {
|
|
38
38
|
logger.trace(`Routing message ${routedMessage.requestId} from ${getNodeIdFromPeerDescriptor(routedMessage.sourcePeer!)} `
|
|
39
39
|
+ `to ${getNodeIdFromPeerDescriptor(routedMessage.destinationPeer!)} is likely a duplicate`)
|
|
40
|
-
return createRouteMessageAck(routedMessage,
|
|
40
|
+
return createRouteMessageAck(routedMessage, RouteMessageError.DUPLICATE)
|
|
41
41
|
}
|
|
42
42
|
logger.trace(`Processing received routeMessage ${routedMessage.requestId}`)
|
|
43
43
|
this.config.addContact(routedMessage.sourcePeer!, true)
|
|
@@ -56,7 +56,7 @@ export class RouterRpcLocal implements IRouterRpc {
|
|
|
56
56
|
if (this.config.duplicateRequestDetector.isMostLikelyDuplicate(forwardMessage.requestId)) {
|
|
57
57
|
logger.trace(`Forwarding message ${forwardMessage.requestId} from ${getNodeIdFromPeerDescriptor(forwardMessage.sourcePeer!)} `
|
|
58
58
|
+ `to ${getNodeIdFromPeerDescriptor(forwardMessage.destinationPeer!)} is likely a duplicate`)
|
|
59
|
-
return createRouteMessageAck(forwardMessage,
|
|
59
|
+
return createRouteMessageAck(forwardMessage, RouteMessageError.DUPLICATE)
|
|
60
60
|
}
|
|
61
61
|
logger.trace(`Processing received forward routeMessage ${forwardMessage.requestId}`)
|
|
62
62
|
this.config.addContact(forwardMessage.sourcePeer!, true)
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
1
|
+
import { RouteMessageError, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
2
2
|
import { v4 } from 'uuid'
|
|
3
3
|
import {
|
|
4
4
|
areEqualPeerDescriptors,
|
|
5
5
|
getNodeIdFromPeerDescriptor
|
|
6
6
|
} from '../../helpers/peerIdFromPeerDescriptor'
|
|
7
7
|
import { IRouterRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
8
|
-
import {
|
|
8
|
+
import { RpcRemote } from '../contact/RpcRemote'
|
|
9
9
|
import { Logger } from '@streamr/utils'
|
|
10
10
|
import { getPreviousPeer } from './getPreviousPeer'
|
|
11
11
|
|
|
12
12
|
const logger = new Logger(module)
|
|
13
13
|
|
|
14
|
-
export class RouterRpcRemote extends
|
|
14
|
+
export class RouterRpcRemote extends RpcRemote<IRouterRpcClient> {
|
|
15
15
|
|
|
16
16
|
async routeMessage(params: RouteMessageWrapper): Promise<boolean> {
|
|
17
17
|
const message: RouteMessageWrapper = {
|
|
@@ -22,16 +22,17 @@ export class RouterRpcRemote extends Remote<IRouterRpcClient> {
|
|
|
22
22
|
reachableThrough: params.reachableThrough ?? [],
|
|
23
23
|
routingPath: params.routingPath
|
|
24
24
|
}
|
|
25
|
-
const options = this.formDhtRpcOptions(
|
|
25
|
+
const options = this.formDhtRpcOptions({
|
|
26
|
+
doNotConnect: true
|
|
27
|
+
})
|
|
26
28
|
try {
|
|
27
29
|
const ack = await this.getClient().routeMessage(message, options)
|
|
28
30
|
// Success signal if sent to destination and error includes duplicate
|
|
29
|
-
if (
|
|
30
|
-
areEqualPeerDescriptors(params.destinationPeer!, this.getPeerDescriptor())
|
|
31
|
-
&& ack.error.includes('duplicate')
|
|
31
|
+
if (ack.error === RouteMessageError.DUPLICATE
|
|
32
|
+
&& areEqualPeerDescriptors(params.destinationPeer!, this.getPeerDescriptor())
|
|
32
33
|
) {
|
|
33
34
|
return true
|
|
34
|
-
} else if (ack.error
|
|
35
|
+
} else if (ack.error !== undefined) {
|
|
35
36
|
return false
|
|
36
37
|
}
|
|
37
38
|
} catch (err) {
|
|
@@ -54,10 +55,12 @@ export class RouterRpcRemote extends Remote<IRouterRpcClient> {
|
|
|
54
55
|
reachableThrough: params.reachableThrough ?? [],
|
|
55
56
|
routingPath: params.routingPath
|
|
56
57
|
}
|
|
57
|
-
const options = this.formDhtRpcOptions(
|
|
58
|
+
const options = this.formDhtRpcOptions({
|
|
59
|
+
doNotConnect: true
|
|
60
|
+
})
|
|
58
61
|
try {
|
|
59
62
|
const ack = await this.getClient().forwardMessage(message, options)
|
|
60
|
-
if (ack.error
|
|
63
|
+
if (ack.error !== undefined) {
|
|
61
64
|
return false
|
|
62
65
|
}
|
|
63
66
|
} catch (err) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
|
|
2
2
|
import { SortedContactList } from '../contact/SortedContactList'
|
|
3
3
|
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
4
|
-
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
4
|
+
import { getNodeIdFromPeerDescriptor, peerIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
5
5
|
import { Logger } from '@streamr/utils'
|
|
6
6
|
import EventEmitter from 'eventemitter3'
|
|
7
7
|
import { v4 } from 'uuid'
|
|
@@ -12,7 +12,7 @@ import { FindRpcClient, RouterRpcClient } from '../../proto/packages/dht/protos/
|
|
|
12
12
|
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
13
13
|
import { Contact } from '../contact/Contact'
|
|
14
14
|
import { FindRpcRemote } from './FindRpcRemote'
|
|
15
|
-
import { EXISTING_CONNECTION_TIMEOUT } from '../contact/
|
|
15
|
+
import { EXISTING_CONNECTION_TIMEOUT } from '../contact/RpcRemote'
|
|
16
16
|
import { getPreviousPeer } from './getPreviousPeer'
|
|
17
17
|
|
|
18
18
|
const logger = new Logger(module)
|
|
@@ -97,15 +97,15 @@ export class RoutingSession extends EventEmitter<RoutingSessionEvents> {
|
|
|
97
97
|
this.parallelism = parallelism
|
|
98
98
|
this.mode = mode
|
|
99
99
|
const previousPeer = getPreviousPeer(messageToRoute)
|
|
100
|
-
const previousId = previousPeer ? PeerID.fromValue(previousPeer.
|
|
101
|
-
this.contactList = new SortedContactList(
|
|
102
|
-
PeerID.fromValue(this.messageToRoute.destinationPeer!.
|
|
103
|
-
10000,
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
)
|
|
100
|
+
const previousId = previousPeer ? PeerID.fromValue(previousPeer.nodeId) : undefined
|
|
101
|
+
this.contactList = new SortedContactList({
|
|
102
|
+
referenceId: PeerID.fromValue(this.messageToRoute.destinationPeer!.nodeId),
|
|
103
|
+
maxSize: 10000, // TODO use config option or named constant?
|
|
104
|
+
allowToContainReferenceId: true,
|
|
105
|
+
peerIdDistanceLimit: previousId,
|
|
106
|
+
excludedPeerIDs: excludedPeerIDs,
|
|
107
|
+
emitEvents: false
|
|
108
|
+
})
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
private onRequestFailed(peerId: PeerID) {
|
|
@@ -116,7 +116,7 @@ export class RoutingSession extends EventEmitter<RoutingSessionEvents> {
|
|
|
116
116
|
if (this.ongoingRequests.has(peerId.toKey())) {
|
|
117
117
|
this.ongoingRequests.delete(peerId.toKey())
|
|
118
118
|
}
|
|
119
|
-
const contacts = this.
|
|
119
|
+
const contacts = this.updateAndGetRoutablePeers()
|
|
120
120
|
if (contacts.length === 0 && this.ongoingRequests.size === 0) {
|
|
121
121
|
logger.trace('routing failed, emitting routingFailed sessionId: ' + this.sessionId)
|
|
122
122
|
// TODO should call this.stop() so that we do cleanup? (after the emitFailure call)
|
|
@@ -143,7 +143,7 @@ export class RoutingSession extends EventEmitter<RoutingSessionEvents> {
|
|
|
143
143
|
return
|
|
144
144
|
}
|
|
145
145
|
this.successfulHopCounter += 1
|
|
146
|
-
const contacts = this.
|
|
146
|
+
const contacts = this.updateAndGetRoutablePeers()
|
|
147
147
|
if (this.successfulHopCounter >= this.parallelism || contacts.length === 0) {
|
|
148
148
|
// TODO should call this.stop() so that we do cleanup? (after the routingSucceeded call)
|
|
149
149
|
this.stopped = true
|
|
@@ -170,10 +170,15 @@ export class RoutingSession extends EventEmitter<RoutingSessionEvents> {
|
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
|
|
174
|
-
logger.trace('
|
|
175
|
-
//
|
|
176
|
-
|
|
173
|
+
updateAndGetRoutablePeers(): RemoteContact[] {
|
|
174
|
+
logger.trace('getRoutablePeers() sessionId: ' + this.sessionId)
|
|
175
|
+
// Remove stale contacts that may have been removed from connections
|
|
176
|
+
this.contactList.getAllContacts().forEach((contact) => {
|
|
177
|
+
const peerId = peerIdFromPeerDescriptor(contact.getPeerDescriptor())
|
|
178
|
+
if (this.connections.has(peerId.toKey()) === false) {
|
|
179
|
+
this.contactList.removeContact(peerId)
|
|
180
|
+
}
|
|
181
|
+
})
|
|
177
182
|
const contacts = Array.from(this.connections.values())
|
|
178
183
|
.map((peer) => new RemoteContact(peer, this.localPeerDescriptor, this.rpcCommunicator))
|
|
179
184
|
this.contactList.addContacts(contacts)
|
|
@@ -2,5 +2,5 @@ import { last } from 'lodash'
|
|
|
2
2
|
import { PeerDescriptor, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
3
3
|
|
|
4
4
|
export const getPreviousPeer = (routeMessage: RouteMessageWrapper): PeerDescriptor | undefined => {
|
|
5
|
-
return last(routeMessage.routingPath
|
|
5
|
+
return last(routeMessage.routingPath)
|
|
6
6
|
}
|