@streamr/dht 100.0.0-testnet-two.4 → 100.0.0-testnet-three.1

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 (306) hide show
  1. package/dist/package.json +8 -9
  2. package/dist/src/connection/Connection.d.ts +1 -0
  3. package/dist/src/connection/Connection.js +10 -3
  4. package/dist/src/connection/Connection.js.map +1 -1
  5. package/dist/src/connection/ConnectionLockHandler.d.ts +3 -3
  6. package/dist/src/connection/ConnectionLockHandler.js +8 -10
  7. package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
  8. package/dist/src/connection/ConnectionLockRpcLocal.js +5 -4
  9. package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
  10. package/dist/src/connection/ConnectionLockRpcRemote.js +4 -4
  11. package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
  12. package/dist/src/connection/ConnectionManager.d.ts +8 -8
  13. package/dist/src/connection/ConnectionManager.js +54 -55
  14. package/dist/src/connection/ConnectionManager.js.map +1 -1
  15. package/dist/src/connection/ConnectorFacade.d.ts +3 -2
  16. package/dist/src/connection/ConnectorFacade.js +7 -0
  17. package/dist/src/connection/ConnectorFacade.js.map +1 -1
  18. package/dist/src/connection/Handshaker.js +3 -1
  19. package/dist/src/connection/Handshaker.js.map +1 -1
  20. package/dist/src/connection/IConnection.d.ts +2 -7
  21. package/dist/src/connection/IConnection.js +1 -8
  22. package/dist/src/connection/IConnection.js.map +1 -1
  23. package/dist/src/connection/ManagedConnection.js +23 -15
  24. package/dist/src/connection/ManagedConnection.js.map +1 -1
  25. package/dist/src/connection/ManagedWebrtcConnection.js.map +1 -1
  26. package/dist/src/connection/connectivityChecker.d.ts +1 -1
  27. package/dist/src/connection/connectivityChecker.js +9 -2
  28. package/dist/src/connection/connectivityChecker.js.map +1 -1
  29. package/dist/src/connection/connectivityRequestHandler.js +8 -2
  30. package/dist/src/connection/connectivityRequestHandler.js.map +1 -1
  31. package/dist/src/connection/simulator/Simulator.js +32 -20
  32. package/dist/src/connection/simulator/Simulator.js.map +1 -1
  33. package/dist/src/connection/simulator/SimulatorConnection.js +13 -10
  34. package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
  35. package/dist/src/connection/simulator/SimulatorConnector.js +9 -6
  36. package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
  37. package/dist/src/connection/simulator/pings.js.map +1 -1
  38. package/dist/src/connection/webrtc/NodeWebrtcConnection.d.ts +1 -1
  39. package/dist/src/connection/webrtc/NodeWebrtcConnection.js +26 -14
  40. package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
  41. package/dist/src/connection/webrtc/WebrtcConnector.d.ts +0 -1
  42. package/dist/src/connection/webrtc/WebrtcConnector.js +18 -17
  43. package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
  44. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +10 -9
  45. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
  46. package/dist/src/connection/webrtc/iceServerAsString.js.map +1 -1
  47. package/dist/src/connection/websocket/AutoCertifierClientFacade.js +3 -0
  48. package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
  49. package/dist/src/connection/websocket/ClientWebsocket.js +7 -4
  50. package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
  51. package/dist/src/connection/websocket/ServerWebsocket.d.ts +1 -0
  52. package/dist/src/connection/websocket/ServerWebsocket.js +22 -3
  53. package/dist/src/connection/websocket/ServerWebsocket.js.map +1 -1
  54. package/dist/src/connection/websocket/WebsocketConnector.d.ts +2 -1
  55. package/dist/src/connection/websocket/WebsocketConnector.js +27 -16
  56. package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
  57. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +2 -1
  58. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +3 -1
  59. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
  60. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +2 -2
  61. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
  62. package/dist/src/connection/websocket/WebsocketServer.js +6 -3
  63. package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
  64. package/dist/src/dht/DhtNode.d.ts +18 -19
  65. package/dist/src/dht/DhtNode.js +56 -65
  66. package/dist/src/dht/DhtNode.js.map +1 -1
  67. package/dist/src/dht/DhtNodeRpcLocal.d.ts +1 -1
  68. package/dist/src/dht/DhtNodeRpcLocal.js +5 -4
  69. package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
  70. package/dist/src/dht/DhtNodeRpcRemote.d.ts +2 -1
  71. package/dist/src/dht/DhtNodeRpcRemote.js +5 -3
  72. package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
  73. package/dist/src/dht/ExternalApiRpcLocal.d.ts +1 -1
  74. package/dist/src/dht/ExternalApiRpcLocal.js +4 -3
  75. package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
  76. package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
  77. package/dist/src/dht/PeerManager.d.ts +5 -5
  78. package/dist/src/dht/PeerManager.js +31 -24
  79. package/dist/src/dht/PeerManager.js.map +1 -1
  80. package/dist/src/dht/contact/Contact.js +3 -2
  81. package/dist/src/dht/contact/Contact.js.map +1 -1
  82. package/dist/src/dht/contact/ContactList.js +9 -5
  83. package/dist/src/dht/contact/ContactList.js.map +1 -1
  84. package/dist/src/dht/contact/RandomContactList.js +1 -0
  85. package/dist/src/dht/contact/RandomContactList.js.map +1 -1
  86. package/dist/src/dht/contact/RpcRemote.d.ts +2 -1
  87. package/dist/src/dht/contact/RpcRemote.js +4 -0
  88. package/dist/src/dht/contact/RpcRemote.js.map +1 -1
  89. package/dist/src/dht/contact/SortedContactList.js +3 -2
  90. package/dist/src/dht/contact/SortedContactList.js.map +1 -1
  91. package/dist/src/dht/discovery/DiscoverySession.js +8 -8
  92. package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
  93. package/dist/src/dht/discovery/PeerDiscovery.d.ts +8 -2
  94. package/dist/src/dht/discovery/PeerDiscovery.js +26 -17
  95. package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
  96. package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +1 -1
  97. package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +9 -9
  98. package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -1
  99. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js +3 -2
  100. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -1
  101. package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js +6 -5
  102. package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -1
  103. package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +2 -1
  104. package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +26 -20
  105. package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -1
  106. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +4 -2
  107. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +5 -2
  108. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -1
  109. package/dist/src/dht/routing/DuplicateDetector.d.ts +2 -4
  110. package/dist/src/dht/routing/DuplicateDetector.js +10 -15
  111. package/dist/src/dht/routing/DuplicateDetector.js.map +1 -1
  112. package/dist/src/dht/routing/Router.d.ts +1 -1
  113. package/dist/src/dht/routing/Router.js +20 -15
  114. package/dist/src/dht/routing/Router.js.map +1 -1
  115. package/dist/src/dht/routing/RouterRpcLocal.js +4 -4
  116. package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
  117. package/dist/src/dht/routing/RouterRpcRemote.js +11 -9
  118. package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
  119. package/dist/src/dht/routing/RoutingSession.d.ts +2 -0
  120. package/dist/src/dht/routing/RoutingSession.js +27 -9
  121. package/dist/src/dht/routing/RoutingSession.js.map +1 -1
  122. package/dist/src/dht/store/LocalDataStore.js +5 -4
  123. package/dist/src/dht/store/LocalDataStore.js.map +1 -1
  124. package/dist/src/dht/store/StoreManager.js +8 -8
  125. package/dist/src/dht/store/StoreManager.js.map +1 -1
  126. package/dist/src/dht/store/StoreRpcLocal.js +1 -0
  127. package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
  128. package/dist/src/dht/store/StoreRpcRemote.js +3 -3
  129. package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
  130. package/dist/src/exports.d.ts +1 -4
  131. package/dist/src/exports.js +4 -7
  132. package/dist/src/exports.js.map +1 -1
  133. package/dist/src/helpers/AddressTools.js.map +1 -1
  134. package/dist/src/helpers/Connectivity.js.map +1 -1
  135. package/dist/src/helpers/MapWithTtl.js +2 -1
  136. package/dist/src/helpers/MapWithTtl.js.map +1 -1
  137. package/dist/src/helpers/createPeerDescriptor.d.ts +3 -0
  138. package/dist/src/helpers/createPeerDescriptor.js +56 -0
  139. package/dist/src/helpers/createPeerDescriptor.js.map +1 -0
  140. package/dist/src/helpers/createPeerDescriptorSignaturePayload.d.ts +2 -0
  141. package/dist/src/helpers/createPeerDescriptorSignaturePayload.js +30 -0
  142. package/dist/src/helpers/createPeerDescriptorSignaturePayload.js.map +1 -0
  143. package/dist/src/helpers/debugHelpers.js.map +1 -1
  144. package/dist/src/helpers/errors.js +2 -0
  145. package/dist/src/helpers/errors.js.map +1 -1
  146. package/dist/src/helpers/offering.d.ts +4 -0
  147. package/dist/src/helpers/offering.js +18 -0
  148. package/dist/src/helpers/offering.js.map +1 -0
  149. package/dist/src/helpers/protoToString.js.map +1 -1
  150. package/dist/src/identifiers.d.ts +3 -0
  151. package/dist/src/identifiers.js +9 -1
  152. package/dist/src/identifiers.js.map +1 -1
  153. package/dist/src/proto/google/protobuf/any.js +8 -8
  154. package/dist/src/proto/google/protobuf/any.js.map +1 -1
  155. package/dist/src/proto/google/protobuf/empty.js +2 -4
  156. package/dist/src/proto/google/protobuf/empty.js.map +1 -1
  157. package/dist/src/proto/google/protobuf/timestamp.js +10 -10
  158. package/dist/src/proto/google/protobuf/timestamp.js.map +1 -1
  159. package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +4 -5
  160. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +38 -29
  161. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
  162. package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +26 -12
  163. package/dist/src/proto/packages/dht/protos/DhtRpc.js +12 -16
  164. package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
  165. package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +2 -3
  166. package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +1 -1
  167. package/dist/src/rpc-protocol/DhtCallContext.d.ts +0 -1
  168. package/dist/src/rpc-protocol/DhtCallContext.js +8 -0
  169. package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -1
  170. package/dist/src/transport/ITransport.d.ts +1 -1
  171. package/dist/src/transport/ListeningRpcCommunicator.js +2 -0
  172. package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
  173. package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -1
  174. package/dist/src/transport/RoutingRpcCommunicator.js +2 -0
  175. package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
  176. package/package.json +8 -9
  177. package/protos/DhtRpc.proto +8 -4
  178. package/src/connection/Connection.ts +6 -1
  179. package/src/connection/ConnectionLockHandler.ts +3 -3
  180. package/src/connection/ConnectionLockRpcLocal.ts +1 -5
  181. package/src/connection/ConnectionLockRpcRemote.ts +1 -1
  182. package/src/connection/ConnectionManager.ts +24 -38
  183. package/src/connection/ConnectorFacade.ts +3 -2
  184. package/src/connection/Handshaker.ts +4 -4
  185. package/src/connection/IConnection.ts +2 -8
  186. package/src/connection/ManagedConnection.ts +3 -3
  187. package/src/connection/connectivityChecker.ts +9 -2
  188. package/src/connection/connectivityRequestHandler.ts +10 -3
  189. package/src/connection/simulator/Simulator.ts +1 -2
  190. package/src/connection/simulator/SimulatorConnection.ts +1 -1
  191. package/src/connection/simulator/SimulatorConnector.ts +1 -2
  192. package/src/connection/webrtc/BrowserWebrtcConnection.ts +4 -3
  193. package/src/connection/webrtc/NodeWebrtcConnection.ts +5 -4
  194. package/src/connection/webrtc/WebrtcConnector.ts +9 -17
  195. package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +9 -9
  196. package/src/connection/websocket/ClientWebsocket.ts +3 -2
  197. package/src/connection/websocket/ServerWebsocket.ts +17 -1
  198. package/src/connection/websocket/WebsocketConnector.ts +16 -13
  199. package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +4 -2
  200. package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +1 -1
  201. package/src/connection/websocket/WebsocketServer.ts +2 -2
  202. package/src/dht/DhtNode.ts +63 -86
  203. package/src/dht/DhtNodeRpcLocal.ts +5 -5
  204. package/src/dht/DhtNodeRpcRemote.ts +3 -3
  205. package/src/dht/ExternalApiRpcLocal.ts +3 -4
  206. package/src/dht/PeerManager.ts +14 -20
  207. package/src/dht/contact/Contact.ts +1 -2
  208. package/src/dht/contact/RpcRemote.ts +2 -1
  209. package/src/dht/discovery/DiscoverySession.ts +2 -3
  210. package/src/dht/discovery/PeerDiscovery.ts +16 -9
  211. package/src/dht/recursive-operation/RecursiveOperationManager.ts +12 -6
  212. package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +1 -1
  213. package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +3 -2
  214. package/src/dht/recursive-operation/RecursiveOperationSession.ts +19 -9
  215. package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +13 -3
  216. package/src/dht/routing/DuplicateDetector.ts +9 -21
  217. package/src/dht/routing/Router.ts +12 -11
  218. package/src/dht/routing/RouterRpcLocal.ts +1 -2
  219. package/src/dht/routing/RouterRpcRemote.ts +5 -5
  220. package/src/dht/routing/RoutingSession.ts +22 -2
  221. package/src/dht/store/StoreManager.ts +1 -2
  222. package/src/dht/store/StoreRpcRemote.ts +1 -1
  223. package/src/exports.ts +9 -4
  224. package/src/helpers/createPeerDescriptor.ts +56 -0
  225. package/src/helpers/createPeerDescriptorSignaturePayload.ts +28 -0
  226. package/src/helpers/offering.ts +15 -0
  227. package/src/identifiers.ts +10 -1
  228. package/src/proto/google/protobuf/any.ts +4 -4
  229. package/src/proto/google/protobuf/empty.ts +2 -4
  230. package/src/proto/google/protobuf/timestamp.ts +4 -4
  231. package/src/proto/packages/dht/protos/DhtRpc.client.ts +6 -7
  232. package/src/proto/packages/dht/protos/DhtRpc.server.ts +3 -4
  233. package/src/proto/packages/dht/protos/DhtRpc.ts +37 -20
  234. package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +1 -1
  235. package/src/rpc-protocol/DhtCallContext.ts +0 -1
  236. package/src/transport/ITransport.ts +1 -1
  237. package/src/transport/RoutingRpcCommunicator.ts +1 -1
  238. package/test/benchmark/Find.test.ts +6 -8
  239. package/test/benchmark/KademliaCorrectness.test.ts +2 -3
  240. package/test/benchmark/SortedContactListBenchmark.test.ts +16 -16
  241. package/test/benchmark/WebsocketServerMemoryLeak.test.ts +1 -1
  242. package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +8 -8
  243. package/test/benchmark/kademlia-simulation/SimulationNode.ts +8 -8
  244. package/test/end-to-end/Layer0-Layer1.test.ts +1 -1
  245. package/test/end-to-end/Layer0.test.ts +4 -4
  246. package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +12 -12
  247. package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +18 -18
  248. package/test/end-to-end/Layer0Webrtc.test.ts +13 -10
  249. package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +2 -2
  250. package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +2 -2
  251. package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +1 -1
  252. package/test/end-to-end/WebsocketConnectionRequest.test.ts +3 -3
  253. package/test/end-to-end/memory-leak.test.ts +2 -2
  254. package/test/integration/ConnectionLocking.test.ts +60 -47
  255. package/test/integration/ConnectionManager.test.ts +8 -7
  256. package/test/integration/ConnectivityChecking.test.ts +64 -0
  257. package/test/integration/DhtJoinPeerDiscovery.test.ts +2 -2
  258. package/test/integration/DhtNodeExternalAPI.test.ts +1 -2
  259. package/test/integration/DhtNodeRpcRemote.test.ts +4 -3
  260. package/test/integration/DhtRpc.test.ts +4 -3
  261. package/test/integration/Find.test.ts +4 -6
  262. package/test/integration/Layer1-scale.test.ts +7 -7
  263. package/test/integration/Mock-Layer1-Layer0.test.ts +21 -24
  264. package/test/integration/MultipleEntryPointJoining.test.ts +7 -7
  265. package/test/integration/ReplicateData.test.ts +52 -161
  266. package/test/integration/RouteMessage.test.ts +24 -47
  267. package/test/integration/RouterRpcRemote.test.ts +7 -4
  268. package/test/integration/ScaleDownDht.test.ts +2 -3
  269. package/test/integration/SimultaneousConnections.test.ts +9 -8
  270. package/test/integration/Store.test.ts +1 -2
  271. package/test/integration/StoreAndDelete.test.ts +5 -0
  272. package/test/integration/StoreOnDhtWithThreeNodes.test.ts +59 -0
  273. package/test/integration/StoreOnDhtWithTwoNodes.test.ts +1 -1
  274. package/test/integration/StoreRpcRemote.test.ts +4 -4
  275. package/test/integration/WebrtcConnectionManagement.test.ts +6 -5
  276. package/test/integration/WebrtcConnectorRpc.test.ts +3 -2
  277. package/test/integration/WebsocketConnectionManagement.test.ts +27 -15
  278. package/test/integration/WebsocketConnectorRpc.test.ts +3 -2
  279. package/test/integration/{RpcErrors.test.ts → rpc-connections-over-webrpc.test.ts} +6 -3
  280. package/test/unit/DuplicateDetector.test.ts +2 -2
  281. package/test/unit/LocalDataStore.test.ts +1 -4
  282. package/test/unit/RandomContactList.test.ts +2 -4
  283. package/test/unit/RecursiveOperationManager.test.ts +5 -6
  284. package/test/unit/RecursiveOperationSession.test.ts +5 -4
  285. package/test/unit/Router.test.ts +25 -8
  286. package/test/unit/RoutingSession.test.ts +3 -3
  287. package/test/unit/SortedContactList.test.ts +2 -4
  288. package/test/unit/connectivityRequestHandler.test.ts +7 -3
  289. package/test/unit/createPeerDescriptor.test.ts +63 -0
  290. package/test/utils/FakeTransport.ts +7 -4
  291. package/test/utils/mock/Transport.ts +1 -1
  292. package/test/utils/utils.ts +6 -6
  293. package/dist/src/helpers/PeerID.d.ts +0 -27
  294. package/dist/src/helpers/PeerID.js +0 -87
  295. package/dist/src/helpers/PeerID.js.map +0 -1
  296. package/dist/src/helpers/UUID.d.ts +0 -8
  297. package/dist/src/helpers/UUID.js +0 -36
  298. package/dist/src/helpers/UUID.js.map +0 -1
  299. package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +0 -7
  300. package/dist/src/helpers/peerIdFromPeerDescriptor.js +0 -23
  301. package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +0 -1
  302. package/src/helpers/PeerID.ts +0 -97
  303. package/src/helpers/UUID.ts +0 -35
  304. package/src/helpers/peerIdFromPeerDescriptor.ts +0 -20
  305. package/test/unit/PeerID.test.ts +0 -22
  306. package/test/unit/UUID.test.ts +0 -55
