@streamr/dht 100.0.0-pretestnet.4 → 100.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (444) hide show
  1. package/README.md +90 -8
  2. package/dist/package.json +70 -0
  3. package/dist/src/connection/Connection.d.ts +1 -0
  4. package/dist/src/connection/Connection.js +10 -3
  5. package/dist/src/connection/Connection.js.map +1 -1
  6. package/dist/src/connection/ConnectionLockHandler.d.ts +14 -14
  7. package/dist/src/connection/ConnectionLockHandler.js +22 -16
  8. package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
  9. package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
  10. package/dist/src/connection/ConnectionLockRpcLocal.js +7 -6
  11. package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
  12. package/dist/src/connection/ConnectionLockRpcRemote.d.ts +4 -6
  13. package/dist/src/connection/ConnectionLockRpcRemote.js +10 -37
  14. package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
  15. package/dist/src/connection/ConnectionManager.d.ts +19 -20
  16. package/dist/src/connection/ConnectionManager.js +132 -117
  17. package/dist/src/connection/ConnectionManager.js.map +1 -1
  18. package/dist/src/connection/ConnectorFacade.d.ts +3 -2
  19. package/dist/src/connection/ConnectorFacade.js +10 -3
  20. package/dist/src/connection/ConnectorFacade.js.map +1 -1
  21. package/dist/src/connection/Handshaker.d.ts +3 -1
  22. package/dist/src/connection/Handshaker.js +18 -9
  23. package/dist/src/connection/Handshaker.js.map +1 -1
  24. package/dist/src/connection/IConnection.d.ts +2 -7
  25. package/dist/src/connection/IConnection.js +1 -8
  26. package/dist/src/connection/IConnection.js.map +1 -1
  27. package/dist/src/connection/ManagedConnection.d.ts +5 -7
  28. package/dist/src/connection/ManagedConnection.js +42 -26
  29. package/dist/src/connection/ManagedConnection.js.map +1 -1
  30. package/dist/src/connection/ManagedWebrtcConnection.js.map +1 -1
  31. package/dist/src/connection/connectivityChecker.d.ts +9 -0
  32. package/dist/src/connection/connectivityChecker.js +130 -0
  33. package/dist/src/connection/connectivityChecker.js.map +1 -0
  34. package/dist/src/connection/connectivityRequestHandler.d.ts +3 -0
  35. package/dist/src/connection/connectivityRequestHandler.js +101 -0
  36. package/dist/src/connection/connectivityRequestHandler.js.map +1 -0
  37. package/dist/src/connection/simulator/Simulator.d.ts +0 -3
  38. package/dist/src/connection/simulator/Simulator.js +34 -69
  39. package/dist/src/connection/simulator/Simulator.js.map +1 -1
  40. package/dist/src/connection/simulator/SimulatorConnection.js +27 -25
  41. package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
  42. package/dist/src/connection/simulator/SimulatorConnector.js +16 -12
  43. package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
  44. package/dist/src/connection/simulator/pings.js.map +1 -1
  45. package/dist/src/connection/webrtc/NodeWebrtcConnection.d.ts +2 -3
  46. package/dist/src/connection/webrtc/NodeWebrtcConnection.js +40 -27
  47. package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
  48. package/dist/src/connection/webrtc/WebrtcConnector.d.ts +0 -2
  49. package/dist/src/connection/webrtc/WebrtcConnector.js +24 -25
  50. package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
  51. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +2 -2
  52. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +27 -20
  53. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
  54. package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +3 -6
  55. package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +2 -5
  56. package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
  57. package/dist/src/connection/webrtc/iceServerAsString.js +1 -1
  58. package/dist/src/connection/webrtc/iceServerAsString.js.map +1 -1
  59. package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +1 -2
  60. package/dist/src/connection/websocket/AutoCertifierClientFacade.js +6 -4
  61. package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
  62. package/dist/src/connection/websocket/ClientWebsocket.d.ts +1 -0
  63. package/dist/src/connection/websocket/ClientWebsocket.js +14 -7
  64. package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
  65. package/dist/src/connection/websocket/WebsocketConnector.d.ts +3 -9
  66. package/dist/src/connection/websocket/WebsocketConnector.js +142 -74
  67. package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
  68. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +5 -3
  69. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +9 -12
  70. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
  71. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +4 -7
  72. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +7 -44
  73. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
  74. package/dist/src/connection/websocket/WebsocketServer.d.ts +1 -4
  75. package/dist/src/connection/websocket/WebsocketServer.js +49 -40
  76. package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
  77. package/dist/src/connection/websocket/{ServerWebsocket.d.ts → WebsocketServerConnection.d.ts} +8 -4
  78. package/dist/src/connection/websocket/WebsocketServerConnection.js +97 -0
  79. package/dist/src/connection/websocket/WebsocketServerConnection.js.map +1 -0
  80. package/dist/src/dht/DhtNode.d.ts +45 -49
  81. package/dist/src/dht/DhtNode.js +229 -317
  82. package/dist/src/dht/DhtNode.js.map +1 -1
  83. package/dist/src/dht/DhtNodeRpcLocal.d.ts +10 -9
  84. package/dist/src/dht/DhtNodeRpcLocal.js +19 -11
  85. package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
  86. package/dist/src/dht/DhtNodeRpcRemote.d.ts +15 -10
  87. package/dist/src/dht/DhtNodeRpcRemote.js +38 -21
  88. package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
  89. package/dist/src/dht/ExternalApiRpcLocal.d.ts +7 -6
  90. package/dist/src/dht/ExternalApiRpcLocal.js +9 -13
  91. package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
  92. package/dist/src/dht/ExternalApiRpcRemote.d.ts +6 -5
  93. package/dist/src/dht/ExternalApiRpcRemote.js +10 -7
  94. package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
  95. package/dist/src/dht/PeerManager.d.ts +61 -0
  96. package/dist/src/dht/PeerManager.js +288 -0
  97. package/dist/src/dht/PeerManager.js.map +1 -0
  98. package/dist/src/dht/contact/Contact.d.ts +2 -2
  99. package/dist/src/dht/contact/Contact.js +4 -3
  100. package/dist/src/dht/contact/Contact.js.map +1 -1
  101. package/dist/src/dht/contact/ContactList.d.ts +8 -8
  102. package/dist/src/dht/contact/ContactList.js +12 -7
  103. package/dist/src/dht/contact/ContactList.js.map +1 -1
  104. package/dist/src/dht/contact/RandomContactList.d.ts +4 -4
  105. package/dist/src/dht/contact/RandomContactList.js +13 -12
  106. package/dist/src/dht/contact/RandomContactList.js.map +1 -1
  107. package/dist/src/dht/contact/RingContactList.d.ts +31 -0
  108. package/dist/src/dht/contact/RingContactList.js +133 -0
  109. package/dist/src/dht/contact/RingContactList.js.map +1 -0
  110. package/dist/src/dht/contact/{Remote.d.ts → RpcRemote.d.ts} +8 -7
  111. package/dist/src/dht/contact/{Remote.js → RpcRemote.js} +16 -13
  112. package/dist/src/dht/contact/RpcRemote.js.map +1 -0
  113. package/dist/src/dht/contact/SortedContactList.d.ts +29 -15
  114. package/dist/src/dht/contact/SortedContactList.js +79 -42
  115. package/dist/src/dht/contact/SortedContactList.js.map +1 -1
  116. package/dist/src/dht/contact/ringIdentifiers.d.ts +16 -0
  117. package/dist/src/dht/contact/ringIdentifiers.js +54 -0
  118. package/dist/src/dht/contact/ringIdentifiers.js.map +1 -0
  119. package/dist/src/dht/discovery/DiscoverySession.d.ts +8 -18
  120. package/dist/src/dht/discovery/DiscoverySession.js +32 -48
  121. package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
  122. package/dist/src/dht/discovery/PeerDiscovery.d.ts +16 -12
  123. package/dist/src/dht/discovery/PeerDiscovery.js +82 -39
  124. package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
  125. package/dist/src/dht/discovery/RingDiscoverySession.d.ts +29 -0
  126. package/dist/src/dht/discovery/RingDiscoverySession.js +123 -0
  127. package/dist/src/dht/discovery/RingDiscoverySession.js.map +1 -0
  128. package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +36 -0
  129. package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +166 -0
  130. package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -0
  131. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +14 -0
  132. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js +26 -0
  133. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -0
  134. package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.d.ts +6 -0
  135. package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js +44 -0
  136. package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -0
  137. package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +44 -0
  138. package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +187 -0
  139. package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -0
  140. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +14 -0
  141. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +20 -0
  142. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -0
  143. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.d.ts +6 -0
  144. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js +21 -0
  145. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js.map +1 -0
  146. package/dist/src/dht/routing/DuplicateDetector.d.ts +2 -4
  147. package/dist/src/dht/routing/DuplicateDetector.js +10 -15
  148. package/dist/src/dht/routing/DuplicateDetector.js.map +1 -1
  149. package/dist/src/dht/routing/Router.d.ts +11 -27
  150. package/dist/src/dht/routing/Router.js +92 -58
  151. package/dist/src/dht/routing/Router.js.map +1 -1
  152. package/dist/src/dht/routing/RouterRpcLocal.d.ts +3 -4
  153. package/dist/src/dht/routing/RouterRpcLocal.js +17 -16
  154. package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
  155. package/dist/src/dht/routing/RouterRpcRemote.d.ts +3 -3
  156. package/dist/src/dht/routing/RouterRpcRemote.js +29 -20
  157. package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
  158. package/dist/src/dht/routing/RoutingSession.d.ts +29 -21
  159. package/dist/src/dht/routing/RoutingSession.js +93 -68
  160. package/dist/src/dht/routing/RoutingSession.js.map +1 -1
  161. package/dist/src/dht/routing/RoutingTablesCache.d.ts +24 -0
  162. package/dist/src/dht/routing/RoutingTablesCache.js +46 -0
  163. package/dist/src/dht/routing/RoutingTablesCache.js.map +1 -0
  164. package/dist/src/dht/routing/getPreviousPeer.js.map +1 -1
  165. package/dist/src/dht/store/LocalDataStore.d.ts +9 -14
  166. package/dist/src/dht/store/LocalDataStore.js +46 -72
  167. package/dist/src/dht/store/LocalDataStore.js.map +1 -1
  168. package/dist/src/dht/store/StoreManager.d.ts +33 -0
  169. package/dist/src/dht/store/StoreManager.js +182 -0
  170. package/dist/src/dht/store/StoreManager.js.map +1 -0
  171. package/dist/src/dht/store/StoreRpcLocal.d.ts +11 -41
  172. package/dist/src/dht/store/StoreRpcLocal.js +27 -234
  173. package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
  174. package/dist/src/dht/store/StoreRpcRemote.d.ts +6 -7
  175. package/dist/src/dht/store/StoreRpcRemote.js +10 -20
  176. package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
  177. package/dist/src/exports.d.ts +5 -3
  178. package/dist/src/exports.js +10 -8
  179. package/dist/src/exports.js.map +1 -1
  180. package/dist/src/helpers/AddressTools.js +2 -0
  181. package/dist/src/helpers/AddressTools.js.map +1 -1
  182. package/dist/src/helpers/Connectivity.js.map +1 -1
  183. package/dist/src/helpers/MapWithTtl.d.ts +14 -0
  184. package/dist/src/helpers/MapWithTtl.js +60 -0
  185. package/dist/src/helpers/MapWithTtl.js.map +1 -0
  186. package/dist/src/helpers/createPeerDescriptor.d.ts +3 -0
  187. package/dist/src/helpers/createPeerDescriptor.js +57 -0
  188. package/dist/src/helpers/createPeerDescriptor.js.map +1 -0
  189. package/dist/src/helpers/createPeerDescriptorSignaturePayload.d.ts +2 -0
  190. package/dist/src/helpers/createPeerDescriptorSignaturePayload.js +30 -0
  191. package/dist/src/helpers/createPeerDescriptorSignaturePayload.js.map +1 -0
  192. package/dist/src/helpers/debugHelpers.js.map +1 -1
  193. package/dist/src/helpers/errors.js +2 -0
  194. package/dist/src/helpers/errors.js.map +1 -1
  195. package/dist/src/helpers/offering.d.ts +4 -0
  196. package/dist/src/helpers/offering.js +18 -0
  197. package/dist/src/helpers/offering.js.map +1 -0
  198. package/dist/src/helpers/protoClasses.js +2 -3
  199. package/dist/src/helpers/protoClasses.js.map +1 -1
  200. package/dist/src/helpers/protoToString.js.map +1 -1
  201. package/dist/src/helpers/version.d.ts +6 -0
  202. package/dist/src/helpers/version.js +38 -0
  203. package/dist/src/helpers/version.js.map +1 -0
  204. package/dist/src/identifiers.d.ts +10 -0
  205. package/dist/src/identifiers.js +31 -0
  206. package/dist/src/identifiers.js.map +1 -0
  207. package/dist/src/proto/google/protobuf/any.js +8 -8
  208. package/dist/src/proto/google/protobuf/any.js.map +1 -1
  209. package/dist/src/proto/google/protobuf/empty.js +2 -4
  210. package/dist/src/proto/google/protobuf/empty.js.map +1 -1
  211. package/dist/src/proto/google/protobuf/timestamp.js +10 -10
  212. package/dist/src/proto/google/protobuf/timestamp.js.map +1 -1
  213. package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +46 -49
  214. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +63 -54
  215. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
  216. package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +226 -232
  217. package/dist/src/proto/packages/dht/protos/DhtRpc.js +146 -168
  218. package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
  219. package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +26 -29
  220. package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +1 -1
  221. package/dist/src/rpc-protocol/DhtCallContext.d.ts +2 -2
  222. package/dist/src/rpc-protocol/DhtCallContext.js +8 -0
  223. package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -1
  224. package/dist/src/rpc-protocol/DhtRpcOptions.d.ts +2 -1
  225. package/dist/src/transport/ITransport.d.ts +10 -2
  226. package/dist/src/transport/ITransport.js +5 -0
  227. package/dist/src/transport/ITransport.js.map +1 -1
  228. package/dist/src/transport/ListeningRpcCommunicator.js +3 -1
  229. package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
  230. package/dist/src/transport/RoutingRpcCommunicator.d.ts +4 -2
  231. package/dist/src/transport/RoutingRpcCommunicator.js +19 -12
  232. package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
  233. package/jest.config.js +4 -1
  234. package/karma-setup.js +2 -0
  235. package/karma.config.js +13 -9
  236. package/package.json +16 -13
  237. package/protos/DhtRpc.proto +76 -76
  238. package/src/connection/Connection.ts +6 -1
  239. package/src/connection/ConnectionLockHandler.ts +30 -22
  240. package/src/connection/ConnectionLockRpcLocal.ts +7 -12
  241. package/src/connection/ConnectionLockRpcRemote.ts +9 -19
  242. package/src/connection/ConnectionManager.ts +132 -138
  243. package/src/connection/ConnectorFacade.ts +10 -9
  244. package/src/connection/Handshaker.ts +23 -11
  245. package/src/connection/IConnection.ts +2 -8
  246. package/src/connection/ManagedConnection.ts +34 -35
  247. package/src/connection/connectivityChecker.ts +109 -0
  248. package/src/connection/connectivityRequestHandler.ts +103 -0
  249. package/src/connection/simulator/Simulator.ts +6 -37
  250. package/src/connection/simulator/SimulatorConnection.ts +23 -25
  251. package/src/connection/simulator/SimulatorConnector.ts +11 -11
  252. package/src/connection/webrtc/BrowserWebrtcConnection.ts +17 -25
  253. package/src/connection/webrtc/NodeWebrtcConnection.ts +24 -26
  254. package/src/connection/webrtc/WebrtcConnector.ts +18 -29
  255. package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +29 -24
  256. package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +4 -14
  257. package/src/connection/webrtc/iceServerAsString.ts +1 -1
  258. package/src/connection/websocket/AutoCertifierClientFacade.ts +4 -6
  259. package/src/connection/websocket/ClientWebsocket.ts +10 -4
  260. package/src/connection/websocket/WebsocketConnector.ts +120 -97
  261. package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +13 -15
  262. package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +7 -27
  263. package/src/connection/websocket/WebsocketServer.ts +50 -53
  264. package/src/connection/websocket/WebsocketServerConnection.ts +104 -0
  265. package/src/dht/DhtNode.ts +289 -410
  266. package/src/dht/DhtNodeRpcLocal.ts +25 -17
  267. package/src/dht/DhtNodeRpcRemote.ts +43 -27
  268. package/src/dht/ExternalApiRpcLocal.ts +30 -17
  269. package/src/dht/ExternalApiRpcRemote.ts +14 -11
  270. package/src/dht/PeerManager.ts +339 -0
  271. package/src/dht/contact/Contact.ts +4 -4
  272. package/src/dht/contact/ContactList.ts +11 -10
  273. package/src/dht/contact/RandomContactList.ts +15 -15
  274. package/src/dht/contact/RingContactList.ts +151 -0
  275. package/src/dht/contact/{Remote.ts → RpcRemote.ts} +16 -19
  276. package/src/dht/contact/SortedContactList.ts +120 -72
  277. package/src/dht/contact/ringIdentifiers.ts +62 -0
  278. package/src/dht/discovery/DiscoverySession.ts +36 -61
  279. package/src/dht/discovery/PeerDiscovery.ts +98 -43
  280. package/src/dht/discovery/RingDiscoverySession.ts +160 -0
  281. package/src/dht/recursive-operation/RecursiveOperationManager.ts +246 -0
  282. package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +34 -0
  283. package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +43 -0
  284. package/src/dht/recursive-operation/RecursiveOperationSession.ts +232 -0
  285. package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +35 -0
  286. package/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.ts +30 -0
  287. package/src/dht/routing/DuplicateDetector.ts +9 -21
  288. package/src/dht/routing/Router.ts +95 -92
  289. package/src/dht/routing/RouterRpcLocal.ts +16 -18
  290. package/src/dht/routing/RouterRpcRemote.ts +26 -24
  291. package/src/dht/routing/RoutingSession.ts +119 -98
  292. package/src/dht/routing/RoutingTablesCache.ts +58 -0
  293. package/src/dht/routing/getPreviousPeer.ts +1 -1
  294. package/src/dht/store/LocalDataStore.ts +47 -77
  295. package/src/dht/store/StoreManager.ts +209 -0
  296. package/src/dht/store/StoreRpcLocal.ts +39 -308
  297. package/src/dht/store/StoreRpcRemote.ts +13 -31
  298. package/src/exports.ts +13 -3
  299. package/src/helpers/AddressTools.ts +2 -0
  300. package/src/helpers/MapWithTtl.ts +71 -0
  301. package/src/helpers/createPeerDescriptor.ts +57 -0
  302. package/src/helpers/createPeerDescriptorSignaturePayload.ts +28 -0
  303. package/src/helpers/offering.ts +15 -0
  304. package/src/helpers/protoClasses.ts +4 -6
  305. package/src/helpers/version.ts +32 -0
  306. package/src/identifiers.ts +29 -0
  307. package/src/proto/google/protobuf/any.ts +4 -4
  308. package/src/proto/google/protobuf/empty.ts +2 -4
  309. package/src/proto/google/protobuf/timestamp.ts +4 -4
  310. package/src/proto/packages/dht/protos/DhtRpc.client.ts +65 -68
  311. package/src/proto/packages/dht/protos/DhtRpc.server.ts +27 -30
  312. package/src/proto/packages/dht/protos/DhtRpc.ts +297 -313
  313. package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +1 -1
  314. package/src/rpc-protocol/DhtCallContext.ts +2 -2
  315. package/src/rpc-protocol/DhtRpcOptions.ts +2 -1
  316. package/src/transport/ITransport.ts +11 -2
  317. package/src/transport/ListeningRpcCommunicator.ts +1 -1
  318. package/src/transport/RoutingRpcCommunicator.ts +21 -14
  319. package/test/RandomGraphSimulation.ts +3 -2
  320. package/test/benchmark/Find.test.ts +13 -28
  321. package/test/benchmark/KademliaCorrectness.test.ts +24 -28
  322. package/test/benchmark/RingCorrectness.test.ts +157 -0
  323. package/test/benchmark/SortedContactListBenchmark.test.ts +151 -0
  324. package/test/benchmark/WebsocketServerMemoryLeak.test.ts +41 -0
  325. package/test/benchmark/hybrid-network-simulation/RingContactList.test.ts +72 -0
  326. package/test/benchmark/kademlia-simulation/Contact.ts +9 -9
  327. package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +11 -11
  328. package/test/benchmark/kademlia-simulation/SimulationNode.ts +29 -25
  329. package/test/data/generateGroundTruthData.ts +7 -6
  330. package/test/end-to-end/Layer0-Layer1.test.ts +11 -15
  331. package/test/end-to-end/Layer0.test.ts +19 -22
  332. package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +19 -21
  333. package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +26 -28
  334. package/test/end-to-end/Layer0Webrtc.test.ts +19 -19
  335. package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +10 -18
  336. package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +8 -15
  337. package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +2 -2
  338. package/test/end-to-end/WebsocketConnectionRequest.test.ts +8 -9
  339. package/test/end-to-end/memory-leak.test.ts +19 -24
  340. package/test/integration/ConnectionLocking.test.ts +66 -60
  341. package/test/integration/ConnectionManager.test.ts +43 -63
  342. package/test/integration/ConnectivityChecking.test.ts +52 -0
  343. package/test/integration/DhtJoinPeerDiscovery.test.ts +8 -12
  344. package/test/integration/DhtNodeExternalAPI.test.ts +17 -21
  345. package/test/integration/DhtNodeRpcRemote.test.ts +19 -26
  346. package/test/integration/DhtRpc.test.ts +20 -24
  347. package/test/integration/Find.test.ts +10 -12
  348. package/test/integration/Layer1-scale.test.ts +25 -37
  349. package/test/integration/Mock-Layer1-Layer0.test.ts +39 -59
  350. package/test/integration/MultipleEntryPointJoining.test.ts +14 -14
  351. package/test/integration/ReplicateData.test.ts +106 -0
  352. package/test/integration/RouteMessage.test.ts +42 -68
  353. package/test/integration/RouterRpcRemote.test.ts +19 -24
  354. package/test/integration/ScaleDownDht.test.ts +14 -12
  355. package/test/integration/SimultaneousConnections.test.ts +112 -111
  356. package/test/integration/Store.test.ts +43 -27
  357. package/test/integration/StoreAndDelete.test.ts +36 -48
  358. package/test/integration/StoreOnDhtWithThreeNodes.test.ts +59 -0
  359. package/test/integration/StoreOnDhtWithTwoNodes.test.ts +17 -37
  360. package/test/integration/StoreRpcRemote.test.ts +20 -32
  361. package/test/integration/WebrtcConnectionManagement.test.ts +39 -25
  362. package/test/integration/WebrtcConnectorRpc.test.ts +6 -11
  363. package/test/integration/WebsocketConnectionManagement.test.ts +87 -22
  364. package/test/integration/WebsocketConnectorRpc.test.ts +14 -24
  365. package/test/integration/{RpcErrors.test.ts → rpc-connections-over-webrpc.test.ts} +15 -26
  366. package/test/unit/AddressTools.test.ts +4 -0
  367. package/test/unit/ConnectivityHelpers.test.ts +9 -17
  368. package/test/unit/DuplicateDetector.test.ts +8 -5
  369. package/test/unit/LocalDataStore.test.ts +78 -75
  370. package/test/unit/PeerManager.test.ts +33 -0
  371. package/test/unit/RandomContactList.test.ts +12 -9
  372. package/test/unit/RecursiveOperationManager.test.ts +157 -0
  373. package/test/unit/RecursiveOperationSession.test.ts +68 -0
  374. package/test/unit/Router.test.ts +52 -35
  375. package/test/unit/RoutingSession.test.ts +79 -0
  376. package/test/unit/SortedContactList.test.ts +61 -30
  377. package/test/unit/StoreManager.test.ts +138 -0
  378. package/test/unit/WebsocketConnector.test.ts +27 -35
  379. package/test/unit/connectivityRequestHandler.test.ts +104 -0
  380. package/test/unit/createPeerDescriptor.test.ts +69 -0
  381. package/test/unit/customMatchers.test.ts +16 -0
  382. package/test/unit/version.test.ts +18 -0
  383. package/test/utils/FakeTransport.ts +47 -0
  384. package/test/utils/customMatchers.ts +71 -0
  385. package/test/utils/mock/MockRpcCommunicator.ts +7 -0
  386. package/test/utils/mock/Router.ts +13 -3
  387. package/test/utils/mock/Transport.ts +1 -1
  388. package/test/utils/mock/mockDataEntry.ts +38 -0
  389. package/test/utils/utils.ts +104 -107
  390. package/tsconfig.browser.json +2 -1
  391. package/tsconfig.jest.json +4 -2
  392. package/tsconfig.node.json +4 -2
  393. package/dist/src/connection/ConnectivityChecker.d.ts +0 -17
  394. package/dist/src/connection/ConnectivityChecker.js +0 -208
  395. package/dist/src/connection/ConnectivityChecker.js.map +0 -1
  396. package/dist/src/connection/websocket/ServerWebsocket.js +0 -100
  397. package/dist/src/connection/websocket/ServerWebsocket.js.map +0 -1
  398. package/dist/src/dht/contact/Remote.js.map +0 -1
  399. package/dist/src/dht/find/FindRpcLocal.d.ts +0 -14
  400. package/dist/src/dht/find/FindRpcLocal.js +0 -25
  401. package/dist/src/dht/find/FindRpcLocal.js.map +0 -1
  402. package/dist/src/dht/find/FindSession.d.ts +0 -44
  403. package/dist/src/dht/find/FindSession.js +0 -145
  404. package/dist/src/dht/find/FindSession.js.map +0 -1
  405. package/dist/src/dht/find/FindSessionRpcLocal.d.ts +0 -12
  406. package/dist/src/dht/find/FindSessionRpcLocal.js +0 -17
  407. package/dist/src/dht/find/FindSessionRpcLocal.js.map +0 -1
  408. package/dist/src/dht/find/FindSessionRpcRemote.d.ts +0 -6
  409. package/dist/src/dht/find/FindSessionRpcRemote.js +0 -21
  410. package/dist/src/dht/find/FindSessionRpcRemote.js.map +0 -1
  411. package/dist/src/dht/find/Finder.d.ts +0 -49
  412. package/dist/src/dht/find/Finder.js +0 -184
  413. package/dist/src/dht/find/Finder.js.map +0 -1
  414. package/dist/src/dht/routing/FindRpcRemote.d.ts +0 -6
  415. package/dist/src/dht/routing/FindRpcRemote.js +0 -41
  416. package/dist/src/dht/routing/FindRpcRemote.js.map +0 -1
  417. package/dist/src/helpers/PeerID.d.ts +0 -24
  418. package/dist/src/helpers/PeerID.js +0 -78
  419. package/dist/src/helpers/PeerID.js.map +0 -1
  420. package/dist/src/helpers/UUID.d.ts +0 -8
  421. package/dist/src/helpers/UUID.js +0 -36
  422. package/dist/src/helpers/UUID.js.map +0 -1
  423. package/dist/src/helpers/kademliaId.d.ts +0 -1
  424. package/dist/src/helpers/kademliaId.js +0 -14
  425. package/dist/src/helpers/kademliaId.js.map +0 -1
  426. package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +0 -6
  427. package/dist/src/helpers/peerIdFromPeerDescriptor.js +0 -23
  428. package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +0 -1
  429. package/src/connection/ConnectivityChecker.ts +0 -199
  430. package/src/connection/websocket/ServerWebsocket.ts +0 -114
  431. package/src/dht/find/FindRpcLocal.ts +0 -35
  432. package/src/dht/find/FindSession.ts +0 -178
  433. package/src/dht/find/FindSessionRpcLocal.ts +0 -25
  434. package/src/dht/find/FindSessionRpcRemote.ts +0 -30
  435. package/src/dht/find/Finder.ts +0 -275
  436. package/src/dht/routing/FindRpcRemote.ts +0 -40
  437. package/src/helpers/PeerID.ts +0 -88
  438. package/src/helpers/UUID.ts +0 -35
  439. package/src/helpers/kademliaId.ts +0 -8
  440. package/src/helpers/peerIdFromPeerDescriptor.ts +0 -20
  441. package/test/integration/MigrateData.test.ts +0 -204
  442. package/test/unit/Finder.test.ts +0 -110
  443. package/test/unit/PeerID.test.ts +0 -22
  444. package/test/unit/UUID.test.ts +0 -55
