@streamr/dht 102.0.0-beta.0 → 102.0.0-beta.2

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 (264) hide show
  1. package/dist/generated/packages/dht/protos/DhtRpc.d.ts +16 -8
  2. package/dist/generated/packages/dht/protos/DhtRpc.js +7 -5
  3. package/dist/generated/packages/dht/protos/DhtRpc.js.map +1 -1
  4. package/dist/package.json +15 -16
  5. package/dist/src/connection/ConnectionManager.js +18 -8
  6. package/dist/src/connection/ConnectionManager.js.map +1 -1
  7. package/dist/src/connection/Handshaker.d.ts +1 -1
  8. package/dist/src/connection/Handshaker.js +9 -5
  9. package/dist/src/connection/Handshaker.js.map +1 -1
  10. package/dist/src/connection/ManagedConnection.js +17 -7
  11. package/dist/src/connection/ManagedConnection.js.map +1 -1
  12. package/dist/src/connection/connectivityChecker.js +20 -10
  13. package/dist/src/connection/connectivityChecker.js.map +1 -1
  14. package/dist/src/connection/connectivityRequestHandler.js +3 -3
  15. package/dist/src/connection/connectivityRequestHandler.js.map +1 -1
  16. package/dist/src/connection/simulator/Simulator.js +3 -2
  17. package/dist/src/connection/simulator/Simulator.js.map +1 -1
  18. package/dist/src/connection/simulator/pings.d.ts +1 -1
  19. package/dist/src/connection/simulator/pings.js +3 -3
  20. package/dist/src/connection/simulator/pings.js.map +1 -1
  21. package/dist/src/connection/webrtc/NodeWebrtcConnection.js +0 -2
  22. package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
  23. package/dist/src/connection/webrtc/WebrtcConnector.js +19 -9
  24. package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
  25. package/dist/src/connection/webrtc/iceServerAsString.js +1 -2
  26. package/dist/src/connection/webrtc/iceServerAsString.js.map +1 -1
  27. package/dist/src/connection/websocket/AbstractWebsocketClientConnection.d.ts +0 -2
  28. package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.d.ts +0 -1
  29. package/dist/src/connection/websocket/WebsocketServerConnection.d.ts +0 -1
  30. package/dist/src/connection/websocket/WebsocketServerConnector.js +28 -18
  31. package/dist/src/connection/websocket/WebsocketServerConnector.js.map +1 -1
  32. package/dist/src/dht/DhtNode.d.ts +1 -0
  33. package/dist/src/dht/DhtNode.js +3 -2
  34. package/dist/src/dht/DhtNode.js.map +1 -1
  35. package/dist/src/dht/DhtNodeRpcLocal.d.ts +1 -1
  36. package/dist/src/dht/PeerManager.d.ts +2 -1
  37. package/dist/src/dht/PeerManager.js +2 -1
  38. package/dist/src/dht/PeerManager.js.map +1 -1
  39. package/dist/src/dht/contact/SortedContactList.js +1 -1
  40. package/dist/src/dht/contact/SortedContactList.js.map +1 -1
  41. package/dist/src/dht/discovery/DiscoverySession.d.ts +0 -1
  42. package/dist/src/dht/discovery/PeerDiscovery.d.ts +0 -1
  43. package/dist/src/dht/discovery/RingDiscoverySession.d.ts +0 -1
  44. package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +2 -2
  45. package/dist/src/dht/routing/DuplicateDetector.js.map +1 -1
  46. package/dist/src/dht/routing/RoutingSession.js +2 -2
  47. package/dist/src/dht/routing/RoutingSession.js.map +1 -1
  48. package/dist/src/dht/routing/RoutingTablesCache.d.ts +1 -1
  49. package/dist/src/dht/store/LocalDataStore.js +1 -1
  50. package/dist/src/dht/store/LocalDataStore.js.map +1 -1
  51. package/dist/src/dht/store/StoreManager.d.ts +1 -1
  52. package/dist/src/dht/store/StoreRpcLocal.d.ts +1 -1
  53. package/dist/src/helpers/AddressTools.js +2 -3
  54. package/dist/src/helpers/AddressTools.js.map +1 -1
  55. package/dist/src/helpers/debugHelpers.js +2 -2
  56. package/dist/src/helpers/debugHelpers.js.map +1 -1
  57. package/dist/src/helpers/protoClasses.d.ts +1 -1
  58. package/dist/src/helpers/protoClasses.js.map +1 -1
  59. package/dist/src/helpers/protoToString.js +1 -2
  60. package/dist/src/helpers/protoToString.js.map +1 -1
  61. package/dist/src/helpers/version.d.ts +1 -1
  62. package/dist/src/helpers/version.js +4 -4
  63. package/dist/src/helpers/version.js.map +1 -1
  64. package/eslint.config.mjs +12 -0
  65. package/jest.config.ts +12 -0
  66. package/package.json +15 -16
  67. package/protos/DhtRpc.proto +6 -4
  68. package/.eslintignore +0 -5
  69. package/.eslintrc +0 -3
  70. package/generated/google/protobuf/any.ts +0 -326
  71. package/generated/google/protobuf/empty.ts +0 -81
  72. package/generated/google/protobuf/timestamp.ts +0 -287
  73. package/generated/packages/dht/protos/DhtRpc.client.ts +0 -419
  74. package/generated/packages/dht/protos/DhtRpc.server.ts +0 -165
  75. package/generated/packages/dht/protos/DhtRpc.ts +0 -1266
  76. package/generated/packages/proto-rpc/protos/ProtoRpc.ts +0 -108
  77. package/jest.config.js +0 -5
  78. package/src/connection/Connection.ts +0 -28
  79. package/src/connection/ConnectionLockRpcLocal.ts +0 -78
  80. package/src/connection/ConnectionLockRpcRemote.ts +0 -64
  81. package/src/connection/ConnectionLockStates.ts +0 -131
  82. package/src/connection/ConnectionManager.ts +0 -661
  83. package/src/connection/ConnectionsView.ts +0 -8
  84. package/src/connection/ConnectorFacade.ts +0 -217
  85. package/src/connection/Handshaker.ts +0 -205
  86. package/src/connection/IConnection.ts +0 -40
  87. package/src/connection/ManagedConnection.ts +0 -113
  88. package/src/connection/OutputBuffer.ts +0 -28
  89. package/src/connection/PendingConnection.ts +0 -68
  90. package/src/connection/connectivityChecker.ts +0 -108
  91. package/src/connection/connectivityRequestHandler.ts +0 -116
  92. package/src/connection/simulator/Simulator.ts +0 -368
  93. package/src/connection/simulator/SimulatorConnection.ts +0 -137
  94. package/src/connection/simulator/SimulatorConnector.ts +0 -98
  95. package/src/connection/simulator/SimulatorTransport.ts +0 -15
  96. package/src/connection/simulator/pings.ts +0 -42
  97. package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -242
  98. package/src/connection/webrtc/IWebrtcConnection.ts +0 -24
  99. package/src/connection/webrtc/NodeWebrtcConnection.ts +0 -247
  100. package/src/connection/webrtc/WebrtcConnector.ts +0 -234
  101. package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +0 -108
  102. package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +0 -60
  103. package/src/connection/webrtc/iceServerAsString.ts +0 -15
  104. package/src/connection/websocket/AbstractWebsocketClientConnection.ts +0 -122
  105. package/src/connection/websocket/AutoCertifierClientFacade.ts +0 -89
  106. package/src/connection/websocket/BrowserWebsocketClientConnection.ts +0 -44
  107. package/src/connection/websocket/NodeWebsocketClientConnection.ts +0 -39
  108. package/src/connection/websocket/WebsocketClientConnector.ts +0 -119
  109. package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +0 -38
  110. package/src/connection/websocket/WebsocketClientConnectorRpcRemote.ts +0 -19
  111. package/src/connection/websocket/WebsocketServer.ts +0 -164
  112. package/src/connection/websocket/WebsocketServerConnection.ts +0 -109
  113. package/src/connection/websocket/WebsocketServerConnector.ts +0 -286
  114. package/src/dht/DhtNode.ts +0 -678
  115. package/src/dht/DhtNodeRpcLocal.ts +0 -84
  116. package/src/dht/DhtNodeRpcRemote.ts +0 -107
  117. package/src/dht/ExternalApiRpcLocal.ts +0 -58
  118. package/src/dht/ExternalApiRpcRemote.ts +0 -41
  119. package/src/dht/PeerManager.ts +0 -303
  120. package/src/dht/contact/Contact.ts +0 -19
  121. package/src/dht/contact/ContactList.ts +0 -43
  122. package/src/dht/contact/RandomContactList.ts +0 -56
  123. package/src/dht/contact/RingContactList.ts +0 -143
  124. package/src/dht/contact/RpcRemote.ts +0 -72
  125. package/src/dht/contact/SortedContactList.ts +0 -173
  126. package/src/dht/contact/getClosestNodes.ts +0 -24
  127. package/src/dht/contact/ringIdentifiers.ts +0 -62
  128. package/src/dht/discovery/DiscoverySession.ts +0 -129
  129. package/src/dht/discovery/PeerDiscovery.ts +0 -244
  130. package/src/dht/discovery/RingDiscoverySession.ts +0 -148
  131. package/src/dht/recursive-operation/RecursiveOperationManager.ts +0 -251
  132. package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +0 -34
  133. package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +0 -43
  134. package/src/dht/recursive-operation/RecursiveOperationSession.ts +0 -231
  135. package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +0 -35
  136. package/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.ts +0 -30
  137. package/src/dht/routing/DuplicateDetector.ts +0 -34
  138. package/src/dht/routing/Router.ts +0 -246
  139. package/src/dht/routing/RouterRpcLocal.ts +0 -78
  140. package/src/dht/routing/RouterRpcRemote.ts +0 -80
  141. package/src/dht/routing/RoutingSession.ts +0 -243
  142. package/src/dht/routing/RoutingTablesCache.ts +0 -60
  143. package/src/dht/routing/getPreviousPeer.ts +0 -6
  144. package/src/dht/store/LocalDataStore.ts +0 -84
  145. package/src/dht/store/StoreManager.ts +0 -170
  146. package/src/dht/store/StoreRpcLocal.ts +0 -89
  147. package/src/dht/store/StoreRpcRemote.ts +0 -32
  148. package/src/exports.ts +0 -33
  149. package/src/helpers/AddressTools.ts +0 -28
  150. package/src/helpers/Connectivity.ts +0 -19
  151. package/src/helpers/browser/isBrowserEnvironment.ts +0 -1
  152. package/src/helpers/browser/isBrowserEnvironment_override.ts +0 -3
  153. package/src/helpers/createPeerDescriptor.ts +0 -57
  154. package/src/helpers/createPeerDescriptorSignaturePayload.ts +0 -28
  155. package/src/helpers/debugHelpers.ts +0 -9
  156. package/src/helpers/errors.ts +0 -49
  157. package/src/helpers/offering.ts +0 -15
  158. package/src/helpers/protoClasses.ts +0 -57
  159. package/src/helpers/protoToString.ts +0 -21
  160. package/src/helpers/version.ts +0 -32
  161. package/src/identifiers.ts +0 -29
  162. package/src/rpc-protocol/DhtCallContext.ts +0 -14
  163. package/src/rpc-protocol/DhtRpcOptions.ts +0 -10
  164. package/src/transport/ITransport.ts +0 -37
  165. package/src/transport/ListeningRpcCommunicator.ts +0 -32
  166. package/src/transport/RoutingRpcCommunicator.ts +0 -66
  167. package/src/types/ServiceID.ts +0 -1
  168. package/src/types/textencoding.d.ts +0 -6
  169. package/test/benchmark/Find.test.ts +0 -72
  170. package/test/benchmark/KademliaCorrectness.test.ts +0 -114
  171. package/test/benchmark/RingCorrectness.test.ts +0 -157
  172. package/test/benchmark/SortedContactListBenchmark.test.ts +0 -108
  173. package/test/benchmark/WebsocketServerMemoryLeak.test.ts +0 -41
  174. package/test/benchmark/hybrid-network-simulation/RingContactList.test.ts +0 -71
  175. package/test/end-to-end/GeoIpLayer0.test.ts +0 -55
  176. package/test/end-to-end/Layer0-Layer1.test.ts +0 -93
  177. package/test/end-to-end/Layer0.test.ts +0 -76
  178. package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +0 -110
  179. package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +0 -137
  180. package/test/end-to-end/Layer0Webrtc.test.ts +0 -85
  181. package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +0 -82
  182. package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +0 -76
  183. package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +0 -52
  184. package/test/end-to-end/WebsocketConnectionRequest.test.ts +0 -69
  185. package/test/end-to-end/memory-leak.test.ts +0 -80
  186. package/test/integration/ConnectionLocking.test.ts +0 -182
  187. package/test/integration/ConnectionManager.test.ts +0 -528
  188. package/test/integration/ConnectivityChecking.test.ts +0 -53
  189. package/test/integration/DhtJoinPeerDiscovery.test.ts +0 -49
  190. package/test/integration/DhtNode.test.ts +0 -66
  191. package/test/integration/DhtNodeExternalAPI.test.ts +0 -48
  192. package/test/integration/DhtNodeRpcRemote.test.ts +0 -66
  193. package/test/integration/DhtRpc.test.ts +0 -121
  194. package/test/integration/Find.test.ts +0 -45
  195. package/test/integration/GeoIpConnectivityChecking.test.ts +0 -72
  196. package/test/integration/Layer1-scale.test.ts +0 -189
  197. package/test/integration/Mock-Layer1-Layer0.test.ts +0 -85
  198. package/test/integration/MultipleEntryPointJoining.test.ts +0 -105
  199. package/test/integration/ReplicateData.test.ts +0 -104
  200. package/test/integration/RouteMessage.test.ts +0 -230
  201. package/test/integration/RouterRpcRemote.test.ts +0 -77
  202. package/test/integration/SimultaneousConnections.test.ts +0 -316
  203. package/test/integration/Store.test.ts +0 -85
  204. package/test/integration/StoreAndDelete.test.ts +0 -77
  205. package/test/integration/StoreOnDhtWithThreeNodes.test.ts +0 -59
  206. package/test/integration/StoreOnDhtWithTwoNodes.test.ts +0 -51
  207. package/test/integration/StoreRpcRemote.test.ts +0 -54
  208. package/test/integration/WebrtcConnectionManagement.test.ts +0 -219
  209. package/test/integration/WebrtcConnectorRpc.test.ts +0 -125
  210. package/test/integration/Websocket.test.ts +0 -65
  211. package/test/integration/WebsocketClientConnectorRpc.test.ts +0 -69
  212. package/test/integration/WebsocketConnectionManagement.test.ts +0 -191
  213. package/test/integration/rpc-connections-over-webrtc.test.ts +0 -158
  214. package/test/kademlia-simulation/data/nodeids.json +0 -13002
  215. package/test/kademlia-simulation/data/orderedneighbors.json +0 -1001
  216. package/test/types/global.d.ts +0 -1
  217. package/test/unit/AddressTools.test.ts +0 -44
  218. package/test/unit/AutoCertifierClientFacade.test.ts +0 -58
  219. package/test/unit/ConnectionManager.test.ts +0 -65
  220. package/test/unit/ConnectivityHelpers.test.ts +0 -61
  221. package/test/unit/DiscoverySession.test.ts +0 -87
  222. package/test/unit/DuplicateDetector.test.ts +0 -31
  223. package/test/unit/Handshaker.test.ts +0 -169
  224. package/test/unit/ListeningRpcCommunicator.test.ts +0 -52
  225. package/test/unit/LocalDataStore.test.ts +0 -108
  226. package/test/unit/ManagedConnection.test.ts +0 -58
  227. package/test/unit/PeerManager.test.ts +0 -93
  228. package/test/unit/PendingConnection.test.ts +0 -57
  229. package/test/unit/ProtobufMessage.test.ts +0 -21
  230. package/test/unit/RandomContactList.test.ts +0 -58
  231. package/test/unit/RecursiveOperationManager.test.ts +0 -161
  232. package/test/unit/RecursiveOperationSession.test.ts +0 -68
  233. package/test/unit/Router.test.ts +0 -137
  234. package/test/unit/RoutingSession.test.ts +0 -79
  235. package/test/unit/SortedContactList.test.ts +0 -115
  236. package/test/unit/StoreManager.test.ts +0 -146
  237. package/test/unit/StoreRpcLocal.test.ts +0 -167
  238. package/test/unit/WebrtcConnection.test.ts +0 -29
  239. package/test/unit/WebrtcConnector.test.ts +0 -56
  240. package/test/unit/WebsocketClientConnector.test.ts +0 -101
  241. package/test/unit/WebsocketServer.test.ts +0 -66
  242. package/test/unit/WebsocketServerConnector.test.ts +0 -102
  243. package/test/unit/connectivityRequestHandler.test.ts +0 -104
  244. package/test/unit/createPeerDescriptor.test.ts +0 -69
  245. package/test/unit/customMatchers.test.ts +0 -16
  246. package/test/unit/getClosestNodes.test.ts +0 -30
  247. package/test/unit/version.test.ts +0 -18
  248. package/test/unit/webrtcReplaceInternalIpWithExternalIp.test.ts +0 -18
  249. package/test/utils/FakeConnectorFacade.ts +0 -41
  250. package/test/utils/FakeRpcCommunicator.ts +0 -23
  251. package/test/utils/FakeTransport.ts +0 -79
  252. package/test/utils/customMatchers.ts +0 -71
  253. package/test/utils/mock/MockConnection.ts +0 -26
  254. package/test/utils/mock/MockConnectionsView.ts +0 -18
  255. package/test/utils/mock/MockRouter.ts +0 -62
  256. package/test/utils/mock/MockRpcCommunicator.ts +0 -7
  257. package/test/utils/mock/MockTransport.ts +0 -26
  258. package/test/utils/mock/mockDataEntry.ts +0 -38
  259. package/test/utils/topology.ts +0 -80
  260. package/test/utils/utils.ts +0 -268
  261. package/tsconfig.browser.json +0 -17
  262. package/tsconfig.jest.json +0 -25
  263. package/tsconfig.json +0 -3
  264. package/tsconfig.node.json +0 -24
