@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.
- package/dist/src/connection/ConnectionLockHandler.js +2 -2
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +2 -2
- package/dist/src/connection/ConnectionLockRpcRemote.js +3 -27
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +0 -1
- package/dist/src/connection/ConnectionManager.js +11 -7
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +2 -2
- package/dist/src/connection/ConnectorFacade.js +1 -2
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/ManagedConnection.js +1 -0
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/connectivityChecker.d.ts +9 -0
- package/dist/src/connection/connectivityChecker.js +122 -0
- package/dist/src/connection/connectivityChecker.js.map +1 -0
- package/dist/src/connection/connectivityRequestHandler.d.ts +2 -0
- package/dist/src/connection/connectivityRequestHandler.js +79 -0
- package/dist/src/connection/connectivityRequestHandler.js.map +1 -0
- package/dist/src/connection/simulator/Simulator.js +3 -2
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.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.js +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js.map +1 -1
- package/dist/src/connection/websocket/ClientWebsocket.d.ts +1 -0
- package/dist/src/connection/websocket/ClientWebsocket.js +7 -3
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/ServerWebsocket.d.ts +4 -0
- package/dist/src/connection/websocket/ServerWebsocket.js +32 -21
- package/dist/src/connection/websocket/ServerWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +0 -2
- package/dist/src/connection/websocket/WebsocketConnector.js +61 -16
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +8 -11
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +4 -4
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +5 -39
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketServer.js +21 -4
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +13 -23
- package/dist/src/dht/DhtNode.js +97 -226
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +1 -4
- package/dist/src/dht/DhtNodeRpcLocal.js +1 -5
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +3 -3
- package/dist/src/dht/DhtNodeRpcRemote.js +4 -4
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +4 -4
- package/dist/src/dht/ExternalApiRpcLocal.js +5 -12
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +3 -3
- package/dist/src/dht/ExternalApiRpcRemote.js +5 -5
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +52 -0
- package/dist/src/dht/PeerManager.js +273 -0
- package/dist/src/dht/PeerManager.js.map +1 -0
- package/dist/src/dht/contact/ContactList.d.ts +1 -1
- package/dist/src/dht/contact/ContactList.js +1 -0
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/{Remote.d.ts → RpcRemote.d.ts} +3 -3
- package/dist/src/dht/contact/{Remote.js → RpcRemote.js} +8 -8
- package/dist/src/dht/contact/RpcRemote.js.map +1 -0
- package/dist/src/dht/contact/SortedContactList.d.ts +20 -6
- package/dist/src/dht/contact/SortedContactList.js +55 -24
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +4 -14
- package/dist/src/dht/discovery/DiscoverySession.js +15 -26
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +2 -9
- package/dist/src/dht/discovery/PeerDiscovery.js +11 -19
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/find/FindRpcLocal.js +2 -1
- package/dist/src/dht/find/FindRpcLocal.js.map +1 -1
- package/dist/src/dht/find/FindSession.d.ts +6 -6
- package/dist/src/dht/find/FindSession.js +18 -13
- package/dist/src/dht/find/FindSession.js.map +1 -1
- package/dist/src/dht/find/FindSessionRpcLocal.d.ts +1 -1
- package/dist/src/dht/find/FindSessionRpcRemote.d.ts +2 -2
- package/dist/src/dht/find/FindSessionRpcRemote.js +2 -2
- package/dist/src/dht/find/FindSessionRpcRemote.js.map +1 -1
- package/dist/src/dht/find/Finder.d.ts +4 -4
- package/dist/src/dht/find/Finder.js +55 -42
- package/dist/src/dht/find/Finder.js.map +1 -1
- package/dist/src/dht/routing/FindRpcRemote.d.ts +2 -2
- package/dist/src/dht/routing/FindRpcRemote.js +7 -5
- package/dist/src/dht/routing/FindRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +3 -7
- package/dist/src/dht/routing/Router.js +29 -22
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +2 -2
- package/dist/src/dht/routing/RouterRpcLocal.js +4 -3
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcRemote.d.ts +2 -2
- package/dist/src/dht/routing/RouterRpcRemote.js +13 -8
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +1 -1
- package/dist/src/dht/routing/RoutingSession.js +23 -11
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/routing/getPreviousPeer.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +3 -3
- package/dist/src/dht/store/LocalDataStore.js +18 -17
- package/dist/src/dht/store/LocalDataStore.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.d.ts +10 -9
- package/dist/src/dht/store/StoreRpcLocal.js +108 -102
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.d.ts +4 -5
- package/dist/src/dht/store/StoreRpcRemote.js +6 -15
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/exports.d.ts +1 -1
- package/dist/src/exports.js +4 -4
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/PeerID.d.ts +1 -0
- package/dist/src/helpers/PeerID.js +9 -4
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/UUID.js +1 -1
- package/dist/src/helpers/UUID.js.map +1 -1
- package/dist/src/helpers/nodeId.d.ts +1 -0
- package/dist/src/helpers/{kademliaId.js → nodeId.js} +4 -4
- package/dist/src/helpers/nodeId.js.map +1 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +4 -4
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +5 -16
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +2 -9
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +80 -95
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +67 -66
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +3 -10
- package/dist/src/transport/RoutingRpcCommunicator.js +2 -0
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/karma.config.js +2 -2
- package/package.json +5 -5
- package/protos/DhtRpc.proto +36 -36
- package/src/connection/ConnectionLockHandler.ts +2 -2
- package/src/connection/ConnectionLockRpcRemote.ts +3 -4
- package/src/connection/ConnectionManager.ts +19 -17
- package/src/connection/ConnectorFacade.ts +5 -7
- package/src/connection/ManagedConnection.ts +1 -0
- package/src/connection/connectivityChecker.ts +102 -0
- package/src/connection/connectivityRequestHandler.ts +79 -0
- package/src/connection/simulator/Simulator.ts +3 -2
- package/src/connection/simulator/SimulatorConnection.ts +1 -1
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +10 -10
- package/src/connection/webrtc/NodeWebrtcConnection.ts +1 -1
- package/src/connection/webrtc/WebrtcConnector.ts +1 -1
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +1 -1
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +2 -2
- package/src/connection/webrtc/iceServerAsString.ts +1 -1
- package/src/connection/websocket/ClientWebsocket.ts +6 -2
- package/src/connection/websocket/ServerWebsocket.ts +40 -25
- package/src/connection/websocket/WebsocketConnector.ts +43 -22
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +9 -11
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +7 -16
- package/src/connection/websocket/WebsocketServer.ts +20 -5
- package/src/dht/DhtNode.ts +123 -280
- package/src/dht/DhtNodeRpcLocal.ts +2 -9
- package/src/dht/DhtNodeRpcRemote.ts +4 -4
- package/src/dht/ExternalApiRpcLocal.ts +8 -13
- package/src/dht/ExternalApiRpcRemote.ts +5 -5
- package/src/dht/PeerManager.ts +330 -0
- package/src/dht/contact/ContactList.ts +3 -2
- package/src/dht/contact/{Remote.ts → RpcRemote.ts} +7 -6
- package/src/dht/contact/SortedContactList.ts +87 -44
- package/src/dht/discovery/DiscoverySession.ts +19 -44
- package/src/dht/discovery/PeerDiscovery.ts +16 -28
- package/src/dht/find/FindRpcLocal.ts +2 -2
- package/src/dht/find/FindSession.ts +25 -20
- package/src/dht/find/FindSessionRpcLocal.ts +1 -1
- package/src/dht/find/FindSessionRpcRemote.ts +2 -2
- package/src/dht/find/Finder.ts +84 -64
- package/src/dht/routing/FindRpcRemote.ts +7 -5
- package/src/dht/routing/Router.ts +30 -25
- package/src/dht/routing/RouterRpcLocal.ts +5 -5
- package/src/dht/routing/RouterRpcRemote.ts +13 -10
- package/src/dht/routing/RoutingSession.ts +22 -17
- package/src/dht/routing/getPreviousPeer.ts +1 -1
- package/src/dht/store/LocalDataStore.ts +18 -17
- package/src/dht/store/StoreRpcLocal.ts +118 -113
- package/src/dht/store/StoreRpcRemote.ts +7 -23
- package/src/exports.ts +1 -1
- package/src/helpers/PeerID.ts +8 -4
- package/src/helpers/UUID.ts +1 -1
- package/src/helpers/{kademliaId.ts → nodeId.ts} +1 -1
- package/src/helpers/peerIdFromPeerDescriptor.ts +6 -6
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +6 -20
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +3 -10
- package/src/proto/packages/dht/protos/DhtRpc.ts +103 -135
- package/src/transport/RoutingRpcCommunicator.ts +2 -0
- package/test/benchmark/Find.test.ts +5 -5
- package/test/benchmark/KademliaCorrectness.test.ts +3 -3
- package/test/benchmark/SortedContactListBenchmark.test.ts +150 -0
- package/test/benchmark/WebsocketServerMemoryLeak.test.ts +41 -0
- package/test/benchmark/kademlia-simulation/Contact.ts +1 -1
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +1 -1
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +6 -1
- package/test/end-to-end/Layer0-Layer1.test.ts +1 -1
- package/test/end-to-end/Layer0.test.ts +4 -4
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +11 -11
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +6 -6
- package/test/end-to-end/Layer0Webrtc.test.ts +2 -2
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +3 -3
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +3 -3
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +1 -1
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +1 -1
- package/test/end-to-end/memory-leak.test.ts +9 -12
- package/test/integration/ConnectionLocking.test.ts +2 -2
- package/test/integration/ConnectionManager.test.ts +14 -14
- package/test/integration/DhtJoinPeerDiscovery.test.ts +3 -3
- package/test/integration/DhtNodeExternalAPI.test.ts +10 -7
- package/test/integration/DhtNodeRpcRemote.test.ts +4 -4
- package/test/integration/DhtRpc.test.ts +6 -6
- 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 +16 -16
- package/test/integration/MultipleEntryPointJoining.test.ts +7 -7
- package/test/integration/{MigrateData.test.ts → ReplicateData.test.ts} +15 -10
- package/test/integration/RouteMessage.test.ts +2 -2
- package/test/integration/RouterRpcRemote.test.ts +2 -2
- package/test/integration/RpcErrors.test.ts +2 -2
- package/test/integration/ScaleDownDht.test.ts +4 -2
- package/test/integration/SimultaneousConnections.test.ts +89 -57
- package/test/integration/Store.test.ts +33 -13
- package/test/integration/StoreAndDelete.test.ts +19 -17
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +21 -21
- package/test/integration/StoreRpcRemote.test.ts +3 -3
- package/test/integration/WebrtcConnectionManagement.test.ts +2 -2
- package/test/integration/WebrtcConnectorRpc.test.ts +1 -1
- package/test/integration/WebsocketConnectionManagement.test.ts +41 -3
- package/test/integration/WebsocketConnectorRpc.test.ts +5 -7
- package/test/unit/ConnectivityHelpers.test.ts +4 -4
- package/test/unit/Finder.test.ts +69 -23
- package/test/unit/LocalDataStore.test.ts +60 -43
- package/test/unit/RandomContactList.test.ts +2 -2
- package/test/unit/Router.test.ts +19 -11
- package/test/unit/RoutingSession.test.ts +76 -0
- package/test/unit/SortedContactList.test.ts +17 -12
- package/test/unit/WebsocketConnector.test.ts +1 -1
- package/test/unit/connectivityRequestHandler.test.ts +71 -0
- package/test/utils/mock/Router.ts +1 -1
- package/test/utils/utils.ts +24 -22
- package/dist/src/connection/ConnectivityChecker.d.ts +0 -17
- package/dist/src/connection/ConnectivityChecker.js +0 -208
- package/dist/src/connection/ConnectivityChecker.js.map +0 -1
- package/dist/src/dht/contact/Remote.js.map +0 -1
- package/dist/src/helpers/kademliaId.d.ts +0 -1
- package/dist/src/helpers/kademliaId.js.map +0 -1
- package/src/connection/ConnectivityChecker.ts +0 -199
package/src/dht/DhtNode.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { DhtNodeRpcRemote } from './DhtNodeRpcRemote'
|
|
2
|
-
import KBucket from 'k-bucket'
|
|
3
2
|
import { EventEmitter } from 'eventemitter3'
|
|
4
|
-
import { SortedContactList } from './contact/SortedContactList'
|
|
5
3
|
import { RoutingRpcCommunicator } from '../transport/RoutingRpcCommunicator'
|
|
6
|
-
import { PeerID
|
|
4
|
+
import { PeerID } from '../helpers/PeerID'
|
|
7
5
|
import {
|
|
8
6
|
ClosestPeersRequest,
|
|
9
7
|
ClosestPeersResponse,
|
|
@@ -19,6 +17,7 @@ import {
|
|
|
19
17
|
ExternalFindDataResponse,
|
|
20
18
|
ExternalStoreDataRequest,
|
|
21
19
|
ExternalStoreDataResponse,
|
|
20
|
+
FindAction,
|
|
22
21
|
} from '../proto/packages/dht/protos/DhtRpc'
|
|
23
22
|
import { ITransport, TransportEvents } from '../transport/ITransport'
|
|
24
23
|
import { ConnectionManager, PortRange, TlsCertificate } from '../connection/ConnectionManager'
|
|
@@ -31,12 +30,10 @@ import {
|
|
|
31
30
|
waitForCondition
|
|
32
31
|
} from '@streamr/utils'
|
|
33
32
|
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
34
|
-
import { RandomContactList } from './contact/RandomContactList'
|
|
35
33
|
import { Any } from '../proto/google/protobuf/any'
|
|
36
34
|
import {
|
|
37
35
|
areEqualPeerDescriptors,
|
|
38
36
|
getNodeIdFromPeerDescriptor,
|
|
39
|
-
keyFromPeerDescriptor,
|
|
40
37
|
peerIdFromPeerDescriptor
|
|
41
38
|
} from '../helpers/peerIdFromPeerDescriptor'
|
|
42
39
|
import { Router } from './routing/Router'
|
|
@@ -54,6 +51,7 @@ import { MarkRequired } from 'ts-essentials'
|
|
|
54
51
|
import { DhtNodeRpcLocal } from './DhtNodeRpcLocal'
|
|
55
52
|
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
56
53
|
import { ExternalApiRpcLocal } from './ExternalApiRpcLocal'
|
|
54
|
+
import { PeerManager, getDistance } from './PeerManager'
|
|
57
55
|
|
|
58
56
|
export interface DhtNodeEvents {
|
|
59
57
|
newContact: (peerDescriptor: PeerDescriptor, closestPeers: PeerDescriptor[]) => void
|
|
@@ -100,7 +98,7 @@ export interface DhtNodeOptions {
|
|
|
100
98
|
autoCertifierConfigFile?: string
|
|
101
99
|
}
|
|
102
100
|
|
|
103
|
-
type StrictDhtNodeOptions = MarkRequired<DhtNodeOptions,
|
|
101
|
+
type StrictDhtNodeOptions = MarkRequired<DhtNodeOptions,
|
|
104
102
|
'serviceId' |
|
|
105
103
|
'joinParallelism' |
|
|
106
104
|
'maxNeighborListSize' |
|
|
@@ -121,14 +119,14 @@ const logger = new Logger(module)
|
|
|
121
119
|
export type Events = TransportEvents & DhtNodeEvents
|
|
122
120
|
|
|
123
121
|
export const createPeerDescriptor = (msg?: ConnectivityResponse, peerId?: string): PeerDescriptor => {
|
|
124
|
-
let
|
|
122
|
+
let nodeId: Uint8Array
|
|
125
123
|
if (msg) {
|
|
126
|
-
|
|
124
|
+
nodeId = (peerId !== undefined) ? hexToBinary(peerId) : PeerID.fromIp(msg.host).value
|
|
127
125
|
} else {
|
|
128
|
-
|
|
126
|
+
nodeId = hexToBinary(peerId!)
|
|
129
127
|
}
|
|
130
|
-
const nodeType = isBrowserEnvironment() ? NodeType.BROWSER : NodeType.NODEJS
|
|
131
|
-
const ret: PeerDescriptor = {
|
|
128
|
+
const nodeType = isBrowserEnvironment() ? NodeType.BROWSER : NodeType.NODEJS
|
|
129
|
+
const ret: PeerDescriptor = { nodeId, type: nodeType }
|
|
132
130
|
if (msg && msg.websocket) {
|
|
133
131
|
ret.websocket = { host: msg.websocket.host, port: msg.websocket.port, tls: msg.websocket.tls }
|
|
134
132
|
}
|
|
@@ -138,10 +136,6 @@ export const createPeerDescriptor = (msg?: ConnectivityResponse, peerId?: string
|
|
|
138
136
|
export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
139
137
|
|
|
140
138
|
private readonly config: StrictDhtNodeOptions
|
|
141
|
-
private bucket?: KBucket<DhtNodeRpcRemote>
|
|
142
|
-
private connections: Map<PeerIDKey, DhtNodeRpcRemote> = new Map()
|
|
143
|
-
private neighborList?: SortedContactList<DhtNodeRpcRemote>
|
|
144
|
-
private randomPeers?: RandomContactList<DhtNodeRpcRemote>
|
|
145
139
|
private rpcCommunicator?: RoutingRpcCommunicator
|
|
146
140
|
private transport?: ITransport
|
|
147
141
|
private localPeerDescriptor?: PeerDescriptor
|
|
@@ -150,10 +144,11 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
150
144
|
private localDataStore = new LocalDataStore()
|
|
151
145
|
private finder?: Finder
|
|
152
146
|
private peerDiscovery?: PeerDiscovery
|
|
147
|
+
private peerManager?: PeerManager
|
|
153
148
|
|
|
154
149
|
public connectionManager?: ConnectionManager
|
|
155
150
|
private started = false
|
|
156
|
-
private
|
|
151
|
+
private abortController = new AbortController()
|
|
157
152
|
private entryPointDisconnectTimeout?: NodeJS.Timeout
|
|
158
153
|
|
|
159
154
|
constructor(conf: DhtNodeOptions) {
|
|
@@ -178,7 +173,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
178
173
|
}
|
|
179
174
|
|
|
180
175
|
public async start(): Promise<void> {
|
|
181
|
-
if (this.started || this.
|
|
176
|
+
if (this.started || this.abortController.signal.aborted) {
|
|
182
177
|
return
|
|
183
178
|
}
|
|
184
179
|
logger.trace(`Starting new Streamr Network DHT Node with serviceId ${this.config.serviceId}`)
|
|
@@ -218,12 +213,12 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
218
213
|
// If own PeerDescriptor is given in config, create a ConnectionManager with ws server
|
|
219
214
|
if (this.config.peerDescriptor?.websocket) {
|
|
220
215
|
connectorFacadeConfig.websocketHost = this.config.peerDescriptor.websocket.host
|
|
221
|
-
connectorFacadeConfig.websocketPortRange = {
|
|
216
|
+
connectorFacadeConfig.websocketPortRange = {
|
|
222
217
|
min: this.config.peerDescriptor.websocket.port,
|
|
223
218
|
max: this.config.peerDescriptor.websocket.port
|
|
224
219
|
}
|
|
225
220
|
// If websocketPortRange is given, create ws server using it, websocketHost can be undefined
|
|
226
|
-
} else if (this.config.websocketPortRange) {
|
|
221
|
+
} else if (this.config.websocketPortRange) {
|
|
227
222
|
connectorFacadeConfig.websocketHost = this.config.websocketHost
|
|
228
223
|
connectorFacadeConfig.websocketPortRange = this.config.websocketPortRange
|
|
229
224
|
}
|
|
@@ -246,26 +241,23 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
246
241
|
|
|
247
242
|
this.transport.on('message', (message: Message) => this.handleMessage(message))
|
|
248
243
|
|
|
249
|
-
this.
|
|
244
|
+
this.initPeerManager()
|
|
245
|
+
|
|
250
246
|
this.peerDiscovery = new PeerDiscovery({
|
|
251
|
-
rpcCommunicator: this.rpcCommunicator,
|
|
252
247
|
localPeerDescriptor: this.localPeerDescriptor!,
|
|
253
|
-
bucket: this.bucket!,
|
|
254
|
-
neighborList: this.neighborList!,
|
|
255
248
|
joinNoProgressLimit: this.config.joinNoProgressLimit,
|
|
256
249
|
peerDiscoveryQueryBatchSize: this.config.peerDiscoveryQueryBatchSize,
|
|
257
250
|
joinTimeout: this.config.dhtJoinTimeout,
|
|
258
251
|
serviceId: this.config.serviceId,
|
|
259
252
|
parallelism: this.config.joinParallelism,
|
|
260
|
-
addContact: this.addNewContact.bind(this),
|
|
261
253
|
connectionManager: this.connectionManager,
|
|
262
|
-
|
|
254
|
+
peerManager: this.peerManager!
|
|
263
255
|
})
|
|
264
256
|
this.router = new Router({
|
|
265
257
|
rpcCommunicator: this.rpcCommunicator,
|
|
266
|
-
connections: this.connections,
|
|
258
|
+
connections: this.peerManager!.connections,
|
|
267
259
|
localPeerDescriptor: this.localPeerDescriptor!,
|
|
268
|
-
addContact: this.
|
|
260
|
+
addContact: (contact: PeerDescriptor, setActive?: boolean) => this.peerManager!.handleNewPeers([contact], setActive),
|
|
269
261
|
serviceId: this.config.serviceId,
|
|
270
262
|
connectionManager: this.connectionManager
|
|
271
263
|
})
|
|
@@ -273,10 +265,10 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
273
265
|
rpcCommunicator: this.rpcCommunicator,
|
|
274
266
|
router: this.router,
|
|
275
267
|
sessionTransport: this,
|
|
276
|
-
connections: this.connections,
|
|
268
|
+
connections: this.peerManager!.connections,
|
|
277
269
|
localPeerDescriptor: this.localPeerDescriptor!,
|
|
278
270
|
serviceId: this.config.serviceId,
|
|
279
|
-
addContact: this.
|
|
271
|
+
addContact: (contact: PeerDescriptor) => this.peerManager!.handleNewPeers([contact]),
|
|
280
272
|
isPeerCloserToIdThanSelf: this.isPeerCloserToIdThanSelf.bind(this),
|
|
281
273
|
localDataStore: this.localDataStore
|
|
282
274
|
})
|
|
@@ -291,126 +283,77 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
291
283
|
localDataStore: this.localDataStore,
|
|
292
284
|
dhtNodeEmitter: this,
|
|
293
285
|
getNodesClosestToIdFromBucket: (id: Uint8Array, n?: number) => {
|
|
294
|
-
return this.
|
|
286
|
+
return this.peerManager!.getClosestNeighborsTo(id, n)
|
|
295
287
|
},
|
|
296
288
|
rpcRequestTimeout: this.config.rpcRequestTimeout
|
|
297
289
|
})
|
|
298
290
|
this.bindRpcLocalMethods()
|
|
299
|
-
if (this.connectionManager
|
|
291
|
+
if ((this.connectionManager !== undefined) && (this.config.entryPoints !== undefined) && this.config.entryPoints.length > 0
|
|
300
292
|
&& !areEqualPeerDescriptors(this.config.entryPoints[0], this.localPeerDescriptor!)) {
|
|
301
293
|
this.connectToEntryPoint(this.config.entryPoints[0])
|
|
302
294
|
}
|
|
303
295
|
}
|
|
304
296
|
|
|
305
|
-
private
|
|
306
|
-
this.
|
|
307
|
-
localNodeId: selfId.value,
|
|
297
|
+
private initPeerManager() {
|
|
298
|
+
this.peerManager = new PeerManager({
|
|
308
299
|
numberOfNodesPerKBucket: this.config.numberOfNodesPerKBucket,
|
|
309
|
-
|
|
300
|
+
maxContactListSize: this.config.maxNeighborListSize,
|
|
301
|
+
ownPeerId: this.getNodeId(),
|
|
302
|
+
connectionManager: this.connectionManager!,
|
|
303
|
+
peerDiscoveryQueryBatchSize: this.config.peerDiscoveryQueryBatchSize,
|
|
304
|
+
isLayer0: (this.connectionManager !== undefined),
|
|
305
|
+
createDhtNodeRpcRemote: (peerDescriptor: PeerDescriptor) => this.createDhtNodeRpcRemote(peerDescriptor)
|
|
310
306
|
})
|
|
311
|
-
this.
|
|
312
|
-
|
|
313
|
-
this.bucket.on('added', (contact: DhtNodeRpcRemote) => this.onKBucketAdded(contact))
|
|
314
|
-
this.bucket.on('updated', () => {
|
|
315
|
-
// TODO: Update contact info to the connection manager and reconnect
|
|
307
|
+
this.peerManager.on('contactRemoved', (peerDescriptor: PeerDescriptor, activeContacts: PeerDescriptor[]) => {
|
|
308
|
+
this.emit('contactRemoved', peerDescriptor, activeContacts)
|
|
316
309
|
})
|
|
317
|
-
this.
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
310
|
+
this.peerManager.on('newContact', (peerDescriptor: PeerDescriptor, activeContacts: PeerDescriptor[]) =>
|
|
311
|
+
this.emit('newContact', peerDescriptor, activeContacts)
|
|
312
|
+
)
|
|
313
|
+
this.peerManager.on('randomContactRemoved', (peerDescriptor: PeerDescriptor, activeContacts: PeerDescriptor[]) =>
|
|
314
|
+
this.emit('randomContactRemoved', peerDescriptor, activeContacts)
|
|
315
|
+
)
|
|
316
|
+
this.peerManager.on('newRandomContact', (peerDescriptor: PeerDescriptor, activeContacts: PeerDescriptor[]) =>
|
|
317
|
+
this.emit('newRandomContact', peerDescriptor, activeContacts)
|
|
318
|
+
)
|
|
319
|
+
this.peerManager.on('kBucketEmpty', () => {
|
|
320
|
+
if (!this.peerDiscovery!.isJoinOngoing()
|
|
321
|
+
&& this.config.entryPoints
|
|
322
|
+
&& this.config.entryPoints.length > 0
|
|
323
|
+
) {
|
|
324
|
+
setImmediate(async () => {
|
|
325
|
+
// TODO should we catch possible promise rejection?
|
|
326
|
+
await Promise.all(this.config.entryPoints!.map((entryPoint) =>
|
|
327
|
+
this.peerDiscovery!.rejoinDht(entryPoint)
|
|
328
|
+
))
|
|
329
|
+
})
|
|
321
330
|
}
|
|
322
|
-
this.emit('contactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
323
|
-
this.randomPeers!.addContact(
|
|
324
|
-
new DhtNodeRpcRemote(
|
|
325
|
-
this.localPeerDescriptor!,
|
|
326
|
-
removedContact.getPeerDescriptor(),
|
|
327
|
-
toProtoRpcClient(new DhtNodeRpcClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
328
|
-
this.config.serviceId,
|
|
329
|
-
this.config.rpcRequestTimeout
|
|
330
|
-
)
|
|
331
|
-
)
|
|
332
331
|
})
|
|
333
|
-
this.
|
|
334
|
-
this.
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
332
|
+
this.transport!.on('connected', (peerDescriptor: PeerDescriptor) => {
|
|
333
|
+
this.peerManager!.handleConnected(peerDescriptor)
|
|
334
|
+
this.emit('connected', peerDescriptor)
|
|
335
|
+
})
|
|
338
336
|
this.transport!.on('disconnected', (peerDescriptor: PeerDescriptor, gracefulLeave: boolean) => {
|
|
339
|
-
this.
|
|
337
|
+
this.peerManager!.handleDisconnected(peerDescriptor, gracefulLeave)
|
|
338
|
+
this.emit('disconnected', peerDescriptor, gracefulLeave)
|
|
340
339
|
})
|
|
341
|
-
|
|
342
340
|
this.transport!.getAllConnectionPeerDescriptors().forEach((peer) => {
|
|
343
|
-
|
|
344
|
-
this.localPeerDescriptor!,
|
|
345
|
-
peer,
|
|
346
|
-
toProtoRpcClient(new DhtNodeRpcClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
347
|
-
this.config.serviceId,
|
|
348
|
-
this.config.rpcRequestTimeout
|
|
349
|
-
)
|
|
350
|
-
if (areEqualPeerDescriptors(peer, this.localPeerDescriptor!)) {
|
|
351
|
-
logger.error('own peerdescriptor added to connections in initKBucket')
|
|
352
|
-
}
|
|
353
|
-
this.connections.set(keyFromPeerDescriptor(peer), rpcRemote)
|
|
341
|
+
this.peerManager!.handleConnected(peer)
|
|
354
342
|
})
|
|
355
|
-
this.randomPeers = new RandomContactList(selfId, this.config.maxNeighborListSize)
|
|
356
|
-
this.randomPeers.on('contactRemoved', (removedContact: DhtNodeRpcRemote, activeContacts: DhtNodeRpcRemote[]) =>
|
|
357
|
-
this.emit('randomContactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
358
|
-
)
|
|
359
|
-
this.randomPeers.on('newContact', (newContact: DhtNodeRpcRemote, activeContacts: DhtNodeRpcRemote[]) =>
|
|
360
|
-
this.emit('newRandomContact', newContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
361
|
-
)
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
private onTransportConnected(peerDescriptor: PeerDescriptor): void {
|
|
365
|
-
|
|
366
|
-
if (areEqualPeerDescriptors(this.localPeerDescriptor!, peerDescriptor)) {
|
|
367
|
-
logger.error('onTransportConnected() to self')
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
const rpcRemote = new DhtNodeRpcRemote(
|
|
371
|
-
this.localPeerDescriptor!,
|
|
372
|
-
peerDescriptor,
|
|
373
|
-
toProtoRpcClient(new DhtNodeRpcClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
374
|
-
this.config.serviceId,
|
|
375
|
-
this.config.rpcRequestTimeout
|
|
376
|
-
)
|
|
377
|
-
if (!this.connections.has(PeerID.fromValue(rpcRemote.id).toKey())) {
|
|
378
|
-
this.connections.set(PeerID.fromValue(rpcRemote.id).toKey(), rpcRemote)
|
|
379
|
-
logger.trace('connectionschange add ' + this.connections.size)
|
|
380
|
-
} else {
|
|
381
|
-
logger.trace('new connection not set to connections, there is already a connection with the peer ID')
|
|
382
|
-
}
|
|
383
|
-
logger.trace('connected: ' + getNodeIdFromPeerDescriptor(peerDescriptor) + ' ' + this.connections.size)
|
|
384
|
-
this.emit('connected', peerDescriptor)
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
private onTransportDisconnected(peerDescriptor: PeerDescriptor, gracefulLeave: boolean): void {
|
|
388
|
-
logger.trace('disconnected: ' + getNodeIdFromPeerDescriptor(peerDescriptor))
|
|
389
|
-
this.connections.delete(keyFromPeerDescriptor(peerDescriptor))
|
|
390
|
-
// only remove from bucket if we are on layer 0
|
|
391
|
-
if (this.connectionManager) {
|
|
392
|
-
this.bucket!.remove(peerDescriptor.kademliaId)
|
|
393
|
-
|
|
394
|
-
if (gracefulLeave === true) {
|
|
395
|
-
logger.trace(getNodeIdFromPeerDescriptor(peerDescriptor) + ' ' + 'onTransportDisconnected with gracefulLeave ' + gracefulLeave)
|
|
396
|
-
this.removeContact(peerDescriptor)
|
|
397
|
-
} else {
|
|
398
|
-
logger.trace(getNodeIdFromPeerDescriptor(peerDescriptor) + ' ' + 'onTransportDisconnected with gracefulLeave ' + gracefulLeave)
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
this.emit('disconnected', peerDescriptor, gracefulLeave)
|
|
403
343
|
}
|
|
404
344
|
|
|
405
345
|
private bindRpcLocalMethods(): void {
|
|
406
|
-
if (!this.started || this.
|
|
346
|
+
if (!this.started || this.abortController.signal.aborted) {
|
|
407
347
|
return
|
|
408
348
|
}
|
|
409
349
|
const dhtNodeRpcLocal = new DhtNodeRpcLocal({
|
|
410
|
-
bucket: this.bucket!,
|
|
411
350
|
serviceId: this.config.serviceId,
|
|
412
351
|
peerDiscoveryQueryBatchSize: this.config.peerDiscoveryQueryBatchSize,
|
|
413
|
-
|
|
352
|
+
getClosestPeersTo: (kademliaId: Uint8Array, limit: number) => {
|
|
353
|
+
return this.peerManager!.getClosestNeighborsTo(kademliaId, limit)
|
|
354
|
+
.map((dhtPeer: DhtNodeRpcRemote) => dhtPeer.getPeerDescriptor())
|
|
355
|
+
},
|
|
356
|
+
addNewContact: (contact: PeerDescriptor) => this.peerManager!.handleNewPeers([contact]),
|
|
414
357
|
removeContact: (contact: PeerDescriptor) => this.removeContact(contact)
|
|
415
358
|
})
|
|
416
359
|
this.rpcCommunicator!.registerRpcMethod(ClosestPeersRequest, ClosestPeersResponse, 'getClosestPeers',
|
|
@@ -420,15 +363,15 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
420
363
|
this.rpcCommunicator!.registerRpcNotification(LeaveNotice, 'leaveNotice',
|
|
421
364
|
(req: LeaveNotice, context) => dhtNodeRpcLocal.leaveNotice(req, context))
|
|
422
365
|
const externalApiRpcLocal = new ExternalApiRpcLocal({
|
|
423
|
-
startFind: (
|
|
424
|
-
return this.startFind(
|
|
366
|
+
startFind: (key: Uint8Array, action: FindAction, excludedPeer: PeerDescriptor) => {
|
|
367
|
+
return this.startFind(key, action, excludedPeer)
|
|
425
368
|
},
|
|
426
|
-
storeDataToDht: (key: Uint8Array, data: Any) => this.storeDataToDht(key, data)
|
|
369
|
+
storeDataToDht: (key: Uint8Array, data: Any, creator?: PeerDescriptor) => this.storeDataToDht(key, data, creator)
|
|
427
370
|
})
|
|
428
371
|
this.rpcCommunicator!.registerRpcMethod(
|
|
429
372
|
ExternalFindDataRequest,
|
|
430
373
|
ExternalFindDataResponse,
|
|
431
|
-
'externalFindData',
|
|
374
|
+
'externalFindData',
|
|
432
375
|
(req: ExternalFindDataRequest, context: ServerCallContext) => externalApiRpcLocal.externalFindData(req, context),
|
|
433
376
|
{ timeout: 10000 }
|
|
434
377
|
)
|
|
@@ -436,14 +379,14 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
436
379
|
ExternalStoreDataRequest,
|
|
437
380
|
ExternalStoreDataResponse,
|
|
438
381
|
'externalStoreData',
|
|
439
|
-
(req: ExternalStoreDataRequest) => externalApiRpcLocal.externalStoreData(req),
|
|
382
|
+
(req: ExternalStoreDataRequest, context: ServerCallContext) => externalApiRpcLocal.externalStoreData(req, context),
|
|
440
383
|
{ timeout: 10000 }
|
|
441
384
|
)
|
|
442
385
|
}
|
|
443
386
|
|
|
444
387
|
private isPeerCloserToIdThanSelf(peer1: PeerDescriptor, compareToId: PeerID): boolean {
|
|
445
|
-
const distance1 =
|
|
446
|
-
const distance2 =
|
|
388
|
+
const distance1 = getDistance(peer1.nodeId, compareToId.value)
|
|
389
|
+
const distance2 = getDistance(this.localPeerDescriptor!.nodeId, compareToId.value)
|
|
447
390
|
return distance1 < distance2
|
|
448
391
|
}
|
|
449
392
|
|
|
@@ -468,122 +411,16 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
468
411
|
return this.localPeerDescriptor
|
|
469
412
|
}
|
|
470
413
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
return
|
|
474
|
-
}
|
|
475
|
-
const sortingList: SortedContactList<DhtNodeRpcRemote> = new SortedContactList(this.getNodeId(), 100)
|
|
476
|
-
sortingList.addContacts(oldContacts)
|
|
477
|
-
const sortedContacts = sortingList.getAllContacts()
|
|
478
|
-
this.connectionManager?.weakUnlockConnection(sortedContacts[sortedContacts.length - 1].getPeerDescriptor())
|
|
479
|
-
this.bucket?.remove(sortedContacts[sortedContacts.length - 1].getPeerId().value)
|
|
480
|
-
this.bucket!.add(newContact)
|
|
414
|
+
public getClosestContacts(limit?: number): PeerDescriptor[] {
|
|
415
|
+
return this.peerManager!.getClosestContactsTo(this.localPeerDescriptor!.nodeId, limit).map((peer) => peer.getPeerDescriptor())
|
|
481
416
|
}
|
|
482
|
-
|
|
483
|
-
private onKBucketRemoved(contact: DhtNodeRpcRemote): void {
|
|
484
|
-
if (this.stopped) {
|
|
485
|
-
return
|
|
486
|
-
}
|
|
487
|
-
this.connectionManager?.weakUnlockConnection(contact.getPeerDescriptor())
|
|
488
|
-
logger.trace(`Removed contact ${getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
489
|
-
if (this.bucket!.count() === 0
|
|
490
|
-
&& !this.peerDiscovery!.isJoinOngoing()
|
|
491
|
-
&& this.config.entryPoints
|
|
492
|
-
&& this.config.entryPoints.length > 0
|
|
493
|
-
) {
|
|
494
|
-
setImmediate(async () => {
|
|
495
|
-
await Promise.all(this.config.entryPoints!.map((entryPoint) =>
|
|
496
|
-
this.peerDiscovery!.rejoinDht(entryPoint)
|
|
497
|
-
))
|
|
498
|
-
})
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
private onKBucketAdded(contact: DhtNodeRpcRemote): void {
|
|
503
|
-
if (this.stopped) {
|
|
504
|
-
return
|
|
505
|
-
}
|
|
506
|
-
if (!contact.getPeerId().equals(this.getNodeId())) {
|
|
507
|
-
// Important to lock here, before the ping result is known
|
|
508
|
-
this.connectionManager?.weakLockConnection(contact.getPeerDescriptor())
|
|
509
|
-
if (this.connections.has(contact.getPeerId().toKey())) {
|
|
510
|
-
logger.trace(`Added new contact ${getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
511
|
-
} else { // open connection by pinging
|
|
512
|
-
logger.trace('starting ping ' + getNodeIdFromPeerDescriptor(contact.getPeerDescriptor()))
|
|
513
|
-
contact.ping().then((result) => {
|
|
514
|
-
if (result) {
|
|
515
|
-
logger.trace(`Added new contact ${getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
516
|
-
} else {
|
|
517
|
-
logger.trace('ping failed ' + getNodeIdFromPeerDescriptor(contact.getPeerDescriptor()))
|
|
518
|
-
this.connectionManager?.weakUnlockConnection(contact.getPeerDescriptor())
|
|
519
|
-
this.removeContact(contact.getPeerDescriptor())
|
|
520
|
-
this.addClosestContactToBucket()
|
|
521
|
-
}
|
|
522
|
-
return
|
|
523
|
-
}).catch((_e) => {
|
|
524
|
-
this.connectionManager?.weakUnlockConnection(contact.getPeerDescriptor())
|
|
525
|
-
this.removeContact(contact.getPeerDescriptor())
|
|
526
|
-
this.addClosestContactToBucket()
|
|
527
|
-
})
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
private addClosestContactToBucket(): void {
|
|
533
|
-
if (!this.started || this.stopped) {
|
|
534
|
-
return
|
|
535
|
-
}
|
|
536
|
-
const closest = this.getClosestActiveContactNotInBucket()
|
|
537
|
-
if (closest) {
|
|
538
|
-
this.addNewContact(closest.getPeerDescriptor())
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
private getClosestActiveContactNotInBucket(): DhtNodeRpcRemote | undefined {
|
|
543
|
-
for (const contactId of this.neighborList!.getContactIds()) {
|
|
544
|
-
if (!this.bucket!.get(contactId.value) && this.neighborList!.isActive(contactId)) {
|
|
545
|
-
return this.neighborList!.getContact(contactId).contact
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
return undefined
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
public getClosestContacts(maxCount?: number): PeerDescriptor[] {
|
|
552
|
-
return this.neighborList!.getClosestContacts(maxCount).map((c) => c.getPeerDescriptor())
|
|
553
|
-
}
|
|
554
|
-
|
|
417
|
+
|
|
555
418
|
public getNodeId(): PeerID {
|
|
556
419
|
return peerIdFromPeerDescriptor(this.localPeerDescriptor!)
|
|
557
420
|
}
|
|
558
421
|
|
|
559
|
-
public
|
|
560
|
-
return this.
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
private addNewContact(contact: PeerDescriptor, setActive = false): void {
|
|
564
|
-
if (!this.started || this.stopped) {
|
|
565
|
-
return
|
|
566
|
-
}
|
|
567
|
-
if (!areEqualPeerDescriptors(contact, this.localPeerDescriptor!)) {
|
|
568
|
-
logger.trace(`Adding new contact ${getNodeIdFromPeerDescriptor(contact)}`)
|
|
569
|
-
const rpcRemote = new DhtNodeRpcRemote(
|
|
570
|
-
this.localPeerDescriptor!,
|
|
571
|
-
contact,
|
|
572
|
-
toProtoRpcClient(new DhtNodeRpcClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
573
|
-
this.config.serviceId,
|
|
574
|
-
this.config.rpcRequestTimeout
|
|
575
|
-
)
|
|
576
|
-
if (!this.bucket!.get(contact.kademliaId) && !this.neighborList!.getContact(peerIdFromPeerDescriptor(contact))) {
|
|
577
|
-
this.neighborList!.addContact(rpcRemote)
|
|
578
|
-
if (setActive) {
|
|
579
|
-
const peerId = peerIdFromPeerDescriptor(contact)
|
|
580
|
-
this.neighborList!.setActive(peerId)
|
|
581
|
-
}
|
|
582
|
-
this.bucket!.add(rpcRemote)
|
|
583
|
-
} else {
|
|
584
|
-
this.randomPeers!.addContact(rpcRemote)
|
|
585
|
-
}
|
|
586
|
-
}
|
|
422
|
+
public getNumberOfNeighbors(): number {
|
|
423
|
+
return this.peerManager!.getNumberOfNeighbors()
|
|
587
424
|
}
|
|
588
425
|
|
|
589
426
|
private connectToEntryPoint(entryPoint: PeerDescriptor): void {
|
|
@@ -594,42 +431,38 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
594
431
|
}
|
|
595
432
|
|
|
596
433
|
public removeContact(contact: PeerDescriptor): void {
|
|
597
|
-
if (!this.started
|
|
434
|
+
if (!this.started) { // the stopped state is checked in PeerManager
|
|
598
435
|
return
|
|
599
436
|
}
|
|
600
|
-
|
|
601
|
-
const peerId = peerIdFromPeerDescriptor(contact)
|
|
602
|
-
this.bucket!.remove(peerId.value)
|
|
603
|
-
this.neighborList!.removeContact(peerId)
|
|
604
|
-
this.randomPeers!.removeContact(peerId)
|
|
437
|
+
this.peerManager!.handlePeerLeaving(contact)
|
|
605
438
|
}
|
|
606
439
|
|
|
607
440
|
public async send(msg: Message): Promise<void> {
|
|
608
|
-
if (!this.started || this.
|
|
441
|
+
if (!this.started || this.abortController.signal.aborted) {
|
|
609
442
|
return
|
|
610
443
|
}
|
|
611
444
|
const reachableThrough = this.peerDiscovery!.isJoinOngoing() ? this.config.entryPoints ?? [] : []
|
|
612
|
-
|
|
445
|
+
this.router!.send(msg, reachableThrough)
|
|
613
446
|
}
|
|
614
447
|
|
|
615
448
|
public async joinDht(entryPointDescriptors: PeerDescriptor[], doAdditionalRandomPeerDiscovery?: boolean, retry?: boolean): Promise<void> {
|
|
616
449
|
if (!this.started) {
|
|
617
450
|
throw new Error('Cannot join DHT before calling start() on DhtNode')
|
|
618
451
|
}
|
|
619
|
-
await Promise.all(entryPointDescriptors.map((entryPoint) =>
|
|
452
|
+
await Promise.all(entryPointDescriptors.map((entryPoint) =>
|
|
620
453
|
this.peerDiscovery!.joinDht(entryPoint, doAdditionalRandomPeerDiscovery, retry)
|
|
621
454
|
))
|
|
622
455
|
}
|
|
623
456
|
|
|
624
|
-
public async startFind(
|
|
625
|
-
return this.finder!.startFind(
|
|
457
|
+
public async startFind(key: Uint8Array, action?: FindAction, excludedPeer?: PeerDescriptor): Promise<FindResult> {
|
|
458
|
+
return this.finder!.startFind(key, action, excludedPeer)
|
|
626
459
|
}
|
|
627
460
|
|
|
628
|
-
public async storeDataToDht(key: Uint8Array, data: Any): Promise<PeerDescriptor[]> {
|
|
461
|
+
public async storeDataToDht(key: Uint8Array, data: Any, creator?: PeerDescriptor): Promise<PeerDescriptor[]> {
|
|
629
462
|
if (this.peerDiscovery!.isJoinOngoing() && this.config.entryPoints && this.config.entryPoints.length > 0) {
|
|
630
463
|
return this.storeDataViaPeer(key, data, sample(this.config.entryPoints)!)
|
|
631
464
|
}
|
|
632
|
-
return this.storeRpcLocal!.storeDataToDht(key, data)
|
|
465
|
+
return this.storeRpcLocal!.storeDataToDht(key, data, creator ?? this.localPeerDescriptor!)
|
|
633
466
|
}
|
|
634
467
|
|
|
635
468
|
public async storeDataViaPeer(key: Uint8Array, data: Any, peer: PeerDescriptor): Promise<PeerDescriptor[]> {
|
|
@@ -642,28 +475,28 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
642
475
|
return await rpcRemote.storeData(key, data)
|
|
643
476
|
}
|
|
644
477
|
|
|
645
|
-
public async getDataFromDht(
|
|
478
|
+
public async getDataFromDht(key: Uint8Array): Promise<DataEntry[]> {
|
|
646
479
|
if (this.peerDiscovery!.isJoinOngoing() && this.config.entryPoints && this.config.entryPoints.length > 0) {
|
|
647
|
-
return this.findDataViaPeer(
|
|
480
|
+
return this.findDataViaPeer(key, sample(this.config.entryPoints)!)
|
|
648
481
|
}
|
|
649
|
-
const result = await this.finder!.startFind(
|
|
650
|
-
return result.dataEntries ?? []
|
|
482
|
+
const result = await this.finder!.startFind(key, FindAction.FETCH_DATA)
|
|
483
|
+
return result.dataEntries ?? [] // TODO is this fallback needed?
|
|
651
484
|
}
|
|
652
485
|
|
|
653
|
-
public async deleteDataFromDht(
|
|
654
|
-
if (!this.
|
|
655
|
-
|
|
486
|
+
public async deleteDataFromDht(key: Uint8Array, waitForCompletion: boolean): Promise<void> {
|
|
487
|
+
if (!this.abortController.signal.aborted) {
|
|
488
|
+
await this.finder!.startFind(key, FindAction.DELETE_DATA, undefined, waitForCompletion)
|
|
656
489
|
}
|
|
657
490
|
}
|
|
658
491
|
|
|
659
|
-
public async findDataViaPeer(
|
|
492
|
+
public async findDataViaPeer(key: Uint8Array, peer: PeerDescriptor): Promise<DataEntry[]> {
|
|
660
493
|
const rpcRemote = new ExternalApiRpcRemote(
|
|
661
494
|
this.localPeerDescriptor!,
|
|
662
495
|
peer,
|
|
663
496
|
this.config.serviceId,
|
|
664
497
|
toProtoRpcClient(new ExternalApiRpcClient(this.rpcCommunicator!.getRpcClientTransport()))
|
|
665
498
|
)
|
|
666
|
-
return await rpcRemote.externalFindData(
|
|
499
|
+
return await rpcRemote.externalFindData(key)
|
|
667
500
|
}
|
|
668
501
|
|
|
669
502
|
public getTransport(): ITransport {
|
|
@@ -675,15 +508,16 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
675
508
|
}
|
|
676
509
|
|
|
677
510
|
public getAllConnectionPeerDescriptors(): PeerDescriptor[] {
|
|
678
|
-
return Array.from(this.connections.values()).map((peer) => peer.getPeerDescriptor())
|
|
511
|
+
return Array.from(this.peerManager!.connections.values()).map((peer) => peer.getPeerDescriptor())
|
|
679
512
|
}
|
|
680
513
|
|
|
681
|
-
|
|
682
|
-
|
|
514
|
+
// TODO rename to getNeighbors
|
|
515
|
+
public getAllNeighborPeerDescriptors(): PeerDescriptor[] {
|
|
516
|
+
return this.peerManager!.getNeighbors()
|
|
683
517
|
}
|
|
684
518
|
|
|
685
519
|
public getNumberOfConnections(): number {
|
|
686
|
-
return this.
|
|
520
|
+
return this.peerManager!.getNumberOfConnections()
|
|
687
521
|
}
|
|
688
522
|
|
|
689
523
|
public getNumberOfLocalLockedConnections(): number {
|
|
@@ -699,7 +533,13 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
699
533
|
}
|
|
700
534
|
|
|
701
535
|
public async waitForNetworkConnectivity(): Promise<void> {
|
|
702
|
-
await waitForCondition(() =>
|
|
536
|
+
await waitForCondition(() => {
|
|
537
|
+
if (!this.peerManager) {
|
|
538
|
+
return false
|
|
539
|
+
} else {
|
|
540
|
+
return (this.peerManager.getNumberOfConnections() > 0)
|
|
541
|
+
}
|
|
542
|
+
}, this.config.networkConnectivityTimeout, 100, this.abortController.signal)
|
|
703
543
|
}
|
|
704
544
|
|
|
705
545
|
public hasJoined(): boolean {
|
|
@@ -707,35 +547,38 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
707
547
|
}
|
|
708
548
|
|
|
709
549
|
public async stop(): Promise<void> {
|
|
710
|
-
if (this.
|
|
550
|
+
if (this.abortController.signal.aborted || !this.started) {
|
|
711
551
|
return
|
|
712
552
|
}
|
|
713
553
|
logger.trace('stop()')
|
|
714
|
-
this.
|
|
715
|
-
|
|
554
|
+
this.abortController.abort()
|
|
555
|
+
await this.storeRpcLocal!.destroy()
|
|
716
556
|
if (this.entryPointDisconnectTimeout) {
|
|
717
557
|
clearTimeout(this.entryPointDisconnectTimeout)
|
|
718
558
|
}
|
|
719
|
-
this.bucket!.toArray().forEach((rpcRemote: DhtNodeRpcRemote) => {
|
|
720
|
-
rpcRemote.leaveNotice()
|
|
721
|
-
this.bucket!.remove(rpcRemote.id)
|
|
722
|
-
})
|
|
723
|
-
this.bucket!.removeAllListeners()
|
|
724
559
|
this.localDataStore.clear()
|
|
725
|
-
this.
|
|
726
|
-
this.randomPeers!.stop()
|
|
560
|
+
this.peerManager?.stop()
|
|
727
561
|
this.rpcCommunicator!.stop()
|
|
728
562
|
this.router!.stop()
|
|
729
563
|
this.finder!.stop()
|
|
730
564
|
this.peerDiscovery!.stop()
|
|
731
565
|
if (this.config.transport === undefined) {
|
|
732
|
-
// if the transport was not given in config, the instance was created in start() and
|
|
566
|
+
// if the transport was not given in config, the instance was created in start() and
|
|
733
567
|
// this component is responsible for stopping it
|
|
734
568
|
await this.transport!.stop()
|
|
735
569
|
}
|
|
736
570
|
this.transport = undefined
|
|
737
571
|
this.connectionManager = undefined
|
|
738
|
-
this.connections.clear()
|
|
739
572
|
this.removeAllListeners()
|
|
740
573
|
}
|
|
574
|
+
|
|
575
|
+
private createDhtNodeRpcRemote(peerDescriptor: PeerDescriptor) {
|
|
576
|
+
return new DhtNodeRpcRemote(
|
|
577
|
+
this.localPeerDescriptor!,
|
|
578
|
+
peerDescriptor,
|
|
579
|
+
toProtoRpcClient(new DhtNodeRpcClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
580
|
+
this.config.serviceId,
|
|
581
|
+
this.config.rpcRequestTimeout
|
|
582
|
+
)
|
|
583
|
+
}
|
|
741
584
|
}
|