@streamr/dht 100.0.0-testnet-one.4 → 100.0.0-testnet-two.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package.json +66 -0
- 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/Handshaker.d.ts +1 -1
- package/dist/src/connection/Handshaker.js +14 -5
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +4 -7
- package/dist/src/connection/ManagedConnection.js +9 -13
- 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 +21 -14
- 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 +48 -44
- 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 +41 -54
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +2 -3
- package/dist/src/dht/DhtNodeRpcLocal.js +1 -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 +9 -11
- 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 -23
- 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/helpers/versionCompatibility.d.ts +2 -0
- package/dist/src/helpers/versionCompatibility.js +18 -0
- package/dist/src/helpers/versionCompatibility.js.map +1 -0
- 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 +49 -65
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +43 -58
- 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 +20 -24
- 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/Handshaker.ts +21 -6
- package/src/connection/ManagedConnection.ts +20 -34
- 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 +25 -18
- 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 +57 -61
- 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 +69 -71
- package/src/dht/DhtNodeRpcLocal.ts +1 -3
- package/src/dht/DhtNodeRpcRemote.ts +14 -18
- 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 -27
- 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/helpers/versionCompatibility.ts +13 -0
- 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 +67 -90
- 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/unit/versionCompatibility.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/tsconfig.browser.json +2 -1
- package/tsconfig.jest.json +2 -1
- package/tsconfig.node.json +2 -1
- 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,15 +1,12 @@
|
|
|
1
|
-
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
2
1
|
import { CountMetric, LevelMetric, Logger, Metric, MetricsContext, MetricsDefinition, RateMetric, waitForEvent3 } from '@streamr/utils'
|
|
3
2
|
import { EventEmitter } from 'eventemitter3'
|
|
4
3
|
import { Contact } from '../dht/contact/Contact'
|
|
5
4
|
import { SortedContactList } from '../dht/contact/SortedContactList'
|
|
6
5
|
import { DuplicateDetector } from '../dht/routing/DuplicateDetector'
|
|
7
|
-
import { PeerIDKey } from '../helpers/PeerID'
|
|
8
6
|
import * as Err from '../helpers/errors'
|
|
9
7
|
import {
|
|
10
8
|
areEqualPeerDescriptors,
|
|
11
9
|
getNodeIdFromPeerDescriptor,
|
|
12
|
-
keyFromPeerDescriptor,
|
|
13
10
|
peerIdFromPeerDescriptor
|
|
14
11
|
} from '../helpers/peerIdFromPeerDescriptor'
|
|
15
12
|
import { protoToString } from '../helpers/protoToString'
|
|
@@ -25,7 +22,7 @@ import {
|
|
|
25
22
|
UnlockRequest
|
|
26
23
|
} from '../proto/packages/dht/protos/DhtRpc'
|
|
27
24
|
import { ConnectionLockRpcClient } from '../proto/packages/dht/protos/DhtRpc.client'
|
|
28
|
-
import { ITransport, TransportEvents } from '../transport/ITransport'
|
|
25
|
+
import { DEFAULT_SEND_OPTIONS, ITransport, SendOptions, TransportEvents } from '../transport/ITransport'
|
|
29
26
|
import { RoutingRpcCommunicator } from '../transport/RoutingRpcCommunicator'
|
|
30
27
|
import { ConnectionLockHandler, LockID } from './ConnectionLockHandler'
|
|
31
28
|
import { ConnectorFacade } from './ConnectorFacade'
|
|
@@ -34,6 +31,7 @@ import { ConnectionLockRpcRemote } from './ConnectionLockRpcRemote'
|
|
|
34
31
|
import { WEBRTC_CLEANUP } from './webrtc/NodeWebrtcConnection'
|
|
35
32
|
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
36
33
|
import { ConnectionLockRpcLocal } from './ConnectionLockRpcLocal'
|
|
34
|
+
import { NodeID } from '../helpers/nodeId'
|
|
37
35
|
|
|
38
36
|
export interface ConnectionManagerConfig {
|
|
39
37
|
maxConnections?: number
|
|
@@ -55,9 +53,6 @@ interface ConnectionManagerMetrics extends MetricsDefinition {
|
|
|
55
53
|
connectionTotalFailureCount: Metric
|
|
56
54
|
}
|
|
57
55
|
|
|
58
|
-
// TODO move this type identifiers.ts and use also in other classes (and rename to ServiceID)
|
|
59
|
-
type ServiceId = string
|
|
60
|
-
|
|
61
56
|
const logger = new Logger(module)
|
|
62
57
|
|
|
63
58
|
enum ConnectionManagerState {
|
|
@@ -68,8 +63,8 @@ enum ConnectionManagerState {
|
|
|
68
63
|
}
|
|
69
64
|
|
|
70
65
|
export interface ConnectionLocker {
|
|
71
|
-
lockConnection(targetDescriptor: PeerDescriptor,
|
|
72
|
-
unlockConnection(targetDescriptor: PeerDescriptor,
|
|
66
|
+
lockConnection(targetDescriptor: PeerDescriptor, lockId: LockID): void
|
|
67
|
+
unlockConnection(targetDescriptor: PeerDescriptor, lockId: LockID): void
|
|
73
68
|
weakLockConnection(targetDescriptor: PeerDescriptor): void
|
|
74
69
|
weakUnlockConnection(targetDescriptor: PeerDescriptor): void
|
|
75
70
|
}
|
|
@@ -107,10 +102,11 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
107
102
|
|
|
108
103
|
private config: ConnectionManagerConfig
|
|
109
104
|
private readonly metricsContext: MetricsContext
|
|
105
|
+
// TODO use config option or named constant?
|
|
110
106
|
private readonly duplicateMessageDetector: DuplicateDetector = new DuplicateDetector(100000, 100)
|
|
111
107
|
private readonly metrics: ConnectionManagerMetrics
|
|
112
108
|
private locks = new ConnectionLockHandler()
|
|
113
|
-
private connections: Map<
|
|
109
|
+
private connections: Map<NodeID, ManagedConnection> = new Map()
|
|
114
110
|
private readonly connectorFacade: ConnectorFacade
|
|
115
111
|
private rpcCommunicator?: RoutingRpcCommunicator
|
|
116
112
|
private disconnectorIntervalRef?: NodeJS.Timeout
|
|
@@ -135,11 +131,11 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
135
131
|
this.connectorFacade = this.config.createConnectorFacade()
|
|
136
132
|
this.send = this.send.bind(this)
|
|
137
133
|
this.rpcCommunicator = new RoutingRpcCommunicator(INTERNAL_SERVICE_ID, this.send, {
|
|
138
|
-
rpcRequestTimeout: 10000
|
|
134
|
+
rpcRequestTimeout: 10000 // TODO use config option or named constant?
|
|
139
135
|
})
|
|
140
136
|
const lockRpcLocal = new ConnectionLockRpcLocal({
|
|
141
|
-
addRemoteLocked: (id:
|
|
142
|
-
removeRemoteLocked: (id:
|
|
137
|
+
addRemoteLocked: (id: NodeID, lockId: LockID) => this.locks.addRemoteLocked(id, lockId),
|
|
138
|
+
removeRemoteLocked: (id: NodeID, lockId: LockID) => this.locks.removeRemoteLocked(id, lockId),
|
|
143
139
|
closeConnection: (peerDescriptor: PeerDescriptor, gracefulLeave: boolean, reason?: string) => {
|
|
144
140
|
// TODO should we have some handling for this floating promise?
|
|
145
141
|
this.closeConnection(peerDescriptor, gracefulLeave, reason)
|
|
@@ -159,13 +155,13 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
159
155
|
return
|
|
160
156
|
}
|
|
161
157
|
const disconnectionCandidates = new SortedContactList<Contact>({
|
|
162
|
-
referenceId:
|
|
158
|
+
referenceId: getNodeIdFromPeerDescriptor(this.getLocalPeerDescriptor()),
|
|
163
159
|
maxSize: 100000, // TODO use config option or named constant?
|
|
164
160
|
allowToContainReferenceId: false,
|
|
165
161
|
emitEvents: false
|
|
166
162
|
})
|
|
167
163
|
this.connections.forEach((connection) => {
|
|
168
|
-
if (!this.locks.isLocked(connection.
|
|
164
|
+
if (!this.locks.isLocked(connection.getNodeId()) && Date.now() - connection.getLastUsed() > lastUsedLimit) {
|
|
169
165
|
logger.trace('disconnecting in timeout interval: ' + getNodeIdOrUnknownFromPeerDescriptor(connection.getPeerDescriptor()))
|
|
170
166
|
disconnectionCandidates.addContact(new Contact(connection.getPeerDescriptor()!))
|
|
171
167
|
}
|
|
@@ -187,6 +183,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
187
183
|
logger.trace(`Starting ConnectionManager...`)
|
|
188
184
|
await this.connectorFacade.start(
|
|
189
185
|
(connection: ManagedConnection) => this.onNewConnection(connection),
|
|
186
|
+
(peerDescriptor: PeerDescriptor) => this.hasConnection(peerDescriptor),
|
|
190
187
|
this
|
|
191
188
|
)
|
|
192
189
|
// Garbage collection of connections
|
|
@@ -194,7 +191,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
194
191
|
logger.trace('disconnectorInterval')
|
|
195
192
|
const LAST_USED_LIMIT = 20000
|
|
196
193
|
this.garbageCollectConnections(this.config.maxConnections ?? 80, LAST_USED_LIMIT)
|
|
197
|
-
}, 5000)
|
|
194
|
+
}, 5000) // TODO use config option or named constant?
|
|
198
195
|
}
|
|
199
196
|
|
|
200
197
|
public async stop(): Promise<void> {
|
|
@@ -217,6 +214,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
217
214
|
}
|
|
218
215
|
} else {
|
|
219
216
|
logger.trace('handshake of connection not completed, force-closing')
|
|
217
|
+
// TODO use config option or named constant?
|
|
220
218
|
const eventReceived = waitForEvent3<ManagedConnectionEvents>(peer, 'disconnected', 2000)
|
|
221
219
|
// TODO should we have some handling for this floating promise?
|
|
222
220
|
peer.close(true)
|
|
@@ -252,8 +250,8 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
252
250
|
return this.locks.getNumberOfWeakLockedConnections()
|
|
253
251
|
}
|
|
254
252
|
|
|
255
|
-
public async send(message: Message,
|
|
256
|
-
if (this.state === ConnectionManagerState.STOPPED && !
|
|
253
|
+
public async send(message: Message, opts: SendOptions = DEFAULT_SEND_OPTIONS): Promise<void> {
|
|
254
|
+
if (this.state === ConnectionManagerState.STOPPED && !opts.sendIfStopped) {
|
|
257
255
|
return
|
|
258
256
|
}
|
|
259
257
|
const peerDescriptor = message.targetDescriptor!
|
|
@@ -265,18 +263,18 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
265
263
|
...message,
|
|
266
264
|
sourceDescriptor: this.getLocalPeerDescriptor()
|
|
267
265
|
}
|
|
268
|
-
const
|
|
269
|
-
let connection = this.connections.get(
|
|
270
|
-
if (!connection &&
|
|
266
|
+
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
267
|
+
let connection = this.connections.get(nodeId)
|
|
268
|
+
if (!connection && opts.connect) {
|
|
271
269
|
connection = this.connectorFacade.createConnection(peerDescriptor)
|
|
272
270
|
this.onNewConnection(connection)
|
|
273
271
|
} else if (!connection) {
|
|
274
|
-
throw new Err.SendFailed('No connection to target,
|
|
272
|
+
throw new Err.SendFailed('No connection to target, connect flag is false')
|
|
275
273
|
}
|
|
276
274
|
const binary = Message.toBinary(message)
|
|
277
275
|
this.metrics.sendBytesPerSecond.record(binary.byteLength)
|
|
278
276
|
this.metrics.sendMessagesPerSecond.record(1)
|
|
279
|
-
return connection.send(binary,
|
|
277
|
+
return connection.send(binary, opts.connect)
|
|
280
278
|
}
|
|
281
279
|
|
|
282
280
|
private isConnectionToSelf(peerDescriptor: PeerDescriptor): boolean {
|
|
@@ -294,8 +292,8 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
294
292
|
}
|
|
295
293
|
|
|
296
294
|
public getConnection(peerDescriptor: PeerDescriptor): ManagedConnection | undefined {
|
|
297
|
-
const
|
|
298
|
-
return this.connections.get(
|
|
295
|
+
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
296
|
+
return this.connections.get(nodeId)
|
|
299
297
|
}
|
|
300
298
|
|
|
301
299
|
public getLocalPeerDescriptor(): PeerDescriptor {
|
|
@@ -303,18 +301,18 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
303
301
|
}
|
|
304
302
|
|
|
305
303
|
public hasConnection(peerDescriptor: PeerDescriptor): boolean {
|
|
306
|
-
const
|
|
307
|
-
return this.connections.has(
|
|
304
|
+
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
305
|
+
return this.connections.has(nodeId)
|
|
308
306
|
}
|
|
309
307
|
|
|
310
308
|
public hasLocalLockedConnection(peerDescriptor: PeerDescriptor): boolean {
|
|
311
|
-
const
|
|
312
|
-
return this.locks.isLocalLocked(
|
|
309
|
+
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
310
|
+
return this.locks.isLocalLocked(nodeId)
|
|
313
311
|
}
|
|
314
312
|
|
|
315
313
|
public hasRemoteLockedConnection(peerDescriptor: PeerDescriptor): boolean {
|
|
316
|
-
const
|
|
317
|
-
return this.locks.isRemoteLocked(
|
|
314
|
+
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
315
|
+
return this.locks.isRemoteLocked(nodeId)
|
|
318
316
|
}
|
|
319
317
|
|
|
320
318
|
public handleMessage(message: Message): void {
|
|
@@ -370,11 +368,11 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
370
368
|
private onDisconnected(connection: ManagedConnection, gracefulLeave: boolean) {
|
|
371
369
|
logger.trace(getNodeIdOrUnknownFromPeerDescriptor(connection.getPeerDescriptor()) + ' onDisconnected() gracefulLeave: ' + gracefulLeave)
|
|
372
370
|
|
|
373
|
-
const
|
|
374
|
-
const storedConnection = this.connections.get(
|
|
371
|
+
const nodeId = getNodeIdFromPeerDescriptor(connection.getPeerDescriptor()!)
|
|
372
|
+
const storedConnection = this.connections.get(nodeId)
|
|
375
373
|
if (storedConnection && storedConnection.connectionId.equals(connection.connectionId)) {
|
|
376
|
-
this.locks.clearAllLocks(
|
|
377
|
-
this.connections.delete(
|
|
374
|
+
this.locks.clearAllLocks(nodeId)
|
|
375
|
+
this.connections.delete(nodeId)
|
|
378
376
|
logger.trace(getNodeIdOrUnknownFromPeerDescriptor(connection.getPeerDescriptor())
|
|
379
377
|
+ ' deleted connection in onDisconnected() gracefulLeave: ' + gracefulLeave)
|
|
380
378
|
this.emit('disconnected', connection.getPeerDescriptor()!, gracefulLeave)
|
|
@@ -395,7 +393,6 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
395
393
|
return false
|
|
396
394
|
}
|
|
397
395
|
logger.trace('onNewConnection()')
|
|
398
|
-
connection.offeredAsIncoming = true
|
|
399
396
|
if (!this.acceptNewConnection(connection)) {
|
|
400
397
|
return false
|
|
401
398
|
}
|
|
@@ -416,13 +413,13 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
416
413
|
private acceptNewConnection(newConnection: ManagedConnection): boolean {
|
|
417
414
|
logger.trace(getNodeIdFromPeerDescriptor(newConnection.getPeerDescriptor()!) + ' acceptIncomingConnection()')
|
|
418
415
|
const newPeerID = peerIdFromPeerDescriptor(newConnection.getPeerDescriptor()!)
|
|
419
|
-
const
|
|
420
|
-
if (this.connections.has(
|
|
416
|
+
const nodeId = getNodeIdFromPeerDescriptor(newConnection.getPeerDescriptor()!)
|
|
417
|
+
if (this.connections.has(nodeId)) {
|
|
421
418
|
if (newPeerID.hasSmallerHashThan(peerIdFromPeerDescriptor(this.getLocalPeerDescriptor()))) {
|
|
422
419
|
logger.trace(getNodeIdOrUnknownFromPeerDescriptor(newConnection.getPeerDescriptor())
|
|
423
420
|
+ ' acceptIncomingConnection() replace current connection')
|
|
424
421
|
// replace the current connection
|
|
425
|
-
const oldConnection = this.connections.get(
|
|
422
|
+
const oldConnection = this.connections.get(nodeId)!
|
|
426
423
|
logger.trace('replaced: ' + getNodeIdFromPeerDescriptor(newConnection.getPeerDescriptor()!))
|
|
427
424
|
const buffer = oldConnection.stealOutputBuffer()
|
|
428
425
|
|
|
@@ -438,14 +435,14 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
438
435
|
}
|
|
439
436
|
|
|
440
437
|
logger.trace(getNodeIdFromPeerDescriptor(newConnection.getPeerDescriptor()!) + ' added to connections at acceptIncomingConnection')
|
|
441
|
-
this.connections.set(
|
|
438
|
+
this.connections.set(nodeId, newConnection)
|
|
442
439
|
|
|
443
440
|
return true
|
|
444
441
|
}
|
|
445
442
|
|
|
446
443
|
private async closeConnection(peerDescriptor: PeerDescriptor, gracefulLeave: boolean, reason?: string): Promise<void> {
|
|
447
444
|
logger.trace(getNodeIdFromPeerDescriptor(peerDescriptor) + ' ' + 'closeConnection() ' + reason)
|
|
448
|
-
const id =
|
|
445
|
+
const id = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
449
446
|
this.locks.clearAllLocks(id)
|
|
450
447
|
if (this.connections.has(id)) {
|
|
451
448
|
const connectionToClose = this.connections.get(id)!
|
|
@@ -461,13 +458,15 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
461
458
|
if (this.state === ConnectionManagerState.STOPPED || areEqualPeerDescriptors(targetDescriptor, this.getLocalPeerDescriptor())) {
|
|
462
459
|
return
|
|
463
460
|
}
|
|
464
|
-
const
|
|
461
|
+
const nodeId = getNodeIdFromPeerDescriptor(targetDescriptor)
|
|
465
462
|
const rpcRemote = new ConnectionLockRpcRemote(
|
|
466
463
|
this.getLocalPeerDescriptor(),
|
|
467
464
|
targetDescriptor,
|
|
468
|
-
|
|
465
|
+
'DUMMY',
|
|
466
|
+
this.rpcCommunicator!,
|
|
467
|
+
ConnectionLockRpcClient
|
|
469
468
|
)
|
|
470
|
-
this.locks.addLocalLocked(
|
|
469
|
+
this.locks.addLocalLocked(nodeId, lockId)
|
|
471
470
|
rpcRemote.lockRequest(lockId)
|
|
472
471
|
.then((_accepted) => logger.trace('LockRequest successful'))
|
|
473
472
|
.catch((err) => { logger.debug(err) })
|
|
@@ -477,14 +476,16 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
477
476
|
if (this.state === ConnectionManagerState.STOPPED || areEqualPeerDescriptors(targetDescriptor, this.getLocalPeerDescriptor())) {
|
|
478
477
|
return
|
|
479
478
|
}
|
|
480
|
-
const
|
|
481
|
-
this.locks.removeLocalLocked(
|
|
479
|
+
const nodeId = getNodeIdFromPeerDescriptor(targetDescriptor)
|
|
480
|
+
this.locks.removeLocalLocked(nodeId, lockId)
|
|
482
481
|
const rpcRemote = new ConnectionLockRpcRemote(
|
|
483
482
|
this.getLocalPeerDescriptor(),
|
|
484
483
|
targetDescriptor,
|
|
485
|
-
|
|
484
|
+
'DUMMY',
|
|
485
|
+
this.rpcCommunicator!,
|
|
486
|
+
ConnectionLockRpcClient
|
|
486
487
|
)
|
|
487
|
-
if (this.connections.has(
|
|
488
|
+
if (this.connections.has(nodeId)) {
|
|
488
489
|
rpcRemote.unlockRequest(lockId)
|
|
489
490
|
}
|
|
490
491
|
}
|
|
@@ -493,22 +494,21 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
493
494
|
if (this.state === ConnectionManagerState.STOPPED || areEqualPeerDescriptors(targetDescriptor, this.getLocalPeerDescriptor())) {
|
|
494
495
|
return
|
|
495
496
|
}
|
|
496
|
-
const
|
|
497
|
-
this.locks.addWeakLocked(
|
|
497
|
+
const nodeId = getNodeIdFromPeerDescriptor(targetDescriptor)
|
|
498
|
+
this.locks.addWeakLocked(nodeId)
|
|
498
499
|
}
|
|
499
500
|
|
|
500
501
|
public weakUnlockConnection(targetDescriptor: PeerDescriptor): void {
|
|
501
502
|
if (this.state === ConnectionManagerState.STOPPED || areEqualPeerDescriptors(targetDescriptor, this.getLocalPeerDescriptor())) {
|
|
502
503
|
return
|
|
503
504
|
}
|
|
504
|
-
const
|
|
505
|
-
this.locks.removeWeakLocked(
|
|
506
|
-
|
|
505
|
+
const nodeId = getNodeIdFromPeerDescriptor(targetDescriptor)
|
|
506
|
+
this.locks.removeWeakLocked(nodeId)
|
|
507
507
|
}
|
|
508
508
|
|
|
509
509
|
private async gracefullyDisconnectAsync(targetDescriptor: PeerDescriptor, disconnectMode: DisconnectMode): Promise<void> {
|
|
510
510
|
|
|
511
|
-
const connection = this.connections.get(
|
|
511
|
+
const connection = this.connections.get(getNodeIdFromPeerDescriptor(targetDescriptor))
|
|
512
512
|
|
|
513
513
|
if (!connection) {
|
|
514
514
|
logger.debug('gracefullyDisconnectedAsync() tried on a non-existing connection')
|
|
@@ -516,6 +516,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
516
516
|
}
|
|
517
517
|
|
|
518
518
|
const promise = new Promise<void>((resolve, _reject) => {
|
|
519
|
+
// TODO use config option or named constant?
|
|
519
520
|
// eslint-disable-next-line promise/catch-or-return
|
|
520
521
|
waitForEvent3<ManagedConnectionEvents>(connection, 'disconnected', 2000).then(() => {
|
|
521
522
|
logger.trace('disconnected event received in gracefullyDisconnectAsync()')
|
|
@@ -546,7 +547,9 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
|
|
|
546
547
|
const rpcRemote = new ConnectionLockRpcRemote(
|
|
547
548
|
this.getLocalPeerDescriptor(),
|
|
548
549
|
targetDescriptor,
|
|
549
|
-
|
|
550
|
+
'DUMMY',
|
|
551
|
+
this.rpcCommunicator!,
|
|
552
|
+
ConnectionLockRpcClient
|
|
550
553
|
)
|
|
551
554
|
try {
|
|
552
555
|
await rpcRemote.gracefulDisconnect(disconnectMode)
|
|
@@ -16,6 +16,7 @@ export interface ConnectorFacade {
|
|
|
16
16
|
getLocalPeerDescriptor: () => PeerDescriptor | undefined
|
|
17
17
|
start: (
|
|
18
18
|
onNewConnection: (connection: ManagedConnection) => boolean,
|
|
19
|
+
hasConnection: (peerDescriptor: PeerDescriptor) => boolean,
|
|
19
20
|
autoCertifierTransport: ITransport
|
|
20
21
|
) => Promise<void>
|
|
21
22
|
stop: () => Promise<void>
|
|
@@ -58,6 +59,7 @@ export class DefaultConnectorFacade implements ConnectorFacade {
|
|
|
58
59
|
|
|
59
60
|
async start(
|
|
60
61
|
onNewConnection: (connection: ManagedConnection) => boolean,
|
|
62
|
+
hasConnection: (peerDescriptor: PeerDescriptor) => boolean,
|
|
61
63
|
autoCertifierTransport: ITransport
|
|
62
64
|
): Promise<void> {
|
|
63
65
|
logger.trace(`Creating WebsocketConnectorRpcLocal`)
|
|
@@ -65,6 +67,7 @@ export class DefaultConnectorFacade implements ConnectorFacade {
|
|
|
65
67
|
transport: this.config.transport,
|
|
66
68
|
// TODO should we use canConnect also for WebrtcConnector? (NET-1142)
|
|
67
69
|
onNewConnection,
|
|
70
|
+
hasConnection,
|
|
68
71
|
portRange: this.config.websocketPortRange,
|
|
69
72
|
host: this.config.websocketHost,
|
|
70
73
|
entrypoints: this.config.entryPoints,
|
|
@@ -3,11 +3,16 @@ import { EventEmitter } from 'eventemitter3'
|
|
|
3
3
|
import { v4 } from 'uuid'
|
|
4
4
|
import { Message, HandshakeRequest, HandshakeResponse, MessageType, PeerDescriptor, HandshakeError } from '../proto/packages/dht/protos/DhtRpc'
|
|
5
5
|
import { IConnection } from './IConnection'
|
|
6
|
+
import { version } from '../../package.json'
|
|
7
|
+
import { isCompatibleVersion } from '../helpers/versionCompatibility'
|
|
8
|
+
|
|
9
|
+
// Used for backwards compatibility with older versions of the protocol that do not send version in handshakes
|
|
10
|
+
const BEFORE_TESTNET_TWO_VERSION = '100.0.0-before-testnet-two.0'
|
|
6
11
|
|
|
7
12
|
const logger = new Logger(module)
|
|
8
13
|
|
|
9
14
|
interface HandshakerEvents {
|
|
10
|
-
handshakeRequest: (source: PeerDescriptor, target?: PeerDescriptor) => void
|
|
15
|
+
handshakeRequest: (source: PeerDescriptor, version: string, target?: PeerDescriptor) => void
|
|
11
16
|
handshakeCompleted: (remote: PeerDescriptor) => void
|
|
12
17
|
handshakeFailed: (error?: HandshakeError) => void
|
|
13
18
|
}
|
|
@@ -34,13 +39,21 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
|
|
|
34
39
|
if (message.body.oneofKind === 'handshakeRequest') {
|
|
35
40
|
logger.trace('handshake request received')
|
|
36
41
|
const handshake = message.body.handshakeRequest
|
|
37
|
-
this.emit(
|
|
42
|
+
this.emit(
|
|
43
|
+
'handshakeRequest',
|
|
44
|
+
handshake.sourcePeerDescriptor!,
|
|
45
|
+
handshake.version ?? BEFORE_TESTNET_TWO_VERSION,
|
|
46
|
+
handshake.targetPeerDescriptor
|
|
47
|
+
)
|
|
38
48
|
}
|
|
39
49
|
if (message.body.oneofKind === 'handshakeResponse') {
|
|
40
50
|
logger.trace('handshake response received')
|
|
41
51
|
const handshake = message.body.handshakeResponse
|
|
42
|
-
|
|
43
|
-
|
|
52
|
+
const sourceVersion = handshake.version ?? BEFORE_TESTNET_TWO_VERSION
|
|
53
|
+
const error = !isCompatibleVersion(sourceVersion, version) ? HandshakeError.UNSUPPORTED_VERSION : undefined
|
|
54
|
+
?? handshake.error
|
|
55
|
+
if (error !== undefined) {
|
|
56
|
+
this.emit('handshakeFailed', error)
|
|
44
57
|
} else {
|
|
45
58
|
this.emit('handshakeCompleted', handshake.sourcePeerDescriptor!)
|
|
46
59
|
}
|
|
@@ -54,7 +67,8 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
|
|
|
54
67
|
public sendHandshakeRequest(remotePeerDescriptor?: PeerDescriptor): void {
|
|
55
68
|
const outgoingHandshake: HandshakeRequest = {
|
|
56
69
|
sourcePeerDescriptor: this.localPeerDescriptor,
|
|
57
|
-
targetPeerDescriptor: remotePeerDescriptor
|
|
70
|
+
targetPeerDescriptor: remotePeerDescriptor,
|
|
71
|
+
version
|
|
58
72
|
}
|
|
59
73
|
const msg: Message = {
|
|
60
74
|
serviceId: Handshaker.HANDSHAKER_SERVICE_ID,
|
|
@@ -72,7 +86,8 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
|
|
|
72
86
|
public sendHandshakeResponse(error?: HandshakeError): void {
|
|
73
87
|
const outgoingHandshakeResponse: HandshakeResponse = {
|
|
74
88
|
sourcePeerDescriptor: this.localPeerDescriptor,
|
|
75
|
-
error
|
|
89
|
+
error,
|
|
90
|
+
version
|
|
76
91
|
}
|
|
77
92
|
const msg: Message = {
|
|
78
93
|
serviceId: Handshaker.HANDSHAKER_SERVICE_ID,
|
|
@@ -4,25 +4,18 @@ import { Handshaker } from './Handshaker'
|
|
|
4
4
|
import { HandshakeError, PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
|
|
5
5
|
import { Logger, runAndRaceEvents3, RunAndRaceEventsReturnType } from '@streamr/utils'
|
|
6
6
|
import EventEmitter from 'eventemitter3'
|
|
7
|
-
import {
|
|
8
|
-
import { keyFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
7
|
+
import { getNodeIdFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
9
8
|
import { getNodeIdOrUnknownFromPeerDescriptor } from './ConnectionManager'
|
|
9
|
+
import { NodeID } from '../helpers/nodeId'
|
|
10
10
|
|
|
11
11
|
export interface ManagedConnectionEvents {
|
|
12
12
|
managedData: (bytes: Uint8Array, remotePeerDescriptor: PeerDescriptor) => void
|
|
13
|
-
handshakeRequest: (source: PeerDescriptor, target?: PeerDescriptor) => void
|
|
13
|
+
handshakeRequest: (source: PeerDescriptor, version: string, target?: PeerDescriptor) => void
|
|
14
14
|
handshakeCompleted: (peerDescriptor: PeerDescriptor) => void
|
|
15
15
|
handshakeFailed: () => void
|
|
16
|
-
bufferSentByOtherConnection: () => void
|
|
17
|
-
closing: () => void
|
|
18
16
|
}
|
|
19
17
|
|
|
20
|
-
interface
|
|
21
|
-
bufferSent: () => void
|
|
22
|
-
bufferSendingFailed: () => void
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
interface OutpuBufferEvents {
|
|
18
|
+
interface OutputBufferEvents {
|
|
26
19
|
bufferSent: () => void
|
|
27
20
|
bufferSendingFailed: () => void
|
|
28
21
|
}
|
|
@@ -34,29 +27,22 @@ export type Events = ManagedConnectionEvents & ConnectionEvents
|
|
|
34
27
|
export class ManagedConnection extends EventEmitter<Events> {
|
|
35
28
|
|
|
36
29
|
private implementation?: IConnection
|
|
37
|
-
|
|
38
|
-
private outputBufferEmitter = new EventEmitter<OutpuBufferEvents>()
|
|
30
|
+
private outputBufferEmitter = new EventEmitter<OutputBufferEvents>()
|
|
39
31
|
private outputBuffer: Uint8Array[] = []
|
|
40
|
-
|
|
41
32
|
private inputBuffer: Uint8Array[] = []
|
|
42
|
-
|
|
43
33
|
public connectionId: ConnectionID
|
|
44
34
|
private remotePeerDescriptor?: PeerDescriptor
|
|
45
35
|
public connectionType: ConnectionType
|
|
46
|
-
|
|
47
36
|
private handshaker?: Handshaker
|
|
48
37
|
private handshakeCompleted = false
|
|
49
|
-
|
|
50
38
|
private lastUsed: number = Date.now()
|
|
51
39
|
private stopped = false
|
|
52
|
-
public offeredAsIncoming = false
|
|
53
40
|
private bufferSentbyOtherConnection = false
|
|
54
41
|
private closing = false
|
|
55
42
|
public replacedByOtherConnection = false
|
|
56
43
|
private localPeerDescriptor: PeerDescriptor
|
|
57
44
|
protected outgoingConnection?: IConnection
|
|
58
45
|
protected incomingConnection?: IConnection
|
|
59
|
-
|
|
60
46
|
// TODO: Temporary debug variable, should be removed in the future.
|
|
61
47
|
private created = Date.now()
|
|
62
48
|
|
|
@@ -87,7 +73,7 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
87
73
|
this.handshaker = new Handshaker(this.localPeerDescriptor, outgoingConnection)
|
|
88
74
|
|
|
89
75
|
this.handshaker.once('handshakeFailed', (error) => {
|
|
90
|
-
if (error === HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR) {
|
|
76
|
+
if (error === HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR || error === HandshakeError.UNSUPPORTED_VERSION) {
|
|
91
77
|
// TODO should we have some handling for this floating promise?
|
|
92
78
|
this.close(false)
|
|
93
79
|
} else {
|
|
@@ -113,9 +99,13 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
113
99
|
} else {
|
|
114
100
|
if (incomingConnection) {
|
|
115
101
|
this.handshaker = new Handshaker(this.localPeerDescriptor, incomingConnection)
|
|
116
|
-
this.handshaker.on('handshakeRequest', (
|
|
102
|
+
this.handshaker.on('handshakeRequest', (
|
|
103
|
+
sourcePeerDescriptor: PeerDescriptor,
|
|
104
|
+
version: string,
|
|
105
|
+
targetPeerDescriptor?: PeerDescriptor
|
|
106
|
+
) => {
|
|
117
107
|
this.setRemotePeerDescriptor(sourcePeerDescriptor)
|
|
118
|
-
this.emit('handshakeRequest', sourcePeerDescriptor, targetPeerDescriptor)
|
|
108
|
+
this.emit('handshakeRequest', sourcePeerDescriptor, version, targetPeerDescriptor)
|
|
119
109
|
})
|
|
120
110
|
|
|
121
111
|
incomingConnection.on('disconnected', this.onDisconnected)
|
|
@@ -166,8 +156,8 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
166
156
|
return this
|
|
167
157
|
}
|
|
168
158
|
|
|
169
|
-
public
|
|
170
|
-
return
|
|
159
|
+
public getNodeId(): NodeID {
|
|
160
|
+
return getNodeIdFromPeerDescriptor(this.remotePeerDescriptor!)
|
|
171
161
|
}
|
|
172
162
|
|
|
173
163
|
public getLastUsed(): number {
|
|
@@ -233,7 +223,7 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
233
223
|
this.emit('disconnected', gracefulLeave)
|
|
234
224
|
}
|
|
235
225
|
|
|
236
|
-
async send(data: Uint8Array,
|
|
226
|
+
async send(data: Uint8Array, connect: boolean): Promise<void> {
|
|
237
227
|
if (this.stopped) {
|
|
238
228
|
throw new Err.SendFailed('ManagedConnection is stopped')
|
|
239
229
|
}
|
|
@@ -242,18 +232,18 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
242
232
|
}
|
|
243
233
|
this.lastUsed = Date.now()
|
|
244
234
|
|
|
245
|
-
if (
|
|
246
|
-
throw new Err.ConnectionNotOpen('Connection not open when calling send() with
|
|
235
|
+
if (!connect && !this.implementation) {
|
|
236
|
+
throw new Err.ConnectionNotOpen('Connection not open when calling send() with connect flag as false')
|
|
247
237
|
} else if (this.implementation) {
|
|
248
238
|
this.implementation.send(data)
|
|
249
239
|
} else {
|
|
250
240
|
logger.trace('adding data to outputBuffer')
|
|
251
241
|
|
|
252
|
-
let result: RunAndRaceEventsReturnType<
|
|
242
|
+
let result: RunAndRaceEventsReturnType<OutputBufferEvents>
|
|
253
243
|
|
|
254
244
|
try {
|
|
255
|
-
result = await runAndRaceEvents3<
|
|
256
|
-
this.outputBufferEmitter, ['bufferSent', 'bufferSendingFailed'], 15000)
|
|
245
|
+
result = await runAndRaceEvents3<OutputBufferEvents>([() => { this.outputBuffer.push(data) }],
|
|
246
|
+
this.outputBufferEmitter, ['bufferSent', 'bufferSendingFailed'], 15000) // TODO use config option or named constant?
|
|
257
247
|
} catch (e) {
|
|
258
248
|
logger.debug(`Connection to ${getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor)} timed out`, {
|
|
259
249
|
peerDescriptor: this.remotePeerDescriptor,
|
|
@@ -289,7 +279,6 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
289
279
|
logger.trace('bufferSentByOtherConnection reported')
|
|
290
280
|
this.bufferSentbyOtherConnection = true
|
|
291
281
|
this.outputBufferEmitter.emit('bufferSent')
|
|
292
|
-
this.emit('bufferSentByOtherConnection')
|
|
293
282
|
}
|
|
294
283
|
|
|
295
284
|
public acceptHandshake(): void {
|
|
@@ -324,7 +313,6 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
324
313
|
this.closing = true
|
|
325
314
|
|
|
326
315
|
this.outputBufferEmitter.emit('bufferSendingFailed')
|
|
327
|
-
this.emit('closing')
|
|
328
316
|
|
|
329
317
|
if (this.implementation) {
|
|
330
318
|
await this.implementation?.close(gracefulLeave)
|
|
@@ -339,8 +327,6 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
339
327
|
|
|
340
328
|
public destroy(): void {
|
|
341
329
|
this.closing = true
|
|
342
|
-
|
|
343
|
-
this.emit('closing')
|
|
344
330
|
if (!this.stopped) {
|
|
345
331
|
this.stopped = true
|
|
346
332
|
|
|
@@ -11,6 +11,7 @@ import { connectivityMethodToWebsocketUrl } from './websocket/WebsocketConnector
|
|
|
11
11
|
|
|
12
12
|
const logger = new Logger(module)
|
|
13
13
|
|
|
14
|
+
// TODO use config option or named constant?
|
|
14
15
|
export const connectAsync = async ({ url, selfSigned, timeoutMs = 1000 }:
|
|
15
16
|
{ url: string, selfSigned: boolean, timeoutMs?: number }
|
|
16
17
|
): Promise<IConnection> => {
|