@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,40 @@
|
|
|
1
|
+
import { getAddressFromIceCandidate, isPrivateIPv4 } from '../../src/helpers/AddressTools'
|
|
2
|
+
|
|
3
|
+
describe('getAddressFromIceCandidate', () => {
|
|
4
|
+
it('extract IPv4 address from ICE host candidate', () => {
|
|
5
|
+
expect(getAddressFromIceCandidate('candidate:1 1 udp 2122262783 203.0.113.180 4444 typ host'))
|
|
6
|
+
.toEqual('203.0.113.180')
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('extract IPv4 address from ICE server reflexive candidate', () => {
|
|
10
|
+
expect(getAddressFromIceCandidate('candidate:1 1 udp 2122262783 198.51.100.130 4445 typ srflx raddr 0.0.0.0 rport 0'))
|
|
11
|
+
.toEqual('198.51.100.130')
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('extract IPv6 address from ICE candidate', () => {
|
|
15
|
+
expect(getAddressFromIceCandidate('candidate:1 1 udp 3756231458 2001:db8::4125:918c:4402:cc54 6666 typ host'))
|
|
16
|
+
.toEqual('2001:db8::4125:918c:4402:cc54')
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('fail on mDNS ICE candidate', () => {
|
|
20
|
+
expect(getAddressFromIceCandidate('candidate:1 1 udp 2122296321 9b36eaac-bb2e-49bb-bb78-21c41c499900.local 7000 typ host')).toBeUndefined()
|
|
21
|
+
})
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
describe('isPrivateIPv4', () => {
|
|
25
|
+
it('return true for range 10.0.0.0/8', () => {
|
|
26
|
+
expect(isPrivateIPv4('10.11.12.13')).toBe(true)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('return true for range 172.16.0.0/12', () => {
|
|
30
|
+
expect(isPrivateIPv4('172.16.130.131')).toBe(true)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('return true for range 192.168.0.0/16', () => {
|
|
34
|
+
expect(isPrivateIPv4('192.168.1.1')).toBe(true)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('return false for a public address', () => {
|
|
38
|
+
expect(isPrivateIPv4('203.0.113.181')).toBe(false)
|
|
39
|
+
})
|
|
40
|
+
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DuplicateDetector } from '../../src/dht/routing/DuplicateDetector'
|
|
2
|
+
|
|
3
|
+
describe('Duplicate Detector', () => {
|
|
4
|
+
let detector: DuplicateDetector
|
|
5
|
+
const maxLimit = 10
|
|
6
|
+
const senderId = 'sender'
|
|
7
|
+
beforeEach(async () => {
|
|
8
|
+
detector = new DuplicateDetector(maxLimit, 100)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('detects duplicates', async () => {
|
|
12
|
+
detector.add('test', senderId)
|
|
13
|
+
expect(detector.size()).toEqual(1)
|
|
14
|
+
expect(detector.isMostLikelyDuplicate('test')).toEqual(true)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('resets on resetLimit', () => {
|
|
18
|
+
for (let i = 0; i < maxLimit; i++) {
|
|
19
|
+
detector.add(`test${i}`, senderId)
|
|
20
|
+
}
|
|
21
|
+
for (let i = 0; i < maxLimit; i++) {
|
|
22
|
+
expect(detector.isMostLikelyDuplicate(`test${i}`)).toEqual(true)
|
|
23
|
+
}
|
|
24
|
+
detector.add('test10', senderId)
|
|
25
|
+
expect(detector.size()).toEqual(10)
|
|
26
|
+
expect(detector.isMostLikelyDuplicate('test0')).toEqual(false)
|
|
27
|
+
expect(detector.isMostLikelyDuplicate('test10')).toEqual(true)
|
|
28
|
+
})
|
|
29
|
+
})
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Any } from '../../src/proto/google/protobuf/any'
|
|
2
|
+
import { NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
3
|
+
import {
|
|
4
|
+
isSamePeerDescriptor,
|
|
5
|
+
keyFromPeerDescriptor,
|
|
6
|
+
peerIdFromPeerDescriptor
|
|
7
|
+
} from '../../src/helpers/peerIdFromPeerDescriptor'
|
|
8
|
+
import { LocalDataStore } from '../../src/dht/store/LocalDataStore'
|
|
9
|
+
import { wait } from '@streamr/utils'
|
|
10
|
+
import { Timestamp } from '../../src/proto/google/protobuf/timestamp'
|
|
11
|
+
|
|
12
|
+
describe('LocalDataStore', () => {
|
|
13
|
+
let localDataStore: LocalDataStore
|
|
14
|
+
const storer1: PeerDescriptor = {
|
|
15
|
+
kademliaId: new Uint8Array([1, 2, 3]),
|
|
16
|
+
type: NodeType.NODEJS,
|
|
17
|
+
nodeName: 'storer1'
|
|
18
|
+
}
|
|
19
|
+
const storer2: PeerDescriptor = {
|
|
20
|
+
kademliaId: new Uint8Array([3, 2, 1]),
|
|
21
|
+
type: NodeType.NODEJS,
|
|
22
|
+
nodeName: 'storer2'
|
|
23
|
+
}
|
|
24
|
+
const data1 = Any.pack(storer1, PeerDescriptor)
|
|
25
|
+
const data2 = Any.pack(storer2, PeerDescriptor)
|
|
26
|
+
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
localDataStore = new LocalDataStore()
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
localDataStore.clear()
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('can store', () => {
|
|
36
|
+
const dataKey = peerIdFromPeerDescriptor(storer1)
|
|
37
|
+
localDataStore.storeEntry({ storer: storer1, kademliaId: dataKey.value, data: data1,
|
|
38
|
+
ttl: 10000, stale: false, deleted: false, storerTime: Timestamp.now() })
|
|
39
|
+
const fetchedData = localDataStore.getEntry(dataKey)
|
|
40
|
+
fetchedData!.forEach((entry) => {
|
|
41
|
+
const fetchedDescriptor = Any.unpack(entry.data!, PeerDescriptor)
|
|
42
|
+
expect(isSamePeerDescriptor(fetchedDescriptor, storer1)).toBeTrue()
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('multiple storers behind one key', () => {
|
|
47
|
+
const dataKey = peerIdFromPeerDescriptor(storer1)
|
|
48
|
+
localDataStore.storeEntry({ storer: storer1, kademliaId: dataKey.value, data: data1,
|
|
49
|
+
ttl: 10000, stale: false, deleted: false, storerTime: Timestamp.now() })
|
|
50
|
+
localDataStore.storeEntry({ storer: storer2, kademliaId: dataKey.value,
|
|
51
|
+
data: data1, ttl: 10000, stale: false, deleted: false, storerTime: Timestamp.now() })
|
|
52
|
+
const fetchedData = localDataStore.getEntry(dataKey)
|
|
53
|
+
fetchedData!.forEach((entry) => {
|
|
54
|
+
const fetchedDescriptor = Any.unpack(entry.data!, PeerDescriptor)
|
|
55
|
+
expect(isSamePeerDescriptor(fetchedDescriptor, storer1)).toBeTrue()
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('can remove data entries', () => {
|
|
60
|
+
const dataKey = peerIdFromPeerDescriptor(storer1)
|
|
61
|
+
localDataStore.storeEntry({ storer: storer1, kademliaId: dataKey.value, data: data1,
|
|
62
|
+
ttl: 10000, stale: false, deleted: false, storerTime: Timestamp.now() })
|
|
63
|
+
localDataStore.storeEntry({ storer: storer2, kademliaId: dataKey.value, data: data2,
|
|
64
|
+
ttl: 10000, stale: false, deleted: false, storerTime: Timestamp.now() })
|
|
65
|
+
localDataStore.deleteEntry(dataKey, storer1)
|
|
66
|
+
const fetchedData = localDataStore.getEntry(dataKey)
|
|
67
|
+
fetchedData!.forEach((entry) => {
|
|
68
|
+
const fetchedDescriptor = Any.unpack(entry.data!, PeerDescriptor)
|
|
69
|
+
expect(isSamePeerDescriptor(fetchedDescriptor, storer2)).toBeTrue()
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('can remove all data entries', () => {
|
|
74
|
+
const dataKey = peerIdFromPeerDescriptor(storer1)
|
|
75
|
+
localDataStore.storeEntry({ storer: storer1, kademliaId: dataKey.value, data: data1,
|
|
76
|
+
ttl: 10000, stale: false, deleted: false, storerTime: Timestamp.now() })
|
|
77
|
+
localDataStore.storeEntry({ storer: storer2, kademliaId: dataKey.value, data: data2,
|
|
78
|
+
ttl: 10000, stale: false, deleted: false, storerTime: Timestamp.now() })
|
|
79
|
+
localDataStore.deleteEntry(dataKey, storer1)
|
|
80
|
+
localDataStore.deleteEntry(dataKey, storer2)
|
|
81
|
+
const fetchedData = localDataStore.getEntry(dataKey)
|
|
82
|
+
expect(fetchedData.size).toBe(0)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('data is deleted after TTL', async () => {
|
|
86
|
+
const dataKey = peerIdFromPeerDescriptor(storer1)
|
|
87
|
+
localDataStore.storeEntry({ storer: storer1, kademliaId: dataKey.value, data: data1,
|
|
88
|
+
ttl: 1000, stale: false, deleted: false, storerTime: Timestamp.now() })
|
|
89
|
+
const intitialStore = localDataStore.getEntry(dataKey)
|
|
90
|
+
expect(intitialStore.size).toBe(1)
|
|
91
|
+
await wait(1100)
|
|
92
|
+
const fetchedData = localDataStore.getEntry(dataKey)
|
|
93
|
+
expect(fetchedData.size).toBe(0)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('can mark data as deleted', () => {
|
|
97
|
+
const dataKey = peerIdFromPeerDescriptor(storer1)
|
|
98
|
+
localDataStore.storeEntry({ storer: storer1, kademliaId: dataKey.value, data: data1,
|
|
99
|
+
ttl: 10000, stale: false, deleted: false, storerTime: Timestamp.now() })
|
|
100
|
+
const notDeletedData = localDataStore.getEntry(dataKey)
|
|
101
|
+
expect(notDeletedData.get(keyFromPeerDescriptor(storer1))!.deleted).toBeFalse()
|
|
102
|
+
localDataStore.markAsDeleted(dataKey.value, peerIdFromPeerDescriptor(storer1))
|
|
103
|
+
const deletedData = localDataStore.getEntry(dataKey)
|
|
104
|
+
expect(deletedData.get(keyFromPeerDescriptor(storer1))!.deleted).toBeTrue()
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
})
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { PeerID } from '../../src/helpers/PeerID'
|
|
2
|
+
|
|
3
|
+
describe('PeerID', () => {
|
|
4
|
+
|
|
5
|
+
it('Conversions between input formats work', async () => {
|
|
6
|
+
const stringId = '123'
|
|
7
|
+
const id1 = PeerID.fromString(stringId)
|
|
8
|
+
const id2 = PeerID.fromValue(id1.value)
|
|
9
|
+
|
|
10
|
+
expect(id1.equals(id2)).toBeTruthy()
|
|
11
|
+
expect(id1.toString()).toEqual(id2.toString())
|
|
12
|
+
expect(stringId).toEqual(id2.toString())
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('peerKey', () => {
|
|
16
|
+
const peerIdFromString = PeerID.fromString('peerId')
|
|
17
|
+
const peerKey = peerIdFromString.toKey()
|
|
18
|
+
const peerIdFromKey = PeerID.fromKey(peerKey)
|
|
19
|
+
expect(peerIdFromString.equals(peerIdFromKey)).toEqual(true)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
import { ProtobufMessage } from '../../src/protobufclasses/ProtobufMessage'
|
|
3
|
+
import { Message, RpcMessage } from '../../src/proto/DhtRpc'
|
|
4
|
+
import { v4 } from 'uuid'
|
|
5
|
+
import { MessageType } from '@protobuf-ts/runtime'
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
describe('ProtobufMessage', () => {
|
|
9
|
+
|
|
10
|
+
it('can parse and print a Message', async () => {
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
const message: Message = {
|
|
14
|
+
messageId: v4(),
|
|
15
|
+
messageType:
|
|
16
|
+
}
|
|
17
|
+
const data = Message.toBinary(message)
|
|
18
|
+
ProtobufMessage.fromBinary(data)
|
|
19
|
+
*/
|
|
20
|
+
})
|
|
21
|
+
})
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { RandomContactList } from '../../src/dht/contact/RandomContactList'
|
|
2
|
+
import type { ServiceInfo, MethodInfo } from '@protobuf-ts/runtime-rpc'
|
|
3
|
+
import { PeerID } from '../../src/helpers/PeerID'
|
|
4
|
+
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
5
|
+
import { IDhtRpcServiceClient } from '../../src/proto/packages/dht/protos/DhtRpc.client'
|
|
6
|
+
import { LeaveNotice, NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
7
|
+
import type { FindDataRequest, FindDataResponse, PingResponse } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
8
|
+
import type { PingRequest } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
9
|
+
import type { ClosestPeersResponse } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
10
|
+
import type { ClosestPeersRequest } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
11
|
+
import { UnaryCall } from '@protobuf-ts/runtime-rpc'
|
|
12
|
+
import type { RpcOptions } from '@protobuf-ts/runtime-rpc'
|
|
13
|
+
import { DhtPeer } from '../../src/dht/DhtPeer'
|
|
14
|
+
import { IMessageType } from '@protobuf-ts/runtime'
|
|
15
|
+
import { Empty } from '../../src/proto/google/protobuf/empty'
|
|
16
|
+
|
|
17
|
+
class MockRpcClient implements IDhtRpcServiceClient, ServiceInfo {
|
|
18
|
+
typeName = 'MockRpcClient'
|
|
19
|
+
methods: MethodInfo<any, any> [] = [
|
|
20
|
+
{ name: 'getClosestPeers', O: {} as IMessageType<ClosestPeersResponse> } as MethodInfo<any, any>,
|
|
21
|
+
{ name: 'ping', O: {} as IMessageType<PingResponse> } as MethodInfo<any, any>,
|
|
22
|
+
{ name: 'findData', O: {} as IMessageType<FindDataRequest> } as MethodInfo<any, any>,
|
|
23
|
+
{ name: 'leaveNotice', O: {} as IMessageType<Empty> } as MethodInfo<any, any>
|
|
24
|
+
]
|
|
25
|
+
options = {}
|
|
26
|
+
|
|
27
|
+
// eslint-disable-next-line class-methods-use-this
|
|
28
|
+
getClosestPeers(_input: ClosestPeersRequest, _options?: RpcOptions): UnaryCall<ClosestPeersRequest, ClosestPeersResponse> {
|
|
29
|
+
return {} as UnaryCall<ClosestPeersRequest, ClosestPeersResponse>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// eslint-disable-next-line class-methods-use-this
|
|
33
|
+
ping(_input: PingRequest, _options?: RpcOptions): UnaryCall <PingRequest, PingResponse> {
|
|
34
|
+
return {} as UnaryCall<PingRequest, PingResponse>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// eslint-disable-next-line class-methods-use-this
|
|
38
|
+
findData(_input: FindDataRequest, _options?: RpcOptions): UnaryCall<FindDataRequest, FindDataResponse> {
|
|
39
|
+
return {} as UnaryCall<FindDataRequest, FindDataResponse>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// eslint-disable-next-line class-methods-use-this
|
|
43
|
+
leaveNotice(_input: LeaveNotice, _options?: RpcOptions): UnaryCall<LeaveNotice, Empty> {
|
|
44
|
+
return {} as UnaryCall<LeaveNotice, Empty>
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe('RandomContactList', () => {
|
|
49
|
+
const serviceId = 'random'
|
|
50
|
+
const id0 = PeerID.fromValue(Buffer.from([0, 0, 0, 0]))
|
|
51
|
+
const id1 = PeerID.fromValue(Buffer.from([0, 0, 0, 1]))
|
|
52
|
+
const id2 = PeerID.fromValue(Buffer.from([0, 0, 0, 2]))
|
|
53
|
+
const id3 = PeerID.fromValue(Buffer.from([0, 0, 0, 3]))
|
|
54
|
+
const id4 = PeerID.fromValue(Buffer.from([0, 0, 0, 4]))
|
|
55
|
+
|
|
56
|
+
const descriptor0: PeerDescriptor = { kademliaId: id0.value, type: NodeType.NODEJS }
|
|
57
|
+
const descriptor1: PeerDescriptor = { kademliaId: id1.value, type: NodeType.NODEJS }
|
|
58
|
+
const descriptor2: PeerDescriptor = { kademliaId: id2.value, type: NodeType.NODEJS }
|
|
59
|
+
const descriptor3: PeerDescriptor = { kademliaId: id3.value, type: NodeType.NODEJS }
|
|
60
|
+
const descriptor4: PeerDescriptor = { kademliaId: id4.value, type: NodeType.NODEJS }
|
|
61
|
+
|
|
62
|
+
const peer1 = new DhtPeer(descriptor0, descriptor1, toProtoRpcClient(new MockRpcClient()), serviceId)
|
|
63
|
+
const peer2 = new DhtPeer(descriptor0, descriptor2, toProtoRpcClient(new MockRpcClient()), serviceId)
|
|
64
|
+
const peer3 = new DhtPeer(descriptor0, descriptor3, toProtoRpcClient(new MockRpcClient()), serviceId)
|
|
65
|
+
const peer4 = new DhtPeer(descriptor0, descriptor4, toProtoRpcClient(new MockRpcClient()), serviceId)
|
|
66
|
+
|
|
67
|
+
it('adds contacts correctly', () => {
|
|
68
|
+
const list = new RandomContactList(id0, 5, 1)
|
|
69
|
+
list.addContact(peer1)
|
|
70
|
+
list.addContact(peer2)
|
|
71
|
+
list.addContact(peer3)
|
|
72
|
+
list.addContact(peer3)
|
|
73
|
+
list.addContact(peer4)
|
|
74
|
+
list.addContact(peer4)
|
|
75
|
+
expect(list.getSize()).toEqual(4)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('removes contacts correctly', () => {
|
|
79
|
+
const list = new RandomContactList(id0, 5, 1)
|
|
80
|
+
list.addContact(peer1)
|
|
81
|
+
list.addContact(peer2)
|
|
82
|
+
list.removeContact(id2)
|
|
83
|
+
expect(list.getContact(id1)).toBeTruthy()
|
|
84
|
+
expect(list.getSize()).toEqual(1)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
})
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FindMode,
|
|
3
|
+
Message,
|
|
4
|
+
MessageType,
|
|
5
|
+
PeerDescriptor,
|
|
6
|
+
RouteMessageWrapper
|
|
7
|
+
} from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
8
|
+
import { PeerID, PeerIDKey } from '../../src/helpers/PeerID'
|
|
9
|
+
import {
|
|
10
|
+
createMockRoutingRpcCommunicator,
|
|
11
|
+
createWrappedClosestPeersRequest,
|
|
12
|
+
createRecursiveFindRequest
|
|
13
|
+
} from '../utils/utils'
|
|
14
|
+
import { RecursiveFinder } from '../../src/dht/find/RecursiveFinder'
|
|
15
|
+
import { DhtPeer } from '../../src/dht/DhtPeer'
|
|
16
|
+
import { LocalDataStore } from '../../src/dht/store/LocalDataStore'
|
|
17
|
+
import { v4 } from 'uuid'
|
|
18
|
+
import { MockRouter } from '../utils/mock/Router'
|
|
19
|
+
import { MockTransport } from '../utils/mock/Transport'
|
|
20
|
+
import { isSamePeerDescriptor } from '../../src/helpers/peerIdFromPeerDescriptor'
|
|
21
|
+
|
|
22
|
+
describe('RecursiveFinder', () => {
|
|
23
|
+
|
|
24
|
+
let recursiveFinder: RecursiveFinder
|
|
25
|
+
let connections: Map<PeerIDKey, DhtPeer>
|
|
26
|
+
|
|
27
|
+
const peerId1 = PeerID.fromString('peerid')
|
|
28
|
+
const peerDescriptor1: PeerDescriptor = {
|
|
29
|
+
kademliaId: peerId1.value,
|
|
30
|
+
type: 0,
|
|
31
|
+
nodeName: 'peerid'
|
|
32
|
+
}
|
|
33
|
+
const peerDescriptor2: PeerDescriptor = {
|
|
34
|
+
kademliaId: PeerID.fromString('destination').value,
|
|
35
|
+
type: 0,
|
|
36
|
+
nodeName: 'destination'
|
|
37
|
+
}
|
|
38
|
+
const recursiveFindRequest = createRecursiveFindRequest(FindMode.NODE)
|
|
39
|
+
const message: Message = {
|
|
40
|
+
serviceId: 'unknown',
|
|
41
|
+
messageId: v4(),
|
|
42
|
+
messageType: MessageType.RPC,
|
|
43
|
+
body: {
|
|
44
|
+
oneofKind: 'recursiveFindRequest',
|
|
45
|
+
recursiveFindRequest
|
|
46
|
+
},
|
|
47
|
+
sourceDescriptor: peerDescriptor1,
|
|
48
|
+
targetDescriptor: peerDescriptor2
|
|
49
|
+
}
|
|
50
|
+
const routedMessage: RouteMessageWrapper = {
|
|
51
|
+
message,
|
|
52
|
+
requestId: 'REQ',
|
|
53
|
+
routingPath: [],
|
|
54
|
+
reachableThrough: [],
|
|
55
|
+
destinationPeer: peerDescriptor1,
|
|
56
|
+
sourcePeer: peerDescriptor2
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
beforeEach(() => {
|
|
60
|
+
connections = new Map()
|
|
61
|
+
recursiveFinder = new RecursiveFinder({
|
|
62
|
+
ownPeerDescriptor: peerDescriptor1,
|
|
63
|
+
ownPeerId: peerId1,
|
|
64
|
+
router: new MockRouter(),
|
|
65
|
+
connections,
|
|
66
|
+
serviceId: 'RecursiveFinder',
|
|
67
|
+
localDataStore: new LocalDataStore(),
|
|
68
|
+
sessionTransport: new MockTransport(),
|
|
69
|
+
addContact: (_contact, _setActive) => {},
|
|
70
|
+
isPeerCloserToIdThanSelf: (_peer1, _compareToId) => true,
|
|
71
|
+
rpcCommunicator: createMockRoutingRpcCommunicator()
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
afterEach(() => {
|
|
76
|
+
recursiveFinder.stop()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('RecursiveFinder server', async () => {
|
|
80
|
+
const res = await recursiveFinder.findRecursively(routedMessage)
|
|
81
|
+
expect(res.error).toEqual('')
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('startRecursiveFind with mode Node returns self if no peers', async () => {
|
|
85
|
+
const res = await recursiveFinder.startRecursiveFind(PeerID.fromString('find').value)
|
|
86
|
+
expect(isSamePeerDescriptor(res.closestNodes[0], peerDescriptor1)).toEqual(true)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('RecursiveFinder server throws if payload is not recursiveFindRequest', async () => {
|
|
90
|
+
const rpcWrapper = createWrappedClosestPeersRequest(peerDescriptor1, peerDescriptor2)
|
|
91
|
+
const badMessage: Message = {
|
|
92
|
+
serviceId: 'unknown',
|
|
93
|
+
messageId: v4(),
|
|
94
|
+
messageType: MessageType.RPC,
|
|
95
|
+
body: {
|
|
96
|
+
oneofKind: 'rpcMessage',
|
|
97
|
+
rpcMessage: rpcWrapper
|
|
98
|
+
},
|
|
99
|
+
sourceDescriptor: peerDescriptor1,
|
|
100
|
+
targetDescriptor: peerDescriptor2
|
|
101
|
+
}
|
|
102
|
+
await expect(recursiveFinder.findRecursively({
|
|
103
|
+
message: badMessage,
|
|
104
|
+
requestId: 'REQ',
|
|
105
|
+
routingPath: [],
|
|
106
|
+
reachableThrough: [],
|
|
107
|
+
destinationPeer: peerDescriptor1,
|
|
108
|
+
sourcePeer: peerDescriptor2
|
|
109
|
+
})).rejects.toThrow()
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
})
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Router } from '../../src/dht/routing/Router'
|
|
2
|
+
import { Message, MessageType, PeerDescriptor, RouteMessageWrapper } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
3
|
+
import { PeerID, PeerIDKey } from '../../src/helpers/PeerID'
|
|
4
|
+
import { DhtPeer } from '../../src/dht/DhtPeer'
|
|
5
|
+
import { createWrappedClosestPeersRequest, createMockRoutingRpcCommunicator } from '../utils/utils'
|
|
6
|
+
import { v4 } from 'uuid'
|
|
7
|
+
import { keyFromPeerDescriptor } from '../../src/helpers/peerIdFromPeerDescriptor'
|
|
8
|
+
|
|
9
|
+
describe('Router', () => {
|
|
10
|
+
let router: Router
|
|
11
|
+
|
|
12
|
+
const peerId = PeerID.fromString('router')
|
|
13
|
+
const peerDescriptor1: PeerDescriptor = {
|
|
14
|
+
kademliaId: peerId.value,
|
|
15
|
+
type: 0,
|
|
16
|
+
nodeName: 'router'
|
|
17
|
+
}
|
|
18
|
+
const peerDescriptor2: PeerDescriptor = {
|
|
19
|
+
kademliaId: PeerID.fromString('destination').value,
|
|
20
|
+
type: 0,
|
|
21
|
+
}
|
|
22
|
+
const rpcWrapper = createWrappedClosestPeersRequest(peerDescriptor1, peerDescriptor2)
|
|
23
|
+
const message: Message = {
|
|
24
|
+
serviceId: 'unknown',
|
|
25
|
+
messageId: v4(),
|
|
26
|
+
messageType: MessageType.RPC,
|
|
27
|
+
body: {
|
|
28
|
+
oneofKind: 'rpcMessage',
|
|
29
|
+
rpcMessage: rpcWrapper
|
|
30
|
+
},
|
|
31
|
+
sourceDescriptor: peerDescriptor1,
|
|
32
|
+
targetDescriptor: peerDescriptor2
|
|
33
|
+
}
|
|
34
|
+
const routedMessage: RouteMessageWrapper = {
|
|
35
|
+
message,
|
|
36
|
+
requestId: 'REQ',
|
|
37
|
+
routingPath: [],
|
|
38
|
+
reachableThrough: [],
|
|
39
|
+
destinationPeer: peerDescriptor1,
|
|
40
|
+
sourcePeer: peerDescriptor2
|
|
41
|
+
}
|
|
42
|
+
let connections: Map<PeerIDKey, DhtPeer>
|
|
43
|
+
const mockRpcCommunicator = createMockRoutingRpcCommunicator()
|
|
44
|
+
|
|
45
|
+
const createMockDhtPeer = (destination: PeerDescriptor): DhtPeer => {
|
|
46
|
+
return new DhtPeer(peerDescriptor1, destination, {} as any, 'router')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
beforeEach(() => {
|
|
50
|
+
connections = new Map()
|
|
51
|
+
router = new Router({
|
|
52
|
+
ownPeerDescriptor: peerDescriptor1,
|
|
53
|
+
ownPeerId: peerId,
|
|
54
|
+
rpcCommunicator: mockRpcCommunicator,
|
|
55
|
+
addContact: (_contact) => {},
|
|
56
|
+
serviceId: 'router',
|
|
57
|
+
connections
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
afterEach(() => {
|
|
62
|
+
router.stop()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('doRouteMessage without connections', () => {
|
|
66
|
+
const ack = router.doRouteMessage({
|
|
67
|
+
message,
|
|
68
|
+
destinationPeer: peerDescriptor2,
|
|
69
|
+
requestId: v4(),
|
|
70
|
+
sourcePeer: peerDescriptor1,
|
|
71
|
+
reachableThrough: [],
|
|
72
|
+
routingPath: []
|
|
73
|
+
})
|
|
74
|
+
expect(ack.error).toEqual('No routing candidates found')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('doRouteMessage with connections', () => {
|
|
78
|
+
connections.set(PeerID.fromString('test').toKey(), createMockDhtPeer(peerDescriptor2))
|
|
79
|
+
const ack = router.doRouteMessage({
|
|
80
|
+
message,
|
|
81
|
+
destinationPeer: peerDescriptor2,
|
|
82
|
+
requestId: v4(),
|
|
83
|
+
sourcePeer: peerDescriptor1,
|
|
84
|
+
reachableThrough: [],
|
|
85
|
+
routingPath: []
|
|
86
|
+
})
|
|
87
|
+
expect(ack.error).toEqual('')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('route server is destination without connections', async () => {
|
|
91
|
+
const ack = await router.routeMessage(routedMessage, {} as any)
|
|
92
|
+
expect(ack.error).toEqual('')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('route server with connections', async () => {
|
|
96
|
+
connections.set(PeerID.fromString('test').toKey(), createMockDhtPeer(peerDescriptor2))
|
|
97
|
+
const ack = await router.routeMessage(routedMessage, {} as any)
|
|
98
|
+
expect(ack.error).toEqual('')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('route server on duplicate message', async () => {
|
|
102
|
+
router.addToDuplicateDetector(routedMessage.requestId, keyFromPeerDescriptor(peerDescriptor2))
|
|
103
|
+
const ack = await router.routeMessage(routedMessage, {} as any)
|
|
104
|
+
expect(ack.error).toEqual('message given to routeMessage() service is likely a duplicate')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('forward server no connections', async () => {
|
|
108
|
+
const ack = await router.forwardMessage(routedMessage, {} as any)
|
|
109
|
+
expect(ack.error).toEqual('No routing candidates found')
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('forward server with connections', async () => {
|
|
113
|
+
connections.set(PeerID.fromString('test').toKey(), createMockDhtPeer(peerDescriptor2))
|
|
114
|
+
const ack = await router.forwardMessage(routedMessage, {} as any)
|
|
115
|
+
expect(ack.error).toEqual('')
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('forward server on duplicate message', async () => {
|
|
119
|
+
router.addToDuplicateDetector(routedMessage.requestId, keyFromPeerDescriptor(peerDescriptor2))
|
|
120
|
+
const ack = await router.forwardMessage(routedMessage, {} as any)
|
|
121
|
+
expect(ack.error).toEqual('message given to forwardMessage() service is likely a duplicate')
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
})
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { SortedContactList } from '../../src/dht/contact/SortedContactList'
|
|
2
|
+
import type { ServiceInfo, MethodInfo } from '@protobuf-ts/runtime-rpc'
|
|
3
|
+
import { PeerID } from '../../src/helpers/PeerID'
|
|
4
|
+
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
5
|
+
import { IDhtRpcServiceClient } from '../../src/proto/packages/dht/protos/DhtRpc.client'
|
|
6
|
+
import { LeaveNotice, NodeType, PeerDescriptor, RouteMessageAck } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
7
|
+
import type { FindDataRequest, FindDataResponse, PingResponse } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
8
|
+
import type { PingRequest } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
9
|
+
import type { ClosestPeersResponse } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
10
|
+
import type { ClosestPeersRequest } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
11
|
+
import { UnaryCall } from '@protobuf-ts/runtime-rpc'
|
|
12
|
+
import type { RpcOptions } from '@protobuf-ts/runtime-rpc'
|
|
13
|
+
import { DhtPeer } from '../../src/dht/DhtPeer'
|
|
14
|
+
import { IMessageType } from '@protobuf-ts/runtime'
|
|
15
|
+
import { Empty } from '../../src/proto/google/protobuf/empty'
|
|
16
|
+
|
|
17
|
+
/* eslint-disable class-methods-use-this */
|
|
18
|
+
class MockRpcClient implements IDhtRpcServiceClient, ServiceInfo {
|
|
19
|
+
typeName = 'MockRpcClient'
|
|
20
|
+
methods: MethodInfo<any, any> [] = [
|
|
21
|
+
{ name: 'getClosestPeers', O: {} as IMessageType<ClosestPeersResponse> } as MethodInfo<any, any>,
|
|
22
|
+
{ name: 'ping', O: {} as IMessageType<PingResponse> } as MethodInfo<any, any>,
|
|
23
|
+
{ name: 'findData', O: {} as IMessageType<RouteMessageAck> } as MethodInfo<any, any>,
|
|
24
|
+
{ name: 'leaveNotice', O: {} as IMessageType<Empty> } as MethodInfo<any, any>
|
|
25
|
+
]
|
|
26
|
+
options = {}
|
|
27
|
+
getClosestPeers(_input: ClosestPeersRequest, _options?: RpcOptions): UnaryCall<ClosestPeersRequest, ClosestPeersResponse> {
|
|
28
|
+
return {} as UnaryCall<ClosestPeersRequest, ClosestPeersResponse>
|
|
29
|
+
}
|
|
30
|
+
ping(_input: PingRequest, _options?: RpcOptions): UnaryCall <PingRequest, PingResponse> {
|
|
31
|
+
return {} as UnaryCall<PingRequest, PingResponse>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// eslint-disable-next-line class-methods-use-this
|
|
35
|
+
findData(_input: FindDataRequest, _options?: RpcOptions): UnaryCall<FindDataRequest, FindDataResponse> {
|
|
36
|
+
return {} as UnaryCall<FindDataRequest, FindDataResponse>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
leaveNotice(_input: LeaveNotice, _options?: RpcOptions): UnaryCall<LeaveNotice, Empty> {
|
|
40
|
+
return {} as UnaryCall<LeaveNotice, Empty>
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
describe('SortedContactList', () => {
|
|
45
|
+
const serviceId = 'sorted'
|
|
46
|
+
const id0 = PeerID.fromValue(Buffer.from([0, 0, 0, 0]))
|
|
47
|
+
const id1 = PeerID.fromValue(Buffer.from([0, 0, 0, 1]))
|
|
48
|
+
const id2 = PeerID.fromValue(Buffer.from([0, 0, 0, 2]))
|
|
49
|
+
const id3 = PeerID.fromValue(Buffer.from([0, 0, 0, 3]))
|
|
50
|
+
const id4 = PeerID.fromValue(Buffer.from([0, 0, 0, 4]))
|
|
51
|
+
|
|
52
|
+
const descriptor0: PeerDescriptor = { kademliaId: id0.value, type: NodeType.NODEJS }
|
|
53
|
+
const descriptor1: PeerDescriptor = { kademliaId: id1.value, type: NodeType.NODEJS }
|
|
54
|
+
const descriptor2: PeerDescriptor = { kademliaId: id2.value, type: NodeType.NODEJS }
|
|
55
|
+
const descriptor3: PeerDescriptor = { kademliaId: id3.value, type: NodeType.NODEJS }
|
|
56
|
+
const descriptor4: PeerDescriptor = { kademliaId: id4.value, type: NodeType.NODEJS }
|
|
57
|
+
|
|
58
|
+
const peer1 = new DhtPeer(descriptor0, descriptor1, toProtoRpcClient(new MockRpcClient()), serviceId)
|
|
59
|
+
const peer2 = new DhtPeer(descriptor0, descriptor2, toProtoRpcClient(new MockRpcClient()), serviceId)
|
|
60
|
+
const peer3 = new DhtPeer(descriptor0, descriptor3, toProtoRpcClient(new MockRpcClient()), serviceId)
|
|
61
|
+
const peer4 = new DhtPeer(descriptor0, descriptor4, toProtoRpcClient(new MockRpcClient()), serviceId)
|
|
62
|
+
|
|
63
|
+
it('compares Ids correctly', async () => {
|
|
64
|
+
const list = new SortedContactList(id0, 10)
|
|
65
|
+
expect(list.compareIds(id0, id0)).toBe(0)
|
|
66
|
+
expect(list.compareIds(id1, id1)).toBe(0)
|
|
67
|
+
expect(list.compareIds(id0, id1)).toBe(-1)
|
|
68
|
+
expect(list.compareIds(id0, id2)).toBe(-2)
|
|
69
|
+
expect(list.compareIds(id1, id0)).toBe(1)
|
|
70
|
+
expect(list.compareIds(id2, id0)).toBe(2)
|
|
71
|
+
expect(list.compareIds(id2, id3)).toBe(-1)
|
|
72
|
+
expect(list.compareIds(id1, id4)).toBe(-3)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('orders itself correctly', async () => {
|
|
76
|
+
|
|
77
|
+
const list = new SortedContactList(id0, 10)
|
|
78
|
+
|
|
79
|
+
list.addContact(peer3)
|
|
80
|
+
list.addContact(peer2)
|
|
81
|
+
list.addContact(peer1)
|
|
82
|
+
|
|
83
|
+
const contacts = list.getUncontactedContacts(3)
|
|
84
|
+
expect(contacts.length).toEqual(3)
|
|
85
|
+
expect(contacts[0]).toEqual(peer1)
|
|
86
|
+
expect(contacts[1]).toEqual(peer2)
|
|
87
|
+
expect(contacts[2]).toEqual(peer3)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('handles contacted nodes correctly', async () => {
|
|
91
|
+
const list = new SortedContactList(id0, 10)
|
|
92
|
+
|
|
93
|
+
list.addContact(peer3)
|
|
94
|
+
list.addContact(peer2)
|
|
95
|
+
list.addContact(peer1)
|
|
96
|
+
|
|
97
|
+
list.setContacted(id2)
|
|
98
|
+
const contacts = list.getUncontactedContacts(3)
|
|
99
|
+
expect(contacts.length).toEqual(2)
|
|
100
|
+
expect(contacts[0]).toEqual(peer1)
|
|
101
|
+
expect(contacts[1]).toEqual(peer3)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('cannot exceed maxSize', async () => {
|
|
105
|
+
const list = new SortedContactList(id0, 3)
|
|
106
|
+
list.addContact(peer4)
|
|
107
|
+
list.addContact(peer3)
|
|
108
|
+
list.addContact(peer2)
|
|
109
|
+
list.addContact(peer1)
|
|
110
|
+
expect(list.getSize()).toEqual(3)
|
|
111
|
+
expect(list.getContact(id4)).toBeFalsy()
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('removing contacts', async () => {
|
|
115
|
+
const list = new SortedContactList(id0, 8)
|
|
116
|
+
list.addContact(peer4)
|
|
117
|
+
list.addContact(peer3)
|
|
118
|
+
list.addContact(peer2)
|
|
119
|
+
list.addContact(peer1)
|
|
120
|
+
list.removeContact(id2)
|
|
121
|
+
expect(list.getSize()).toEqual(3)
|
|
122
|
+
expect(list.getContact(id2)).toBeFalsy()
|
|
123
|
+
expect(list.getContactIds()).toEqual(list.getContactIds().sort(list.compareIds))
|
|
124
|
+
const ret = list.removeContact(PeerID.fromValue(Buffer.from([0, 0, 0, 6])))
|
|
125
|
+
expect(ret).toEqual(false)
|
|
126
|
+
})
|
|
127
|
+
})
|