@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,108 @@
1
+ // @generated by protobuf-ts 2.8.2 with parameter server_generic,generate_dependencies,long_type_number
2
+ // @generated from protobuf file "packages/proto-rpc/protos/ProtoRpc.proto" (package "protorpc", syntax proto3)
3
+ // tslint:disable
4
+ import { MessageType } from "@protobuf-ts/runtime";
5
+ import { Empty } from "../../../google/protobuf/empty";
6
+ import { Any } from "../../../google/protobuf/any";
7
+ /**
8
+ * @generated from protobuf message protorpc.RpcMessage
9
+ */
10
+ export interface RpcMessage {
11
+ /**
12
+ * @generated from protobuf field: map<string, string> header = 1;
13
+ */
14
+ header: {
15
+ [key: string]: string;
16
+ };
17
+ /**
18
+ * @generated from protobuf field: google.protobuf.Any body = 2;
19
+ */
20
+ body?: Any;
21
+ /**
22
+ * @generated from protobuf field: string requestId = 3;
23
+ */
24
+ requestId: string;
25
+ /**
26
+ * @generated from protobuf field: optional protorpc.RpcErrorType errorType = 4;
27
+ */
28
+ errorType?: RpcErrorType;
29
+ /**
30
+ * @generated from protobuf field: optional string errorClassName = 5;
31
+ */
32
+ errorClassName?: string;
33
+ /**
34
+ * @generated from protobuf field: optional string errorCode = 6;
35
+ */
36
+ errorCode?: string;
37
+ /**
38
+ * @generated from protobuf field: optional string errorMessage = 7;
39
+ */
40
+ errorMessage?: string;
41
+ }
42
+ // Dummy message to force the generation of the typescript class "google.prototype.Empty"
43
+ // We need this generated class in RpcCommunicator
44
+
45
+ /**
46
+ * @generated from protobuf message protorpc.Mnfo2uhnf92hvqi2nviouq2hv9puhq
47
+ */
48
+ export interface Mnfo2uhnf92hvqi2nviouq2hv9puhq {
49
+ /**
50
+ * @generated from protobuf field: google.protobuf.Empty empty = 1;
51
+ */
52
+ empty?: Empty;
53
+ }
54
+ /**
55
+ * @generated from protobuf enum protorpc.RpcErrorType
56
+ */
57
+ export enum RpcErrorType {
58
+ /**
59
+ * @generated from protobuf enum value: SERVER_TIMEOUT = 0;
60
+ */
61
+ SERVER_TIMEOUT = 0,
62
+ /**
63
+ * @generated from protobuf enum value: CLIENT_TIMEOUT = 1;
64
+ */
65
+ CLIENT_TIMEOUT = 1,
66
+ /**
67
+ * @generated from protobuf enum value: UNKNOWN_RPC_METHOD = 2;
68
+ */
69
+ UNKNOWN_RPC_METHOD = 2,
70
+ /**
71
+ * @generated from protobuf enum value: CLIENT_ERROR = 3;
72
+ */
73
+ CLIENT_ERROR = 3,
74
+ /**
75
+ * @generated from protobuf enum value: SERVER_ERROR = 4;
76
+ */
77
+ SERVER_ERROR = 4
78
+ }
79
+ // @generated message type with reflection information, may provide speed optimized methods
80
+ class RpcMessage$Type extends MessageType<RpcMessage> {
81
+ constructor() {
82
+ super("protorpc.RpcMessage", [
83
+ { no: 1, name: "header", kind: "map", K: 9 /*ScalarType.STRING*/, V: { kind: "scalar", T: 9 /*ScalarType.STRING*/ } },
84
+ { no: 2, name: "body", kind: "message", T: () => Any },
85
+ { no: 3, name: "requestId", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
86
+ { no: 4, name: "errorType", kind: "enum", opt: true, T: () => ["protorpc.RpcErrorType", RpcErrorType] },
87
+ { no: 5, name: "errorClassName", kind: "scalar", opt: true, T: 9 /*ScalarType.STRING*/ },
88
+ { no: 6, name: "errorCode", kind: "scalar", opt: true, T: 9 /*ScalarType.STRING*/ },
89
+ { no: 7, name: "errorMessage", kind: "scalar", opt: true, T: 9 /*ScalarType.STRING*/ }
90
+ ]);
91
+ }
92
+ }
93
+ /**
94
+ * @generated MessageType for protobuf message protorpc.RpcMessage
95
+ */
96
+ export const RpcMessage = new RpcMessage$Type();
97
+ // @generated message type with reflection information, may provide speed optimized methods
98
+ class Mnfo2uhnf92hvqi2nviouq2hv9puhq$Type extends MessageType<Mnfo2uhnf92hvqi2nviouq2hv9puhq> {
99
+ constructor() {
100
+ super("protorpc.Mnfo2uhnf92hvqi2nviouq2hv9puhq", [
101
+ { no: 1, name: "empty", kind: "message", T: () => Empty }
102
+ ]);
103
+ }
104
+ }
105
+ /**
106
+ * @generated MessageType for protobuf message protorpc.Mnfo2uhnf92hvqi2nviouq2hv9puhq
107
+ */
108
+ export const Mnfo2uhnf92hvqi2nviouq2hv9puhq = new Mnfo2uhnf92hvqi2nviouq2hv9puhq$Type();
@@ -0,0 +1,52 @@
1
+ // @generated by protobuf-ts 2.8.0 with parameter server_generic,generate_dependencies
2
+ // @generated from protobuf file "tests.proto" (package "tests", syntax proto3)
3
+ // tslint:disable
4
+ import { MessageType } from "@protobuf-ts/runtime";
5
+ import { Any } from "./google/protobuf/any";
6
+ /**
7
+ * @generated from protobuf message tests.TestMessage
8
+ */
9
+ export interface TestMessage {
10
+ /**
11
+ * @generated from protobuf field: string messageId = 1;
12
+ */
13
+ messageId: string;
14
+ /**
15
+ * @generated from protobuf field: google.protobuf.Any body = 2;
16
+ */
17
+ body?: Any;
18
+ }
19
+ /**
20
+ * @generated from protobuf message tests.SomeMessage
21
+ */
22
+ export interface SomeMessage {
23
+ /**
24
+ * @generated from protobuf field: string juttu = 1;
25
+ */
26
+ juttu: string;
27
+ }
28
+ // @generated message type with reflection information, may provide speed optimized methods
29
+ class TestMessage$Type extends MessageType<TestMessage> {
30
+ constructor() {
31
+ super("tests.TestMessage", [
32
+ { no: 1, name: "messageId", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
33
+ { no: 2, name: "body", kind: "message", T: () => Any }
34
+ ]);
35
+ }
36
+ }
37
+ /**
38
+ * @generated MessageType for protobuf message tests.TestMessage
39
+ */
40
+ export const TestMessage = new TestMessage$Type();
41
+ // @generated message type with reflection information, may provide speed optimized methods
42
+ class SomeMessage$Type extends MessageType<SomeMessage> {
43
+ constructor() {
44
+ super("tests.SomeMessage", [
45
+ { no: 1, name: "juttu", kind: "scalar", T: 9 /*ScalarType.STRING*/ }
46
+ ]);
47
+ }
48
+ }
49
+ /**
50
+ * @generated MessageType for protobuf message tests.SomeMessage
51
+ */
52
+ export const SomeMessage = new SomeMessage$Type();
@@ -0,0 +1,15 @@
1
+ import { ProtoCallContext } from '@streamr/proto-rpc'
2
+ import { PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
3
+ import { DhtRpcOptions } from './DhtRpcOptions'
4
+
5
+ export class DhtCallContext extends ProtoCallContext implements DhtRpcOptions {
6
+ // used by client
7
+ targetDescriptor?: PeerDescriptor
8
+ sourceDescriptor?: PeerDescriptor
9
+ notification?: boolean
10
+ clientId?: number
11
+ doNotConnect?: boolean
12
+ //used in incoming calls
13
+ incomingTargetDescriptor?: PeerDescriptor
14
+ incomingSourceDescriptor?: PeerDescriptor
15
+ }
@@ -0,0 +1,9 @@
1
+ import { ProtoRpcOptions } from '@streamr/proto-rpc'
2
+ import { PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
3
+
4
+ export interface DhtRpcOptions extends ProtoRpcOptions {
5
+ targetDescriptor?: PeerDescriptor
6
+ sourceDescriptor?: PeerDescriptor
7
+ clientId?: number
8
+ doNotConnect?: boolean
9
+ }
@@ -0,0 +1,31 @@
1
+ import { Message, PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
2
+
3
+ export type DisconnectionType = 'OUTGOING_GRACEFUL_DISCONNECT' | 'OUTGOING_GRACEFUL_LEAVE' |
4
+ 'INCOMING_GRACEFUL_DISCONNECT' | 'INCOMING_GRACEFUL_LEAVE' | 'OTHER'
5
+
6
+ export interface TransportEvents {
7
+ disconnected: (peerDescriptor: PeerDescriptor, disconnectionType: DisconnectionType) => void
8
+ message: (message: Message) => void
9
+ connected: (peerDescriptor: PeerDescriptor) => void
10
+
11
+ }
12
+
13
+ export interface ITransport {
14
+ on<T extends keyof TransportEvents>(eventName: T, listener: (message: Message) => void): void
15
+ on<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor) => void): void
16
+ on<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor, disconnectionType: DisconnectionType) => void): void
17
+
18
+ once<T extends keyof TransportEvents>(eventName: T, listener: (message: Message) => void): void
19
+ once<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor) => void): void
20
+ once<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor,
21
+ disconnectionType: DisconnectionType) => void): void
22
+
23
+ off<T extends keyof TransportEvents>(eventName: T, listener: (message: Message) => void): void
24
+ off<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor) => void): void
25
+ off<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor, disconnectionType: DisconnectionType) => void): void
26
+
27
+ send(msg: Message, doNotConnect?: boolean): Promise<void>
28
+ getPeerDescriptor(): PeerDescriptor
29
+ getAllConnectionPeerDescriptors(): PeerDescriptor[]
30
+ stop(): void | Promise<void>
31
+ }
@@ -0,0 +1,14 @@
1
+ import { ITransport } from './ITransport'
2
+ import { RoutingRpcCommunicator } from './RoutingRpcCommunicator'
3
+ import { RpcCommunicatorConfig } from '@streamr/proto-rpc'
4
+ import { Message } from '../proto/packages/dht/protos/DhtRpc'
5
+
6
+ export class ListeningRpcCommunicator extends RoutingRpcCommunicator {
7
+ constructor(ownServiceId: string, transport: ITransport, config?: RpcCommunicatorConfig) {
8
+ super(ownServiceId, transport.send, config)
9
+
10
+ transport.on('message', (msg: Message) => {
11
+ this.handleMessageFromPeer(msg)
12
+ })
13
+ }
14
+ }
@@ -0,0 +1,59 @@
1
+ import { Message, MessageType, PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
2
+ import { v4 } from 'uuid'
3
+ import { RpcCommunicator, RpcCommunicatorConfig } from '@streamr/proto-rpc'
4
+ import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
5
+ import { RpcMessage } from '../proto/packages/proto-rpc/protos/ProtoRpc'
6
+
7
+ export class RoutingRpcCommunicator extends RpcCommunicator {
8
+ private ownServiceId: string
9
+ private sendFn: (msg: Message, doNotConnect?: boolean, doNotMindStopped?: boolean) => Promise<void>
10
+
11
+ constructor(
12
+ ownServiceId: string,
13
+ sendFn: (msg: Message, doNotConnect?: boolean) => Promise<void>,
14
+ config?: RpcCommunicatorConfig
15
+ ) {
16
+ super(config)
17
+ this.ownServiceId = ownServiceId
18
+ this.sendFn = sendFn
19
+
20
+ this.setOutgoingMessageListener((msg: RpcMessage, _requestId: string, callContext?: DhtCallContext) => {
21
+ let targetDescriptor: PeerDescriptor
22
+ // rpc call message
23
+
24
+ if (callContext!.targetDescriptor) {
25
+ targetDescriptor = callContext!.targetDescriptor!
26
+ } else { // rpc reply message
27
+ targetDescriptor = callContext!.incomingSourceDescriptor!
28
+ }
29
+
30
+ const message: Message = {
31
+ messageId: v4(),
32
+ serviceId: this.ownServiceId,
33
+ body: {
34
+ oneofKind: 'rpcMessage',
35
+ rpcMessage: msg
36
+ },
37
+ messageType: MessageType.RPC,
38
+ targetDescriptor: targetDescriptor
39
+ }
40
+
41
+ if (msg.header.response || callContext && callContext.doNotConnect && callContext.doNotMindStopped ) {
42
+ return this.sendFn(message, true, true)
43
+ } else if (msg.header.response || callContext && callContext.doNotConnect) {
44
+ return this.sendFn(message, true)
45
+ } else {
46
+ return this.sendFn(message)
47
+ }
48
+
49
+ })
50
+ }
51
+
52
+ public handleMessageFromPeer(message: Message): void {
53
+ if (message.serviceId == this.ownServiceId && message.body.oneofKind === 'rpcMessage') {
54
+ const context = new DhtCallContext()
55
+ context.incomingSourceDescriptor = message.sourceDescriptor
56
+ this.handleIncomingMessage(message.body.rpcMessage, context)
57
+ }
58
+ }
59
+ }
@@ -0,0 +1 @@
1
+ import 'jest-extended'
@@ -0,0 +1,7 @@
1
+ import { TextEncoder as _TextEncoder } from 'node:util'
2
+ import { TextDecoder as _TextDecoder } from 'node:util'
3
+
4
+ declare global {
5
+ const TextEncoder: typeof _TextEncoder
6
+ const TextDecoder: typeof _TextDecoder
7
+ }
@@ -0,0 +1,52 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import KBucket from 'k-bucket'
4
+ import { range } from 'lodash'
5
+
6
+ const compareIds = (id1: Uint8Array, id2: Uint8Array) => {
7
+ const sorted = [id1, id2].sort()
8
+ return KBucket.distance(sorted[0], sorted[1])
9
+ }
10
+
11
+ const generateIDs = (count: number): Uint8Array[] => {
12
+ return range(count).map((i) => new Uint8Array([i, 1, 1]))
13
+ }
14
+
15
+ const count = 9
16
+ const ids = generateIDs(count)
17
+
18
+ const distances = ids.map((id1) => ids.map((id2) => {
19
+ return {
20
+ distance: compareIds(id1, id2),
21
+ id: id2.toString()
22
+ }
23
+ }))
24
+
25
+ const graph: any[] = []
26
+ for (let i = 0; i < count; i++) {
27
+ const sorted = distances[i].sort((a, b) => a.distance - b.distance)
28
+ graph.push([sorted[1], sorted[2], sorted[3], sorted[4]])
29
+ }
30
+ const result = new Map<string, number>()
31
+ ids.forEach((key) => {
32
+ result.set(key.toString(), 0)
33
+ })
34
+
35
+ for (let i = 0; i < count; i++) {
36
+ graph[i].forEach((val: any) => {
37
+ result.set(val.id, result.get(val.id)! + 1)
38
+ })
39
+ }
40
+
41
+ const bidirectional = ids.map((id, i) => {
42
+ const stringId = id.toString()
43
+ const allFound = graph[i].some((obj: any) => {
44
+ const neighborId = obj.id
45
+ const neighborIndex = parseInt(neighborId.split(',')[0])
46
+ return graph[neighborIndex].some((obj: any) => obj.id === stringId)
47
+ })
48
+ return allFound
49
+ })
50
+
51
+ console.log(bidirectional)
52
+ console.log(graph)
@@ -0,0 +1,115 @@
1
+ /* eslint-disable no-console */
2
+ import { Simulator } from '../../src/connection/Simulator/Simulator'
3
+ import { DhtNode } from '../../src/dht/DhtNode'
4
+ import { NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
5
+ import { createMockConnectionDhtNode } from '../utils/utils'
6
+ import { execSync } from 'child_process'
7
+ import fs from 'fs'
8
+
9
+ describe('Kademlia correctness', () => {
10
+ let entryPoint: DhtNode
11
+ let nodes: DhtNode[]
12
+ let entrypointDescriptor: PeerDescriptor
13
+ const simulator = new Simulator()
14
+ const NUM_NODES = 1000
15
+
16
+ const nodeIndicesById: Record<string, number> = {}
17
+
18
+ if (!fs.existsSync('test/data/nodeids.json')) {
19
+ console.log('gound truth data does not exist yet, generating..')
20
+ execSync('npm run prepare-kademlia-simulation')
21
+ }
22
+
23
+ const dhtIds: Array<{ type: string, data: Array<number> }> = JSON.parse(fs.readFileSync('test/data/nodeids.json').toString())
24
+ const groundTruth: Record<string, Array<{ name: string, distance: number, id: { type: string, data: Array<number> } }>>
25
+ = JSON.parse(fs.readFileSync('test/data/orderedneighbors.json').toString())
26
+
27
+ beforeEach(async () => {
28
+
29
+ nodes = []
30
+ const entryPointId = '0'
31
+ entryPoint = await createMockConnectionDhtNode(entryPointId, simulator, Uint8Array.from(dhtIds[0].data), 8)
32
+ nodes.push(entryPoint)
33
+ nodeIndicesById[entryPoint.getNodeId().toKey()] = 0
34
+ entrypointDescriptor = {
35
+ kademliaId: entryPoint.getNodeId().value,
36
+ type: NodeType.NODEJS
37
+ }
38
+
39
+ for (let i = 1; i < NUM_NODES; i++) {
40
+ const nodeId = `${i}`
41
+
42
+ const node = await createMockConnectionDhtNode(nodeId, simulator, Uint8Array.from(dhtIds[i].data))
43
+ nodeIndicesById[node.getNodeId().toKey()] = i
44
+ nodes.push(node)
45
+ }
46
+ })
47
+
48
+ afterEach(async () => {
49
+ await Promise.all([
50
+ entryPoint.stop(),
51
+ ...nodes.map(async (node) => await node.stop())
52
+ ])
53
+ })
54
+
55
+ it('Can find correct neighbors', async () => {
56
+ await entryPoint.joinDht([entrypointDescriptor])
57
+
58
+ await Promise.allSettled(
59
+ nodes.map((node) => node.joinDht([entrypointDescriptor]))
60
+ )
61
+
62
+ let minimumCorrectNeighbors = Number.MAX_SAFE_INTEGER
63
+ let sumCorrectNeighbors = 0
64
+ let sumKbucketSize = 1
65
+
66
+ for (let i = nodes.length - 1; i >= 0; i--) {
67
+ let groundTruthString = 'groundTruthNeighb: '
68
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
69
+ for (let j = 0; j < groundTruth[i + ''].length; j++) {
70
+ groundTruthString += groundTruth[i + ''][j].name + ','
71
+ }
72
+
73
+ const kademliaNeighbors = nodes[i].getNeighborList().getContactIds()
74
+
75
+ let kadString = 'kademliaNeighbors: '
76
+ kademliaNeighbors.forEach((neighbor) => {
77
+ kadString += nodeIndicesById[neighbor.toKey()] + ','
78
+ })
79
+
80
+ let correctNeighbors = 0
81
+ try {
82
+ for (let j = 0; j < groundTruth[i + ''].length; j++) {
83
+ if (groundTruth[i + ''][j].name != (nodeIndicesById[kademliaNeighbors[j].toKey()] + '')) {
84
+ break
85
+ }
86
+ correctNeighbors++
87
+ }
88
+ } catch (e) {
89
+ console.error('Node ' + nodes[i].getNodeName() + ' had only ' + kademliaNeighbors.length + ' kademlia neighbors')
90
+ }
91
+ if (correctNeighbors === 0) {
92
+ console.log('No correct neighbors found for node ' + i)
93
+ console.log(groundTruthString)
94
+ console.log(kadString)
95
+ }
96
+ if (correctNeighbors < minimumCorrectNeighbors) {
97
+ console.log('NEW MIN', i, correctNeighbors)
98
+ minimumCorrectNeighbors = correctNeighbors
99
+ }
100
+
101
+ if (i > 0) {
102
+ sumKbucketSize += nodes[i].getBucketSize()
103
+ sumCorrectNeighbors += correctNeighbors
104
+ }
105
+ }
106
+
107
+ const avgKbucketSize = sumKbucketSize / (NUM_NODES - 1)
108
+ const avgCorrectNeighbors = sumCorrectNeighbors / (NUM_NODES - 1)
109
+
110
+ console.log('----------- Simulation results ------------------')
111
+ console.log('Minimum correct neighbors: ' + minimumCorrectNeighbors)
112
+ console.log('Average correct neighbors: ' + avgCorrectNeighbors)
113
+ console.log('Average Kbucket size: ' + avgKbucketSize)
114
+ })
115
+ })
@@ -0,0 +1,87 @@
1
+ /* eslint-disable no-console */
2
+ import { LatencyType, Simulator } from '../../src/connection/Simulator/Simulator'
3
+ import { DhtNode } from '../../src/dht/DhtNode'
4
+ import { NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
5
+ import { createMockConnectionDhtNode } from '../utils/utils'
6
+ import { execSync } from 'child_process'
7
+ import fs from 'fs'
8
+ import { PeerID, peerIdFromPeerDescriptor } from '../../src/exports'
9
+ import { Logger, wait } from '@streamr/utils'
10
+ import { debugVars } from '../../src/helpers/debugHelpers'
11
+
12
+ const logger = new Logger(module)
13
+
14
+ describe('Recursive find correctness', () => {
15
+ let entryPoint: DhtNode
16
+ let nodes: DhtNode[]
17
+ let entrypointDescriptor: PeerDescriptor
18
+ const simulator = new Simulator(LatencyType.NONE)
19
+ const NUM_NODES = 1000
20
+
21
+ const nodeIndicesById: Record<string, number> = {}
22
+
23
+ if (!fs.existsSync('test/data/nodeids.json')) {
24
+ console.log('ground truth data does not exist yet, generating..')
25
+ execSync('npm run prepare-kademlia-simulation')
26
+ }
27
+
28
+ const dhtIds: Array<{ type: string, data: Array<number> }> = JSON.parse(fs.readFileSync('test/data/nodeids.json').toString())
29
+
30
+ beforeEach(async () => {
31
+
32
+ nodes = []
33
+ const entryPointId = '0'
34
+ entryPoint = await createMockConnectionDhtNode(entryPointId, simulator, Uint8Array.from(dhtIds[0].data), undefined, entryPointId)
35
+ nodes.push(entryPoint)
36
+ nodeIndicesById[entryPoint.getNodeId().toKey()] = 0
37
+ entrypointDescriptor = {
38
+ kademliaId: entryPoint.getNodeId().value,
39
+ type: NodeType.NODEJS,
40
+ nodeName: entryPointId
41
+ }
42
+
43
+ for (let i = 1; i < NUM_NODES; i++) {
44
+ const nodeId = `${i}`
45
+
46
+ const node = await createMockConnectionDhtNode(nodeId, simulator, Uint8Array.from(dhtIds[i].data), undefined, nodeId)
47
+ nodeIndicesById[node.getNodeId().toKey()] = i
48
+ nodes.push(node)
49
+ }
50
+ })
51
+
52
+ afterEach(async () => {
53
+ await Promise.all([
54
+ entryPoint.stop(),
55
+ ...nodes.map(async (node) => await node.stop())
56
+ ])
57
+ })
58
+
59
+ it('Entrypoint can find a node from the network (exact match)', async () => {
60
+ await entryPoint.joinDht([entrypointDescriptor])
61
+
62
+ await Promise.all(
63
+ nodes.map((node) => node.joinDht([entrypointDescriptor]))
64
+ )
65
+
66
+ logger.info('waiting 120s')
67
+ debugVars['waiting'] = true
68
+
69
+ await wait(120000)
70
+ debugVars['waiting'] = false
71
+ logger.info('waiting over')
72
+
73
+ nodes.forEach((node) => logger.info(node.getNodeName() + ': connections:' +
74
+ node.getNumberOfConnections() + ', kbucket: ' + node.getBucketSize()
75
+ + ', localLocked: ' + node.getNumberOfLocalLockedConnections()
76
+ + ', remoteLocked: ' + node.getNumberOfRemoteLockedConnections()
77
+ + ', weakLocked: ' + node.getNumberOfWeakLockedConnections()))
78
+
79
+ logger.info('starting recursive find')
80
+ const kademliaIdToFind = Uint8Array.from(dhtIds[9].data)
81
+ const results = await nodes[159].startRecursiveFind(kademliaIdToFind)
82
+ logger.info('recursive find over')
83
+ expect(results.closestNodes).toBeGreaterThanOrEqual(5)
84
+ expect(PeerID.fromValue(kademliaIdToFind).equals(peerIdFromPeerDescriptor(results.closestNodes[0])))
85
+
86
+ }, 180000)
87
+ })
@@ -0,0 +1,28 @@
1
+ import { Logger } from '@streamr/utils'
2
+ import { Any } from '../../src/proto/google/protobuf/any'
3
+ import { SomeMessage, TestMessage } from '../../src/proto/tests'
4
+
5
+ const logger = new Logger(module)
6
+
7
+ const some: SomeMessage = SomeMessage.create({
8
+ juttu: 'kivaa'
9
+ })
10
+ logger.info(SomeMessage.typeName)
11
+
12
+ const message: TestMessage = TestMessage.create({
13
+ messageId: 'jee',
14
+ body: Any.pack(some, SomeMessage)
15
+ })
16
+
17
+ const binary = TestMessage.toBinary(message)
18
+
19
+ const recovered = TestMessage.fromBinary(binary)
20
+ const jee = Any.unpack(recovered.body!, SomeMessage)
21
+ logger.info(JSON.stringify(jee))
22
+
23
+ logger.info(TestMessage.toJsonString(recovered, {
24
+ typeRegistry: [
25
+ TestMessage,
26
+ SomeMessage
27
+ ]
28
+ }))
@@ -0,0 +1,32 @@
1
+ import { PeerID } from '../../../src/helpers/PeerID'
2
+ import type { SimulationNode } from './SimulationNode'
3
+ import { NodeType, PeerDescriptor } from '../../../src/proto/packages/dht/protos/DhtRpc'
4
+
5
+ export class Contact {
6
+ private static counter = 0
7
+
8
+ public peerId: PeerID
9
+ public id: Uint8Array
10
+ public vectorClock = 0
11
+ public dhtNode: SimulationNode | undefined
12
+
13
+ constructor(ownId: PeerID, dhtNode?: SimulationNode) {
14
+ this.peerId = ownId
15
+ this.vectorClock = Contact.counter++
16
+ this.dhtNode = dhtNode
17
+ this.id = ownId.value
18
+ }
19
+
20
+ getPeerDescriptor(): PeerDescriptor {
21
+ const peerDescriptor: PeerDescriptor = {
22
+ kademliaId: this.peerId.value,
23
+ type: NodeType.NODEJS
24
+ }
25
+ return peerDescriptor
26
+ }
27
+
28
+ getPeerId(): PeerID {
29
+ return this.peerId
30
+ }
31
+
32
+ }