@streamr/dht 100.0.0-testnet-one.3 → 100.0.0-testnet-two.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 (307) hide show
  1. package/dist/src/connection/ConnectionLockHandler.d.ts +11 -11
  2. package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
  3. package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
  4. package/dist/src/connection/ConnectionLockRpcLocal.js +4 -4
  5. package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
  6. package/dist/src/connection/ConnectionLockRpcRemote.d.ts +3 -5
  7. package/dist/src/connection/ConnectionLockRpcRemote.js +4 -7
  8. package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
  9. package/dist/src/connection/ConnectionManager.d.ts +4 -6
  10. package/dist/src/connection/ConnectionManager.js +48 -46
  11. package/dist/src/connection/ConnectionManager.js.map +1 -1
  12. package/dist/src/connection/ConnectorFacade.d.ts +2 -2
  13. package/dist/src/connection/ConnectorFacade.js +2 -1
  14. package/dist/src/connection/ConnectorFacade.js.map +1 -1
  15. package/dist/src/connection/ManagedConnection.d.ts +3 -6
  16. package/dist/src/connection/ManagedConnection.js +6 -10
  17. package/dist/src/connection/ManagedConnection.js.map +1 -1
  18. package/dist/src/connection/connectivityChecker.js +1 -0
  19. package/dist/src/connection/connectivityChecker.js.map +1 -1
  20. package/dist/src/connection/simulator/Simulator.d.ts +0 -3
  21. package/dist/src/connection/simulator/Simulator.js +3 -51
  22. package/dist/src/connection/simulator/Simulator.js.map +1 -1
  23. package/dist/src/connection/simulator/SimulatorConnector.js +5 -5
  24. package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
  25. package/dist/src/connection/webrtc/NodeWebrtcConnection.js +2 -2
  26. package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
  27. package/dist/src/connection/webrtc/WebrtcConnector.d.ts +0 -1
  28. package/dist/src/connection/webrtc/WebrtcConnector.js +7 -9
  29. package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
  30. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +2 -2
  31. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +11 -12
  32. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
  33. package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +2 -5
  34. package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +1 -4
  35. package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
  36. package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +1 -2
  37. package/dist/src/connection/websocket/AutoCertifierClientFacade.js +3 -4
  38. package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
  39. package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
  40. package/dist/src/connection/websocket/WebsocketConnector.d.ts +2 -7
  41. package/dist/src/connection/websocket/WebsocketConnector.js +32 -38
  42. package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
  43. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +3 -2
  44. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +8 -10
  45. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
  46. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +3 -6
  47. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +0 -3
  48. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
  49. package/dist/src/connection/websocket/WebsocketServer.d.ts +1 -4
  50. package/dist/src/connection/websocket/WebsocketServer.js +11 -12
  51. package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
  52. package/dist/src/dht/DhtNode.d.ts +10 -13
  53. package/dist/src/dht/DhtNode.js +39 -52
  54. package/dist/src/dht/DhtNode.js.map +1 -1
  55. package/dist/src/dht/DhtNodeRpcLocal.d.ts +0 -1
  56. package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
  57. package/dist/src/dht/DhtNodeRpcRemote.d.ts +7 -7
  58. package/dist/src/dht/DhtNodeRpcRemote.js +8 -7
  59. package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
  60. package/dist/src/dht/ExternalApiRpcLocal.d.ts +5 -4
  61. package/dist/src/dht/ExternalApiRpcLocal.js +3 -2
  62. package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
  63. package/dist/src/dht/ExternalApiRpcRemote.d.ts +2 -2
  64. package/dist/src/dht/ExternalApiRpcRemote.js +2 -0
  65. package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
  66. package/dist/src/dht/PeerManager.d.ts +13 -14
  67. package/dist/src/dht/PeerManager.js +45 -60
  68. package/dist/src/dht/PeerManager.js.map +1 -1
  69. package/dist/src/dht/contact/Contact.d.ts +2 -2
  70. package/dist/src/dht/contact/Contact.js +2 -2
  71. package/dist/src/dht/contact/Contact.js.map +1 -1
  72. package/dist/src/dht/contact/ContactList.d.ts +7 -7
  73. package/dist/src/dht/contact/ContactList.js +3 -3
  74. package/dist/src/dht/contact/ContactList.js.map +1 -1
  75. package/dist/src/dht/contact/RandomContactList.d.ts +4 -4
  76. package/dist/src/dht/contact/RandomContactList.js +12 -11
  77. package/dist/src/dht/contact/RandomContactList.js.map +1 -1
  78. package/dist/src/dht/contact/RpcRemote.d.ts +7 -4
  79. package/dist/src/dht/contact/RpcRemote.js +5 -2
  80. package/dist/src/dht/contact/RpcRemote.js.map +1 -1
  81. package/dist/src/dht/contact/SortedContactList.d.ts +13 -13
  82. package/dist/src/dht/contact/SortedContactList.js +33 -29
  83. package/dist/src/dht/contact/SortedContactList.js.map +1 -1
  84. package/dist/src/dht/discovery/DiscoverySession.d.ts +2 -4
  85. package/dist/src/dht/discovery/DiscoverySession.js +19 -20
  86. package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
  87. package/dist/src/dht/discovery/PeerDiscovery.d.ts +2 -5
  88. package/dist/src/dht/discovery/PeerDiscovery.js +12 -20
  89. package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
  90. package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +36 -0
  91. package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +165 -0
  92. package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -0
  93. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +14 -0
  94. package/dist/src/dht/{find/FindRpcLocal.js → recursive-operation/RecursiveOperationRpcLocal.js} +7 -7
  95. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -0
  96. package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.d.ts +6 -0
  97. package/dist/src/dht/{routing/FindRpcRemote.js → recursive-operation/RecursiveOperationRpcRemote.js} +11 -11
  98. package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -0
  99. package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +42 -0
  100. package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +182 -0
  101. package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -0
  102. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +12 -0
  103. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +17 -0
  104. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -0
  105. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.d.ts +6 -0
  106. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js +21 -0
  107. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js.map +1 -0
  108. package/dist/src/dht/routing/Router.d.ts +4 -19
  109. package/dist/src/dht/routing/Router.js +41 -33
  110. package/dist/src/dht/routing/Router.js.map +1 -1
  111. package/dist/src/dht/routing/RouterRpcLocal.js +6 -5
  112. package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
  113. package/dist/src/dht/routing/RouterRpcRemote.d.ts +2 -2
  114. package/dist/src/dht/routing/RouterRpcRemote.js +7 -7
  115. package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
  116. package/dist/src/dht/routing/RoutingSession.d.ts +20 -16
  117. package/dist/src/dht/routing/RoutingSession.js +37 -45
  118. package/dist/src/dht/routing/RoutingSession.js.map +1 -1
  119. package/dist/src/dht/store/LocalDataStore.d.ts +11 -13
  120. package/dist/src/dht/store/LocalDataStore.js +36 -56
  121. package/dist/src/dht/store/LocalDataStore.js.map +1 -1
  122. package/dist/src/dht/store/StoreManager.d.ts +33 -0
  123. package/dist/src/dht/store/StoreManager.js +186 -0
  124. package/dist/src/dht/store/StoreManager.js.map +1 -0
  125. package/dist/src/dht/store/StoreRpcLocal.d.ts +8 -40
  126. package/dist/src/dht/store/StoreRpcLocal.js +13 -229
  127. package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
  128. package/dist/src/dht/store/StoreRpcRemote.d.ts +5 -5
  129. package/dist/src/dht/store/StoreRpcRemote.js +4 -5
  130. package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
  131. package/dist/src/exports.d.ts +2 -0
  132. package/dist/src/exports.js.map +1 -1
  133. package/dist/src/helpers/MapWithTtl.d.ts +14 -0
  134. package/dist/src/helpers/MapWithTtl.js +59 -0
  135. package/dist/src/helpers/MapWithTtl.js.map +1 -0
  136. package/dist/src/helpers/PeerID.d.ts +2 -0
  137. package/dist/src/helpers/PeerID.js +4 -0
  138. package/dist/src/helpers/PeerID.js.map +1 -1
  139. package/dist/src/helpers/nodeId.d.ts +5 -0
  140. package/dist/src/helpers/nodeId.js +18 -1
  141. package/dist/src/helpers/nodeId.js.map +1 -1
  142. package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +2 -1
  143. package/dist/src/helpers/peerIdFromPeerDescriptor.js +2 -1
  144. package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
  145. package/dist/src/helpers/protoClasses.js +2 -3
  146. package/dist/src/helpers/protoClasses.js.map +1 -1
  147. package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +21 -22
  148. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +18 -18
  149. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
  150. package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +36 -60
  151. package/dist/src/proto/packages/dht/protos/DhtRpc.js +34 -53
  152. package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
  153. package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +11 -12
  154. package/dist/src/rpc-protocol/DhtCallContext.d.ts +2 -1
  155. package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -1
  156. package/dist/src/rpc-protocol/DhtRpcOptions.d.ts +2 -1
  157. package/dist/src/transport/ITransport.d.ts +9 -1
  158. package/dist/src/transport/ITransport.js +5 -0
  159. package/dist/src/transport/ITransport.js.map +1 -1
  160. package/dist/src/transport/ListeningRpcCommunicator.js +1 -1
  161. package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
  162. package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -1
  163. package/dist/src/transport/RoutingRpcCommunicator.js +16 -11
  164. package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
  165. package/jest.config.js +4 -1
  166. package/karma-setup.js +2 -0
  167. package/karma.config.js +11 -7
  168. package/package.json +6 -7
  169. package/protos/DhtRpc.proto +17 -23
  170. package/src/connection/ConnectionLockHandler.ts +15 -15
  171. package/src/connection/ConnectionLockRpcLocal.ts +8 -9
  172. package/src/connection/ConnectionLockRpcRemote.ts +7 -16
  173. package/src/connection/ConnectionManager.ts +58 -55
  174. package/src/connection/ConnectorFacade.ts +3 -0
  175. package/src/connection/ManagedConnection.ts +12 -30
  176. package/src/connection/connectivityChecker.ts +1 -0
  177. package/src/connection/simulator/Simulator.ts +5 -36
  178. package/src/connection/simulator/SimulatorConnector.ts +8 -8
  179. package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -3
  180. package/src/connection/webrtc/NodeWebrtcConnection.ts +3 -4
  181. package/src/connection/webrtc/WebrtcConnector.ts +11 -13
  182. package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +16 -16
  183. package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +3 -13
  184. package/src/connection/websocket/AutoCertifierClientFacade.ts +4 -6
  185. package/src/connection/websocket/ClientWebsocket.ts +1 -0
  186. package/src/connection/websocket/WebsocketConnector.ts +43 -56
  187. package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +11 -13
  188. package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +4 -15
  189. package/src/connection/websocket/WebsocketServer.ts +14 -17
  190. package/src/dht/DhtNode.ts +67 -69
  191. package/src/dht/DhtNodeRpcLocal.ts +0 -1
  192. package/src/dht/DhtNodeRpcRemote.ts +13 -13
  193. package/src/dht/ExternalApiRpcLocal.ts +16 -6
  194. package/src/dht/ExternalApiRpcRemote.ts +5 -3
  195. package/src/dht/PeerManager.ts +73 -90
  196. package/src/dht/contact/Contact.ts +5 -4
  197. package/src/dht/contact/ContactList.ts +9 -9
  198. package/src/dht/contact/RandomContactList.ts +14 -14
  199. package/src/dht/contact/RpcRemote.ts +10 -7
  200. package/src/dht/contact/SortedContactList.ts +48 -45
  201. package/src/dht/discovery/DiscoverySession.ts +25 -25
  202. package/src/dht/discovery/PeerDiscovery.ts +16 -37
  203. package/src/dht/recursive-operation/RecursiveOperationManager.ts +240 -0
  204. package/src/dht/{find/FindRpcLocal.ts → recursive-operation/RecursiveOperationRpcLocal.ts} +9 -9
  205. package/src/dht/{routing/FindRpcRemote.ts → recursive-operation/RecursiveOperationRpcRemote.ts} +10 -10
  206. package/src/dht/recursive-operation/RecursiveOperationSession.ts +224 -0
  207. package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +25 -0
  208. package/src/dht/{find/FindSessionRpcRemote.ts → recursive-operation/RecursiveOperationSessionRpcRemote.ts} +8 -8
  209. package/src/dht/routing/Router.ts +45 -64
  210. package/src/dht/routing/RouterRpcLocal.ts +7 -6
  211. package/src/dht/routing/RouterRpcRemote.ts +9 -10
  212. package/src/dht/routing/RoutingSession.ts +59 -68
  213. package/src/dht/store/LocalDataStore.ts +47 -66
  214. package/src/dht/store/StoreManager.ts +217 -0
  215. package/src/dht/store/StoreRpcLocal.ts +26 -302
  216. package/src/dht/store/StoreRpcRemote.ts +9 -11
  217. package/src/exports.ts +2 -0
  218. package/src/helpers/MapWithTtl.ts +71 -0
  219. package/src/helpers/PeerID.ts +5 -0
  220. package/src/helpers/nodeId.ts +20 -0
  221. package/src/helpers/peerIdFromPeerDescriptor.ts +5 -3
  222. package/src/helpers/protoClasses.ts +4 -6
  223. package/src/proto/packages/dht/protos/DhtRpc.client.ts +31 -32
  224. package/src/proto/packages/dht/protos/DhtRpc.server.ts +11 -12
  225. package/src/proto/packages/dht/protos/DhtRpc.ts +49 -80
  226. package/src/rpc-protocol/DhtCallContext.ts +2 -1
  227. package/src/rpc-protocol/DhtRpcOptions.ts +2 -1
  228. package/src/transport/ITransport.ts +10 -1
  229. package/src/transport/ListeningRpcCommunicator.ts +1 -1
  230. package/src/transport/RoutingRpcCommunicator.ts +18 -12
  231. package/test/benchmark/Find.test.ts +8 -21
  232. package/test/benchmark/KademliaCorrectness.test.ts +11 -20
  233. package/test/benchmark/SortedContactListBenchmark.test.ts +9 -9
  234. package/test/benchmark/kademlia-simulation/Contact.ts +9 -8
  235. package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +2 -2
  236. package/test/benchmark/kademlia-simulation/SimulationNode.ts +17 -17
  237. package/test/end-to-end/Layer0-Layer1.test.ts +10 -14
  238. package/test/end-to-end/Layer0.test.ts +15 -18
  239. package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +7 -9
  240. package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +4 -10
  241. package/test/end-to-end/Layer0Webrtc.test.ts +5 -6
  242. package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +8 -16
  243. package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +6 -13
  244. package/test/end-to-end/WebsocketConnectionRequest.test.ts +5 -6
  245. package/test/end-to-end/memory-leak.test.ts +6 -8
  246. package/test/integration/ConnectionLocking.test.ts +6 -13
  247. package/test/integration/ConnectionManager.test.ts +26 -43
  248. package/test/integration/DhtJoinPeerDiscovery.test.ts +5 -10
  249. package/test/integration/DhtNodeExternalAPI.test.ts +16 -22
  250. package/test/integration/DhtNodeRpcRemote.test.ts +13 -22
  251. package/test/integration/DhtRpc.test.ts +13 -18
  252. package/test/integration/Find.test.ts +10 -10
  253. package/test/integration/Layer1-scale.test.ts +17 -28
  254. package/test/integration/Mock-Layer1-Layer0.test.ts +25 -43
  255. package/test/integration/MultipleEntryPointJoining.test.ts +7 -7
  256. package/test/integration/ReplicateData.test.ts +28 -30
  257. package/test/integration/RouteMessage.test.ts +30 -27
  258. package/test/integration/RouterRpcRemote.test.ts +12 -18
  259. package/test/integration/RpcErrors.test.ts +8 -21
  260. package/test/integration/ScaleDownDht.test.ts +11 -10
  261. package/test/integration/SimultaneousConnections.test.ts +43 -67
  262. package/test/integration/Store.test.ts +23 -36
  263. package/test/integration/StoreAndDelete.test.ts +30 -50
  264. package/test/integration/StoreOnDhtWithTwoNodes.test.ts +16 -37
  265. package/test/integration/StoreRpcRemote.test.ts +20 -30
  266. package/test/integration/WebrtcConnectionManagement.test.ts +3 -13
  267. package/test/integration/WebrtcConnectorRpc.test.ts +3 -9
  268. package/test/integration/WebsocketConnectionManagement.test.ts +7 -10
  269. package/test/integration/WebsocketConnectorRpc.test.ts +8 -17
  270. package/test/unit/ConnectivityHelpers.test.ts +9 -17
  271. package/test/unit/DuplicateDetector.test.ts +7 -4
  272. package/test/unit/LocalDataStore.test.ts +66 -77
  273. package/test/unit/PeerManager.test.ts +31 -0
  274. package/test/unit/RandomContactList.test.ts +13 -8
  275. package/test/unit/{Finder.test.ts → RecursiveOperationManager.test.ts} +38 -43
  276. package/test/unit/RecursiveOperationSession.test.ts +68 -0
  277. package/test/unit/Router.test.ts +15 -22
  278. package/test/unit/RoutingSession.test.ts +23 -25
  279. package/test/unit/SortedContactList.test.ts +41 -26
  280. package/test/unit/StoreManager.test.ts +132 -0
  281. package/test/unit/WebsocketConnector.test.ts +27 -35
  282. package/test/unit/customMatchers.test.ts +16 -0
  283. package/test/utils/FakeTransport.ts +44 -0
  284. package/test/utils/customMatchers.ts +70 -0
  285. package/test/utils/mock/MockRpcCommunicator.ts +7 -0
  286. package/test/utils/mock/Router.ts +3 -2
  287. package/test/utils/mock/mockDataEntry.ts +36 -0
  288. package/test/utils/utils.ts +49 -87
  289. package/dist/src/dht/find/FindRpcLocal.d.ts +0 -14
  290. package/dist/src/dht/find/FindRpcLocal.js.map +0 -1
  291. package/dist/src/dht/find/FindSession.d.ts +0 -44
  292. package/dist/src/dht/find/FindSession.js +0 -150
  293. package/dist/src/dht/find/FindSession.js.map +0 -1
  294. package/dist/src/dht/find/FindSessionRpcLocal.d.ts +0 -12
  295. package/dist/src/dht/find/FindSessionRpcLocal.js +0 -17
  296. package/dist/src/dht/find/FindSessionRpcLocal.js.map +0 -1
  297. package/dist/src/dht/find/FindSessionRpcRemote.d.ts +0 -6
  298. package/dist/src/dht/find/FindSessionRpcRemote.js +0 -21
  299. package/dist/src/dht/find/FindSessionRpcRemote.js.map +0 -1
  300. package/dist/src/dht/find/Finder.d.ts +0 -49
  301. package/dist/src/dht/find/Finder.js +0 -197
  302. package/dist/src/dht/find/Finder.js.map +0 -1
  303. package/dist/src/dht/routing/FindRpcRemote.d.ts +0 -6
  304. package/dist/src/dht/routing/FindRpcRemote.js.map +0 -1
  305. package/src/dht/find/FindSession.ts +0 -183
  306. package/src/dht/find/FindSessionRpcLocal.ts +0 -25
  307. package/src/dht/find/Finder.ts +0 -295