@@ -3,15 +3,9 @@ import { EventEmitter } from 'eventemitter3'
3
3
  import { SortedContactList } from '../dht/contact/SortedContactList'
4
4
  import { DuplicateDetector } from '../dht/routing/DuplicateDetector'
5
5
  import * as Err from '../helpers/errors'
6
- import {
7
- areEqualPeerDescriptors,
8
- getNodeIdFromPeerDescriptor,
9
- peerIdFromPeerDescriptor
10
- } from '../helpers/peerIdFromPeerDescriptor'
11
6
  import {
12
7
  DisconnectMode,
13
8
  DisconnectNotice,
14
- DisconnectNoticeResponse,
15
9
  LockRequest,
16
10
  LockResponse,
17
11
  Message,
@@ -29,7 +23,8 @@ import { ConnectionLockRpcRemote } from './ConnectionLockRpcRemote'
29
23
  import { WEBRTC_CLEANUP } from './webrtc/NodeWebrtcConnection'
30
24
  import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
31
25
  import { ConnectionLockRpcLocal } from './ConnectionLockRpcLocal'
32
- import { DhtAddress } from '../identifiers'
26
+ import { DhtAddress, areEqualPeerDescriptors, getNodeIdFromPeerDescriptor } from '../identifiers'
27
+ import { getOfferer } from '../helpers/offering'
33
28
 
34
29
  export interface ConnectionManagerConfig {
35
30
  maxConnections?: number
@@ -101,7 +96,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
101
96
  private config: ConnectionManagerConfig
102
97
  private readonly metricsContext: MetricsContext
103
98
  // TODO use config option or named constant?
104
- private readonly duplicateMessageDetector: DuplicateDetector = new DuplicateDetector(100000, 100)
99
+ private readonly duplicateMessageDetector: DuplicateDetector = new DuplicateDetector(100000)
105
100
  private readonly metrics: ConnectionManagerMetrics
106
101
  private locks = new ConnectionLockHandler()
107
102
  private connections: Map<DhtAddress, ManagedConnection> = new Map()
@@ -144,7 +139,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
144
139
  (req: LockRequest, context: ServerCallContext) => lockRpcLocal.lockRequest(req, context))
145
140
  this.rpcCommunicator.registerRpcNotification(UnlockRequest, 'unlockRequest',
146
141
  (req: UnlockRequest, context: ServerCallContext) => lockRpcLocal.unlockRequest(req, context))
147
- this.rpcCommunicator.registerRpcMethod(DisconnectNotice, DisconnectNoticeResponse, 'gracefulDisconnect',
142
+ this.rpcCommunicator.registerRpcNotification(DisconnectNotice, 'gracefulDisconnect',
148
143
  (req: DisconnectNotice, context: ServerCallContext) => lockRpcLocal.gracefulDisconnect(req, context))
149
144
  }
150
145
 
@@ -181,7 +176,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
181
176
  logger.trace(`Starting ConnectionManager...`)
182
177
  await this.connectorFacade.start(
183
178
  (connection: ManagedConnection) => this.onNewConnection(connection),
184
- (peerDescriptor: PeerDescriptor) => this.hasConnection(peerDescriptor),
179
+ (nodeId: DhtAddress) => this.hasConnection(nodeId),
185
180
  this
186
181
  )
187
182
  // Garbage collection of connections
@@ -236,20 +231,20 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
236
231
  WEBRTC_CLEANUP.cleanUp()
237
232
  }
