@streamr/dht 102.0.0-beta.1 → 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 (210) hide show
  1. package/dist/generated/google/protobuf/any.d.ts +180 -0
  2. package/dist/generated/google/protobuf/any.js +155 -0
  3. package/dist/generated/google/protobuf/any.js.map +1 -0
  4. package/dist/generated/google/protobuf/empty.d.ts +31 -0
  5. package/dist/generated/google/protobuf/empty.js +32 -0
  6. package/dist/generated/google/protobuf/empty.js.map +1 -0
  7. package/dist/generated/google/protobuf/timestamp.d.ts +155 -0
  8. package/dist/generated/google/protobuf/timestamp.js +136 -0
  9. package/dist/generated/google/protobuf/timestamp.js.map +1 -0
  10. package/dist/generated/packages/dht/protos/DhtRpc.client.d.ts +361 -0
  11. package/dist/generated/packages/dht/protos/DhtRpc.client.js +285 -0
  12. package/dist/generated/packages/dht/protos/DhtRpc.client.js.map +1 -0
  13. package/dist/generated/packages/dht/protos/DhtRpc.d.ts +999 -0
  14. package/dist/generated/packages/dht/protos/DhtRpc.js +677 -0
  15. package/dist/generated/packages/dht/protos/DhtRpc.js.map +1 -0
  16. package/dist/generated/packages/dht/protos/DhtRpc.server.d.ts +162 -0
  17. package/dist/generated/packages/dht/protos/DhtRpc.server.js +3 -0
  18. package/dist/generated/packages/dht/protos/DhtRpc.server.js.map +1 -0
  19. package/dist/generated/packages/proto-rpc/protos/ProtoRpc.d.ts +87 -0
  20. package/dist/generated/packages/proto-rpc/protos/ProtoRpc.js +66 -0
  21. package/dist/generated/packages/proto-rpc/protos/ProtoRpc.js.map +1 -0
  22. package/dist/package.json +7 -7
  23. package/package.json +7 -7
  24. package/src/connection/Connection.ts +0 -28
  25. package/src/connection/ConnectionLockRpcLocal.ts +0 -78
  26. package/src/connection/ConnectionLockRpcRemote.ts +0 -64
  27. package/src/connection/ConnectionLockStates.ts +0 -131
  28. package/src/connection/ConnectionManager.ts +0 -661
  29. package/src/connection/ConnectionsView.ts +0 -8
  30. package/src/connection/ConnectorFacade.ts +0 -217
  31. package/src/connection/Handshaker.ts +0 -209
  32. package/src/connection/IConnection.ts +0 -40
  33. package/src/connection/ManagedConnection.ts +0 -113
  34. package/src/connection/OutputBuffer.ts +0 -28
  35. package/src/connection/PendingConnection.ts +0 -68
  36. package/src/connection/connectivityChecker.ts +0 -108
  37. package/src/connection/connectivityRequestHandler.ts +0 -116
  38. package/src/connection/simulator/Simulator.ts +0 -369
  39. package/src/connection/simulator/SimulatorConnection.ts +0 -137
  40. package/src/connection/simulator/SimulatorConnector.ts +0 -98
  41. package/src/connection/simulator/SimulatorTransport.ts +0 -15
  42. package/src/connection/simulator/pings.ts +0 -42
  43. package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -242
  44. package/src/connection/webrtc/IWebrtcConnection.ts +0 -24
  45. package/src/connection/webrtc/NodeWebrtcConnection.ts +0 -245
  46. package/src/connection/webrtc/WebrtcConnector.ts +0 -234
  47. package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +0 -108
  48. package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +0 -60
  49. package/src/connection/webrtc/iceServerAsString.ts +0 -15
  50. package/src/connection/websocket/AbstractWebsocketClientConnection.ts +0 -122
  51. package/src/connection/websocket/AutoCertifierClientFacade.ts +0 -89
  52. package/src/connection/websocket/BrowserWebsocketClientConnection.ts +0 -44
  53. package/src/connection/websocket/NodeWebsocketClientConnection.ts +0 -39
  54. package/src/connection/websocket/WebsocketClientConnector.ts +0 -119
  55. package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +0 -38
  56. package/src/connection/websocket/WebsocketClientConnectorRpcRemote.ts +0 -19
  57. package/src/connection/websocket/WebsocketServer.ts +0 -164
  58. package/src/connection/websocket/WebsocketServerConnection.ts +0 -109
  59. package/src/connection/websocket/WebsocketServerConnector.ts +0 -290
  60. package/src/dht/DhtNode.ts +0 -683
  61. package/src/dht/DhtNodeRpcLocal.ts +0 -84
  62. package/src/dht/DhtNodeRpcRemote.ts +0 -107
  63. package/src/dht/ExternalApiRpcLocal.ts +0 -58
  64. package/src/dht/ExternalApiRpcRemote.ts +0 -41
  65. package/src/dht/PeerManager.ts +0 -305
  66. package/src/dht/contact/Contact.ts +0 -19
  67. package/src/dht/contact/ContactList.ts +0 -43
  68. package/src/dht/contact/RandomContactList.ts +0 -56
  69. package/src/dht/contact/RingContactList.ts +0 -143
  70. package/src/dht/contact/RpcRemote.ts +0 -72
  71. package/src/dht/contact/SortedContactList.ts +0 -173
  72. package/src/dht/contact/getClosestNodes.ts +0 -24
  73. package/src/dht/contact/ringIdentifiers.ts +0 -62
  74. package/src/dht/discovery/DiscoverySession.ts +0 -129
  75. package/src/dht/discovery/PeerDiscovery.ts +0 -244
  76. package/src/dht/discovery/RingDiscoverySession.ts +0 -148
  77. package/src/dht/recursive-operation/RecursiveOperationManager.ts +0 -251
  78. package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +0 -34
  79. package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +0 -43
  80. package/src/dht/recursive-operation/RecursiveOperationSession.ts +0 -231
  81. package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +0 -35
  82. package/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.ts +0 -30
  83. package/src/dht/routing/DuplicateDetector.ts +0 -34
  84. package/src/dht/routing/Router.ts +0 -246
  85. package/src/dht/routing/RouterRpcLocal.ts +0 -78
  86. package/src/dht/routing/RouterRpcRemote.ts +0 -80
  87. package/src/dht/routing/RoutingSession.ts +0 -243
  88. package/src/dht/routing/RoutingTablesCache.ts +0 -60
  89. package/src/dht/routing/getPreviousPeer.ts +0 -6
  90. package/src/dht/store/LocalDataStore.ts +0 -84
  91. package/src/dht/store/StoreManager.ts +0 -170
  92. package/src/dht/store/StoreRpcLocal.ts +0 -89
  93. package/src/dht/store/StoreRpcRemote.ts +0 -32
  94. package/src/exports.ts +0 -33
  95. package/src/helpers/AddressTools.ts +0 -28
  96. package/src/helpers/Connectivity.ts +0 -19
  97. package/src/helpers/browser/isBrowserEnvironment.ts +0 -1
  98. package/src/helpers/browser/isBrowserEnvironment_override.ts +0 -3
  99. package/src/helpers/createPeerDescriptor.ts +0 -57
  100. package/src/helpers/createPeerDescriptorSignaturePayload.ts +0 -28
  101. package/src/helpers/debugHelpers.ts +0 -9
  102. package/src/helpers/errors.ts +0 -49
  103. package/src/helpers/offering.ts +0 -15
  104. package/src/helpers/protoClasses.ts +0 -57
  105. package/src/helpers/protoToString.ts +0 -21
  106. package/src/helpers/version.ts +0 -32
  107. package/src/identifiers.ts +0 -29
  108. package/src/rpc-protocol/DhtCallContext.ts +0 -14
  109. package/src/rpc-protocol/DhtRpcOptions.ts +0 -10
  110. package/src/transport/ITransport.ts +0 -37
  111. package/src/transport/ListeningRpcCommunicator.ts +0 -32
  112. package/src/transport/RoutingRpcCommunicator.ts +0 -66
  113. package/src/types/ServiceID.ts +0 -1
  114. package/src/types/textencoding.d.ts +0 -6
  115. package/test/benchmark/Find.test.ts +0 -72
  116. package/test/benchmark/KademliaCorrectness.test.ts +0 -114
  117. package/test/benchmark/RingCorrectness.test.ts +0 -157
  118. package/test/benchmark/SortedContactListBenchmark.test.ts +0 -108
  119. package/test/benchmark/WebsocketServerMemoryLeak.test.ts +0 -41
  120. package/test/benchmark/hybrid-network-simulation/RingContactList.test.ts +0 -71
  121. package/test/end-to-end/GeoIpLayer0.test.ts +0 -55
  122. package/test/end-to-end/Layer0-Layer1.test.ts +0 -93
  123. package/test/end-to-end/Layer0.test.ts +0 -76
  124. package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +0 -110
  125. package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +0 -137
  126. package/test/end-to-end/Layer0Webrtc.test.ts +0 -85
  127. package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +0 -82
  128. package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +0 -76
  129. package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +0 -52
  130. package/test/end-to-end/WebsocketConnectionRequest.test.ts +0 -69
  131. package/test/end-to-end/memory-leak.test.ts +0 -80
  132. package/test/integration/ConnectionLocking.test.ts +0 -192
  133. package/test/integration/ConnectionManager.test.ts +0 -528
  134. package/test/integration/ConnectivityChecking.test.ts +0 -53
  135. package/test/integration/DhtJoinPeerDiscovery.test.ts +0 -49
  136. package/test/integration/DhtNode.test.ts +0 -66
  137. package/test/integration/DhtNodeExternalAPI.test.ts +0 -48
  138. package/test/integration/DhtNodeRpcRemote.test.ts +0 -66
  139. package/test/integration/DhtRpc.test.ts +0 -121
  140. package/test/integration/Find.test.ts +0 -45
  141. package/test/integration/GeoIpConnectivityChecking.test.ts +0 -72
  142. package/test/integration/Layer1-scale.test.ts +0 -189
  143. package/test/integration/Mock-Layer1-Layer0.test.ts +0 -85
  144. package/test/integration/MultipleEntryPointJoining.test.ts +0 -105
  145. package/test/integration/ReplicateData.test.ts +0 -104
  146. package/test/integration/RouteMessage.test.ts +0 -230
  147. package/test/integration/RouterRpcRemote.test.ts +0 -77
  148. package/test/integration/SimultaneousConnections.test.ts +0 -316
  149. package/test/integration/Store.test.ts +0 -85
  150. package/test/integration/StoreAndDelete.test.ts +0 -77
  151. package/test/integration/StoreOnDhtWithThreeNodes.test.ts +0 -59
  152. package/test/integration/StoreOnDhtWithTwoNodes.test.ts +0 -51
  153. package/test/integration/StoreRpcRemote.test.ts +0 -54
  154. package/test/integration/WebrtcConnectionManagement.test.ts +0 -191
  155. package/test/integration/WebrtcConnectorRpc.test.ts +0 -125
  156. package/test/integration/Websocket.test.ts +0 -65
  157. package/test/integration/WebsocketClientConnectorRpc.test.ts +0 -69
  158. package/test/integration/WebsocketConnectionManagement.test.ts +0 -191
  159. package/test/integration/rpc-connections-over-webrtc.test.ts +0 -123
  160. package/test/kademlia-simulation/data/nodeids.json +0 -13002
  161. package/test/kademlia-simulation/data/orderedneighbors.json +0 -1001
  162. package/test/types/global.d.ts +0 -1
  163. package/test/unit/AddressTools.test.ts +0 -44
  164. package/test/unit/AutoCertifierClientFacade.test.ts +0 -58
  165. package/test/unit/ConnectionManager.test.ts +0 -65
  166. package/test/unit/ConnectivityHelpers.test.ts +0 -61
  167. package/test/unit/DiscoverySession.test.ts +0 -87
  168. package/test/unit/DuplicateDetector.test.ts +0 -31
  169. package/test/unit/Handshaker.test.ts +0 -169
  170. package/test/unit/ListeningRpcCommunicator.test.ts +0 -52
  171. package/test/unit/LocalDataStore.test.ts +0 -108
  172. package/test/unit/ManagedConnection.test.ts +0 -58
  173. package/test/unit/PeerManager.test.ts +0 -93
  174. package/test/unit/PendingConnection.test.ts +0 -57
  175. package/test/unit/ProtobufMessage.test.ts +0 -21
  176. package/test/unit/RandomContactList.test.ts +0 -58
  177. package/test/unit/RecursiveOperationManager.test.ts +0 -161
  178. package/test/unit/RecursiveOperationSession.test.ts +0 -68
  179. package/test/unit/Router.test.ts +0 -137
  180. package/test/unit/RoutingSession.test.ts +0 -86
  181. package/test/unit/SortedContactList.test.ts +0 -115
  182. package/test/unit/StoreManager.test.ts +0 -146
  183. package/test/unit/StoreRpcLocal.test.ts +0 -167
  184. package/test/unit/WebrtcConnection.test.ts +0 -29
  185. package/test/unit/WebrtcConnector.test.ts +0 -56
  186. package/test/unit/WebsocketClientConnector.test.ts +0 -101
  187. package/test/unit/WebsocketServer.test.ts +0 -66
  188. package/test/unit/WebsocketServerConnector.test.ts +0 -102
  189. package/test/unit/connectivityRequestHandler.test.ts +0 -104
  190. package/test/unit/createPeerDescriptor.test.ts +0 -69
  191. package/test/unit/customMatchers.test.ts +0 -34
  192. package/test/unit/getClosestNodes.test.ts +0 -30
  193. package/test/unit/version.test.ts +0 -18
  194. package/test/unit/webrtcReplaceInternalIpWithExternalIp.test.ts +0 -18
  195. package/test/utils/FakeConnectorFacade.ts +0 -41
  196. package/test/utils/FakeRpcCommunicator.ts +0 -23
  197. package/test/utils/FakeTransport.ts +0 -79
  198. package/test/utils/customMatchers.ts +0 -71
  199. package/test/utils/mock/MockConnection.ts +0 -26
  200. package/test/utils/mock/MockConnectionsView.ts +0 -18
  201. package/test/utils/mock/MockRouter.ts +0 -62
  202. package/test/utils/mock/MockRpcCommunicator.ts +0 -7
  203. package/test/utils/mock/MockTransport.ts +0 -26
  204. package/test/utils/mock/mockDataEntry.ts +0 -38
  205. package/test/utils/topology.ts +0 -79
  206. package/test/utils/utils.ts +0 -268
  207. package/tsconfig.browser.json +0 -17
  208. package/tsconfig.jest.json +0 -25
  209. package/tsconfig.json +0 -3
  210. 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 { until } 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 until(() => {
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 { until } 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 until(() => 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
- })