@streamr/dht 100.0.0-testnet-two.1 → 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 +5 -5
- 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/ManagedConnection.d.ts +2 -2
- 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 +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.js +2 -2
- 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 +21 -16
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +2 -1
- package/dist/src/dht/DhtNodeRpcLocal.js +2 -1
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +6 -5
- package/dist/src/dht/DhtNodeRpcRemote.js +9 -7
- 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 -9
- 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/identifiers.d.ts +6 -0
- package/dist/src/identifiers.js +23 -0
- package/dist/src/identifiers.js.map +1 -0
- package/package.json +5 -5
- package/src/connection/ConnectionLockHandler.ts +15 -15
- package/src/connection/ConnectionLockRpcLocal.ts +3 -3
- package/src/connection/ConnectionManager.ts +4 -7
- package/src/connection/ManagedConnection.ts +2 -2
- 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 +2 -3
- package/src/connection/websocket/WebsocketConnector.ts +5 -6
- package/src/dht/DhtNode.ts +31 -29
- package/src/dht/DhtNodeRpcLocal.ts +3 -2
- package/src/dht/DhtNodeRpcRemote.ts +14 -12
- 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 -14
- 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/identifiers.ts +20 -0
- 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/utils/customMatchers.ts +3 -2
- package/test/utils/mock/mockDataEntry.ts +8 -6
- package/test/utils/utils.ts +4 -4
- 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
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 }
|
|
@@ -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]),
|
|
@@ -364,10 +362,10 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
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
|
)
|
|
@@ -11,10 +11,11 @@ import {
|
|
|
11
11
|
} from '../proto/packages/dht/protos/DhtRpc'
|
|
12
12
|
import { IDhtNodeRpc } from '../proto/packages/dht/protos/DhtRpc.server'
|
|
13
13
|
import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
|
|
14
|
+
import { DhtAddress, getDhtAddressFromRaw } from '../identifiers'
|
|
14
15
|
|
|
15
16
|
interface DhtNodeRpcLocalConfig {
|
|
16
17
|
peerDiscoveryQueryBatchSize: number
|
|
17
|
-
getClosestPeersTo: (nodeId:
|
|
18
|
+
getClosestPeersTo: (nodeId: DhtAddress, limit: number) => PeerDescriptor[]
|
|
18
19
|
addNewContact: (contact: PeerDescriptor) => void
|
|
19
20
|
removeContact: (contact: PeerDescriptor) => void
|
|
20
21
|
}
|
|
@@ -32,7 +33,7 @@ export class DhtNodeRpcLocal implements IDhtNodeRpc {
|
|
|
32
33
|
async getClosestPeers(request: ClosestPeersRequest, context: ServerCallContext): Promise<ClosestPeersResponse> {
|
|
33
34
|
this.config.addNewContact((context as DhtCallContext).incomingSourceDescriptor!)
|
|
34
35
|
const response = {
|
|
35
|
-
peers: this.config.getClosestPeersTo(request.nodeId, this.config.peerDiscoveryQueryBatchSize),
|
|
36
|
+
peers: this.config.getClosestPeersTo(getDhtAddressFromRaw(request.nodeId), this.config.peerDiscoveryQueryBatchSize),
|
|
36
37
|
requestId: request.requestId
|
|
37
38
|
}
|
|
38
39
|
return response
|
|
@@ -1,7 +1,7 @@
|
|
|
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,
|
|
@@ -16,7 +16,7 @@ const logger = new Logger(module)
|
|
|
16
16
|
|
|
17
17
|
// Fields required by objects stored in the k-bucket library
|
|
18
18
|
export interface KBucketContact {
|
|
19
|
-
id:
|
|
19
|
+
id: DhtAddressRaw
|
|
20
20
|
vectorClock: number
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -24,7 +24,8 @@ export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBu
|
|
|
24
24
|
|
|
25
25
|
private static counter = 0
|
|
26
26
|
public vectorClock: number
|
|
27
|
-
public readonly id:
|
|
27
|
+
public readonly id: DhtAddressRaw
|
|
28
|
+
private readonly serviceId: ServiceID
|
|
28
29
|
|
|
29
30
|
constructor(
|
|
30
31
|
localPeerDescriptor: PeerDescriptor,
|
|
@@ -33,28 +34,29 @@ export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBu
|
|
|
33
34
|
rpcCommunicator: RpcCommunicator,
|
|
34
35
|
rpcRequestTimeout?: number
|
|
35
36
|
) {
|
|
36
|
-
super(localPeerDescriptor, peerDescriptor,
|
|
37
|
+
super(localPeerDescriptor, peerDescriptor, rpcCommunicator, DhtNodeRpcClient, rpcRequestTimeout)
|
|
37
38
|
this.id = this.getPeerDescriptor().nodeId
|
|
38
39
|
this.vectorClock = DhtNodeRpcRemote.counter++
|
|
40
|
+
this.serviceId = serviceId
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
async getClosestPeers(nodeId:
|
|
42
|
-
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())}`)
|
|
43
45
|
const request: ClosestPeersRequest = {
|
|
44
|
-
nodeId,
|
|
46
|
+
nodeId: getRawFromDhtAddress(nodeId),
|
|
45
47
|
requestId: v4()
|
|
46
48
|
}
|
|
47
49
|
try {
|
|
48
50
|
const peers = await this.getClient().getClosestPeers(request, this.formDhtRpcOptions())
|
|
49
51
|
return peers.peers
|
|
50
52
|
} catch (err) {
|
|
51
|
-
logger.trace(`getClosestPeers error ${this.
|
|
53
|
+
logger.trace(`getClosestPeers error ${this.serviceId}`, { err })
|
|
52
54
|
throw err
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
async ping(): Promise<boolean> {
|
|
57
|
-
logger.trace(`Requesting ping on ${this.
|
|
59
|
+
logger.trace(`Requesting ping on ${this.serviceId} from ${getNodeIdFromPeerDescriptor(this.getPeerDescriptor())}`)
|
|
58
60
|
const request: PingRequest = {
|
|
59
61
|
requestId: v4()
|
|
60
62
|
}
|
|
@@ -65,13 +67,13 @@ export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBu
|
|
|
65
67
|
return true
|
|
66
68
|
}
|
|
67
69
|
} catch (err) {
|
|
68
|
-
logger.trace(`ping failed on ${this.
|
|
70
|
+
logger.trace(`ping failed on ${this.serviceId} to ${getNodeIdFromPeerDescriptor(this.getPeerDescriptor())}: ${err}`)
|
|
69
71
|
}
|
|
70
72
|
return false
|
|
71
73
|
}
|
|
72
74
|
|
|
73
75
|
leaveNotice(): void {
|
|
74
|
-
logger.trace(`Sending leaveNotice on ${this.
|
|
76
|
+
logger.trace(`Sending leaveNotice on ${this.serviceId} from ${getNodeIdFromPeerDescriptor(this.getPeerDescriptor())}`)
|
|
75
77
|
const options = this.formDhtRpcOptions({
|
|
76
78
|
notification: true
|
|
77
79
|
})
|
|
@@ -80,7 +82,7 @@ export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBu
|
|
|
80
82
|
})
|
|
81
83
|
}
|
|
82
84
|
|
|
83
|
-
getNodeId():
|
|
85
|
+
getNodeId(): DhtAddress {
|
|
84
86
|
return getNodeIdFromPeerDescriptor(this.getPeerDescriptor())
|
|
85
87
|
}
|
|
86
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
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import EventEmitter from 'eventemitter3'
|
|
2
|
-
import {
|
|
2
|
+
import { DhtAddress } from '../../identifiers'
|
|
3
3
|
|
|
4
4
|
export class ContactState<C> {
|
|
5
5
|
public contacted = false
|
|
@@ -16,17 +16,17 @@ export interface Events<C> {
|
|
|
16
16
|
newContact: (newContact: C, closestContacts: C[]) => void
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export class ContactList<C extends { getNodeId: () =>
|
|
19
|
+
export class ContactList<C extends { getNodeId: () => DhtAddress }> extends EventEmitter<Events<C>> {
|
|
20
20
|
|
|
21
|
-
protected contactsById: Map<
|
|
21
|
+
protected contactsById: Map<DhtAddress, ContactState<C>> = new Map()
|
|
22
22
|
// TODO move this to SortedContactList
|
|
23
|
-
protected contactIds:
|
|
24
|
-
protected localNodeId:
|
|
23
|
+
protected contactIds: DhtAddress[] = []
|
|
24
|
+
protected localNodeId: DhtAddress
|
|
25
25
|
protected maxSize: number
|
|
26
26
|
protected defaultContactQueryLimit
|
|
27
27
|
|
|
28
28
|
constructor(
|
|
29
|
-
localNodeId:
|
|
29
|
+
localNodeId: DhtAddress,
|
|
30
30
|
maxSize: number,
|
|
31
31
|
defaultContactQueryLimit = 20
|
|
32
32
|
) {
|
|
@@ -36,7 +36,7 @@ export class ContactList<C extends { getNodeId: () => NodeID }> extends EventEmi
|
|
|
36
36
|
this.defaultContactQueryLimit = defaultContactQueryLimit
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
public getContact(id:
|
|
39
|
+
public getContact(id: DhtAddress): ContactState<C> | undefined {
|
|
40
40
|
return this.contactsById.get(id)
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DhtAddress } from '../../identifiers'
|
|
2
2
|
import { ContactList, ContactState } from './ContactList'
|
|
3
3
|
|
|
4
|
-
export class RandomContactList<C extends { getNodeId: () =>
|
|
4
|
+
export class RandomContactList<C extends { getNodeId: () => DhtAddress }> extends ContactList<C> {
|
|
5
5
|
|
|
6
6
|
private randomness: number
|
|
7
7
|
|
|
8
8
|
constructor(
|
|
9
|
-
localNodeId:
|
|
9
|
+
localNodeId: DhtAddress,
|
|
10
10
|
maxSize: number,
|
|
11
11
|
randomness = 0.20,
|
|
12
12
|
defaultContactQueryLimit?: number
|
|
@@ -16,7 +16,7 @@ export class RandomContactList<C extends { getNodeId: () => NodeID }> extends Co
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
addContact(contact: C): void {
|
|
19
|
-
if (
|
|
19
|
+
if (this.localNodeId === contact.getNodeId()) {
|
|
20
20
|
return
|
|
21
21
|
}
|
|
22
22
|
if (!this.contactsById.has(contact.getNodeId())) {
|
|
@@ -37,10 +37,10 @@ export class RandomContactList<C extends { getNodeId: () => NodeID }> extends Co
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
removeContact(id:
|
|
40
|
+
removeContact(id: DhtAddress): boolean {
|
|
41
41
|
if (this.contactsById.has(id)) {
|
|
42
42
|
const removed = this.contactsById.get(id)!.contact
|
|
43
|
-
const index = this.contactIds.findIndex((nodeId) =>
|
|
43
|
+
const index = this.contactIds.findIndex((nodeId) => (nodeId === id))
|
|
44
44
|
this.contactIds.splice(index, 1)
|
|
45
45
|
this.contactsById.delete(id)
|
|
46
46
|
this.emit('contactRemoved', removed, this.getContacts())
|
|
@@ -4,7 +4,6 @@ import { ConnectionType } from '../../connection/IConnection'
|
|
|
4
4
|
import { expectedConnectionType } from '../../helpers/Connectivity'
|
|
5
5
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
6
6
|
import { DhtRpcOptions } from '../../rpc-protocol/DhtRpcOptions'
|
|
7
|
-
import { ServiceID } from '../../types/ServiceID'
|
|
8
7
|
|
|
9
8
|
// Should connect directly to the server, timeout can be low
|
|
10
9
|
const WEBSOCKET_CLIENT_TIMEOUT = 5000
|
|
@@ -32,14 +31,12 @@ export abstract class RpcRemote<T extends ServiceInfo & ClassType> {
|
|
|
32
31
|
|
|
33
32
|
private readonly localPeerDescriptor: PeerDescriptor
|
|
34
33
|
private readonly remotePeerDescriptor: PeerDescriptor
|
|
35
|
-
private readonly serviceId: ServiceID
|
|
36
34
|
private readonly client: ProtoRpcClient<T>
|
|
37
35
|
private readonly timeout?: number
|
|
38
36
|
|
|
39
37
|
constructor(
|
|
40
38
|
localPeerDescriptor: PeerDescriptor,
|
|
41
39
|
remotePeerDescriptor: PeerDescriptor,
|
|
42
|
-
serviceId: ServiceID,
|
|
43
40
|
rpcCommunicator: RpcCommunicator,
|
|
44
41
|
// eslint-disable-next-line @typescript-eslint/prefer-function-type
|
|
45
42
|
clientClass: { new (clientTransport: ClientTransport): T },
|
|
@@ -48,7 +45,6 @@ export abstract class RpcRemote<T extends ServiceInfo & ClassType> {
|
|
|
48
45
|
this.localPeerDescriptor = localPeerDescriptor
|
|
49
46
|
this.remotePeerDescriptor = remotePeerDescriptor
|
|
50
47
|
this.client = toProtoRpcClient(new clientClass(rpcCommunicator.getRpcClientTransport()))
|
|
51
|
-
this.serviceId = serviceId
|
|
52
48
|
this.timeout = timeout
|
|
53
49
|
}
|
|
54
50
|
|
|
@@ -60,10 +56,6 @@ export abstract class RpcRemote<T extends ServiceInfo & ClassType> {
|
|
|
60
56
|
return this.localPeerDescriptor
|
|
61
57
|
}
|
|
62
58
|
|
|
63
|
-
getServiceId(): ServiceID {
|
|
64
|
-
return this.serviceId
|
|
65
|
-
}
|
|
66
|
-
|
|
67
59
|
getClient(): ProtoRpcClient<T> {
|
|
68
60
|
return this.client
|
|
69
61
|
}
|