@streamr/dht 100.0.0-pretestnet.6 → 100.0.0-testnet-one.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. package/dist/src/connection/ConnectionLockHandler.js +2 -2
  2. package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
  3. package/dist/src/connection/ConnectionLockRpcRemote.d.ts +2 -2
  4. package/dist/src/connection/ConnectionLockRpcRemote.js +3 -27
  5. package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
  6. package/dist/src/connection/ConnectionManager.d.ts +0 -1
  7. package/dist/src/connection/ConnectionManager.js +11 -7
  8. package/dist/src/connection/ConnectionManager.js.map +1 -1
  9. package/dist/src/connection/ConnectorFacade.d.ts +2 -2
  10. package/dist/src/connection/ConnectorFacade.js +1 -2
  11. package/dist/src/connection/ConnectorFacade.js.map +1 -1
  12. package/dist/src/connection/ManagedConnection.js +1 -0
  13. package/dist/src/connection/ManagedConnection.js.map +1 -1
  14. package/dist/src/connection/connectivityChecker.d.ts +9 -0
  15. package/dist/src/connection/connectivityChecker.js +122 -0
  16. package/dist/src/connection/connectivityChecker.js.map +1 -0
  17. package/dist/src/connection/connectivityRequestHandler.d.ts +2 -0
  18. package/dist/src/connection/connectivityRequestHandler.js +79 -0
  19. package/dist/src/connection/connectivityRequestHandler.js.map +1 -0
  20. package/dist/src/connection/simulator/Simulator.js +3 -2
  21. package/dist/src/connection/simulator/Simulator.js.map +1 -1
  22. package/dist/src/connection/simulator/SimulatorConnection.js +1 -1
  23. package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
  24. package/dist/src/connection/webrtc/NodeWebrtcConnection.js +1 -1
  25. package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
  26. package/dist/src/connection/webrtc/WebrtcConnector.js +1 -1
  27. package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
  28. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +1 -1
  29. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
  30. package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +2 -2
  31. package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +2 -2
  32. package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
  33. package/dist/src/connection/webrtc/iceServerAsString.js +1 -1
  34. package/dist/src/connection/webrtc/iceServerAsString.js.map +1 -1
  35. package/dist/src/connection/websocket/ClientWebsocket.d.ts +1 -0
  36. package/dist/src/connection/websocket/ClientWebsocket.js +7 -3
  37. package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
  38. package/dist/src/connection/websocket/ServerWebsocket.d.ts +4 -0
  39. package/dist/src/connection/websocket/ServerWebsocket.js +32 -21
  40. package/dist/src/connection/websocket/ServerWebsocket.js.map +1 -1
  41. package/dist/src/connection/websocket/WebsocketConnector.d.ts +0 -2
  42. package/dist/src/connection/websocket/WebsocketConnector.js +61 -16
  43. package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
  44. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +1 -1
  45. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +8 -11
  46. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
  47. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +4 -4
  48. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +5 -39
  49. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
  50. package/dist/src/connection/websocket/WebsocketServer.js +21 -4
  51. package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
  52. package/dist/src/dht/DhtNode.d.ts +13 -23
  53. package/dist/src/dht/DhtNode.js +97 -226
  54. package/dist/src/dht/DhtNode.js.map +1 -1
  55. package/dist/src/dht/DhtNodeRpcLocal.d.ts +1 -4
  56. package/dist/src/dht/DhtNodeRpcLocal.js +1 -5
  57. package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
  58. package/dist/src/dht/DhtNodeRpcRemote.d.ts +3 -3
  59. package/dist/src/dht/DhtNodeRpcRemote.js +4 -4
  60. package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
  61. package/dist/src/dht/ExternalApiRpcLocal.d.ts +4 -4
  62. package/dist/src/dht/ExternalApiRpcLocal.js +5 -12
  63. package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
  64. package/dist/src/dht/ExternalApiRpcRemote.d.ts +3 -3
  65. package/dist/src/dht/ExternalApiRpcRemote.js +5 -5
  66. package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
  67. package/dist/src/dht/PeerManager.d.ts +52 -0
  68. package/dist/src/dht/PeerManager.js +273 -0
  69. package/dist/src/dht/PeerManager.js.map +1 -0
  70. package/dist/src/dht/contact/ContactList.d.ts +1 -1
  71. package/dist/src/dht/contact/ContactList.js +1 -0
  72. package/dist/src/dht/contact/ContactList.js.map +1 -1
  73. package/dist/src/dht/contact/{Remote.d.ts → RpcRemote.d.ts} +3 -3
  74. package/dist/src/dht/contact/{Remote.js → RpcRemote.js} +8 -8
  75. package/dist/src/dht/contact/RpcRemote.js.map +1 -0
  76. package/dist/src/dht/contact/SortedContactList.d.ts +20 -6
  77. package/dist/src/dht/contact/SortedContactList.js +55 -24
  78. package/dist/src/dht/contact/SortedContactList.js.map +1 -1
  79. package/dist/src/dht/discovery/DiscoverySession.d.ts +4 -14
  80. package/dist/src/dht/discovery/DiscoverySession.js +15 -26
  81. package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
  82. package/dist/src/dht/discovery/PeerDiscovery.d.ts +2 -9
  83. package/dist/src/dht/discovery/PeerDiscovery.js +11 -19
  84. package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
  85. package/dist/src/dht/find/FindRpcLocal.js +2 -1
  86. package/dist/src/dht/find/FindRpcLocal.js.map +1 -1
  87. package/dist/src/dht/find/FindSession.d.ts +6 -6
  88. package/dist/src/dht/find/FindSession.js +18 -13
  89. package/dist/src/dht/find/FindSession.js.map +1 -1
  90. package/dist/src/dht/find/FindSessionRpcLocal.d.ts +1 -1
  91. package/dist/src/dht/find/FindSessionRpcRemote.d.ts +2 -2
  92. package/dist/src/dht/find/FindSessionRpcRemote.js +2 -2
  93. package/dist/src/dht/find/FindSessionRpcRemote.js.map +1 -1
  94. package/dist/src/dht/find/Finder.d.ts +4 -4
  95. package/dist/src/dht/find/Finder.js +55 -42
  96. package/dist/src/dht/find/Finder.js.map +1 -1
  97. package/dist/src/dht/routing/FindRpcRemote.d.ts +2 -2
  98. package/dist/src/dht/routing/FindRpcRemote.js +7 -5
  99. package/dist/src/dht/routing/FindRpcRemote.js.map +1 -1
  100. package/dist/src/dht/routing/Router.d.ts +3 -7
  101. package/dist/src/dht/routing/Router.js +29 -22
  102. package/dist/src/dht/routing/Router.js.map +1 -1
  103. package/dist/src/dht/routing/RouterRpcLocal.d.ts +2 -2
  104. package/dist/src/dht/routing/RouterRpcLocal.js +4 -3
  105. package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
  106. package/dist/src/dht/routing/RouterRpcRemote.d.ts +2 -2
  107. package/dist/src/dht/routing/RouterRpcRemote.js +13 -8
  108. package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
  109. package/dist/src/dht/routing/RoutingSession.d.ts +1 -1
  110. package/dist/src/dht/routing/RoutingSession.js +23 -11
  111. package/dist/src/dht/routing/RoutingSession.js.map +1 -1
  112. package/dist/src/dht/routing/getPreviousPeer.js.map +1 -1
  113. package/dist/src/dht/store/LocalDataStore.d.ts +3 -3
  114. package/dist/src/dht/store/LocalDataStore.js +18 -17
  115. package/dist/src/dht/store/LocalDataStore.js.map +1 -1
  116. package/dist/src/dht/store/StoreRpcLocal.d.ts +10 -9
  117. package/dist/src/dht/store/StoreRpcLocal.js +108 -102
  118. package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
  119. package/dist/src/dht/store/StoreRpcRemote.d.ts +4 -5
  120. package/dist/src/dht/store/StoreRpcRemote.js +6 -15
  121. package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
  122. package/dist/src/exports.d.ts +1 -1
  123. package/dist/src/exports.js +4 -4
  124. package/dist/src/exports.js.map +1 -1
  125. package/dist/src/helpers/PeerID.d.ts +1 -0
  126. package/dist/src/helpers/PeerID.js +9 -4
  127. package/dist/src/helpers/PeerID.js.map +1 -1
  128. package/dist/src/helpers/UUID.js +1 -1
  129. package/dist/src/helpers/UUID.js.map +1 -1
  130. package/dist/src/helpers/nodeId.d.ts +1 -0
  131. package/dist/src/helpers/{kademliaId.js → nodeId.js} +4 -4
  132. package/dist/src/helpers/nodeId.js.map +1 -0
  133. package/dist/src/helpers/peerIdFromPeerDescriptor.js +4 -4
  134. package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
  135. package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +5 -16
  136. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +2 -9
  137. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
  138. package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +80 -95
  139. package/dist/src/proto/packages/dht/protos/DhtRpc.js +67 -66
  140. package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
  141. package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +3 -10
  142. package/dist/src/transport/RoutingRpcCommunicator.js +2 -0
  143. package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
  144. package/karma.config.js +2 -2
  145. package/package.json +5 -5
  146. package/protos/DhtRpc.proto +36 -36
  147. package/src/connection/ConnectionLockHandler.ts +2 -2
  148. package/src/connection/ConnectionLockRpcRemote.ts +3 -4
  149. package/src/connection/ConnectionManager.ts +19 -17
  150. package/src/connection/ConnectorFacade.ts +5 -7
  151. package/src/connection/ManagedConnection.ts +1 -0
  152. package/src/connection/connectivityChecker.ts +102 -0
  153. package/src/connection/connectivityRequestHandler.ts +79 -0
  154. package/src/connection/simulator/Simulator.ts +3 -2
  155. package/src/connection/simulator/SimulatorConnection.ts +1 -1
  156. package/src/connection/webrtc/BrowserWebrtcConnection.ts +10 -10
  157. package/src/connection/webrtc/NodeWebrtcConnection.ts +1 -1
  158. package/src/connection/webrtc/WebrtcConnector.ts +1 -1
  159. package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +1 -1
  160. package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +2 -2
  161. package/src/connection/webrtc/iceServerAsString.ts +1 -1
  162. package/src/connection/websocket/ClientWebsocket.ts +6 -2
  163. package/src/connection/websocket/ServerWebsocket.ts +40 -25
  164. package/src/connection/websocket/WebsocketConnector.ts +43 -22
  165. package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +9 -11
  166. package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +7 -16
  167. package/src/connection/websocket/WebsocketServer.ts +20 -5
  168. package/src/dht/DhtNode.ts +123 -280
  169. package/src/dht/DhtNodeRpcLocal.ts +2 -9
  170. package/src/dht/DhtNodeRpcRemote.ts +4 -4
  171. package/src/dht/ExternalApiRpcLocal.ts +8 -13
  172. package/src/dht/ExternalApiRpcRemote.ts +5 -5
  173. package/src/dht/PeerManager.ts +330 -0
  174. package/src/dht/contact/ContactList.ts +3 -2
  175. package/src/dht/contact/{Remote.ts → RpcRemote.ts} +7 -6
  176. package/src/dht/contact/SortedContactList.ts +87 -44
  177. package/src/dht/discovery/DiscoverySession.ts +19 -44
  178. package/src/dht/discovery/PeerDiscovery.ts +16 -28
  179. package/src/dht/find/FindRpcLocal.ts +2 -2
  180. package/src/dht/find/FindSession.ts +25 -20
  181. package/src/dht/find/FindSessionRpcLocal.ts +1 -1
  182. package/src/dht/find/FindSessionRpcRemote.ts +2 -2
  183. package/src/dht/find/Finder.ts +84 -64
  184. package/src/dht/routing/FindRpcRemote.ts +7 -5
  185. package/src/dht/routing/Router.ts +30 -25
  186. package/src/dht/routing/RouterRpcLocal.ts +5 -5
  187. package/src/dht/routing/RouterRpcRemote.ts +13 -10
  188. package/src/dht/routing/RoutingSession.ts +22 -17
  189. package/src/dht/routing/getPreviousPeer.ts +1 -1
  190. package/src/dht/store/LocalDataStore.ts +18 -17
  191. package/src/dht/store/StoreRpcLocal.ts +118 -113
  192. package/src/dht/store/StoreRpcRemote.ts +7 -23
  193. package/src/exports.ts +1 -1
  194. package/src/helpers/PeerID.ts +8 -4
  195. package/src/helpers/UUID.ts +1 -1
  196. package/src/helpers/{kademliaId.ts → nodeId.ts} +1 -1
  197. package/src/helpers/peerIdFromPeerDescriptor.ts +6 -6
  198. package/src/proto/packages/dht/protos/DhtRpc.client.ts +6 -20
  199. package/src/proto/packages/dht/protos/DhtRpc.server.ts +3 -10
  200. package/src/proto/packages/dht/protos/DhtRpc.ts +103 -135
  201. package/src/transport/RoutingRpcCommunicator.ts +2 -0
  202. package/test/benchmark/Find.test.ts +5 -5
  203. package/test/benchmark/KademliaCorrectness.test.ts +3 -3
  204. package/test/benchmark/SortedContactListBenchmark.test.ts +150 -0
  205. package/test/benchmark/WebsocketServerMemoryLeak.test.ts +41 -0
  206. package/test/benchmark/kademlia-simulation/Contact.ts +1 -1
  207. package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +1 -1
  208. package/test/benchmark/kademlia-simulation/SimulationNode.ts +6 -1
  209. package/test/end-to-end/Layer0-Layer1.test.ts +1 -1
  210. package/test/end-to-end/Layer0.test.ts +4 -4
  211. package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +11 -11
  212. package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +6 -6
  213. package/test/end-to-end/Layer0Webrtc.test.ts +2 -2
  214. package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +3 -3
  215. package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +3 -3
  216. package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +1 -1
  217. package/test/end-to-end/WebsocketConnectionRequest.test.ts +1 -1
  218. package/test/end-to-end/memory-leak.test.ts +9 -12
  219. package/test/integration/ConnectionLocking.test.ts +2 -2
  220. package/test/integration/ConnectionManager.test.ts +14 -14
  221. package/test/integration/DhtJoinPeerDiscovery.test.ts +3 -3
  222. package/test/integration/DhtNodeExternalAPI.test.ts +10 -7
  223. package/test/integration/DhtNodeRpcRemote.test.ts +4 -4
  224. package/test/integration/DhtRpc.test.ts +6 -6
  225. package/test/integration/Find.test.ts +3 -3
  226. package/test/integration/Layer1-scale.test.ts +3 -3
  227. package/test/integration/Mock-Layer1-Layer0.test.ts +16 -16
  228. package/test/integration/MultipleEntryPointJoining.test.ts +7 -7
  229. package/test/integration/{MigrateData.test.ts → ReplicateData.test.ts} +15 -10
  230. package/test/integration/RouteMessage.test.ts +2 -2
  231. package/test/integration/RouterRpcRemote.test.ts +2 -2
  232. package/test/integration/RpcErrors.test.ts +2 -2
  233. package/test/integration/ScaleDownDht.test.ts +4 -2
  234. package/test/integration/SimultaneousConnections.test.ts +89 -57
  235. package/test/integration/Store.test.ts +33 -13
  236. package/test/integration/StoreAndDelete.test.ts +19 -17
  237. package/test/integration/StoreOnDhtWithTwoNodes.test.ts +21 -21
  238. package/test/integration/StoreRpcRemote.test.ts +3 -3
  239. package/test/integration/WebrtcConnectionManagement.test.ts +2 -2
  240. package/test/integration/WebrtcConnectorRpc.test.ts +1 -1
  241. package/test/integration/WebsocketConnectionManagement.test.ts +41 -3
  242. package/test/integration/WebsocketConnectorRpc.test.ts +5 -7
  243. package/test/unit/ConnectivityHelpers.test.ts +4 -4
  244. package/test/unit/Finder.test.ts +69 -23
  245. package/test/unit/LocalDataStore.test.ts +60 -43
  246. package/test/unit/RandomContactList.test.ts +2 -2
  247. package/test/unit/Router.test.ts +19 -11
  248. package/test/unit/RoutingSession.test.ts +76 -0
  249. package/test/unit/SortedContactList.test.ts +17 -12
  250. package/test/unit/WebsocketConnector.test.ts +1 -1
  251. package/test/unit/connectivityRequestHandler.test.ts +71 -0
  252. package/test/utils/mock/Router.ts +1 -1
  253. package/test/utils/utils.ts +24 -22
  254. package/dist/src/connection/ConnectivityChecker.d.ts +0 -17
  255. package/dist/src/connection/ConnectivityChecker.js +0 -208
  256. package/dist/src/connection/ConnectivityChecker.js.map +0 -1
  257. package/dist/src/dht/contact/Remote.js.map +0 -1
  258. package/dist/src/helpers/kademliaId.d.ts +0 -1
  259. package/dist/src/helpers/kademliaId.js.map +0 -1
  260. package/src/connection/ConnectivityChecker.ts +0 -199
