@streamr/dht 100.0.0-pretestnet.4 → 100.0.0-rc.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/README.md +90 -8
- package/dist/package.json +70 -0
- package/dist/src/connection/Connection.d.ts +1 -0
- package/dist/src/connection/Connection.js +10 -3
- package/dist/src/connection/Connection.js.map +1 -1
- package/dist/src/connection/ConnectionLockHandler.d.ts +14 -14
- package/dist/src/connection/ConnectionLockHandler.js +22 -16
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionLockRpcLocal.js +7 -6
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +4 -6
- package/dist/src/connection/ConnectionLockRpcRemote.js +10 -37
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +19 -20
- package/dist/src/connection/ConnectionManager.js +132 -117
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +3 -2
- package/dist/src/connection/ConnectorFacade.js +10 -3
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +3 -1
- package/dist/src/connection/Handshaker.js +18 -9
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/IConnection.d.ts +2 -7
- package/dist/src/connection/IConnection.js +1 -8
- package/dist/src/connection/IConnection.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +5 -7
- package/dist/src/connection/ManagedConnection.js +42 -26
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/ManagedWebrtcConnection.js.map +1 -1
- package/dist/src/connection/connectivityChecker.d.ts +9 -0
- package/dist/src/connection/connectivityChecker.js +130 -0
- package/dist/src/connection/connectivityChecker.js.map +1 -0
- package/dist/src/connection/connectivityRequestHandler.d.ts +3 -0
- package/dist/src/connection/connectivityRequestHandler.js +101 -0
- package/dist/src/connection/connectivityRequestHandler.js.map +1 -0
- package/dist/src/connection/simulator/Simulator.d.ts +0 -3
- package/dist/src/connection/simulator/Simulator.js +34 -69
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +27 -25
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.js +16 -12
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/simulator/pings.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.d.ts +2 -3
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +40 -27
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +0 -2
- package/dist/src/connection/webrtc/WebrtcConnector.js +24 -25
- 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 +27 -20
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +3 -6
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +2 -5
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +1 -2
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +6 -4
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/ClientWebsocket.d.ts +1 -0
- package/dist/src/connection/websocket/ClientWebsocket.js +14 -7
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +3 -9
- package/dist/src/connection/websocket/WebsocketConnector.js +142 -74
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +5 -3
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +9 -12
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +4 -7
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +7 -44
- 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 +49 -40
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/connection/websocket/{ServerWebsocket.d.ts → WebsocketServerConnection.d.ts} +8 -4
- package/dist/src/connection/websocket/WebsocketServerConnection.js +97 -0
- package/dist/src/connection/websocket/WebsocketServerConnection.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +45 -49
- package/dist/src/dht/DhtNode.js +229 -317
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +10 -9
- package/dist/src/dht/DhtNodeRpcLocal.js +19 -11
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +15 -10
- package/dist/src/dht/DhtNodeRpcRemote.js +38 -21
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +7 -6
- package/dist/src/dht/ExternalApiRpcLocal.js +9 -13
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +6 -5
- package/dist/src/dht/ExternalApiRpcRemote.js +10 -7
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +61 -0
- package/dist/src/dht/PeerManager.js +288 -0
- package/dist/src/dht/PeerManager.js.map +1 -0
- package/dist/src/dht/contact/Contact.d.ts +2 -2
- package/dist/src/dht/contact/Contact.js +4 -3
- package/dist/src/dht/contact/Contact.js.map +1 -1
- package/dist/src/dht/contact/ContactList.d.ts +8 -8
- package/dist/src/dht/contact/ContactList.js +12 -7
- 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 +13 -12
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/RingContactList.d.ts +31 -0
- package/dist/src/dht/contact/RingContactList.js +133 -0
- package/dist/src/dht/contact/RingContactList.js.map +1 -0
- package/dist/src/dht/contact/{Remote.d.ts → RpcRemote.d.ts} +8 -7
- package/dist/src/dht/contact/{Remote.js → RpcRemote.js} +16 -13
- package/dist/src/dht/contact/RpcRemote.js.map +1 -0
- package/dist/src/dht/contact/SortedContactList.d.ts +29 -15
- package/dist/src/dht/contact/SortedContactList.js +79 -42
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/contact/ringIdentifiers.d.ts +16 -0
- package/dist/src/dht/contact/ringIdentifiers.js +54 -0
- package/dist/src/dht/contact/ringIdentifiers.js.map +1 -0
- package/dist/src/dht/discovery/DiscoverySession.d.ts +8 -18
- package/dist/src/dht/discovery/DiscoverySession.js +32 -48
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +16 -12
- package/dist/src/dht/discovery/PeerDiscovery.js +82 -39
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/discovery/RingDiscoverySession.d.ts +29 -0
- package/dist/src/dht/discovery/RingDiscoverySession.js +123 -0
- package/dist/src/dht/discovery/RingDiscoverySession.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +36 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +166 -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/recursive-operation/RecursiveOperationRpcLocal.js +26 -0
- 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/recursive-operation/RecursiveOperationRpcRemote.js +44 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +44 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +187 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +14 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +20 -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/DuplicateDetector.d.ts +2 -4
- package/dist/src/dht/routing/DuplicateDetector.js +10 -15
- package/dist/src/dht/routing/DuplicateDetector.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +11 -27
- package/dist/src/dht/routing/Router.js +92 -58
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +3 -4
- package/dist/src/dht/routing/RouterRpcLocal.js +17 -16
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcRemote.d.ts +3 -3
- package/dist/src/dht/routing/RouterRpcRemote.js +29 -20
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +29 -21
- package/dist/src/dht/routing/RoutingSession.js +93 -68
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/routing/RoutingTablesCache.d.ts +24 -0
- package/dist/src/dht/routing/RoutingTablesCache.js +46 -0
- package/dist/src/dht/routing/RoutingTablesCache.js.map +1 -0
- package/dist/src/dht/routing/getPreviousPeer.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +9 -14
- package/dist/src/dht/store/LocalDataStore.js +46 -72
- 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 +182 -0
- package/dist/src/dht/store/StoreManager.js.map +1 -0
- package/dist/src/dht/store/StoreRpcLocal.d.ts +11 -41
- package/dist/src/dht/store/StoreRpcLocal.js +27 -234
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.d.ts +6 -7
- package/dist/src/dht/store/StoreRpcRemote.js +10 -20
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/exports.d.ts +5 -3
- package/dist/src/exports.js +10 -8
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/AddressTools.js +2 -0
- package/dist/src/helpers/AddressTools.js.map +1 -1
- package/dist/src/helpers/Connectivity.js.map +1 -1
- package/dist/src/helpers/MapWithTtl.d.ts +14 -0
- package/dist/src/helpers/MapWithTtl.js +60 -0
- package/dist/src/helpers/MapWithTtl.js.map +1 -0
- package/dist/src/helpers/createPeerDescriptor.d.ts +3 -0
- package/dist/src/helpers/createPeerDescriptor.js +57 -0
- package/dist/src/helpers/createPeerDescriptor.js.map +1 -0
- package/dist/src/helpers/createPeerDescriptorSignaturePayload.d.ts +2 -0
- package/dist/src/helpers/createPeerDescriptorSignaturePayload.js +30 -0
- package/dist/src/helpers/createPeerDescriptorSignaturePayload.js.map +1 -0
- package/dist/src/helpers/debugHelpers.js.map +1 -1
- package/dist/src/helpers/errors.js +2 -0
- package/dist/src/helpers/errors.js.map +1 -1
- package/dist/src/helpers/offering.d.ts +4 -0
- package/dist/src/helpers/offering.js +18 -0
- package/dist/src/helpers/offering.js.map +1 -0
- package/dist/src/helpers/protoClasses.js +2 -3
- package/dist/src/helpers/protoClasses.js.map +1 -1
- package/dist/src/helpers/protoToString.js.map +1 -1
- package/dist/src/helpers/version.d.ts +6 -0
- package/dist/src/helpers/version.js +38 -0
- package/dist/src/helpers/version.js.map +1 -0
- package/dist/src/identifiers.d.ts +10 -0
- package/dist/src/identifiers.js +31 -0
- package/dist/src/identifiers.js.map +1 -0
- package/dist/src/proto/google/protobuf/any.js +8 -8
- package/dist/src/proto/google/protobuf/any.js.map +1 -1
- package/dist/src/proto/google/protobuf/empty.js +2 -4
- package/dist/src/proto/google/protobuf/empty.js.map +1 -1
- package/dist/src/proto/google/protobuf/timestamp.js +10 -10
- package/dist/src/proto/google/protobuf/timestamp.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +46 -49
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +63 -54
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +226 -232
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +146 -168
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +26 -29
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +1 -1
- package/dist/src/rpc-protocol/DhtCallContext.d.ts +2 -2
- package/dist/src/rpc-protocol/DhtCallContext.js +8 -0
- 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 +10 -2
- package/dist/src/transport/ITransport.js +5 -0
- package/dist/src/transport/ITransport.js.map +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.js +3 -1
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +4 -2
- package/dist/src/transport/RoutingRpcCommunicator.js +19 -12
- 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 +13 -9
- package/package.json +16 -13
- package/protos/DhtRpc.proto +76 -76
- package/src/connection/Connection.ts +6 -1
- package/src/connection/ConnectionLockHandler.ts +30 -22
- package/src/connection/ConnectionLockRpcLocal.ts +7 -12
- package/src/connection/ConnectionLockRpcRemote.ts +9 -19
- package/src/connection/ConnectionManager.ts +132 -138
- package/src/connection/ConnectorFacade.ts +10 -9
- package/src/connection/Handshaker.ts +23 -11
- package/src/connection/IConnection.ts +2 -8
- package/src/connection/ManagedConnection.ts +34 -35
- package/src/connection/connectivityChecker.ts +109 -0
- package/src/connection/connectivityRequestHandler.ts +103 -0
- package/src/connection/simulator/Simulator.ts +6 -37
- package/src/connection/simulator/SimulatorConnection.ts +23 -25
- package/src/connection/simulator/SimulatorConnector.ts +11 -11
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +17 -25
- package/src/connection/webrtc/NodeWebrtcConnection.ts +24 -26
- package/src/connection/webrtc/WebrtcConnector.ts +18 -29
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +29 -24
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +4 -14
- package/src/connection/webrtc/iceServerAsString.ts +1 -1
- package/src/connection/websocket/AutoCertifierClientFacade.ts +4 -6
- package/src/connection/websocket/ClientWebsocket.ts +10 -4
- package/src/connection/websocket/WebsocketConnector.ts +120 -97
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +13 -15
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +7 -27
- package/src/connection/websocket/WebsocketServer.ts +50 -53
- package/src/connection/websocket/WebsocketServerConnection.ts +104 -0
- package/src/dht/DhtNode.ts +289 -410
- package/src/dht/DhtNodeRpcLocal.ts +25 -17
- package/src/dht/DhtNodeRpcRemote.ts +43 -27
- package/src/dht/ExternalApiRpcLocal.ts +30 -17
- package/src/dht/ExternalApiRpcRemote.ts +14 -11
- package/src/dht/PeerManager.ts +339 -0
- package/src/dht/contact/Contact.ts +4 -4
- package/src/dht/contact/ContactList.ts +11 -10
- package/src/dht/contact/RandomContactList.ts +15 -15
- package/src/dht/contact/RingContactList.ts +151 -0
- package/src/dht/contact/{Remote.ts → RpcRemote.ts} +16 -19
- package/src/dht/contact/SortedContactList.ts +120 -72
- package/src/dht/contact/ringIdentifiers.ts +62 -0
- package/src/dht/discovery/DiscoverySession.ts +36 -61
- package/src/dht/discovery/PeerDiscovery.ts +98 -43
- package/src/dht/discovery/RingDiscoverySession.ts +160 -0
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +246 -0
- package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +34 -0
- package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +43 -0
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +232 -0
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +35 -0
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.ts +30 -0
- package/src/dht/routing/DuplicateDetector.ts +9 -21
- package/src/dht/routing/Router.ts +95 -92
- package/src/dht/routing/RouterRpcLocal.ts +16 -18
- package/src/dht/routing/RouterRpcRemote.ts +26 -24
- package/src/dht/routing/RoutingSession.ts +119 -98
- package/src/dht/routing/RoutingTablesCache.ts +58 -0
- package/src/dht/routing/getPreviousPeer.ts +1 -1
- package/src/dht/store/LocalDataStore.ts +47 -77
- package/src/dht/store/StoreManager.ts +209 -0
- package/src/dht/store/StoreRpcLocal.ts +39 -308
- package/src/dht/store/StoreRpcRemote.ts +13 -31
- package/src/exports.ts +13 -3
- package/src/helpers/AddressTools.ts +2 -0
- package/src/helpers/MapWithTtl.ts +71 -0
- package/src/helpers/createPeerDescriptor.ts +57 -0
- package/src/helpers/createPeerDescriptorSignaturePayload.ts +28 -0
- package/src/helpers/offering.ts +15 -0
- package/src/helpers/protoClasses.ts +4 -6
- package/src/helpers/version.ts +32 -0
- package/src/identifiers.ts +29 -0
- package/src/proto/google/protobuf/any.ts +4 -4
- package/src/proto/google/protobuf/empty.ts +2 -4
- package/src/proto/google/protobuf/timestamp.ts +4 -4
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +65 -68
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +27 -30
- package/src/proto/packages/dht/protos/DhtRpc.ts +297 -313
- package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +1 -1
- package/src/rpc-protocol/DhtCallContext.ts +2 -2
- package/src/rpc-protocol/DhtRpcOptions.ts +2 -1
- package/src/transport/ITransport.ts +11 -2
- package/src/transport/ListeningRpcCommunicator.ts +1 -1
- package/src/transport/RoutingRpcCommunicator.ts +21 -14
- package/test/RandomGraphSimulation.ts +3 -2
- package/test/benchmark/Find.test.ts +13 -28
- package/test/benchmark/KademliaCorrectness.test.ts +24 -28
- package/test/benchmark/RingCorrectness.test.ts +157 -0
- package/test/benchmark/SortedContactListBenchmark.test.ts +151 -0
- package/test/benchmark/WebsocketServerMemoryLeak.test.ts +41 -0
- package/test/benchmark/hybrid-network-simulation/RingContactList.test.ts +72 -0
- package/test/benchmark/kademlia-simulation/Contact.ts +9 -9
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +11 -11
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +29 -25
- package/test/data/generateGroundTruthData.ts +7 -6
- package/test/end-to-end/Layer0-Layer1.test.ts +11 -15
- package/test/end-to-end/Layer0.test.ts +19 -22
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +19 -21
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +26 -28
- package/test/end-to-end/Layer0Webrtc.test.ts +19 -19
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +10 -18
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +8 -15
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +2 -2
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +8 -9
- package/test/end-to-end/memory-leak.test.ts +19 -24
- package/test/integration/ConnectionLocking.test.ts +66 -60
- package/test/integration/ConnectionManager.test.ts +43 -63
- package/test/integration/ConnectivityChecking.test.ts +52 -0
- package/test/integration/DhtJoinPeerDiscovery.test.ts +8 -12
- package/test/integration/DhtNodeExternalAPI.test.ts +17 -21
- package/test/integration/DhtNodeRpcRemote.test.ts +19 -26
- package/test/integration/DhtRpc.test.ts +20 -24
- package/test/integration/Find.test.ts +10 -12
- package/test/integration/Layer1-scale.test.ts +25 -37
- package/test/integration/Mock-Layer1-Layer0.test.ts +39 -59
- package/test/integration/MultipleEntryPointJoining.test.ts +14 -14
- package/test/integration/ReplicateData.test.ts +106 -0
- package/test/integration/RouteMessage.test.ts +42 -68
- package/test/integration/RouterRpcRemote.test.ts +19 -24
- package/test/integration/ScaleDownDht.test.ts +14 -12
- package/test/integration/SimultaneousConnections.test.ts +112 -111
- package/test/integration/Store.test.ts +43 -27
- package/test/integration/StoreAndDelete.test.ts +36 -48
- package/test/integration/StoreOnDhtWithThreeNodes.test.ts +59 -0
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +17 -37
- package/test/integration/StoreRpcRemote.test.ts +20 -32
- package/test/integration/WebrtcConnectionManagement.test.ts +39 -25
- package/test/integration/WebrtcConnectorRpc.test.ts +6 -11
- package/test/integration/WebsocketConnectionManagement.test.ts +87 -22
- package/test/integration/WebsocketConnectorRpc.test.ts +14 -24
- package/test/integration/{RpcErrors.test.ts → rpc-connections-over-webrpc.test.ts} +15 -26
- package/test/unit/AddressTools.test.ts +4 -0
- package/test/unit/ConnectivityHelpers.test.ts +9 -17
- package/test/unit/DuplicateDetector.test.ts +8 -5
- package/test/unit/LocalDataStore.test.ts +78 -75
- package/test/unit/PeerManager.test.ts +33 -0
- package/test/unit/RandomContactList.test.ts +12 -9
- package/test/unit/RecursiveOperationManager.test.ts +157 -0
- package/test/unit/RecursiveOperationSession.test.ts +68 -0
- package/test/unit/Router.test.ts +52 -35
- package/test/unit/RoutingSession.test.ts +79 -0
- package/test/unit/SortedContactList.test.ts +61 -30
- package/test/unit/StoreManager.test.ts +138 -0
- package/test/unit/WebsocketConnector.test.ts +27 -35
- package/test/unit/connectivityRequestHandler.test.ts +104 -0
- package/test/unit/createPeerDescriptor.test.ts +69 -0
- package/test/unit/customMatchers.test.ts +16 -0
- package/test/unit/version.test.ts +18 -0
- package/test/utils/FakeTransport.ts +47 -0
- package/test/utils/customMatchers.ts +71 -0
- package/test/utils/mock/MockRpcCommunicator.ts +7 -0
- package/test/utils/mock/Router.ts +13 -3
- package/test/utils/mock/Transport.ts +1 -1
- package/test/utils/mock/mockDataEntry.ts +38 -0
- package/test/utils/utils.ts +104 -107
- package/tsconfig.browser.json +2 -1
- package/tsconfig.jest.json +4 -2
- package/tsconfig.node.json +4 -2
- package/dist/src/connection/ConnectivityChecker.d.ts +0 -17
- package/dist/src/connection/ConnectivityChecker.js +0 -208
- package/dist/src/connection/ConnectivityChecker.js.map +0 -1
- package/dist/src/connection/websocket/ServerWebsocket.js +0 -100
- package/dist/src/connection/websocket/ServerWebsocket.js.map +0 -1
- package/dist/src/dht/contact/Remote.js.map +0 -1
- package/dist/src/dht/find/FindRpcLocal.d.ts +0 -14
- package/dist/src/dht/find/FindRpcLocal.js +0 -25
- 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 -145
- 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 -184
- 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 +0 -41
- package/dist/src/dht/routing/FindRpcRemote.js.map +0 -1
- package/dist/src/helpers/PeerID.d.ts +0 -24
- package/dist/src/helpers/PeerID.js +0 -78
- package/dist/src/helpers/PeerID.js.map +0 -1
- package/dist/src/helpers/UUID.d.ts +0 -8
- package/dist/src/helpers/UUID.js +0 -36
- package/dist/src/helpers/UUID.js.map +0 -1
- package/dist/src/helpers/kademliaId.d.ts +0 -1
- package/dist/src/helpers/kademliaId.js +0 -14
- package/dist/src/helpers/kademliaId.js.map +0 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +0 -6
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +0 -23
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +0 -1
- package/src/connection/ConnectivityChecker.ts +0 -199
- package/src/connection/websocket/ServerWebsocket.ts +0 -114
- package/src/dht/find/FindRpcLocal.ts +0 -35
- package/src/dht/find/FindSession.ts +0 -178
- package/src/dht/find/FindSessionRpcLocal.ts +0 -25
- package/src/dht/find/FindSessionRpcRemote.ts +0 -30
- package/src/dht/find/Finder.ts +0 -275
- package/src/dht/routing/FindRpcRemote.ts +0 -40
- package/src/helpers/PeerID.ts +0 -88
- package/src/helpers/UUID.ts +0 -35
- package/src/helpers/kademliaId.ts +0 -8
- package/src/helpers/peerIdFromPeerDescriptor.ts +0 -20
- package/test/integration/MigrateData.test.ts +0 -204
- package/test/unit/Finder.test.ts +0 -110
- package/test/unit/PeerID.test.ts +0 -22
- package/test/unit/UUID.test.ts +0 -55
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { Logger } from '@streamr/utils'
|
|
2
2
|
import { EventEmitter } from 'eventemitter3'
|
|
3
3
|
import { v4 } from 'uuid'
|
|
4
|
-
import { Message, HandshakeRequest, HandshakeResponse,
|
|
4
|
+
import { Message, HandshakeRequest, HandshakeResponse, PeerDescriptor, HandshakeError } from '../proto/packages/dht/protos/DhtRpc'
|
|
5
5
|
import { IConnection } from './IConnection'
|
|
6
|
+
import { LOCAL_PROTOCOL_VERSION, isMaybeSupportedVersion } from '../helpers/version'
|
|
6
7
|
|
|
7
8
|
const logger = new Logger(module)
|
|
8
9
|
|
|
9
10
|
interface HandshakerEvents {
|
|
10
|
-
handshakeRequest: (source: PeerDescriptor, target?: PeerDescriptor) => void
|
|
11
|
+
handshakeRequest: (source: PeerDescriptor, version: string, target?: PeerDescriptor) => void
|
|
11
12
|
handshakeCompleted: (remote: PeerDescriptor) => void
|
|
12
13
|
handshakeFailed: (error?: HandshakeError) => void
|
|
13
14
|
}
|
|
@@ -17,7 +18,7 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
|
|
|
17
18
|
private static readonly HANDSHAKER_SERVICE_ID = 'system/handshaker'
|
|
18
19
|
private localPeerDescriptor: PeerDescriptor
|
|
19
20
|
private connection: IConnection
|
|
20
|
-
|
|
21
|
+
private readonly onDataListener: (data: Uint8Array) => void
|
|
21
22
|
constructor(
|
|
22
23
|
localPeerDescriptor: PeerDescriptor,
|
|
23
24
|
connection: IConnection
|
|
@@ -25,7 +26,8 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
|
|
|
25
26
|
super()
|
|
26
27
|
this.localPeerDescriptor = localPeerDescriptor
|
|
27
28
|
this.connection = connection
|
|
28
|
-
this.
|
|
29
|
+
this.onDataListener = (data: Uint8Array) => this.onData(data)
|
|
30
|
+
this.connection.on('data', this.onDataListener)
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
private onData(data: Uint8Array) {
|
|
@@ -34,13 +36,19 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
|
|
|
34
36
|
if (message.body.oneofKind === 'handshakeRequest') {
|
|
35
37
|
logger.trace('handshake request received')
|
|
36
38
|
const handshake = message.body.handshakeRequest
|
|
37
|
-
this.emit(
|
|
39
|
+
this.emit(
|
|
40
|
+
'handshakeRequest',
|
|
41
|
+
handshake.sourcePeerDescriptor!,
|
|
42
|
+
handshake.version,
|
|
43
|
+
handshake.targetPeerDescriptor
|
|
44
|
+
)
|
|
38
45
|
}
|
|
39
46
|
if (message.body.oneofKind === 'handshakeResponse') {
|
|
40
47
|
logger.trace('handshake response received')
|
|
41
48
|
const handshake = message.body.handshakeResponse
|
|
42
|
-
|
|
43
|
-
|
|
49
|
+
const error = !isMaybeSupportedVersion(handshake.version) ? HandshakeError.UNSUPPORTED_VERSION : handshake.error
|
|
50
|
+
if (error !== undefined) {
|
|
51
|
+
this.emit('handshakeFailed', error)
|
|
44
52
|
} else {
|
|
45
53
|
this.emit('handshakeCompleted', handshake.sourcePeerDescriptor!)
|
|
46
54
|
}
|
|
@@ -54,11 +62,11 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
|
|
|
54
62
|
public sendHandshakeRequest(remotePeerDescriptor?: PeerDescriptor): void {
|
|
55
63
|
const outgoingHandshake: HandshakeRequest = {
|
|
56
64
|
sourcePeerDescriptor: this.localPeerDescriptor,
|
|
57
|
-
targetPeerDescriptor: remotePeerDescriptor
|
|
65
|
+
targetPeerDescriptor: remotePeerDescriptor,
|
|
66
|
+
version: LOCAL_PROTOCOL_VERSION
|
|
58
67
|
}
|
|
59
68
|
const msg: Message = {
|
|
60
69
|
serviceId: Handshaker.HANDSHAKER_SERVICE_ID,
|
|
61
|
-
messageType: MessageType.HANDSHAKE_REQUEST,
|
|
62
70
|
messageId: v4(),
|
|
63
71
|
body: {
|
|
64
72
|
oneofKind: 'handshakeRequest',
|
|
@@ -72,11 +80,11 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
|
|
|
72
80
|
public sendHandshakeResponse(error?: HandshakeError): void {
|
|
73
81
|
const outgoingHandshakeResponse: HandshakeResponse = {
|
|
74
82
|
sourcePeerDescriptor: this.localPeerDescriptor,
|
|
75
|
-
error
|
|
83
|
+
error,
|
|
84
|
+
version: LOCAL_PROTOCOL_VERSION
|
|
76
85
|
}
|
|
77
86
|
const msg: Message = {
|
|
78
87
|
serviceId: Handshaker.HANDSHAKER_SERVICE_ID,
|
|
79
|
-
messageType: MessageType.HANDSHAKE_RESPONSE,
|
|
80
88
|
messageId: v4(),
|
|
81
89
|
body: {
|
|
82
90
|
oneofKind: 'handshakeResponse',
|
|
@@ -86,4 +94,8 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
|
|
|
86
94
|
this.connection.send(Message.toBinary(msg))
|
|
87
95
|
logger.trace('handshake response sent')
|
|
88
96
|
}
|
|
97
|
+
|
|
98
|
+
public stop(): void {
|
|
99
|
+
this.connection.off('data', this.onDataListener)
|
|
100
|
+
}
|
|
89
101
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BrandedString } from '@streamr/utils'
|
|
2
2
|
|
|
3
3
|
export interface ConnectionEvents {
|
|
4
4
|
data: (bytes: Uint8Array) => void
|
|
@@ -15,13 +15,7 @@ export enum ConnectionType {
|
|
|
15
15
|
SIMULATOR_CLIENT = 'simulator-client',
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
export type
|
|
19
|
-
|
|
20
|
-
export class ConnectionID extends UUID {
|
|
21
|
-
toMapKey(): ConnectionIDKey {
|
|
22
|
-
return this.toString() as ConnectionIDKey
|
|
23
|
-
}
|
|
24
|
-
}
|
|
18
|
+
export type ConnectionID = BrandedString<'ConnectionID'>
|
|
25
19
|
|
|
26
20
|
export interface IConnection {
|
|
27
21
|
|
|
@@ -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 { PeerIDKey } from '../helpers/PeerID'
|
|
8
|
-
import { keyFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
9
7
|
import { getNodeIdOrUnknownFromPeerDescriptor } from './ConnectionManager'
|
|
8
|
+
import { DhtAddress, getNodeIdFromPeerDescriptor } from '../identifiers'
|
|
9
|
+
import { createRandomConnectionId } from './Connection'
|
|
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,28 +27,24 @@ 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
|
|
46
|
+
// TODO: Temporary debug variable, should be removed in the future.
|
|
47
|
+
private created = Date.now()
|
|
59
48
|
|
|
60
49
|
constructor(
|
|
61
50
|
localPeerDescriptor: PeerDescriptor,
|
|
@@ -70,7 +59,7 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
70
59
|
this.outgoingConnection = outgoingConnection
|
|
71
60
|
this.incomingConnection = incomingConnection
|
|
72
61
|
this.connectionType = connectionType
|
|
73
|
-
this.connectionId =
|
|
62
|
+
this.connectionId = createRandomConnectionId()
|
|
74
63
|
|
|
75
64
|
this.send = this.send.bind(this)
|
|
76
65
|
this.onDisconnected = this.onDisconnected.bind(this)
|
|
@@ -84,7 +73,8 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
84
73
|
this.handshaker = new Handshaker(this.localPeerDescriptor, outgoingConnection)
|
|
85
74
|
|
|
86
75
|
this.handshaker.once('handshakeFailed', (error) => {
|
|
87
|
-
if (error === HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR) {
|
|
76
|
+
if (error === HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR || error === HandshakeError.UNSUPPORTED_VERSION) {
|
|
77
|
+
// TODO should we have some handling for this floating promise?
|
|
88
78
|
this.close(false)
|
|
89
79
|
} else {
|
|
90
80
|
logger.trace(getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor) + ' handshakeFailed: ' + error)
|
|
@@ -109,9 +99,13 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
109
99
|
} else {
|
|
110
100
|
if (incomingConnection) {
|
|
111
101
|
this.handshaker = new Handshaker(this.localPeerDescriptor, incomingConnection)
|
|
112
|
-
this.handshaker.on('handshakeRequest', (
|
|
102
|
+
this.handshaker.on('handshakeRequest', (
|
|
103
|
+
sourcePeerDescriptor: PeerDescriptor,
|
|
104
|
+
version: string,
|
|
105
|
+
targetPeerDescriptor?: PeerDescriptor
|
|
106
|
+
) => {
|
|
113
107
|
this.setRemotePeerDescriptor(sourcePeerDescriptor)
|
|
114
|
-
this.emit('handshakeRequest', sourcePeerDescriptor, targetPeerDescriptor)
|
|
108
|
+
this.emit('handshakeRequest', sourcePeerDescriptor, version, targetPeerDescriptor)
|
|
115
109
|
})
|
|
116
110
|
|
|
117
111
|
incomingConnection.on('disconnected', this.onDisconnected)
|
|
@@ -162,8 +156,8 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
162
156
|
return this
|
|
163
157
|
}
|
|
164
158
|
|
|
165
|
-
public
|
|
166
|
-
return
|
|
159
|
+
public getNodeId(): DhtAddress {
|
|
160
|
+
return getNodeIdFromPeerDescriptor(this.remotePeerDescriptor!)
|
|
167
161
|
}
|
|
168
162
|
|
|
169
163
|
public getLastUsed(): number {
|
|
@@ -183,6 +177,7 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
183
177
|
|
|
184
178
|
this.setRemotePeerDescriptor(peerDescriptor)
|
|
185
179
|
this.handshakeCompleted = true
|
|
180
|
+
this.handshaker!.stop()
|
|
186
181
|
|
|
187
182
|
while (this.outputBuffer.length > 0) {
|
|
188
183
|
logger.trace('emptying outputBuffer')
|
|
@@ -229,7 +224,7 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
229
224
|
this.emit('disconnected', gracefulLeave)
|
|
230
225
|
}
|
|
231
226
|
|
|
232
|
-
async send(data: Uint8Array,
|
|
227
|
+
async send(data: Uint8Array, connect: boolean): Promise<void> {
|
|
233
228
|
if (this.stopped) {
|
|
234
229
|
throw new Err.SendFailed('ManagedConnection is stopped')
|
|
235
230
|
}
|
|
@@ -238,20 +233,25 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
238
233
|
}
|
|
239
234
|
this.lastUsed = Date.now()
|
|
240
235
|
|
|
241
|
-
if (
|
|
242
|
-
throw new Err.ConnectionNotOpen('Connection not open when calling send() with
|
|
236
|
+
if (!connect && !this.implementation) {
|
|
237
|
+
throw new Err.ConnectionNotOpen('Connection not open when calling send() with connect flag as false')
|
|
243
238
|
} else if (this.implementation) {
|
|
244
239
|
this.implementation.send(data)
|
|
245
240
|
} else {
|
|
246
241
|
logger.trace('adding data to outputBuffer')
|
|
247
242
|
|
|
248
|
-
let result: RunAndRaceEventsReturnType<
|
|
243
|
+
let result: RunAndRaceEventsReturnType<OutputBufferEvents>
|
|
249
244
|
|
|
250
245
|
try {
|
|
251
|
-
result = await runAndRaceEvents3<
|
|
252
|
-
this.outputBufferEmitter, ['bufferSent', 'bufferSendingFailed'], 15000)
|
|
246
|
+
result = await runAndRaceEvents3<OutputBufferEvents>([() => { this.outputBuffer.push(data) }],
|
|
247
|
+
this.outputBufferEmitter, ['bufferSent', 'bufferSendingFailed'], 15000) // TODO use config option or named constant?
|
|
253
248
|
} catch (e) {
|
|
254
|
-
logger.debug(`Connection to ${getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor)} timed out
|
|
249
|
+
logger.debug(`Connection to ${getNodeIdOrUnknownFromPeerDescriptor(this.remotePeerDescriptor)} timed out`, {
|
|
250
|
+
peerDescriptor: this.remotePeerDescriptor,
|
|
251
|
+
type: this.connectionType,
|
|
252
|
+
lifetime: Date.now() - this.created
|
|
253
|
+
})
|
|
254
|
+
await this.close(false)
|
|
255
255
|
throw new Err.SendFailed('Sending buffer timed out')
|
|
256
256
|
}
|
|
257
257
|
|
|
@@ -280,7 +280,6 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
280
280
|
logger.trace('bufferSentByOtherConnection reported')
|
|
281
281
|
this.bufferSentbyOtherConnection = true
|
|
282
282
|
this.outputBufferEmitter.emit('bufferSent')
|
|
283
|
-
this.emit('bufferSentByOtherConnection')
|
|
284
283
|
}
|
|
285
284
|
|
|
286
285
|
public acceptHandshake(): void {
|
|
@@ -306,13 +305,15 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
306
305
|
}
|
|
307
306
|
|
|
308
307
|
public async close(gracefulLeave: boolean): Promise<void> {
|
|
308
|
+
if (this.closing) {
|
|
309
|
+
return
|
|
310
|
+
}
|
|
309
311
|
if (this.replacedByOtherConnection) {
|
|
310
312
|
logger.trace('close() called on replaced connection')
|
|
311
313
|
}
|
|
312
314
|
this.closing = true
|
|
313
315
|
|
|
314
316
|
this.outputBufferEmitter.emit('bufferSendingFailed')
|
|
315
|
-
this.emit('closing')
|
|
316
317
|
|
|
317
318
|
if (this.implementation) {
|
|
318
319
|
await this.implementation?.close(gracefulLeave)
|
|
@@ -327,8 +328,6 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
327
328
|
|
|
328
329
|
public destroy(): void {
|
|
329
330
|
this.closing = true
|
|
330
|
-
|
|
331
|
-
this.emit('closing')
|
|
332
331
|
if (!this.stopped) {
|
|
333
332
|
this.stopped = true
|
|
334
333
|
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Logger, RunAndRaceEventsReturnType, runAndRaceEvents3 } from '@streamr/utils'
|
|
2
|
+
import { v4 } from 'uuid'
|
|
3
|
+
import * as Err from '../helpers/errors'
|
|
4
|
+
import {
|
|
5
|
+
ConnectivityRequest, ConnectivityResponse,
|
|
6
|
+
Message, PeerDescriptor
|
|
7
|
+
} from '../proto/packages/dht/protos/DhtRpc'
|
|
8
|
+
import { ConnectionEvents, IConnection } from './IConnection'
|
|
9
|
+
import { ClientWebsocket } from './websocket/ClientWebsocket'
|
|
10
|
+
import { connectivityMethodToWebsocketUrl } from './websocket/WebsocketConnector'
|
|
11
|
+
import { isMaybeSupportedVersion } from '../helpers/version'
|
|
12
|
+
|
|
13
|
+
const logger = new Logger(module)
|
|
14
|
+
|
|
15
|
+
// TODO use config option or named constant?
|
|
16
|
+
export const connectAsync = async ({ url, selfSigned, timeoutMs = 1000 }:
|
|
17
|
+
{ url: string, selfSigned: boolean, timeoutMs?: number }
|
|
18
|
+
): Promise<IConnection> => {
|
|
19
|
+
const socket = new ClientWebsocket()
|
|
20
|
+
let result: RunAndRaceEventsReturnType<ConnectionEvents>
|
|
21
|
+
try {
|
|
22
|
+
result = await runAndRaceEvents3<ConnectionEvents>([
|
|
23
|
+
() => { socket.connect(url, selfSigned) }],
|
|
24
|
+
socket, ['connected', 'error'],
|
|
25
|
+
timeoutMs)
|
|
26
|
+
} catch (e) {
|
|
27
|
+
throw new Err.ConnectionFailed('WebSocket connection timed out')
|
|
28
|
+
}
|
|
29
|
+
if (result.winnerName === 'error') {
|
|
30
|
+
throw new Err.ConnectionFailed('Could not open WebSocket connection')
|
|
31
|
+
}
|
|
32
|
+
return socket
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const CONNECTIVITY_CHECKER_SERVICE_ID = 'system/connectivity-checker'
|
|
36
|
+
const CONNECTIVITY_CHECKER_TIMEOUT = 5000
|
|
37
|
+
|
|
38
|
+
export const sendConnectivityRequest = async (
|
|
39
|
+
request: ConnectivityRequest,
|
|
40
|
+
entryPoint: PeerDescriptor
|
|
41
|
+
): Promise<ConnectivityResponse> => {
|
|
42
|
+
let outgoingConnection: IConnection
|
|
43
|
+
const wsServerInfo = {
|
|
44
|
+
host: entryPoint.websocket!.host,
|
|
45
|
+
port: entryPoint.websocket!.port,
|
|
46
|
+
tls: entryPoint.websocket!.tls,
|
|
47
|
+
}
|
|
48
|
+
const url = connectivityMethodToWebsocketUrl(wsServerInfo, 'connectivityRequest')
|
|
49
|
+
logger.debug(`Attempting connectivity check with entrypoint ${url}`)
|
|
50
|
+
try {
|
|
51
|
+
outgoingConnection = await connectAsync({
|
|
52
|
+
url,
|
|
53
|
+
selfSigned: request.selfSigned
|
|
54
|
+
})
|
|
55
|
+
} catch (e) {
|
|
56
|
+
throw new Err.ConnectionFailed(`Failed to connect to entrypoint for connectivity check: ${url}`, e)
|
|
57
|
+
}
|
|
58
|
+
// send connectivity request
|
|
59
|
+
const msg: Message = {
|
|
60
|
+
serviceId: CONNECTIVITY_CHECKER_SERVICE_ID,
|
|
61
|
+
messageId: v4(),
|
|
62
|
+
body: {
|
|
63
|
+
oneofKind: 'connectivityRequest',
|
|
64
|
+
connectivityRequest: request
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const responseAwaiter = () => {
|
|
68
|
+
return new Promise((resolve: (res: ConnectivityResponse) => void, reject) => {
|
|
69
|
+
const timeoutId = setTimeout(() => {
|
|
70
|
+
// TODO should we have some handling for this floating promise?
|
|
71
|
+
outgoingConnection.close(false)
|
|
72
|
+
reject(new Err.ConnectivityResponseTimeout('timeout'))
|
|
73
|
+
}, CONNECTIVITY_CHECKER_TIMEOUT)
|
|
74
|
+
const listener = (bytes: Uint8Array) => {
|
|
75
|
+
// TODO should we have some handling for this floating promise?
|
|
76
|
+
outgoingConnection.close(false)
|
|
77
|
+
try {
|
|
78
|
+
const message: Message = Message.fromBinary(bytes)
|
|
79
|
+
if (message.body.oneofKind === 'connectivityResponse') {
|
|
80
|
+
logger.debug('ConnectivityResponse received: ' + JSON.stringify(Message.toJson(message)))
|
|
81
|
+
const connectivityResponseMessage = message.body.connectivityResponse
|
|
82
|
+
const remoteVersion = connectivityResponseMessage.version
|
|
83
|
+
outgoingConnection!.off('data', listener)
|
|
84
|
+
clearTimeout(timeoutId)
|
|
85
|
+
if (isMaybeSupportedVersion(remoteVersion)) {
|
|
86
|
+
resolve(connectivityResponseMessage)
|
|
87
|
+
} else {
|
|
88
|
+
reject(`Unsupported version: ${remoteVersion}`)
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
} catch (err) {
|
|
94
|
+
logger.trace(`Could not parse message: ${err}`)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
outgoingConnection!.on('data', listener)
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
const retPromise = responseAwaiter()
|
|
102
|
+
outgoingConnection.send(Message.toBinary(msg))
|
|
103
|
+
logger.trace('ConnectivityRequest sent: ' + JSON.stringify(Message.toJson(msg)))
|
|
104
|
+
return await retPromise
|
|
105
|
+
} catch (e) {
|
|
106
|
+
logger.error('error getting connectivityresponse')
|
|
107
|
+
throw e
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { ipv4ToNumber, Logger } from '@streamr/utils'
|
|
2
|
+
import { v4 } from 'uuid'
|
|
3
|
+
import {
|
|
4
|
+
ConnectivityRequest,
|
|
5
|
+
ConnectivityResponse,
|
|
6
|
+
Message
|
|
7
|
+
} from '../proto/packages/dht/protos/DhtRpc'
|
|
8
|
+
import { NatType } from './ConnectionManager'
|
|
9
|
+
import { CONNECTIVITY_CHECKER_SERVICE_ID, connectAsync } from './connectivityChecker'
|
|
10
|
+
import { IConnection } from './IConnection'
|
|
11
|
+
import { WebsocketServerConnection } from './websocket/WebsocketServerConnection'
|
|
12
|
+
import { connectivityMethodToWebsocketUrl } from './websocket/WebsocketConnector'
|
|
13
|
+
import { LOCAL_PROTOCOL_VERSION } from '../helpers/version'
|
|
14
|
+
|
|
15
|
+
export const DISABLE_CONNECTIVITY_PROBE = 0
|
|
16
|
+
|
|
17
|
+
const logger = new Logger(module)
|
|
18
|
+
|
|
19
|
+
export const attachConnectivityRequestHandler = (connectionToListenTo: WebsocketServerConnection): void => {
|
|
20
|
+
connectionToListenTo.on('data', async (data: Uint8Array) => {
|
|
21
|
+
logger.trace('server received data')
|
|
22
|
+
try {
|
|
23
|
+
const message = Message.fromBinary(data)
|
|
24
|
+
if (message.body.oneofKind === 'connectivityRequest') {
|
|
25
|
+
logger.trace('ConnectivityRequest received: ' + JSON.stringify(Message.toJson(message)))
|
|
26
|
+
try {
|
|
27
|
+
await handleIncomingConnectivityRequest(connectionToListenTo, message.body.connectivityRequest)
|
|
28
|
+
logger.trace('handleIncomingConnectivityRequest ok')
|
|
29
|
+
} catch (e) {
|
|
30
|
+
logger.error('handleIncomingConnectivityRequest', { error: e })
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
} catch (err) {
|
|
34
|
+
logger.trace(`Could not parse message: ${err}`)
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const handleIncomingConnectivityRequest = async (connection: WebsocketServerConnection, connectivityRequest: ConnectivityRequest): Promise<void> => {
|
|
40
|
+
const host = connectivityRequest.host ?? connection.remoteIpAddress
|
|
41
|
+
const ipAddress = connection.remoteIpAddress
|
|
42
|
+
let connectivityResponse: ConnectivityResponse
|
|
43
|
+
if (connectivityRequest.port !== DISABLE_CONNECTIVITY_PROBE) {
|
|
44
|
+
connectivityResponse = await connectivityProbe(connectivityRequest, ipAddress, host)
|
|
45
|
+
} else {
|
|
46
|
+
logger.trace('ConnectivityRequest port is 0, replying without connectivityProbe')
|
|
47
|
+
connectivityResponse = {
|
|
48
|
+
host,
|
|
49
|
+
natType: NatType.UNKNOWN,
|
|
50
|
+
ipAddress: ipv4ToNumber(ipAddress),
|
|
51
|
+
version: LOCAL_PROTOCOL_VERSION
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const msg: Message = {
|
|
55
|
+
serviceId: CONNECTIVITY_CHECKER_SERVICE_ID,
|
|
56
|
+
messageId: v4(),
|
|
57
|
+
body: {
|
|
58
|
+
oneofKind: 'connectivityResponse',
|
|
59
|
+
connectivityResponse
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
connection.send(Message.toBinary(msg))
|
|
63
|
+
logger.trace('ConnectivityResponse sent: ' + JSON.stringify(Message.toJson(msg)))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const connectivityProbe = async (connectivityRequest: ConnectivityRequest, ipAddress: string, host: string): Promise<ConnectivityResponse> => {
|
|
67
|
+
let outgoingConnection: IConnection | undefined
|
|
68
|
+
let connectivityResponseMessage: ConnectivityResponse
|
|
69
|
+
try {
|
|
70
|
+
const wsServerInfo = {
|
|
71
|
+
host,
|
|
72
|
+
port: connectivityRequest.port,
|
|
73
|
+
tls: connectivityRequest.tls
|
|
74
|
+
}
|
|
75
|
+
const url = connectivityMethodToWebsocketUrl(wsServerInfo, 'connectivityProbe')
|
|
76
|
+
logger.trace(`Attempting Connectivity Check to ${url}`)
|
|
77
|
+
outgoingConnection = await connectAsync({
|
|
78
|
+
url,
|
|
79
|
+
selfSigned: connectivityRequest.selfSigned
|
|
80
|
+
})
|
|
81
|
+
logger.trace('Connectivity test produced positive result, communicating reply to the requester ' + host + ':' + connectivityRequest.port)
|
|
82
|
+
connectivityResponseMessage = {
|
|
83
|
+
host,
|
|
84
|
+
natType: NatType.OPEN_INTERNET,
|
|
85
|
+
websocket: { host, port: connectivityRequest.port, tls: connectivityRequest.tls },
|
|
86
|
+
ipAddress: ipv4ToNumber(ipAddress),
|
|
87
|
+
version: LOCAL_PROTOCOL_VERSION
|
|
88
|
+
}
|
|
89
|
+
} catch (err) {
|
|
90
|
+
logger.debug('error', { err })
|
|
91
|
+
connectivityResponseMessage = {
|
|
92
|
+
host,
|
|
93
|
+
natType: NatType.UNKNOWN,
|
|
94
|
+
ipAddress: ipv4ToNumber(ipAddress),
|
|
95
|
+
version: LOCAL_PROTOCOL_VERSION
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (outgoingConnection) {
|
|
99
|
+
// TODO should we have some handling for this floating promise?
|
|
100
|
+
outgoingConnection.close(false)
|
|
101
|
+
}
|
|
102
|
+
return connectivityResponseMessage
|
|
103
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/parameter-properties */
|
|
2
2
|
import EventEmitter from 'eventemitter3'
|
|
3
|
-
import { PeerIDKey } from '../../helpers/PeerID'
|
|
4
3
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
5
4
|
import { ConnectionSourceEvents } from '../IConnectionSource'
|
|
6
5
|
import { SimulatorConnector } from './SimulatorConnector'
|
|
@@ -8,19 +7,9 @@ import { SimulatorConnection } from './SimulatorConnection'
|
|
|
8
7
|
import { ConnectionID } from '../IConnection'
|
|
9
8
|
import { Logger } from '@streamr/utils'
|
|
10
9
|
import { getRegionDelayMatrix } from './pings'
|
|
11
|
-
import { keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
12
10
|
import Heap from 'heap'
|
|
13
11
|
import { debugVars } from '../../helpers/debugHelpers'
|
|
14
|
-
import
|
|
15
|
-
|
|
16
|
-
// TODO take this from @streamr/test-utils (we can't access devDependencies as Simulator
|
|
17
|
-
// is currently in "src" directory instead of "test" directory)
|
|
18
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
19
|
-
declare let _streamr_electron_test: any
|
|
20
|
-
export function isRunningInElectron(): boolean {
|
|
21
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
22
|
-
return typeof _streamr_electron_test !== 'undefined'
|
|
23
|
-
}
|
|
12
|
+
import { DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
24
13
|
|
|
25
14
|
const logger = new Logger(module)
|
|
26
15
|
|
|
@@ -102,7 +91,7 @@ class CloseOperation extends SimulatorOperation {
|
|
|
102
91
|
|
|
103
92
|
export class Simulator extends EventEmitter<ConnectionSourceEvents> {
|
|
104
93
|
private stopped = false
|
|
105
|
-
private connectors: Map<
|
|
94
|
+
private connectors: Map<DhtAddress, SimulatorConnector> = new Map()
|
|
106
95
|
private latencyTable?: Array<Array<number>>
|
|
107
96
|
private associations: Map<ConnectionID, Association> = new Map()
|
|
108
97
|
|
|
@@ -121,22 +110,6 @@ export class Simulator extends EventEmitter<ConnectionSourceEvents> {
|
|
|
121
110
|
})
|
|
122
111
|
|
|
123
112
|
private simulatorTimeout?: NodeJS.Timeout
|
|
124
|
-
private static clock: sinon.SinonFakeTimers | undefined
|
|
125
|
-
|
|
126
|
-
static useFakeTimers(on = true): void {
|
|
127
|
-
if (!isRunningInElectron()) { // never use fake timers in browser environment
|
|
128
|
-
if (on) {
|
|
129
|
-
if (!Simulator.clock) {
|
|
130
|
-
Simulator.clock = sinon.useFakeTimers()
|
|
131
|
-
}
|
|
132
|
-
} else {
|
|
133
|
-
if (Simulator.clock) {
|
|
134
|
-
Simulator.clock.restore()
|
|
135
|
-
Simulator.clock = undefined
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
113
|
|
|
141
114
|
constructor(latencyType: LatencyType = LatencyType.NONE, fixedLatency?: number) {
|
|
142
115
|
super()
|
|
@@ -148,7 +121,7 @@ export class Simulator extends EventEmitter<ConnectionSourceEvents> {
|
|
|
148
121
|
this.latencyTable = getRegionDelayMatrix()
|
|
149
122
|
}
|
|
150
123
|
|
|
151
|
-
if (this.latencyType === LatencyType.FIXED &&
|
|
124
|
+
if ((this.latencyType === LatencyType.FIXED) && (this.fixedLatency === undefined)) {
|
|
152
125
|
throw new Error('LatencyType.FIXED requires the desired latency to be given as second parameter')
|
|
153
126
|
}
|
|
154
127
|
|
|
@@ -185,7 +158,7 @@ export class Simulator extends EventEmitter<ConnectionSourceEvents> {
|
|
|
185
158
|
|
|
186
159
|
if (sourceRegion === undefined || targetRegion === undefined || sourceRegion > 15 || targetRegion > 15) {
|
|
187
160
|
logger.error('invalid region index given to Simulator')
|
|
188
|
-
throw ('invalid region index given to Simulator')
|
|
161
|
+
throw new Error('invalid region index given to Simulator')
|
|
189
162
|
}
|
|
190
163
|
|
|
191
164
|
latency = this.latencyTable![sourceRegion][targetRegion]
|
|
@@ -214,11 +187,11 @@ export class Simulator extends EventEmitter<ConnectionSourceEvents> {
|
|
|
214
187
|
}
|
|
215
188
|
|
|
216
189
|
public addConnector(connector: SimulatorConnector): void {
|
|
217
|
-
this.connectors.set(
|
|
190
|
+
this.connectors.set(getNodeIdFromPeerDescriptor(connector.getPeerDescriptor()), connector)
|
|
218
191
|
}
|
|
219
192
|
|
|
220
193
|
private executeConnectOperation(operation: ConnectOperation): void {
|
|
221
|
-
const target = this.connectors.get(
|
|
194
|
+
const target = this.connectors.get(getNodeIdFromPeerDescriptor(operation.targetDescriptor))
|
|
222
195
|
|
|
223
196
|
if (!target) {
|
|
224
197
|
logger.error('Target connector not found when executing connect operation')
|
|
@@ -311,10 +284,6 @@ export class Simulator extends EventEmitter<ConnectionSourceEvents> {
|
|
|
311
284
|
const timeDifference = firstOperationTime - currentTime
|
|
312
285
|
|
|
313
286
|
this.simulatorTimeout = setTimeout(this.executeQueuedOperations, timeDifference)
|
|
314
|
-
|
|
315
|
-
if (Simulator.clock) {
|
|
316
|
-
Simulator.clock.runAllAsync()
|
|
317
|
-
}
|
|
318
287
|
}
|
|
319
288
|
|
|
320
289
|
private scheduleOperation(operation: SimulatorOperation) {
|