@streamr/dht 100.0.0-testnet-two.0 → 100.0.0-testnet-two.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.
- package/dist/package.json +66 -0
- package/dist/src/connection/ConnectionLockHandler.d.ts +11 -11
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionManager.js +3 -3
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +1 -1
- package/dist/src/connection/Handshaker.js +14 -5
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +3 -3
- package/dist/src/connection/ManagedConnection.js +3 -3
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +11 -3
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.js +18 -8
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +9 -8
- package/dist/src/dht/DhtNode.js +23 -18
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +4 -3
- package/dist/src/dht/DhtNodeRpcLocal.js +3 -2
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +6 -5
- package/dist/src/dht/DhtNodeRpcRemote.js +10 -11
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +3 -3
- package/dist/src/dht/ExternalApiRpcLocal.js +3 -2
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +3 -2
- package/dist/src/dht/ExternalApiRpcRemote.js +3 -2
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +9 -9
- package/dist/src/dht/PeerManager.js +12 -12
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/Contact.d.ts +2 -2
- package/dist/src/dht/contact/ContactList.d.ts +7 -7
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +4 -4
- package/dist/src/dht/contact/RandomContactList.js +2 -3
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/RpcRemote.d.ts +1 -4
- package/dist/src/dht/contact/RpcRemote.js +1 -5
- package/dist/src/dht/contact/RpcRemote.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +13 -13
- package/dist/src/dht/contact/SortedContactList.js +5 -5
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +3 -2
- package/dist/src/dht/discovery/DiscoverySession.js +9 -12
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +3 -1
- package/dist/src/dht/discovery/PeerDiscovery.js +16 -9
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +12 -11
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +2 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +6 -6
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +2 -2
- package/dist/src/dht/routing/Router.js +2 -2
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.js +3 -3
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -3
- package/dist/src/dht/routing/RoutingSession.js +4 -4
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +6 -8
- package/dist/src/dht/store/LocalDataStore.js +18 -24
- package/dist/src/dht/store/LocalDataStore.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +3 -3
- package/dist/src/dht/store/StoreManager.js +20 -21
- package/dist/src/dht/store/StoreManager.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.d.ts +2 -1
- package/dist/src/dht/store/StoreRpcLocal.js +10 -8
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/exports.d.ts +2 -0
- package/dist/src/exports.js +6 -1
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/AddressTools.js +2 -0
- package/dist/src/helpers/AddressTools.js.map +1 -1
- package/dist/src/helpers/PeerID.d.ts +2 -2
- package/dist/src/helpers/PeerID.js +3 -3
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +2 -2
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +2 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/helpers/versionCompatibility.d.ts +2 -0
- package/dist/src/helpers/versionCompatibility.js +18 -0
- package/dist/src/helpers/versionCompatibility.js.map +1 -0
- package/dist/src/identifiers.d.ts +6 -0
- package/dist/src/identifiers.js +23 -0
- package/dist/src/identifiers.js.map +1 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +13 -5
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +9 -5
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/package.json +5 -5
- package/protos/DhtRpc.proto +3 -1
- package/src/connection/ConnectionLockHandler.ts +15 -15
- package/src/connection/ConnectionLockRpcLocal.ts +3 -3
- package/src/connection/ConnectionManager.ts +4 -7
- package/src/connection/Handshaker.ts +21 -6
- package/src/connection/ManagedConnection.ts +10 -6
- package/src/connection/simulator/Simulator.ts +2 -2
- package/src/connection/simulator/SimulatorConnector.ts +2 -2
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -4
- package/src/connection/webrtc/WebrtcConnector.ts +2 -3
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +11 -5
- package/src/connection/websocket/WebsocketConnector.ts +19 -11
- package/src/dht/DhtNode.ts +33 -31
- package/src/dht/DhtNodeRpcLocal.ts +4 -4
- package/src/dht/DhtNodeRpcRemote.ts +15 -17
- package/src/dht/ExternalApiRpcLocal.ts +15 -6
- package/src/dht/ExternalApiRpcRemote.ts +5 -4
- package/src/dht/PeerManager.ts +20 -20
- package/src/dht/contact/Contact.ts +2 -2
- package/src/dht/contact/ContactList.ts +7 -7
- package/src/dht/contact/RandomContactList.ts +6 -6
- package/src/dht/contact/RpcRemote.ts +0 -8
- package/src/dht/contact/SortedContactList.ts +22 -22
- package/src/dht/discovery/DiscoverySession.ts +16 -16
- package/src/dht/discovery/PeerDiscovery.ts +32 -11
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +14 -14
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +11 -11
- package/src/dht/routing/Router.ts +5 -5
- package/src/dht/routing/RouterRpcLocal.ts +3 -3
- package/src/dht/routing/RoutingSession.ts +6 -8
- package/src/dht/store/LocalDataStore.ts +25 -33
- package/src/dht/store/StoreManager.ts +25 -29
- package/src/dht/store/StoreRpcLocal.ts +11 -9
- package/src/exports.ts +2 -0
- package/src/helpers/AddressTools.ts +2 -0
- package/src/helpers/PeerID.ts +4 -4
- package/src/helpers/peerIdFromPeerDescriptor.ts +4 -4
- package/src/helpers/versionCompatibility.ts +13 -0
- package/src/identifiers.ts +20 -0
- package/src/proto/packages/dht/protos/DhtRpc.ts +18 -10
- package/test/RandomGraphSimulation.ts +3 -2
- package/test/benchmark/Find.test.ts +4 -3
- package/test/benchmark/KademliaCorrectness.test.ts +4 -4
- package/test/benchmark/SortedContactListBenchmark.test.ts +15 -14
- package/test/benchmark/kademlia-simulation/Contact.ts +7 -8
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +2 -2
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +10 -11
- package/test/data/generateGroundTruthData.ts +5 -4
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +2 -2
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +1 -1
- package/test/end-to-end/memory-leak.test.ts +3 -2
- package/test/integration/ConnectionManager.test.ts +3 -3
- package/test/integration/DhtJoinPeerDiscovery.test.ts +2 -1
- package/test/integration/DhtNodeExternalAPI.test.ts +7 -7
- package/test/integration/DhtNodeRpcRemote.test.ts +3 -2
- package/test/integration/Find.test.ts +3 -3
- package/test/integration/Layer1-scale.test.ts +3 -3
- package/test/integration/Mock-Layer1-Layer0.test.ts +6 -5
- package/test/integration/ReplicateData.test.ts +25 -23
- package/test/integration/RouteMessage.test.ts +9 -10
- package/test/integration/RouterRpcRemote.test.ts +1 -1
- package/test/integration/ScaleDownDht.test.ts +4 -4
- package/test/integration/SimultaneousConnections.test.ts +7 -14
- package/test/integration/Store.test.ts +17 -7
- package/test/integration/StoreAndDelete.test.ts +11 -10
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +7 -6
- package/test/integration/StoreRpcRemote.test.ts +3 -5
- package/test/unit/AddressTools.test.ts +4 -0
- package/test/unit/LocalDataStore.test.ts +37 -29
- package/test/unit/PeerManager.test.ts +7 -8
- package/test/unit/RandomContactList.test.ts +3 -3
- package/test/unit/RecursiveOperationManager.test.ts +2 -1
- package/test/unit/RecursiveOperationSession.test.ts +2 -3
- package/test/unit/Router.test.ts +2 -2
- package/test/unit/RoutingSession.test.ts +2 -2
- package/test/unit/SortedContactList.test.ts +6 -6
- package/test/unit/StoreManager.test.ts +26 -23
- package/test/unit/versionCompatibility.test.ts +16 -0
- package/test/utils/customMatchers.ts +3 -2
- package/test/utils/mock/mockDataEntry.ts +8 -6
- package/test/utils/utils.ts +4 -4
- package/tsconfig.browser.json +2 -1
- package/tsconfig.jest.json +2 -1
- package/tsconfig.node.json +2 -1
- package/dist/src/helpers/nodeId.d.ts +0 -6
- package/dist/src/helpers/nodeId.js +0 -31
- package/dist/src/helpers/nodeId.js.map +0 -1
- package/src/helpers/nodeId.ts +0 -28
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
WebsocketConnectionRequest
|
|
13
13
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
14
14
|
import { WebsocketConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
15
|
-
import { Logger,
|
|
15
|
+
import { Logger, wait } from '@streamr/utils'
|
|
16
16
|
import { ManagedConnection } from '../ManagedConnection'
|
|
17
17
|
import { WebsocketServer } from './WebsocketServer'
|
|
18
18
|
import { sendConnectivityRequest } from '../connectivityChecker'
|
|
@@ -29,7 +29,9 @@ import { AutoCertifierClientFacade } from './AutoCertifierClientFacade'
|
|
|
29
29
|
import { attachConnectivityRequestHandler } from '../connectivityRequestHandler'
|
|
30
30
|
import * as Err from '../../helpers/errors'
|
|
31
31
|
import { Empty } from '../../proto/google/protobuf/empty'
|
|
32
|
-
import {
|
|
32
|
+
import { DhtAddress } from '../../identifiers'
|
|
33
|
+
import { version } from '../../../package.json'
|
|
34
|
+
import { isCompatibleVersion } from '../../helpers/versionCompatibility'
|
|
33
35
|
|
|
34
36
|
const logger = new Logger(module)
|
|
35
37
|
|
|
@@ -61,12 +63,12 @@ export class WebsocketConnector {
|
|
|
61
63
|
private static readonly WEBSOCKET_CONNECTOR_SERVICE_ID = 'system/websocket-connector'
|
|
62
64
|
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
63
65
|
private readonly websocketServer?: WebsocketServer
|
|
64
|
-
private readonly ongoingConnectRequests: Map<
|
|
66
|
+
private readonly ongoingConnectRequests: Map<DhtAddress, ManagedConnection> = new Map()
|
|
65
67
|
private host?: string
|
|
66
68
|
private autoCertifierClient?: AutoCertifierClientFacade
|
|
67
69
|
private selectedPort?: number
|
|
68
70
|
private localPeerDescriptor?: PeerDescriptor
|
|
69
|
-
private connectingConnections: Map<
|
|
71
|
+
private connectingConnections: Map<DhtAddress, ManagedConnection> = new Map()
|
|
70
72
|
private abortController = new AbortController()
|
|
71
73
|
private readonly config: WebsocketConnectorConfig
|
|
72
74
|
|
|
@@ -118,8 +120,8 @@ export class WebsocketConnector {
|
|
|
118
120
|
|
|
119
121
|
private attachHandshaker(connection: IConnection) {
|
|
120
122
|
const handshaker = new Handshaker(this.localPeerDescriptor!, connection)
|
|
121
|
-
handshaker.once('handshakeRequest', (localPeerDescriptor: PeerDescriptor, remotePeerDescriptor?: PeerDescriptor) => {
|
|
122
|
-
this.onServerSocketHandshakeRequest(localPeerDescriptor, connection, remotePeerDescriptor)
|
|
123
|
+
handshaker.once('handshakeRequest', (localPeerDescriptor: PeerDescriptor, sourceVersion: string, remotePeerDescriptor?: PeerDescriptor) => {
|
|
124
|
+
this.onServerSocketHandshakeRequest(localPeerDescriptor, connection, sourceVersion, remotePeerDescriptor)
|
|
123
125
|
})
|
|
124
126
|
}
|
|
125
127
|
|
|
@@ -201,7 +203,7 @@ export class WebsocketConnector {
|
|
|
201
203
|
}
|
|
202
204
|
} catch (err) {
|
|
203
205
|
if (reattempt < ENTRY_POINT_CONNECTION_ATTEMPTS) {
|
|
204
|
-
const error = `Failed to connect to entrypoint with id ${
|
|
206
|
+
const error = `Failed to connect to entrypoint with id ${getNodeIdFromPeerDescriptor(entryPoint)} `
|
|
205
207
|
+ `and URL ${connectivityMethodToWebsocketUrl(entryPoint.websocket!)}`
|
|
206
208
|
logger.error(error, { error: err })
|
|
207
209
|
await wait(2000)
|
|
@@ -262,7 +264,6 @@ export class WebsocketConnector {
|
|
|
262
264
|
const remoteConnector = new WebsocketConnectorRpcRemote(
|
|
263
265
|
localPeerDescriptor,
|
|
264
266
|
targetPeerDescriptor,
|
|
265
|
-
'DUMMY',
|
|
266
267
|
this.rpcCommunicator,
|
|
267
268
|
WebsocketConnectorRpcClient
|
|
268
269
|
)
|
|
@@ -291,13 +292,18 @@ export class WebsocketConnector {
|
|
|
291
292
|
private onServerSocketHandshakeRequest(
|
|
292
293
|
sourcePeerDescriptor: PeerDescriptor,
|
|
293
294
|
serverWebsocket: IConnection,
|
|
295
|
+
sourceVersion: string,
|
|
294
296
|
targetPeerDescriptor?: PeerDescriptor
|
|
295
297
|
) {
|
|
296
298
|
const nodeId = getNodeIdFromPeerDescriptor(sourcePeerDescriptor)
|
|
297
299
|
if (this.ongoingConnectRequests.has(nodeId)) {
|
|
298
300
|
const ongoingConnectRequest = this.ongoingConnectRequests.get(nodeId)!
|
|
299
|
-
|
|
300
|
-
|
|
301
|
+
if (!isCompatibleVersion(sourceVersion, version)) {
|
|
302
|
+
ongoingConnectRequest.rejectHandshake(HandshakeError.UNSUPPORTED_VERSION)
|
|
303
|
+
} else {
|
|
304
|
+
ongoingConnectRequest.attachImplementation(serverWebsocket)
|
|
305
|
+
ongoingConnectRequest.acceptHandshake()
|
|
306
|
+
}
|
|
301
307
|
this.ongoingConnectRequests.delete(nodeId)
|
|
302
308
|
} else {
|
|
303
309
|
const managedConnection = new ManagedConnection(
|
|
@@ -308,7 +314,9 @@ export class WebsocketConnector {
|
|
|
308
314
|
targetPeerDescriptor
|
|
309
315
|
)
|
|
310
316
|
managedConnection.setRemotePeerDescriptor(sourcePeerDescriptor)
|
|
311
|
-
if (
|
|
317
|
+
if (!isCompatibleVersion(sourceVersion, version)) {
|
|
318
|
+
managedConnection.rejectHandshake(HandshakeError.UNSUPPORTED_VERSION)
|
|
319
|
+
} else if (targetPeerDescriptor && !areEqualPeerDescriptors(this.localPeerDescriptor!, targetPeerDescriptor)) {
|
|
312
320
|
managedConnection.rejectHandshake(HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
|
|
313
321
|
} else if (this.config.onNewConnection(managedConnection)) {
|
|
314
322
|
managedConnection.acceptHandshake()
|
package/src/dht/DhtNode.ts
CHANGED
|
@@ -25,7 +25,6 @@ import { ExternalApiRpcClient, StoreRpcClient } from '../proto/packages/dht/prot
|
|
|
25
25
|
import {
|
|
26
26
|
Logger,
|
|
27
27
|
MetricsContext,
|
|
28
|
-
hexToBinary,
|
|
29
28
|
merge,
|
|
30
29
|
waitForCondition
|
|
31
30
|
} from '@streamr/utils'
|
|
@@ -50,7 +49,7 @@ import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
|
50
49
|
import { ExternalApiRpcLocal } from './ExternalApiRpcLocal'
|
|
51
50
|
import { PeerManager } from './PeerManager'
|
|
52
51
|
import { ServiceID } from '../types/ServiceID'
|
|
53
|
-
import {
|
|
52
|
+
import { DhtAddress, DhtAddressRaw, getRawFromDhtAddress } from '../identifiers'
|
|
54
53
|
import { StoreRpcRemote } from './store/StoreRpcRemote'
|
|
55
54
|
|
|
56
55
|
export interface DhtNodeEvents {
|
|
@@ -118,11 +117,11 @@ const logger = new Logger(module)
|
|
|
118
117
|
export type Events = TransportEvents & DhtNodeEvents
|
|
119
118
|
|
|
120
119
|
export const createPeerDescriptor = (msg?: ConnectivityResponse, peerId?: string): PeerDescriptor => {
|
|
121
|
-
let nodeId:
|
|
120
|
+
let nodeId: DhtAddressRaw
|
|
122
121
|
if (msg) {
|
|
123
|
-
nodeId = (peerId !== undefined) ?
|
|
122
|
+
nodeId = (peerId !== undefined) ? getRawFromDhtAddress(peerId as DhtAddress) : PeerID.fromIp(msg.host).value
|
|
124
123
|
} else {
|
|
125
|
-
nodeId =
|
|
124
|
+
nodeId = getRawFromDhtAddress(peerId! as DhtAddress)
|
|
126
125
|
}
|
|
127
126
|
const nodeType = isBrowserEnvironment() ? NodeType.BROWSER : NodeType.NODEJS
|
|
128
127
|
const ret: PeerDescriptor = { nodeId, type: nodeType }
|
|
@@ -155,7 +154,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
155
154
|
joinParallelism: 3,
|
|
156
155
|
maxNeighborListSize: 200,
|
|
157
156
|
numberOfNodesPerKBucket: 8,
|
|
158
|
-
joinNoProgressLimit:
|
|
157
|
+
joinNoProgressLimit: 5,
|
|
159
158
|
dhtJoinTimeout: 60000,
|
|
160
159
|
peerDiscoveryQueryBatchSize: 5,
|
|
161
160
|
maxConnections: 80,
|
|
@@ -276,14 +275,13 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
276
275
|
highestTtl: this.config.storeHighestTtl,
|
|
277
276
|
redundancyFactor: this.config.storageRedundancyFactor,
|
|
278
277
|
localDataStore: this.localDataStore,
|
|
279
|
-
getClosestNeighborsTo: (
|
|
280
|
-
return this.peerManager!.getClosestNeighborsTo(
|
|
278
|
+
getClosestNeighborsTo: (key: DhtAddress, n?: number) => {
|
|
279
|
+
return this.peerManager!.getClosestNeighborsTo(key, n).map((n) => n.getPeerDescriptor())
|
|
281
280
|
},
|
|
282
281
|
createRpcRemote: (contact: PeerDescriptor) => {
|
|
283
282
|
return new StoreRpcRemote(
|
|
284
283
|
this.localPeerDescriptor!,
|
|
285
284
|
contact,
|
|
286
|
-
this.config.serviceId,
|
|
287
285
|
this.rpcCommunicator!,
|
|
288
286
|
StoreRpcClient,
|
|
289
287
|
this.config.rpcRequestTimeout
|
|
@@ -350,8 +348,8 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
350
348
|
}
|
|
351
349
|
const dhtNodeRpcLocal = new DhtNodeRpcLocal({
|
|
352
350
|
peerDiscoveryQueryBatchSize: this.config.peerDiscoveryQueryBatchSize,
|
|
353
|
-
getClosestPeersTo: (
|
|
354
|
-
return this.peerManager!.getClosestNeighborsTo(
|
|
351
|
+
getClosestPeersTo: (nodeId: DhtAddress, limit: number) => {
|
|
352
|
+
return this.peerManager!.getClosestNeighborsTo(nodeId, limit)
|
|
355
353
|
.map((dhtPeer: DhtNodeRpcRemote) => dhtPeer.getPeerDescriptor())
|
|
356
354
|
},
|
|
357
355
|
addNewContact: (contact: PeerDescriptor) => this.peerManager!.handleNewPeers([contact]),
|
|
@@ -362,12 +360,12 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
362
360
|
this.rpcCommunicator!.registerRpcMethod(PingRequest, PingResponse, 'ping',
|
|
363
361
|
(req: PingRequest, context) => dhtNodeRpcLocal.ping(req, context))
|
|
364
362
|
this.rpcCommunicator!.registerRpcNotification(LeaveNotice, 'leaveNotice',
|
|
365
|
-
(
|
|
363
|
+
(_req: LeaveNotice, context) => dhtNodeRpcLocal.leaveNotice(context))
|
|
366
364
|
const externalApiRpcLocal = new ExternalApiRpcLocal({
|
|
367
|
-
executeRecursiveOperation: (key:
|
|
365
|
+
executeRecursiveOperation: (key: DhtAddress, operation: RecursiveOperation, excludedPeer: PeerDescriptor) => {
|
|
368
366
|
return this.executeRecursiveOperation(key, operation, excludedPeer)
|
|
369
367
|
},
|
|
370
|
-
storeDataToDht: (key:
|
|
368
|
+
storeDataToDht: (key: DhtAddress, data: Any, creator?: DhtAddress) => this.storeDataToDht(key, data, creator)
|
|
371
369
|
})
|
|
372
370
|
this.rpcCommunicator!.registerRpcMethod(
|
|
373
371
|
ExternalFindDataRequest,
|
|
@@ -413,7 +411,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
413
411
|
)
|
|
414
412
|
}
|
|
415
413
|
|
|
416
|
-
public getNodeId():
|
|
414
|
+
public getNodeId(): DhtAddress {
|
|
417
415
|
return getNodeIdFromPeerDescriptor(this.localPeerDescriptor!)
|
|
418
416
|
}
|
|
419
417
|
|
|
@@ -432,66 +430,70 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
432
430
|
if (!this.started || this.abortController.signal.aborted) {
|
|
433
431
|
return
|
|
434
432
|
}
|
|
435
|
-
const reachableThrough = this.peerDiscovery!.isJoinOngoing() ? this.
|
|
433
|
+
const reachableThrough = this.peerDiscovery!.isJoinOngoing() ? this.getConnectedEntryPoints() : []
|
|
436
434
|
this.router!.send(msg, reachableThrough)
|
|
437
435
|
}
|
|
438
436
|
|
|
437
|
+
private getConnectedEntryPoints(): PeerDescriptor[] {
|
|
438
|
+
return this.config.entryPoints !== undefined ? this.config.entryPoints.filter((entryPoint) =>
|
|
439
|
+
this.peerManager!.connections.has(getNodeIdFromPeerDescriptor(entryPoint))
|
|
440
|
+
) : []
|
|
441
|
+
}
|
|
442
|
+
|
|
439
443
|
public async joinDht(entryPointDescriptors: PeerDescriptor[], doAdditionalRandomPeerDiscovery?: boolean, retry?: boolean): Promise<void> {
|
|
440
444
|
if (!this.started) {
|
|
441
445
|
throw new Error('Cannot join DHT before calling start() on DhtNode')
|
|
442
446
|
}
|
|
443
|
-
await
|
|
444
|
-
this.peerDiscovery!.joinDht(entryPoint, doAdditionalRandomPeerDiscovery, retry)
|
|
445
|
-
))
|
|
447
|
+
await this.peerDiscovery!.joinDht(entryPointDescriptors, doAdditionalRandomPeerDiscovery, retry)
|
|
446
448
|
}
|
|
447
449
|
|
|
448
450
|
// TODO make this private and unify the public API of find/fetch/store/delete methods
|
|
449
451
|
// (we already have storeDataToDht etc. here)
|
|
450
452
|
public async executeRecursiveOperation(
|
|
451
|
-
key:
|
|
453
|
+
key: DhtAddress,
|
|
452
454
|
operation: RecursiveOperation,
|
|
453
455
|
excludedPeer?: PeerDescriptor
|
|
454
456
|
): Promise<RecursiveOperationResult> {
|
|
455
457
|
return this.recursiveOperationManager!.execute(key, operation, excludedPeer)
|
|
456
458
|
}
|
|
457
459
|
|
|
458
|
-
public async storeDataToDht(key:
|
|
459
|
-
|
|
460
|
-
|
|
460
|
+
public async storeDataToDht(key: DhtAddress, data: Any, creator?: DhtAddress): Promise<PeerDescriptor[]> {
|
|
461
|
+
const connectedEntryPoints = this.getConnectedEntryPoints()
|
|
462
|
+
if (this.peerDiscovery!.isJoinOngoing() && connectedEntryPoints.length > 0) {
|
|
463
|
+
return this.storeDataViaPeer(key, data, sample(connectedEntryPoints)!)
|
|
461
464
|
}
|
|
462
465
|
return this.storeManager!.storeDataToDht(key, data, creator ?? getNodeIdFromPeerDescriptor(this.localPeerDescriptor!))
|
|
463
466
|
}
|
|
464
467
|
|
|
465
|
-
public async storeDataViaPeer(key:
|
|
468
|
+
public async storeDataViaPeer(key: DhtAddress, data: Any, peer: PeerDescriptor): Promise<PeerDescriptor[]> {
|
|
466
469
|
const rpcRemote = new ExternalApiRpcRemote(
|
|
467
470
|
this.localPeerDescriptor!,
|
|
468
471
|
peer,
|
|
469
|
-
this.config.serviceId,
|
|
470
472
|
this.rpcCommunicator!,
|
|
471
473
|
ExternalApiRpcClient
|
|
472
474
|
)
|
|
473
475
|
return await rpcRemote.storeData(key, data)
|
|
474
476
|
}
|
|
475
477
|
|
|
476
|
-
public async getDataFromDht(key:
|
|
477
|
-
|
|
478
|
-
|
|
478
|
+
public async getDataFromDht(key: DhtAddress): Promise<DataEntry[]> {
|
|
479
|
+
const connectedEntryPoints = this.getConnectedEntryPoints()
|
|
480
|
+
if (this.peerDiscovery!.isJoinOngoing() && connectedEntryPoints.length > 0) {
|
|
481
|
+
return this.findDataViaPeer(key, sample(connectedEntryPoints)!)
|
|
479
482
|
}
|
|
480
483
|
const result = await this.recursiveOperationManager!.execute(key, RecursiveOperation.FETCH_DATA)
|
|
481
484
|
return result.dataEntries ?? [] // TODO is this fallback needed?
|
|
482
485
|
}
|
|
483
486
|
|
|
484
|
-
public async deleteDataFromDht(key:
|
|
487
|
+
public async deleteDataFromDht(key: DhtAddress, waitForCompletion: boolean): Promise<void> {
|
|
485
488
|
if (!this.abortController.signal.aborted) {
|
|
486
489
|
await this.recursiveOperationManager!.execute(key, RecursiveOperation.DELETE_DATA, undefined, waitForCompletion)
|
|
487
490
|
}
|
|
488
491
|
}
|
|
489
492
|
|
|
490
|
-
public async findDataViaPeer(key:
|
|
493
|
+
public async findDataViaPeer(key: DhtAddress, peer: PeerDescriptor): Promise<DataEntry[]> {
|
|
491
494
|
const rpcRemote = new ExternalApiRpcRemote(
|
|
492
495
|
this.localPeerDescriptor!,
|
|
493
496
|
peer,
|
|
494
|
-
this.config.serviceId,
|
|
495
497
|
this.rpcCommunicator!,
|
|
496
498
|
ExternalApiRpcClient
|
|
497
499
|
)
|
|
@@ -5,17 +5,17 @@ import { Empty } from '../proto/google/protobuf/empty'
|
|
|
5
5
|
import {
|
|
6
6
|
ClosestPeersRequest,
|
|
7
7
|
ClosestPeersResponse,
|
|
8
|
-
LeaveNotice,
|
|
9
8
|
PeerDescriptor,
|
|
10
9
|
PingRequest,
|
|
11
10
|
PingResponse
|
|
12
11
|
} from '../proto/packages/dht/protos/DhtRpc'
|
|
13
12
|
import { IDhtNodeRpc } from '../proto/packages/dht/protos/DhtRpc.server'
|
|
14
13
|
import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
|
|
14
|
+
import { DhtAddress, getDhtAddressFromRaw } from '../identifiers'
|
|
15
15
|
|
|
16
16
|
interface DhtNodeRpcLocalConfig {
|
|
17
17
|
peerDiscoveryQueryBatchSize: number
|
|
18
|
-
getClosestPeersTo: (nodeId:
|
|
18
|
+
getClosestPeersTo: (nodeId: DhtAddress, limit: number) => PeerDescriptor[]
|
|
19
19
|
addNewContact: (contact: PeerDescriptor) => void
|
|
20
20
|
removeContact: (contact: PeerDescriptor) => void
|
|
21
21
|
}
|
|
@@ -33,7 +33,7 @@ export class DhtNodeRpcLocal implements IDhtNodeRpc {
|
|
|
33
33
|
async getClosestPeers(request: ClosestPeersRequest, context: ServerCallContext): Promise<ClosestPeersResponse> {
|
|
34
34
|
this.config.addNewContact((context as DhtCallContext).incomingSourceDescriptor!)
|
|
35
35
|
const response = {
|
|
36
|
-
peers: this.config.getClosestPeersTo(request.nodeId, this.config.peerDiscoveryQueryBatchSize),
|
|
36
|
+
peers: this.config.getClosestPeersTo(getDhtAddressFromRaw(request.nodeId), this.config.peerDiscoveryQueryBatchSize),
|
|
37
37
|
requestId: request.requestId
|
|
38
38
|
}
|
|
39
39
|
return response
|
|
@@ -50,7 +50,7 @@ export class DhtNodeRpcLocal implements IDhtNodeRpc {
|
|
|
50
50
|
return response
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
async leaveNotice(
|
|
53
|
+
async leaveNotice(context: ServerCallContext): Promise<Empty> {
|
|
54
54
|
// TODO check signature??
|
|
55
55
|
const sender = (context as DhtCallContext).incomingSourceDescriptor!
|
|
56
56
|
logger.trace('received leave notice: ' + getNodeIdFromPeerDescriptor(sender))
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { RpcCommunicator } from '@streamr/proto-rpc'
|
|
2
2
|
import { Logger } from '@streamr/utils'
|
|
3
3
|
import { v4 } from 'uuid'
|
|
4
|
-
import {
|
|
4
|
+
import { DhtAddress, DhtAddressRaw, getRawFromDhtAddress } from '../identifiers'
|
|
5
5
|
import { getNodeIdFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
6
6
|
import {
|
|
7
7
|
ClosestPeersRequest,
|
|
8
|
-
LeaveNotice,
|
|
9
8
|
PeerDescriptor,
|
|
10
9
|
PingRequest
|
|
11
10
|
} from '../proto/packages/dht/protos/DhtRpc'
|
|
@@ -17,7 +16,7 @@ const logger = new Logger(module)
|
|
|
17
16
|
|
|
18
17
|
// Fields required by objects stored in the k-bucket library
|
|
19
18
|
export interface KBucketContact {
|
|
20
|
-
id:
|
|
19
|
+
id: DhtAddressRaw
|
|
21
20
|
vectorClock: number
|
|
22
21
|
}
|
|
23
22
|
|
|
@@ -25,7 +24,8 @@ export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBu
|
|
|
25
24
|
|
|
26
25
|
private static counter = 0
|
|
27
26
|
public vectorClock: number
|
|
28
|
-
public readonly id:
|
|
27
|
+
public readonly id: DhtAddressRaw
|
|
28
|
+
private readonly serviceId: ServiceID
|
|
29
29
|
|
|
30
30
|
constructor(
|
|
31
31
|
localPeerDescriptor: PeerDescriptor,
|
|
@@ -34,28 +34,29 @@ export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBu
|
|
|
34
34
|
rpcCommunicator: RpcCommunicator,
|
|
35
35
|
rpcRequestTimeout?: number
|
|
36
36
|
) {
|
|
37
|
-
super(localPeerDescriptor, peerDescriptor,
|
|
37
|
+
super(localPeerDescriptor, peerDescriptor, rpcCommunicator, DhtNodeRpcClient, rpcRequestTimeout)
|
|
38
38
|
this.id = this.getPeerDescriptor().nodeId
|
|
39
39
|
this.vectorClock = DhtNodeRpcRemote.counter++
|
|
40
|
+
this.serviceId = serviceId
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
async getClosestPeers(nodeId:
|
|
43
|
-
logger.trace(`Requesting getClosestPeers on ${this.
|
|
43
|
+
async getClosestPeers(nodeId: DhtAddress): Promise<PeerDescriptor[]> {
|
|
44
|
+
logger.trace(`Requesting getClosestPeers on ${this.serviceId} from ${getNodeIdFromPeerDescriptor(this.getPeerDescriptor())}`)
|
|
44
45
|
const request: ClosestPeersRequest = {
|
|
45
|
-
nodeId,
|
|
46
|
+
nodeId: getRawFromDhtAddress(nodeId),
|
|
46
47
|
requestId: v4()
|
|
47
48
|
}
|
|
48
49
|
try {
|
|
49
50
|
const peers = await this.getClient().getClosestPeers(request, this.formDhtRpcOptions())
|
|
50
51
|
return peers.peers
|
|
51
52
|
} catch (err) {
|
|
52
|
-
logger.trace(`getClosestPeers error ${this.
|
|
53
|
+
logger.trace(`getClosestPeers error ${this.serviceId}`, { err })
|
|
53
54
|
throw err
|
|
54
55
|
}
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
async ping(): Promise<boolean> {
|
|
58
|
-
logger.trace(`Requesting ping on ${this.
|
|
59
|
+
logger.trace(`Requesting ping on ${this.serviceId} from ${getNodeIdFromPeerDescriptor(this.getPeerDescriptor())}`)
|
|
59
60
|
const request: PingRequest = {
|
|
60
61
|
requestId: v4()
|
|
61
62
|
}
|
|
@@ -66,25 +67,22 @@ export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBu
|
|
|
66
67
|
return true
|
|
67
68
|
}
|
|
68
69
|
} catch (err) {
|
|
69
|
-
logger.trace(`ping failed on ${this.
|
|
70
|
+
logger.trace(`ping failed on ${this.serviceId} to ${getNodeIdFromPeerDescriptor(this.getPeerDescriptor())}: ${err}`)
|
|
70
71
|
}
|
|
71
72
|
return false
|
|
72
73
|
}
|
|
73
74
|
|
|
74
75
|
leaveNotice(): void {
|
|
75
|
-
logger.trace(`Sending leaveNotice on ${this.
|
|
76
|
-
const request: LeaveNotice = {
|
|
77
|
-
serviceId: this.getServiceId()
|
|
78
|
-
}
|
|
76
|
+
logger.trace(`Sending leaveNotice on ${this.serviceId} from ${getNodeIdFromPeerDescriptor(this.getPeerDescriptor())}`)
|
|
79
77
|
const options = this.formDhtRpcOptions({
|
|
80
78
|
notification: true
|
|
81
79
|
})
|
|
82
|
-
this.getClient().leaveNotice(
|
|
80
|
+
this.getClient().leaveNotice({}, options).catch((e) => {
|
|
83
81
|
logger.trace('Failed to send leaveNotice' + e)
|
|
84
82
|
})
|
|
85
83
|
}
|
|
86
84
|
|
|
87
|
-
getNodeId():
|
|
85
|
+
getNodeId(): DhtAddress {
|
|
88
86
|
return getNodeIdFromPeerDescriptor(this.getPeerDescriptor())
|
|
89
87
|
}
|
|
90
88
|
}
|
|
@@ -11,19 +11,20 @@ import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
|
11
11
|
import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
|
|
12
12
|
import { RecursiveOperationResult } from './recursive-operation/RecursiveOperationManager'
|
|
13
13
|
import { Any } from '../proto/google/protobuf/any'
|
|
14
|
-
import {
|
|
14
|
+
import { DhtAddress } from '../identifiers'
|
|
15
15
|
import { getNodeIdFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
16
|
+
import { getDhtAddressFromRaw } from '../identifiers'
|
|
16
17
|
|
|
17
18
|
interface ExternalApiRpcLocalConfig {
|
|
18
19
|
executeRecursiveOperation: (
|
|
19
|
-
targetId:
|
|
20
|
+
targetId: DhtAddress,
|
|
20
21
|
operation: RecursiveOperation,
|
|
21
22
|
excludedPeer: PeerDescriptor
|
|
22
23
|
) => Promise<RecursiveOperationResult>
|
|
23
24
|
storeDataToDht: (
|
|
24
|
-
key:
|
|
25
|
+
key: DhtAddress,
|
|
25
26
|
data: Any,
|
|
26
|
-
creator:
|
|
27
|
+
creator: DhtAddress
|
|
27
28
|
) => Promise<PeerDescriptor[]>
|
|
28
29
|
}
|
|
29
30
|
|
|
@@ -37,13 +38,21 @@ export class ExternalApiRpcLocal implements IExternalApiRpc {
|
|
|
37
38
|
|
|
38
39
|
async externalFindData(findDataRequest: ExternalFindDataRequest, context: ServerCallContext): Promise<ExternalFindDataResponse> {
|
|
39
40
|
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
40
|
-
const result = await this.config.executeRecursiveOperation(
|
|
41
|
+
const result = await this.config.executeRecursiveOperation(
|
|
42
|
+
getDhtAddressFromRaw(findDataRequest.key),
|
|
43
|
+
RecursiveOperation.FETCH_DATA,
|
|
44
|
+
senderPeerDescriptor
|
|
45
|
+
)
|
|
41
46
|
return ExternalFindDataResponse.create({ entries: result.dataEntries ?? [] })
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
async externalStoreData(request: ExternalStoreDataRequest, context: ServerCallContext): Promise<ExternalStoreDataResponse> {
|
|
45
50
|
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
46
|
-
const result = await this.config.storeDataToDht(
|
|
51
|
+
const result = await this.config.storeDataToDht(
|
|
52
|
+
getDhtAddressFromRaw(request.key),
|
|
53
|
+
request.data!,
|
|
54
|
+
getNodeIdFromPeerDescriptor(senderPeerDescriptor)
|
|
55
|
+
)
|
|
47
56
|
return ExternalStoreDataResponse.create({
|
|
48
57
|
storers: result
|
|
49
58
|
})
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DhtAddress, getRawFromDhtAddress } from '../identifiers'
|
|
1
2
|
import { Any } from '../proto/google/protobuf/any'
|
|
2
3
|
import { DataEntry, ExternalFindDataRequest, ExternalStoreDataRequest, PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
|
|
3
4
|
import { ExternalApiRpcClient } from '../proto/packages/dht/protos/DhtRpc.client'
|
|
@@ -5,9 +6,9 @@ import { RpcRemote } from './contact/RpcRemote'
|
|
|
5
6
|
|
|
6
7
|
export class ExternalApiRpcRemote extends RpcRemote<ExternalApiRpcClient> {
|
|
7
8
|
|
|
8
|
-
async externalFindData(key:
|
|
9
|
+
async externalFindData(key: DhtAddress): Promise<DataEntry[]> {
|
|
9
10
|
const request: ExternalFindDataRequest = {
|
|
10
|
-
key
|
|
11
|
+
key: getRawFromDhtAddress(key)
|
|
11
12
|
}
|
|
12
13
|
const options = this.formDhtRpcOptions({
|
|
13
14
|
// TODO use config option or named constant?
|
|
@@ -21,9 +22,9 @@ export class ExternalApiRpcRemote extends RpcRemote<ExternalApiRpcClient> {
|
|
|
21
22
|
}
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
async storeData(key:
|
|
25
|
+
async storeData(key: DhtAddress, data: Any): Promise<PeerDescriptor[]> {
|
|
25
26
|
const request: ExternalStoreDataRequest = {
|
|
26
|
-
key,
|
|
27
|
+
key: getRawFromDhtAddress(key),
|
|
27
28
|
data
|
|
28
29
|
}
|
|
29
30
|
const options = this.formDhtRpcOptions({
|
package/src/dht/PeerManager.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
Logger
|
|
2
|
+
Logger
|
|
3
3
|
} from '@streamr/utils'
|
|
4
4
|
import KBucket from 'k-bucket'
|
|
5
5
|
import {
|
|
@@ -13,7 +13,7 @@ import { RandomContactList } from './contact/RandomContactList'
|
|
|
13
13
|
import { SortedContactList } from './contact/SortedContactList'
|
|
14
14
|
import { ConnectionManager } from '../connection/ConnectionManager'
|
|
15
15
|
import EventEmitter from 'eventemitter3'
|
|
16
|
-
import {
|
|
16
|
+
import { DhtAddress, DhtAddressRaw, getRawFromDhtAddress } from '../identifiers'
|
|
17
17
|
|
|
18
18
|
const logger = new Logger(module)
|
|
19
19
|
|
|
@@ -21,7 +21,7 @@ interface PeerManagerConfig {
|
|
|
21
21
|
numberOfNodesPerKBucket: number
|
|
22
22
|
maxContactListSize: number
|
|
23
23
|
peerDiscoveryQueryBatchSize: number
|
|
24
|
-
localNodeId:
|
|
24
|
+
localNodeId: DhtAddress
|
|
25
25
|
connectionManager: ConnectionManager
|
|
26
26
|
isLayer0: boolean
|
|
27
27
|
createDhtNodeRpcRemote: (peerDescriptor: PeerDescriptor) => DhtNodeRpcRemote
|
|
@@ -35,8 +35,8 @@ export interface PeerManagerEvents {
|
|
|
35
35
|
kBucketEmpty: () => void
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
export const getDistance = (
|
|
39
|
-
return KBucket.distance(
|
|
38
|
+
export const getDistance = (nodeIdOrDataKeyRaw1: DhtAddressRaw, nodeIdOrDataKeyRaw2: DhtAddressRaw): number => {
|
|
39
|
+
return KBucket.distance(nodeIdOrDataKeyRaw1, nodeIdOrDataKeyRaw2)
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
@@ -50,7 +50,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
50
50
|
// The kademlia k-bucket
|
|
51
51
|
private bucket: KBucket<DhtNodeRpcRemote>
|
|
52
52
|
// Nodes that are connected to this node on Layer0
|
|
53
|
-
public readonly connections: Map<
|
|
53
|
+
public readonly connections: Map<DhtAddress, DhtNodeRpcRemote> = new Map()
|
|
54
54
|
// All nodes that we know about
|
|
55
55
|
private contacts: SortedContactList<DhtNodeRpcRemote>
|
|
56
56
|
private randomPeers: RandomContactList<DhtNodeRpcRemote>
|
|
@@ -61,7 +61,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
61
61
|
super()
|
|
62
62
|
this.config = config
|
|
63
63
|
this.bucket = new KBucket<DhtNodeRpcRemote>({
|
|
64
|
-
localNodeId:
|
|
64
|
+
localNodeId: getRawFromDhtAddress(this.config.localNodeId),
|
|
65
65
|
numberOfNodesPerKBucket: this.config.numberOfNodesPerKBucket,
|
|
66
66
|
numberOfNodesToPing: this.config.numberOfNodesPerKBucket
|
|
67
67
|
})
|
|
@@ -109,7 +109,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
109
109
|
sortingList.addContacts(oldContacts)
|
|
110
110
|
const sortedContacts = sortingList.getAllContacts()
|
|
111
111
|
this.config.connectionManager?.weakUnlockConnection(sortedContacts[sortedContacts.length - 1].getPeerDescriptor())
|
|
112
|
-
this.bucket.remove(
|
|
112
|
+
this.bucket.remove(getRawFromDhtAddress(sortedContacts[sortedContacts.length - 1].getNodeId()))
|
|
113
113
|
this.bucket.add(newContact)
|
|
114
114
|
}
|
|
115
115
|
|
|
@@ -128,7 +128,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
128
128
|
if (this.stopped) {
|
|
129
129
|
return
|
|
130
130
|
}
|
|
131
|
-
if (
|
|
131
|
+
if (contact.getNodeId() !== this.config.localNodeId) {
|
|
132
132
|
// Important to lock here, before the ping result is known
|
|
133
133
|
this.config.connectionManager?.weakLockConnection(contact.getPeerDescriptor())
|
|
134
134
|
if (this.connections.has(contact.getNodeId())) {
|
|
@@ -166,7 +166,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
166
166
|
|
|
167
167
|
private getClosestActiveContactNotInBucket(): DhtNodeRpcRemote | undefined {
|
|
168
168
|
for (const contactId of this.contacts.getContactIds()) {
|
|
169
|
-
if (!this.bucket.get(
|
|
169
|
+
if (!this.bucket.get(getRawFromDhtAddress(contactId)) && this.contacts.isActive(contactId)) {
|
|
170
170
|
return this.contacts.getContact(contactId)!.contact
|
|
171
171
|
}
|
|
172
172
|
}
|
|
@@ -175,7 +175,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
175
175
|
|
|
176
176
|
handleConnected(peerDescriptor: PeerDescriptor): void {
|
|
177
177
|
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
178
|
-
if (
|
|
178
|
+
if (nodeId === this.config.localNodeId) {
|
|
179
179
|
logger.error('handleConnected() to self')
|
|
180
180
|
}
|
|
181
181
|
const rpcRemote = this.config.createDhtNodeRpcRemote(peerDescriptor)
|
|
@@ -212,7 +212,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
212
212
|
}
|
|
213
213
|
logger.trace(`Removing contact ${getNodeIdFromPeerDescriptor(contact)}`)
|
|
214
214
|
const nodeId = getNodeIdFromPeerDescriptor(contact)
|
|
215
|
-
this.bucket.remove(
|
|
215
|
+
this.bucket.remove(getRawFromDhtAddress(nodeId))
|
|
216
216
|
this.contacts.removeContact(nodeId)
|
|
217
217
|
this.randomPeers.removeContact(nodeId)
|
|
218
218
|
}
|
|
@@ -229,19 +229,19 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
229
229
|
this.connections.clear()
|
|
230
230
|
}
|
|
231
231
|
|
|
232
|
-
getClosestNeighborsTo(referenceId:
|
|
232
|
+
getClosestNeighborsTo(referenceId: DhtAddress, limit?: number, excludedNodeIds?: Set<DhtAddress>): DhtNodeRpcRemote[] {
|
|
233
233
|
const closest = new SortedContactList<DhtNodeRpcRemote>({
|
|
234
234
|
referenceId,
|
|
235
235
|
allowToContainReferenceId: true,
|
|
236
236
|
emitEvents: false,
|
|
237
237
|
excludedNodeIds
|
|
238
238
|
})
|
|
239
|
-
this.bucket.toArray().
|
|
239
|
+
this.bucket.toArray().forEach((contact) => closest.addContact(contact))
|
|
240
240
|
return closest.getClosestContacts(limit)
|
|
241
241
|
}
|
|
242
242
|
|
|
243
243
|
// TODO reduce copy-paste?
|
|
244
|
-
getClosestContactsTo(referenceId:
|
|
244
|
+
getClosestContactsTo(referenceId: DhtAddress, limit?: number, excludedNodeIds?: Set<DhtAddress>): DhtNodeRpcRemote[] {
|
|
245
245
|
const closest = new SortedContactList<DhtNodeRpcRemote>({
|
|
246
246
|
referenceId,
|
|
247
247
|
allowToContainReferenceId: true,
|
|
@@ -253,7 +253,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
253
253
|
return closest.getClosestContacts(limit)
|
|
254
254
|
}
|
|
255
255
|
|
|
256
|
-
getNumberOfContacts(excludedNodeIds?: Set<
|
|
256
|
+
getNumberOfContacts(excludedNodeIds?: Set<DhtAddress>): number {
|
|
257
257
|
return this.contacts.getAllContacts().filter((contact) => {
|
|
258
258
|
if (!excludedNodeIds) {
|
|
259
259
|
return true
|
|
@@ -275,12 +275,12 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
275
275
|
return this.bucket.toArray().map((rpcRemote: DhtNodeRpcRemote) => rpcRemote.getPeerDescriptor())
|
|
276
276
|
}
|
|
277
277
|
|
|
278
|
-
handlePeerActive(nodeId:
|
|
278
|
+
handlePeerActive(nodeId: DhtAddress): void {
|
|
279
279
|
this.contacts.setActive(nodeId)
|
|
280
280
|
}
|
|
281
281
|
|
|
282
|
-
handlePeerUnresponsive(nodeId:
|
|
283
|
-
this.bucket.remove(
|
|
282
|
+
handlePeerUnresponsive(nodeId: DhtAddress): void {
|
|
283
|
+
this.bucket.remove(getRawFromDhtAddress(nodeId))
|
|
284
284
|
this.contacts.removeContact(nodeId)
|
|
285
285
|
}
|
|
286
286
|
|
|
@@ -290,7 +290,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
290
290
|
}
|
|
291
291
|
peerDescriptors.forEach((contact) => {
|
|
292
292
|
const nodeId = getNodeIdFromPeerDescriptor(contact)
|
|
293
|
-
if (
|
|
293
|
+
if (nodeId !== this.config.localNodeId) {
|
|
294
294
|
logger.trace(`Adding new contact ${nodeId}`)
|
|
295
295
|
const remote = this.config.createDhtNodeRpcRemote(contact)
|
|
296
296
|
const isInBucket = (this.bucket.get(contact.nodeId) !== null)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
2
2
|
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
3
|
-
import {
|
|
3
|
+
import { DhtAddress } from '../../identifiers'
|
|
4
4
|
|
|
5
5
|
export class Contact {
|
|
6
6
|
|
|
@@ -14,7 +14,7 @@ export class Contact {
|
|
|
14
14
|
return this.peerDescriptor
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
public getNodeId():
|
|
17
|
+
public getNodeId(): DhtAddress {
|
|
18
18
|
return getNodeIdFromPeerDescriptor(this.peerDescriptor)
|
|
19
19
|
}
|
|
20
20
|
}
|