@@ -1,26 +1,34 @@
1
- import KBucket from 'k-bucket'
2
- import { PeerID } from '../../helpers/PeerID'
3
- import { ContactList, ContactState } from './ContactList'
1
+ import { PeerID, PeerIDKey } from '../../helpers/PeerID'
2
+ import { ContactState, Events } from './ContactList'
3
+ import { sortedIndexBy } from 'lodash'
4
+ import EventEmitter from 'eventemitter3'
5
+ import { getDistance } from '../PeerManager'
6
+
7
+ export interface SortedContactListConfig {
8
+ referenceId: PeerID // all contacts in this list are in sorted by the distance to this ID
9
+ allowToContainReferenceId: boolean
10
+ // TODO could maybe optimize this by removing the flag and then we'd check whether we have
11
+ // any listeners before we emit the event
12
+ emitEvents: boolean
13
+ maxSize?: number
14
+ // if set, the list can't contain any contacts which are futher away than this limit
15
+ peerIdDistanceLimit?: PeerID
16
+ // if set, the list can't contain contacts with these ids
17
+ excludedPeerIDs?: PeerID[]
18
+ }
4
19
 
5
- export class SortedContactList<C extends { getPeerId: () => PeerID }> extends ContactList<C> {
20
+ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends EventEmitter<Events<C>> {
6
21
 
7
- private allowLocalPeerId: boolean
8
- private peerIdDistanceLimit?: PeerID
9
- private excludedPeerIDs?: PeerID[]
22
+ private config: SortedContactListConfig
23
+ private contactsById: Map<PeerIDKey, ContactState<C>> = new Map()
24
+ private contactIds: PeerID[] = []
10
25
 
11
26
  constructor(
12
- ownId: PeerID,
13
- maxSize: number,
14
- defaultContactQueryLimit?: number,
15
- allowLocalPeerId = false,
16
- peerIdDistanceLimit?: PeerID,
17
- excludedPeerIDs?: PeerID[]
27
+ config: SortedContactListConfig
18
28
  ) {
19
- super(ownId, maxSize, defaultContactQueryLimit)
29
+ super()
30
+ this.config = config
20
31
  this.compareIds = this.compareIds.bind(this)
21
- this.allowLocalPeerId = allowLocalPeerId
22
- this.peerIdDistanceLimit = peerIdDistanceLimit
23
- this.excludedPeerIDs = excludedPeerIDs
24
32
  }
25
33
 
26
34
  public getClosestContactId(): PeerID {
@@ -32,38 +40,44 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
32
40
  }
33
41
 
34
42
  public addContact(contact: C): void {
35
- if (this.excludedPeerIDs
36
- && this.excludedPeerIDs.some((peerId) => contact.getPeerId().equals(peerId))) {
43
+ if (this.config.excludedPeerIDs !== undefined
44
+ && this.config.excludedPeerIDs.some((peerId) => contact.getPeerId().equals(peerId))) {
37
45
  return
38
46
  }
39
-
40
- if ((!this.allowLocalPeerId && this.ownId.equals(contact.getPeerId())) ||
41
- (this.peerIdDistanceLimit !== undefined && this.compareIds(this.peerIdDistanceLimit, contact.getPeerId()) < 0)) {
47
+
48
+ if ((!this.config.allowToContainReferenceId && this.config.referenceId.equals(contact.getPeerId())) ||
49
+ (this.config.peerIdDistanceLimit !== undefined && this.compareIds(this.config.peerIdDistanceLimit, contact.getPeerId()) < 0)) {
42
50
  return
43
51
  }
44
52
  if (!this.contactsById.has(contact.getPeerId().toKey())) {
45
- if (this.contactIds.length < this.maxSize) {
53
+ if ((this.config.maxSize === undefined) || (this.contactIds.length < this.config.maxSize)) {
46
54
  this.contactsById.set(contact.getPeerId().toKey(), new ContactState(contact))
47
- this.contactIds.push(contact.getPeerId())
48
- this.contactIds.sort(this.compareIds)
49
- } else if (this.compareIds(this.contactIds[this.maxSize - 1], contact.getPeerId()) > 0) {
55
+
56
+ const index = sortedIndexBy(this.contactIds, contact.getPeerId(), (id: PeerID) => { return this.distanceToReferenceId(id) })
57
+ this.contactIds.splice(index, 0, contact.getPeerId())
58
+ } else if (this.compareIds(this.contactIds[this.config.maxSize - 1], contact.getPeerId()) > 0) {
50
59
  const removedId = this.contactIds.pop()
51
60
  const removedContact = this.contactsById.get(removedId!.toKey())!.contact
52
61
  this.contactsById.delete(removedId!.toKey())
53
62
  this.contactsById.set(contact.getPeerId().toKey(), new ContactState(contact))
54
- this.contactIds.push(contact.getPeerId())
55
- this.contactIds.sort(this.compareIds)
63
+
64
+ const index = sortedIndexBy(this.contactIds, contact.getPeerId(), (id: PeerID) => { return this.distanceToReferenceId(id) })
65
+ this.contactIds.splice(index, 0, contact.getPeerId())
66
+ if (this.config.emitEvents) {
67
+ this.emit(
68
+ 'contactRemoved',
69
+ removedContact,
70
+ this.getClosestContacts()
71
+ )
72
+ }
73
+ }
74
+ if (this.config.emitEvents) {
56
75
  this.emit(
57
- 'contactRemoved',
58
- removedContact,
76
+ 'newContact',
77
+ contact,
59
78
  this.getClosestContacts()
60
79
  )
61
80
  }
62
- this.emit(
63
- 'newContact',
64
- contact,
65
- this.getClosestContacts()
66
- )
67
81
  }
68
82
  }
69
83
 
@@ -71,6 +85,10 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
71
85
  contacts.forEach((contact) => this.addContact(contact))
72
86
  }
73
87
 
88
+ public getContact(id: PeerID): ContactState<C> | undefined {
89
+ return this.contactsById.get(id.toKey())
90
+ }
91
+
74
92
  public setContacted(contactId: PeerID): void {
75
93
  if (this.contactsById.has(contactId.toKey())) {
76
94
  this.contactsById.get(contactId.toKey())!.contacted = true
@@ -83,7 +101,7 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
83
101
  }
84
102
  }
85
103
 
86
- public getClosestContacts(limit = this.defaultContactQueryLimit): C[] {
104
+ public getClosestContacts(limit?: number): C[] {
87
105
  const ret: C[] = []
88
106
  this.contactIds.forEach((contactId) => {
89
107
  const contact = this.contactsById.get(contactId.toKey())
@@ -91,7 +109,11 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
91
109
  ret.push(contact.contact)
92
110
  }
93
111
  })
94
- return ret.slice(0, limit)
112
+ if (limit === undefined) {
113
+ return ret
114
+ } else {
115
+ return ret.slice(0, limit)
116
+ }
95
117
  }
96
118
 
97
119
  public getUncontactedContacts(num: number): C[] {
@@ -124,22 +146,29 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
124
146
  }
125
147
 
126
148
  public compareIds(id1: PeerID, id2: PeerID): number {
127
- const distance1 = KBucket.distance(this.ownId.value, id1.value)
128
- const distance2 = KBucket.distance(this.ownId.value, id2.value)
149
+ const distance1 = this.distanceToReferenceId(id1)
150
+ const distance2 = this.distanceToReferenceId(id2)
129
151
  return distance1 - distance2
130
152
  }
131
153
 
154
+ // TODO inline this method?
155
+ private distanceToReferenceId(id: PeerID): number {
156
+ return getDistance(this.config.referenceId.value, id.value)
157
+ }
158
+
132
159
  public removeContact(id: PeerID): boolean {
133
160
  if (this.contactsById.has(id.toKey())) {
134
161
  const removed = this.contactsById.get(id.toKey())!.contact
135
162
  const index = this.contactIds.findIndex((element) => element.equals(id))
136
163
  this.contactIds.splice(index, 1)
137
164
  this.contactsById.delete(id.toKey())
138
- this.emit(
139
- 'contactRemoved',
140
- removed,
141
- this.getClosestContacts()
142
- )
165
+ if (this.config.emitEvents) {
166
+ this.emit(
167
+ 'contactRemoved',
168
+ removed,
169
+ this.getClosestContacts()
170
+ )
171
+ }
143
172
  return true
144
173
  }
145
174
  return false
@@ -152,4 +181,18 @@ export class SortedContactList<C extends { getPeerId: () => PeerID }> extends Co
152
181
  public getAllContacts(): C[] {
153
182
  return this.contactIds.map((peerId) => this.contactsById.get(peerId.toKey())!.contact)
154
183
  }
184
+
185
+ public getSize(): number {
186
+ return this.contactIds.length
187
+ }
188
+
189
+ public clear(): void {
190
+ this.contactsById.clear()
191
+ this.contactIds = []
192
+ }
193
+
194
+ public stop(): void {
195
+ this.removeAllListeners()
196
+ this.clear()
197
+ }
155
198
  }
@@ -1,15 +1,11 @@
1
- import { RpcCommunicator, toProtoRpcClient } from '@streamr/proto-rpc'
2
1
  import { Logger, runAndWaitForEvents3 } from '@streamr/utils'
3
2
  import EventEmitter from 'eventemitter3'
4
- import KBucket from 'k-bucket'
5
3
  import { v4 } from 'uuid'
6
- import { PeerID } from '../../helpers/PeerID'
4
+ import { PeerID, PeerIDKey } from '../../helpers/PeerID'
7
5
  import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
8
- import { DhtNodeRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
9
- import { SortedContactList } from '../contact/SortedContactList'
6
+ import { PeerManager, getDistance } from '../PeerManager'
10
7
  import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
11
- import { areEqualPeerDescriptors, getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
12
- import { ServiceID } from '../../types/ServiceID'
8
+ import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
13
9
 
14
10
  const logger = new Logger(module)
15
11
 
@@ -18,16 +14,10 @@ interface DiscoverySessionEvents {
18
14
  }
19
15
 
20
16
  interface DiscoverySessionConfig {
21
- bucket: KBucket<DhtNodeRpcRemote>
22
- neighborList: SortedContactList<DhtNodeRpcRemote>
23
17
  targetId: Uint8Array
24
- localPeerDescriptor: PeerDescriptor
25
- serviceId: ServiceID
26
- rpcCommunicator: RpcCommunicator
27
18
  parallelism: number
28
19
  noProgressLimit: number
29
- newContactListener?: (rpcRemote: DhtNodeRpcRemote) => void
30
- rpcRequestTimeout?: number
20
+ peerManager: PeerManager
31
21
  }
32
22
 
33
23
  export class DiscoverySession {
@@ -39,6 +29,7 @@ export class DiscoverySession {
39
29
  private noProgressCounter = 0
40
30
  private ongoingClosestPeersRequests: Set<string> = new Set()
41
31
  private readonly config: DiscoverySessionConfig
32
+ private contactedPeers: Set<PeerIDKey> = new Set()
42
33
 
43
34
  constructor(config: DiscoverySessionConfig) {
44
35
  this.config = config
@@ -48,23 +39,7 @@ export class DiscoverySession {
48
39
  if (this.stopped) {
49
40
  return
50
41
  }
51
- contacts.forEach((contact) => {
52
- if (!areEqualPeerDescriptors(contact, this.config.localPeerDescriptor)) {
53
- const rpcRemote = new DhtNodeRpcRemote(
54
- this.config.localPeerDescriptor,
55
- contact,
56
- toProtoRpcClient(new DhtNodeRpcClient(this.config.rpcCommunicator.getRpcClientTransport())),
57
- this.config.serviceId,
58
- this.config.rpcRequestTimeout
59
- )
60
- if (this.config.newContactListener) {
61
- this.config.newContactListener(rpcRemote)
62
- }
63
- if (!this.config.neighborList.getContact(rpcRemote.getPeerId())) {
64
- this.config.neighborList.addContact(rpcRemote)
65
- }
66
- }
67
- })
42
+ this.config.peerManager.handleNewPeers(contacts)
68
43
  }
69
44
 
70
45
  private async getClosestPeersFromContact(contact: DhtNodeRpcRemote): Promise<PeerDescriptor[]> {
@@ -73,9 +48,9 @@ export class DiscoverySession {
73
48
  }
74
49
  logger.trace(`Getting closest peers from contact: ${getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())}`)
75
50
  this.outgoingClosestPeersRequestsCounter++
76
- this.config.neighborList.setContacted(contact.getPeerId())
51
+ this.contactedPeers.add(contact.getPeerId().toKey())
77
52
  const returnedContacts = await contact.getClosestPeers(this.config.targetId)
78
- this.config.neighborList.setActive(contact.getPeerId())
53
+ this.config.peerManager.handlePeerActive(contact.getPeerId())
79
54
  return returnedContacts
80
55
  }
81
56
 
@@ -84,9 +59,12 @@ export class DiscoverySession {
84
59
  return
85
60
  }
86
61
  this.ongoingClosestPeersRequests.delete(peerId.toKey())
87
- const oldClosestContact = this.config.neighborList.getClosestContactId()
62
+ const oldClosestNeighbor = this.config.peerManager.getClosestNeighborsTo(this.config.targetId, 1)[0]
63
+ const oldClosestDistance = getDistance(this.config.targetId, oldClosestNeighbor.getPeerId().value)
88
64
  this.addNewContacts(contacts)
89
- if (this.config.neighborList.getClosestContactId().equals(oldClosestContact)) {
65
+ const newClosestNeighbor = this.config.peerManager.getClosestNeighborsTo(this.config.targetId, 1)[0]
66
+ const newClosestDistance = getDistance(this.config.targetId, newClosestNeighbor.getPeerId().value)
67
+ if (newClosestDistance >= oldClosestDistance) {
90
68
  this.noProgressCounter++
91
69
  } else {
92
70
  this.noProgressCounter = 0
@@ -98,15 +76,14 @@ export class DiscoverySession {
98
76
  return
99
77
  }
100
78
  this.ongoingClosestPeersRequests.delete(peer.getPeerId().toKey())
101
- this.config.bucket.remove(peer.getPeerId().value)
102
- this.config.neighborList.removeContact(peer.getPeerId())
79
+ this.config.peerManager.handlePeerUnresponsive(peer.getPeerId())
103
80
  }
104
81
 
105
82
  private findMoreContacts(): void {
106
83
  if (this.stopped) {
107
84
  return
108
85
  }
109
- const uncontacted = this.config.neighborList.getUncontactedContacts(this.config.parallelism)
86
+ const uncontacted = this.config.peerManager.getClosestContactsTo(this.config.targetId, this.config.parallelism, this.contactedPeers)
110
87
  if (uncontacted.length === 0 || this.noProgressCounter >= this.config.noProgressLimit) {
111
88
  this.emitter.emit('discoveryCompleted')
112
89
  this.stopped = true
@@ -128,18 +105,16 @@ export class DiscoverySession {
128
105
  }
129
106
  }
130
107
 
131
- public async findClosestNodes(timeout: number): Promise<SortedContactList<DhtNodeRpcRemote>> {
132
- if (this.config.neighborList.getUncontactedContacts(this.config.parallelism).length === 0) {
133
- logger.trace('getUncontactedContacts length was 0 in beginning of discovery, this.neighborList.size: '
134
- + this.config.neighborList.getSize())
135
- return this.config.neighborList
108
+ public async findClosestNodes(timeout: number): Promise<void> {
109
+ if (this.config.peerManager.getNumberOfContacts(this.contactedPeers) === 0) {
110
+ return
136
111
  }
112
+ // TODO add abortController and signal it in stop()
137
113
  await runAndWaitForEvents3<DiscoverySessionEvents>(
138
114
  [this.findMoreContacts.bind(this)],
139
115
  [[this.emitter, 'discoveryCompleted']],
140
116
  timeout
141
117
  )
142
- return this.config.neighborList
143
118
  }
144
119
 
145
120
  public stop(): void {
@@ -3,26 +3,20 @@ import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
3
3
  import { areEqualPeerDescriptors, getNodeIdFromPeerDescriptor, peerIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
4
4
  import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
5
5
  import { Logger, scheduleAtInterval, setAbortableTimeout } from '@streamr/utils'
6
- import KBucket from 'k-bucket'
7
- import { SortedContactList } from '../contact/SortedContactList'
8
6
  import { ConnectionManager } from '../../connection/ConnectionManager'
9
- import { RoutingRpcCommunicator } from '../../transport/RoutingRpcCommunicator'
10
- import { createRandomKademliaId } from '../../helpers/kademliaId'
7
+ import { PeerManager } from '../PeerManager'
8
+ import { createRandomNodeId } from '../../helpers/nodeId'
11
9
  import { ServiceID } from '../../types/ServiceID'
12
10
 
13
11
  interface PeerDiscoveryConfig {
14
- rpcCommunicator: RoutingRpcCommunicator
15
12
  localPeerDescriptor: PeerDescriptor
16
- bucket: KBucket<DhtNodeRpcRemote>
17
- neighborList: SortedContactList<DhtNodeRpcRemote>
18
13
  joinNoProgressLimit: number
19
14
  peerDiscoveryQueryBatchSize: number
20
15
  serviceId: ServiceID
21
16
  parallelism: number
22
17
  joinTimeout: number
23
- addContact: (contact: PeerDescriptor, setActive?: boolean) => void
24
18
  connectionManager?: ConnectionManager
25
- rpcRequestTimeout?: number
19
+ peerManager: PeerManager
26
20
  }
27
21
 
28
22
  const logger = new Logger(module)
@@ -55,13 +49,11 @@ export class PeerDiscovery {
55
49
  return
56
50
  }
57
51
  this.config.connectionManager?.lockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
58
- this.config.addContact(entryPointDescriptor)
52
+ this.config.peerManager.handleNewPeers([entryPointDescriptor])
59
53
  const targetId = peerIdFromPeerDescriptor(this.config.localPeerDescriptor).value
60
- const closest = this.config.bucket.closest(targetId, this.config.peerDiscoveryQueryBatchSize)
61
- this.config.neighborList.addContacts(closest)
62
54
  const sessions = [this.createSession(targetId)]
63
55
  if (doAdditionalRandomPeerDiscovery) {
64
- sessions.push(this.createSession(createRandomKademliaId()))
56
+ sessions.push(this.createSession(createRandomNodeId()))
65
57
  }
66
58
  await this.runSessions(sessions, entryPointDescriptor, retry)
67
59
  this.config.connectionManager?.unlockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
@@ -70,15 +62,10 @@ export class PeerDiscovery {
70
62
 
71
63
  private createSession(targetId: Uint8Array): DiscoverySession {
72
64
  const sessionOptions = {
73
- bucket: this.config.bucket,
74
- neighborList: this.config.neighborList,
75
65
  targetId,
76
- localPeerDescriptor: this.config.localPeerDescriptor,
77
- serviceId: this.config.serviceId,
78
- rpcCommunicator: this.config.rpcCommunicator,
79
66
  parallelism: this.config.parallelism,
80
67
  noProgressLimit: this.config.joinNoProgressLimit,
81
- newContactListener: (newPeer: DhtNodeRpcRemote) => this.config.addContact(newPeer.getPeerDescriptor())
68
+ peerManager: this.config.peerManager
82
69
  }
83
70
  return new DiscoverySession(sessionOptions)
84
71
  }
@@ -93,8 +80,9 @@ export class PeerDiscovery {
93
80
  logger.debug(`DHT join on ${this.config.serviceId} timed out`)
94
81
  } finally {
95
82
  if (!this.isStopped()) {
96
- if (this.config.bucket.count() === 0) {
83
+ if (this.config.peerManager.getNumberOfNeighbors() === 0) {
97
84
  if (retry) {
85
+ // TODO should we catch possible promise rejection?
98
86
  setAbortableTimeout(() => this.rejoinDht(entryPointDescriptor), 1000, this.abortController.signal)
99
87
  }
100
88
  } else {
@@ -112,12 +100,12 @@ export class PeerDiscovery {
112
100
  logger.debug(`Rejoining DHT ${this.config.serviceId}`)
113
101
  this.rejoinOngoing = true
114
102
  try {
115
- this.config.neighborList.clear()
116
103
  await this.joinDht(entryPoint)
117
104
  logger.debug(`Rejoined DHT successfully ${this.config.serviceId}!`)
118
105
  } catch (err) {
119
106
  logger.warn(`Rejoining DHT ${this.config.serviceId} failed`)
120
107
  if (!this.isStopped()) {
108
+ // TODO should we catch possible promise rejection?
121
109
  setAbortableTimeout(() => this.rejoinDht(entryPoint), 5000, this.abortController.signal)
122
110
  }
123
111
  } finally {
@@ -136,13 +124,13 @@ export class PeerDiscovery {
136
124
  if (this.isStopped()) {
137
125
  return
138
126
  }
139
- const nodes = this.config.bucket.closest(peerIdFromPeerDescriptor(this.config.localPeerDescriptor).value, this.config.parallelism)
140
- await Promise.allSettled(nodes.map(async (peer: DhtNodeRpcRemote) => {
141
- const contacts = await peer.getClosestPeers(this.config.localPeerDescriptor.kademliaId)
142
- contacts.forEach((contact) => {
143
- this.config.addContact(contact)
127
+ const nodes = this.config.peerManager.getClosestNeighborsTo(this.config.localPeerDescriptor.nodeId, this.config.parallelism)
128
+ await Promise.allSettled(
129
+ nodes.map(async (peer: DhtNodeRpcRemote) => {
130
+ const contacts = await peer.getClosestPeers(this.config.localPeerDescriptor.nodeId!)
131
+ this.config.peerManager.handleNewPeers(contacts)
144
132
  })
145
- }))
133
+ )
146
134
  }
147
135
 
148
136
  public isJoinOngoing(): boolean {
@@ -163,7 +151,7 @@ export class PeerDiscovery {
163
151
  clearTimeout(this.rejoinTimeoutRef)
164
152
  this.rejoinTimeoutRef = undefined
165
153
  }
166
- this.ongoingDiscoverySessions.forEach((session, _id) => {
154
+ this.ongoingDiscoverySessions.forEach((session) => {
167
155
  session.stop()
168
156
  })
169
157
  }
@@ -1,5 +1,5 @@
1
1
  import { Logger } from '@streamr/utils'
2
- import { PeerDescriptor, RouteMessageAck, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
2
+ import { PeerDescriptor, RouteMessageAck, RouteMessageError, RouteMessageWrapper } from '../../proto/packages/dht/protos/DhtRpc'
3
3
  import { IFindRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
4
4
  import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
5
5
  import { createRouteMessageAck } from '../routing/RouterRpcLocal'
@@ -24,7 +24,7 @@ export class FindRpcLocal implements IFindRpc {
24
24
 
25
25
  async routeFindRequest(routedMessage: RouteMessageWrapper): Promise<RouteMessageAck> {
26
26
  if (this.config.isMostLikelyDuplicate(routedMessage.requestId)) {
27
- return createRouteMessageAck(routedMessage, 'message given to routeFindRequest() service is likely a duplicate')
27
+ return createRouteMessageAck(routedMessage, RouteMessageError.DUPLICATE)
28
28
  }
29
29
  const senderId = getNodeIdFromPeerDescriptor(getPreviousPeer(routedMessage) ?? routedMessage.sourcePeer!)
30
30
  logger.trace(`Received routeFindRequest call from ${senderId}`)
@@ -1,6 +1,6 @@
1
1
  import EventEmitter from 'eventemitter3'
2
2
  import { PeerID, PeerIDKey } from '../../helpers/PeerID'
3
- import { DataEntry, PeerDescriptor, FindResponse } from '../../proto/packages/dht/protos/DhtRpc'
3
+ import { DataEntry, PeerDescriptor, FindResponse, FindAction } from '../../proto/packages/dht/protos/DhtRpc'
4
4
  import { ITransport } from '../../transport/ITransport'
5
5
  import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
6
6
  import { Contact } from '../contact/Contact'
@@ -17,20 +17,20 @@ export interface FindSessionEvents {
17
17
  export interface FindSessionConfig {
18
18
  serviceId: ServiceID
19
19
  transport: ITransport
20
- kademliaIdToFind: Uint8Array
20
+ nodeIdToFind: Uint8Array
21
21
  localPeerId: PeerID
22
22
  waitedRoutingPathCompletions: number
23
- fetchData: boolean
23
+ action: FindAction
24
24
  }
25
25
 
26
26
  export class FindSession extends EventEmitter<FindSessionEvents> {
27
27
  private readonly serviceId: ServiceID
28
28
  private readonly transport: ITransport
29
- private readonly kademliaIdToFind: Uint8Array
29
+ private readonly nodeIdToFind: Uint8Array
30
30
  private readonly localPeerId: PeerID
31
31
  private readonly waitedRoutingPathCompletions: number
32
32
  private readonly rpcCommunicator: ListeningRpcCommunicator
33
- private readonly fetchData: boolean
33
+ private readonly action: FindAction
34
34
  private results: SortedContactList<Contact>
35
35
  private foundData: Map<string, DataEntry> = new Map()
36
36
  private allKnownHops: Set<PeerIDKey> = new Set()
@@ -43,11 +43,16 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
43
43
  super()
44
44
  this.serviceId = config.serviceId
45
45
  this.transport = config.transport
46
- this.kademliaIdToFind = config.kademliaIdToFind
46
+ this.nodeIdToFind = config.nodeIdToFind
47
47
  this.localPeerId = config.localPeerId
48
48
  this.waitedRoutingPathCompletions = config.waitedRoutingPathCompletions
49
- this.results = new SortedContactList(PeerID.fromValue(this.kademliaIdToFind), 10, undefined, true)
50
- this.fetchData = config.fetchData
49
+ this.results = new SortedContactList({
50
+ referenceId: PeerID.fromValue(this.nodeIdToFind),
51
+ maxSize: 10, // TODO use config option or named constant?
52
+ allowToContainReferenceId: true,
53
+ emitEvents: false
54
+ })
55
+ this.action = config.action
51
56
  this.rpcCommunicator = new ListeningRpcCommunicator(this.serviceId, this.transport, {
52
57
  rpcRequestTimeout: 15000
53
58
  })
@@ -56,7 +61,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
56
61
 
57
62
  private registerLocalRpcMethods() {
58
63
  const rpcLocal = new FindSessionRpcLocal({
59
- doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound?: boolean) => {
64
+ doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound: boolean) => {
60
65
  this.doSendFindResponse(routingPath, nodes, dataEntries, noCloserNodesFound)
61
66
  }
62
67
  })
@@ -70,10 +75,10 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
70
75
  unreportedHops.delete(id)
71
76
  })
72
77
  if (this.noCloserNodesReceivedCounter >= 1 && unreportedHops.size === 0) {
73
- if (this.fetchData
78
+ if (this.action === FindAction.FETCH_DATA
74
79
  && (this.hasNonStaleData() || this.noCloserNodesReceivedCounter >= this.waitedRoutingPathCompletions)) {
75
80
  return true
76
- } else if (this.fetchData) {
81
+ } else if (this.action === FindAction.FETCH_DATA) {
77
82
  return false
78
83
  }
79
84
  return true
@@ -89,7 +94,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
89
94
  routingPath: PeerDescriptor[],
90
95
  nodes: PeerDescriptor[],
91
96
  dataEntries: DataEntry[],
92
- noCloserNodesFound?: boolean
97
+ noCloserNodesFound: boolean
93
98
  ): void {
94
99
  this.addKnownHops(routingPath)
95
100
  if (routingPath.length >= 1) {
@@ -106,7 +111,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
106
111
 
107
112
  private addKnownHops(routingPath: PeerDescriptor[]) {
108
113
  routingPath.forEach((desc) => {
109
- const newPeerId = PeerID.fromValue(desc.kademliaId)
114
+ const newPeerId = PeerID.fromValue(desc.nodeId)
110
115
  if (!this.localPeerId.equals(newPeerId)) {
111
116
  this.allKnownHops.add(newPeerId.toKey())
112
117
  }
@@ -114,7 +119,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
114
119
  }
115
120
 
116
121
  private setHopAsReported(desc: PeerDescriptor) {
117
- const newPeerId = PeerID.fromValue(desc.kademliaId)
122
+ const newPeerId = PeerID.fromValue(desc.nodeId)
118
123
  if (!this.localPeerId.equals(newPeerId)) {
119
124
  this.reportedHops.add(newPeerId.toKey())
120
125
  }
@@ -132,11 +137,11 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
132
137
 
133
138
  private processFoundData(dataEntries: DataEntry[]): void {
134
139
  dataEntries.forEach((entry) => {
135
- const storerKey = keyFromPeerDescriptor(entry.storer!)
136
- const existingEntry = this.foundData.get(storerKey)
137
- if (!existingEntry || existingEntry.storerTime! < entry.storerTime!
138
- || (existingEntry.storerTime! <= entry.storerTime! && entry.deleted)) {
139
- this.foundData.set(storerKey, entry)
140
+ const creatorKey = keyFromPeerDescriptor(entry.creator!)
141
+ const existingEntry = this.foundData.get(creatorKey)
142
+ if (!existingEntry || existingEntry.createdAt! < entry.createdAt!
143
+ || (existingEntry.createdAt! <= entry.createdAt! && entry.deleted)) {
144
+ this.foundData.set(creatorKey, entry)
140
145
  }
141
146
  })
142
147
  }
@@ -164,7 +169,7 @@ export class FindSession extends EventEmitter<FindSessionEvents> {
164
169
 
165
170
  public getResults = (): FindResult => ({
166
171
  closestNodes: this.results.getAllContacts().map((contact) => contact.getPeerDescriptor()),
167
- dataEntries: (this.foundData && this.foundData.size > 0) ? Array.from(this.foundData.values()) : undefined
172
+ dataEntries: Array.from(this.foundData.values())
168
173
  })
169
174
 
170
175
  public stop(): void {
@@ -6,7 +6,7 @@ import { Logger } from '@streamr/utils'
6
6
  const logger = new Logger(module)
7
7
 
8
8
  interface FindSessionRpcLocalConfig {
9
- doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound?: boolean) => void
9
+ doSendFindResponse: (routingPath: PeerDescriptor[], nodes: PeerDescriptor[], dataEntries: DataEntry[], noCloserNodesFound: boolean) => void
10
10
  }
11
11
 
12
12
  export class FindSessionRpcLocal implements IFindSessionRpc {
@@ -5,11 +5,11 @@ import {
5
5
  } from '../../proto/packages/dht/protos/DhtRpc'
6
6
  import { IFindSessionRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
7
7
  import { Logger } from '@streamr/utils'
8
- import { Remote } from '../contact/Remote'
8
+ import { RpcRemote } from '../contact/RpcRemote'
9
9
 
10
10
  const logger = new Logger(module)
11
11
 
12
- export class FindSessionRpcRemote extends Remote<IFindSessionRpcClient> {
12
+ export class FindSessionRpcRemote extends RpcRemote<IFindSessionRpcClient> {
13
13
 
14
14
  sendFindResponse(
15
15
  routingPath: PeerDescriptor[],