@@ -1,115 +1,96 @@
1
1
  import { PeerID, PeerIDKey } from '../../helpers/PeerID'
2
- import { DataEntry, PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
3
- import { keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
2
+ import { DataEntry } from '../../proto/packages/dht/protos/DhtRpc'
3
+ import { MapWithTtl } from '../../helpers/MapWithTtl'
4
+ import { NodeID, getNodeIdFromBinary } from '../../helpers/nodeId'
4
5
 
5
- const MIN_TTL = 1 * 1000
6
- const MAX_TTL = 300 * 1000
6
+ type Key = Uint8Array
7
7
 
8
- const createTtlValue = (ttl: number): number => {
9
- if (ttl < MIN_TTL) {
10
- return MIN_TTL
11
- } else if (ttl > MAX_TTL) {
12
- return MAX_TTL
13
- } else {
14
- return ttl
15
- }
16
- }
8
+ export class LocalDataStore {
17
9
 
18
- interface LocalDataEntry {
19
- dataEntry: DataEntry
20
- ttlTimeout: NodeJS.Timeout
21
- }
10
+ private readonly maxTtl: number
11
+
12
+ constructor(maxTtl: number) {
13
+ this.maxTtl = maxTtl
14
+ }
22
15
 
23
- export class LocalDataStore {
24
16
  // A map into which each node can store one value per data key
25
17
  // The first key is the key of the data, the second key is the
26
- // PeerID of the creator of the data
27
- private store: Map<PeerIDKey, Map<PeerIDKey, LocalDataEntry>> = new Map()
18
+ // NodeID of the creator of the data
19
+ private store: Map<PeerIDKey, MapWithTtl<NodeID, DataEntry>> = new Map()
28
20
 
29
21
  public storeEntry(dataEntry: DataEntry): boolean {
30
- const publisherKey = PeerID.fromValue(dataEntry.creator!.nodeId).toKey()
31
22
  const dataKey = PeerID.fromValue(dataEntry.key).toKey()
32
-
23
+ const creatorNodeId = getNodeIdFromBinary(dataEntry.creator)
33
24
  if (!this.store.has(dataKey)) {
34
- this.store.set(dataKey, new Map())
25
+ this.store.set(dataKey, new MapWithTtl((e) => Math.min(e.ttl, this.maxTtl)))
35
26
  }
36
-
37
- if (this.store.get(dataKey)!.has(publisherKey)) {
27
+ if (this.store.get(dataKey)!.has(creatorNodeId)) {
38
28
  const storedMillis = (dataEntry.createdAt!.seconds * 1000) + (dataEntry.createdAt!.nanos / 1000000)
39
- const oldLocalEntry = this.store.get(dataKey)!.get(publisherKey)!
40
- const oldStoredMillis = (oldLocalEntry.dataEntry.createdAt!.seconds * 1000) + (oldLocalEntry.dataEntry.createdAt!.nanos / 1000000)
41
-
29
+ const oldLocalEntry = this.store.get(dataKey)!.get(creatorNodeId)!
30
+ const oldStoredMillis = (oldLocalEntry.createdAt!.seconds * 1000) + (oldLocalEntry.createdAt!.nanos / 1000000)
42
31
  // do nothing if old entry is newer than the one being replicated
43
32
  if (oldStoredMillis >= storedMillis) {
44
33
  return false
45
- } else {
46
- clearTimeout(oldLocalEntry.ttlTimeout)
47
34
  }
48
35
  }
49
- this.store.get(dataKey)!.set(publisherKey, {
50
- dataEntry,
51
- ttlTimeout: setTimeout(() => {
52
- this.deleteEntry(PeerID.fromValue(dataEntry.key), dataEntry.creator!)
53
- }, createTtlValue(dataEntry.ttl))
54
- })
36
+ this.store.get(dataKey)!.set(creatorNodeId, dataEntry)
55
37
  return true
56
38
  }
57
39
 
58
- public markAsDeleted(id: Uint8Array, creator: PeerID): boolean {
59
- const dataKey = PeerID.fromValue(id).toKey()
40
+ public markAsDeleted(key: Key, creator: NodeID): boolean {
41
+ const dataKey = PeerID.fromValue(key).toKey()
60
42
  const item = this.store.get(dataKey)
61
- if ((item === undefined) || !item.has(creator.toKey())) {
43
+ if ((item === undefined) || !item.has(creator)) {
62
44
  return false
63
45
  }
64
- const storedEntry = item.get(creator.toKey())
65
- storedEntry!.dataEntry.deleted = true
46
+ const storedEntry = item.get(creator)
47
+ storedEntry!.deleted = true
66
48
  return true
67
49
  }
68
50
 
69
- public getStore(): Map<PeerIDKey, Map<PeerIDKey, LocalDataEntry>> {
70
- return this.store
51
+ public* values(): IterableIterator<DataEntry> {
52
+ for (const v of this.store.values()) {
53
+ yield* v.values()
54
+ }
71
55
  }
72
56
 
73
- public getEntry(key: PeerID): Map<PeerIDKey, DataEntry> {
74
- const dataEntries = new Map<PeerIDKey, DataEntry>
75
- this.store.get(key.toKey())?.forEach((value, key) => {
76
- dataEntries.set(key, value.dataEntry)
57
+ public getEntries(key: Key): Map<NodeID, DataEntry> {
58
+ const dataEntries = new Map<NodeID, DataEntry>
59
+ const mapKey = PeerID.fromValue(key).toKey()
60
+ this.store.get(mapKey)?.forEach((value, key) => {
61
+ dataEntries.set(key, value)
77
62
  })
78
63
  return dataEntries
79
64
  }
80
65
 
81
- public setStale(key: PeerID, creator: PeerDescriptor, stale: boolean): void {
82
- const creatorKey = keyFromPeerDescriptor(creator)
83
- const storedEntry = this.store.get(key.toKey())?.get(creatorKey)
66
+ public setStale(key: Key, creator: NodeID, stale: boolean): void {
67
+ const mapKey = PeerID.fromValue(key).toKey()
68
+ const storedEntry = this.store.get(mapKey)?.get(creator)
84
69
  if (storedEntry) {
85
- storedEntry.dataEntry.stale = stale
70
+ storedEntry.stale = stale
86
71
  }
87
72
  }
88
73
 
89
- public setAllEntriesAsStale(key: PeerID): void {
90
- this.store.get(key.toKey())?.forEach((value) => {
91
- value.dataEntry.stale = true
74
+ public setAllEntriesAsStale(key: Key): void {
75
+ const mapKey = PeerID.fromValue(key).toKey()
76
+ this.store.get(mapKey)?.forEach((value) => {
77
+ value.stale = true
92
78
  })
93
79
  }
94
80
 
95
- public deleteEntry(key: PeerID, creator: PeerDescriptor): void {
96
- const creatorKey = keyFromPeerDescriptor(creator)
97
- const storedEntry = this.store.get(key.toKey())?.get(creatorKey)
81
+ public deleteEntry(key: Key, creator: NodeID): void {
82
+ const mapKey = PeerID.fromValue(key).toKey()
83
+ const storedEntry = this.store.get(mapKey)?.get(creator)
98
84
  if (storedEntry) {
99
- clearTimeout(storedEntry.ttlTimeout)
100
- this.store.get(key.toKey())?.delete(creatorKey)
101
- if (this.store.get(key.toKey())?.size === 0) {
102
- this.store.delete(key.toKey())
85
+ this.store.get(mapKey)?.delete(creator)
86
+ if (this.store.get(mapKey)?.size() === 0) {
87
+ this.store.delete(mapKey)
103
88
  }
104
89
  }
105
90
  }
106
91
 
107
92
  public clear(): void {
108
- this.store.forEach((value) => {
109
- value.forEach((value) => {
110
- clearTimeout(value.ttlTimeout)
111
- })
112
- })
93
+ this.store.forEach((value) => value.clear())
113
94
  this.store.clear()
114
95
  }
115
96
  }
@@ -0,0 +1,217 @@
1
+ import {
2
+ DataEntry, ReplicateDataRequest, PeerDescriptor,
3
+ StoreDataRequest, StoreDataResponse, RecursiveOperation
4
+ } from '../../proto/packages/dht/protos/DhtRpc'
5
+ import { Any } from '../../proto/google/protobuf/any'
6
+ import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
7
+ import { RoutingRpcCommunicator } from '../../transport/RoutingRpcCommunicator'
8
+ import { RecursiveOperationManager } from '../recursive-operation/RecursiveOperationManager'
9
+ import { areEqualPeerDescriptors, getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
10
+ import { Logger, executeSafePromise, hexToBinary } from '@streamr/utils'
11
+ import { LocalDataStore } from './LocalDataStore'
12
+ import { StoreRpcRemote } from './StoreRpcRemote'
13
+ import { Timestamp } from '../../proto/google/protobuf/timestamp'
14
+ import { SortedContactList } from '../contact/SortedContactList'
15
+ import { Contact } from '../contact/Contact'
16
+ import { ServiceID } from '../../types/ServiceID'
17
+ import { findIndex } from 'lodash'
18
+ import { NodeID, areEqualNodeIds, getNodeIdFromBinary, getNodeIdFromDataKey } from '../../helpers/nodeId'
19
+ import { StoreRpcLocal } from './StoreRpcLocal'
20
+ import { getDistance } from '../PeerManager'
21
+
22
+ interface StoreManagerConfig {
23
+ rpcCommunicator: RoutingRpcCommunicator
24
+ recursiveOperationManager: RecursiveOperationManager
25
+ localPeerDescriptor: PeerDescriptor
26
+ localDataStore: LocalDataStore
27
+ serviceId: ServiceID
28
+ highestTtl: number
29
+ redundancyFactor: number
30
+ getClosestNeighborsTo: (id: Uint8Array, n?: number) => PeerDescriptor[]
31
+ createRpcRemote: (contact: PeerDescriptor) => StoreRpcRemote
32
+ }
33
+
34
+ const logger = new Logger(module)
35
+
36
+ export class StoreManager {
37
+
38
+ private readonly config: StoreManagerConfig
39
+
40
+ constructor(config: StoreManagerConfig) {
41
+ this.config = config
42
+ this.registerLocalRpcMethods()
43
+ }
44
+
45
+ private registerLocalRpcMethods() {
46
+ const rpcLocal = new StoreRpcLocal({
47
+ localDataStore: this.config.localDataStore,
48
+ replicateDataToNeighbors: (incomingPeer: PeerDescriptor, dataEntry: DataEntry) => this.replicateDataToNeighbors(incomingPeer, dataEntry),
49
+ selfIsWithinRedundancyFactor: (key: Uint8Array): boolean => this.selfIsWithinRedundancyFactor(key)
50
+ })
51
+ this.config.rpcCommunicator.registerRpcMethod(StoreDataRequest, StoreDataResponse, 'storeData',
52
+ (request: StoreDataRequest) => rpcLocal.storeData(request))
53
+ this.config.rpcCommunicator.registerRpcNotification(ReplicateDataRequest, 'replicateData',
54
+ (request: ReplicateDataRequest, context: ServerCallContext) => rpcLocal.replicateData(request, context))
55
+ }
56
+
57
+ onNewContact(peerDescriptor: PeerDescriptor): void {
58
+ for (const dataEntry of this.config.localDataStore.values()) {
59
+ this.replicateAndUpdateStaleState(dataEntry, peerDescriptor)
60
+ }
61
+ }
62
+
63
+ private replicateAndUpdateStaleState(dataEntry: DataEntry, newNode: PeerDescriptor): void {
64
+ const newNodeId = getNodeIdFromPeerDescriptor(newNode)
65
+ // TODO use config option or named constant?
66
+ const closestToData = this.config.getClosestNeighborsTo(dataEntry.key, 10)
67
+ const sortedList = new SortedContactList<Contact>({
68
+ referenceId: getNodeIdFromDataKey(dataEntry.key),
69
+ maxSize: 20, // TODO use config option or named constant?
70
+ allowToContainReferenceId: true,
71
+ emitEvents: false
72
+ })
73
+ sortedList.addContact(new Contact(this.config.localPeerDescriptor))
74
+ closestToData.forEach((neighbor) => {
75
+ if (!areEqualNodeIds(newNodeId, getNodeIdFromPeerDescriptor(neighbor))) {
76
+ sortedList.addContact(new Contact(neighbor))
77
+ }
78
+ })
79
+ const selfIsPrimaryStorer = areEqualNodeIds(
80
+ sortedList.getClosestContactId(),
81
+ getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
82
+ )
83
+ if (selfIsPrimaryStorer) {
84
+ sortedList.addContact(new Contact(newNode))
85
+ const sorted = sortedList.getContactIds()
86
+ // findIndex should never return -1 here because we just added the new node to the list
87
+ const index = findIndex(sorted, (nodeId) => areEqualNodeIds(nodeId, newNodeId))
88
+ // if new node is within the storageRedundancyFactor closest nodes to the data
89
+ // do replicate data to it
90
+ if (index < this.config.redundancyFactor) {
91
+ setImmediate(async () => {
92
+ await this.replicateDataToContact(dataEntry, newNode)
93
+ })
94
+ }
95
+ } else if (!this.selfIsWithinRedundancyFactor(dataEntry.key)) {
96
+ this.config.localDataStore.setStale(dataEntry.key, getNodeIdFromBinary(dataEntry.creator), true)
97
+ }
98
+ }
99
+
100
+ private async replicateDataToContact(dataEntry: DataEntry, contact: PeerDescriptor): Promise<void> {
101
+ const rpcRemote = this.config.createRpcRemote(contact)
102
+ try {
103
+ await rpcRemote.replicateData({ entry: dataEntry })
104
+ } catch (e) {
105
+ logger.trace('replicateData() threw an exception ' + e)
106
+ }
107
+ }
108
+
109
+ public async storeDataToDht(key: Uint8Array, data: Any, creator: NodeID): Promise<PeerDescriptor[]> {
110
+ logger.debug(`Storing data to DHT ${this.config.serviceId}`)
111
+ const result = await this.config.recursiveOperationManager.execute(key, RecursiveOperation.FIND_NODE)
112
+ const closestNodes = result.closestNodes
113
+ const successfulNodes: PeerDescriptor[] = []
114
+ const ttl = this.config.highestTtl // ToDo: make TTL decrease according to some nice curve
115
+ const createdAt = Timestamp.now()
116
+ for (let i = 0; i < closestNodes.length && successfulNodes.length < this.config.redundancyFactor; i++) {
117
+ if (areEqualPeerDescriptors(this.config.localPeerDescriptor, closestNodes[i])) {
118
+ this.config.localDataStore.storeEntry({
119
+ key,
120
+ data,
121
+ creator: hexToBinary(creator),
122
+ createdAt,
123
+ storedAt: Timestamp.now(),
124
+ ttl,
125
+ stale: false,
126
+ deleted: false,
127
+ })
128
+ successfulNodes.push(closestNodes[i])
129
+ continue
130
+ }
131
+ const rpcRemote = this.config.createRpcRemote(closestNodes[i])
132
+ try {
133
+ await rpcRemote.storeData({
134
+ key,
135
+ data,
136
+ creator: hexToBinary(creator),
137
+ createdAt,
138
+ ttl
139
+ })
140
+ successfulNodes.push(closestNodes[i])
141
+ logger.trace('remote.storeData() success')
142
+ } catch (e) {
143
+ logger.trace('remote.storeData() threw an exception ' + e)
144
+ }
145
+ }
146
+ return successfulNodes
147
+ }
148
+
149
+ private selfIsWithinRedundancyFactor(dataKey: Uint8Array): boolean {
150
+ const closestNeighbors = this.config.getClosestNeighborsTo(dataKey, this.config.redundancyFactor)
151
+ if (closestNeighbors.length < this.config.redundancyFactor) {
152
+ return true
153
+ } else {
154
+ const localNodeId = getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
155
+ const furthestCloseNeighbor = getNodeIdFromPeerDescriptor(closestNeighbors[closestNeighbors.length - 1])
156
+ const dataId = getNodeIdFromDataKey(dataKey)
157
+ return getDistance(dataId, localNodeId) < getDistance(dataId, furthestCloseNeighbor)
158
+ }
159
+ }
160
+
161
+ private async replicateDataToClosestNodes(): Promise<void> {
162
+ const dataEntries = Array.from(this.config.localDataStore.values())
163
+ await Promise.all(dataEntries.map(async (dataEntry) => {
164
+ const neighbors = this.config.getClosestNeighborsTo(dataEntry.key, this.config.redundancyFactor)
165
+ await Promise.all(neighbors.map(async (neighbor) => {
166
+ const rpcRemote = this.config.createRpcRemote(neighbor)
167
+ try {
168
+ await rpcRemote.replicateData({ entry: dataEntry })
169
+ } catch (err) {
170
+ logger.trace('Failed to replicate data in replicateDataToClosestNodes', { error: err })
171
+ }
172
+ }))
173
+ }))
174
+ }
175
+
176
+ private replicateDataToNeighbors(incomingPeer: PeerDescriptor, dataEntry: DataEntry): void {
177
+ // sort own contact list according to data id
178
+ const localNodeId = getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
179
+ const incomingNodeId = getNodeIdFromPeerDescriptor(incomingPeer)
180
+ // TODO use config option or named constant?
181
+ const closestToData = this.config.getClosestNeighborsTo(dataEntry.key, 10)
182
+ const sortedList = new SortedContactList<Contact>({
183
+ referenceId: getNodeIdFromDataKey(dataEntry.key),
184
+ maxSize: this.config.redundancyFactor,
185
+ allowToContainReferenceId: true,
186
+ emitEvents: false
187
+ })
188
+ sortedList.addContact(new Contact(this.config.localPeerDescriptor))
189
+ closestToData.forEach((neighbor) => {
190
+ sortedList.addContact(new Contact(neighbor))
191
+ })
192
+ const selfIsPrimaryStorer = areEqualNodeIds(sortedList.getClosestContactId(), localNodeId)
193
+ const targets = selfIsPrimaryStorer
194
+ // if we are the closest to the data, replicate to all storageRedundancyFactor nearest
195
+ ? sortedList.getAllContacts()
196
+ // if we are not the closest node to the data, replicate only to the closest one to the data
197
+ : [sortedList.getAllContacts()[0]]
198
+ targets.forEach((contact) => {
199
+ const contactNodeId = getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())
200
+ if (!areEqualNodeIds(incomingNodeId, contactNodeId) && !areEqualNodeIds(localNodeId, contactNodeId)) {
201
+ setImmediate(() => {
202
+ executeSafePromise(async () => {
203
+ await this.replicateDataToContact(dataEntry, contact.getPeerDescriptor())
204
+ logger.trace('replicateDataToContact() returned', {
205
+ node: getNodeIdFromPeerDescriptor(contact.getPeerDescriptor()),
206
+ replicateOnlyToClosest: !selfIsPrimaryStorer
207
+ })
208
+ })
209
+ })
210
+ }
211
+ })
212
+ }
213
+
214
+ async destroy(): Promise<void> {
215
+ await this.replicateDataToClosestNodes()
216
+ }
217
+ }