@@ -0,0 +1,34 @@
1
+ import { Logger } from '@streamr/utils'
2
+ import { PeerDescriptor, RouteMessageAck, RouteMessageError, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
3
+ import { IRecursiveOperationRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
4
+ import { createRouteMessageAck } from '../routing/RouterRpcLocal'
5
+ import { getPreviousPeer } from '../routing/getPreviousPeer'
6
+ import { getNodeIdFromPeerDescriptor } from '../../identifiers'
7
+
8
+ const logger = new Logger(module)
9
+
10
+ interface RecursiveOperationRpcLocalConfig {
11
+ doRouteRequest: (routedMessage: RouteMessageWrapper) => RouteMessageAck
12
+ addContact: (contact: PeerDescriptor, setActive?: boolean) => void
13
+ isMostLikelyDuplicate: (requestId: string) => boolean
14
+ addToDuplicateDetector: (requestId: string) => void
15
+ }
16
+
17
+ export class RecursiveOperationRpcLocal implements IRecursiveOperationRpc {
18
+
19
+ private readonly config: RecursiveOperationRpcLocalConfig
20
+
21
+ constructor(config: RecursiveOperationRpcLocalConfig) {
22
+ this.config = config
23
+ }
24
+
25
+ async routeRequest(routedMessage: RouteMessageWrapper): Promise<RouteMessageAck> {
26
+ if (this.config.isMostLikelyDuplicate(routedMessage.requestId)) {
27
+ return createRouteMessageAck(routedMessage, RouteMessageError.DUPLICATE)
28
+ }
29
+ const senderId = getNodeIdFromPeerDescriptor(getPreviousPeer(routedMessage) ?? routedMessage.sourcePeer!)
30
+ logger.trace(`Received routeRequest call from ${senderId}`)
31
+ this.config.addToDuplicateDetector(routedMessage.requestId)
32
+ return this.config.doRouteRequest(routedMessage)
33
+ }
34
+ }
@@ -0,0 +1,43 @@
1
+ import { Logger } from '@streamr/utils'
2
+ import { v4 } from 'uuid'
3
+ import { RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
4
+ import { RecursiveOperationRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
5
+ import { RpcRemote } from '../contact/RpcRemote'
6
+ import { getPreviousPeer } from '../routing/getPreviousPeer'
7
+ import { getNodeIdFromPeerDescriptor } from '../../identifiers'
8
+
9
+ const logger = new Logger(module)
10
+
11
+ export class RecursiveOperationRpcRemote extends RpcRemote<RecursiveOperationRpcClient> {
12
+
13
+ async routeRequest(params: RouteMessageWrapper): Promise<boolean> {
14
+ const message: RouteMessageWrapper = {
15
+ target: params.target,
16
+ sourcePeer: params.sourcePeer,
17
+ message: params.message,
18
+ requestId: params.requestId ?? v4(),
19
+ reachableThrough: params.reachableThrough ?? [],
20
+ routingPath: params.routingPath,
21
+ parallelRootNodeIds: params.parallelRootNodeIds
22
+ }
23
+ const options = this.formDhtRpcOptions({
24
+ connect: false
25
+ })
26
+ try {
27
+ const ack = await this.getClient().routeRequest(message, options)
28
+ if (ack.error !== undefined) {
29
+ logger.trace('Next hop responded with error ' + ack.error)
30
+ return false
31
+ }
32
+ } catch (err) {
33
+ const previousPeer = getPreviousPeer(params)
34
+ const fromNode = previousPeer
35
+ ? getNodeIdFromPeerDescriptor(previousPeer)
36
+ : getNodeIdFromPeerDescriptor(params.sourcePeer!)
37
+ const toNode = getNodeIdFromPeerDescriptor(this.getPeerDescriptor())
38
+ logger.debug(`Failed to send routeRequest message from ${fromNode} to ${toNode} with: ${err}`)
39
+ return false
40
+ }
41
+ return true
42
+ }
43
+ }
@@ -0,0 +1,232 @@
1
+ import EventEmitter from 'eventemitter3'
2
+ import { v4 } from 'uuid'
3
+ import {
4
+ DataEntry,
5
+ PeerDescriptor,
6
+ RecursiveOperationResponse,
7
+ RecursiveOperation,
8
+ RouteMessageWrapper,
9
+ RouteMessageAck,
10
+ RecursiveOperationRequest,
11
+ Message
12
+ } from '../../proto/packages/dht/protos/DhtRpc'
13
+ import { ITransport } from '../../transport/ITransport'
14
+ import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
15
+ import { Contact } from '../contact/Contact'
16
+ import { SortedContactList } from '../contact/SortedContactList'
17
+ import { RecursiveOperationResult } from './RecursiveOperationManager'
18
+ import { ServiceID } from '../../types/ServiceID'
19
+ import { RecursiveOperationSessionRpcLocal } from './RecursiveOperationSessionRpcLocal'
20
+ import { DhtAddress, getDhtAddressFromRaw, getNodeIdFromPeerDescriptor, getRawFromDhtAddress } from '../../identifiers'
21
+ import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
22
+
23
+ export interface RecursiveOperationSessionEvents {
24
+ completed: () => void
25
+ }
26
+
27
+ export interface RecursiveOperationSessionConfig {
28
+ transport: ITransport
29
+ targetId: DhtAddress
30
+ localPeerDescriptor: PeerDescriptor
31
+ waitedRoutingPathCompletions: number
32
+ operation: RecursiveOperation
33
+ doRouteRequest: (routedMessage: RouteMessageWrapper) => RouteMessageAck
34
+ }
35
+
36
+ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSessionEvents> {
37
+
38
+ private readonly id = v4()
39
+ private readonly rpcCommunicator: ListeningRpcCommunicator
40
+ private results: SortedContactList<Contact>
41
+ private foundData: Map<DhtAddress, DataEntry> = new Map()
42
+ private allKnownHops: Set<DhtAddress> = new Set()
43
+ private reportedHops: Set<DhtAddress> = new Set()
44
+ private timeoutTask?: NodeJS.Timeout
45
+ private completionEventEmitted = false
46
+ private noCloserNodesReceivedCounter = 0
47
+ private readonly noCloserNodesReceivedFrom: Set<DhtAddress> = new Set()
48
+ private readonly config: RecursiveOperationSessionConfig
49
+
50
+ constructor(config: RecursiveOperationSessionConfig) {
51
+ super()
52
+ this.config = config
53
+ this.results = new SortedContactList({
54
+ referenceId: config.targetId,
55
+ maxSize: 10, // TODO use config option or named constant?
56
+ allowToContainReferenceId: true,
57
+ emitEvents: false
58
+ })
59
+ this.rpcCommunicator = new ListeningRpcCommunicator(this.id, config.transport, {
60
+ rpcRequestTimeout: 15000 // TODO use config option or named constant?
61
+ })
62
+ this.registerLocalRpcMethods()
63
+ }
64
+
65
+ private registerLocalRpcMethods() {
66
+ const rpcLocal = new RecursiveOperationSessionRpcLocal({
67
+ onResponseReceived: (
68
+ sourceId: DhtAddress,
69
+ routingPath: PeerDescriptor[],
70
+ nodes: PeerDescriptor[],
71
+ dataEntries: DataEntry[],
72
+ noCloserNodesFound: boolean
73
+ ) => {
74
+ this.onResponseReceived(sourceId, routingPath, nodes, dataEntries, noCloserNodesFound)
75
+ }
76
+ })
77
+ this.rpcCommunicator.registerRpcNotification(RecursiveOperationResponse, 'sendResponse',
78
+ (req: RecursiveOperationResponse, context: ServerCallContext) => rpcLocal.sendResponse(req, context))
79
+ }
80
+
81
+ public start(serviceId: ServiceID): void {
82
+ const routeMessage = this.wrapRequest(serviceId)
83
+ this.config.doRouteRequest(routeMessage)
84
+ }
85
+
86
+ private wrapRequest(serviceId: ServiceID): RouteMessageWrapper {
87
+ const request: RecursiveOperationRequest = {
88
+ sessionId: this.getId(),
89
+ operation: this.config.operation
90
+ }
91
+ const msg: Message = {
92
+ messageId: v4(),
93
+ serviceId,
94
+ body: {
95
+ oneofKind: 'recursiveOperationRequest',
96
+ recursiveOperationRequest: request
97
+ }
98
+ }
99
+ const routeMessage: RouteMessageWrapper = {
100
+ message: msg,
101
+ requestId: v4(),
102
+ target: getRawFromDhtAddress(this.config.targetId),
103
+ sourcePeer: this.config.localPeerDescriptor,
104
+ reachableThrough: [],
105
+ routingPath: [],
106
+ parallelRootNodeIds: []
107
+ }
108
+ return routeMessage
109
+ }
110
+
111
+ private isCompleted(): boolean {
112
+ const unreportedHops: Set<DhtAddress> = new Set(this.allKnownHops)
113
+ this.reportedHops.forEach((id) => {
114
+ unreportedHops.delete(id)
115
+ })
116
+ if (this.noCloserNodesReceivedCounter >= 1 && unreportedHops.size === 0) {
117
+ if (this.config.operation === RecursiveOperation.FETCH_DATA
118
+ && (this.hasNonStaleData() || this.noCloserNodesReceivedCounter >= this.config.waitedRoutingPathCompletions)) {
119
+ return true
120
+ } else if (this.config.operation === RecursiveOperation.FETCH_DATA) {
121
+ return false
122
+ }
123
+ return true
124
+ }
125
+ return false
126
+ }
127
+
128
+ private hasNonStaleData(): boolean {
129
+ return Array.from(this.foundData.values()).some((entry) => entry.stale === false)
130
+ }
131
+
132
+ public onResponseReceived(
133
+ sourceId: DhtAddress,
134
+ routingPath: PeerDescriptor[],
135
+ nodes: PeerDescriptor[],
136
+ dataEntries: DataEntry[],
137
+ noCloserNodesFound: boolean
138
+ ): void {
139
+ this.addKnownHops(routingPath)
140
+ if (routingPath.length >= 1) {
141
+ this.setHopAsReported(routingPath[routingPath.length - 1])
142
+ }
143
+ nodes.forEach((descriptor: PeerDescriptor) => {
144
+ this.results.addContact(new Contact(descriptor))
145
+ })
146
+ this.processFoundData(dataEntries)
147
+ if (noCloserNodesFound || this.noCloserNodesReceivedFrom.has(sourceId)) {
148
+ this.onNoCloserPeersFound(sourceId)
149
+ }
150
+ }
151
+
152
+ private addKnownHops(routingPath: PeerDescriptor[]) {
153
+ const localNodeId = getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
154
+ routingPath.forEach((desc) => {
155
+ const newNodeId = getNodeIdFromPeerDescriptor(desc)
156
+ if (localNodeId !== newNodeId) {
157
+ this.allKnownHops.add(newNodeId)
158
+ }
159
+ })
160
+ }
161
+
162
+ private setHopAsReported(desc: PeerDescriptor) {
163
+ const localNodeId = getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
164
+ const newNodeId = getNodeIdFromPeerDescriptor(desc)
165
+ if (localNodeId !== newNodeId) {
166
+ this.reportedHops.add(newNodeId)
167
+ }
168
+ if (this.isCompleted()) {
169
+ if (!this.completionEventEmitted && this.isCompleted()) {
170
+ if (this.timeoutTask) {
171
+ clearTimeout(this.timeoutTask)
172
+ this.timeoutTask = undefined
173
+ }
174
+ this.emit('completed')
175
+ this.completionEventEmitted = true
176
+ }
177
+ }
178
+ }
179
+
180
+ private processFoundData(dataEntries: DataEntry[]): void {
181
+ dataEntries.forEach((entry) => {
182
+ const creatorNodeId = getDhtAddressFromRaw(entry.creator)
183
+ const existingEntry = this.foundData.get(creatorNodeId)
184
+ if (!existingEntry || existingEntry.createdAt! < entry.createdAt!
185
+ || (existingEntry.createdAt! <= entry.createdAt! && entry.deleted)) {
186
+ this.foundData.set(creatorNodeId, entry)
187
+ }
188
+ })
189
+ }
190
+
191
+ private onNoCloserPeersFound(sourceId: DhtAddress): void {
192
+ this.noCloserNodesReceivedCounter += 1
193
+ this.noCloserNodesReceivedFrom.add(sourceId)
194
+ if (this.isCompleted()) {
195
+ this.emit('completed')
196
+ this.completionEventEmitted = true
197
+ if (this.timeoutTask) {
198
+ clearTimeout(this.timeoutTask)
199
+ this.timeoutTask = undefined
200
+ }
201
+ } else {
202
+ if (!this.timeoutTask && !this.completionEventEmitted) {
203
+ this.timeoutTask = setTimeout(() => {
204
+ if (!this.completionEventEmitted) {
205
+ this.emit('completed')
206
+ this.completionEventEmitted = true
207
+ }
208
+ }, 4000) // TODO use config option or named constant?
209
+ }
210
+ }
211
+ }
212
+
213
+ public getResults(): RecursiveOperationResult {
214
+ return {
215
+ closestNodes: this.results.getAllContacts().map((contact) => contact.getPeerDescriptor()),
216
+ dataEntries: Array.from(this.foundData.values())
217
+ }
218
+ }
219
+
220
+ public getId(): string {
221
+ return this.id
222
+ }
223
+
224
+ public stop(): void {
225
+ if (this.timeoutTask) {
226
+ clearTimeout(this.timeoutTask)
227
+ this.timeoutTask = undefined
228
+ }
229
+ this.rpcCommunicator.destroy()
230
+ this.emit('completed')
231
+ }
232
+ }
@@ -0,0 +1,35 @@
1
+ import { IRecursiveOperationSessionRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
2
+ import { Empty } from '../../proto/google/protobuf/empty'
3
+ import { DataEntry, RecursiveOperationResponse, PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
4
+ import { Logger } from '@streamr/utils'
5
+ import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
6
+ import { DhtCallContext } from '../../rpc-protocol/DhtCallContext'
7
+ import { DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
8
+
9
+ const logger = new Logger(module)
10
+
11
+ interface RecursiveOperationSessionRpcLocalConfig {
12
+ onResponseReceived: (
13
+ sourceId: DhtAddress,
14
+ routingPath: PeerDescriptor[],
15
+ nodes: PeerDescriptor[],
16
+ dataEntries: DataEntry[],
17
+ noCloserNodesFound: boolean
18
+ ) => void
19
+ }
20
+
21
+ export class RecursiveOperationSessionRpcLocal implements IRecursiveOperationSessionRpc {
22
+
23
+ private readonly config: RecursiveOperationSessionRpcLocalConfig
24
+
25
+ constructor(config: RecursiveOperationSessionRpcLocalConfig) {
26
+ this.config = config
27
+ }
28
+
29
+ async sendResponse(report: RecursiveOperationResponse, context: ServerCallContext): Promise<Empty> {
30
+ const sourceId = getNodeIdFromPeerDescriptor((context as DhtCallContext).incomingSourceDescriptor!)
31
+ logger.trace('RecursiveOperationResponse arrived: ' + JSON.stringify(report))
32
+ this.config.onResponseReceived(sourceId, report.routingPath, report.closestConnectedPeers, report.dataEntries, report.noCloserNodesFound)
33
+ return {}
34
+ }
35
+ }
@@ -0,0 +1,30 @@
1
+ import { Logger } from '@streamr/utils'
2
+ import {
3
+ DataEntry,
4
+ PeerDescriptor,
5
+ RecursiveOperationResponse
6
+ } from '../../proto/packages/dht/protos/DhtRpc'
7
+ import { RecursiveOperationSessionRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
8
+ import { RpcRemote } from '../contact/RpcRemote'
9
+
10
+ const logger = new Logger(module)
11
+
12
+ export class RecursiveOperationSessionRpcRemote extends RpcRemote<RecursiveOperationSessionRpcClient> {
13
+
14
+ sendResponse(
15
+ routingPath: PeerDescriptor[],
16
+ closestNodes: PeerDescriptor[],
17
+ dataEntries: DataEntry[],
18
+ noCloserNodesFound: boolean
19
+ ): void {
20
+ const report: RecursiveOperationResponse = {
21
+ routingPath,
22
+ closestConnectedPeers: closestNodes,
23
+ dataEntries,
24
+ noCloserNodesFound
25
+ }
26
+ this.getClient().sendResponse(report, this.formDhtRpcOptions()).catch((_e) => {
27
+ logger.trace('Failed to send RecursiveOperationResponse')
28
+ })
29
+ }
30
+ }
@@ -1,40 +1,28 @@
1
- type QueueEntry = [timestamp: number, value: string]
2
-
3
1
  export class DuplicateDetector {
4
2
 
5
3
  private values: Set<string> = new Set()
6
- private queue: Array<QueueEntry> = []
7
- private maxAge: number
8
- private maxNumberOfValues: number
4
+ private queue: Array<string> = []
5
+ private maxItemCount: number
9
6
 
10
7
  constructor(
11
- maxNumberOfValues: number,
12
- maxAgeInSeconds: number
8
+ maxItemCount: number,
13
9
  ) {
14
- this.maxNumberOfValues = maxNumberOfValues
15
- this.maxAge = maxAgeInSeconds * 1000
10
+ this.maxItemCount = maxItemCount
16
11
  }
17
12
 
18
13
  public add(value: string): void {
19
14
  this.values.add(value)
20
- this.queue.push([Date.now(), value])
21
- this.cleanUp()
15
+ this.queue.push(value)
16
+ if (this.queue.length > this.maxItemCount) {
17
+ const removed = this.queue.shift()!
18
+ this.values.delete(removed)
19
+ }
22
20
  }
23
21
 
24
22
  public isMostLikelyDuplicate(value: string): boolean {
25
23
  return this.values.has(value)
26
24
  }
27
25
 
28
- private cleanUp(): void {
29
- const currentTime = Date.now()
30
-
31
- while (this.queue.length > 0 && (this.queue.length > this.maxNumberOfValues ||
32
- (currentTime - this.queue[0][0]) > this.maxAge)) {
33
- const oldestEntry = this.queue.shift()
34
- this.values.delete(oldestEntry![1])
35
- }
36
- }
37
-
38
26
  public size(): number {
39
27
  return this.values.size
40
28
  }