@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,399 @@
1
+ /* eslint-disable @typescript-eslint/parameter-properties */
2
+ import EventEmitter from 'eventemitter3'
3
+ import { PeerIDKey } from '../../helpers/PeerID'
4
+ import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
5
+ import { ConnectionSourceEvents } from '../IConnectionSource'
6
+ import { SimulatorConnector } from './SimulatorConnector'
7
+ import { SimulatorConnection } from './SimulatorConnection'
8
+ import { ConnectionID } from '../IConnection'
9
+ import { Logger } from '@streamr/utils'
10
+ import { getRegionDelayMatrix } from './pings'
11
+ import { keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
12
+ import Heap from 'heap'
13
+ import { debugVars } from '../../helpers/debugHelpers'
14
+ import * as sinon from 'sinon'
15
+
16
+ const logger = new Logger(module)
17
+
18
+ export enum LatencyType { NONE = 'NONE', RANDOM = 'RANDOM', REAL = 'REAL', FIXED = 'FIXED' }
19
+
20
+ // One-way 'pipe' of messages
21
+
22
+ class Association {
23
+ public sourceConnection: SimulatorConnection
24
+ public destinationConnection?: SimulatorConnection
25
+ private lastOperationAt: number = 0
26
+ private closing = false
27
+
28
+ constructor(sourceConnection: SimulatorConnection,
29
+ destinationConnection?: SimulatorConnection,
30
+ public connectedCallback?: (error?: string) => void
31
+ ) {
32
+ this.sourceConnection = sourceConnection
33
+ this.destinationConnection = destinationConnection
34
+ }
35
+
36
+ public setDestinationConnection(connection: SimulatorConnection) {
37
+ this.destinationConnection = connection
38
+ }
39
+
40
+ public getLastOperationAt(): number {
41
+ return this.lastOperationAt
42
+ }
43
+
44
+ public setLastOperationAt(executionTime: number): void {
45
+ this.lastOperationAt = executionTime
46
+ }
47
+
48
+ public setClosing(): void {
49
+ this.closing = true
50
+ }
51
+
52
+ public isClosing(): boolean {
53
+ return this.closing
54
+ }
55
+ }
56
+
57
+ class SimulatorOperation {
58
+ private static objectCounter = 0
59
+ public objectId = 0
60
+
61
+ constructor(
62
+ public executionTime: number,
63
+ public association: Association
64
+ ) {
65
+ this.objectId = SimulatorOperation.objectCounter
66
+ SimulatorOperation.objectCounter++
67
+ }
68
+ }
69
+
70
+ class ConnectOperation extends SimulatorOperation {
71
+ constructor(
72
+ executionTime: number,
73
+ association: Association,
74
+ public sourceConnection: SimulatorConnection,
75
+ public targetDescriptor: PeerDescriptor) {
76
+
77
+ super(executionTime, association)
78
+ }
79
+ }
80
+
81
+ class SendOperation extends SimulatorOperation {
82
+ constructor(
83
+ executionTime: number,
84
+ association: Association,
85
+ public data: Uint8Array
86
+ ) {
87
+ super(executionTime, association)
88
+ }
89
+ }
90
+
91
+ class CloseOperation extends SimulatorOperation {
92
+ }
93
+
94
+ export class Simulator extends EventEmitter<ConnectionSourceEvents> {
95
+ private stopped = false
96
+ private connectors: Map<PeerIDKey, SimulatorConnector> = new Map()
97
+ private latencyTable?: Array<Array<number>>
98
+ private associations: Map<ConnectionID, Association> = new Map()
99
+
100
+ private latencyType: LatencyType
101
+ private fixedLatency?: number
102
+
103
+ private loopCounter = 0
104
+ private operationCounter = 0
105
+ private MAX_LOOPS = 1000
106
+
107
+ private operationQueue: Heap<SimulatorOperation> = new Heap<SimulatorOperation>((a: SimulatorOperation, b: SimulatorOperation) => {
108
+ if ((a.executionTime - b.executionTime) === 0) {
109
+ return (a.objectId - b.objectId)
110
+ } else {
111
+ return (a.executionTime - b.executionTime)
112
+ }
113
+ })
114
+
115
+ private simulatorTimeout?: NodeJS.Timeout
116
+ private static clock: sinon.SinonFakeTimers | undefined
117
+
118
+ static useFakeTimers(on = true): void {
119
+ if (on) {
120
+ if (!Simulator.clock) {
121
+ Simulator.clock = sinon.useFakeTimers()
122
+ }
123
+ } else {
124
+ if (Simulator.clock) {
125
+ Simulator.clock.restore()
126
+ Simulator.clock = undefined
127
+ }
128
+ }
129
+ }
130
+
131
+ constructor(latencyType: LatencyType = LatencyType.NONE, fixedLatency?: number) {
132
+ super()
133
+
134
+ this.latencyType = latencyType
135
+ this.fixedLatency = fixedLatency
136
+
137
+ if (this.latencyType === LatencyType.REAL) {
138
+ this.latencyTable = getRegionDelayMatrix()
139
+ }
140
+
141
+ if (this.latencyType === LatencyType.FIXED && !this.fixedLatency) {
142
+ throw new Error('LatencyType.FIXED requires the desired latency to be given as second parameter')
143
+ }
144
+
145
+ this.generateExecutionTime = this.generateExecutionTime.bind(this)
146
+ this.getLatency = this.getLatency.bind(this)
147
+ this.executeCloseOperation = this.executeCloseOperation.bind(this)
148
+ this.executeConnectOperation = this.executeConnectOperation.bind(this)
149
+ this.executeSendOperation = this.executeSendOperation.bind(this)
150
+ this.executeQueuedOperations = this.executeQueuedOperations.bind(this)
151
+ this.accept = this.accept.bind(this)
152
+ this.send = this.send.bind(this)
153
+ this.close = this.close.bind(this)
154
+ this.scheduleNextTimeout = this.scheduleNextTimeout.bind(this)
155
+ this.scheduleOperation = this.scheduleOperation.bind(this)
156
+ }
157
+
158
+ private generateExecutionTime(association: Association, sourceRegion: number | undefined, targetRegion: number | undefined): number {
159
+ let executionTime = Date.now() + this.getLatency(sourceRegion, targetRegion)
160
+ if (association.getLastOperationAt() > executionTime) {
161
+ executionTime = association.getLastOperationAt()
162
+ }
163
+
164
+ return executionTime
165
+ }
166
+
167
+ private getLatency(sourceRegion: number | undefined, targetRegion: number | undefined): number {
168
+ let latency: number = 0
169
+
170
+ if (this.latencyType === LatencyType.FIXED) {
171
+ latency = this.fixedLatency!
172
+ }
173
+
174
+ if (this.latencyType === LatencyType.REAL) {
175
+
176
+ if (sourceRegion === undefined || targetRegion === undefined || sourceRegion > 15 || targetRegion > 15) {
177
+ logger.error('invalid region index given to Simulator')
178
+ throw ('invalid region index given to Simulator')
179
+ }
180
+
181
+ latency = this.latencyTable![sourceRegion!][targetRegion!]
182
+ }
183
+ if (this.latencyType === LatencyType.RANDOM) {
184
+ latency = Math.random() * (250 - 5) + 5
185
+ }
186
+
187
+ return latency
188
+ }
189
+
190
+ public accept(sourceConnection: SimulatorConnection, targetConnection: SimulatorConnection): void {
191
+
192
+ const sourceAssociation = this.associations.get(sourceConnection.connectionId)
193
+
194
+ if (!sourceAssociation) {
195
+ logger.error('source association not found in accept()')
196
+ return
197
+ }
198
+ sourceAssociation!.setDestinationConnection(targetConnection)
199
+
200
+ const targetAssociation = new Association(targetConnection, sourceConnection)
201
+ this.associations.set(targetConnection.connectionId, targetAssociation)
202
+
203
+ sourceAssociation.connectedCallback!()
204
+ }
205
+
206
+ public addConnector(connector: SimulatorConnector): void {
207
+ this.connectors.set(keyFromPeerDescriptor(connector.getPeerDescriptor()), connector)
208
+ }
209
+
210
+ private executeConnectOperation(operation: ConnectOperation): void {
211
+ const target = this.connectors.get(keyFromPeerDescriptor(operation.targetDescriptor))
212
+
213
+ if (!target) {
214
+ logger.error('Target connector not found when executing connect operation')
215
+ return operation.association.connectedCallback!('Target connector not found')
216
+ }
217
+
218
+ target!.handleIncomingConnection(operation.sourceConnection)
219
+ }
220
+
221
+ private executeCloseOperation(operation: CloseOperation): void {
222
+
223
+ if (this.stopped) {
224
+ return
225
+ }
226
+
227
+ const target = operation.association.destinationConnection
228
+
229
+ let counterAssociation: Association | undefined
230
+
231
+ if (target) {
232
+ counterAssociation = this.associations.get(target!.connectionId)
233
+ }
234
+
235
+ if (!target || !counterAssociation) {
236
+ this.associations.delete(operation.association.sourceConnection.connectionId)
237
+
238
+ } else if (!counterAssociation!.isClosing()) {
239
+ target!.handleIncomingDisconnection()
240
+ this.close(target!)
241
+ } else {
242
+ // this is the 'ack' of the CloseOperation to the original closer
243
+ this.associations.delete(target!.connectionId)
244
+ this.associations.delete(operation.association.sourceConnection.connectionId)
245
+ }
246
+ }
247
+
248
+ private executeSendOperation(operation: SendOperation): void {
249
+
250
+ if (this.stopped) {
251
+ return
252
+ }
253
+
254
+ const target = operation.association.destinationConnection
255
+ target!.handleIncomingData(operation.data)
256
+
257
+ }
258
+
259
+ private executeQueuedOperations(): void {
260
+ const currentTime = Date.now()
261
+ while (this.operationQueue.size() > 0
262
+ && this.operationQueue.peek()!.executionTime <= currentTime) {
263
+ const operation = this.operationQueue.pop()
264
+
265
+ this.operationCounter++
266
+
267
+ if (operation instanceof ConnectOperation) {
268
+ this.executeConnectOperation(operation)
269
+ } else if (operation instanceof CloseOperation) {
270
+ this.executeCloseOperation(operation)
271
+ } else if (operation instanceof SendOperation) {
272
+ this.executeSendOperation(operation)
273
+ } else {
274
+ logger.error('Unknown SimulatorOperation')
275
+ }
276
+
277
+ this.loopCounter++
278
+ if (this.loopCounter >= this.MAX_LOOPS) {
279
+ this.loopCounter = 0
280
+ setTimeout(() => this.executeQueuedOperations(), 0)
281
+ return
282
+ }
283
+ }
284
+
285
+ this.scheduleNextTimeout()
286
+ }
287
+
288
+ public getOperationCounter(): number {
289
+ return this.operationCounter
290
+ }
291
+
292
+ private scheduleNextTimeout(): void {
293
+ if (this.simulatorTimeout) {
294
+ clearTimeout(this.simulatorTimeout)
295
+ this.simulatorTimeout = undefined
296
+ }
297
+
298
+ const currentTime = Date.now()
299
+
300
+ const firstOperation = this.operationQueue.peek()
301
+
302
+ if (!firstOperation) {
303
+ return
304
+ }
305
+
306
+ const firstOperationTime = firstOperation!.executionTime
307
+ const timeDifference = firstOperationTime - currentTime
308
+
309
+ this.simulatorTimeout = setTimeout(this.executeQueuedOperations, timeDifference)
310
+
311
+ if (Simulator.clock) {
312
+ Simulator.clock.runAllAsync()
313
+ }
314
+ }
315
+
316
+ private scheduleOperation(operation: SimulatorOperation) {
317
+ this.operationQueue.push(operation)
318
+ this.scheduleNextTimeout()
319
+ }
320
+
321
+ public connect(sourceConnection: SimulatorConnection, targetDescriptor: PeerDescriptor, connectedCallback: (error?: string) => void): void {
322
+
323
+ if (this.stopped) {
324
+ logger.error('connect() called on a stopped simulator ' + (new Error().stack))
325
+ return
326
+ }
327
+ debugVars['simulatorHeapSize'] = this.operationQueue.size()
328
+
329
+ const association = new Association(sourceConnection, undefined, connectedCallback)
330
+ this.associations.set(sourceConnection.connectionId, association)
331
+
332
+ const executionTime = this.generateExecutionTime(association, sourceConnection.ownPeerDescriptor.region, targetDescriptor.region)
333
+ association.setLastOperationAt(executionTime)
334
+
335
+ const operation = new ConnectOperation(executionTime, association,
336
+ sourceConnection, targetDescriptor)
337
+
338
+ this.scheduleOperation(operation)
339
+ }
340
+
341
+ public close(sourceConnection: SimulatorConnection): void {
342
+
343
+ if (this.stopped) {
344
+ return
345
+ }
346
+
347
+ const association = this.associations.get(sourceConnection.connectionId)
348
+ if (!association) {
349
+ return
350
+ }
351
+
352
+ association.setClosing()
353
+
354
+ const executionTime = this.generateExecutionTime(association,
355
+ sourceConnection.ownPeerDescriptor.region,
356
+ sourceConnection.getPeerDescriptor()?.region)
357
+ association.setLastOperationAt(executionTime)
358
+
359
+ const operation = new CloseOperation(executionTime, association)
360
+
361
+ this.scheduleOperation(operation)
362
+ }
363
+
364
+ public send(sourceConnection: SimulatorConnection, data: Uint8Array): void {
365
+
366
+ if (this.stopped) {
367
+ return
368
+ }
369
+
370
+ const association = this.associations.get(sourceConnection.connectionId)
371
+ if (!association) {
372
+ return
373
+ }
374
+
375
+ if (association.isClosing()) {
376
+ logger.trace('Tried to call send() on a closing association')
377
+ return
378
+ }
379
+
380
+ const executionTime = this.generateExecutionTime(association,
381
+ sourceConnection.ownPeerDescriptor.region,
382
+ association.destinationConnection!.ownPeerDescriptor.region)
383
+
384
+ association.setLastOperationAt(executionTime)
385
+
386
+ const operation = new SendOperation(executionTime, association, data)
387
+
388
+ this.scheduleOperation(operation)
389
+ }
390
+
391
+ public stop(): void {
392
+ this.stopped = true
393
+ logger.info(this.associations.size + ' associations in the beginning of stop()')
394
+
395
+ if (this.simulatorTimeout) {
396
+ clearTimeout(this.simulatorTimeout)
397
+ }
398
+ }
399
+ }
@@ -0,0 +1,137 @@
1
+ import { ConnectionType, IConnection } from '../IConnection'
2
+ import { Simulator } from './Simulator'
3
+ import { Message, PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
4
+ import { Connection } from '../Connection'
5
+ import { Logger } from '@streamr/utils'
6
+ import { protoToString } from '../../helpers/protoToString'
7
+ import { DisconnectionType } from '../../transport/ITransport'
8
+
9
+ const logger = new Logger(module)
10
+
11
+ export class SimulatorConnection extends Connection implements IConnection {
12
+
13
+ private stopped = false
14
+ public ownPeerDescriptor: PeerDescriptor
15
+ private targetPeerDescriptor: PeerDescriptor
16
+ private simulator: Simulator
17
+
18
+ constructor(
19
+ ownPeerDescriptor: PeerDescriptor,
20
+ targetPeerDescriptor: PeerDescriptor,
21
+ connectionType: ConnectionType,
22
+ simulator: Simulator
23
+ ) {
24
+ super(connectionType)
25
+
26
+ this.ownPeerDescriptor = ownPeerDescriptor
27
+ this.setPeerDescriptor(targetPeerDescriptor)
28
+ this.targetPeerDescriptor = targetPeerDescriptor
29
+ this.connectionType = connectionType
30
+ this.simulator = simulator
31
+
32
+ this.send = this.send.bind(this)
33
+ this.close = this.close.bind(this)
34
+ this.connect = this.connect.bind(this)
35
+ this.handleIncomingData = this.handleIncomingData.bind(this)
36
+ this.handleIncomingDisconnection = this.handleIncomingDisconnection.bind(this)
37
+ this.destroy = this.destroy.bind(this)
38
+ this.doDisconnect = this.doDisconnect.bind(this)
39
+ }
40
+
41
+ public send(data: Uint8Array): void {
42
+ logger.trace('send()')
43
+ if (!this.stopped) {
44
+
45
+ this.simulator.send(this, data)
46
+
47
+ } else {
48
+ logger.error(' ' + this.ownPeerDescriptor.nodeName + ', ' + (this.targetPeerDescriptor.nodeName!) +
49
+ 'tried to send() on a stopped connection')
50
+ }
51
+ }
52
+
53
+ public async close(disconnectionType: DisconnectionType): Promise<void> {
54
+ logger.trace(' ' + this.ownPeerDescriptor.nodeName + ', ' + (this.targetPeerDescriptor.nodeName!) + ' close()')
55
+
56
+ if (!this.stopped) {
57
+ logger.trace(' ' + this.ownPeerDescriptor.nodeName + ', ' + (this.targetPeerDescriptor.nodeName!) + ' close() not stopped')
58
+ this.stopped = true
59
+
60
+ try {
61
+ logger.trace(' ' + this.ownPeerDescriptor.nodeName + ', ' + (this.targetPeerDescriptor.nodeName!) +
62
+ ' close() calling simulator.disconnect()')
63
+ this.simulator.close(this)
64
+ logger.trace(' ' + this.ownPeerDescriptor.nodeName + ', ' + (this.targetPeerDescriptor.nodeName!) +
65
+ ' close() simulator.disconnect returned')
66
+ } catch (e) {
67
+ logger.trace(' ' + this.ownPeerDescriptor.nodeName + ', ' + (this.targetPeerDescriptor.nodeName!) +
68
+ 'close aborted' + e)
69
+ } finally {
70
+ logger.trace(' ' + this.ownPeerDescriptor.nodeName + ', ' + (this.targetPeerDescriptor.nodeName!) +
71
+ ' calling this.doDisconnect')
72
+ this.doDisconnect(disconnectionType)
73
+ }
74
+
75
+ } else {
76
+ logger.trace(' ' + this.ownPeerDescriptor.nodeName + ', ' + (this.targetPeerDescriptor.nodeName!) +
77
+ ' close() tried to close a stopped connection')
78
+ }
79
+ }
80
+
81
+ public connect(): void {
82
+ if (!this.stopped) {
83
+ logger.trace('connect() called')
84
+
85
+ this.simulator.connect(this, this.targetPeerDescriptor, (error?: string) => {
86
+ if (error) {
87
+ logger.trace(error)
88
+ this.doDisconnect('OTHER')
89
+ } else {
90
+ this.emit('connected')
91
+ }
92
+ })
93
+ } else {
94
+ logger.trace('tried to connect() a stopped connection')
95
+ }
96
+ }
97
+
98
+ public handleIncomingData(data: Uint8Array): void {
99
+ if (!this.stopped) {
100
+ logger.trace('handleIncomingData()')
101
+ logger.trace(protoToString(Message.fromBinary(data), Message))
102
+ this.emit('data', data)
103
+ } else {
104
+ logger.trace('tried to call handleIncomingData() a stopped connection')
105
+ }
106
+ }
107
+
108
+ public handleIncomingDisconnection(): void {
109
+ if (!this.stopped) {
110
+ logger.trace(this.ownPeerDescriptor.nodeName + ' handleIncomingDisconnection()')
111
+ this.stopped = true
112
+ this.doDisconnect('OTHER')
113
+ } else {
114
+ logger.trace('tried to call handleIncomingDisconnection() a stopped connection')
115
+ }
116
+ }
117
+
118
+ public destroy(): void {
119
+ if (!this.stopped) {
120
+ logger.trace(this.ownPeerDescriptor.nodeName + ' destroy()')
121
+ this.removeAllListeners()
122
+ this.close('OTHER').catch((_e) => { })
123
+ } else {
124
+ logger.trace(this.ownPeerDescriptor.nodeName + ' tried to call destroy() a stopped connection')
125
+ }
126
+ }
127
+
128
+ private doDisconnect(disconnectionType: DisconnectionType) {
129
+ logger.trace(this.ownPeerDescriptor.nodeName + ' doDisconnect()')
130
+ this.stopped = true
131
+
132
+ logger.trace(' ' + this.ownPeerDescriptor.nodeName + ', ' + (this.targetPeerDescriptor.nodeName!) + ' doDisconnect emitting')
133
+
134
+ this.emit('disconnected', disconnectionType)
135
+
136
+ }
137
+ }
@@ -0,0 +1,104 @@
1
+ import { ConnectionType } from '../IConnection'
2
+
3
+ import {
4
+ PeerDescriptor,
5
+ } from '../../proto/packages/dht/protos/DhtRpc'
6
+ import { Logger } from '@streamr/utils'
7
+ import { ManagedConnection } from '../ManagedConnection'
8
+ import { PeerIDKey } from '../../helpers/PeerID'
9
+ import { Simulator } from './Simulator'
10
+ import { SimulatorConnection } from './SimulatorConnection'
11
+ import { keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
12
+
13
+ const logger = new Logger(module)
14
+
15
+ export class SimulatorConnector {
16
+
17
+ private connectingConnections: Map<PeerIDKey, ManagedConnection> = new Map()
18
+ private stopped = false
19
+ private protocolVersion: string
20
+ private ownPeerDescriptor: PeerDescriptor
21
+ private simulator: Simulator
22
+ private incomingConnectionCallback: (connection: ManagedConnection) => boolean
23
+
24
+ constructor(
25
+ protocolVersion: string,
26
+ ownPeerDescriptor: PeerDescriptor,
27
+ simulator: Simulator,
28
+ incomingConnectionCallback: (connection: ManagedConnection) => boolean
29
+ ) {
30
+ this.protocolVersion = protocolVersion
31
+ this.ownPeerDescriptor = ownPeerDescriptor
32
+ this.simulator = simulator
33
+ this.incomingConnectionCallback = incomingConnectionCallback
34
+ }
35
+
36
+ public connect(targetPeerDescriptor: PeerDescriptor): ManagedConnection {
37
+ logger.trace('connect() ' + this.ownPeerDescriptor.nodeName + ',' + targetPeerDescriptor.nodeName)
38
+ const peerKey = keyFromPeerDescriptor(targetPeerDescriptor)
39
+ const existingConnection = this.connectingConnections.get(peerKey)
40
+ if (existingConnection) {
41
+ return existingConnection
42
+ }
43
+
44
+ const connection = new SimulatorConnection(this.ownPeerDescriptor!, targetPeerDescriptor, ConnectionType.SIMULATOR_CLIENT, this.simulator)
45
+
46
+ const managedConnection = new ManagedConnection(this.ownPeerDescriptor!, this.protocolVersion,
47
+ ConnectionType.SIMULATOR_CLIENT, connection, undefined)
48
+ managedConnection.setPeerDescriptor(targetPeerDescriptor!)
49
+
50
+ this.connectingConnections.set(peerKey, managedConnection)
51
+ connection.once('disconnected', () => {
52
+ this.connectingConnections.delete(peerKey)
53
+ })
54
+ connection.once('connected', () => {
55
+ this.connectingConnections.delete(peerKey)
56
+ })
57
+
58
+ connection.connect()
59
+
60
+ return managedConnection
61
+ }
62
+
63
+ public getPeerDescriptor(): PeerDescriptor {
64
+ return this.ownPeerDescriptor
65
+ }
66
+
67
+ public handleIncomingConnection(sourceConnection: SimulatorConnection): void {
68
+ logger.trace(' ' + this.ownPeerDescriptor.nodeName + ', ' + sourceConnection.ownPeerDescriptor.nodeName +
69
+ ' incoming connection, stopped: ' + this.stopped)
70
+ if (this.stopped) {
71
+ return
72
+ }
73
+ const connection = new SimulatorConnection(this.ownPeerDescriptor!,
74
+ sourceConnection.ownPeerDescriptor, ConnectionType.SIMULATOR_SERVER, this.simulator)
75
+
76
+ const managedConnection = new ManagedConnection(this.ownPeerDescriptor!, this.protocolVersion,
77
+ ConnectionType.SIMULATOR_SERVER, undefined, connection)
78
+
79
+ logger.trace('connected, objectId: ' + managedConnection.objectId)
80
+
81
+ managedConnection.once('handshakeRequest', (_peerDescriptor: PeerDescriptor) => {
82
+ logger.trace(' ' + this.ownPeerDescriptor.nodeName + ', ' + sourceConnection.ownPeerDescriptor.nodeName + ' incoming handshake request')
83
+ logger.trace('incoming handshake request objectId: ' + managedConnection.objectId)
84
+
85
+ if (this.incomingConnectionCallback(managedConnection)) {
86
+ logger.trace(' ' + this.ownPeerDescriptor.nodeName + ', ' + sourceConnection.ownPeerDescriptor.nodeName + ' calling acceptHandshake')
87
+ managedConnection.acceptHandshake()
88
+ } else {
89
+ managedConnection.rejectHandshake('Duplicate connection')
90
+ managedConnection.destroy()
91
+ }
92
+ })
93
+
94
+ this.simulator.accept(sourceConnection, connection)
95
+ }
96
+
97
+ public async stop(): Promise<void> {
98
+ this.stopped = true
99
+ const conns = Array.from(this.connectingConnections.values())
100
+ await Promise.allSettled(conns.map((conn) =>
101
+ conn.close('OTHER')
102
+ ))
103
+ }
104
+ }
@@ -0,0 +1,9 @@
1
+ import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
2
+ import { ConnectionManager } from '../ConnectionManager'
3
+ import { Simulator } from './Simulator'
4
+
5
+ export class SimulatorTransport extends ConnectionManager {
6
+ constructor(ownPeerDescriptor: PeerDescriptor, simulator: Simulator) {
7
+ super({ ownPeerDescriptor: ownPeerDescriptor, simulator, serviceIdPrefix: 'simulator/', nodeName: ownPeerDescriptor.nodeName })
8
+ }
9
+ }
@@ -0,0 +1,42 @@
1
+ /* eslint-disable max-len, @typescript-eslint/prefer-for-of */
2
+
3
+ // This file contains a 16x16 matrix of ping times measured between
4
+ // amazon regions. Converted to typescript from the file pings.csv
5
+ // in order to make loading into browser easier.
6
+
7
+ export enum RegionIndex { eu_central_1, eu_west_1, eu_west_2, eu_west_3, eu_north_1, us_east_1, us_east_2, us_west_1, us_west_2, ca_central_1, ap_south_1, ap_northeast_2, ap_southeast_1, ap_southeast_2, ap_northeast_1, sa_east_1 }
8
+
9
+ export const regionPingMatrix = [
10
+ [0.325, 23.163, 12.942, 8.923, 20.699, 89.208, 99.98, 150.077, 139.224, 90.454, 117.0, 226.823, 154.592, 286.753, 222.719, 205.563],
11
+ [24.911, 0.195, 10.022, 18.544, 39.802, 66.161, 93.877, 136.271, 127.362, 73.733, 122.187, 229.064, 177.256, 254.277, 200.207, 186.817],
12
+ [12.879, 9.994, 0.167, 8.626, 31.896, 74.663, 85.231, 136.185, 125.328, 77.811, 111.29, 236.34, 164.357, 262.577, 207.745, 187.383],
13
+ [9.007, 16.823, 8.761, 0.191, 28.449, 81.969, 92.599, 140.961, 132.269, 85.444, 107.421, 243.981, 167.17, 279.322, 214.088, 197.986],
14
+ [20.661, 40.0, 31.176, 27.645, 0.137, 104.171, 115.704, 165.01, 155.741, 107.635, 136.75, 267.321, 173.951, 292.266, 238.492, 216.048],
15
+ [89.076, 67.701, 75.414, 81.598, 104.866, 0.284, 11.501, 61.662, 78.612, 14.115, 186.697, 174.546, 210.93, 196.071, 145.622, 114.919],
16
+ [100.018, 93.968, 85.06, 92.582, 115.719, 11.514, 0.184, 51.668, 48.447, 25.808, 195.433, 160.031, 197.27, 186.913, 133.115, 127.954],
17
+ [148.544, 137.18, 136.137, 141.333, 164.73, 60.257, 50.886, 0.197, 21.509, 77.668, 229.32, 134.858, 174.472, 137.329, 107.566, 175.944],
18
+ [137.885, 128.304, 125.316, 132.271, 155.726, 63.376, 48.457, 20.743, 0.293, 59.645, 218.285, 126.988, 162.72, 138.599, 100.203, 174.612],
19
+ [90.494, 78.203, 77.998, 84.348, 109.549, 14.136, 25.917, 77.323, 59.692, 0.163, 187.854, 169.253, 206.944, 195.467, 141.054, 126.962],
20
+ [120.19, 119.978, 112.023, 107.434, 139.361, 185.043, 195.679, 233.008, 216.025, 186.888, 0.177, 130.21, 56.019, 145.643, 128.57, 298.98],
21
+ [226.296, 229.187, 237.024, 243.839, 266.919, 174.727, 160.066, 136.623, 124.799, 169.854, 129.131, 0.189, 81.065, 139.967, 33.24, 284.578],
22
+ [154.691, 178.279, 170.997, 154.261, 173.776, 211.511, 197.551, 173.735, 162.834, 206.659, 56.964, 68.853, 0.215, 91.601, 69.684, 323.146],
23
+ [289.784, 253.234, 261.752, 277.504, 292.125, 198.099, 185.709, 136.413, 138.613, 194.986, 142.484, 147.706, 91.615, 0.156, 114.518, 311.12],
24
+ [223.926, 201.084, 210.133, 216.779, 237.762, 145.336, 132.887, 108.26, 98.057, 142.613, 122.716, 33.185, 68.547, 114.901, 0.162, 257.403],
25
+ [205.555, 178.082, 186.533, 195.774, 216.701, 117.089, 128.001, 175.761, 176.018, 125.551, 303.904, 285.782, 323.15, 309.618, 255.904, 0.262]
26
+ ]
27
+
28
+ export function getRegionDelayMatrix(): Array<Array<number>> {
29
+ const ret = []
30
+ for (let i = 0; i < regionPingMatrix.length; i++) {
31
+ const row = []
32
+ for (let j = 0; j < regionPingMatrix[i].length; j++) {
33
+ row.push(regionPingMatrix[i][j] / 2)
34
+ }
35
+ ret.push(row)
36
+ }
37
+ return ret
38
+ }
39
+
40
+ export function getRandomRegion(): number {
41
+ return Math.floor(Math.random() * 15)
42
+ }