@streamr/dht 100.0.0-pretestnet.4 → 100.0.0-testnet-one.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/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 +2 -2
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.js +4 -1
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- 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 +121 -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.js +1 -0
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +0 -1
- package/dist/src/connection/websocket/WebsocketConnector.js +41 -7
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +2 -2
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +2 -2
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +9 -19
- package/dist/src/dht/DhtNode.js +76 -216
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.js +3 -3
- 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 +48 -0
- package/dist/src/dht/PeerManager.js +208 -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/discovery/DiscoverySession.d.ts +2 -9
- package/dist/src/dht/discovery/DiscoverySession.js +12 -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 +13 -15
- 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 +13 -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 +50 -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 +2 -6
- package/dist/src/dht/routing/Router.js +21 -18
- 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 +16 -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 +91 -99
- 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.js +2 -2
- 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 +3 -3
- 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 +2 -2
- package/src/connection/ConnectionManager.ts +4 -1
- package/src/connection/ConnectorFacade.ts +5 -4
- package/src/connection/ManagedConnection.ts +1 -0
- package/src/connection/connectivityChecker.ts +101 -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 +1 -0
- package/src/connection/websocket/WebsocketConnector.ts +22 -12
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +2 -2
- package/src/dht/DhtNode.ts +102 -269
- package/src/dht/DhtNodeRpcLocal.ts +3 -3
- 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 +256 -0
- package/src/dht/contact/ContactList.ts +3 -2
- package/src/dht/contact/{Remote.ts → RpcRemote.ts} +7 -6
- package/src/dht/discovery/DiscoverySession.ts +15 -40
- package/src/dht/discovery/PeerDiscovery.ts +18 -23
- package/src/dht/find/FindRpcLocal.ts +2 -2
- package/src/dht/find/FindSession.ts +20 -20
- package/src/dht/find/FindSessionRpcLocal.ts +1 -1
- package/src/dht/find/FindSessionRpcRemote.ts +2 -2
- package/src/dht/find/Finder.ts +79 -58
- package/src/dht/routing/FindRpcRemote.ts +7 -5
- package/src/dht/routing/Router.ts +22 -21
- package/src/dht/routing/RouterRpcLocal.ts +5 -5
- package/src/dht/routing/RouterRpcRemote.ts +13 -10
- package/src/dht/routing/RoutingSession.ts +15 -10
- package/src/dht/routing/getPreviousPeer.ts +1 -1
- package/src/dht/store/LocalDataStore.ts +18 -17
- package/src/dht/store/StoreRpcLocal.ts +101 -108
- package/src/dht/store/StoreRpcRemote.ts +7 -23
- package/src/exports.ts +1 -1
- package/src/helpers/PeerID.ts +2 -2
- package/src/helpers/UUID.ts +1 -1
- package/src/helpers/{kademliaId.ts → nodeId.ts} +1 -1
- package/src/helpers/peerIdFromPeerDescriptor.ts +3 -3
- 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 +4 -4
- package/test/benchmark/KademliaCorrectness.test.ts +2 -2
- package/test/benchmark/kademlia-simulation/Contact.ts +1 -1
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +1 -1
- package/test/end-to-end/Layer0-Layer1.test.ts +1 -1
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +1 -1
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +2 -2
- package/test/end-to-end/Layer0Webrtc.test.ts +2 -2
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +1 -1
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +1 -1
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +1 -1
- package/test/end-to-end/memory-leak.test.ts +8 -12
- package/test/integration/ConnectionLocking.test.ts +2 -2
- package/test/integration/ConnectionManager.test.ts +14 -14
- package/test/integration/DhtJoinPeerDiscovery.test.ts +1 -1
- 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 +2 -2
- package/test/integration/Mock-Layer1-Layer0.test.ts +1 -1
- package/test/integration/{MigrateData.test.ts → ReplicateData.test.ts} +9 -9
- 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 +8 -8
- package/test/integration/Store.test.ts +33 -13
- package/test/integration/StoreAndDelete.test.ts +19 -17
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +20 -20
- 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 +2 -2
- package/test/integration/WebsocketConnectorRpc.test.ts +2 -2
- 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 +2 -2
- package/test/unit/WebsocketConnector.test.ts +1 -1
- package/test/unit/connectivityRequestHandler.test.ts +71 -0
- package/test/utils/mock/Router.ts +1 -1
- package/test/utils/utils.ts +24 -22
- package/dist/src/connection/ConnectivityChecker.d.ts +0 -17
- package/dist/src/connection/ConnectivityChecker.js +0 -208
- package/dist/src/connection/ConnectivityChecker.js.map +0 -1
- package/dist/src/dht/contact/Remote.js.map +0 -1
- package/dist/src/helpers/kademliaId.d.ts +0 -1
- package/dist/src/helpers/kademliaId.js.map +0 -1
- package/src/connection/ConnectivityChecker.ts +0 -199
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Logger } from '@streamr/utils'
|
|
2
|
-
import { PeerDescriptor, RouteMessageAck, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
2
|
+
import { PeerDescriptor, RouteMessageAck, RouteMessageError, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
3
3
|
import { IFindRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
4
4
|
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
5
5
|
import { createRouteMessageAck } from '../routing/RouterRpcLocal'
|
|
@@ -24,7 +24,7 @@ export class FindRpcLocal implements IFindRpc {
|
|
|
24
24
|
|
|
25
25
|
async routeFindRequest(routedMessage: RouteMessageWrapper): Promise<RouteMessageAck> {
|
|
26
26
|
if (this.config.isMostLikelyDuplicate(routedMessage.requestId)) {
|
|
27
|
-
return createRouteMessageAck(routedMessage,
|
|
27
|
+
return createRouteMessageAck(routedMessage, RouteMessageError.DUPLICATE)
|
|
28
28
|
}
|
|
29
29
|
const senderId = getNodeIdFromPeerDescriptor(getPreviousPeer(routedMessage) ?? routedMessage.sourcePeer!)
|
|
30
30
|
logger.trace(`Received routeFindRequest call from ${senderId}`)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import EventEmitter from 'eventemitter3'
|
|
2
2
|
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
3
|
-
import { DataEntry, PeerDescriptor, FindResponse } from '../../proto/packages/dht/protos/DhtRpc'
|
|
3
|
+
import { DataEntry, PeerDescriptor, FindResponse, FindAction } from '../../proto/packages/dht/protos/DhtRpc'
|
|
4
4
|
import { ITransport } from '../../transport/ITransport'
|
|
5
5
|
import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
|
|
6
6
|
import { Contact } from '../contact/Contact'
|
|
@@ -17,20 +17,20 @@ export interface FindSessionEvents {
|
|
|
17
17
|
export interface FindSessionConfig {
|
|
18
18
|
serviceId: ServiceID
|
|
19
19
|
transport: ITransport
|
|
20
|
-
|
|
20
|
+
nodeIdToFind: Uint8Array
|
|
21
21
|
localPeerId: PeerID
|
|
22
22
|
waitedRoutingPathCompletions: number
|
|
23
|
-
|
|
23
|
+
action: FindAction
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
27
27
|
private readonly serviceId: ServiceID
|
|
28
28
|
private readonly transport: ITransport
|
|
29
|
-
private readonly
|
|
29
|
+
private readonly nodeIdToFind: Uint8Array
|
|
30
30
|
private readonly localPeerId: PeerID
|
|
31
31
|
private readonly waitedRoutingPathCompletions: number
|
|
32
32
|
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
33
|
-
private readonly
|
|
33
|
+
private readonly action: FindAction
|
|
34
34
|
private results: SortedContactList<Contact>
|
|
35
35
|
private foundData: Map<string, DataEntry> = new Map()
|
|
36
36
|
private allKnownHops: Set<PeerIDKey> = new Set()
|
|
@@ -43,11 +43,11 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
43
43
|
super()
|
|
44
44
|
this.serviceId = config.serviceId
|
|
45
45
|
this.transport = config.transport
|
|
46
|
-
this.
|
|
46
|
+
this.nodeIdToFind = config.nodeIdToFind
|
|
47
47
|
this.localPeerId = config.localPeerId
|
|
48
48
|
this.waitedRoutingPathCompletions = config.waitedRoutingPathCompletions
|
|
49
|
-
this.results = new SortedContactList(PeerID.fromValue(this.
|
|
50
|
-
this.
|
|
49
|
+
this.results = new SortedContactList(PeerID.fromValue(this.nodeIdToFind), 10, undefined, true)
|
|
50
|
+
this.action = config.action
|
|
51
51
|
this.rpcCommunicator = new ListeningRpcCommunicator(this.serviceId, this.transport, {
|
|
52
52
|
rpcRequestTimeout: 15000
|
|
53
53
|
})
|
|
@@ -56,7 +56,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
56
56
|
|
|
57
57
|
private registerLocalRpcMethods() {
|
|
58
58
|
const rpcLocal = new FindSessionRpcLocal({
|
|
59
|
-
doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound
|
|
59
|
+
doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound: boolean) => {
|
|
60
60
|
this.doSendFindResponse(routingPath, nodes, dataEntries, noCloserNodesFound)
|
|
61
61
|
}
|
|
62
62
|
})
|
|
@@ -70,10 +70,10 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
70
70
|
unreportedHops.delete(id)
|
|
71
71
|
})
|
|
72
72
|
if (this.noCloserNodesReceivedCounter >= 1 && unreportedHops.size === 0) {
|
|
73
|
-
if (this.
|
|
73
|
+
if (this.action === FindAction.FETCH_DATA
|
|
74
74
|
&& (this.hasNonStaleData() || this.noCloserNodesReceivedCounter >= this.waitedRoutingPathCompletions)) {
|
|
75
75
|
return true
|
|
76
|
-
} else if (this.
|
|
76
|
+
} else if (this.action === FindAction.FETCH_DATA) {
|
|
77
77
|
return false
|
|
78
78
|
}
|
|
79
79
|
return true
|
|
@@ -89,7 +89,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
89
89
|
routingPath: PeerDescriptor[],
|
|
90
90
|
nodes: PeerDescriptor[],
|
|
91
91
|
dataEntries: DataEntry[],
|
|
92
|
-
noCloserNodesFound
|
|
92
|
+
noCloserNodesFound: boolean
|
|
93
93
|
): void {
|
|
94
94
|
this.addKnownHops(routingPath)
|
|
95
95
|
if (routingPath.length >= 1) {
|
|
@@ -106,7 +106,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
106
106
|
|
|
107
107
|
private addKnownHops(routingPath: PeerDescriptor[]) {
|
|
108
108
|
routingPath.forEach((desc) => {
|
|
109
|
-
const newPeerId = PeerID.fromValue(desc.
|
|
109
|
+
const newPeerId = PeerID.fromValue(desc.nodeId)
|
|
110
110
|
if (!this.localPeerId.equals(newPeerId)) {
|
|
111
111
|
this.allKnownHops.add(newPeerId.toKey())
|
|
112
112
|
}
|
|
@@ -114,7 +114,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
private setHopAsReported(desc: PeerDescriptor) {
|
|
117
|
-
const newPeerId = PeerID.fromValue(desc.
|
|
117
|
+
const newPeerId = PeerID.fromValue(desc.nodeId)
|
|
118
118
|
if (!this.localPeerId.equals(newPeerId)) {
|
|
119
119
|
this.reportedHops.add(newPeerId.toKey())
|
|
120
120
|
}
|
|
@@ -132,11 +132,11 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
132
132
|
|
|
133
133
|
private processFoundData(dataEntries: DataEntry[]): void {
|
|
134
134
|
dataEntries.forEach((entry) => {
|
|
135
|
-
const
|
|
136
|
-
const existingEntry = this.foundData.get(
|
|
137
|
-
if (!existingEntry || existingEntry.
|
|
138
|
-
|| (existingEntry.
|
|
139
|
-
this.foundData.set(
|
|
135
|
+
const creatorKey = keyFromPeerDescriptor(entry.creator!)
|
|
136
|
+
const existingEntry = this.foundData.get(creatorKey)
|
|
137
|
+
if (!existingEntry || existingEntry.createdAt! < entry.createdAt!
|
|
138
|
+
|| (existingEntry.createdAt! <= entry.createdAt! && entry.deleted)) {
|
|
139
|
+
this.foundData.set(creatorKey, entry)
|
|
140
140
|
}
|
|
141
141
|
})
|
|
142
142
|
}
|
|
@@ -164,7 +164,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
|
164
164
|
|
|
165
165
|
public getResults = (): FindResult => ({
|
|
166
166
|
closestNodes: this.results.getAllContacts().map((contact) => contact.getPeerDescriptor()),
|
|
167
|
-
dataEntries:
|
|
167
|
+
dataEntries: Array.from(this.foundData.values())
|
|
168
168
|
})
|
|
169
169
|
|
|
170
170
|
public stop(): void {
|
|
@@ -6,7 +6,7 @@ import { Logger } from '@streamr/utils'
|
|
|
6
6
|
const logger = new Logger(module)
|
|
7
7
|
|
|
8
8
|
interface FindSessionRpcLocalConfig {
|
|
9
|
-
doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound
|
|
9
|
+
doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound: boolean) => void
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export class FindSessionRpcLocal implements IFindSessionRpc {
|
|
@@ -5,11 +5,11 @@ import {
|
|
|
5
5
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
6
6
|
import { IFindSessionRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
7
7
|
import { Logger } from '@streamr/utils'
|
|
8
|
-
import {
|
|
8
|
+
import { RpcRemote } from '../contact/RpcRemote'
|
|
9
9
|
|
|
10
10
|
const logger = new Logger(module)
|
|
11
11
|
|
|
12
|
-
export class FindSessionRpcRemote extends
|
|
12
|
+
export class FindSessionRpcRemote extends RpcRemote<IFindSessionRpcClient> {
|
|
13
13
|
|
|
14
14
|
sendFindResponse(
|
|
15
15
|
routingPath: PeerDescriptor[],
|
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,41 +230,53 @@ 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
278
|
const closestPeers = new SortedContactList<DhtNodeRpcRemote>(
|
|
258
|
-
PeerID.fromValue(
|
|
279
|
+
PeerID.fromValue(nodeId),
|
|
259
280
|
limit,
|
|
260
281
|
undefined,
|
|
261
282
|
true,
|
|
@@ -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
137
|
public doRouteMessage(routedMessage: RouteMessageWrapper, mode = RoutingMode.ROUTE): 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
143
|
const session = this.createRoutingSession(routedMessage, mode)
|
|
139
|
-
const contacts = session.
|
|
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,13 +168,8 @@ 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
|
|
|
@@ -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) {
|