@streamr/dht 100.0.0-testnet-one.4 → 100.0.0-testnet-two.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.d.ts +11 -11
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionLockRpcLocal.js +4 -4
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +3 -5
- package/dist/src/connection/ConnectionLockRpcRemote.js +4 -7
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +4 -6
- package/dist/src/connection/ConnectionManager.js +48 -46
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +2 -2
- package/dist/src/connection/ConnectorFacade.js +2 -1
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +3 -6
- package/dist/src/connection/ManagedConnection.js +6 -10
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/connectivityChecker.js +1 -0
- package/dist/src/connection/connectivityChecker.js.map +1 -1
- package/dist/src/connection/simulator/Simulator.d.ts +0 -3
- package/dist/src/connection/simulator/Simulator.js +3 -51
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.js +5 -5
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +2 -2
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +0 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js +7 -9
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +11 -12
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +2 -5
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +1 -4
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +1 -2
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +3 -4
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +2 -7
- package/dist/src/connection/websocket/WebsocketConnector.js +32 -38
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +3 -2
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +8 -10
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +3 -6
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +0 -3
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketServer.d.ts +1 -4
- package/dist/src/connection/websocket/WebsocketServer.js +11 -12
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +10 -13
- package/dist/src/dht/DhtNode.js +39 -52
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +0 -1
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +7 -7
- package/dist/src/dht/DhtNodeRpcRemote.js +8 -7
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +5 -4
- package/dist/src/dht/ExternalApiRpcLocal.js +3 -2
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +2 -2
- package/dist/src/dht/ExternalApiRpcRemote.js +2 -0
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +13 -14
- package/dist/src/dht/PeerManager.js +45 -60
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/Contact.d.ts +2 -2
- package/dist/src/dht/contact/Contact.js +2 -2
- package/dist/src/dht/contact/Contact.js.map +1 -1
- package/dist/src/dht/contact/ContactList.d.ts +7 -7
- package/dist/src/dht/contact/ContactList.js +3 -3
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +4 -4
- package/dist/src/dht/contact/RandomContactList.js +12 -11
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/RpcRemote.d.ts +7 -4
- package/dist/src/dht/contact/RpcRemote.js +5 -2
- package/dist/src/dht/contact/RpcRemote.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +13 -13
- package/dist/src/dht/contact/SortedContactList.js +33 -29
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +2 -4
- package/dist/src/dht/discovery/DiscoverySession.js +19 -20
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +2 -5
- package/dist/src/dht/discovery/PeerDiscovery.js +12 -20
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +36 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +165 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +14 -0
- package/dist/src/dht/{find/FindRpcLocal.js → recursive-operation/RecursiveOperationRpcLocal.js} +7 -7
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.d.ts +6 -0
- package/dist/src/dht/{routing/FindRpcRemote.js → recursive-operation/RecursiveOperationRpcRemote.js} +11 -11
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +42 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +182 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +12 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +17 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.d.ts +6 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js +21 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js.map +1 -0
- package/dist/src/dht/routing/Router.d.ts +4 -19
- package/dist/src/dht/routing/Router.js +41 -33
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.js +6 -5
- 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 +7 -7
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +20 -16
- package/dist/src/dht/routing/RoutingSession.js +37 -45
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +11 -13
- package/dist/src/dht/store/LocalDataStore.js +36 -56
- package/dist/src/dht/store/LocalDataStore.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +33 -0
- package/dist/src/dht/store/StoreManager.js +186 -0
- package/dist/src/dht/store/StoreManager.js.map +1 -0
- package/dist/src/dht/store/StoreRpcLocal.d.ts +8 -40
- package/dist/src/dht/store/StoreRpcLocal.js +13 -229
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.d.ts +5 -5
- package/dist/src/dht/store/StoreRpcRemote.js +4 -5
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/exports.d.ts +2 -0
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/MapWithTtl.d.ts +14 -0
- package/dist/src/helpers/MapWithTtl.js +59 -0
- package/dist/src/helpers/MapWithTtl.js.map +1 -0
- package/dist/src/helpers/PeerID.d.ts +2 -0
- package/dist/src/helpers/PeerID.js +4 -0
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/nodeId.d.ts +5 -0
- package/dist/src/helpers/nodeId.js +18 -1
- package/dist/src/helpers/nodeId.js.map +1 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +2 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +2 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/helpers/protoClasses.js +2 -3
- package/dist/src/helpers/protoClasses.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +21 -22
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +18 -18
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +36 -60
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +34 -53
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +11 -12
- package/dist/src/rpc-protocol/DhtCallContext.d.ts +2 -1
- package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -1
- package/dist/src/rpc-protocol/DhtRpcOptions.d.ts +2 -1
- package/dist/src/transport/ITransport.d.ts +9 -1
- package/dist/src/transport/ITransport.js +5 -0
- package/dist/src/transport/ITransport.js.map +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.js +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -1
- package/dist/src/transport/RoutingRpcCommunicator.js +16 -11
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/jest.config.js +4 -1
- package/karma-setup.js +2 -0
- package/karma.config.js +11 -7
- package/package.json +6 -7
- package/protos/DhtRpc.proto +17 -23
- package/src/connection/ConnectionLockHandler.ts +15 -15
- package/src/connection/ConnectionLockRpcLocal.ts +8 -9
- package/src/connection/ConnectionLockRpcRemote.ts +7 -16
- package/src/connection/ConnectionManager.ts +58 -55
- package/src/connection/ConnectorFacade.ts +3 -0
- package/src/connection/ManagedConnection.ts +12 -30
- package/src/connection/connectivityChecker.ts +1 -0
- package/src/connection/simulator/Simulator.ts +5 -36
- package/src/connection/simulator/SimulatorConnector.ts +8 -8
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -3
- package/src/connection/webrtc/NodeWebrtcConnection.ts +3 -4
- package/src/connection/webrtc/WebrtcConnector.ts +11 -13
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +16 -16
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +3 -13
- package/src/connection/websocket/AutoCertifierClientFacade.ts +4 -6
- package/src/connection/websocket/ClientWebsocket.ts +1 -0
- package/src/connection/websocket/WebsocketConnector.ts +43 -56
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +11 -13
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +4 -15
- package/src/connection/websocket/WebsocketServer.ts +14 -17
- package/src/dht/DhtNode.ts +67 -69
- package/src/dht/DhtNodeRpcLocal.ts +0 -1
- package/src/dht/DhtNodeRpcRemote.ts +13 -13
- package/src/dht/ExternalApiRpcLocal.ts +16 -6
- package/src/dht/ExternalApiRpcRemote.ts +5 -3
- package/src/dht/PeerManager.ts +73 -90
- package/src/dht/contact/Contact.ts +5 -4
- package/src/dht/contact/ContactList.ts +9 -9
- package/src/dht/contact/RandomContactList.ts +14 -14
- package/src/dht/contact/RpcRemote.ts +10 -7
- package/src/dht/contact/SortedContactList.ts +48 -45
- package/src/dht/discovery/DiscoverySession.ts +25 -25
- package/src/dht/discovery/PeerDiscovery.ts +16 -37
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +240 -0
- package/src/dht/{find/FindRpcLocal.ts → recursive-operation/RecursiveOperationRpcLocal.ts} +9 -9
- package/src/dht/{routing/FindRpcRemote.ts → recursive-operation/RecursiveOperationRpcRemote.ts} +10 -10
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +224 -0
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +25 -0
- package/src/dht/{find/FindSessionRpcRemote.ts → recursive-operation/RecursiveOperationSessionRpcRemote.ts} +8 -8
- package/src/dht/routing/Router.ts +45 -64
- package/src/dht/routing/RouterRpcLocal.ts +7 -6
- package/src/dht/routing/RouterRpcRemote.ts +9 -10
- package/src/dht/routing/RoutingSession.ts +59 -68
- package/src/dht/store/LocalDataStore.ts +47 -66
- package/src/dht/store/StoreManager.ts +217 -0
- package/src/dht/store/StoreRpcLocal.ts +26 -302
- package/src/dht/store/StoreRpcRemote.ts +9 -11
- package/src/exports.ts +2 -0
- package/src/helpers/MapWithTtl.ts +71 -0
- package/src/helpers/PeerID.ts +5 -0
- package/src/helpers/nodeId.ts +20 -0
- package/src/helpers/peerIdFromPeerDescriptor.ts +5 -3
- package/src/helpers/protoClasses.ts +4 -6
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +31 -32
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +11 -12
- package/src/proto/packages/dht/protos/DhtRpc.ts +49 -80
- package/src/rpc-protocol/DhtCallContext.ts +2 -1
- package/src/rpc-protocol/DhtRpcOptions.ts +2 -1
- package/src/transport/ITransport.ts +10 -1
- package/src/transport/ListeningRpcCommunicator.ts +1 -1
- package/src/transport/RoutingRpcCommunicator.ts +18 -12
- package/test/benchmark/Find.test.ts +8 -21
- package/test/benchmark/KademliaCorrectness.test.ts +11 -20
- package/test/benchmark/SortedContactListBenchmark.test.ts +9 -9
- package/test/benchmark/kademlia-simulation/Contact.ts +9 -8
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +2 -2
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +17 -17
- package/test/end-to-end/Layer0-Layer1.test.ts +10 -14
- package/test/end-to-end/Layer0.test.ts +15 -18
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +7 -9
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +4 -10
- package/test/end-to-end/Layer0Webrtc.test.ts +5 -6
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +8 -16
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +6 -13
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +5 -6
- package/test/end-to-end/memory-leak.test.ts +6 -8
- package/test/integration/ConnectionLocking.test.ts +6 -13
- package/test/integration/ConnectionManager.test.ts +26 -43
- package/test/integration/DhtJoinPeerDiscovery.test.ts +5 -10
- package/test/integration/DhtNodeExternalAPI.test.ts +16 -22
- package/test/integration/DhtNodeRpcRemote.test.ts +13 -22
- package/test/integration/DhtRpc.test.ts +13 -18
- package/test/integration/Find.test.ts +10 -10
- package/test/integration/Layer1-scale.test.ts +17 -28
- package/test/integration/Mock-Layer1-Layer0.test.ts +25 -43
- package/test/integration/MultipleEntryPointJoining.test.ts +7 -7
- package/test/integration/ReplicateData.test.ts +28 -30
- package/test/integration/RouteMessage.test.ts +30 -27
- package/test/integration/RouterRpcRemote.test.ts +12 -18
- package/test/integration/RpcErrors.test.ts +8 -21
- package/test/integration/ScaleDownDht.test.ts +11 -10
- package/test/integration/SimultaneousConnections.test.ts +43 -67
- package/test/integration/Store.test.ts +23 -36
- package/test/integration/StoreAndDelete.test.ts +30 -50
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +16 -37
- package/test/integration/StoreRpcRemote.test.ts +20 -30
- package/test/integration/WebrtcConnectionManagement.test.ts +3 -13
- package/test/integration/WebrtcConnectorRpc.test.ts +3 -9
- package/test/integration/WebsocketConnectionManagement.test.ts +7 -10
- package/test/integration/WebsocketConnectorRpc.test.ts +8 -17
- package/test/unit/ConnectivityHelpers.test.ts +9 -17
- package/test/unit/DuplicateDetector.test.ts +7 -4
- package/test/unit/LocalDataStore.test.ts +66 -77
- package/test/unit/PeerManager.test.ts +31 -0
- package/test/unit/RandomContactList.test.ts +13 -8
- package/test/unit/{Finder.test.ts → RecursiveOperationManager.test.ts} +38 -43
- package/test/unit/RecursiveOperationSession.test.ts +68 -0
- package/test/unit/Router.test.ts +15 -22
- package/test/unit/RoutingSession.test.ts +23 -25
- package/test/unit/SortedContactList.test.ts +41 -26
- package/test/unit/StoreManager.test.ts +132 -0
- package/test/unit/WebsocketConnector.test.ts +27 -35
- package/test/unit/customMatchers.test.ts +16 -0
- package/test/utils/FakeTransport.ts +44 -0
- package/test/utils/customMatchers.ts +70 -0
- package/test/utils/mock/MockRpcCommunicator.ts +7 -0
- package/test/utils/mock/Router.ts +3 -2
- package/test/utils/mock/mockDataEntry.ts +36 -0
- package/test/utils/utils.ts +49 -87
- package/dist/src/dht/find/FindRpcLocal.d.ts +0 -14
- package/dist/src/dht/find/FindRpcLocal.js.map +0 -1
- package/dist/src/dht/find/FindSession.d.ts +0 -44
- package/dist/src/dht/find/FindSession.js +0 -150
- package/dist/src/dht/find/FindSession.js.map +0 -1
- package/dist/src/dht/find/FindSessionRpcLocal.d.ts +0 -12
- package/dist/src/dht/find/FindSessionRpcLocal.js +0 -17
- package/dist/src/dht/find/FindSessionRpcLocal.js.map +0 -1
- package/dist/src/dht/find/FindSessionRpcRemote.d.ts +0 -6
- package/dist/src/dht/find/FindSessionRpcRemote.js +0 -21
- package/dist/src/dht/find/FindSessionRpcRemote.js.map +0 -1
- package/dist/src/dht/find/Finder.d.ts +0 -49
- package/dist/src/dht/find/Finder.js +0 -197
- package/dist/src/dht/find/Finder.js.map +0 -1
- package/dist/src/dht/routing/FindRpcRemote.d.ts +0 -6
- package/dist/src/dht/routing/FindRpcRemote.js.map +0 -1
- package/src/dht/find/FindSession.ts +0 -183
- package/src/dht/find/FindSessionRpcLocal.ts +0 -25
- package/src/dht/find/Finder.ts +0 -295
|
@@ -1,30 +1,24 @@
|
|
|
1
1
|
import { Message, PeerDescriptor, RouteMessageAck, RouteMessageError, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
2
2
|
import {
|
|
3
3
|
areEqualPeerDescriptors,
|
|
4
|
-
getNodeIdFromPeerDescriptor
|
|
5
|
-
keyFromPeerDescriptor,
|
|
6
|
-
peerIdFromPeerDescriptor
|
|
4
|
+
getNodeIdFromPeerDescriptor
|
|
7
5
|
} from '../../helpers/peerIdFromPeerDescriptor'
|
|
8
6
|
import { RoutingMode, RoutingSession, RoutingSessionEvents } from './RoutingSession'
|
|
9
7
|
import { Logger, executeSafePromise, raceEvents3, withTimeout } from '@streamr/utils'
|
|
10
8
|
import { RoutingRpcCommunicator } from '../../transport/RoutingRpcCommunicator'
|
|
11
|
-
import { PeerIDKey } from '../../helpers/PeerID'
|
|
12
9
|
import { DuplicateDetector } from './DuplicateDetector'
|
|
13
10
|
import { ConnectionManager } from '../../connection/ConnectionManager'
|
|
14
11
|
import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
|
|
15
12
|
import { v4 } from 'uuid'
|
|
16
13
|
import { RouterRpcLocal, createRouteMessageAck } from './RouterRpcLocal'
|
|
17
|
-
import {
|
|
14
|
+
import { NodeID, getNodeIdFromBinary } from '../../helpers/nodeId'
|
|
18
15
|
|
|
19
16
|
export interface RouterConfig {
|
|
20
17
|
rpcCommunicator: RoutingRpcCommunicator
|
|
21
18
|
localPeerDescriptor: PeerDescriptor
|
|
22
|
-
connections: Map<
|
|
19
|
+
connections: Map<NodeID, DhtNodeRpcRemote>
|
|
23
20
|
addContact: (contact: PeerDescriptor, setActive?: boolean) => void
|
|
24
|
-
serviceId: ServiceID
|
|
25
21
|
connectionManager?: ConnectionManager
|
|
26
|
-
rpcRequestTimeout?: number
|
|
27
|
-
|
|
28
22
|
}
|
|
29
23
|
|
|
30
24
|
interface ForwardingTableEntry {
|
|
@@ -32,46 +26,32 @@ interface ForwardingTableEntry {
|
|
|
32
26
|
peerDescriptors: PeerDescriptor[]
|
|
33
27
|
}
|
|
34
28
|
|
|
35
|
-
export interface IRouter {
|
|
36
|
-
doRouteMessage(routedMessage: RouteMessageWrapper, mode: RoutingMode, excludedPeer?: PeerDescriptor): RouteMessageAck
|
|
37
|
-
send(msg: Message, reachableThrough: PeerDescriptor[]): void
|
|
38
|
-
isMostLikelyDuplicate(requestId: string): boolean
|
|
39
|
-
addToDuplicateDetector(requestId: string): void
|
|
40
|
-
addRoutingSession(session: RoutingSession): void
|
|
41
|
-
removeRoutingSession(sessionId: string): void
|
|
42
|
-
stop(): void
|
|
43
|
-
}
|
|
44
|
-
|
|
45
29
|
const logger = new Logger(module)
|
|
46
30
|
|
|
47
|
-
export class Router
|
|
48
|
-
|
|
49
|
-
private readonly
|
|
50
|
-
private readonly connections: Map<PeerIDKey, DhtNodeRpcRemote>
|
|
51
|
-
private readonly serviceId: ServiceID
|
|
52
|
-
private readonly forwardingTable: Map<string, ForwardingTableEntry> = new Map()
|
|
31
|
+
export class Router {
|
|
32
|
+
|
|
33
|
+
private readonly forwardingTable: Map<NodeID, ForwardingTableEntry> = new Map()
|
|
53
34
|
private ongoingRoutingSessions: Map<string, RoutingSession> = new Map()
|
|
35
|
+
// TODO use config option or named constant?
|
|
54
36
|
private readonly duplicateRequestDetector: DuplicateDetector = new DuplicateDetector(100000, 100)
|
|
55
37
|
private stopped = false
|
|
38
|
+
private readonly config: RouterConfig
|
|
56
39
|
|
|
57
40
|
constructor(config: RouterConfig) {
|
|
58
|
-
this.
|
|
59
|
-
this.
|
|
60
|
-
this.connections = config.connections
|
|
61
|
-
this.serviceId = config.serviceId
|
|
62
|
-
this.registerLocalRpcMethods(config)
|
|
41
|
+
this.config = config
|
|
42
|
+
this.registerLocalRpcMethods()
|
|
63
43
|
}
|
|
64
44
|
|
|
65
|
-
private registerLocalRpcMethods(
|
|
45
|
+
private registerLocalRpcMethods() {
|
|
66
46
|
const rpcLocal = new RouterRpcLocal({
|
|
67
47
|
doRouteMessage: (routedMessage: RouteMessageWrapper, mode?: RoutingMode) => this.doRouteMessage(routedMessage, mode),
|
|
68
|
-
addContact: (contact: PeerDescriptor, setActive: boolean) => config.addContact(contact, setActive),
|
|
48
|
+
addContact: (contact: PeerDescriptor, setActive: boolean) => this.config.addContact(contact, setActive),
|
|
69
49
|
setForwardingEntries: (routedMessage: RouteMessageWrapper) => this.setForwardingEntries(routedMessage),
|
|
70
50
|
duplicateRequestDetector: this.duplicateRequestDetector,
|
|
71
|
-
localPeerDescriptor: this.localPeerDescriptor,
|
|
72
|
-
connectionManager: config.connectionManager
|
|
51
|
+
localPeerDescriptor: this.config.localPeerDescriptor,
|
|
52
|
+
connectionManager: this.config.connectionManager
|
|
73
53
|
})
|
|
74
|
-
this.rpcCommunicator.registerRpcMethod(
|
|
54
|
+
this.config.rpcCommunicator.registerRpcMethod(
|
|
75
55
|
RouteMessageWrapper,
|
|
76
56
|
RouteMessageAck,
|
|
77
57
|
'routeMessage',
|
|
@@ -82,7 +62,7 @@ export class Router implements IRouter {
|
|
|
82
62
|
return rpcLocal.routeMessage(routedMessage)
|
|
83
63
|
}
|
|
84
64
|
)
|
|
85
|
-
this.rpcCommunicator.registerRpcMethod(
|
|
65
|
+
this.config.rpcCommunicator.registerRpcMethod(
|
|
86
66
|
RouteMessageWrapper,
|
|
87
67
|
RouteMessageAck,
|
|
88
68
|
'forwardMessage',
|
|
@@ -97,16 +77,15 @@ export class Router implements IRouter {
|
|
|
97
77
|
}
|
|
98
78
|
|
|
99
79
|
public send(msg: Message, reachableThrough: PeerDescriptor[]): void {
|
|
100
|
-
msg.sourceDescriptor = this.localPeerDescriptor
|
|
80
|
+
msg.sourceDescriptor = this.config.localPeerDescriptor
|
|
101
81
|
const targetPeerDescriptor = msg.targetDescriptor!
|
|
102
|
-
const forwardingEntry = this.forwardingTable.get(
|
|
82
|
+
const forwardingEntry = this.forwardingTable.get(getNodeIdFromPeerDescriptor(targetPeerDescriptor))
|
|
103
83
|
if (forwardingEntry && forwardingEntry.peerDescriptors.length > 0) {
|
|
104
|
-
const forwardingPeer = forwardingEntry.peerDescriptors[0]
|
|
105
84
|
const forwardedMessage: RouteMessageWrapper = {
|
|
106
85
|
message: msg,
|
|
107
86
|
requestId: v4(),
|
|
108
|
-
|
|
109
|
-
sourcePeer: this.localPeerDescriptor,
|
|
87
|
+
target: forwardingEntry.peerDescriptors[0].nodeId,
|
|
88
|
+
sourcePeer: this.config.localPeerDescriptor,
|
|
110
89
|
reachableThrough,
|
|
111
90
|
routingPath: []
|
|
112
91
|
}
|
|
@@ -120,8 +99,8 @@ export class Router implements IRouter {
|
|
|
120
99
|
const routedMessage: RouteMessageWrapper = {
|
|
121
100
|
message: msg,
|
|
122
101
|
requestId: v4(),
|
|
123
|
-
|
|
124
|
-
sourcePeer: this.localPeerDescriptor,
|
|
102
|
+
target: targetPeerDescriptor.nodeId,
|
|
103
|
+
sourcePeer: this.config.localPeerDescriptor,
|
|
125
104
|
reachableThrough,
|
|
126
105
|
routingPath: []
|
|
127
106
|
}
|
|
@@ -139,7 +118,7 @@ export class Router implements IRouter {
|
|
|
139
118
|
return createRouteMessageAck(routedMessage, RouteMessageError.STOPPED)
|
|
140
119
|
}
|
|
141
120
|
logger.trace(`Routing message ${routedMessage.requestId} from ${getNodeIdFromPeerDescriptor(routedMessage.sourcePeer!)} `
|
|
142
|
-
+ `to ${
|
|
121
|
+
+ `to ${getNodeIdFromBinary(routedMessage.target)}`)
|
|
143
122
|
const session = this.createRoutingSession(routedMessage, mode, excludedPeer)
|
|
144
123
|
const contacts = session.updateAndGetRoutablePeers()
|
|
145
124
|
if (contacts.length > 0) {
|
|
@@ -157,6 +136,7 @@ export class Router implements IRouter {
|
|
|
157
136
|
})
|
|
158
137
|
setImmediate(async () => {
|
|
159
138
|
try {
|
|
139
|
+
// TODO use config option or named constant?
|
|
160
140
|
await withTimeout(eventReceived, 10000)
|
|
161
141
|
logger.trace('raceEvents ended from routingSession: ' + session.sessionId)
|
|
162
142
|
} catch (e) {
|
|
@@ -173,21 +153,22 @@ export class Router implements IRouter {
|
|
|
173
153
|
}
|
|
174
154
|
}
|
|
175
155
|
|
|
176
|
-
private createRoutingSession(routedMessage: RouteMessageWrapper, mode: RoutingMode,
|
|
177
|
-
const
|
|
178
|
-
if (
|
|
179
|
-
|
|
156
|
+
private createRoutingSession(routedMessage: RouteMessageWrapper, mode: RoutingMode, excludedNode?: PeerDescriptor): RoutingSession {
|
|
157
|
+
const excludedNodeIds = new Set<NodeID>(routedMessage.routingPath.map((descriptor) => getNodeIdFromPeerDescriptor(descriptor)))
|
|
158
|
+
if (excludedNode) {
|
|
159
|
+
excludedNodeIds.add(getNodeIdFromPeerDescriptor(excludedNode))
|
|
180
160
|
}
|
|
181
|
-
logger.trace('routing session created with connections: ' + this.connections.size)
|
|
182
|
-
return new RoutingSession(
|
|
183
|
-
this.rpcCommunicator,
|
|
184
|
-
this.localPeerDescriptor,
|
|
161
|
+
logger.trace('routing session created with connections: ' + this.config.connections.size)
|
|
162
|
+
return new RoutingSession({
|
|
163
|
+
rpcCommunicator: this.config.rpcCommunicator,
|
|
164
|
+
localPeerDescriptor: this.config.localPeerDescriptor,
|
|
185
165
|
routedMessage,
|
|
186
|
-
this.connections,
|
|
187
|
-
|
|
166
|
+
connections: this.config.connections,
|
|
167
|
+
// TODO use config option or named constant?
|
|
168
|
+
parallelism: areEqualPeerDescriptors(this.config.localPeerDescriptor, routedMessage.sourcePeer!) ? 2 : 1,
|
|
188
169
|
mode,
|
|
189
|
-
|
|
190
|
-
)
|
|
170
|
+
excludedNodeIds
|
|
171
|
+
})
|
|
191
172
|
}
|
|
192
173
|
|
|
193
174
|
public isMostLikelyDuplicate(requestId: string): boolean {
|
|
@@ -221,23 +202,23 @@ export class Router implements IRouter {
|
|
|
221
202
|
|
|
222
203
|
private setForwardingEntries(routedMessage: RouteMessageWrapper): void {
|
|
223
204
|
const reachableThroughWithoutSelf = routedMessage.reachableThrough.filter((peer) => {
|
|
224
|
-
return !areEqualPeerDescriptors(peer, this.localPeerDescriptor)
|
|
205
|
+
return !areEqualPeerDescriptors(peer, this.config.localPeerDescriptor)
|
|
225
206
|
})
|
|
226
207
|
|
|
227
208
|
if (reachableThroughWithoutSelf.length > 0) {
|
|
228
|
-
const
|
|
229
|
-
if (this.forwardingTable.has(
|
|
230
|
-
const oldEntry = this.forwardingTable.get(
|
|
209
|
+
const sourceNodeId = getNodeIdFromPeerDescriptor(routedMessage.sourcePeer!)
|
|
210
|
+
if (this.forwardingTable.has(sourceNodeId)) {
|
|
211
|
+
const oldEntry = this.forwardingTable.get(sourceNodeId)
|
|
231
212
|
clearTimeout(oldEntry!.timeout)
|
|
232
|
-
this.forwardingTable.delete(
|
|
213
|
+
this.forwardingTable.delete(sourceNodeId)
|
|
233
214
|
}
|
|
234
215
|
const forwardingEntry: ForwardingTableEntry = {
|
|
235
216
|
peerDescriptors: reachableThroughWithoutSelf,
|
|
236
217
|
timeout: setTimeout(() => {
|
|
237
|
-
this.forwardingTable.delete(
|
|
238
|
-
}, 10000)
|
|
218
|
+
this.forwardingTable.delete(sourceNodeId)
|
|
219
|
+
}, 10000) // TODO use config option or named constant?
|
|
239
220
|
}
|
|
240
|
-
this.forwardingTable.set(
|
|
221
|
+
this.forwardingTable.set(sourceNodeId, forwardingEntry)
|
|
241
222
|
}
|
|
242
223
|
}
|
|
243
224
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { Logger } from '@streamr/utils'
|
|
1
|
+
import { Logger, areEqualBinaries } from '@streamr/utils'
|
|
2
2
|
import { ConnectionManager } from '../../connection/ConnectionManager'
|
|
3
3
|
import { areEqualPeerDescriptors, getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
4
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'
|
|
8
|
+
import { getNodeIdFromBinary } from '../../helpers/nodeId'
|
|
8
9
|
|
|
9
10
|
interface RouterRpcLocalConfig {
|
|
10
11
|
doRouteMessage: (routedMessage: RouteMessageWrapper, mode?: RoutingMode) => RouteMessageAck
|
|
@@ -36,13 +37,13 @@ export class RouterRpcLocal implements IRouterRpc {
|
|
|
36
37
|
async routeMessage(routedMessage: RouteMessageWrapper): Promise<RouteMessageAck> {
|
|
37
38
|
if (this.config.duplicateRequestDetector.isMostLikelyDuplicate(routedMessage.requestId)) {
|
|
38
39
|
logger.trace(`Routing message ${routedMessage.requestId} from ${getNodeIdFromPeerDescriptor(routedMessage.sourcePeer!)} `
|
|
39
|
-
+ `to ${
|
|
40
|
+
+ `to ${getNodeIdFromBinary(routedMessage.target)} is likely a duplicate`)
|
|
40
41
|
return createRouteMessageAck(routedMessage, RouteMessageError.DUPLICATE)
|
|
41
42
|
}
|
|
42
43
|
logger.trace(`Processing received routeMessage ${routedMessage.requestId}`)
|
|
43
44
|
this.config.addContact(routedMessage.sourcePeer!, true)
|
|
44
45
|
this.config.duplicateRequestDetector.add(routedMessage.requestId)
|
|
45
|
-
if (
|
|
46
|
+
if (areEqualBinaries(this.config.localPeerDescriptor.nodeId, routedMessage.target)) {
|
|
46
47
|
logger.trace(`routing message targeted to self ${routedMessage.requestId}`)
|
|
47
48
|
this.config.setForwardingEntries(routedMessage)
|
|
48
49
|
this.config.connectionManager?.handleMessage(routedMessage.message!)
|
|
@@ -55,13 +56,13 @@ export class RouterRpcLocal implements IRouterRpc {
|
|
|
55
56
|
async forwardMessage(forwardMessage: RouteMessageWrapper): Promise<RouteMessageAck> {
|
|
56
57
|
if (this.config.duplicateRequestDetector.isMostLikelyDuplicate(forwardMessage.requestId)) {
|
|
57
58
|
logger.trace(`Forwarding message ${forwardMessage.requestId} from ${getNodeIdFromPeerDescriptor(forwardMessage.sourcePeer!)} `
|
|
58
|
-
+ `to ${
|
|
59
|
+
+ `to ${getNodeIdFromBinary(forwardMessage.target)} is likely a duplicate`)
|
|
59
60
|
return createRouteMessageAck(forwardMessage, RouteMessageError.DUPLICATE)
|
|
60
61
|
}
|
|
61
62
|
logger.trace(`Processing received forward routeMessage ${forwardMessage.requestId}`)
|
|
62
63
|
this.config.addContact(forwardMessage.sourcePeer!, true)
|
|
63
64
|
this.config.duplicateRequestDetector.add(forwardMessage.requestId)
|
|
64
|
-
if (
|
|
65
|
+
if (areEqualBinaries(this.config.localPeerDescriptor.nodeId, forwardMessage.target)) {
|
|
65
66
|
return this.forwardToDestination(forwardMessage)
|
|
66
67
|
} else {
|
|
67
68
|
return this.config.doRouteMessage(forwardMessage, RoutingMode.FORWARD)
|
|
@@ -75,7 +76,7 @@ export class RouterRpcLocal implements IRouterRpc {
|
|
|
75
76
|
this.config.connectionManager?.handleMessage(forwardedMessage)
|
|
76
77
|
return createRouteMessageAck(routedMessage)
|
|
77
78
|
}
|
|
78
|
-
return this.config.doRouteMessage({ ...routedMessage,
|
|
79
|
+
return this.config.doRouteMessage({ ...routedMessage, target: forwardedMessage.targetDescriptor!.nodeId })
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
}
|
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Logger, areEqualBinaries } from '@streamr/utils'
|
|
2
2
|
import { v4 } from 'uuid'
|
|
3
3
|
import {
|
|
4
|
-
areEqualPeerDescriptors,
|
|
5
4
|
getNodeIdFromPeerDescriptor
|
|
6
5
|
} from '../../helpers/peerIdFromPeerDescriptor'
|
|
7
|
-
import {
|
|
6
|
+
import { RouteMessageError, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
7
|
+
import { RouterRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
8
8
|
import { RpcRemote } from '../contact/RpcRemote'
|
|
9
|
-
import { Logger } from '@streamr/utils'
|
|
10
9
|
import { getPreviousPeer } from './getPreviousPeer'
|
|
11
10
|
|
|
12
11
|
const logger = new Logger(module)
|
|
13
12
|
|
|
14
|
-
export class RouterRpcRemote extends RpcRemote<
|
|
13
|
+
export class RouterRpcRemote extends RpcRemote<RouterRpcClient> {
|
|
15
14
|
|
|
16
15
|
async routeMessage(params: RouteMessageWrapper): Promise<boolean> {
|
|
17
16
|
const message: RouteMessageWrapper = {
|
|
18
|
-
|
|
17
|
+
target: params.target,
|
|
19
18
|
sourcePeer: params.sourcePeer,
|
|
20
19
|
message: params.message,
|
|
21
20
|
requestId: params.requestId ?? v4(),
|
|
@@ -23,13 +22,13 @@ export class RouterRpcRemote extends RpcRemote<IRouterRpcClient> {
|
|
|
23
22
|
routingPath: params.routingPath
|
|
24
23
|
}
|
|
25
24
|
const options = this.formDhtRpcOptions({
|
|
26
|
-
|
|
25
|
+
connect: false
|
|
27
26
|
})
|
|
28
27
|
try {
|
|
29
28
|
const ack = await this.getClient().routeMessage(message, options)
|
|
30
29
|
// Success signal if sent to destination and error includes duplicate
|
|
31
30
|
if (ack.error === RouteMessageError.DUPLICATE
|
|
32
|
-
&&
|
|
31
|
+
&& areEqualBinaries(params.target, this.getPeerDescriptor().nodeId)
|
|
33
32
|
) {
|
|
34
33
|
return true
|
|
35
34
|
} else if (ack.error !== undefined) {
|
|
@@ -48,7 +47,7 @@ export class RouterRpcRemote extends RpcRemote<IRouterRpcClient> {
|
|
|
48
47
|
|
|
49
48
|
async forwardMessage(params: RouteMessageWrapper): Promise<boolean> {
|
|
50
49
|
const message: RouteMessageWrapper = {
|
|
51
|
-
|
|
50
|
+
target: params.target,
|
|
52
51
|
sourcePeer: params.sourcePeer,
|
|
53
52
|
message: params.message,
|
|
54
53
|
requestId: params.requestId ?? v4(),
|
|
@@ -56,7 +55,7 @@ export class RouterRpcRemote extends RpcRemote<IRouterRpcClient> {
|
|
|
56
55
|
routingPath: params.routingPath
|
|
57
56
|
}
|
|
58
57
|
const options = this.formDhtRpcOptions({
|
|
59
|
-
|
|
58
|
+
connect: false
|
|
60
59
|
})
|
|
61
60
|
try {
|
|
62
61
|
const ack = await this.getClient().forwardMessage(message, options)
|
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
|
|
2
2
|
import { SortedContactList } from '../contact/SortedContactList'
|
|
3
|
-
import {
|
|
4
|
-
import { getNodeIdFromPeerDescriptor, peerIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
3
|
+
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
5
4
|
import { Logger } from '@streamr/utils'
|
|
6
5
|
import EventEmitter from 'eventemitter3'
|
|
7
6
|
import { v4 } from 'uuid'
|
|
8
7
|
import { PeerDescriptor, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
9
8
|
import { RouterRpcRemote } from './RouterRpcRemote'
|
|
10
9
|
import { RoutingRpcCommunicator } from '../../transport/RoutingRpcCommunicator'
|
|
11
|
-
import {
|
|
12
|
-
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
10
|
+
import { RecursiveOperationRpcClient, RouterRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
13
11
|
import { Contact } from '../contact/Contact'
|
|
14
|
-
import {
|
|
12
|
+
import { RecursiveOperationRpcRemote } from '../recursive-operation/RecursiveOperationRpcRemote'
|
|
15
13
|
import { EXISTING_CONNECTION_TIMEOUT } from '../contact/RpcRemote'
|
|
16
14
|
import { getPreviousPeer } from './getPreviousPeer'
|
|
15
|
+
import { NodeID, getNodeIdFromBinary } from '../../helpers/nodeId'
|
|
17
16
|
|
|
18
17
|
const logger = new Logger(module)
|
|
19
18
|
|
|
@@ -22,7 +21,7 @@ const MAX_FAILED_HOPS = 2
|
|
|
22
21
|
class RemoteContact extends Contact {
|
|
23
22
|
|
|
24
23
|
private routerRpcRemote: RouterRpcRemote
|
|
25
|
-
private
|
|
24
|
+
private recursiveOperationRpcRemote: RecursiveOperationRpcRemote
|
|
26
25
|
|
|
27
26
|
constructor(peer: DhtNodeRpcRemote, localPeerDescriptor: PeerDescriptor, rpcCommunicator: RoutingRpcCommunicator) {
|
|
28
27
|
super(peer.getPeerDescriptor())
|
|
@@ -30,14 +29,16 @@ class RemoteContact extends Contact {
|
|
|
30
29
|
localPeerDescriptor,
|
|
31
30
|
peer.getPeerDescriptor(),
|
|
32
31
|
peer.getServiceId(),
|
|
33
|
-
|
|
32
|
+
rpcCommunicator,
|
|
33
|
+
RouterRpcClient,
|
|
34
34
|
EXISTING_CONNECTION_TIMEOUT
|
|
35
35
|
)
|
|
36
|
-
this.
|
|
36
|
+
this.recursiveOperationRpcRemote = new RecursiveOperationRpcRemote(
|
|
37
37
|
localPeerDescriptor,
|
|
38
38
|
peer.getPeerDescriptor(),
|
|
39
39
|
peer.getServiceId(),
|
|
40
|
-
|
|
40
|
+
rpcCommunicator,
|
|
41
|
+
RecursiveOperationRpcClient,
|
|
41
42
|
EXISTING_CONNECTION_TIMEOUT
|
|
42
43
|
)
|
|
43
44
|
}
|
|
@@ -46,75 +47,66 @@ class RemoteContact extends Contact {
|
|
|
46
47
|
return this.routerRpcRemote
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
return this.
|
|
50
|
+
getRecursiveOperationRpcRemote(): RecursiveOperationRpcRemote {
|
|
51
|
+
return this.recursiveOperationRpcRemote
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
export interface RoutingSessionEvents {
|
|
55
56
|
// This event is emitted when a peer responds with a success ack
|
|
56
57
|
// to routeMessage call
|
|
57
|
-
routingSucceeded: (
|
|
58
|
-
partialSuccess: (
|
|
59
|
-
|
|
58
|
+
routingSucceeded: () => void
|
|
59
|
+
partialSuccess: () => void
|
|
60
60
|
// This event is emitted when all the candidates have been gone
|
|
61
61
|
// through, and none of them responds with a success ack
|
|
62
|
-
routingFailed: (
|
|
63
|
-
stopped: (
|
|
62
|
+
routingFailed: () => void
|
|
63
|
+
stopped: () => void
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
export enum RoutingMode { ROUTE, FORWARD,
|
|
66
|
+
export enum RoutingMode { ROUTE, FORWARD, RECURSIVE }
|
|
67
|
+
|
|
68
|
+
interface RoutingSessionConfig {
|
|
69
|
+
rpcCommunicator: RoutingRpcCommunicator
|
|
70
|
+
localPeerDescriptor: PeerDescriptor
|
|
71
|
+
routedMessage: RouteMessageWrapper
|
|
72
|
+
connections: Map<NodeID, DhtNodeRpcRemote>
|
|
73
|
+
parallelism: number
|
|
74
|
+
mode: RoutingMode
|
|
75
|
+
excludedNodeIds?: Set<NodeID>
|
|
76
|
+
}
|
|
67
77
|
|
|
68
78
|
export class RoutingSession extends EventEmitter<RoutingSessionEvents> {
|
|
69
79
|
|
|
70
80
|
public readonly sessionId = v4()
|
|
71
|
-
private
|
|
72
|
-
private ongoingRequests: Set<PeerIDKey> = new Set()
|
|
81
|
+
private ongoingRequests: Set<NodeID> = new Set()
|
|
73
82
|
private contactList: SortedContactList<RemoteContact>
|
|
74
|
-
private readonly localPeerDescriptor: PeerDescriptor
|
|
75
|
-
private readonly messageToRoute: RouteMessageWrapper
|
|
76
|
-
private connections: Map<PeerIDKey, DhtNodeRpcRemote>
|
|
77
|
-
private readonly parallelism: number
|
|
78
83
|
private failedHopCounter = 0
|
|
79
84
|
private successfulHopCounter = 0
|
|
80
|
-
private readonly mode: RoutingMode = RoutingMode.ROUTE
|
|
81
85
|
private stopped = false
|
|
86
|
+
private readonly config: RoutingSessionConfig
|
|
82
87
|
|
|
83
|
-
constructor(
|
|
84
|
-
rpcCommunicator: RoutingRpcCommunicator,
|
|
85
|
-
localPeerDescriptor: PeerDescriptor,
|
|
86
|
-
messageToRoute: RouteMessageWrapper,
|
|
87
|
-
connections: Map<PeerIDKey, DhtNodeRpcRemote>,
|
|
88
|
-
parallelism: number,
|
|
89
|
-
mode: RoutingMode = RoutingMode.ROUTE,
|
|
90
|
-
excludedPeerIDs?: PeerID[]
|
|
91
|
-
) {
|
|
88
|
+
constructor(config: RoutingSessionConfig) {
|
|
92
89
|
super()
|
|
93
|
-
this.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
this.connections = connections
|
|
97
|
-
this.parallelism = parallelism
|
|
98
|
-
this.mode = mode
|
|
99
|
-
const previousPeer = getPreviousPeer(messageToRoute)
|
|
100
|
-
const previousId = previousPeer ? PeerID.fromValue(previousPeer.nodeId) : undefined
|
|
90
|
+
this.config = config
|
|
91
|
+
const previousPeer = getPreviousPeer(config.routedMessage)
|
|
92
|
+
const previousId = previousPeer ? getNodeIdFromPeerDescriptor(previousPeer) : undefined
|
|
101
93
|
this.contactList = new SortedContactList({
|
|
102
|
-
referenceId:
|
|
94
|
+
referenceId: getNodeIdFromBinary(config.routedMessage.target),
|
|
103
95
|
maxSize: 10000, // TODO use config option or named constant?
|
|
104
96
|
allowToContainReferenceId: true,
|
|
105
|
-
|
|
106
|
-
|
|
97
|
+
nodeIdDistanceLimit: previousId,
|
|
98
|
+
excludedNodeIds: config.excludedNodeIds,
|
|
107
99
|
emitEvents: false
|
|
108
100
|
})
|
|
109
101
|
}
|
|
110
102
|
|
|
111
|
-
private onRequestFailed(
|
|
103
|
+
private onRequestFailed(nodeId: NodeID) {
|
|
112
104
|
logger.trace('onRequestFailed() sessionId: ' + this.sessionId)
|
|
113
105
|
if (this.stopped) {
|
|
114
106
|
return
|
|
115
107
|
}
|
|
116
|
-
if (this.ongoingRequests.has(
|
|
117
|
-
this.ongoingRequests.delete(
|
|
108
|
+
if (this.ongoingRequests.has(nodeId)) {
|
|
109
|
+
this.ongoingRequests.delete(nodeId)
|
|
118
110
|
}
|
|
119
111
|
const contacts = this.updateAndGetRoutablePeers()
|
|
120
112
|
if (contacts.length === 0 && this.ongoingRequests.size === 0) {
|
|
@@ -131,9 +123,9 @@ export class RoutingSession extends EventEmitter<RoutingSessionEvents> {
|
|
|
131
123
|
|
|
132
124
|
private emitFailure() {
|
|
133
125
|
if (this.successfulHopCounter >= 1) {
|
|
134
|
-
this.emit('partialSuccess'
|
|
126
|
+
this.emit('partialSuccess')
|
|
135
127
|
} else {
|
|
136
|
-
this.emit('routingFailed'
|
|
128
|
+
this.emit('routingFailed')
|
|
137
129
|
}
|
|
138
130
|
}
|
|
139
131
|
|
|
@@ -144,10 +136,10 @@ export class RoutingSession extends EventEmitter<RoutingSessionEvents> {
|
|
|
144
136
|
}
|
|
145
137
|
this.successfulHopCounter += 1
|
|
146
138
|
const contacts = this.updateAndGetRoutablePeers()
|
|
147
|
-
if (this.successfulHopCounter >= this.parallelism || contacts.length === 0) {
|
|
139
|
+
if (this.successfulHopCounter >= this.config.parallelism || contacts.length === 0) {
|
|
148
140
|
// TODO should call this.stop() so that we do cleanup? (after the routingSucceeded call)
|
|
149
141
|
this.stopped = true
|
|
150
|
-
this.emit('routingSucceeded'
|
|
142
|
+
this.emit('routingSucceeded')
|
|
151
143
|
} else if (contacts.length > 0 && this.ongoingRequests.size === 0) {
|
|
152
144
|
this.sendMoreRequests(contacts)
|
|
153
145
|
}
|
|
@@ -158,13 +150,13 @@ export class RoutingSession extends EventEmitter<RoutingSessionEvents> {
|
|
|
158
150
|
return false
|
|
159
151
|
}
|
|
160
152
|
const msg = {
|
|
161
|
-
...this.
|
|
162
|
-
routingPath: this.
|
|
153
|
+
...this.config.routedMessage,
|
|
154
|
+
routingPath: this.config.routedMessage.routingPath.concat([this.config.localPeerDescriptor])
|
|
163
155
|
}
|
|
164
|
-
if (this.mode === RoutingMode.FORWARD) {
|
|
156
|
+
if (this.config.mode === RoutingMode.FORWARD) {
|
|
165
157
|
return contact.getRouterRpcRemote().forwardMessage(msg)
|
|
166
|
-
} else if (this.mode === RoutingMode.
|
|
167
|
-
return contact.
|
|
158
|
+
} else if (this.config.mode === RoutingMode.RECURSIVE) {
|
|
159
|
+
return contact.getRecursiveOperationRpcRemote().routeRequest(msg)
|
|
168
160
|
} else {
|
|
169
161
|
return contact.getRouterRpcRemote().routeMessage(msg)
|
|
170
162
|
}
|
|
@@ -173,16 +165,15 @@ export class RoutingSession extends EventEmitter<RoutingSessionEvents> {
|
|
|
173
165
|
updateAndGetRoutablePeers(): RemoteContact[] {
|
|
174
166
|
logger.trace('getRoutablePeers() sessionId: ' + this.sessionId)
|
|
175
167
|
// Remove stale contacts that may have been removed from connections
|
|
176
|
-
this.contactList.
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
this.contactList.removeContact(peerId)
|
|
168
|
+
this.contactList.getContactIds().forEach((nodeId) => {
|
|
169
|
+
if (!this.config.connections.has(nodeId)) {
|
|
170
|
+
this.contactList.removeContact(nodeId)
|
|
180
171
|
}
|
|
181
172
|
})
|
|
182
|
-
const contacts = Array.from(this.connections.values())
|
|
183
|
-
.map((peer) => new RemoteContact(peer, this.localPeerDescriptor, this.rpcCommunicator))
|
|
173
|
+
const contacts = Array.from(this.config.connections.values())
|
|
174
|
+
.map((peer) => new RemoteContact(peer, this.config.localPeerDescriptor, this.config.rpcCommunicator))
|
|
184
175
|
this.contactList.addContacts(contacts)
|
|
185
|
-
return this.contactList.getUncontactedContacts(this.parallelism)
|
|
176
|
+
return this.contactList.getUncontactedContacts(this.config.parallelism)
|
|
186
177
|
}
|
|
187
178
|
|
|
188
179
|
sendMoreRequests(uncontacted: RemoteContact[]): void {
|
|
@@ -199,19 +190,19 @@ export class RoutingSession extends EventEmitter<RoutingSessionEvents> {
|
|
|
199
190
|
this.emitFailure()
|
|
200
191
|
return
|
|
201
192
|
}
|
|
202
|
-
while ((this.ongoingRequests.size < this.parallelism) && (uncontacted.length > 0) && !this.stopped) {
|
|
193
|
+
while ((this.ongoingRequests.size < this.config.parallelism) && (uncontacted.length > 0) && !this.stopped) {
|
|
203
194
|
const nextPeer = uncontacted.shift()
|
|
204
195
|
// eslint-disable-next-line max-len
|
|
205
196
|
logger.trace(`Sending routeMessage request to contact: ${getNodeIdFromPeerDescriptor(nextPeer!.getPeerDescriptor())} (sessionId=${this.sessionId})`)
|
|
206
|
-
this.contactList.setContacted(nextPeer!.
|
|
207
|
-
this.ongoingRequests.add(nextPeer!.
|
|
197
|
+
this.contactList.setContacted(nextPeer!.getNodeId())
|
|
198
|
+
this.ongoingRequests.add(nextPeer!.getNodeId())
|
|
208
199
|
setImmediate(async () => {
|
|
209
200
|
try {
|
|
210
201
|
const succeeded = await this.sendRouteMessageRequest(nextPeer!)
|
|
211
202
|
if (succeeded) {
|
|
212
203
|
this.onRequestSucceeded()
|
|
213
204
|
} else {
|
|
214
|
-
this.onRequestFailed(nextPeer!.
|
|
205
|
+
this.onRequestFailed(nextPeer!.getNodeId())
|
|
215
206
|
}
|
|
216
207
|
} catch (e) {
|
|
217
208
|
logger.debug('Unable to route message ', { error: e })
|
|
@@ -225,7 +216,7 @@ export class RoutingSession extends EventEmitter<RoutingSessionEvents> {
|
|
|
225
216
|
public stop(): void {
|
|
226
217
|
this.stopped = true
|
|
227
218
|
this.contactList.stop()
|
|
228
|
-
this.emit('stopped'
|
|
219
|
+
this.emit('stopped')
|
|
229
220
|
this.removeAllListeners()
|
|
230
221
|
}
|
|
231
222
|
}
|