@streamr/dht 0.0.1-tatum.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/.eslintignore +5 -0
- package/.eslintrc +3 -0
- package/README.md +38 -0
- package/dist/src/connection/Connection.d.ts +11 -0
- package/dist/src/connection/Connection.js +23 -0
- package/dist/src/connection/Connection.js.map +1 -0
- package/dist/src/connection/ConnectionLockHandler.d.ts +23 -0
- package/dist/src/connection/ConnectionLockHandler.js +94 -0
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -0
- package/dist/src/connection/ConnectionManager.d.ts +94 -0
- package/dist/src/connection/ConnectionManager.js +554 -0
- package/dist/src/connection/ConnectionManager.js.map +1 -0
- package/dist/src/connection/ConnectivityChecker.d.ts +17 -0
- package/dist/src/connection/ConnectivityChecker.js +187 -0
- package/dist/src/connection/ConnectivityChecker.js.map +1 -0
- package/dist/src/connection/Handshaker.d.ts +19 -0
- package/dist/src/connection/Handshaker.js +77 -0
- package/dist/src/connection/Handshaker.js.map +1 -0
- package/dist/src/connection/IConnection.d.ts +38 -0
- package/dist/src/connection/IConnection.js +19 -0
- package/dist/src/connection/IConnection.js.map +1 -0
- package/dist/src/connection/IConnectionSource.d.ts +4 -0
- package/dist/src/connection/IConnectionSource.js +3 -0
- package/dist/src/connection/IConnectionSource.js.map +1 -0
- package/dist/src/connection/ManagedConnection.d.ts +60 -0
- package/dist/src/connection/ManagedConnection.js +352 -0
- package/dist/src/connection/ManagedConnection.js.map +1 -0
- package/dist/src/connection/ManagedWebRtcConnection.d.ts +7 -0
- package/dist/src/connection/ManagedWebRtcConnection.js +20 -0
- package/dist/src/connection/ManagedWebRtcConnection.js.map +1 -0
- package/dist/src/connection/RemoteConnectionLocker.d.ts +14 -0
- package/dist/src/connection/RemoteConnectionLocker.js +93 -0
- package/dist/src/connection/RemoteConnectionLocker.js.map +1 -0
- package/dist/src/connection/Simulator/Simulator.d.ts +42 -0
- package/dist/src/connection/Simulator/Simulator.js +325 -0
- package/dist/src/connection/Simulator/Simulator.js.map +1 -0
- package/dist/src/connection/Simulator/SimulatorConnection.d.ts +19 -0
- package/dist/src/connection/Simulator/SimulatorConnection.js +118 -0
- package/dist/src/connection/Simulator/SimulatorConnection.js.map +1 -0
- package/dist/src/connection/Simulator/SimulatorConnector.d.ts +17 -0
- package/dist/src/connection/Simulator/SimulatorConnector.js +72 -0
- package/dist/src/connection/Simulator/SimulatorConnector.js.map +1 -0
- package/dist/src/connection/Simulator/SimulatorTransport.d.ts +6 -0
- package/dist/src/connection/Simulator/SimulatorTransport.js +11 -0
- package/dist/src/connection/Simulator/SimulatorTransport.js.map +1 -0
- package/dist/src/connection/Simulator/pings.d.ts +21 -0
- package/dist/src/connection/Simulator/pings.js +61 -0
- package/dist/src/connection/Simulator/pings.js.map +1 -0
- package/dist/src/connection/WebRTC/IWebRtcConnection.d.ts +20 -0
- package/dist/src/connection/WebRTC/IWebRtcConnection.js +9 -0
- package/dist/src/connection/WebRTC/IWebRtcConnection.js.map +1 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.d.ts +47 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js +233 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js.map +1 -0
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.d.ts +12 -0
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js +74 -0
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js.map +1 -0
- package/dist/src/connection/WebRTC/WebRtcConnector.d.ts +47 -0
- package/dist/src/connection/WebRTC/WebRtcConnector.js +227 -0
- package/dist/src/connection/WebRTC/WebRtcConnector.js.map +1 -0
- package/dist/src/connection/WebRTC/iceServerAsString.d.ts +2 -0
- package/dist/src/connection/WebRTC/iceServerAsString.js +18 -0
- package/dist/src/connection/WebRTC/iceServerAsString.js.map +1 -0
- package/dist/src/connection/WebSocket/ClientWebSocket.d.ts +15 -0
- package/dist/src/connection/WebSocket/ClientWebSocket.js +113 -0
- package/dist/src/connection/WebSocket/ClientWebSocket.js.map +1 -0
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.d.ts +9 -0
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js +63 -0
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js.map +1 -0
- package/dist/src/connection/WebSocket/ServerWebSocket.d.ts +18 -0
- package/dist/src/connection/WebSocket/ServerWebSocket.js +103 -0
- package/dist/src/connection/WebSocket/ServerWebSocket.js.map +1 -0
- package/dist/src/connection/WebSocket/WebSocketConnector.d.ts +31 -0
- package/dist/src/connection/WebSocket/WebSocketConnector.js +202 -0
- package/dist/src/connection/WebSocket/WebSocketConnector.js.map +1 -0
- package/dist/src/connection/WebSocket/WebSocketServer.d.ts +9 -0
- package/dist/src/connection/WebSocket/WebSocketServer.js +101 -0
- package/dist/src/connection/WebSocket/WebSocketServer.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +153 -0
- package/dist/src/dht/DhtNode.js +599 -0
- package/dist/src/dht/DhtNode.js.map +1 -0
- package/dist/src/dht/DhtPeer.d.ts +18 -0
- package/dist/src/dht/DhtPeer.js +74 -0
- package/dist/src/dht/DhtPeer.js.map +1 -0
- package/dist/src/dht/ExternalApi.d.ts +8 -0
- package/dist/src/dht/ExternalApi.js +26 -0
- package/dist/src/dht/ExternalApi.js.map +1 -0
- package/dist/src/dht/RemoteExternalApi.d.ts +6 -0
- package/dist/src/dht/RemoteExternalApi.js +26 -0
- package/dist/src/dht/RemoteExternalApi.js.map +1 -0
- package/dist/src/dht/contact/Contact.d.ts +22 -0
- package/dist/src/dht/contact/Contact.js +25 -0
- package/dist/src/dht/contact/Contact.js.map +1 -0
- package/dist/src/dht/contact/RandomContactList.d.ts +20 -0
- package/dist/src/dht/contact/RandomContactList.js +78 -0
- package/dist/src/dht/contact/RandomContactList.js.map +1 -0
- package/dist/src/dht/contact/Remote.d.ts +15 -0
- package/dist/src/dht/contact/Remote.js +24 -0
- package/dist/src/dht/contact/Remote.js.map +1 -0
- package/dist/src/dht/contact/SortedContactList.d.ts +35 -0
- package/dist/src/dht/contact/SortedContactList.js +156 -0
- package/dist/src/dht/contact/SortedContactList.js.map +1 -0
- package/dist/src/dht/discovery/DiscoverySession.d.ts +36 -0
- package/dist/src/dht/discovery/DiscoverySession.js +116 -0
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -0
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +42 -0
- package/dist/src/dht/discovery/PeerDiscovery.js +157 -0
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -0
- package/dist/src/dht/find/RecursiveFindSession.d.ts +46 -0
- package/dist/src/dht/find/RecursiveFindSession.js +142 -0
- package/dist/src/dht/find/RecursiveFindSession.js.map +1 -0
- package/dist/src/dht/find/RecursiveFinder.d.ts +54 -0
- package/dist/src/dht/find/RecursiveFinder.js +180 -0
- package/dist/src/dht/find/RecursiveFinder.js.map +1 -0
- package/dist/src/dht/find/RemoteRecursiveFindSession.d.ts +6 -0
- package/dist/src/dht/find/RemoteRecursiveFindSession.js +25 -0
- package/dist/src/dht/find/RemoteRecursiveFindSession.js.map +1 -0
- package/dist/src/dht/routing/DuplicateDetector.d.ts +13 -0
- package/dist/src/dht/routing/DuplicateDetector.js +41 -0
- package/dist/src/dht/routing/DuplicateDetector.js.map +1 -0
- package/dist/src/dht/routing/RemoteRouter.d.ts +8 -0
- package/dist/src/dht/routing/RemoteRouter.js +106 -0
- package/dist/src/dht/routing/RemoteRouter.js.map +1 -0
- package/dist/src/dht/routing/Router.d.ts +60 -0
- package/dist/src/dht/routing/Router.js +207 -0
- package/dist/src/dht/routing/Router.js.map +1 -0
- package/dist/src/dht/routing/RoutingSession.d.ts +42 -0
- package/dist/src/dht/routing/RoutingSession.js +178 -0
- package/dist/src/dht/routing/RoutingSession.js.map +1 -0
- package/dist/src/dht/store/DataStore.d.ts +45 -0
- package/dist/src/dht/store/DataStore.js +244 -0
- package/dist/src/dht/store/DataStore.js.map +1 -0
- package/dist/src/dht/store/LocalDataStore.d.ts +19 -0
- package/dist/src/dht/store/LocalDataStore.js +104 -0
- package/dist/src/dht/store/LocalDataStore.js.map +1 -0
- package/dist/src/dht/store/RemoteStore.d.ts +8 -0
- package/dist/src/dht/store/RemoteStore.js +44 -0
- package/dist/src/dht/store/RemoteStore.js.map +1 -0
- package/dist/src/exports.d.ts +19 -0
- package/dist/src/exports.js +41 -0
- package/dist/src/exports.js.map +1 -0
- package/dist/src/helpers/AddressTools.d.ts +2 -0
- package/dist/src/helpers/AddressTools.js +31 -0
- package/dist/src/helpers/AddressTools.js.map +1 -0
- package/dist/src/helpers/PeerID.d.ts +25 -0
- package/dist/src/helpers/PeerID.js +84 -0
- package/dist/src/helpers/PeerID.js.map +1 -0
- package/dist/src/helpers/UUID.d.ts +7 -0
- package/dist/src/helpers/UUID.js +32 -0
- package/dist/src/helpers/UUID.js.map +1 -0
- package/dist/src/helpers/debugHelpers.d.ts +3 -0
- package/dist/src/helpers/debugHelpers.js +11 -0
- package/dist/src/helpers/debugHelpers.js.map +1 -0
- package/dist/src/helpers/errors.d.ts +72 -0
- package/dist/src/helpers/errors.js +95 -0
- package/dist/src/helpers/errors.js.map +1 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +5 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +17 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -0
- package/dist/src/helpers/protoClasses.d.ts +2 -0
- package/dist/src/helpers/protoClasses.js +35 -0
- package/dist/src/helpers/protoClasses.js.map +1 -0
- package/dist/src/helpers/protoToString.d.ts +2 -0
- package/dist/src/helpers/protoToString.js +20 -0
- package/dist/src/helpers/protoToString.js.map +1 -0
- package/dist/src/proto/google/protobuf/any.d.ts +173 -0
- package/dist/src/proto/google/protobuf/any.js +155 -0
- package/dist/src/proto/google/protobuf/any.js.map +1 -0
- package/dist/src/proto/google/protobuf/empty.d.ts +32 -0
- package/dist/src/proto/google/protobuf/empty.js +34 -0
- package/dist/src/proto/google/protobuf/empty.js.map +1 -0
- package/dist/src/proto/google/protobuf/timestamp.d.ts +149 -0
- package/dist/src/proto/google/protobuf/timestamp.js +136 -0
- package/dist/src/proto/google/protobuf/timestamp.js.map +1 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +320 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +245 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +1089 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +710 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +145 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.js +3 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.js.map +1 -0
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.d.ts +87 -0
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +66 -0
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js.map +1 -0
- package/dist/src/proto/tests.d.ts +39 -0
- package/dist/src/proto/tests.js +34 -0
- package/dist/src/proto/tests.js.map +1 -0
- package/dist/src/rpc-protocol/DhtCallContext.d.ts +12 -0
- package/dist/src/rpc-protocol/DhtCallContext.js +8 -0
- package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -0
- package/dist/src/rpc-protocol/DhtRpcOptions.d.ts +8 -0
- package/dist/src/rpc-protocol/DhtRpcOptions.js +3 -0
- package/dist/src/rpc-protocol/DhtRpcOptions.js.map +1 -0
- package/dist/src/transport/ITransport.d.ts +22 -0
- package/dist/src/transport/ITransport.js +3 -0
- package/dist/src/transport/ITransport.js.map +1 -0
- package/dist/src/transport/ListeningRpcCommunicator.d.ts +6 -0
- package/dist/src/transport/ListeningRpcCommunicator.js +14 -0
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -0
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +8 -0
- package/dist/src/transport/RoutingRpcCommunicator.js +52 -0
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -0
- package/jest.config.js +2 -0
- package/karma.config.js +20 -0
- package/package.json +64 -0
- package/proto.sh +3 -0
- package/protos/DhtRpc.proto +330 -0
- package/protos/tests.proto +16 -0
- package/src/connection/Connection.ts +23 -0
- package/src/connection/ConnectionLockHandler.ts +105 -0
- package/src/connection/ConnectionManager.ts +676 -0
- package/src/connection/ConnectivityChecker.ts +173 -0
- package/src/connection/Handshaker.ts +92 -0
- package/src/connection/IConnection.ts +47 -0
- package/src/connection/IConnectionSource.ts +6 -0
- package/src/connection/ManagedConnection.ts +398 -0
- package/src/connection/ManagedWebRtcConnection.ts +27 -0
- package/src/connection/RemoteConnectionLocker.ts +88 -0
- package/src/connection/Simulator/Simulator.ts +399 -0
- package/src/connection/Simulator/SimulatorConnection.ts +137 -0
- package/src/connection/Simulator/SimulatorConnector.ts +104 -0
- package/src/connection/Simulator/SimulatorTransport.ts +9 -0
- package/src/connection/Simulator/pings.ts +42 -0
- package/src/connection/WebRTC/BrowserWebRtcConnection.ts +227 -0
- package/src/connection/WebRTC/IWebRtcConnection.ts +24 -0
- package/src/connection/WebRTC/NodeWebRtcConnection.ts +256 -0
- package/src/connection/WebRTC/RemoteWebrtcConnector.ts +93 -0
- package/src/connection/WebRTC/WebRtcConnector.ts +306 -0
- package/src/connection/WebRTC/iceServerAsString.ts +15 -0
- package/src/connection/WebSocket/ClientWebSocket.ts +118 -0
- package/src/connection/WebSocket/RemoteWebSocketConnector.ts +49 -0
- package/src/connection/WebSocket/ServerWebSocket.ts +119 -0
- package/src/connection/WebSocket/WebSocketConnector.ts +264 -0
- package/src/connection/WebSocket/WebSocketServer.ts +97 -0
- package/src/dht/DhtNode.ts +776 -0
- package/src/dht/DhtPeer.ts +96 -0
- package/src/dht/ExternalApi.ts +29 -0
- package/src/dht/RemoteExternalApi.ts +25 -0
- package/src/dht/contact/Contact.ts +36 -0
- package/src/dht/contact/RandomContactList.ts +92 -0
- package/src/dht/contact/Remote.ts +40 -0
- package/src/dht/contact/SortedContactList.ts +196 -0
- package/src/dht/discovery/DiscoverySession.ts +150 -0
- package/src/dht/discovery/PeerDiscovery.ts +162 -0
- package/src/dht/find/RecursiveFindSession.ts +178 -0
- package/src/dht/find/RecursiveFinder.ts +272 -0
- package/src/dht/find/RemoteRecursiveFindSession.ts +33 -0
- package/src/dht/routing/DuplicateDetector.ts +53 -0
- package/src/dht/routing/RemoteRouter.ts +115 -0
- package/src/dht/routing/Router.ts +266 -0
- package/src/dht/routing/RoutingSession.ts +222 -0
- package/src/dht/store/DataStore.ts +321 -0
- package/src/dht/store/LocalDataStore.ts +114 -0
- package/src/dht/store/RemoteStore.ts +58 -0
- package/src/exports.ts +19 -0
- package/src/helpers/AddressTools.ts +26 -0
- package/src/helpers/PeerID.ts +95 -0
- package/src/helpers/UUID.ts +30 -0
- package/src/helpers/debugHelpers.ts +9 -0
- package/src/helpers/errors.ts +49 -0
- package/src/helpers/peerIdFromPeerDescriptor.ts +14 -0
- package/src/helpers/protoClasses.ts +63 -0
- package/src/helpers/protoToString.ts +21 -0
- package/src/proto/google/protobuf/any.ts +319 -0
- package/src/proto/google/protobuf/empty.ts +84 -0
- package/src/proto/google/protobuf/timestamp.ts +281 -0
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +373 -0
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +148 -0
- package/src/proto/packages/dht/protos/DhtRpc.ts +1399 -0
- package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +108 -0
- package/src/proto/tests.ts +52 -0
- package/src/rpc-protocol/DhtCallContext.ts +15 -0
- package/src/rpc-protocol/DhtRpcOptions.ts +9 -0
- package/src/transport/ITransport.ts +31 -0
- package/src/transport/ListeningRpcCommunicator.ts +14 -0
- package/src/transport/RoutingRpcCommunicator.ts +59 -0
- package/src/types/glogal.d.ts +1 -0
- package/src/types/textencoding.d.ts +7 -0
- package/test/RandomGraphSimulation.ts +52 -0
- package/test/benchmark/KademliaCorrectness.test.ts +115 -0
- package/test/benchmark/RecursiveFind.test.ts +87 -0
- package/test/benchmark/any.test.ts +28 -0
- package/test/benchmark/kademlia-simulation/Contact.ts +32 -0
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +94 -0
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +125 -0
- package/test/data/generateGroundTruthData.ts +70 -0
- package/test/end-to-end/Layer0-Layer1.test.ts +87 -0
- package/test/end-to-end/Layer0.test.ts +60 -0
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +94 -0
- package/test/end-to-end/Layer0WebRTC-Layer1.test.ts +134 -0
- package/test/end-to-end/Layer0WebRTC.test.ts +98 -0
- package/test/end-to-end/Layer1-Scale-WebRTC.test.ts +69 -0
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +73 -0
- package/test/end-to-end/WebSocketConnectionRequest.test.ts +62 -0
- package/test/integration/ConnectionLocking.test.ts +166 -0
- package/test/integration/ConnectionManager.test.ts +291 -0
- package/test/integration/DhtNodeExternalAPI.test.ts +43 -0
- package/test/integration/DhtPeer.test.ts +73 -0
- package/test/integration/DhtRpc.test.ts +131 -0
- package/test/integration/DhtWithMockConnectionLatencies.test.ts +46 -0
- package/test/integration/DhtWithMockConnections.test.ts +46 -0
- package/test/integration/DhtWithRealConnectionLatencies.test.ts +47 -0
- package/test/integration/Layer1-scale.test.ts +200 -0
- package/test/integration/MigrateData.test.ts +203 -0
- package/test/integration/Mock-Layer1-Layer0.test.ts +106 -0
- package/test/integration/MultipleEntryPointJoining.test.ts +105 -0
- package/test/integration/RecursiveFind.test.ts +50 -0
- package/test/integration/RemoteRouter.test.ts +83 -0
- package/test/integration/RemoteStore.test.ts +66 -0
- package/test/integration/RouteMessage.test.ts +254 -0
- package/test/integration/RpcErrors.test.ts +153 -0
- package/test/integration/ScaleDownDht.test.ts +66 -0
- package/test/integration/SimultaneousConnections.test.ts +308 -0
- package/test/integration/Store.test.ts +72 -0
- package/test/integration/StoreAndDelete.test.ts +93 -0
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +71 -0
- package/test/integration/WebRtcConnectionManagement.test.ts +205 -0
- package/test/integration/WebRtcConnectorRpc.test.ts +145 -0
- package/test/integration/WebSocket.test.ts +64 -0
- package/test/integration/WebSocketConnectionManagement.test.ts +131 -0
- package/test/integration/WebSocketConnectorRpc.test.ts +86 -0
- package/test/kademlia-simulation/data/nodeids.json +13002 -0
- package/test/kademlia-simulation/data/orderedneighbors.json +1001 -0
- package/test/unit/AddressTools.test.ts +40 -0
- package/test/unit/DuplicateDetector.test.ts +29 -0
- package/test/unit/LocalDataStore.test.ts +107 -0
- package/test/unit/PeerID.test.ts +22 -0
- package/test/unit/ProtobufMessage.test.ts +21 -0
- package/test/unit/RandomContactList.test.ts +87 -0
- package/test/unit/RecursiveFinder.test.ts +112 -0
- package/test/unit/Router.test.ts +124 -0
- package/test/unit/SortedContactList.test.ts +127 -0
- package/test/unit/UUID.test.ts +49 -0
- package/test/unit/WebSocketServer.test.ts +42 -0
- package/test/utils/mock/RecursiveFinder.ts +19 -0
- package/test/utils/mock/Router.ts +53 -0
- package/test/utils/mock/Transport.ts +26 -0
- package/test/utils/utils.ts +311 -0
- package/tsconfig.browser.json +15 -0
- package/tsconfig.jest.json +19 -0
- package/tsconfig.json +3 -0
- package/tsconfig.node.json +18 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { ConnectionManager } from '../../src/connection/ConnectionManager'
|
|
2
|
+
import { Message, MessageType, NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
3
|
+
import { PeerID } from '../../src/helpers/PeerID'
|
|
4
|
+
import { Simulator } from '../../src/connection/Simulator/Simulator'
|
|
5
|
+
import { createPeerDescriptor } from '../../src/dht/DhtNode'
|
|
6
|
+
import { RpcMessage } from '../../src/proto/packages/proto-rpc/protos/ProtoRpc'
|
|
7
|
+
import { Logger } from '@streamr/utils'
|
|
8
|
+
|
|
9
|
+
const logger = new Logger(module)
|
|
10
|
+
|
|
11
|
+
describe('ConnectionManager', () => {
|
|
12
|
+
const serviceId = 'demo'
|
|
13
|
+
|
|
14
|
+
const mockPeerDescriptor1: PeerDescriptor = {
|
|
15
|
+
kademliaId: PeerID.fromString('tester1').value,
|
|
16
|
+
nodeName: 'tester1',
|
|
17
|
+
type: NodeType.NODEJS
|
|
18
|
+
}
|
|
19
|
+
const mockPeerDescriptor2: PeerDescriptor = {
|
|
20
|
+
kademliaId: PeerID.fromString('tester2').value,
|
|
21
|
+
nodeName: 'tester2',
|
|
22
|
+
type: NodeType.NODEJS
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const mockPeerDescriptor3: PeerDescriptor = {
|
|
26
|
+
kademliaId: PeerID.fromString('tester3').value,
|
|
27
|
+
nodeName: 'tester3',
|
|
28
|
+
type: NodeType.NODEJS
|
|
29
|
+
}
|
|
30
|
+
const mockPeerDescriptor4: PeerDescriptor = {
|
|
31
|
+
kademliaId: PeerID.fromString('tester4').value,
|
|
32
|
+
nodeName: 'tester4',
|
|
33
|
+
type: NodeType.NODEJS
|
|
34
|
+
}
|
|
35
|
+
const simulator = new Simulator()
|
|
36
|
+
|
|
37
|
+
const mockTransport = new ConnectionManager({ ownPeerDescriptor: mockPeerDescriptor1, simulator: simulator })
|
|
38
|
+
const mockConnectorTransport1 = new ConnectionManager({ ownPeerDescriptor: mockPeerDescriptor1, simulator })
|
|
39
|
+
const mockConnectorTransport2 = new ConnectionManager({ ownPeerDescriptor: mockPeerDescriptor2, simulator })
|
|
40
|
+
|
|
41
|
+
afterAll(async ()=> {
|
|
42
|
+
await mockTransport.stop()
|
|
43
|
+
await mockConnectorTransport1.stop()
|
|
44
|
+
await mockConnectorTransport2.stop()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('Can start alone', async () => {
|
|
48
|
+
const connectionManager = new ConnectionManager({ transportLayer: mockTransport, webSocketHost: '127.0.0.1', webSocketPort: 9991 })
|
|
49
|
+
|
|
50
|
+
await connectionManager.start((report) => {
|
|
51
|
+
expect(report.ip).toEqual('127.0.0.1')
|
|
52
|
+
expect(report.openInternet).toEqual(true)
|
|
53
|
+
return createPeerDescriptor(report)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
await connectionManager.stop()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('Throws an async exception if fails to connect to entrypoints', async () => {
|
|
60
|
+
|
|
61
|
+
const connectionManager = new ConnectionManager({
|
|
62
|
+
transportLayer: mockTransport,
|
|
63
|
+
webSocketPort: 9992, entryPoints: [
|
|
64
|
+
{ kademliaId: Uint8Array.from([1, 2, 3]), type: NodeType.NODEJS, websocket: { ip: '127.0.0.1', port: 12345 } }
|
|
65
|
+
]
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
await expect(connectionManager.start((report) => {
|
|
69
|
+
return createPeerDescriptor(report)
|
|
70
|
+
})).rejects.toThrow('Failed to connect to the entrypoints')
|
|
71
|
+
|
|
72
|
+
await connectionManager.stop()
|
|
73
|
+
}, 15000)
|
|
74
|
+
|
|
75
|
+
it('Can probe connectivity in open internet', async () => {
|
|
76
|
+
const connectionManager1 = new ConnectionManager({ transportLayer: mockTransport, webSocketHost: '127.0.0.1', webSocketPort: 9993 })
|
|
77
|
+
|
|
78
|
+
await connectionManager1.start((report) => {
|
|
79
|
+
expect(report.ip).toEqual('127.0.0.1')
|
|
80
|
+
expect(report.openInternet).toEqual(true)
|
|
81
|
+
return createPeerDescriptor(report)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const connectionManager2 = new ConnectionManager({
|
|
85
|
+
transportLayer: mockConnectorTransport2,
|
|
86
|
+
webSocketPort: 9994, entryPoints: [
|
|
87
|
+
{ kademliaId: Uint8Array.from([1, 2, 3]), type: NodeType.NODEJS, websocket: { ip: '127.0.0.1', port: 9993 } }
|
|
88
|
+
]
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
await connectionManager2.start((report) => {
|
|
92
|
+
expect(report.ip).toEqual('127.0.0.1')
|
|
93
|
+
expect(report.openInternet).toEqual(true)
|
|
94
|
+
return createPeerDescriptor(report)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
await connectionManager1.stop()
|
|
98
|
+
await connectionManager2.stop()
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('Can send data to other connectionmanager over websocket', async () => {
|
|
102
|
+
const connectionManager1 = new ConnectionManager({ transportLayer: mockConnectorTransport1, webSocketHost: '127.0.0.1', webSocketPort: 9995 })
|
|
103
|
+
|
|
104
|
+
let peerDescriptor: PeerDescriptor | undefined
|
|
105
|
+
|
|
106
|
+
await connectionManager1.start((report) => {
|
|
107
|
+
expect(report.ip).toEqual('127.0.0.1')
|
|
108
|
+
expect(report.openInternet).toEqual(true)
|
|
109
|
+
peerDescriptor = createPeerDescriptor(report)
|
|
110
|
+
return peerDescriptor
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
const connectionManager2 = new ConnectionManager({
|
|
114
|
+
transportLayer: mockConnectorTransport2,
|
|
115
|
+
webSocketPort: 9996, entryPoints: [
|
|
116
|
+
peerDescriptor!
|
|
117
|
+
]
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
let peerDescriptor2: PeerDescriptor | undefined
|
|
121
|
+
await connectionManager2.start((report2) => {
|
|
122
|
+
expect(report2.ip).toEqual('127.0.0.1')
|
|
123
|
+
expect(report2.openInternet).toEqual(true)
|
|
124
|
+
peerDescriptor2 = createPeerDescriptor(report2)
|
|
125
|
+
return peerDescriptor2
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
const msg: Message = {
|
|
129
|
+
serviceId: serviceId,
|
|
130
|
+
messageType: MessageType.RPC,
|
|
131
|
+
messageId: '1',
|
|
132
|
+
body: {
|
|
133
|
+
oneofKind: 'rpcMessage',
|
|
134
|
+
rpcMessage: RpcMessage.create()
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const promise = new Promise<void>((resolve, _reject) => {
|
|
139
|
+
connectionManager2.on('message', async (message: Message) => {
|
|
140
|
+
expect(message.messageType).toBe(MessageType.RPC)
|
|
141
|
+
resolve()
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
const connectedPromise1 = new Promise<void>((resolve, _reject) => {
|
|
146
|
+
connectionManager1.on('connected', (_peerDescriptor: PeerDescriptor) => {
|
|
147
|
+
resolve()
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
const connectedPromise2 = new Promise<void>((resolve, _reject) => {
|
|
152
|
+
connectionManager2.on('connected', (_peerDescriptor: PeerDescriptor) => {
|
|
153
|
+
resolve()
|
|
154
|
+
})
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
msg.targetDescriptor = peerDescriptor2
|
|
158
|
+
connectionManager1.send(msg)
|
|
159
|
+
|
|
160
|
+
await Promise.all([promise, connectedPromise1, connectedPromise2])
|
|
161
|
+
|
|
162
|
+
await connectionManager1.stop()
|
|
163
|
+
await connectionManager2.stop()
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('Can disconnect websockets', async () => {
|
|
167
|
+
const connectionManager1 = new ConnectionManager({ transportLayer: mockConnectorTransport1, webSocketHost: '127.0.0.1', webSocketPort: 9997 })
|
|
168
|
+
|
|
169
|
+
let peerDescriptor: PeerDescriptor | undefined
|
|
170
|
+
await connectionManager1.start((report) => {
|
|
171
|
+
expect(report.ip).toEqual('127.0.0.1')
|
|
172
|
+
expect(report.openInternet).toEqual(true)
|
|
173
|
+
peerDescriptor = createPeerDescriptor(report)
|
|
174
|
+
return peerDescriptor
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
const connectionManager2 = new ConnectionManager({
|
|
178
|
+
transportLayer: mockConnectorTransport2,
|
|
179
|
+
webSocketPort: 9999, entryPoints: [
|
|
180
|
+
peerDescriptor!
|
|
181
|
+
]
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
let peerDescriptor2: PeerDescriptor | undefined
|
|
185
|
+
await connectionManager2.start((report2) => {
|
|
186
|
+
peerDescriptor2 = createPeerDescriptor(report2)
|
|
187
|
+
return peerDescriptor2
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
const msg: Message = {
|
|
191
|
+
serviceId: serviceId,
|
|
192
|
+
messageType: MessageType.RPC,
|
|
193
|
+
messageId: '1',
|
|
194
|
+
body: {
|
|
195
|
+
oneofKind: 'rpcMessage',
|
|
196
|
+
rpcMessage: RpcMessage.create()
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const disconnectedPromise1 = new Promise<void>((resolve, _reject) => {
|
|
201
|
+
connectionManager1.on('disconnected', (_peerDescriptor: PeerDescriptor) => {
|
|
202
|
+
logger.info('disconnectedPromise1')
|
|
203
|
+
resolve()
|
|
204
|
+
})
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
const disconnectedPromise2 = new Promise<void>((resolve, _reject) => {
|
|
208
|
+
connectionManager2.on('disconnected', (_peerDescriptor: PeerDescriptor) => {
|
|
209
|
+
logger.info('disconnectedPromise2')
|
|
210
|
+
resolve()
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
const promise = new Promise<void>((resolve, _reject) => {
|
|
215
|
+
connectionManager2.on('message', async (message: Message) => {
|
|
216
|
+
expect(message.messageType).toBe(MessageType.RPC)
|
|
217
|
+
resolve()
|
|
218
|
+
})
|
|
219
|
+
})
|
|
220
|
+
msg.targetDescriptor = peerDescriptor2
|
|
221
|
+
connectionManager1.send(msg)
|
|
222
|
+
|
|
223
|
+
await promise
|
|
224
|
+
|
|
225
|
+
// @ts-expect-error private field
|
|
226
|
+
connectionManager1.closeConnection(peerDescriptor2)
|
|
227
|
+
|
|
228
|
+
await Promise.all([disconnectedPromise1, disconnectedPromise2])
|
|
229
|
+
|
|
230
|
+
await connectionManager1.stop()
|
|
231
|
+
await connectionManager2.stop()
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
it('Connects and disconnects over simulated connections', async () => {
|
|
235
|
+
const simulator2 = new Simulator()
|
|
236
|
+
const connectionManager3 = new ConnectionManager({ ownPeerDescriptor: mockPeerDescriptor3, simulator: simulator2 })
|
|
237
|
+
const connectionManager4 = new ConnectionManager({ ownPeerDescriptor: mockPeerDescriptor4, simulator: simulator2 })
|
|
238
|
+
|
|
239
|
+
const msg: Message = {
|
|
240
|
+
serviceId: serviceId,
|
|
241
|
+
messageType: MessageType.RPC,
|
|
242
|
+
messageId: '1',
|
|
243
|
+
body: {
|
|
244
|
+
oneofKind: 'rpcMessage',
|
|
245
|
+
rpcMessage: RpcMessage.create()
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const dataPromise = new Promise<void>((resolve, _reject) => {
|
|
250
|
+
connectionManager4.on('message', async (message: Message) => {
|
|
251
|
+
expect(message.messageType).toBe(MessageType.RPC)
|
|
252
|
+
resolve()
|
|
253
|
+
})
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
const connectedPromise1 = new Promise<void>((resolve, _reject) => {
|
|
257
|
+
connectionManager4.on('connected', (_peerDescriptor: PeerDescriptor) => {
|
|
258
|
+
resolve()
|
|
259
|
+
})
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
const connectedPromise2 = new Promise<void>((resolve, _reject) => {
|
|
263
|
+
connectionManager3.on('connected', (_peerDescriptor: PeerDescriptor) => {
|
|
264
|
+
resolve()
|
|
265
|
+
})
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
const disconnectedPromise1 = new Promise<void>((resolve, _reject) => {
|
|
269
|
+
connectionManager4.on('disconnected', (_peerDescriptor: PeerDescriptor) => {
|
|
270
|
+
resolve()
|
|
271
|
+
})
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
const disconnectedPromise2 = new Promise<void>((resolve, _reject) => {
|
|
275
|
+
connectionManager3.on('disconnected', (_peerDescriptor: PeerDescriptor) => {
|
|
276
|
+
resolve()
|
|
277
|
+
})
|
|
278
|
+
})
|
|
279
|
+
msg.targetDescriptor = mockPeerDescriptor4
|
|
280
|
+
connectionManager3.send(msg)
|
|
281
|
+
await Promise.all([dataPromise, connectedPromise1, connectedPromise2])
|
|
282
|
+
|
|
283
|
+
// @ts-expect-error private field
|
|
284
|
+
connectionManager3.closeConnection(mockPeerDescriptor4)
|
|
285
|
+
|
|
286
|
+
await Promise.all([disconnectedPromise1, disconnectedPromise2])
|
|
287
|
+
await connectionManager3.stop()
|
|
288
|
+
await connectionManager4.stop()
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
})
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { DhtNode } from '../../src/dht/DhtNode'
|
|
2
|
+
import { LatencyType, Simulator } from '../../src/connection/Simulator/Simulator'
|
|
3
|
+
import { createMockConnectionDhtNode } from '../utils/utils'
|
|
4
|
+
import { Any } from '../../src/proto/google/protobuf/any'
|
|
5
|
+
import { PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
6
|
+
import { PeerID } from '../../src/helpers/PeerID'
|
|
7
|
+
|
|
8
|
+
describe('DhtNodeExternalApi', () => {
|
|
9
|
+
|
|
10
|
+
let simulator: Simulator
|
|
11
|
+
let dhtNode1: DhtNode
|
|
12
|
+
let remote: DhtNode
|
|
13
|
+
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
simulator = new Simulator(LatencyType.NONE)
|
|
16
|
+
dhtNode1 = await createMockConnectionDhtNode('node1', simulator)
|
|
17
|
+
remote = await createMockConnectionDhtNode('remote', simulator)
|
|
18
|
+
await dhtNode1.joinDht([dhtNode1.getPeerDescriptor()])
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
afterEach(async () => {
|
|
22
|
+
await Promise.all([
|
|
23
|
+
dhtNode1.stop(),
|
|
24
|
+
remote.stop()
|
|
25
|
+
])
|
|
26
|
+
simulator.stop()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('findData happy path', async () => {
|
|
30
|
+
const data = Any.pack(dhtNode1.getPeerDescriptor(), PeerDescriptor)
|
|
31
|
+
const key = PeerID.fromString('key').value
|
|
32
|
+
await dhtNode1.storeDataToDht(key, data)
|
|
33
|
+
|
|
34
|
+
const foundData = await remote.findDataViaPeer(key, dhtNode1.getPeerDescriptor())
|
|
35
|
+
expect(Any.unpack(foundData[0].data!, PeerDescriptor)).toEqual(dhtNode1.getPeerDescriptor())
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('findData returns empty array if no data found', async () => {
|
|
39
|
+
const foundData = await remote.findDataViaPeer(PeerID.fromString('key').value, dhtNode1.getPeerDescriptor())
|
|
40
|
+
expect(foundData).toEqual([])
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
})
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { DhtPeer } from '../../src/dht/DhtPeer'
|
|
2
|
+
import { RpcCommunicator, toProtoRpcClient } from '@streamr/proto-rpc'
|
|
3
|
+
import { getMockPeers, MockDhtRpc } from '../utils/utils'
|
|
4
|
+
import {
|
|
5
|
+
ClosestPeersRequest,
|
|
6
|
+
ClosestPeersResponse,
|
|
7
|
+
PeerDescriptor,
|
|
8
|
+
PingRequest,
|
|
9
|
+
PingResponse
|
|
10
|
+
} from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
11
|
+
import { RpcMessage } from '../../src/proto/packages/proto-rpc/protos/ProtoRpc'
|
|
12
|
+
import { DhtRpcServiceClient } from '../../src/proto/packages/dht/protos/DhtRpc.client'
|
|
13
|
+
import { generateId } from '../utils/utils'
|
|
14
|
+
import { DhtCallContext } from '../../src/rpc-protocol/DhtCallContext'
|
|
15
|
+
|
|
16
|
+
describe('DhtPeer', () => {
|
|
17
|
+
|
|
18
|
+
let dhtPeer: DhtPeer
|
|
19
|
+
let clientRpcCommunicator: RpcCommunicator
|
|
20
|
+
let serverRpcCommunicator: RpcCommunicator
|
|
21
|
+
const serviceId = 'test'
|
|
22
|
+
const clientPeerDescriptor: PeerDescriptor = {
|
|
23
|
+
kademliaId: generateId('dhtPeer'),
|
|
24
|
+
type: 0
|
|
25
|
+
}
|
|
26
|
+
const serverPeerDescriptor: PeerDescriptor = {
|
|
27
|
+
kademliaId: generateId('server'),
|
|
28
|
+
type: 0
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
clientRpcCommunicator = new RpcCommunicator()
|
|
33
|
+
serverRpcCommunicator = new RpcCommunicator()
|
|
34
|
+
serverRpcCommunicator.registerRpcMethod(ClosestPeersRequest, ClosestPeersResponse, 'getClosestPeers', MockDhtRpc.getClosestPeers)
|
|
35
|
+
serverRpcCommunicator.registerRpcMethod(PingRequest, PingResponse, 'ping', MockDhtRpc.ping)
|
|
36
|
+
clientRpcCommunicator.on('outgoingMessage', (message: RpcMessage, _requestId: string, _ucallContext?: DhtCallContext) => {
|
|
37
|
+
serverRpcCommunicator.handleIncomingMessage(message)
|
|
38
|
+
})
|
|
39
|
+
serverRpcCommunicator.on('outgoingMessage', (message: RpcMessage, _requestId: string, _ucallContext?: DhtCallContext) => {
|
|
40
|
+
clientRpcCommunicator.handleIncomingMessage(message)
|
|
41
|
+
})
|
|
42
|
+
const client = toProtoRpcClient(new DhtRpcServiceClient(clientRpcCommunicator.getRpcClientTransport()))
|
|
43
|
+
dhtPeer = new DhtPeer(clientPeerDescriptor, serverPeerDescriptor, client, serviceId)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
clientRpcCommunicator.stop()
|
|
48
|
+
serverRpcCommunicator.stop()
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('Ping happy path', async () => {
|
|
52
|
+
const active = await dhtPeer.ping()
|
|
53
|
+
expect(active).toEqual(true)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('getClosestPeers happy path', async () => {
|
|
57
|
+
const neighbors = await dhtPeer.getClosestPeers(clientPeerDescriptor.kademliaId)
|
|
58
|
+
expect(neighbors.length).toEqual(getMockPeers().length)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('ping error path', async () => {
|
|
62
|
+
serverRpcCommunicator.registerRpcMethod(PingRequest, PingResponse, 'ping', MockDhtRpc.throwPingError)
|
|
63
|
+
const active = await dhtPeer.ping()
|
|
64
|
+
expect(active).toEqual(false)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('getClosestPeers error path', async () => {
|
|
68
|
+
serverRpcCommunicator.registerRpcMethod(ClosestPeersRequest, ClosestPeersResponse, 'getClosestPeers', MockDhtRpc.throwGetClosestPeersError)
|
|
69
|
+
await expect(dhtPeer.getClosestPeers(clientPeerDescriptor.kademliaId))
|
|
70
|
+
.rejects.toThrow('Closest peers error')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
})
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { getMockPeers, MockDhtRpc } from '../utils/utils'
|
|
2
|
+
import { ProtoRpcClient, RpcCommunicator, RpcError, toProtoRpcClient } from '@streamr/proto-rpc'
|
|
3
|
+
import { DhtRpcServiceClient } from '../../src/proto/packages/dht/protos/DhtRpc.client'
|
|
4
|
+
import { generateId } from '../utils/utils'
|
|
5
|
+
import { ClosestPeersRequest, ClosestPeersResponse, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
6
|
+
import { wait } from '@streamr/utils'
|
|
7
|
+
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
8
|
+
import { DhtCallContext } from '../../src/rpc-protocol/DhtCallContext'
|
|
9
|
+
import { RpcMessage } from '../../src/proto/packages/proto-rpc/protos/ProtoRpc'
|
|
10
|
+
|
|
11
|
+
describe('DhtRpc', () => {
|
|
12
|
+
let rpcCommunicator1: RpcCommunicator
|
|
13
|
+
let rpcCommunicator2: RpcCommunicator
|
|
14
|
+
let client1: ProtoRpcClient<DhtRpcServiceClient>
|
|
15
|
+
let client2: ProtoRpcClient<DhtRpcServiceClient>
|
|
16
|
+
|
|
17
|
+
const peerDescriptor1: PeerDescriptor = {
|
|
18
|
+
kademliaId: generateId('peer1'),
|
|
19
|
+
type: 0
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const peerDescriptor2: PeerDescriptor = {
|
|
23
|
+
kademliaId: generateId('peer2'),
|
|
24
|
+
type: 0
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const outgoingListener2 = (message: RpcMessage, _requestId: string, _ucallContext?: DhtCallContext) => {
|
|
28
|
+
rpcCommunicator1.handleIncomingMessage(message)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
rpcCommunicator1 = new RpcCommunicator()
|
|
33
|
+
rpcCommunicator1.registerRpcMethod(ClosestPeersRequest, ClosestPeersResponse, 'getClosestPeers', MockDhtRpc.getClosestPeers)
|
|
34
|
+
|
|
35
|
+
rpcCommunicator2 = new RpcCommunicator()
|
|
36
|
+
rpcCommunicator2.registerRpcMethod(ClosestPeersRequest, ClosestPeersResponse, 'getClosestPeers', MockDhtRpc.getClosestPeers)
|
|
37
|
+
|
|
38
|
+
rpcCommunicator1.on('outgoingMessage', (message: RpcMessage, _requestId: string, _ucallContext?: DhtCallContext) => {
|
|
39
|
+
rpcCommunicator2.handleIncomingMessage(message)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
rpcCommunicator2.on('outgoingMessage', outgoingListener2)
|
|
43
|
+
|
|
44
|
+
client1 = toProtoRpcClient(new DhtRpcServiceClient(rpcCommunicator1.getRpcClientTransport()))
|
|
45
|
+
client2 = toProtoRpcClient(new DhtRpcServiceClient(rpcCommunicator1.getRpcClientTransport()))
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
afterEach(async () => {
|
|
49
|
+
await rpcCommunicator1.stop()
|
|
50
|
+
await rpcCommunicator2.stop()
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('Happy path', async () => {
|
|
54
|
+
const response1 = client1.getClosestPeers(
|
|
55
|
+
{ kademliaId: peerDescriptor1.kademliaId, requestId: '1' },
|
|
56
|
+
{
|
|
57
|
+
sourceDescriptor: peerDescriptor1,
|
|
58
|
+
targetDescriptor: peerDescriptor2,
|
|
59
|
+
}
|
|
60
|
+
)
|
|
61
|
+
const res1 = await response1
|
|
62
|
+
expect(res1.peers).toEqual(getMockPeers())
|
|
63
|
+
|
|
64
|
+
const response2 = client2.getClosestPeers(
|
|
65
|
+
{ kademliaId: peerDescriptor2.kademliaId, requestId: '1' },
|
|
66
|
+
{
|
|
67
|
+
sourceDescriptor: peerDescriptor2,
|
|
68
|
+
targetDescriptor: peerDescriptor1
|
|
69
|
+
}
|
|
70
|
+
)
|
|
71
|
+
const res2 = await response2
|
|
72
|
+
expect(res2.peers).toEqual(getMockPeers())
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('Default RPC timeout, client side', async () => {
|
|
76
|
+
rpcCommunicator2.off('outgoingMessage', outgoingListener2)
|
|
77
|
+
rpcCommunicator2.on('outgoingMessage', async (_message: RpcMessage, _requestId: string, _ucallContext?: DhtCallContext) => {
|
|
78
|
+
await wait(3000)
|
|
79
|
+
})
|
|
80
|
+
const response2 = client2.getClosestPeers(
|
|
81
|
+
{ kademliaId: peerDescriptor2.kademliaId, requestId: '1' },
|
|
82
|
+
{
|
|
83
|
+
sourceDescriptor: peerDescriptor2,
|
|
84
|
+
targetDescriptor: peerDescriptor1
|
|
85
|
+
}
|
|
86
|
+
)
|
|
87
|
+
await expect(response2).rejects.toEqual(
|
|
88
|
+
new RpcError.RpcTimeout('Rpc request timed out')
|
|
89
|
+
)
|
|
90
|
+
}, 15000)
|
|
91
|
+
|
|
92
|
+
it('Server side timeout', async () => {
|
|
93
|
+
let timeout: NodeJS.Timeout
|
|
94
|
+
|
|
95
|
+
function respondGetClosestPeersWithTimeout(_request: ClosestPeersRequest, _context: ServerCallContext): Promise<ClosestPeersResponse> {
|
|
96
|
+
const neighbors = getMockPeers()
|
|
97
|
+
const response: ClosestPeersResponse = {
|
|
98
|
+
peers: neighbors,
|
|
99
|
+
requestId: 'why am i still here'
|
|
100
|
+
}
|
|
101
|
+
return new Promise((resolve, _reject) => {
|
|
102
|
+
timeout = setTimeout(() => {
|
|
103
|
+
resolve(response)
|
|
104
|
+
}, 5000)
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
rpcCommunicator2.registerRpcMethod(ClosestPeersRequest, ClosestPeersResponse, 'getClosestPeers', respondGetClosestPeersWithTimeout)
|
|
109
|
+
const response = client2.getClosestPeers(
|
|
110
|
+
{ kademliaId: peerDescriptor2.kademliaId, requestId: '1' },
|
|
111
|
+
{
|
|
112
|
+
sourceDescriptor: peerDescriptor2,
|
|
113
|
+
targetDescriptor: peerDescriptor1
|
|
114
|
+
}
|
|
115
|
+
)
|
|
116
|
+
await expect(response).rejects.toEqual(
|
|
117
|
+
new RpcError.RpcTimeout('Server timed out on request')
|
|
118
|
+
)
|
|
119
|
+
clearTimeout(timeout!)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('Server responds with error on unknown method', async () => {
|
|
123
|
+
const response = client2.ping(
|
|
124
|
+
{ requestId: '1' },
|
|
125
|
+
{ targetDescriptor: peerDescriptor1 }
|
|
126
|
+
)
|
|
127
|
+
await expect(response).rejects.toEqual(
|
|
128
|
+
new RpcError.UnknownRpcMethod('Server does not implement method ping')
|
|
129
|
+
)
|
|
130
|
+
})
|
|
131
|
+
})
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { DhtNode } from '../../src/dht/DhtNode'
|
|
2
|
+
import { PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
3
|
+
import { createMockConnectionDhtNode } from '../utils/utils'
|
|
4
|
+
import { LatencyType, Simulator } from '../../src/connection/Simulator/Simulator'
|
|
5
|
+
|
|
6
|
+
describe('Mock connection Dht joining with latencies', () => {
|
|
7
|
+
let entryPoint: DhtNode
|
|
8
|
+
let nodes: DhtNode[]
|
|
9
|
+
let simulator: Simulator
|
|
10
|
+
let entrypointDescriptor: PeerDescriptor
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
nodes = []
|
|
14
|
+
simulator = new Simulator(LatencyType.RANDOM)
|
|
15
|
+
const entryPointId = '0'
|
|
16
|
+
entryPoint = await createMockConnectionDhtNode(entryPointId, simulator)
|
|
17
|
+
entrypointDescriptor = {
|
|
18
|
+
kademliaId: entryPoint.getNodeId().value,
|
|
19
|
+
type: 0,
|
|
20
|
+
nodeName: '0'
|
|
21
|
+
}
|
|
22
|
+
for (let i = 1; i < 100; i++) {
|
|
23
|
+
const nodeId = `${i}`
|
|
24
|
+
const node = await createMockConnectionDhtNode(nodeId, simulator)
|
|
25
|
+
nodes.push(node)
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
afterEach(async () => {
|
|
30
|
+
await Promise.all([
|
|
31
|
+
entryPoint.stop(),
|
|
32
|
+
...nodes.map((node) => node.stop())
|
|
33
|
+
])
|
|
34
|
+
simulator.stop()
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('Happy path', async () => {
|
|
38
|
+
await entryPoint.joinDht([entrypointDescriptor])
|
|
39
|
+
await Promise.all(nodes.map((node) => node.joinDht([entrypointDescriptor])))
|
|
40
|
+
nodes.forEach((node) => {
|
|
41
|
+
expect(node.getBucketSize()).toBeGreaterThanOrEqual(node.getK() - 2)
|
|
42
|
+
expect(node.getNeighborList().getSize()).toBeGreaterThanOrEqual(node.getK() - 2)
|
|
43
|
+
})
|
|
44
|
+
expect(entryPoint.getBucketSize()).toBeGreaterThanOrEqual(entryPoint.getK() - 2)
|
|
45
|
+
}, 60 * 1000)
|
|
46
|
+
})
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Simulator } from '../../src/connection/Simulator/Simulator'
|
|
2
|
+
import { DhtNode } from '../../src/dht/DhtNode'
|
|
3
|
+
import { PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
4
|
+
import { createMockConnectionDhtNode } from '../utils/utils'
|
|
5
|
+
|
|
6
|
+
describe('Mock IConnection DHT Joining', () => {
|
|
7
|
+
let entryPoint: DhtNode
|
|
8
|
+
let nodes: DhtNode[]
|
|
9
|
+
let entrypointDescriptor: PeerDescriptor
|
|
10
|
+
let simulator: Simulator
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
nodes = []
|
|
14
|
+
simulator = new Simulator()
|
|
15
|
+
const entryPointId = '0'
|
|
16
|
+
entryPoint = await createMockConnectionDhtNode(entryPointId, simulator)
|
|
17
|
+
entrypointDescriptor = {
|
|
18
|
+
kademliaId: entryPoint.getNodeId().value,
|
|
19
|
+
type: 0,
|
|
20
|
+
nodeName: '0'
|
|
21
|
+
}
|
|
22
|
+
for (let i = 1; i < 100; i++) {
|
|
23
|
+
const nodeId = `${i}`
|
|
24
|
+
const node = await createMockConnectionDhtNode(nodeId, simulator)
|
|
25
|
+
nodes.push(node)
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
afterEach(async () => {
|
|
30
|
+
await Promise.all([
|
|
31
|
+
entryPoint.stop(),
|
|
32
|
+
...nodes.map(async (node) => node.stop())
|
|
33
|
+
])
|
|
34
|
+
simulator.stop()
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('Happy path', async () => {
|
|
38
|
+
await entryPoint.joinDht([entrypointDescriptor])
|
|
39
|
+
await Promise.all(nodes.map((node) => node.joinDht([entrypointDescriptor])))
|
|
40
|
+
nodes.forEach((node) => {
|
|
41
|
+
expect(node.getBucketSize()).toBeGreaterThanOrEqual(node.getK() - 2)
|
|
42
|
+
expect(node.getNeighborList().getSize()).toBeGreaterThanOrEqual(node.getK() - 2)
|
|
43
|
+
})
|
|
44
|
+
expect(entryPoint.getBucketSize()).toBeGreaterThanOrEqual(entryPoint.getK() - 2)
|
|
45
|
+
}, 60000)
|
|
46
|
+
})
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { DhtNode } from '../../src/dht/DhtNode'
|
|
2
|
+
import { createMockConnectionDhtNode } from '../utils/utils'
|
|
3
|
+
import { LatencyType, Simulator } from '../../src/connection/Simulator/Simulator'
|
|
4
|
+
import { PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
5
|
+
import { getRandomRegion } from '../../src/connection/Simulator/pings'
|
|
6
|
+
|
|
7
|
+
describe('Mock connection Dht joining with real latencies', () => {
|
|
8
|
+
let entryPoint: DhtNode
|
|
9
|
+
let nodes: DhtNode[]
|
|
10
|
+
let simulator: Simulator
|
|
11
|
+
let entrypointDescriptor: PeerDescriptor
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
nodes = []
|
|
15
|
+
simulator = new Simulator(LatencyType.REAL)
|
|
16
|
+
const entryPointId = '0'
|
|
17
|
+
entryPoint = await createMockConnectionDhtNode(entryPointId, simulator)
|
|
18
|
+
entrypointDescriptor = {
|
|
19
|
+
kademliaId: entryPoint.getNodeId().value,
|
|
20
|
+
type: 0,
|
|
21
|
+
region: getRandomRegion()
|
|
22
|
+
}
|
|
23
|
+
for (let i = 1; i < 100; i++) {
|
|
24
|
+
const nodeId = `${i}`
|
|
25
|
+
const node = await createMockConnectionDhtNode(nodeId, simulator)
|
|
26
|
+
nodes.push(node)
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
afterEach(async () => {
|
|
31
|
+
await Promise.all([
|
|
32
|
+
entryPoint.stop(),
|
|
33
|
+
...nodes.map((node) => node.stop())
|
|
34
|
+
])
|
|
35
|
+
simulator.stop()
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('Happy path', async () => {
|
|
39
|
+
await entryPoint.joinDht([entrypointDescriptor])
|
|
40
|
+
await Promise.all(nodes.map((node) => node.joinDht([entrypointDescriptor])))
|
|
41
|
+
nodes.forEach((node) => {
|
|
42
|
+
expect(node.getBucketSize()).toBeGreaterThanOrEqual(node.getK() - 3)
|
|
43
|
+
expect(node.getNeighborList().getSize()).toBeGreaterThanOrEqual(node.getK() - 3)
|
|
44
|
+
})
|
|
45
|
+
expect(entryPoint.getBucketSize()).toBeGreaterThanOrEqual(entryPoint.getK())
|
|
46
|
+
}, 60 * 1000)
|
|
47
|
+
})
|