@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,84 +0,0 @@
1
- import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
2
- import { Logger } from '@streamr/utils'
3
- import { DhtAddress, toDhtAddress, toNodeId } from '../identifiers'
4
- import { Empty } from '../../generated/google/protobuf/empty'
5
- import {
6
- ClosestPeersRequest,
7
- ClosestPeersResponse,
8
- ClosestRingPeersRequest,
9
- ClosestRingPeersResponse,
10
- PeerDescriptor,
11
- PingRequest,
12
- PingResponse
13
- } from '../../generated/packages/dht/protos/DhtRpc'
14
- import { IDhtNodeRpc } from '../../generated/packages/dht/protos/DhtRpc.server'
15
- import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
16
- import { RingContacts } from './contact/RingContactList'
17
- import { getClosestNodes } from './contact/getClosestNodes'
18
- import { RingIdRaw } from './contact/ringIdentifiers'
19
-
20
- interface DhtNodeRpcLocalOptions {
21
- peerDiscoveryQueryBatchSize: number
22
- getNeighbors: () => ReadonlyArray<PeerDescriptor>
23
- getClosestRingContactsTo: (id: RingIdRaw, limit: number) => RingContacts
24
- addContact: (contact: PeerDescriptor) => void
25
- removeContact: (nodeId: DhtAddress) => void
26
- }
27
-
28
- const logger = new Logger(module)
29
-
30
- export class DhtNodeRpcLocal implements IDhtNodeRpc {
31
-
32
- private readonly options: DhtNodeRpcLocalOptions
33
-
34
- constructor(options: DhtNodeRpcLocalOptions) {
35
- this.options = options
36
- }
37
-
38
- // TODO rename to getClosestNeighbors (breaking change)
39
- async getClosestPeers(request: ClosestPeersRequest, context: ServerCallContext): Promise<ClosestPeersResponse> {
40
- this.options.addContact((context as DhtCallContext).incomingSourceDescriptor!)
41
- const peers = getClosestNodes(
42
- toDhtAddress(request.nodeId),
43
- this.options.getNeighbors(),
44
- { maxCount: this.options.peerDiscoveryQueryBatchSize }
45
- )
46
- const response = {
47
- peers,
48
- requestId: request.requestId
49
- }
50
- return response
51
- }
52
-
53
- // TODO rename to getClosestRingContacts (breaking change)
54
- async getClosestRingPeers(request: ClosestRingPeersRequest, context: ServerCallContext): Promise<ClosestRingPeersResponse> {
55
- this.options.addContact((context as DhtCallContext).incomingSourceDescriptor!)
56
- const closestContacts = this.options.getClosestRingContactsTo(request.ringId as RingIdRaw, this.options.peerDiscoveryQueryBatchSize)
57
- const response = {
58
- leftPeers: closestContacts.left,
59
- rightPeers: closestContacts.right,
60
- requestId: request.requestId
61
- }
62
- return response
63
- }
64
-
65
- async ping(request: PingRequest, context: ServerCallContext): Promise<PingResponse> {
66
- logger.trace('received ping request: ' + toNodeId((context as DhtCallContext).incomingSourceDescriptor!))
67
- setImmediate(() => {
68
- this.options.addContact((context as DhtCallContext).incomingSourceDescriptor!)
69
- })
70
- const response: PingResponse = {
71
- requestId: request.requestId
72
- }
73
- return response
74
- }
75
-
76
- async leaveNotice(context: ServerCallContext): Promise<Empty> {
77
- // TODO check signature??
78
- const sender = (context as DhtCallContext).incomingSourceDescriptor!
79
- const senderNodeId = toNodeId(sender)
80
- logger.trace('received leave notice: ' + senderNodeId)
81
- this.options.removeContact(senderNodeId)
82
- return {}
83
- }
84
- }
@@ -1,107 +0,0 @@
1
- import { RpcCommunicator } from '@streamr/proto-rpc'
2
- import { Logger } from '@streamr/utils'
3
- import { v4 } from 'uuid'
4
- import { DhtAddress, DhtAddressRaw, toNodeId, toDhtAddressRaw } from '../identifiers'
5
- import {
6
- ClosestPeersRequest,
7
- ClosestRingPeersRequest,
8
- PeerDescriptor,
9
- PingRequest
10
- } from '../../generated/packages/dht/protos/DhtRpc'
11
- import { DhtNodeRpcClient } from '../../generated/packages/dht/protos/DhtRpc.client'
12
- import { ServiceID } from '../types/ServiceID'
13
- import { RpcRemote } from './contact/RpcRemote'
14
- import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
15
- import { RingIdRaw } from './contact/ringIdentifiers'
16
- import { RingContacts } from './contact/RingContactList'
17
-
18
- const logger = new Logger(module)
19
-
20
- // Fields required by objects stored in the k-bucket library
21
- export interface KBucketContact {
22
- id: DhtAddressRaw
23
- vectorClock: number
24
- }
25
-
26
- export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBucketContact {
27
-
28
- private static counter = 0
29
- public vectorClock: number
30
- public readonly id: DhtAddressRaw
31
- private readonly serviceId: ServiceID
32
-
33
- constructor(
34
- localPeerDescriptor: PeerDescriptor,
35
- peerDescriptor: PeerDescriptor,
36
- serviceId: ServiceID,
37
- rpcCommunicator: RpcCommunicator<DhtCallContext>,
38
- rpcRequestTimeout?: number
39
- ) {
40
- super(localPeerDescriptor, peerDescriptor, rpcCommunicator, DhtNodeRpcClient, rpcRequestTimeout)
41
- this.id = this.getPeerDescriptor().nodeId
42
- this.vectorClock = DhtNodeRpcRemote.counter++
43
- this.serviceId = serviceId
44
- }
45
-
46
- async getClosestPeers(nodeId: DhtAddress): Promise<PeerDescriptor[]> {
47
- logger.trace(`Requesting getClosestPeers on ${this.serviceId} from ${this.getNodeId()}`)
48
- const request: ClosestPeersRequest = {
49
- nodeId: toDhtAddressRaw(nodeId),
50
- requestId: v4()
51
- }
52
- try {
53
- const peers = await this.getClient().getClosestPeers(request, this.formDhtRpcOptions())
54
- return peers.peers
55
- } catch (err) {
56
- logger.trace(`getClosestPeers error ${this.serviceId}`, { err })
57
- throw err
58
- }
59
- }
60
-
61
- // TODO rename to getClosestRingContacts (breaking change)
62
- async getClosestRingPeers(ringIdRaw: RingIdRaw): Promise<RingContacts> {
63
- logger.trace(`Requesting getClosestRingPeers on ${this.serviceId} from ${this.getNodeId()}`)
64
- const request: ClosestRingPeersRequest = {
65
- ringId: ringIdRaw,
66
- requestId: v4()
67
- }
68
- try {
69
- const response = await this.getClient().getClosestRingPeers(request, this.formDhtRpcOptions())
70
- return { left: response.leftPeers ?? [], right: response.rightPeers ?? [] }
71
- } catch (err) {
72
- logger.trace(`getClosestRingPeers error ${this.serviceId}`, { err })
73
- throw err
74
- }
75
- }
76
-
77
- async ping(): Promise<boolean> {
78
- logger.trace(`Requesting ping on ${this.serviceId} from ${this.getNodeId()}`)
79
- const request: PingRequest = {
80
- requestId: v4()
81
- }
82
- const options = this.formDhtRpcOptions()
83
- try {
84
- const pong = await this.getClient().ping(request, options)
85
- if (pong.requestId === request.requestId) {
86
- return true
87
- }
88
- } catch (err) {
89
- logger.trace(`ping failed on ${this.serviceId} to ${this.getNodeId()}`, { err })
90
- }
91
- return false
92
- }
93
-
94
- leaveNotice(): void {
95
- logger.trace(`Sending leaveNotice on ${this.serviceId} from ${this.getNodeId()}`)
96
- const options = this.formDhtRpcOptions({
97
- notification: true
98
- })
99
- this.getClient().leaveNotice({}, options).catch((e) => {
100
- logger.trace('Failed to send leaveNotice' + e)
101
- })
102
- }
103
-
104
- getNodeId(): DhtAddress {
105
- return toNodeId(this.getPeerDescriptor())
106
- }
107
- }
@@ -1,58 +0,0 @@
1
- import { IExternalApiRpc } from '../../generated/packages/dht/protos/DhtRpc.server'
2
- import {
3
- ExternalFetchDataRequest,
4
- ExternalFetchDataResponse,
5
- ExternalStoreDataRequest,
6
- ExternalStoreDataResponse,
7
- RecursiveOperation,
8
- PeerDescriptor
9
- } from '../../generated/packages/dht/protos/DhtRpc'
10
- import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
11
- import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
12
- import { RecursiveOperationResult } from './recursive-operation/RecursiveOperationManager'
13
- import { Any } from '../../generated/google/protobuf/any'
14
- import { DhtAddress, toNodeId, toDhtAddress } from '../identifiers'
15
-
16
- interface ExternalApiRpcLocalOptions {
17
- executeRecursiveOperation: (
18
- targetId: DhtAddress,
19
- operation: RecursiveOperation,
20
- excludedPeer: DhtAddress
21
- ) => Promise<RecursiveOperationResult>
22
- storeDataToDht: (
23
- key: DhtAddress,
24
- data: Any,
25
- creator: DhtAddress
26
- ) => Promise<PeerDescriptor[]>
27
- }
28
-
29
- export class ExternalApiRpcLocal implements IExternalApiRpc {
30
-
31
- private readonly options: ExternalApiRpcLocalOptions
32
-
33
- constructor(options: ExternalApiRpcLocalOptions) {
34
- this.options = options
35
- }
36
-
37
- async externalFetchData(request: ExternalFetchDataRequest, context: ServerCallContext): Promise<ExternalFetchDataResponse> {
38
- const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
39
- const result = await this.options.executeRecursiveOperation(
40
- toDhtAddress(request.key),
41
- RecursiveOperation.FETCH_DATA,
42
- toNodeId(senderPeerDescriptor)
43
- )
44
- return ExternalFetchDataResponse.create({ entries: result.dataEntries ?? [] })
45
- }
46
-
47
- async externalStoreData(request: ExternalStoreDataRequest, context: ServerCallContext): Promise<ExternalStoreDataResponse> {
48
- const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
49
- const result = await this.options.storeDataToDht(
50
- toDhtAddress(request.key),
51
- request.data!,
52
- toNodeId(senderPeerDescriptor)
53
- )
54
- return ExternalStoreDataResponse.create({
55
- storers: result
56
- })
57
- }
58
- }
@@ -1,41 +0,0 @@
1
- import { DhtAddress, toDhtAddressRaw } from '../identifiers'
2
- import { Any } from '../../generated/google/protobuf/any'
3
- import { DataEntry, ExternalFetchDataRequest, ExternalStoreDataRequest, PeerDescriptor } from '../../generated/packages/dht/protos/DhtRpc'
4
- import { ExternalApiRpcClient } from '../../generated/packages/dht/protos/DhtRpc.client'
5
- import { RpcRemote } from './contact/RpcRemote'
6
-
7
- export class ExternalApiRpcRemote extends RpcRemote<ExternalApiRpcClient> {
8
-
9
- async externalFetchData(key: DhtAddress): Promise<DataEntry[]> {
10
- const request: ExternalFetchDataRequest = {
11
- key: toDhtAddressRaw(key)
12
- }
13
- const options = this.formDhtRpcOptions({
14
- // TODO use options option or named constant?
15
- timeout: 10000
16
- })
17
- try {
18
- const data = await this.getClient().externalFetchData(request, options)
19
- return data.entries
20
- } catch {
21
- return []
22
- }
23
- }
24
-
25
- async storeData(key: DhtAddress, data: Any): Promise<PeerDescriptor[]> {
26
- const request: ExternalStoreDataRequest = {
27
- key: toDhtAddressRaw(key),
28
- data
29
- }
30
- const options = this.formDhtRpcOptions({
31
- // TODO use options option or named constant?
32
- timeout: 10000
33
- })
34
- try {
35
- const response = await this.getClient().externalStoreData(request, options)
36
- return response.storers
37
- } catch {
38
- return []
39
- }
40
- }
41
- }
@@ -1,303 +0,0 @@
1
- import {
2
- Logger
3
- } from '@streamr/utils'
4
- import EventEmitter from 'eventemitter3'
5
- import KBucket from 'k-bucket'
6
- import { LockID } from '../connection/ConnectionLockStates'
7
- import { ConnectionLocker } from '../connection/ConnectionManager'
8
- import { DhtAddress, DhtAddressRaw, toNodeId, toDhtAddressRaw } from '../identifiers'
9
- import {
10
- PeerDescriptor
11
- } from '../../generated/packages/dht/protos/DhtRpc'
12
- import { DhtNodeRpcRemote } from './DhtNodeRpcRemote'
13
- import { RandomContactList } from './contact/RandomContactList'
14
- import { RingContactList } from './contact/RingContactList'
15
- import { ReadonlySortedContactList, SortedContactList } from './contact/SortedContactList'
16
- import { RingIdRaw, getRingIdRawFromPeerDescriptor } from './contact/ringIdentifiers'
17
-
18
- const logger = new Logger(module)
19
-
20
- interface PeerManagerOptions {
21
- numberOfNodesPerKBucket: number
22
- maxContactCount: number
23
- localNodeId: DhtAddress
24
- localPeerDescriptor: PeerDescriptor
25
- connectionLocker?: ConnectionLocker
26
- lockId: LockID
27
- createDhtNodeRpcRemote: (peerDescriptor: PeerDescriptor) => DhtNodeRpcRemote
28
- hasConnection: (nodeId: DhtAddress) => boolean
29
- }
30
-
31
- // Returns all offline nodes, sets contacts as active if they are online
32
- const pingNodes = async (nodes: DhtNodeRpcRemote[], activeContacts: Set<DhtAddress>): Promise<PeerDescriptor[]> => {
33
- const offlineNeighbors: PeerDescriptor[] = []
34
- await Promise.allSettled(nodes.map(async (contact) => {
35
- const isOnline = await contact.ping()
36
- if (!isOnline) {
37
- activeContacts.delete(contact.getNodeId())
38
- offlineNeighbors.push(contact.getPeerDescriptor())
39
- } else {
40
- activeContacts.add(contact.getNodeId())
41
- }
42
- }))
43
- return offlineNeighbors
44
- }
45
-
46
- export interface PeerManagerEvents {
47
- nearbyContactAdded: (peerDescriptor: PeerDescriptor) => void
48
- nearbyContactRemoved: (peerDescriptor: PeerDescriptor) => void
49
- randomContactAdded: (peerDescriptor: PeerDescriptor) => void
50
- randomContactRemoved: (peerDescriptor: PeerDescriptor) => void
51
- ringContactAdded: (peerDescriptor: PeerDescriptor) => void
52
- ringContactRemoved: (peerDescriptor: PeerDescriptor) => void
53
- kBucketEmpty: () => void
54
- }
55
-
56
- export const getDistance = (nodeIdOrDataKeyRaw1: DhtAddressRaw, nodeIdOrDataKeyRaw2: DhtAddressRaw): number => {
57
- return KBucket.distance(nodeIdOrDataKeyRaw1, nodeIdOrDataKeyRaw2)
58
- }
59
-
60
- export class PeerManager extends EventEmitter<PeerManagerEvents> {
61
-
62
- // Glossary:
63
- // * 'neighbors' are the nodes that are our neighbors according to
64
- // the protocol of the layer we are in
65
- // * 'connections' are the nodes that are connected to this node on Layer0
66
- // * 'contacts' are all non-unresponsive nodes that we know about
67
-
68
- private neighbors: KBucket<DhtNodeRpcRemote>
69
- private nearbyContacts: SortedContactList<DhtNodeRpcRemote>
70
- private activeContacts: Set<DhtAddress>
71
- private ringContacts: RingContactList<DhtNodeRpcRemote>
72
- private randomContacts: RandomContactList<DhtNodeRpcRemote>
73
- private stopped: boolean = false
74
- private readonly options: PeerManagerOptions
75
-
76
- constructor(options: PeerManagerOptions) {
77
- super()
78
- this.options = options
79
- this.neighbors = new KBucket<DhtNodeRpcRemote>({
80
- localNodeId: toDhtAddressRaw(this.options.localNodeId),
81
- numberOfNodesPerKBucket: this.options.numberOfNodesPerKBucket,
82
- numberOfNodesToPing: this.options.numberOfNodesPerKBucket
83
- })
84
- this.ringContacts = new RingContactList<DhtNodeRpcRemote>(getRingIdRawFromPeerDescriptor(this.options.localPeerDescriptor))
85
- this.ringContacts.on('contactAdded', (contact: DhtNodeRpcRemote) => {
86
- this.emit('ringContactAdded', contact.getPeerDescriptor())
87
- })
88
- this.ringContacts.on('contactRemoved', (contact: DhtNodeRpcRemote) => {
89
- this.emit('ringContactRemoved', contact.getPeerDescriptor())
90
- })
91
- this.neighbors.on('ping', (oldContacts: DhtNodeRpcRemote[], newContact: DhtNodeRpcRemote) => this.onKBucketPing(oldContacts, newContact))
92
- this.neighbors.on('removed', (contact: DhtNodeRpcRemote) => this.onKBucketRemoved(toNodeId(contact.getPeerDescriptor())))
93
- this.neighbors.on('added', (contact: DhtNodeRpcRemote) => this.onKBucketAdded(contact))
94
- this.neighbors.on('updated', () => {
95
- // TODO: Update contact info to the connection manager and reconnect
96
- })
97
- this.nearbyContacts = new SortedContactList({
98
- referenceId: this.options.localNodeId,
99
- maxSize: this.options.maxContactCount,
100
- allowToContainReferenceId: false
101
- })
102
- this.nearbyContacts.on('contactRemoved', (contact: DhtNodeRpcRemote) => {
103
- if (this.stopped) {
104
- return
105
- }
106
- this.emit('nearbyContactRemoved', contact.getPeerDescriptor())
107
- this.randomContacts.addContact(this.options.createDhtNodeRpcRemote(contact.getPeerDescriptor()))
108
- })
109
- this.nearbyContacts.on('contactAdded', (contact: DhtNodeRpcRemote) =>
110
- this.emit('nearbyContactAdded', contact.getPeerDescriptor())
111
- )
112
- this.activeContacts = new Set()
113
- this.randomContacts = new RandomContactList(this.options.localNodeId, this.options.maxContactCount)
114
- this.randomContacts.on('contactRemoved', (removedContact: DhtNodeRpcRemote) =>
115
- this.emit('randomContactRemoved', removedContact.getPeerDescriptor())
116
- )
117
- this.randomContacts.on('contactAdded', (contactAdded: DhtNodeRpcRemote) =>
118
- this.emit('randomContactAdded', contactAdded.getPeerDescriptor())
119
- )
120
- }
121
-
122
- private onKBucketPing(oldContacts: DhtNodeRpcRemote[], newContact: DhtNodeRpcRemote): void {
123
- if (this.stopped) {
124
- return
125
- }
126
- const sortingList: SortedContactList<DhtNodeRpcRemote> = new SortedContactList({
127
- referenceId: this.options.localNodeId,
128
- allowToContainReferenceId: false
129
- })
130
- sortingList.addContacts(oldContacts)
131
- const removableNodeId = sortingList.getFurthestContacts(1)[0].getNodeId()
132
- this.options.connectionLocker?.weakUnlockConnection(removableNodeId, this.options.lockId)
133
- this.neighbors.remove(toDhtAddressRaw(removableNodeId))
134
- this.neighbors.add(newContact)
135
- }
136
-
137
- private onKBucketRemoved(nodeId: DhtAddress): void {
138
- if (this.stopped) {
139
- return
140
- }
141
- this.options.connectionLocker?.weakUnlockConnection(nodeId, this.options.lockId)
142
- logger.trace(`Removed contact ${nodeId}`)
143
- if (this.neighbors.count() === 0) {
144
- this.emit('kBucketEmpty')
145
- }
146
- }
147
-
148
- private onKBucketAdded(contact: DhtNodeRpcRemote): void {
149
- if (this.stopped) {
150
- return
151
- }
152
- if (contact.getNodeId() !== this.options.localNodeId) {
153
- const peerDescriptor = contact.getPeerDescriptor()
154
- const nodeId = toNodeId(peerDescriptor)
155
- // Important to lock here, before the ping result is known
156
- this.options.connectionLocker?.weakLockConnection(nodeId, this.options.lockId)
157
- if (this.options.hasConnection(contact.getNodeId())) {
158
- logger.trace(`Added new contact ${nodeId}`)
159
- } else { // open connection by pinging
160
- logger.trace('starting ping ' + nodeId)
161
- contact.ping().then((result) => {
162
- if (result) {
163
- logger.trace(`Added new contact ${nodeId}`)
164
- } else {
165
- logger.trace('ping failed ' + nodeId)
166
- this.options.connectionLocker?.weakUnlockConnection(nodeId, this.options.lockId)
167
- this.removeContact(nodeId)
168
- this.addNearbyContactToNeighbors()
169
- }
170
- }).catch((_e) => {
171
- this.options.connectionLocker?.weakUnlockConnection(nodeId, this.options.lockId)
172
- this.removeContact(nodeId)
173
- this.addNearbyContactToNeighbors()
174
- })
175
- }
176
- }
177
- }
178
-
179
- private addNearbyContactToNeighbors(): void {
180
- if (this.stopped) {
181
- return
182
- }
183
- const closest = this.getNearbyActiveContactNotInNeighbors()
184
- if (closest) {
185
- this.addContact(closest.getPeerDescriptor())
186
- }
187
- }
188
-
189
- private getNearbyActiveContactNotInNeighbors(): DhtNodeRpcRemote | undefined {
190
- for (const contactId of this.nearbyContacts.getContactIds()) {
191
- if (!this.neighbors.get(toDhtAddressRaw(contactId)) && this.activeContacts.has(contactId)) {
192
- return this.nearbyContacts.getContact(contactId)!
193
- }
194
- }
195
- return undefined
196
- }
197
-
198
- removeContact(nodeId: DhtAddress): void {
199
- if (this.stopped) {
200
- return
201
- }
202
- logger.trace(`Removing contact ${nodeId}`)
203
- this.ringContacts.removeContact(this.nearbyContacts.getContact(nodeId))
204
- this.neighbors.remove(toDhtAddressRaw(nodeId))
205
- this.nearbyContacts.removeContact(nodeId)
206
- this.activeContacts.delete(nodeId)
207
- this.randomContacts.removeContact(nodeId)
208
- }
209
-
210
- removeNeighbor(nodeId: DhtAddress): void {
211
- this.neighbors.remove(toDhtAddressRaw(nodeId))
212
- }
213
-
214
- async pruneOfflineNodes(nodes: DhtNodeRpcRemote[]): Promise<void> {
215
- logger.trace('Pruning offline nodes', { nodes: nodes.length })
216
- const offlineNeighbors = await pingNodes(nodes, this.activeContacts)
217
- offlineNeighbors.forEach((offlineNeighbor) => {
218
- logger.trace('Removing offline node', { node: toNodeId(offlineNeighbor) })
219
- this.removeContact(toNodeId(offlineNeighbor))
220
- })
221
- }
222
-
223
- stop(): void {
224
- this.stopped = true
225
- this.neighbors.toArray().forEach((rpcRemote: DhtNodeRpcRemote) => {
226
- rpcRemote.leaveNotice()
227
- this.neighbors.remove(rpcRemote.id)
228
- })
229
- this.neighbors.removeAllListeners()
230
- this.ringContacts.getAllContacts().forEach((rpcRemote) => {
231
- rpcRemote.leaveNotice()
232
- this.ringContacts.removeContact(rpcRemote)
233
- })
234
- this.nearbyContacts.stop()
235
- this.randomContacts.stop()
236
- }
237
-
238
- getNearbyContacts(): ReadonlySortedContactList<DhtNodeRpcRemote> {
239
- return this.nearbyContacts
240
- }
241
-
242
- getClosestRingContactsTo(
243
- ringIdRaw: RingIdRaw,
244
- limit?: number,
245
- excludedIds?: Set<DhtAddress>
246
- ): { left: DhtNodeRpcRemote[], right: DhtNodeRpcRemote[] } {
247
- const closest = new RingContactList<DhtNodeRpcRemote>(ringIdRaw, excludedIds)
248
- this.ringContacts.getAllContacts().map((contact) => closest.addContact(contact))
249
- // TODO use options option or named constant?
250
- return closest.getClosestContacts(limit ?? 8)
251
- }
252
-
253
- getRandomContacts(): RandomContactList<DhtNodeRpcRemote> {
254
- return this.randomContacts
255
- }
256
-
257
- getRingContacts(): RingContactList<DhtNodeRpcRemote> {
258
- return this.ringContacts
259
- }
260
-
261
- getNearbyContactCount(excludedNodeIds?: Set<DhtAddress>): number {
262
- return this.nearbyContacts.getSize(excludedNodeIds)
263
- }
264
-
265
- getNeighborCount(): number {
266
- return this.neighbors.count()
267
- }
268
-
269
- getNeighbors(): ReadonlyArray<DhtNodeRpcRemote> {
270
- return this.neighbors.toArray()
271
- }
272
-
273
- setContactActive(nodeId: DhtAddress): void {
274
- this.activeContacts.add(nodeId)
275
- }
276
-
277
- addContact(peerDescriptor: PeerDescriptor): void {
278
- if (this.stopped) {
279
- return
280
- }
281
- const nodeId = toNodeId(peerDescriptor)
282
- if (nodeId !== this.options.localNodeId) {
283
- logger.trace(`Adding new contact ${nodeId}`)
284
- const remote = this.options.createDhtNodeRpcRemote(peerDescriptor)
285
- const isInNeighbors = (this.neighbors.get(peerDescriptor.nodeId) !== null)
286
- const isInNearbyContacts = (this.nearbyContacts.getContact(nodeId) !== undefined)
287
- const isInRingContacts = this.ringContacts.getContact(peerDescriptor) !== undefined
288
-
289
- if (isInNeighbors || isInNearbyContacts) {
290
- this.randomContacts.addContact(remote)
291
- }
292
- if (!isInNeighbors) {
293
- this.neighbors.add(remote)
294
- }
295
- if (!isInNearbyContacts) {
296
- this.nearbyContacts.addContact(remote)
297
- }
298
- if (!isInRingContacts) {
299
- this.ringContacts.addContact(remote)
300
- }
301
- }
302
- }
303
- }
@@ -1,19 +0,0 @@
1
- import { PeerDescriptor } from '../../../generated/packages/dht/protos/DhtRpc'
2
- import { DhtAddress, toNodeId } from '../../identifiers'
3
-
4
- export class Contact {
5
-
6
- private peerDescriptor: PeerDescriptor
7
-
8
- constructor(peerDescriptor: PeerDescriptor) {
9
- this.peerDescriptor = peerDescriptor
10
- }
11
-
12
- public getPeerDescriptor(): PeerDescriptor {
13
- return this.peerDescriptor
14
- }
15
-
16
- public getNodeId(): DhtAddress {
17
- return toNodeId(this.peerDescriptor)
18
- }
19
- }
@@ -1,43 +0,0 @@
1
- import EventEmitter from 'eventemitter3'
2
- import { DhtAddress } from '../../identifiers'
3
-
4
- export interface Events<C> {
5
- contactRemoved: (removedContact: C) => void
6
- contactAdded: (contactAdded: C) => void
7
- }
8
-
9
- export class ContactList<C extends { getNodeId: () => DhtAddress }> extends EventEmitter<Events<C>> {
10
-
11
- protected contactsById: Map<DhtAddress, C> = new Map()
12
- // TODO move this to SortedContactList
13
- protected contactIds: DhtAddress[] = []
14
- protected localNodeId: DhtAddress
15
- protected maxSize: number
16
-
17
- constructor(
18
- localNodeId: DhtAddress,
19
- maxSize: number
20
- ) {
21
- super()
22
- this.localNodeId = localNodeId
23
- this.maxSize = maxSize
24
- }
25
-
26
- public getContact(id: DhtAddress): C | undefined {
27
- return this.contactsById.get(id)
28
- }
29
-
30
- public getSize(): number {
31
- return this.contactIds.length
32
- }
33
-
34
- public clear(): void {
35
- this.contactsById.clear()
36
- this.contactIds = []
37
- }
38
-
39
- public stop(): void {
40
- this.removeAllListeners()
41
- this.clear()
42
- }
43
- }