@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.
Files changed (344) hide show
  1. package/.eslintignore +5 -0
  2. package/.eslintrc +3 -0
  3. package/README.md +38 -0
  4. package/dist/src/connection/Connection.d.ts +11 -0
  5. package/dist/src/connection/Connection.js +23 -0
  6. package/dist/src/connection/Connection.js.map +1 -0
  7. package/dist/src/connection/ConnectionLockHandler.d.ts +23 -0
  8. package/dist/src/connection/ConnectionLockHandler.js +94 -0
  9. package/dist/src/connection/ConnectionLockHandler.js.map +1 -0
  10. package/dist/src/connection/ConnectionManager.d.ts +94 -0
  11. package/dist/src/connection/ConnectionManager.js +554 -0
  12. package/dist/src/connection/ConnectionManager.js.map +1 -0
  13. package/dist/src/connection/ConnectivityChecker.d.ts +17 -0
  14. package/dist/src/connection/ConnectivityChecker.js +187 -0
  15. package/dist/src/connection/ConnectivityChecker.js.map +1 -0
  16. package/dist/src/connection/Handshaker.d.ts +19 -0
  17. package/dist/src/connection/Handshaker.js +77 -0
  18. package/dist/src/connection/Handshaker.js.map +1 -0
  19. package/dist/src/connection/IConnection.d.ts +38 -0
  20. package/dist/src/connection/IConnection.js +19 -0
  21. package/dist/src/connection/IConnection.js.map +1 -0
  22. package/dist/src/connection/IConnectionSource.d.ts +4 -0
  23. package/dist/src/connection/IConnectionSource.js +3 -0
  24. package/dist/src/connection/IConnectionSource.js.map +1 -0
  25. package/dist/src/connection/ManagedConnection.d.ts +60 -0
  26. package/dist/src/connection/ManagedConnection.js +352 -0
  27. package/dist/src/connection/ManagedConnection.js.map +1 -0
  28. package/dist/src/connection/ManagedWebRtcConnection.d.ts +7 -0
  29. package/dist/src/connection/ManagedWebRtcConnection.js +20 -0
  30. package/dist/src/connection/ManagedWebRtcConnection.js.map +1 -0
  31. package/dist/src/connection/RemoteConnectionLocker.d.ts +14 -0
  32. package/dist/src/connection/RemoteConnectionLocker.js +93 -0
  33. package/dist/src/connection/RemoteConnectionLocker.js.map +1 -0
  34. package/dist/src/connection/Simulator/Simulator.d.ts +42 -0
  35. package/dist/src/connection/Simulator/Simulator.js +325 -0
  36. package/dist/src/connection/Simulator/Simulator.js.map +1 -0
  37. package/dist/src/connection/Simulator/SimulatorConnection.d.ts +19 -0
  38. package/dist/src/connection/Simulator/SimulatorConnection.js +118 -0
  39. package/dist/src/connection/Simulator/SimulatorConnection.js.map +1 -0
  40. package/dist/src/connection/Simulator/SimulatorConnector.d.ts +17 -0
  41. package/dist/src/connection/Simulator/SimulatorConnector.js +72 -0
  42. package/dist/src/connection/Simulator/SimulatorConnector.js.map +1 -0
  43. package/dist/src/connection/Simulator/SimulatorTransport.d.ts +6 -0
  44. package/dist/src/connection/Simulator/SimulatorTransport.js +11 -0
  45. package/dist/src/connection/Simulator/SimulatorTransport.js.map +1 -0
  46. package/dist/src/connection/Simulator/pings.d.ts +21 -0
  47. package/dist/src/connection/Simulator/pings.js +61 -0
  48. package/dist/src/connection/Simulator/pings.js.map +1 -0
  49. package/dist/src/connection/WebRTC/IWebRtcConnection.d.ts +20 -0
  50. package/dist/src/connection/WebRTC/IWebRtcConnection.js +9 -0
  51. package/dist/src/connection/WebRTC/IWebRtcConnection.js.map +1 -0
  52. package/dist/src/connection/WebRTC/NodeWebRtcConnection.d.ts +47 -0
  53. package/dist/src/connection/WebRTC/NodeWebRtcConnection.js +233 -0
  54. package/dist/src/connection/WebRTC/NodeWebRtcConnection.js.map +1 -0
  55. package/dist/src/connection/WebRTC/RemoteWebrtcConnector.d.ts +12 -0
  56. package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js +74 -0
  57. package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js.map +1 -0
  58. package/dist/src/connection/WebRTC/WebRtcConnector.d.ts +47 -0
  59. package/dist/src/connection/WebRTC/WebRtcConnector.js +227 -0
  60. package/dist/src/connection/WebRTC/WebRtcConnector.js.map +1 -0
  61. package/dist/src/connection/WebRTC/iceServerAsString.d.ts +2 -0
  62. package/dist/src/connection/WebRTC/iceServerAsString.js +18 -0
  63. package/dist/src/connection/WebRTC/iceServerAsString.js.map +1 -0
  64. package/dist/src/connection/WebSocket/ClientWebSocket.d.ts +15 -0
  65. package/dist/src/connection/WebSocket/ClientWebSocket.js +113 -0
  66. package/dist/src/connection/WebSocket/ClientWebSocket.js.map +1 -0
  67. package/dist/src/connection/WebSocket/RemoteWebSocketConnector.d.ts +9 -0
  68. package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js +63 -0
  69. package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js.map +1 -0
  70. package/dist/src/connection/WebSocket/ServerWebSocket.d.ts +18 -0
  71. package/dist/src/connection/WebSocket/ServerWebSocket.js +103 -0
  72. package/dist/src/connection/WebSocket/ServerWebSocket.js.map +1 -0
  73. package/dist/src/connection/WebSocket/WebSocketConnector.d.ts +31 -0
  74. package/dist/src/connection/WebSocket/WebSocketConnector.js +202 -0
  75. package/dist/src/connection/WebSocket/WebSocketConnector.js.map +1 -0
  76. package/dist/src/connection/WebSocket/WebSocketServer.d.ts +9 -0
  77. package/dist/src/connection/WebSocket/WebSocketServer.js +101 -0
  78. package/dist/src/connection/WebSocket/WebSocketServer.js.map +1 -0
  79. package/dist/src/dht/DhtNode.d.ts +153 -0
  80. package/dist/src/dht/DhtNode.js +599 -0
  81. package/dist/src/dht/DhtNode.js.map +1 -0
  82. package/dist/src/dht/DhtPeer.d.ts +18 -0
  83. package/dist/src/dht/DhtPeer.js +74 -0
  84. package/dist/src/dht/DhtPeer.js.map +1 -0
  85. package/dist/src/dht/ExternalApi.d.ts +8 -0
  86. package/dist/src/dht/ExternalApi.js +26 -0
  87. package/dist/src/dht/ExternalApi.js.map +1 -0
  88. package/dist/src/dht/RemoteExternalApi.d.ts +6 -0
  89. package/dist/src/dht/RemoteExternalApi.js +26 -0
  90. package/dist/src/dht/RemoteExternalApi.js.map +1 -0
  91. package/dist/src/dht/contact/Contact.d.ts +22 -0
  92. package/dist/src/dht/contact/Contact.js +25 -0
  93. package/dist/src/dht/contact/Contact.js.map +1 -0
  94. package/dist/src/dht/contact/RandomContactList.d.ts +20 -0
  95. package/dist/src/dht/contact/RandomContactList.js +78 -0
  96. package/dist/src/dht/contact/RandomContactList.js.map +1 -0
  97. package/dist/src/dht/contact/Remote.d.ts +15 -0
  98. package/dist/src/dht/contact/Remote.js +24 -0
  99. package/dist/src/dht/contact/Remote.js.map +1 -0
  100. package/dist/src/dht/contact/SortedContactList.d.ts +35 -0
  101. package/dist/src/dht/contact/SortedContactList.js +156 -0
  102. package/dist/src/dht/contact/SortedContactList.js.map +1 -0
  103. package/dist/src/dht/discovery/DiscoverySession.d.ts +36 -0
  104. package/dist/src/dht/discovery/DiscoverySession.js +116 -0
  105. package/dist/src/dht/discovery/DiscoverySession.js.map +1 -0
  106. package/dist/src/dht/discovery/PeerDiscovery.d.ts +42 -0
  107. package/dist/src/dht/discovery/PeerDiscovery.js +157 -0
  108. package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -0
  109. package/dist/src/dht/find/RecursiveFindSession.d.ts +46 -0
  110. package/dist/src/dht/find/RecursiveFindSession.js +142 -0
  111. package/dist/src/dht/find/RecursiveFindSession.js.map +1 -0
  112. package/dist/src/dht/find/RecursiveFinder.d.ts +54 -0
  113. package/dist/src/dht/find/RecursiveFinder.js +180 -0
  114. package/dist/src/dht/find/RecursiveFinder.js.map +1 -0
  115. package/dist/src/dht/find/RemoteRecursiveFindSession.d.ts +6 -0
  116. package/dist/src/dht/find/RemoteRecursiveFindSession.js +25 -0
  117. package/dist/src/dht/find/RemoteRecursiveFindSession.js.map +1 -0
  118. package/dist/src/dht/routing/DuplicateDetector.d.ts +13 -0
  119. package/dist/src/dht/routing/DuplicateDetector.js +41 -0
  120. package/dist/src/dht/routing/DuplicateDetector.js.map +1 -0
  121. package/dist/src/dht/routing/RemoteRouter.d.ts +8 -0
  122. package/dist/src/dht/routing/RemoteRouter.js +106 -0
  123. package/dist/src/dht/routing/RemoteRouter.js.map +1 -0
  124. package/dist/src/dht/routing/Router.d.ts +60 -0
  125. package/dist/src/dht/routing/Router.js +207 -0
  126. package/dist/src/dht/routing/Router.js.map +1 -0
  127. package/dist/src/dht/routing/RoutingSession.d.ts +42 -0
  128. package/dist/src/dht/routing/RoutingSession.js +178 -0
  129. package/dist/src/dht/routing/RoutingSession.js.map +1 -0
  130. package/dist/src/dht/store/DataStore.d.ts +45 -0
  131. package/dist/src/dht/store/DataStore.js +244 -0
  132. package/dist/src/dht/store/DataStore.js.map +1 -0
  133. package/dist/src/dht/store/LocalDataStore.d.ts +19 -0
  134. package/dist/src/dht/store/LocalDataStore.js +104 -0
  135. package/dist/src/dht/store/LocalDataStore.js.map +1 -0
  136. package/dist/src/dht/store/RemoteStore.d.ts +8 -0
  137. package/dist/src/dht/store/RemoteStore.js +44 -0
  138. package/dist/src/dht/store/RemoteStore.js.map +1 -0
  139. package/dist/src/exports.d.ts +19 -0
  140. package/dist/src/exports.js +41 -0
  141. package/dist/src/exports.js.map +1 -0
  142. package/dist/src/helpers/AddressTools.d.ts +2 -0
  143. package/dist/src/helpers/AddressTools.js +31 -0
  144. package/dist/src/helpers/AddressTools.js.map +1 -0
  145. package/dist/src/helpers/PeerID.d.ts +25 -0
  146. package/dist/src/helpers/PeerID.js +84 -0
  147. package/dist/src/helpers/PeerID.js.map +1 -0
  148. package/dist/src/helpers/UUID.d.ts +7 -0
  149. package/dist/src/helpers/UUID.js +32 -0
  150. package/dist/src/helpers/UUID.js.map +1 -0
  151. package/dist/src/helpers/debugHelpers.d.ts +3 -0
  152. package/dist/src/helpers/debugHelpers.js +11 -0
  153. package/dist/src/helpers/debugHelpers.js.map +1 -0
  154. package/dist/src/helpers/errors.d.ts +72 -0
  155. package/dist/src/helpers/errors.js +95 -0
  156. package/dist/src/helpers/errors.js.map +1 -0
  157. package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +5 -0
  158. package/dist/src/helpers/peerIdFromPeerDescriptor.js +17 -0
  159. package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -0
  160. package/dist/src/helpers/protoClasses.d.ts +2 -0
  161. package/dist/src/helpers/protoClasses.js +35 -0
  162. package/dist/src/helpers/protoClasses.js.map +1 -0
  163. package/dist/src/helpers/protoToString.d.ts +2 -0
  164. package/dist/src/helpers/protoToString.js +20 -0
  165. package/dist/src/helpers/protoToString.js.map +1 -0
  166. package/dist/src/proto/google/protobuf/any.d.ts +173 -0
  167. package/dist/src/proto/google/protobuf/any.js +155 -0
  168. package/dist/src/proto/google/protobuf/any.js.map +1 -0
  169. package/dist/src/proto/google/protobuf/empty.d.ts +32 -0
  170. package/dist/src/proto/google/protobuf/empty.js +34 -0
  171. package/dist/src/proto/google/protobuf/empty.js.map +1 -0
  172. package/dist/src/proto/google/protobuf/timestamp.d.ts +149 -0
  173. package/dist/src/proto/google/protobuf/timestamp.js +136 -0
  174. package/dist/src/proto/google/protobuf/timestamp.js.map +1 -0
  175. package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +320 -0
  176. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +245 -0
  177. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -0
  178. package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +1089 -0
  179. package/dist/src/proto/packages/dht/protos/DhtRpc.js +710 -0
  180. package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -0
  181. package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +145 -0
  182. package/dist/src/proto/packages/dht/protos/DhtRpc.server.js +3 -0
  183. package/dist/src/proto/packages/dht/protos/DhtRpc.server.js.map +1 -0
  184. package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.d.ts +87 -0
  185. package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +66 -0
  186. package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js.map +1 -0
  187. package/dist/src/proto/tests.d.ts +39 -0
  188. package/dist/src/proto/tests.js +34 -0
  189. package/dist/src/proto/tests.js.map +1 -0
  190. package/dist/src/rpc-protocol/DhtCallContext.d.ts +12 -0
  191. package/dist/src/rpc-protocol/DhtCallContext.js +8 -0
  192. package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -0
  193. package/dist/src/rpc-protocol/DhtRpcOptions.d.ts +8 -0
  194. package/dist/src/rpc-protocol/DhtRpcOptions.js +3 -0
  195. package/dist/src/rpc-protocol/DhtRpcOptions.js.map +1 -0
  196. package/dist/src/transport/ITransport.d.ts +22 -0
  197. package/dist/src/transport/ITransport.js +3 -0
  198. package/dist/src/transport/ITransport.js.map +1 -0
  199. package/dist/src/transport/ListeningRpcCommunicator.d.ts +6 -0
  200. package/dist/src/transport/ListeningRpcCommunicator.js +14 -0
  201. package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -0
  202. package/dist/src/transport/RoutingRpcCommunicator.d.ts +8 -0
  203. package/dist/src/transport/RoutingRpcCommunicator.js +52 -0
  204. package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -0
  205. package/jest.config.js +2 -0
  206. package/karma.config.js +20 -0
  207. package/package.json +64 -0
  208. package/proto.sh +3 -0
  209. package/protos/DhtRpc.proto +330 -0
  210. package/protos/tests.proto +16 -0
  211. package/src/connection/Connection.ts +23 -0
  212. package/src/connection/ConnectionLockHandler.ts +105 -0
  213. package/src/connection/ConnectionManager.ts +676 -0
  214. package/src/connection/ConnectivityChecker.ts +173 -0
  215. package/src/connection/Handshaker.ts +92 -0
  216. package/src/connection/IConnection.ts +47 -0
  217. package/src/connection/IConnectionSource.ts +6 -0
  218. package/src/connection/ManagedConnection.ts +398 -0
  219. package/src/connection/ManagedWebRtcConnection.ts +27 -0
  220. package/src/connection/RemoteConnectionLocker.ts +88 -0
  221. package/src/connection/Simulator/Simulator.ts +399 -0
  222. package/src/connection/Simulator/SimulatorConnection.ts +137 -0
  223. package/src/connection/Simulator/SimulatorConnector.ts +104 -0
  224. package/src/connection/Simulator/SimulatorTransport.ts +9 -0
  225. package/src/connection/Simulator/pings.ts +42 -0
  226. package/src/connection/WebRTC/BrowserWebRtcConnection.ts +227 -0
  227. package/src/connection/WebRTC/IWebRtcConnection.ts +24 -0
  228. package/src/connection/WebRTC/NodeWebRtcConnection.ts +256 -0
  229. package/src/connection/WebRTC/RemoteWebrtcConnector.ts +93 -0
  230. package/src/connection/WebRTC/WebRtcConnector.ts +306 -0
  231. package/src/connection/WebRTC/iceServerAsString.ts +15 -0
  232. package/src/connection/WebSocket/ClientWebSocket.ts +118 -0
  233. package/src/connection/WebSocket/RemoteWebSocketConnector.ts +49 -0
  234. package/src/connection/WebSocket/ServerWebSocket.ts +119 -0
  235. package/src/connection/WebSocket/WebSocketConnector.ts +264 -0
  236. package/src/connection/WebSocket/WebSocketServer.ts +97 -0
  237. package/src/dht/DhtNode.ts +776 -0
  238. package/src/dht/DhtPeer.ts +96 -0
  239. package/src/dht/ExternalApi.ts +29 -0
  240. package/src/dht/RemoteExternalApi.ts +25 -0
  241. package/src/dht/contact/Contact.ts +36 -0
  242. package/src/dht/contact/RandomContactList.ts +92 -0
  243. package/src/dht/contact/Remote.ts +40 -0
  244. package/src/dht/contact/SortedContactList.ts +196 -0
  245. package/src/dht/discovery/DiscoverySession.ts +150 -0
  246. package/src/dht/discovery/PeerDiscovery.ts +162 -0
  247. package/src/dht/find/RecursiveFindSession.ts +178 -0
  248. package/src/dht/find/RecursiveFinder.ts +272 -0
  249. package/src/dht/find/RemoteRecursiveFindSession.ts +33 -0
  250. package/src/dht/routing/DuplicateDetector.ts +53 -0
  251. package/src/dht/routing/RemoteRouter.ts +115 -0
  252. package/src/dht/routing/Router.ts +266 -0
  253. package/src/dht/routing/RoutingSession.ts +222 -0
  254. package/src/dht/store/DataStore.ts +321 -0
  255. package/src/dht/store/LocalDataStore.ts +114 -0
  256. package/src/dht/store/RemoteStore.ts +58 -0
  257. package/src/exports.ts +19 -0
  258. package/src/helpers/AddressTools.ts +26 -0
  259. package/src/helpers/PeerID.ts +95 -0
  260. package/src/helpers/UUID.ts +30 -0
  261. package/src/helpers/debugHelpers.ts +9 -0
  262. package/src/helpers/errors.ts +49 -0
  263. package/src/helpers/peerIdFromPeerDescriptor.ts +14 -0
  264. package/src/helpers/protoClasses.ts +63 -0
  265. package/src/helpers/protoToString.ts +21 -0
  266. package/src/proto/google/protobuf/any.ts +319 -0
  267. package/src/proto/google/protobuf/empty.ts +84 -0
  268. package/src/proto/google/protobuf/timestamp.ts +281 -0
  269. package/src/proto/packages/dht/protos/DhtRpc.client.ts +373 -0
  270. package/src/proto/packages/dht/protos/DhtRpc.server.ts +148 -0
  271. package/src/proto/packages/dht/protos/DhtRpc.ts +1399 -0
  272. package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +108 -0
  273. package/src/proto/tests.ts +52 -0
  274. package/src/rpc-protocol/DhtCallContext.ts +15 -0
  275. package/src/rpc-protocol/DhtRpcOptions.ts +9 -0
  276. package/src/transport/ITransport.ts +31 -0
  277. package/src/transport/ListeningRpcCommunicator.ts +14 -0
  278. package/src/transport/RoutingRpcCommunicator.ts +59 -0
  279. package/src/types/glogal.d.ts +1 -0
  280. package/src/types/textencoding.d.ts +7 -0
  281. package/test/RandomGraphSimulation.ts +52 -0
  282. package/test/benchmark/KademliaCorrectness.test.ts +115 -0
  283. package/test/benchmark/RecursiveFind.test.ts +87 -0
  284. package/test/benchmark/any.test.ts +28 -0
  285. package/test/benchmark/kademlia-simulation/Contact.ts +32 -0
  286. package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +94 -0
  287. package/test/benchmark/kademlia-simulation/SimulationNode.ts +125 -0
  288. package/test/data/generateGroundTruthData.ts +70 -0
  289. package/test/end-to-end/Layer0-Layer1.test.ts +87 -0
  290. package/test/end-to-end/Layer0.test.ts +60 -0
  291. package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +94 -0
  292. package/test/end-to-end/Layer0WebRTC-Layer1.test.ts +134 -0
  293. package/test/end-to-end/Layer0WebRTC.test.ts +98 -0
  294. package/test/end-to-end/Layer1-Scale-WebRTC.test.ts +69 -0
  295. package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +73 -0
  296. package/test/end-to-end/WebSocketConnectionRequest.test.ts +62 -0
  297. package/test/integration/ConnectionLocking.test.ts +166 -0
  298. package/test/integration/ConnectionManager.test.ts +291 -0
  299. package/test/integration/DhtNodeExternalAPI.test.ts +43 -0
  300. package/test/integration/DhtPeer.test.ts +73 -0
  301. package/test/integration/DhtRpc.test.ts +131 -0
  302. package/test/integration/DhtWithMockConnectionLatencies.test.ts +46 -0
  303. package/test/integration/DhtWithMockConnections.test.ts +46 -0
  304. package/test/integration/DhtWithRealConnectionLatencies.test.ts +47 -0
  305. package/test/integration/Layer1-scale.test.ts +200 -0
  306. package/test/integration/MigrateData.test.ts +203 -0
  307. package/test/integration/Mock-Layer1-Layer0.test.ts +106 -0
  308. package/test/integration/MultipleEntryPointJoining.test.ts +105 -0
  309. package/test/integration/RecursiveFind.test.ts +50 -0
  310. package/test/integration/RemoteRouter.test.ts +83 -0
  311. package/test/integration/RemoteStore.test.ts +66 -0
  312. package/test/integration/RouteMessage.test.ts +254 -0
  313. package/test/integration/RpcErrors.test.ts +153 -0
  314. package/test/integration/ScaleDownDht.test.ts +66 -0
  315. package/test/integration/SimultaneousConnections.test.ts +308 -0
  316. package/test/integration/Store.test.ts +72 -0
  317. package/test/integration/StoreAndDelete.test.ts +93 -0
  318. package/test/integration/StoreOnDhtWithTwoNodes.test.ts +71 -0
  319. package/test/integration/WebRtcConnectionManagement.test.ts +205 -0
  320. package/test/integration/WebRtcConnectorRpc.test.ts +145 -0
  321. package/test/integration/WebSocket.test.ts +64 -0
  322. package/test/integration/WebSocketConnectionManagement.test.ts +131 -0
  323. package/test/integration/WebSocketConnectorRpc.test.ts +86 -0
  324. package/test/kademlia-simulation/data/nodeids.json +13002 -0
  325. package/test/kademlia-simulation/data/orderedneighbors.json +1001 -0
  326. package/test/unit/AddressTools.test.ts +40 -0
  327. package/test/unit/DuplicateDetector.test.ts +29 -0
  328. package/test/unit/LocalDataStore.test.ts +107 -0
  329. package/test/unit/PeerID.test.ts +22 -0
  330. package/test/unit/ProtobufMessage.test.ts +21 -0
  331. package/test/unit/RandomContactList.test.ts +87 -0
  332. package/test/unit/RecursiveFinder.test.ts +112 -0
  333. package/test/unit/Router.test.ts +124 -0
  334. package/test/unit/SortedContactList.test.ts +127 -0
  335. package/test/unit/UUID.test.ts +49 -0
  336. package/test/unit/WebSocketServer.test.ts +42 -0
  337. package/test/utils/mock/RecursiveFinder.ts +19 -0
  338. package/test/utils/mock/Router.ts +53 -0
  339. package/test/utils/mock/Transport.ts +26 -0
  340. package/test/utils/utils.ts +311 -0
  341. package/tsconfig.browser.json +15 -0
  342. package/tsconfig.jest.json +19 -0
  343. package/tsconfig.json +3 -0
  344. 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
+ })