238
233
 
239
- public getNumberOfLocalLockedConnections(): number {
240
- return this.locks.getNumberOfLocalLockedConnections()
234
+ public getLocalLockedConnectionCount(): number {
235
+ return this.locks.getLocalLockedConnectionCount()
241
236
  }
242
237
 
243
- public getNumberOfRemoteLockedConnections(): number {
244
- return this.locks.getNumberOfRemoteLockedConnections()
238
+ public getRemoteLockedConnectionCount(): number {
239
+ return this.locks.getRemoteLockedConnectionCount()
245
240
  }
246
241
 
247
- public getNumberOfWeakLockedConnections(): number {
248
- return this.locks.getNumberOfWeakLockedConnections()
242
+ public getWeakLockedConnectionCount(): number {
243
+ return this.locks.getWeakLockedConnectionCount()
249
244
  }
250
245
 
251
246
  public async send(message: Message, opts: SendOptions = DEFAULT_SEND_OPTIONS): Promise<void> {
252
- if (this.state === ConnectionManagerState.STOPPED && !opts.sendIfStopped) {
247
+ if ((this.state === ConnectionManagerState.STOPPED || this.state === ConnectionManagerState.STOPPING) && !opts.sendIfStopped) {
253
248
  return
254
249
  }
255
250
  const peerDescriptor = message.targetDescriptor!
@@ -289,8 +284,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
289
284
  }
290
285
  }
291
286
 
292
- public getConnection(peerDescriptor: PeerDescriptor): ManagedConnection | undefined {
293
- const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
287
+ public getConnection(nodeId: DhtAddress): ManagedConnection | undefined {
294
288
  return this.connections.get(nodeId)
295
289
  }
296
290
 
@@ -298,18 +292,15 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
298
292
  return this.connectorFacade.getLocalPeerDescriptor()!
299
293
  }
300
294
 
301
- public hasConnection(peerDescriptor: PeerDescriptor): boolean {
302
- const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
295
+ public hasConnection(nodeId: DhtAddress): boolean {
303
296
  return this.connections.has(nodeId)
304
297
  }
305
298
 
306
- public hasLocalLockedConnection(peerDescriptor: PeerDescriptor): boolean {
307
- const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
299
+ public hasLocalLockedConnection(nodeId: DhtAddress): boolean {
308
300
  return this.locks.isLocalLocked(nodeId)
309
301
  }
310
302
 
311
- public hasRemoteLockedConnection(peerDescriptor: PeerDescriptor): boolean {
312
- const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
303
+ public hasRemoteLockedConnection(nodeId: DhtAddress): boolean {
313
304
  return this.locks.isRemoteLocked(nodeId)
314
305
  }
315
306
 
@@ -366,7 +357,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
366
357
  const nodeId = getNodeIdFromPeerDescriptor(connection.getPeerDescriptor()!)
367
358
  logger.trace(nodeId + ' onDisconnected() gracefulLeave: ' + gracefulLeave)
368
359
  const storedConnection = this.connections.get(nodeId)
369
- if (storedConnection && storedConnection.connectionId.equals(connection.connectionId)) {
360
+ if (storedConnection && (storedConnection.connectionId === connection.connectionId)) {
370
361
  this.locks.clearAllLocks(nodeId)
371
362
  this.connections.delete(nodeId)
372
363
  logger.trace(nodeId + ' deleted connection in onDisconnected() gracefulLeave: ' + gracefulLeave)
@@ -375,8 +366,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
375
366
  } else {
376
367
  logger.trace(nodeId + ' onDisconnected() did nothing, no such connection in connectionManager')
377
368
  if (storedConnection) {
378
- const connectionId = storedConnection.connectionId.toString()
379
- logger.trace(nodeId + ' connectionIds do not match ' + connectionId + ' ' + connection.connectionId.toString())
369
+ logger.trace(nodeId + ' connectionIds do not match ' + storedConnection.connectionId + ' ' + connection.connectionId.toString())
380
370
  }
381
371
  }
382
372
  }
@@ -407,8 +397,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
407
397
  const nodeId = getNodeIdFromPeerDescriptor(newConnection.getPeerDescriptor()!)
408
398
  logger.trace(nodeId + ' acceptIncomingConnection()')
409
399
  if (this.connections.has(nodeId)) {
410
- const newPeerID = peerIdFromPeerDescriptor(newConnection.getPeerDescriptor()!)
411
- if (newPeerID.hasSmallerHashThan(peerIdFromPeerDescriptor(this.getLocalPeerDescriptor()))) {
400
+ if (getOfferer(getNodeIdFromPeerDescriptor(this.getLocalPeerDescriptor()), nodeId) === 'remote') {
412
401
  logger.trace(nodeId + ' acceptIncomingConnection() replace current connection')
413
402
  // replace the current connection
414
403
  const oldConnection = this.connections.get(nodeId)!
@@ -521,13 +510,10 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
521
510
  })
522
511
  })
523
512
 
524
- this.doGracefullyDisconnectAsync(targetDescriptor, disconnectMode)
525
- .then(() => { return })
526
- .catch((e) => {
527
- logger.error(e)
528
- })
529
-
530
- await promise
513
+ await Promise.all([
514
+ promise,
515
+ this.doGracefullyDisconnectAsync(targetDescriptor, disconnectMode)
516
+ ])
531
517
  }
532
518
 
533
519
  private async doGracefullyDisconnectAsync(targetDescriptor: PeerDescriptor, disconnectMode: DisconnectMode): Promise<void> {
@@ -546,7 +532,7 @@ export class ConnectionManager extends EventEmitter<TransportEvents> implements
546
532
  }
547
533
  }
548
534
 
549
- public getAllConnectionPeerDescriptors(): PeerDescriptor[] {
535
+ public getConnections(): PeerDescriptor[] {
550
536
  return Array.from(this.connections.values())
551
537
  .filter((managedConnection: ManagedConnection) => managedConnection.isHandshakeCompleted())
552
538
  .map((managedConnection: ManagedConnection) => managedConnection.getPeerDescriptor()!)
@@ -10,13 +10,14 @@ import { Simulator } from './simulator/Simulator'
10
10
  import { SimulatorConnector } from './simulator/SimulatorConnector'
11
11
  import { IceServer, WebrtcConnector } from './webrtc/WebrtcConnector'
12
12
  import { WebsocketConnector, WebsocketConnectorConfig } from './websocket/WebsocketConnector'
13
+ import { DhtAddress } from '../identifiers'
13
14
 
14
15
  export interface ConnectorFacade {
15
16
  createConnection: (peerDescriptor: PeerDescriptor) => ManagedConnection
16
17
  getLocalPeerDescriptor: () => PeerDescriptor | undefined
17
18
  start: (
18
19
  onNewConnection: (connection: ManagedConnection) => boolean,
19
- hasConnection: (peerDescriptor: PeerDescriptor) => boolean,
20
+ hasConnection: (nodeId: DhtAddress) => boolean,
20
21
  autoCertifierTransport: ITransport
21
22
  ) => Promise<void>
22
23
  stop: () => Promise<void>
@@ -59,7 +60,7 @@ export class DefaultConnectorFacade implements ConnectorFacade {
59
60
 
60
61
  async start(
61
62
  onNewConnection: (connection: ManagedConnection) => boolean,
62
- hasConnection: (peerDescriptor: PeerDescriptor) => boolean,
63
+ hasConnection: (nodeId: DhtAddress) => boolean,
63
64
  autoCertifierTransport: ITransport
64
65
  ): Promise<void> {
65
66
  logger.trace(`Creating WebsocketConnectorRpcLocal`)
@@ -3,7 +3,7 @@ import { EventEmitter } from 'eventemitter3'
3
3
  import { v4 } from 'uuid'
4
4
  import { Message, HandshakeRequest, HandshakeResponse, MessageType, PeerDescriptor, HandshakeError } from '../proto/packages/dht/protos/DhtRpc'
5
5
  import { IConnection } from './IConnection'
6
- import { version } from '../../package.json'
6
+ import { version as localVersion } from '../../package.json'
7
7
  import { isCompatibleVersion } from '../helpers/versionCompatibility'
8
8
 
9
9
  // Used for backwards compatibility with older versions of the protocol that do not send version in handshakes
@@ -50,7 +50,7 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
50
50
  logger.trace('handshake response received')
51
51
  const handshake = message.body.handshakeResponse
52
52
  const sourceVersion = handshake.version ?? BEFORE_TESTNET_TWO_VERSION
53
- const error = !isCompatibleVersion(sourceVersion, version) ? HandshakeError.UNSUPPORTED_VERSION : undefined
53
+ const error = !isCompatibleVersion(sourceVersion, localVersion) ? HandshakeError.UNSUPPORTED_VERSION : undefined
54
54
  ?? handshake.error
55
55
  if (error !== undefined) {
56
56
  this.emit('handshakeFailed', error)
@@ -68,7 +68,7 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
68
68
  const outgoingHandshake: HandshakeRequest = {
69
69
  sourcePeerDescriptor: this.localPeerDescriptor,
70
70
  targetPeerDescriptor: remotePeerDescriptor,
71
- version
71
+ version: localVersion
72
72
  }
73
73
  const msg: Message = {
74
74
  serviceId: Handshaker.HANDSHAKER_SERVICE_ID,
@@ -87,7 +87,7 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
87
87
  const outgoingHandshakeResponse: HandshakeResponse = {
88
88
  sourcePeerDescriptor: this.localPeerDescriptor,
89
89
  error,
90
- version
90
+ version: localVersion
91
91
  }
92
92
  const msg: Message = {
93
93
  serviceId: Handshaker.HANDSHAKER_SERVICE_ID,
@@ -1,4 +1,4 @@
1
- import { UUID } from '../helpers/UUID'
1
+ import { BrandedString } from '@streamr/utils'
2
2
 
3
3
  export interface ConnectionEvents {
4
4
  data: (bytes: Uint8Array) => void
@@ -15,13 +15,7 @@ export enum ConnectionType {
15
15
  SIMULATOR_CLIENT = 'simulator-client',
16
16
  }
17
17
 
18
- export type ConnectionIDKey = string & { readonly __brand: 'connectionIDKey' } // Nominal typing
19
-
20
- export class ConnectionID extends UUID {
21
- toMapKey(): ConnectionIDKey {
22
- return this.toString() as ConnectionIDKey
23
- }
24
- }
18
+ export type ConnectionID = BrandedString<'ConnectionID'>
25
19
 
26
20
  export interface IConnection {
27
21
 
@@ -4,9 +4,9 @@ import { Handshaker } from './Handshaker'
4
4
  import { HandshakeError, PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
5
5
  import { Logger, runAndRaceEvents3, RunAndRaceEventsReturnType } from '@streamr/utils'
6
6
  import EventEmitter from 'eventemitter3'
7
- import { getNodeIdFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
8
7
  import { getNodeIdOrUnknownFromPeerDescriptor } from './ConnectionManager'
9
- import { DhtAddress } from '../identifiers'
8
+ import { DhtAddress, getNodeIdFromPeerDescriptor } from '../identifiers'
9
+ import { createRandomConnectionId } from './Connection'
10
10
 
11
11
  export interface ManagedConnectionEvents {
12
12
  managedData: (bytes: Uint8Array, remotePeerDescriptor: PeerDescriptor) => void
@@ -59,7 +59,7 @@ export class ManagedConnection extends EventEmitter<Events> {
59
59
  this.outgoingConnection = outgoingConnection
60
60
  this.incomingConnection = incomingConnection
61
61
  this.connectionType = connectionType
62
- this.connectionId = new ConnectionID()
62
+ this.connectionId = createRandomConnectionId()
63
63
 
64
64
  this.send = this.send.bind(this)
65
65
  this.onDisconnected = this.onDisconnected.bind(this)
@@ -8,6 +8,7 @@ import {
8
8
  import { ConnectionEvents, IConnection } from './IConnection'
9
9
  import { ClientWebsocket } from './websocket/ClientWebsocket'
10
10
  import { connectivityMethodToWebsocketUrl } from './websocket/WebsocketConnector'
11
+ import { isCompatibleVersion } from '../helpers/versionCompatibility'
11
12
 
12
13
  const logger = new Logger(module)
13
14
 
@@ -36,7 +37,8 @@ const CONNECTIVITY_CHECKER_TIMEOUT = 5000
36
37
 
37
38
  export const sendConnectivityRequest = async (
38
39
  request: ConnectivityRequest,
39
- entryPoint: PeerDescriptor
40
+ entryPoint: PeerDescriptor,
41
+ localVersion: string
40
42
  ): Promise<ConnectivityResponse> => {
41
43
  let outgoingConnection: IConnection
42
44
  const wsServerInfo = {
@@ -78,9 +80,14 @@ export const sendConnectivityRequest = async (
78
80
  if (message.body.oneofKind === 'connectivityResponse') {
79
81
  logger.debug('ConnectivityResponse received: ' + JSON.stringify(Message.toJson(message)))
80
82
  const connectivityResponseMessage = message.body.connectivityResponse
83
+ const remoteVersion = connectivityResponseMessage.version
81
84
  outgoingConnection!.off('data', listener)
82
85
  clearTimeout(timeoutId)
83
- resolve(connectivityResponseMessage)
86
+ if (isCompatibleVersion(localVersion, remoteVersion)) {
87
+ resolve(connectivityResponseMessage)
88
+ } else {
89
+ reject(`Invalid version: ${remoteVersion}`)
90
+ }
84
91
  } else {
85
92
  return
86
93
  }
@@ -1,4 +1,4 @@
1
- import { Logger } from '@streamr/utils'
1
+ import { ipv4ToNumber, Logger } from '@streamr/utils'
2
2
  import { v4 } from 'uuid'
3
3
  import {
4
4
  ConnectivityRequest, ConnectivityResponse,
@@ -9,6 +9,7 @@ import { CONNECTIVITY_CHECKER_SERVICE_ID, connectAsync } from './connectivityChe
9
9
  import { IConnection } from './IConnection'
10
10
  import { ServerWebsocket } from './websocket/ServerWebsocket'
11
11
  import { connectivityMethodToWebsocketUrl } from './websocket/WebsocketConnector'
12
+ import { version as localVersion } from '../../package.json'
12
13
 
13
14
  const logger = new Logger(module)
14
15
 
@@ -36,6 +37,7 @@ const handleIncomingConnectivityRequest = async (connection: ServerWebsocket, co
36
37
  let outgoingConnection: IConnection | undefined
37
38
  let connectivityResponseMessage: ConnectivityResponse | undefined
38
39
  const host = connectivityRequest.host ?? connection.getRemoteAddress()
40
+ const ipAddress = connection.getRemoteIp()
39
41
  try {
40
42
  const wsServerInfo = {
41
43
  host,
@@ -52,17 +54,22 @@ const handleIncomingConnectivityRequest = async (connection: ServerWebsocket, co
52
54
  logger.debug('error', { err })
53
55
  connectivityResponseMessage = {
54
56
  host,
55
- natType: NatType.UNKNOWN
57
+ natType: NatType.UNKNOWN,
58
+ ipAddress: ipv4ToNumber(ipAddress),
59
+ version: localVersion
56
60
  }
57
61
  }
58
62
  if (outgoingConnection) {
59
63
  // TODO should we have some handling for this floating promise?
60
64
  outgoingConnection.close(false)
61
65
  logger.trace('Connectivity test produced positive result, communicating reply to the requester ' + host + ':' + connectivityRequest.port)
66
+
62
67
  connectivityResponseMessage = {
63
68
  host,
64
69
  natType: NatType.OPEN_INTERNET,
65
- websocket: { host, port: connectivityRequest.port, tls: connectivityRequest.tls }
70
+ websocket: { host, port: connectivityRequest.port, tls: connectivityRequest.tls },
71
+ ipAddress: ipv4ToNumber(ipAddress),
72
+ version: localVersion
66
73
  }
67
74
  }
68
75
  const msg: Message = {
@@ -7,10 +7,9 @@ import { SimulatorConnection } from './SimulatorConnection'
7
7
  import { ConnectionID } from '../IConnection'
8
8
  import { Logger } from '@streamr/utils'
9
9
  import { getRegionDelayMatrix } from './pings'
10
- import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
11
10
  import Heap from 'heap'
12
11
  import { debugVars } from '../../helpers/debugHelpers'
13
- import { DhtAddress } from '../../identifiers'
12
+ import { DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
14
13
 
15
14
  const logger = new Logger(module)
16
15
 
@@ -4,7 +4,7 @@ import { Message, PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
4
4
  import { Connection } from '../Connection'
5
5
  import { Logger } from '@streamr/utils'
6
6
  import { protoToString } from '../../helpers/protoToString'
7
- import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
7
+ import { getNodeIdFromPeerDescriptor } from '../../identifiers'
8
8
 
9
9
  const logger = new Logger(module)
10
10
 
@@ -8,8 +8,7 @@ import { Logger } from '@streamr/utils'
8
8
  import { ManagedConnection } from '../ManagedConnection'
9
9
  import { Simulator } from './Simulator'
10
10
  import { SimulatorConnection } from './SimulatorConnection'
11
- import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
12
- import { DhtAddress } from '../../identifiers'
11
+ import { DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
13
12
 
14
13
  const logger = new Logger(module)
15
14
 
@@ -3,6 +3,7 @@ import { WebrtcConnectionEvents, IWebrtcConnection, RtcDescription } from './IWe
3
3
  import { IConnection, ConnectionID, ConnectionEvents, ConnectionType } from '../IConnection'
4
4
  import { Logger } from '@streamr/utils'
5
5
  import { IceServer } from './WebrtcConnector'
6
+ import { createRandomConnectionId } from '../Connection'
6
7
 
7
8
  const logger = new Logger(module)
8
9
 
@@ -34,7 +35,7 @@ export class NodeWebrtcConnection extends EventEmitter<Events> implements IWebrt
34
35
 
35
36
  constructor(params: Params) {
36
37
  super()
37
- this.connectionId = new ConnectionID()
38
+ this.connectionId = createRandomConnectionId()
38
39
  this.iceServers = params.iceServers ?? []
39
40
  }
40
41
 
@@ -222,7 +223,7 @@ export class NodeWebrtcConnection extends EventEmitter<Events> implements IWebrt
222
223
  this.emit('connected')
223
224
  }
224
225
 
225
- public setConnectionId(connectionID: string): void {
226
- this.connectionId = new ConnectionID(connectionID)
226
+ public setConnectionId(connectionId: ConnectionID): void {
227
+ this.connectionId = connectionId
227
228
  }
228
229
  }
@@ -5,10 +5,11 @@ import EventEmitter from 'eventemitter3'
5
5
  import nodeDatachannel, { DataChannel, DescriptionType, PeerConnection } from 'node-datachannel'
6
6
  import { Logger } from '@streamr/utils'
7
7
  import { IllegalRtcPeerConnectionState } from '../../helpers/errors'
8
- import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
9
8
  import { iceServerAsString } from './iceServerAsString'
10
9
  import { IceServer } from './WebrtcConnector'
11
10
  import { PortRange } from '../ConnectionManager'
11
+ import { getNodeIdFromPeerDescriptor } from '../../identifiers'
12
+ import { createRandomConnectionId } from '../Connection'
12
13
 
13
14
  const logger = new Logger(module)
14
15
 
@@ -67,7 +68,7 @@ export class NodeWebrtcConnection extends EventEmitter<Events> implements IConne
67
68
 
68
69
  constructor(params: Params) {
69
70
  super()
70
- this.connectionId = new ConnectionID()
71
+ this.connectionId = createRandomConnectionId()
71
72
  this.iceServers = params.iceServers ?? []
72
73
  // eslint-disable-next-line no-underscore-dangle
73
74
  this._bufferThresholdHigh = params.bufferThresholdHigh ?? 2 ** 17
@@ -253,7 +254,7 @@ export class NodeWebrtcConnection extends EventEmitter<Events> implements IConne
253
254
  return !this.closed && this.lastState === 'connected' && !!this.dataChannel
254
255
  }
255
256
 
256
- public setConnectionId(connectionID: string): void {
257
- this.connectionId = new ConnectionID(connectionID)
257
+ public setConnectionId(connectionId: ConnectionID): void {
258
+ this.connectionId = connectionId
258
259
  }
259
260
  }
@@ -13,15 +13,11 @@ import { ManagedWebrtcConnection } from '../ManagedWebrtcConnection'
13
13
  import { Logger } from '@streamr/utils'
14
14
  import * as Err from '../../helpers/errors'
15
15
  import { ManagedConnection } from '../ManagedConnection'
16
- import {
17
- areEqualPeerDescriptors,
18
- getNodeIdFromPeerDescriptor,
19
- peerIdFromPeerDescriptor
20
- } from '../../helpers/peerIdFromPeerDescriptor'
21
16
  import { PortRange } from '../ConnectionManager'
22
17
  import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
23
18
  import { WebrtcConnectorRpcLocal } from './WebrtcConnectorRpcLocal'
24
- import { DhtAddress } from '../../identifiers'
19
+ import { DhtAddress, areEqualPeerDescriptors, getNodeIdFromPeerDescriptor } from '../../identifiers'
20
+ import { getOfferer } from '../../helpers/offering'
25
21
 
26
22
  const logger = new Logger(module)
27
23
 
@@ -146,7 +142,9 @@ export class WebrtcConnector {
146
142
  portRange: this.config.portRange
147
143
  })
148
144
 
149
- const offering = this.isOffering(targetPeerDescriptor)
145
+ const localNodeId = getNodeIdFromPeerDescriptor(this.localPeerDescriptor!)
146
+ const targetNodeId = getNodeIdFromPeerDescriptor(targetPeerDescriptor)
147
+ const offering = (getOfferer(localNodeId, targetNodeId) === 'local')
150
148
  let managedConnection: ManagedWebrtcConnection
151
149
 
152
150
  if (offering) {
@@ -157,7 +155,7 @@ export class WebrtcConnector {
157
155
 
158
156
  managedConnection.setRemotePeerDescriptor(targetPeerDescriptor)
159
157
 
160
- this.ongoingConnectAttempts.set(getNodeIdFromPeerDescriptor(targetPeerDescriptor), managedConnection)
158
+ this.ongoingConnectAttempts.set(targetNodeId, managedConnection)
161
159
 
162
160
  const delFunc = () => {
163
161
  this.ongoingConnectAttempts.delete(nodeId)
@@ -179,16 +177,16 @@ export class WebrtcConnector {
179
177
  candidate = replaceInternalIpWithExternalIp(candidate, this.config.externalIp)
180
178
  logger.debug(`onLocalCandidate injected external ip ${candidate} ${mid}`)
181
179
  }
182
- remoteConnector.sendIceCandidate(candidate, mid, connection.connectionId.toString())
180
+ remoteConnector.sendIceCandidate(candidate, mid, connection.connectionId)
183
181
  })
184
182
 
185
183
  if (offering) {
186
184
  connection.once('localDescription', (description: string) => {
187
- remoteConnector.sendRtcOffer(description, connection.connectionId.toString())
185
+ remoteConnector.sendRtcOffer(description, connection.connectionId)
188
186
  })
189
187
  } else {
190
188
  connection.once('localDescription', (description: string) => {
191
- remoteConnector.sendRtcAnswer(description, connection.connectionId.toString())
189
+ remoteConnector.sendRtcAnswer(description, connection.connectionId)
192
190
  })
193
191
  }
194
192
 
@@ -214,10 +212,4 @@ export class WebrtcConnector {
214
212
 
215
213
  this.rpcCommunicator.destroy()
216
214
  }
217
-
218
- public isOffering(targetPeerDescriptor: PeerDescriptor): boolean {
219
- const myId = peerIdFromPeerDescriptor(this.localPeerDescriptor!)
220
- const theirId = peerIdFromPeerDescriptor(targetPeerDescriptor)
221
- return myId.hasSmallerHashThan(theirId)
222
- }
223
215
  }
@@ -1,7 +1,6 @@
1
1
  import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
2
2
  import { Logger } from '@streamr/utils'
3
3
  import { getAddressFromIceCandidate, isPrivateIPv4 } from '../../helpers/AddressTools'
4
- import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
5
4
  import { Empty } from '../../proto/google/protobuf/empty'
6
5
  import {
7
6
  HandshakeError,
@@ -18,9 +17,10 @@ import { ManagedConnection } from '../ManagedConnection'
18
17
  import { ManagedWebrtcConnection } from '../ManagedWebrtcConnection'
19
18
  import { NodeWebrtcConnection } from './NodeWebrtcConnection'
20
19
  import { WebrtcConnectorRpcRemote } from './WebrtcConnectorRpcRemote'
21
- import { DhtAddress } from '../../identifiers'
22
- import { version } from '../../../package.json'
20
+ import { DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
21
+ import { version as localVersion } from '../../../package.json'
23
22
  import { isCompatibleVersion } from '../../helpers/versionCompatibility'
23
+ import { ConnectionID } from '../IConnection'
24
24
 
25
25
  const logger = new Logger(module)
26
26
 
@@ -72,23 +72,23 @@ export class WebrtcConnectorRpcLocal implements IWebrtcConnectorRpc {
72
72
  WebrtcConnectorRpcClient
73
73
  )
74
74
  connection.on('localCandidate', (candidate: string, mid: string) => {
75
- remoteConnector.sendIceCandidate(candidate, mid, connection!.connectionId.toString())
75
+ remoteConnector.sendIceCandidate(candidate, mid, connection!.connectionId)
76
76
  })
77
77
  connection.once('localDescription', (description: string) => {
78
- remoteConnector.sendRtcAnswer(description, connection!.connectionId.toString())
78
+ remoteConnector.sendRtcAnswer(description, connection!.connectionId)
79
79
  })
80
80
  connection.start(false)
81
81
  }
82
82
 
83
83
  // Always use offerers connectionId
84
- connection!.setConnectionId(request.connectionId)
84
+ connection!.setConnectionId(request.connectionId as ConnectionID)
85
85
  connection!.setRemoteDescription(request.description, 'offer')
86
86
 
87
87
  managedConnection.on('handshakeRequest', (_sourceDescriptor: PeerDescriptor, sourceVersion: string) => {
88
88
  if (this.config.ongoingConnectAttempts.has(nodeId)) {
89
89
  this.config.ongoingConnectAttempts.delete(nodeId)
90
90
  }
91
- if (!isCompatibleVersion(sourceVersion, version)) {
91
+ if (!isCompatibleVersion(sourceVersion, localVersion)) {
92
92
  managedConnection!.rejectHandshake(HandshakeError.UNSUPPORTED_VERSION)
93
93
  } else {
94
94
  managedConnection!.acceptHandshake()
@@ -103,7 +103,7 @@ export class WebrtcConnectorRpcLocal implements IWebrtcConnectorRpc {
103
103
  const connection = this.config.ongoingConnectAttempts.get(nodeId)?.getWebrtcConnection()
104
104
  if (!connection) {
105
105
  return {}
106
- } else if (connection.connectionId.toString() !== request.connectionId) {
106
+ } else if (connection.connectionId !== request.connectionId) {
107
107
  logger.trace(`Ignoring RTC answer due to connectionId mismatch`)
108
108
  return {}
109
109
  }
@@ -117,7 +117,7 @@ export class WebrtcConnectorRpcLocal implements IWebrtcConnectorRpc {
117
117
  const connection = this.config.ongoingConnectAttempts.get(nodeId)?.getWebrtcConnection()
118
118
  if (!connection) {
119
119
  return {}
120
- } else if (connection.connectionId.toString() !== request.connectionId) {
120
+ } else if (connection.connectionId !== request.connectionId) {
121
121
  logger.trace(`Ignoring remote candidate due to connectionId mismatch`)
122
122
  return {}
123
123
  } else if (this.isIceCandidateAllowed(request.candidate)) {
@@ -2,6 +2,7 @@ import { Logger } from '@streamr/utils'
2
2
  import EventEmitter from 'eventemitter3'
3
3
  import { ICloseEvent, IMessageEvent, w3cwebsocket as Websocket } from 'websocket'
4
4
  import { ConnectionEvents, ConnectionID, ConnectionType, IConnection } from '../IConnection'
5
+ import { createRandomConnectionId } from '../Connection'
5
6
 
6
7
  const logger = new Logger(module)
7
8
 
@@ -24,7 +25,7 @@ export class ClientWebsocket extends EventEmitter<ConnectionEvents> implements I
24
25
 
25
26
  constructor() {
26
27
  super()
27
- this.connectionId = new ConnectionID()
28
+ this.connectionId = createRandomConnectionId()
28
29
  }
29
30
 
30
31
  // TODO explicit default value for "selfSigned" or make it required
@@ -95,7 +96,7 @@ export class ClientWebsocket extends EventEmitter<ConnectionEvents> implements I
95
96
  this.emit('disconnected', gracefulLeave, undefined, 'close() called')
96
97
  this.removeAllListeners()
97
98
  if (!this.destroyed) {
98
- logger.trace(`Closing socket for connection ${this.connectionId.toString()}`)
99
+ logger.trace(`Closing socket for connection ${this.connectionId}`)
99
100
  this.socket?.close(gracefulLeave ? CUSTOM_GOING_AWAY : undefined)
100
101
  } else {
101
102
  logger.debug('Tried to close() a stopped connection')