@@ -1,58 +0,0 @@
1
- import { wait } from '@streamr/utils'
2
- import { ManagedConnection } from '../../src/exports'
3
- import { MockConnection } from '../utils/mock/MockConnection'
4
- import { createMockPeerDescriptor } from '../utils/utils'
5
-
6
- describe('ManagedConnection', () => {
7
-
8
- let managedConnection: ManagedConnection
9
- let connection: MockConnection
10
- beforeEach(() => {
11
- connection = new MockConnection()
12
- managedConnection = new ManagedConnection(createMockPeerDescriptor(), connection)
13
- })
14
-
15
- afterEach(() => {
16
- managedConnection.close(false)
17
- })
18
-
19
- it('emits disconnected after close', (done) => {
20
- managedConnection.once('disconnected', (graceful) => {
21
- expect(graceful).toBe(true)
22
- done()
23
- })
24
- managedConnection.close(true)
25
- })
26
-
27
- it('sends data', () => {
28
- const data = new Uint8Array([1, 2, 3])
29
- managedConnection.send(data)
30
- expect(connection.sentData[0]).toEqual(data)
31
- })
32
-
33
- it('emits data', (done) => {
34
- const data = new Uint8Array([1, 2, 3])
35
- managedConnection.on('managedData', (data) => {
36
- expect(data).toEqual(data)
37
- done()
38
- })
39
- connection.emitData(data)
40
- })
41
-
42
- it('sets lastUsedTimestamp on send', async () => {
43
- const createdTimestamp = managedConnection.getLastUsedTimestamp()
44
- await wait(5)
45
- managedConnection.send(new Uint8Array([1, 2, 3]))
46
- expect(managedConnection.getLastUsedTimestamp()).toBeGreaterThan(createdTimestamp)
47
- })
48
-
49
- it('replace as duplicate', async () => {
50
- managedConnection.once('disconnected', () => {
51
- throw new Error('disconnected')
52
- })
53
- managedConnection.replaceAsDuplicate()
54
- managedConnection.close(true)
55
- await wait(50)
56
- })
57
-
58
- })
@@ -1,93 +0,0 @@
1
- import { waitForCondition } from '@streamr/utils'
2
- import { range, sample, sampleSize } from 'lodash'
3
- import { DhtNodeRpcRemote } from '../../src/dht/DhtNodeRpcRemote'
4
- import { PeerManager } from '../../src/dht/PeerManager'
5
- import { getClosestNodes } from '../../src/dht/contact/getClosestNodes'
6
- import { DhtAddress, randomDhtAddress, toNodeId, toDhtAddressRaw } from '../../src/identifiers'
7
- import { NodeType, PeerDescriptor } from '../../generated/packages/dht/protos/DhtRpc'
8
- import { MockRpcCommunicator } from '../utils/mock/MockRpcCommunicator'
9
- import { createMockPeerDescriptor } from '../utils/utils'
10
-
11
- const createDhtNodeRpcRemote = (
12
- peerDescriptor: PeerDescriptor,
13
- localPeerDescriptor: PeerDescriptor,
14
- pingFailures: Set<DhtAddress>
15
- ) => {
16
- const remote = new class extends DhtNodeRpcRemote {
17
- // eslint-disable-next-line class-methods-use-this
18
- async ping(): Promise<boolean> {
19
- return !pingFailures.has(toNodeId(peerDescriptor))
20
- }
21
- }(localPeerDescriptor, peerDescriptor, undefined as any, new MockRpcCommunicator())
22
- return remote
23
- }
24
-
25
- const createPeerManager = (
26
- nodeIds: DhtAddress[],
27
- localPeerDescriptor = createMockPeerDescriptor(),
28
- pingFailures: Set<DhtAddress> = new Set()
29
- ) => {
30
- const manager = new PeerManager({
31
- localNodeId: toNodeId(localPeerDescriptor),
32
- localPeerDescriptor: localPeerDescriptor,
33
- isLayer0: true,
34
- createDhtNodeRpcRemote: (peerDescriptor: PeerDescriptor) => createDhtNodeRpcRemote(peerDescriptor, localPeerDescriptor, pingFailures),
35
- hasConnection: () => false
36
- } as any)
37
- const contacts = nodeIds.map((n) => ({ nodeId: toDhtAddressRaw(n), type: NodeType.NODEJS }))
38
- for (const contact of contacts) {
39
- manager.addContact(contact)
40
- }
41
- return manager
42
- }
43
-
44
- describe('PeerManager', () => {
45
-
46
- it('getNearbyContactCount', () => {
47
- const nodeIds = range(10).map(() => randomDhtAddress())
48
- const manager = createPeerManager(nodeIds)
49
- expect(manager.getNearbyContactCount()).toBe(10)
50
- expect(manager.getNearbyContactCount(new Set(sampleSize(nodeIds, 2)))).toBe(8)
51
- expect(manager.getNearbyContactCount(new Set([sample(nodeIds)!, randomDhtAddress()]))).toBe(9)
52
- })
53
-
54
- it('addContact: ping fails', async () => {
55
- const localPeerDescriptor = createMockPeerDescriptor()
56
- const successContacts = range(5).map(() => createMockPeerDescriptor())
57
- const failureContact = createMockPeerDescriptor()
58
- const manager = createPeerManager([], localPeerDescriptor, new Set([toNodeId(failureContact)]))
59
- for (const successContact of successContacts) {
60
- manager.addContact(successContact)
61
- manager.setContactActive(toNodeId(successContact))
62
- manager.removeNeighbor(toNodeId(successContact))
63
- }
64
- expect(manager.getNeighborCount()).toBe(0)
65
- manager.addContact(failureContact)
66
- const closesSuccessContact = getClosestNodes(toNodeId(localPeerDescriptor), successContacts)[0]
67
- await waitForCondition(() => {
68
- const neighborNodeIds = manager.getNeighbors().map((n) => n.getNodeId())
69
- return neighborNodeIds.includes(toNodeId(closesSuccessContact))
70
- })
71
- expect(manager.getNeighborCount()).toBe(1)
72
- expect(manager.getNeighbors()[0].getPeerDescriptor()).toEqualPeerDescriptor(closesSuccessContact)
73
- })
74
-
75
- it('pruneOfflineNodes removes offline nodes', async () => {
76
- const localPeerDescriptor = createMockPeerDescriptor()
77
- const successContacts = range(5).map(() => createMockPeerDescriptor())
78
- const failureContact = createMockPeerDescriptor()
79
- const failureSet: Set<DhtAddress> = new Set()
80
- const manager = createPeerManager([], localPeerDescriptor, failureSet)
81
- for (const successContact of successContacts) {
82
- manager.addContact(successContact)
83
- }
84
- manager.addContact(failureContact)
85
- expect(manager.getNeighborCount()).toBe(6)
86
- failureSet.add(toNodeId(failureContact))
87
- await manager.pruneOfflineNodes(
88
- manager.getNeighbors().map((node) => createDhtNodeRpcRemote(node.getPeerDescriptor(), localPeerDescriptor, failureSet))
89
- )
90
- expect(manager.getNeighborCount()).toBe(5)
91
- })
92
-
93
- })
@@ -1,57 +0,0 @@
1
- import { wait, waitForEvent3 } from '@streamr/utils'
2
- import { PendingConnection } from '../../src/exports'
3
- import { createMockPeerDescriptor } from '../utils/utils'
4
- import { PendingConnectionEvents } from '../../src/connection/PendingConnection'
5
- import { MockConnection } from '../utils/mock/MockConnection'
6
-
7
- describe('PendingConnection', () => {
8
-
9
- let pendingConnection: PendingConnection
10
-
11
- beforeEach(() => {
12
- pendingConnection = new PendingConnection(createMockPeerDescriptor(), 500)
13
- })
14
-
15
- afterEach(() => {
16
- pendingConnection.close(false)
17
- })
18
-
19
- it('does not emit disconnected after replacedAsDuplicate', async () => {
20
- pendingConnection.once('disconnected', () => {
21
- throw new Error('disconnected')
22
- })
23
- pendingConnection.replaceAsDuplicate()
24
- pendingConnection.close(false)
25
- await wait(50)
26
- })
27
-
28
- it('emits disconnected after timed out', async () => {
29
- await waitForEvent3<PendingConnectionEvents>(pendingConnection, 'disconnected')
30
- })
31
-
32
- it('does not emit disconnected if destroyed', async () => {
33
- pendingConnection.once('disconnected', () => {
34
- throw new Error('disconnected')
35
- })
36
- pendingConnection.destroy()
37
- await wait(50)
38
- })
39
-
40
- it('emites connected', (done) => {
41
- const mockConnection = new MockConnection()
42
- pendingConnection.once('connected', (_peerDescriptor, connection) => {
43
- expect(mockConnection).toEqual(connection)
44
- done()
45
- })
46
- pendingConnection.onHandshakeCompleted(mockConnection)
47
- })
48
-
49
- it('does not emit connected if replaced', async () => {
50
- pendingConnection.once('connected', () => {
51
- throw new Error('connected')
52
- })
53
- pendingConnection.replaceAsDuplicate()
54
- await wait(50)
55
- })
56
-
57
- })
@@ -1,21 +0,0 @@
1
- /*
2
- import { ProtobufMessage } from '../../src/protobufclasses/ProtobufMessage'
3
- import { Message, RpcMessage } from '../../generated/DhtRpc'
4
- import { v4 } from 'uuid'
5
- import { MessageType } from '@protobuf-ts/runtime'
6
- */
7
-
8
- describe('ProtobufMessage', () => {
9
-
10
- it('can parse and print a Message', async () => {
11
-
12
- /*
13
- const message: Message = {
14
- messageId: v4(),
15
- messageType:
16
- }
17
- const data = Message.toBinary(message)
18
- ProtobufMessage.fromBinary(data)
19
- */
20
- })
21
- })
@@ -1,58 +0,0 @@
1
- import { RandomContactList } from '../../src/dht/contact/RandomContactList'
2
- import { DhtAddress, DhtAddressRaw, toDhtAddress } from '../../src/identifiers'
3
-
4
- const createItem = (nodeId: DhtAddressRaw): { getNodeId: () => DhtAddress } => {
5
- return {
6
- getNodeId: () => toDhtAddress(nodeId)
7
- }
8
- }
9
-
10
- describe('RandomContactList', () => {
11
-
12
- const item0 = createItem(new Uint8Array([0, 0, 0, 0]))
13
- const item1 = createItem(new Uint8Array([0, 0, 0, 1]))
14
- const item2 = createItem(new Uint8Array([0, 0, 0, 2]))
15
- const item3 = createItem(new Uint8Array([0, 0, 0, 3]))
16
- const item4 = createItem(new Uint8Array([0, 0, 0, 4]))
17
-
18
- it('adds contacts correctly', () => {
19
- const list = new RandomContactList(item0.getNodeId(), 5, 1)
20
- list.addContact(item1)
21
- list.addContact(item2)
22
- list.addContact(item3)
23
- list.addContact(item3)
24
- list.addContact(item4)
25
- list.addContact(item4)
26
- expect(list.getSize()).toEqual(4)
27
- expect(list.getContacts()).toEqual(
28
- [item1, item2, item3, item4]
29
- )
30
- })
31
-
32
- it('removes contacts correctly', () => {
33
- const list = new RandomContactList(item0.getNodeId(), 5, 1)
34
- list.addContact(item1)
35
- list.addContact(item2)
36
- list.addContact(item3)
37
- list.addContact(item4)
38
- list.removeContact(item2.getNodeId())
39
- expect(list.getContact(item1.getNodeId())).toBeTruthy()
40
- expect(list.getContact(item3.getNodeId())).toBeTruthy()
41
- expect(list.getContact(item4.getNodeId())).toBeTruthy()
42
- expect(list.getContacts()).toEqual(
43
- [item1, item3, item4]
44
- )
45
- expect(list.getSize()).toEqual(3)
46
- })
47
-
48
- it('get contacts', () => {
49
- const list = new RandomContactList(item0.getNodeId(), 5, 1)
50
- list.addContact(item1)
51
- list.addContact(item2)
52
- list.addContact(item3)
53
- list.addContact(item4)
54
- expect(list.getContacts()).toHaveLength(4)
55
- expect(list.getContacts(3)).toHaveLength(3)
56
- expect(list.getContacts(-2)).toHaveLength(0)
57
- })
58
- })
@@ -1,161 +0,0 @@
1
- import {
2
- RecursiveOperation,
3
- Message,
4
- RouteMessageAck,
5
- RouteMessageError,
6
- RouteMessageWrapper,
7
- RecursiveOperationRequest
8
- } from '../../generated/packages/dht/protos/DhtRpc'
9
- import {
10
- createWrappedClosestPeersRequest,
11
- createMockPeerDescriptor
12
- } from '../utils/utils'
13
- import { RecursiveOperationManager } from '../../src/dht/recursive-operation/RecursiveOperationManager'
14
- import { LocalDataStore } from '../../src/dht/store/LocalDataStore'
15
- import { v4 } from 'uuid'
16
- import { MockRouter } from '../utils/mock/MockRouter'
17
- import { MockTransport } from '../utils/mock/MockTransport'
18
- import { FakeRpcCommunicator } from '../utils/FakeRpcCommunicator'
19
- import { Router } from '../../src/dht/routing/Router'
20
- import { ITransport } from '../../src/transport/ITransport'
21
- import { areEqualPeerDescriptors, randomDhtAddress } from '../../src/identifiers'
22
- import { MockConnectionsView } from '../utils/mock/MockConnectionsView'
23
-
24
- const createMockRouter = (error?: RouteMessageError): Partial<Router> => {
25
- return {
26
- doRouteMessage: (routedMessage: RouteMessageWrapper) => {
27
- return {
28
- requestId: routedMessage.requestId,
29
- error
30
- }
31
- },
32
- isMostLikelyDuplicate: () => false,
33
- addToDuplicateDetector: () => {}
34
- }
35
- }
36
-
37
- const createRequest = (): RecursiveOperationRequest => {
38
- const request: RecursiveOperationRequest = {
39
- operation: RecursiveOperation.FIND_CLOSEST_NODES,
40
- sessionId: v4()
41
- }
42
- return request
43
- }
44
-
45
- describe('RecursiveOperationManager', () => {
46
-
47
- const peerDescriptor1 = createMockPeerDescriptor()
48
- const peerDescriptor2 = createMockPeerDescriptor()
49
- const recursiveOperationRequest = createRequest()
50
- const message: Message = {
51
- serviceId: 'unknown',
52
- messageId: v4(),
53
- body: {
54
- oneofKind: 'recursiveOperationRequest',
55
- recursiveOperationRequest
56
- },
57
- sourceDescriptor: peerDescriptor1,
58
- targetDescriptor: peerDescriptor2
59
- }
60
- const routedMessage: RouteMessageWrapper = {
61
- message,
62
- requestId: 'REQ',
63
- routingPath: [],
64
- reachableThrough: [],
65
- sourcePeer: peerDescriptor1,
66
- target: peerDescriptor2.nodeId,
67
- parallelRootNodeIds: []
68
- }
69
- const rpcCommunicator = new FakeRpcCommunicator()
70
-
71
- const createRecursiveOperationManager = (
72
- router: Router = new MockRouter() as any,
73
- transport: ITransport = new MockTransport()
74
- ): RecursiveOperationManager => {
75
- return new RecursiveOperationManager({
76
- localPeerDescriptor: peerDescriptor1,
77
- router,
78
- serviceId: 'RecursiveOperationManager',
79
- localDataStore: new LocalDataStore(30 * 100),
80
- sessionTransport: transport,
81
- connectionsView: new MockConnectionsView(),
82
- addContact: () => {},
83
- rpcCommunicator: rpcCommunicator as any,
84
- createDhtNodeRpcRemote: () => undefined as any
85
- })
86
- }
87
-
88
- it('RecursiveOperationManager server', async () => {
89
- const recursiveOperationManager = createRecursiveOperationManager()
90
- const res = await rpcCommunicator.callRpcMethod('routeRequest', routedMessage) as RouteMessageAck
91
- expect(res.error).toBeUndefined()
92
- recursiveOperationManager.stop()
93
- })
94
-
95
- it('find closest nodes returns self if no peers', async () => {
96
- const recursiveOperationManager = createRecursiveOperationManager()
97
- const res = await recursiveOperationManager.execute(randomDhtAddress(), RecursiveOperation.FIND_CLOSEST_NODES)
98
- expect(areEqualPeerDescriptors(res.closestNodes[0], peerDescriptor1)).toEqual(true)
99
- recursiveOperationManager.stop()
100
- })
101
-
102
- it('RecursiveOperationManager server throws if payload is not RecursiveOperationRequest', async () => {
103
- const manager = createRecursiveOperationManager(new MockRouter() as any)
104
- const rpcWrapper = createWrappedClosestPeersRequest(peerDescriptor1)
105
- const badMessage: Message = {
106
- serviceId: 'unknown',
107
- messageId: v4(),
108
- body: {
109
- oneofKind: 'rpcMessage',
110
- rpcMessage: rpcWrapper
111
- },
112
- sourceDescriptor: peerDescriptor1,
113
- targetDescriptor: peerDescriptor2
114
- }
115
- await expect(() => rpcCommunicator.callRpcMethod('routeRequest', {
116
- message: badMessage,
117
- requestId: 'REQ',
118
- routingPath: [],
119
- reachableThrough: [],
120
- target: peerDescriptor1.nodeId,
121
- sourcePeer: peerDescriptor2
122
- })).rejects.toThrow()
123
- manager.stop()
124
- })
125
-
126
- it('no targets', async () => {
127
- const router = createMockRouter(RouteMessageError.NO_TARGETS)
128
- const send = jest.fn()
129
- const transport = {
130
- send,
131
- getConnections: () => [],
132
- on: () => {},
133
- off: () => {}
134
- }
135
- const recursiveOperationManager = createRecursiveOperationManager(router as any, transport as any)
136
- const ack = await rpcCommunicator.callRpcMethod('routeRequest', routedMessage)
137
- expect(ack).toEqual({
138
- requestId: routedMessage.requestId,
139
- error: RouteMessageError.NO_TARGETS
140
- })
141
- expect(send).toHaveBeenCalledTimes(1)
142
- recursiveOperationManager.stop()
143
- })
144
-
145
- it('error', async () => {
146
- const router = createMockRouter(RouteMessageError.DUPLICATE)
147
- const send = jest.fn()
148
- const transport = {
149
- send,
150
- getConnections: () => []
151
- }
152
- const recursiveOperationManager = createRecursiveOperationManager(router as any, transport as any)
153
- const ack = await rpcCommunicator.callRpcMethod('routeRequest', routedMessage)
154
- expect(ack).toEqual({
155
- requestId: routedMessage.requestId,
156
- error: RouteMessageError.DUPLICATE
157
- })
158
- expect(send).not.toHaveBeenCalled()
159
- recursiveOperationManager.stop()
160
- })
161
- })
@@ -1,68 +0,0 @@
1
- import { waitForCondition } from '@streamr/utils'
2
- import { range } from 'lodash'
3
- import { RecursiveOperationSession } from '../../src/dht/recursive-operation/RecursiveOperationSession'
4
- import { RecursiveOperationSessionRpcRemote } from '../../src/dht/recursive-operation/RecursiveOperationSessionRpcRemote'
5
- import { ServiceID } from '../../src/types/ServiceID'
6
- import { randomDhtAddress } from '../../src/identifiers'
7
- import { Message, PeerDescriptor, RecursiveOperation } from '../../generated/packages/dht/protos/DhtRpc'
8
- import { RecursiveOperationSessionRpcClient } from '../../generated/packages/dht/protos/DhtRpc.client'
9
- import { RoutingRpcCommunicator } from '../../src/transport/RoutingRpcCommunicator'
10
- import { FakeEnvironment } from '../utils/FakeTransport'
11
- import { createMockPeerDescriptor } from '../utils/utils'
12
-
13
- describe('RecursiveOperationSession', () => {
14
-
15
- let environment: FakeEnvironment
16
- let localPeerDescriptor: PeerDescriptor
17
-
18
- const createRpcRemote = (serviceId: ServiceID) => {
19
- const mockPeerDescriptor = createMockPeerDescriptor()
20
- const transport = environment.createTransport(mockPeerDescriptor)
21
- const send = (msg: Message) => transport.send(msg)
22
- return new RecursiveOperationSessionRpcRemote(
23
- mockPeerDescriptor,
24
- localPeerDescriptor,
25
- new RoutingRpcCommunicator(serviceId, send),
26
- RecursiveOperationSessionRpcClient
27
- )
28
- }
29
-
30
- beforeEach(() => {
31
- environment = new FakeEnvironment()
32
- localPeerDescriptor = createMockPeerDescriptor()
33
- })
34
-
35
- it('happy path', async () => {
36
- const doRouteRequest = jest.fn()
37
- const session = new RecursiveOperationSession({
38
- transport: environment.createTransport(localPeerDescriptor),
39
- targetId: randomDhtAddress(),
40
- localPeerDescriptor,
41
- waitedRoutingPathCompletions: 3,
42
- operation: RecursiveOperation.FIND_CLOSEST_NODES,
43
- doRouteRequest
44
- })
45
- const onCompleted = jest.fn()
46
- session.on('completed', onCompleted)
47
-
48
- session.start('')
49
- expect(doRouteRequest).toHaveBeenCalled()
50
- range(3).forEach(() => {
51
- const remote = createRpcRemote(session.getId())
52
- remote.sendResponse(
53
- [createMockPeerDescriptor(), createMockPeerDescriptor()],
54
- [createMockPeerDescriptor(), createMockPeerDescriptor()],
55
- [],
56
- true
57
- )
58
- })
59
-
60
- // TODO now waits for the 4s timeout, could setup test so that it completes by receiving
61
- // all data it wants
62
- await waitForCondition(() => onCompleted.mock.calls.length > 0)
63
- const result = session.getResults()
64
- // TODO assert peer descriptors
65
- expect(result.closestNodes).toHaveLength(6)
66
- expect(result.dataEntries).toEqual([])
67
- })
68
- })
@@ -1,137 +0,0 @@
1
- import { v4 } from 'uuid'
2
- import { DhtNodeRpcRemote } from '../../src/dht/DhtNodeRpcRemote'
3
- import { Router } from '../../src/dht/routing/Router'
4
- import {
5
- Message,
6
- PeerDescriptor,
7
- RouteMessageAck,
8
- RouteMessageError,
9
- RouteMessageWrapper
10
- } from '../../generated/packages/dht/protos/DhtRpc'
11
- import { createMockPeerDescriptor, createWrappedClosestPeersRequest } from '../utils/utils'
12
- import { FakeRpcCommunicator } from '../utils/FakeRpcCommunicator'
13
- import { DhtAddress, toNodeId, randomDhtAddress } from '../../src/identifiers'
14
- import { MockRpcCommunicator } from '../utils/mock/MockRpcCommunicator'
15
-
16
- describe('Router', () => {
17
-
18
- let router: Router
19
- const peerDescriptor1 = createMockPeerDescriptor()
20
- const peerDescriptor2 = createMockPeerDescriptor()
21
- const rpcWrapper = createWrappedClosestPeersRequest(peerDescriptor1)
22
- const message: Message = {
23
- serviceId: 'unknown',
24
- messageId: v4(),
25
- body: {
26
- oneofKind: 'rpcMessage',
27
- rpcMessage: rpcWrapper
28
- },
29
- sourceDescriptor: peerDescriptor1,
30
- targetDescriptor: peerDescriptor2
31
- }
32
- const routedMessage: RouteMessageWrapper = {
33
- message,
34
- requestId: 'REQ',
35
- routingPath: [],
36
- reachableThrough: [],
37
- target: peerDescriptor1.nodeId,
38
- sourcePeer: peerDescriptor2,
39
- parallelRootNodeIds: []
40
- }
41
- let connections: Map<DhtAddress, DhtNodeRpcRemote>
42
- const rpcCommunicator = new FakeRpcCommunicator()
43
-
44
- const createMockDhtNodeRpcRemote = (destination: PeerDescriptor): DhtNodeRpcRemote => {
45
- return new DhtNodeRpcRemote(peerDescriptor1, destination, undefined as any, new MockRpcCommunicator())
46
- }
47
-
48
- beforeEach(() => {
49
- connections = new Map()
50
- router = new Router({
51
- localPeerDescriptor: peerDescriptor1,
52
- rpcCommunicator: rpcCommunicator as any,
53
- handleMessage: () => {},
54
- getConnections: () => [...connections.values()].map((c) => c.getPeerDescriptor())
55
- })
56
- })
57
-
58
- afterEach(() => {
59
- router.stop()
60
- })
61
-
62
- it('doRouteMessage without connections', async () => {
63
- const ack = await rpcCommunicator.callRpcMethod('routeMessage', {
64
- message,
65
- target: peerDescriptor2.nodeId,
66
- requestId: v4(),
67
- sourcePeer: peerDescriptor1,
68
- reachableThrough: [],
69
- routingPath: [],
70
- parallelRootNodeIds: []
71
- }) as RouteMessageAck
72
- expect(ack.error).toEqual(RouteMessageError.NO_TARGETS)
73
- })
74
-
75
- it('doRouteMessage with connections', async () => {
76
- connections.set(randomDhtAddress(), createMockDhtNodeRpcRemote(peerDescriptor2))
77
- const ack = await rpcCommunicator.callRpcMethod('routeMessage', {
78
- message,
79
- target: peerDescriptor2.nodeId,
80
- requestId: v4(),
81
- sourcePeer: peerDescriptor1,
82
- reachableThrough: [],
83
- routingPath: [],
84
- parallelRootNodeIds: []
85
- }) as RouteMessageAck
86
- expect(ack.error).toBeUndefined()
87
- })
88
-
89
- it('doRouteMessage with parallelRootNodeIds', async () => {
90
- const nodeId = toNodeId(peerDescriptor2)
91
- connections.set(nodeId, createMockDhtNodeRpcRemote(peerDescriptor2))
92
- const ack = await rpcCommunicator.callRpcMethod('routeMessage', {
93
- message,
94
- target: peerDescriptor2.nodeId,
95
- requestId: v4(),
96
- sourcePeer: peerDescriptor1,
97
- reachableThrough: [],
98
- routingPath: [],
99
- parallelRootNodeIds: [nodeId]
100
- }) as RouteMessageAck
101
- expect(ack.error).toEqual(RouteMessageError.NO_TARGETS)
102
- })
103
-
104
- it('route server is destination without connections', async () => {
105
- const ack = await rpcCommunicator.callRpcMethod('routeMessage', routedMessage) as RouteMessageAck
106
- expect(ack.error).toBeUndefined()
107
- })
108
-
109
- it('route server with connections', async () => {
110
- connections.set(randomDhtAddress(), createMockDhtNodeRpcRemote(peerDescriptor2))
111
- const ack = await rpcCommunicator.callRpcMethod('routeMessage', routedMessage) as RouteMessageAck
112
- expect(ack.error).toBeUndefined()
113
- })
114
-
115
- it('route server on duplicate message', async () => {
116
- router.addToDuplicateDetector(routedMessage.requestId)
117
- const ack = await rpcCommunicator.callRpcMethod('routeMessage', routedMessage) as RouteMessageAck
118
- expect(ack.error).toEqual(RouteMessageError.DUPLICATE)
119
- })
120
-
121
- it('forward server no connections', async () => {
122
- const ack = await rpcCommunicator.callRpcMethod('forwardMessage', routedMessage) as RouteMessageAck
123
- expect(ack.error).toEqual(RouteMessageError.NO_TARGETS)
124
- })
125
-
126
- it('forward server with connections', async () => {
127
- connections.set(randomDhtAddress(), createMockDhtNodeRpcRemote(peerDescriptor2))
128
- const ack = await rpcCommunicator.callRpcMethod('forwardMessage', routedMessage) as RouteMessageAck
129
- expect(ack.error).toBeUndefined()
130
- })
131
-
132
- it('forward server on duplicate message', async () => {
133
- router.addToDuplicateDetector(routedMessage.requestId)
134
- const ack = await rpcCommunicator.callRpcMethod('forwardMessage', routedMessage) as RouteMessageAck
135
- expect(ack.error).toEqual(RouteMessageError.DUPLICATE)
136
- })
137
- })