@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,199 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ConnectivityRequest, ConnectivityResponse,
|
|
3
|
-
Message, MessageType, PeerDescriptor
|
|
4
|
-
} from '../proto/packages/dht/protos/DhtRpc'
|
|
5
|
-
import { ConnectionEvents, IConnection } from './IConnection'
|
|
6
|
-
import { Logger, runAndRaceEvents3, RunAndRaceEventsReturnType } from '@streamr/utils'
|
|
7
|
-
import * as Err from '../helpers/errors'
|
|
8
|
-
import { ClientWebsocket } from './websocket/ClientWebsocket'
|
|
9
|
-
import { v4 } from 'uuid'
|
|
10
|
-
import { NatType } from './ConnectionManager'
|
|
11
|
-
import { ServerWebsocket } from './websocket/ServerWebsocket'
|
|
12
|
-
import { connectivityMethodToWebsocketUrl } from './websocket/WebsocketConnector'
|
|
13
|
-
|
|
14
|
-
const logger = new Logger(module)
|
|
15
|
-
|
|
16
|
-
// Class for handling both client and server side of the connectivity
|
|
17
|
-
// checks. This is attached to all ServerWebsockets to listen to
|
|
18
|
-
// ConnectivityRequest messages.
|
|
19
|
-
|
|
20
|
-
export class ConnectivityChecker {
|
|
21
|
-
|
|
22
|
-
private static readonly CONNECTIVITY_CHECKER_SERVICE_ID = 'system/connectivity-checker'
|
|
23
|
-
private static readonly CONNECTIVITY_CHECKER_TIMEOUT = 5000
|
|
24
|
-
private destroyed = false
|
|
25
|
-
private readonly websocketPort: number
|
|
26
|
-
private readonly tls: boolean
|
|
27
|
-
private host?: string
|
|
28
|
-
|
|
29
|
-
constructor(websocketPort: number, tls: boolean, host?: string) {
|
|
30
|
-
this.websocketPort = websocketPort
|
|
31
|
-
this.tls = tls
|
|
32
|
-
this.host = host
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public async sendConnectivityRequest(entryPoint: PeerDescriptor, selfSigned: boolean): Promise<ConnectivityResponse> {
|
|
36
|
-
if (this.destroyed) {
|
|
37
|
-
throw new Err.ConnectionFailed('ConnectivityChecker is destroyed')
|
|
38
|
-
}
|
|
39
|
-
let outgoingConnection: IConnection
|
|
40
|
-
const wsServerInfo = {
|
|
41
|
-
host: entryPoint.websocket!.host,
|
|
42
|
-
port: entryPoint.websocket!.port,
|
|
43
|
-
tls: entryPoint.websocket!.tls,
|
|
44
|
-
}
|
|
45
|
-
const url = connectivityMethodToWebsocketUrl(wsServerInfo, 'connectivityRequest')
|
|
46
|
-
try {
|
|
47
|
-
outgoingConnection = await this.connectAsync({
|
|
48
|
-
url,
|
|
49
|
-
selfSigned
|
|
50
|
-
})
|
|
51
|
-
} catch (e) {
|
|
52
|
-
throw new Err.ConnectionFailed(`Failed to connect to the entrypoint ${url}`, e)
|
|
53
|
-
}
|
|
54
|
-
// send connectivity request
|
|
55
|
-
const connectivityRequestMessage: ConnectivityRequest = { port: this.websocketPort, host: this.host, tls: this.tls, selfSigned }
|
|
56
|
-
const msg: Message = {
|
|
57
|
-
serviceId: ConnectivityChecker.CONNECTIVITY_CHECKER_SERVICE_ID,
|
|
58
|
-
messageType: MessageType.CONNECTIVITY_REQUEST, messageId: v4(),
|
|
59
|
-
body: {
|
|
60
|
-
oneofKind: 'connectivityRequest',
|
|
61
|
-
connectivityRequest: connectivityRequestMessage
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
const responseAwaiter = () => {
|
|
65
|
-
return new Promise((resolve: (res: ConnectivityResponse) => void, reject) => {
|
|
66
|
-
const timeoutId = setTimeout(() => {
|
|
67
|
-
outgoingConnection.close(false)
|
|
68
|
-
reject(new Err.ConnectivityResponseTimeout('timeout'))
|
|
69
|
-
}, ConnectivityChecker.CONNECTIVITY_CHECKER_TIMEOUT)
|
|
70
|
-
const listener = (bytes: Uint8Array) => {
|
|
71
|
-
outgoingConnection.close(false)
|
|
72
|
-
try {
|
|
73
|
-
const message: Message = Message.fromBinary(bytes)
|
|
74
|
-
if (message.body.oneofKind === 'connectivityResponse') {
|
|
75
|
-
logger.trace('ConnectivityResponse received: ' + JSON.stringify(Message.toJson(message)))
|
|
76
|
-
const connectivityResponseMessage = message.body.connectivityResponse
|
|
77
|
-
outgoingConnection!.off('data', listener)
|
|
78
|
-
clearTimeout(timeoutId)
|
|
79
|
-
resolve(connectivityResponseMessage)
|
|
80
|
-
} else {
|
|
81
|
-
return
|
|
82
|
-
}
|
|
83
|
-
} catch (err) {
|
|
84
|
-
logger.trace(`Could not parse message: ${err}`)
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
outgoingConnection!.on('data', listener)
|
|
88
|
-
})
|
|
89
|
-
}
|
|
90
|
-
try {
|
|
91
|
-
const retPromise = responseAwaiter()
|
|
92
|
-
outgoingConnection.send(Message.toBinary(msg))
|
|
93
|
-
logger.trace('ConnectivityRequest sent: ' + JSON.stringify(Message.toJson(msg)))
|
|
94
|
-
return await retPromise
|
|
95
|
-
} catch (e) {
|
|
96
|
-
logger.error('error getting connectivityresponse')
|
|
97
|
-
throw e
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public setHost(hostName: string): void {
|
|
102
|
-
this.host = hostName
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public listenToIncomingConnectivityRequests(connectionToListenTo: ServerWebsocket): void {
|
|
106
|
-
connectionToListenTo.on('data', (data: Uint8Array) => {
|
|
107
|
-
logger.trace('server received data')
|
|
108
|
-
try {
|
|
109
|
-
const message = Message.fromBinary(data)
|
|
110
|
-
if (message.body.oneofKind === 'connectivityRequest') {
|
|
111
|
-
logger.trace('ConnectivityRequest received: ' + JSON.stringify(Message.toJson(message)))
|
|
112
|
-
this.handleIncomingConnectivityRequest(connectionToListenTo, message.body.connectivityRequest).then(() => {
|
|
113
|
-
logger.trace('handleIncomingConnectivityRequest ok')
|
|
114
|
-
return
|
|
115
|
-
}).catch((e) => {
|
|
116
|
-
logger.error('handleIncomingConnectivityRequest' + e)
|
|
117
|
-
})
|
|
118
|
-
}
|
|
119
|
-
} catch (err) {
|
|
120
|
-
logger.trace(`Could not parse message: ${err}`)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
})
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
private async handleIncomingConnectivityRequest(
|
|
127
|
-
connection: ServerWebsocket,
|
|
128
|
-
connectivityRequest: ConnectivityRequest
|
|
129
|
-
): Promise<void> {
|
|
130
|
-
if (this.destroyed) {
|
|
131
|
-
return
|
|
132
|
-
}
|
|
133
|
-
let outgoingConnection: IConnection | undefined
|
|
134
|
-
let connectivityResponseMessage: ConnectivityResponse | undefined
|
|
135
|
-
const host = connectivityRequest.host ?? connection.getRemoteAddress()
|
|
136
|
-
try {
|
|
137
|
-
const wsServerInfo = {
|
|
138
|
-
host,
|
|
139
|
-
port: connectivityRequest.port,
|
|
140
|
-
tls: connectivityRequest.tls
|
|
141
|
-
}
|
|
142
|
-
const url = connectivityMethodToWebsocketUrl(wsServerInfo, 'connectivityProbe')
|
|
143
|
-
logger.trace(`Attempting Connectivity Check to ${url}`)
|
|
144
|
-
outgoingConnection = await this.connectAsync({
|
|
145
|
-
url,
|
|
146
|
-
selfSigned: connectivityRequest.selfSigned
|
|
147
|
-
})
|
|
148
|
-
} catch (err) {
|
|
149
|
-
logger.debug('error', { err })
|
|
150
|
-
connectivityResponseMessage = {
|
|
151
|
-
host,
|
|
152
|
-
natType: NatType.UNKNOWN
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
if (outgoingConnection) {
|
|
156
|
-
outgoingConnection.close(false)
|
|
157
|
-
logger.trace('Connectivity test produced positive result, communicating reply to the requester ' + host + ':' + connectivityRequest.port)
|
|
158
|
-
connectivityResponseMessage = {
|
|
159
|
-
host,
|
|
160
|
-
natType: NatType.OPEN_INTERNET,
|
|
161
|
-
websocket: { host, port: connectivityRequest.port, tls: connectivityRequest.tls }
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
const msg: Message = {
|
|
165
|
-
serviceId: ConnectivityChecker.CONNECTIVITY_CHECKER_SERVICE_ID,
|
|
166
|
-
messageType: MessageType.CONNECTIVITY_RESPONSE, messageId: v4(),
|
|
167
|
-
body: {
|
|
168
|
-
oneofKind: 'connectivityResponse',
|
|
169
|
-
connectivityResponse: connectivityResponseMessage!
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
connection.send(Message.toBinary(msg))
|
|
173
|
-
logger.trace('ConnectivityResponse sent: ' + JSON.stringify(Message.toJson(msg)))
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// eslint-disable-next-line class-methods-use-this
|
|
177
|
-
private async connectAsync({ url, selfSigned, timeoutMs = 1000, }:
|
|
178
|
-
{ url: string, selfSigned: boolean, timeoutMs?: number }
|
|
179
|
-
): Promise<IConnection> {
|
|
180
|
-
const socket = new ClientWebsocket()
|
|
181
|
-
let result: RunAndRaceEventsReturnType<ConnectionEvents>
|
|
182
|
-
try {
|
|
183
|
-
result = await runAndRaceEvents3<ConnectionEvents>([
|
|
184
|
-
() => { socket.connect(url, selfSigned) }],
|
|
185
|
-
socket, ['connected', 'error'],
|
|
186
|
-
timeoutMs)
|
|
187
|
-
} catch (e) {
|
|
188
|
-
throw (new Err.ConnectionFailed('WebSocket connection timed out'))
|
|
189
|
-
}
|
|
190
|
-
if (result.winnerName === 'error') {
|
|
191
|
-
throw (new Err.ConnectionFailed('Could not open WebSocket connection'))
|
|
192
|
-
}
|
|
193
|
-
return socket
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
public destroy(): void {
|
|
197
|
-
this.destroyed = true
|
|
198
|
-
}
|
|
199
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import EventEmitter from 'eventemitter3'
|
|
2
|
-
import { IConnection, ConnectionID, ConnectionEvents, ConnectionType } from '../IConnection'
|
|
3
|
-
import { connection as WsConnection } from 'websocket'
|
|
4
|
-
import { Logger } from '@streamr/utils'
|
|
5
|
-
import { Url } from 'url'
|
|
6
|
-
import { GOING_AWAY } from './ClientWebsocket'
|
|
7
|
-
|
|
8
|
-
const logger = new Logger(module)
|
|
9
|
-
|
|
10
|
-
// NodeJsBuffer is global defined in preload.js of Karma
|
|
11
|
-
// It is used to make Karma/Electron tests to use the NodeJS
|
|
12
|
-
// implementation of Buffer instead of the browser polyfill
|
|
13
|
-
|
|
14
|
-
declare let NodeJsBuffer: BufferConstructor
|
|
15
|
-
|
|
16
|
-
enum MessageType {
|
|
17
|
-
UTF8 = 'utf8',
|
|
18
|
-
BINARY = 'binary'
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class ServerWebsocket extends EventEmitter<ConnectionEvents> implements IConnection {
|
|
22
|
-
|
|
23
|
-
public readonly connectionId: ConnectionID
|
|
24
|
-
public readonly connectionType = ConnectionType.WEBSOCKET_SERVER
|
|
25
|
-
public readonly resourceURL: Url
|
|
26
|
-
private socket?: WsConnection
|
|
27
|
-
private stopped = false
|
|
28
|
-
|
|
29
|
-
constructor(socket: WsConnection, resourceURL: Url) {
|
|
30
|
-
super()
|
|
31
|
-
|
|
32
|
-
this.resourceURL = resourceURL
|
|
33
|
-
this.connectionId = new ConnectionID()
|
|
34
|
-
|
|
35
|
-
socket.on('message', (message) => {
|
|
36
|
-
logger.trace('ServerWebsocket::onMessage')
|
|
37
|
-
if (message.type === MessageType.UTF8) {
|
|
38
|
-
logger.debug('Received string Message: ' + message.utf8Data)
|
|
39
|
-
} else if (message.type === MessageType.BINARY) {
|
|
40
|
-
logger.trace('Received Binary Message of ' + message.binaryData.length + ' bytes')
|
|
41
|
-
this.emit('data',
|
|
42
|
-
new Uint8Array(message.binaryData.buffer, message.binaryData.byteOffset,
|
|
43
|
-
message.binaryData.byteLength / Uint8Array.BYTES_PER_ELEMENT))
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
socket.on('close', (reasonCode, description) => {
|
|
47
|
-
logger.trace('Peer ' + socket.remoteAddress + ' disconnected.')
|
|
48
|
-
this.doDisconnect(reasonCode, description)
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
socket.on('error', (error) => {
|
|
52
|
-
this.emit('error', error.name)
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
this.socket = socket
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private doDisconnect(reasonCode: number, description: string): void {
|
|
59
|
-
this.stopped = true
|
|
60
|
-
this.socket?.removeAllListeners()
|
|
61
|
-
this.socket = undefined
|
|
62
|
-
const gracefulLeave = reasonCode === GOING_AWAY
|
|
63
|
-
this.emit('disconnected', gracefulLeave, reasonCode, description)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public send(data: Uint8Array): void {
|
|
67
|
-
// If in an Karma / Electron test, use the NodeJS implementation
|
|
68
|
-
// of Buffer instead of the browser polyfill
|
|
69
|
-
|
|
70
|
-
if (!this.stopped && this.socket) {
|
|
71
|
-
if (typeof NodeJsBuffer !== 'undefined') {
|
|
72
|
-
this.socket.sendBytes(NodeJsBuffer.from(data))
|
|
73
|
-
} else {
|
|
74
|
-
this.socket.sendBytes(Buffer.from(data))
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
logger.debug('Tried to call send() on a stopped socket')
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public async close(gracefulLeave: boolean): Promise<void> {
|
|
83
|
-
this.emit('disconnected', gracefulLeave, undefined, 'close() called')
|
|
84
|
-
this.removeAllListeners()
|
|
85
|
-
if (!this.stopped) {
|
|
86
|
-
this.socket?.close(gracefulLeave === true ? GOING_AWAY : undefined)
|
|
87
|
-
} else {
|
|
88
|
-
logger.debug('Tried to close a stopped connection')
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
public destroy(): void {
|
|
93
|
-
if (!this.stopped) {
|
|
94
|
-
this.removeAllListeners()
|
|
95
|
-
if (this.socket) {
|
|
96
|
-
this.socket.removeAllListeners()
|
|
97
|
-
this.socket.close()
|
|
98
|
-
this.socket = undefined
|
|
99
|
-
}
|
|
100
|
-
this.stopped = true
|
|
101
|
-
} else {
|
|
102
|
-
logger.debug('Tried to destroy() a stopped connection')
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
public getRemoteAddress(): string {
|
|
107
|
-
if (!this.stopped && this.socket) {
|
|
108
|
-
return this.socket.remoteAddress
|
|
109
|
-
} else {
|
|
110
|
-
logger.error('Tried to get the remoteAddress of a stopped connection')
|
|
111
|
-
return ''
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Logger } from '@streamr/utils'
|
|
2
|
-
import { PeerDescriptor, RouteMessageAck, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
|
|
3
|
-
import { IFindRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
4
|
-
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
5
|
-
import { createRouteMessageAck } from '../routing/RouterRpcLocal'
|
|
6
|
-
import { getPreviousPeer } from '../routing/getPreviousPeer'
|
|
7
|
-
|
|
8
|
-
const logger = new Logger(module)
|
|
9
|
-
|
|
10
|
-
interface FindRpcLocalConfig {
|
|
11
|
-
doRouteFindRequest: (routedMessage: RouteMessageWrapper) => RouteMessageAck
|
|
12
|
-
addContact: (contact: PeerDescriptor, setActive?: boolean) => void
|
|
13
|
-
isMostLikelyDuplicate: (requestId: string) => boolean
|
|
14
|
-
addToDuplicateDetector: (requestId: string) => void
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class FindRpcLocal implements IFindRpc {
|
|
18
|
-
|
|
19
|
-
private readonly config: FindRpcLocalConfig
|
|
20
|
-
|
|
21
|
-
constructor(config: FindRpcLocalConfig) {
|
|
22
|
-
this.config = config
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async routeFindRequest(routedMessage: RouteMessageWrapper): Promise<RouteMessageAck> {
|
|
26
|
-
if (this.config.isMostLikelyDuplicate(routedMessage.requestId)) {
|
|
27
|
-
return createRouteMessageAck(routedMessage, 'message given to routeFindRequest() service is likely a duplicate')
|
|
28
|
-
}
|
|
29
|
-
const senderId = getNodeIdFromPeerDescriptor(getPreviousPeer(routedMessage) ?? routedMessage.sourcePeer!)
|
|
30
|
-
logger.trace(`Received routeFindRequest call from ${senderId}`)
|
|
31
|
-
this.config.addContact(routedMessage.sourcePeer!, true)
|
|
32
|
-
this.config.addToDuplicateDetector(routedMessage.requestId)
|
|
33
|
-
return this.config.doRouteFindRequest(routedMessage)
|
|
34
|
-
}
|
|
35
|
-
}
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import EventEmitter from 'eventemitter3'
|
|
2
|
-
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
3
|
-
import { DataEntry, PeerDescriptor, FindResponse } from '../../proto/packages/dht/protos/DhtRpc'
|
|
4
|
-
import { ITransport } from '../../transport/ITransport'
|
|
5
|
-
import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
|
|
6
|
-
import { Contact } from '../contact/Contact'
|
|
7
|
-
import { SortedContactList } from '../contact/SortedContactList'
|
|
8
|
-
import { FindResult } from './Finder'
|
|
9
|
-
import { keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
10
|
-
import { ServiceID } from '../../types/ServiceID'
|
|
11
|
-
import { FindSessionRpcLocal } from './FindSessionRpcLocal'
|
|
12
|
-
|
|
13
|
-
export interface FindSessionEvents {
|
|
14
|
-
findCompleted: (results: PeerDescriptor[]) => void
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface FindSessionConfig {
|
|
18
|
-
serviceId: ServiceID
|
|
19
|
-
transport: ITransport
|
|
20
|
-
kademliaIdToFind: Uint8Array
|
|
21
|
-
localPeerId: PeerID
|
|
22
|
-
waitedRoutingPathCompletions: number
|
|
23
|
-
fetchData: boolean
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export class FindSession extends EventEmitter<FindSessionEvents> {
|
|
27
|
-
private readonly serviceId: ServiceID
|
|
28
|
-
private readonly transport: ITransport
|
|
29
|
-
private readonly kademliaIdToFind: Uint8Array
|
|
30
|
-
private readonly localPeerId: PeerID
|
|
31
|
-
private readonly waitedRoutingPathCompletions: number
|
|
32
|
-
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
33
|
-
private readonly fetchData: boolean
|
|
34
|
-
private results: SortedContactList<Contact>
|
|
35
|
-
private foundData: Map<string, DataEntry> = new Map()
|
|
36
|
-
private allKnownHops: Set<PeerIDKey> = new Set()
|
|
37
|
-
private reportedHops: Set<PeerIDKey> = new Set()
|
|
38
|
-
private reportFindCompletedTimeout?: NodeJS.Timeout
|
|
39
|
-
private findCompletedEmitted = false
|
|
40
|
-
private noCloserNodesReceivedCounter = 0
|
|
41
|
-
|
|
42
|
-
constructor(config: FindSessionConfig) {
|
|
43
|
-
super()
|
|
44
|
-
this.serviceId = config.serviceId
|
|
45
|
-
this.transport = config.transport
|
|
46
|
-
this.kademliaIdToFind = config.kademliaIdToFind
|
|
47
|
-
this.localPeerId = config.localPeerId
|
|
48
|
-
this.waitedRoutingPathCompletions = config.waitedRoutingPathCompletions
|
|
49
|
-
this.results = new SortedContactList(PeerID.fromValue(this.kademliaIdToFind), 10, undefined, true)
|
|
50
|
-
this.fetchData = config.fetchData
|
|
51
|
-
this.rpcCommunicator = new ListeningRpcCommunicator(this.serviceId, this.transport, {
|
|
52
|
-
rpcRequestTimeout: 15000
|
|
53
|
-
})
|
|
54
|
-
this.registerLocalRpcMethods()
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private registerLocalRpcMethods() {
|
|
58
|
-
const rpcLocal = new FindSessionRpcLocal({
|
|
59
|
-
doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound?: boolean) => {
|
|
60
|
-
this.doSendFindResponse(routingPath, nodes, dataEntries, noCloserNodesFound)
|
|
61
|
-
}
|
|
62
|
-
})
|
|
63
|
-
this.rpcCommunicator.registerRpcNotification(FindResponse, 'sendFindResponse',
|
|
64
|
-
(req: FindResponse) => rpcLocal.sendFindResponse(req))
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
private isFindCompleted(): boolean {
|
|
68
|
-
const unreportedHops: Set<PeerIDKey> = new Set(this.allKnownHops)
|
|
69
|
-
this.reportedHops.forEach((id) => {
|
|
70
|
-
unreportedHops.delete(id)
|
|
71
|
-
})
|
|
72
|
-
if (this.noCloserNodesReceivedCounter >= 1 && unreportedHops.size === 0) {
|
|
73
|
-
if (this.fetchData
|
|
74
|
-
&& (this.hasNonStaleData() || this.noCloserNodesReceivedCounter >= this.waitedRoutingPathCompletions)) {
|
|
75
|
-
return true
|
|
76
|
-
} else if (this.fetchData) {
|
|
77
|
-
return false
|
|
78
|
-
}
|
|
79
|
-
return true
|
|
80
|
-
}
|
|
81
|
-
return false
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
private hasNonStaleData(): boolean {
|
|
85
|
-
return Array.from(this.foundData.values()).some((entry) => entry.stale === false)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public doSendFindResponse(
|
|
89
|
-
routingPath: PeerDescriptor[],
|
|
90
|
-
nodes: PeerDescriptor[],
|
|
91
|
-
dataEntries: DataEntry[],
|
|
92
|
-
noCloserNodesFound?: boolean
|
|
93
|
-
): void {
|
|
94
|
-
this.addKnownHops(routingPath)
|
|
95
|
-
if (routingPath.length >= 1) {
|
|
96
|
-
this.setHopAsReported(routingPath[routingPath.length - 1])
|
|
97
|
-
}
|
|
98
|
-
nodes.forEach((descriptor: PeerDescriptor) => {
|
|
99
|
-
this.results.addContact(new Contact(descriptor))
|
|
100
|
-
})
|
|
101
|
-
this.processFoundData(dataEntries)
|
|
102
|
-
if (noCloserNodesFound) {
|
|
103
|
-
this.onNoCloserPeersFound()
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
private addKnownHops(routingPath: PeerDescriptor[]) {
|
|
108
|
-
routingPath.forEach((desc) => {
|
|
109
|
-
const newPeerId = PeerID.fromValue(desc.kademliaId)
|
|
110
|
-
if (!this.localPeerId.equals(newPeerId)) {
|
|
111
|
-
this.allKnownHops.add(newPeerId.toKey())
|
|
112
|
-
}
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
private setHopAsReported(desc: PeerDescriptor) {
|
|
117
|
-
const newPeerId = PeerID.fromValue(desc.kademliaId)
|
|
118
|
-
if (!this.localPeerId.equals(newPeerId)) {
|
|
119
|
-
this.reportedHops.add(newPeerId.toKey())
|
|
120
|
-
}
|
|
121
|
-
if (this.isFindCompleted()) {
|
|
122
|
-
if (!this.findCompletedEmitted && this.isFindCompleted()) {
|
|
123
|
-
if (this.reportFindCompletedTimeout) {
|
|
124
|
-
clearTimeout(this.reportFindCompletedTimeout)
|
|
125
|
-
this.reportFindCompletedTimeout = undefined
|
|
126
|
-
}
|
|
127
|
-
this.emit('findCompleted', this.results.getAllContacts().map((contact) => contact.getPeerDescriptor()))
|
|
128
|
-
this.findCompletedEmitted = true
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
private processFoundData(dataEntries: DataEntry[]): void {
|
|
134
|
-
dataEntries.forEach((entry) => {
|
|
135
|
-
const storerKey = keyFromPeerDescriptor(entry.storer!)
|
|
136
|
-
const existingEntry = this.foundData.get(storerKey)
|
|
137
|
-
if (!existingEntry || existingEntry.storerTime! < entry.storerTime!
|
|
138
|
-
|| (existingEntry.storerTime! <= entry.storerTime! && entry.deleted)) {
|
|
139
|
-
this.foundData.set(storerKey, entry)
|
|
140
|
-
}
|
|
141
|
-
})
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
private onNoCloserPeersFound(): void {
|
|
145
|
-
this.noCloserNodesReceivedCounter += 1
|
|
146
|
-
if (this.isFindCompleted()) {
|
|
147
|
-
this.emit('findCompleted', this.results.getAllContacts().map((contact) => contact.getPeerDescriptor()))
|
|
148
|
-
this.findCompletedEmitted = true
|
|
149
|
-
if (this.reportFindCompletedTimeout) {
|
|
150
|
-
clearTimeout(this.reportFindCompletedTimeout)
|
|
151
|
-
this.reportFindCompletedTimeout = undefined
|
|
152
|
-
}
|
|
153
|
-
} else {
|
|
154
|
-
if (!this.reportFindCompletedTimeout && !this.findCompletedEmitted) {
|
|
155
|
-
this.reportFindCompletedTimeout = setTimeout(() => {
|
|
156
|
-
if (!this.findCompletedEmitted) {
|
|
157
|
-
this.emit('findCompleted', this.results.getAllContacts().map((contact) => contact.getPeerDescriptor()))
|
|
158
|
-
this.findCompletedEmitted = true
|
|
159
|
-
}
|
|
160
|
-
}, 4000)
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
public getResults = (): FindResult => ({
|
|
166
|
-
closestNodes: this.results.getAllContacts().map((contact) => contact.getPeerDescriptor()),
|
|
167
|
-
dataEntries: (this.foundData && this.foundData.size > 0) ? Array.from(this.foundData.values()) : undefined
|
|
168
|
-
})
|
|
169
|
-
|
|
170
|
-
public stop(): void {
|
|
171
|
-
if (this.reportFindCompletedTimeout) {
|
|
172
|
-
clearTimeout(this.reportFindCompletedTimeout)
|
|
173
|
-
this.reportFindCompletedTimeout = undefined
|
|
174
|
-
}
|
|
175
|
-
this.rpcCommunicator.destroy()
|
|
176
|
-
this.emit('findCompleted', [])
|
|
177
|
-
}
|
|
178
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { IFindSessionRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
2
|
-
import { Empty } from '../../proto/google/protobuf/empty'
|
|
3
|
-
import { DataEntry, FindResponse, PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
4
|
-
import { Logger } from '@streamr/utils'
|
|
5
|
-
|
|
6
|
-
const logger = new Logger(module)
|
|
7
|
-
|
|
8
|
-
interface FindSessionRpcLocalConfig {
|
|
9
|
-
doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound?: boolean) => void
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export class FindSessionRpcLocal implements IFindSessionRpc {
|
|
13
|
-
|
|
14
|
-
private readonly config: FindSessionRpcLocalConfig
|
|
15
|
-
|
|
16
|
-
constructor(config: FindSessionRpcLocalConfig) {
|
|
17
|
-
this.config = config
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async sendFindResponse(report: FindResponse): Promise<Empty> {
|
|
21
|
-
logger.trace('FindResponse arrived: ' + JSON.stringify(report))
|
|
22
|
-
this.config.doSendFindResponse(report.routingPath, report.closestConnectedPeers, report.dataEntries, report.noCloserNodesFound)
|
|
23
|
-
return {}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DataEntry,
|
|
3
|
-
PeerDescriptor,
|
|
4
|
-
FindResponse
|
|
5
|
-
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
6
|
-
import { IFindSessionRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
7
|
-
import { Logger } from '@streamr/utils'
|
|
8
|
-
import { Remote } from '../contact/Remote'
|
|
9
|
-
|
|
10
|
-
const logger = new Logger(module)
|
|
11
|
-
|
|
12
|
-
export class FindSessionRpcRemote extends Remote<IFindSessionRpcClient> {
|
|
13
|
-
|
|
14
|
-
sendFindResponse(
|
|
15
|
-
routingPath: PeerDescriptor[],
|
|
16
|
-
closestNodes: PeerDescriptor[],
|
|
17
|
-
dataEntries: DataEntry[],
|
|
18
|
-
noCloserNodesFound: boolean
|
|
19
|
-
): void {
|
|
20
|
-
const report: FindResponse = {
|
|
21
|
-
routingPath,
|
|
22
|
-
closestConnectedPeers: closestNodes,
|
|
23
|
-
dataEntries,
|
|
24
|
-
noCloserNodesFound
|
|
25
|
-
}
|
|
26
|
-
this.getClient().sendFindResponse(report, this.formDhtRpcOptions()).catch((_e) => {
|
|
27
|
-
logger.trace('Failed to send FindResponse')
|
|
28
|
-
})
|
|
29
|
-
}
|
|
30
|
-
}
|