@streamr/dht 0.0.1-tatum.5 → 0.0.1-tatum.7
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.d.ts +1 -1
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +17 -39
- package/dist/src/connection/ConnectionManager.js +138 -192
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectivityChecker.js +14 -11
- package/dist/src/connection/ConnectivityChecker.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +49 -0
- package/dist/src/connection/ConnectorFacade.js +86 -0
- package/dist/src/connection/ConnectorFacade.js.map +1 -0
- package/dist/src/connection/ManagedConnection.d.ts +1 -4
- package/dist/src/connection/ManagedConnection.js +23 -31
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/RemoteConnectionLocker.js +4 -3
- package/dist/src/connection/RemoteConnectionLocker.js.map +1 -1
- package/dist/src/connection/Simulator/Simulator.d.ts +0 -2
- package/dist/src/connection/Simulator/Simulator.js +0 -5
- package/dist/src/connection/Simulator/Simulator.js.map +1 -1
- package/dist/src/connection/Simulator/SimulatorConnection.js +16 -13
- package/dist/src/connection/Simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/Simulator/SimulatorConnector.d.ts +2 -2
- package/dist/src/connection/Simulator/SimulatorConnector.js +10 -11
- package/dist/src/connection/Simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/Simulator/SimulatorTransport.js +6 -1
- package/dist/src/connection/Simulator/SimulatorTransport.js.map +1 -1
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.d.ts +2 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js +12 -12
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js.map +1 -1
- package/dist/src/connection/WebRTC/WebRtcConnector.d.ts +9 -9
- package/dist/src/connection/WebRTC/WebRtcConnector.js +22 -22
- package/dist/src/connection/WebRTC/WebRtcConnector.js.map +1 -1
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js +2 -1
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js.map +1 -1
- package/dist/src/connection/WebSocket/WebSocketConnector.d.ts +19 -8
- package/dist/src/connection/WebSocket/WebSocketConnector.js +67 -46
- package/dist/src/connection/WebSocket/WebSocketConnector.js.map +1 -1
- package/dist/src/connection/WebSocket/WebSocketServer.d.ts +11 -1
- package/dist/src/connection/WebSocket/WebSocketServer.js +15 -10
- package/dist/src/connection/WebSocket/WebSocketServer.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +16 -54
- package/dist/src/dht/DhtNode.js +116 -137
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/{DhtPeer.d.ts → RemoteDhtNode.d.ts} +3 -2
- package/dist/src/dht/{DhtPeer.js → RemoteDhtNode.js} +22 -16
- package/dist/src/dht/RemoteDhtNode.js.map +1 -0
- package/dist/src/dht/contact/Contact.d.ts +1 -15
- package/dist/src/dht/contact/Contact.js +1 -9
- package/dist/src/dht/contact/Contact.js.map +1 -1
- package/dist/src/dht/contact/ContactList.d.ts +13 -3
- package/dist/src/dht/contact/ContactList.js +9 -4
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +3 -3
- package/dist/src/dht/contact/RandomContactList.js +4 -8
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/Remote.d.ts +1 -5
- package/dist/src/dht/contact/Remote.js +0 -5
- package/dist/src/dht/contact/Remote.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +3 -5
- package/dist/src/dht/contact/SortedContactList.js +9 -19
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +5 -7
- package/dist/src/dht/discovery/DiscoverySession.js +9 -10
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +11 -10
- package/dist/src/dht/discovery/PeerDiscovery.js +32 -37
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/find/RecursiveFindSession.d.ts +5 -6
- package/dist/src/dht/find/RecursiveFindSession.js +8 -8
- package/dist/src/dht/find/RecursiveFindSession.js.map +1 -1
- package/dist/src/dht/find/RecursiveFinder.d.ts +2 -4
- package/dist/src/dht/find/RecursiveFinder.js +11 -12
- package/dist/src/dht/find/RecursiveFinder.js.map +1 -1
- package/dist/src/dht/registerExternalApiRpcMethods.d.ts +1 -1
- package/dist/src/dht/routing/DuplicateDetector.d.ts +1 -2
- package/dist/src/dht/routing/DuplicateDetector.js +2 -7
- package/dist/src/dht/routing/DuplicateDetector.js.map +1 -1
- package/dist/src/dht/routing/RemoteRouter.js +4 -4
- package/dist/src/dht/routing/RemoteRouter.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +10 -13
- package/dist/src/dht/routing/Router.js +28 -29
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -5
- package/dist/src/dht/routing/RoutingSession.js +19 -13
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/DataStore.d.ts +2 -2
- package/dist/src/dht/store/DataStore.js +7 -7
- package/dist/src/dht/store/DataStore.js.map +1 -1
- package/dist/src/exports.d.ts +1 -8
- package/dist/src/exports.js +2 -16
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/PeerID.d.ts +0 -1
- package/dist/src/helpers/PeerID.js +0 -6
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/browser/isBrowserEnvironment.d.ts +1 -0
- package/dist/src/helpers/browser/isBrowserEnvironment.js +6 -0
- package/dist/src/helpers/browser/isBrowserEnvironment.js.map +1 -0
- package/dist/src/helpers/browser/isBrowserEnvironment_override.d.ts +1 -0
- package/dist/src/helpers/browser/isBrowserEnvironment_override.js +7 -0
- package/dist/src/helpers/browser/isBrowserEnvironment_override.js.map +1 -0
- package/dist/src/helpers/kademliaId.d.ts +1 -0
- package/dist/src/helpers/kademliaId.js +14 -0
- package/dist/src/helpers/kademliaId.js.map +1 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +1 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +3 -3
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +0 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +1 -2
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/package.json +10 -9
- package/protos/DhtRpc.proto +0 -1
- package/src/connection/ConnectionLockHandler.ts +1 -1
- package/src/connection/ConnectionManager.ts +156 -240
- package/src/connection/ConnectivityChecker.ts +14 -11
- package/src/connection/ConnectorFacade.ts +143 -0
- package/src/connection/ManagedConnection.ts +23 -34
- package/src/connection/RemoteConnectionLocker.ts +4 -3
- package/src/connection/Simulator/Simulator.ts +0 -7
- package/src/connection/Simulator/SimulatorConnection.ts +16 -13
- package/src/connection/Simulator/SimulatorConnector.ts +11 -12
- package/src/connection/Simulator/SimulatorTransport.ts +6 -1
- package/src/connection/WebRTC/NodeWebRtcConnection.ts +14 -13
- package/src/connection/WebRTC/WebRtcConnector.ts +31 -31
- package/src/connection/WebSocket/RemoteWebSocketConnector.ts +2 -1
- package/src/connection/WebSocket/WebSocketConnector.ts +85 -62
- package/src/connection/WebSocket/WebSocketServer.ts +26 -8
- package/src/dht/DhtNode.ts +164 -189
- package/src/dht/{DhtPeer.ts → RemoteDhtNode.ts} +14 -7
- package/src/dht/contact/Contact.ts +1 -18
- package/src/dht/contact/ContactList.ts +16 -6
- package/src/dht/contact/RandomContactList.ts +6 -11
- package/src/dht/contact/Remote.ts +1 -10
- package/src/dht/contact/SortedContactList.ts +12 -25
- package/src/dht/discovery/DiscoverySession.ts +20 -23
- package/src/dht/discovery/PeerDiscovery.ts +45 -44
- package/src/dht/find/RecursiveFindSession.ts +12 -13
- package/src/dht/find/RecursiveFinder.ts +16 -19
- package/src/dht/registerExternalApiRpcMethods.ts +1 -1
- package/src/dht/routing/DuplicateDetector.ts +3 -10
- package/src/dht/routing/RemoteRouter.ts +5 -5
- package/src/dht/routing/Router.ts +35 -39
- package/src/dht/routing/RoutingSession.ts +37 -28
- package/src/dht/store/DataStore.ts +11 -11
- package/src/exports.ts +1 -8
- package/src/helpers/PeerID.ts +0 -7
- package/src/helpers/browser/isBrowserEnvironment.ts +1 -0
- package/src/helpers/browser/isBrowserEnvironment_override.ts +3 -0
- package/src/helpers/kademliaId.ts +8 -0
- package/src/helpers/peerIdFromPeerDescriptor.ts +1 -1
- package/src/proto/packages/dht/protos/DhtRpc.ts +1 -6
- package/test/benchmark/KademliaCorrectness.test.ts +5 -2
- package/test/benchmark/RecursiveFind.test.ts +6 -6
- package/test/end-to-end/Layer0-Layer1.test.ts +14 -14
- package/test/end-to-end/Layer0WebRTC-Layer1.test.ts +5 -5
- package/test/end-to-end/Layer0WebRTC.test.ts +5 -6
- package/test/end-to-end/Layer1-Scale-WebRTC.test.ts +13 -8
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +15 -10
- package/test/end-to-end/WebSocketConnectionRequest.test.ts +5 -5
- package/test/integration/ConnectionLocking.test.ts +32 -26
- package/test/integration/ConnectionManager.test.ts +90 -93
- package/test/integration/DhtJoinPeerDiscovery.test.ts +53 -0
- package/test/integration/DhtRpc.test.ts +4 -6
- package/test/integration/Layer1-scale.test.ts +8 -8
- package/test/integration/MigrateData.test.ts +9 -9
- package/test/integration/Mock-Layer1-Layer0.test.ts +1 -2
- package/test/integration/RecursiveFind.test.ts +5 -5
- package/test/integration/{DhtPeer.test.ts → RemoteDhtNode.test.ts} +11 -12
- package/test/integration/RemoteRouter.test.ts +5 -6
- package/test/integration/RemoteStore.test.ts +4 -5
- package/test/integration/RouteMessage.test.ts +7 -9
- package/test/integration/RpcErrors.test.ts +25 -10
- package/test/integration/ScaleDownDht.test.ts +8 -8
- package/test/integration/SimultaneousConnections.test.ts +35 -36
- package/test/integration/Store.test.ts +8 -9
- package/test/integration/StoreAndDelete.test.ts +11 -11
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +7 -7
- package/test/integration/WebRtcConnectionManagement.test.ts +26 -19
- package/test/integration/WebRtcConnectorRpc.test.ts +6 -8
- package/test/integration/WebSocket.test.ts +4 -2
- package/test/integration/WebSocketConnectionManagement.test.ts +30 -17
- package/test/integration/WebSocketConnectorRpc.test.ts +2 -3
- package/test/unit/DuplicateDetector.test.ts +3 -4
- package/test/unit/LocalDataStore.test.ts +6 -8
- package/test/unit/RandomContactList.test.ts +25 -74
- package/test/unit/RecursiveFinder.test.ts +8 -12
- package/test/unit/Router.test.ts +18 -21
- package/test/unit/SortedContactList.test.ts +62 -112
- package/test/unit/WebSocketConnector.test.ts +64 -0
- package/test/unit/WebSocketServer.test.ts +24 -12
- package/test/utils/mock/RecursiveFinder.ts +2 -2
- package/test/utils/mock/Router.ts +9 -11
- package/test/utils/mock/Transport.ts +2 -2
- package/test/utils/utils.ts +40 -49
- package/dist/src/dht/DhtPeer.js.map +0 -1
- package/dist/src/helpers/browser/isBrowser.d.ts +0 -1
- package/dist/src/helpers/browser/isBrowser.js +0 -6
- package/dist/src/helpers/browser/isBrowser.js.map +0 -1
- package/dist/src/helpers/browser/isNodeJS.d.ts +0 -1
- package/dist/src/helpers/browser/isNodeJS.js +0 -6
- package/dist/src/helpers/browser/isNodeJS.js.map +0 -1
- package/src/helpers/browser/isBrowser.ts +0 -1
- package/src/helpers/browser/isNodeJS.ts +0 -1
- package/test/integration/DhtWithMockConnectionLatencies.test.ts +0 -46
- package/test/integration/DhtWithMockConnections.test.ts +0 -46
- package/test/integration/DhtWithRealConnectionLatencies.test.ts +0 -47
- /package/test/unit/{webrtcReplaceInternalIpWithExternalIp.ts → webrtcReplaceInternalIpWithExternalIp.test.ts} +0 -0
package/src/dht/DhtNode.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RemoteDhtNode } from './RemoteDhtNode'
|
|
2
2
|
import KBucket from 'k-bucket'
|
|
3
3
|
import { EventEmitter } from 'eventemitter3'
|
|
4
4
|
import { SortedContactList } from './contact/SortedContactList'
|
|
@@ -19,20 +19,20 @@ import {
|
|
|
19
19
|
DataEntry,
|
|
20
20
|
} from '../proto/packages/dht/protos/DhtRpc'
|
|
21
21
|
import { DisconnectionType, ITransport, TransportEvents } from '../transport/ITransport'
|
|
22
|
-
import { ConnectionManager,
|
|
22
|
+
import { ConnectionManager, PortRange, TlsCertificate } from '../connection/ConnectionManager'
|
|
23
23
|
import { DhtRpcServiceClient, ExternalApiServiceClient } from '../proto/packages/dht/protos/DhtRpc.client'
|
|
24
24
|
import {
|
|
25
25
|
Logger,
|
|
26
26
|
MetricsContext,
|
|
27
27
|
hexToBinary,
|
|
28
|
-
|
|
28
|
+
waitForCondition
|
|
29
29
|
} from '@streamr/utils'
|
|
30
30
|
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
31
31
|
import { RandomContactList } from './contact/RandomContactList'
|
|
32
32
|
import { Empty } from '../proto/google/protobuf/empty'
|
|
33
33
|
import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
|
|
34
34
|
import { Any } from '../proto/google/protobuf/any'
|
|
35
|
-
import {
|
|
35
|
+
import { areEqualPeerDescriptors, keyFromPeerDescriptor, peerIdFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
36
36
|
import { Router } from './routing/Router'
|
|
37
37
|
import { RecursiveFinder, RecursiveFindResult } from './find/RecursiveFinder'
|
|
38
38
|
import { DataStore } from './store/DataStore'
|
|
@@ -41,9 +41,11 @@ import { LocalDataStore } from './store/LocalDataStore'
|
|
|
41
41
|
import { IceServer } from '../connection/WebRTC/WebRtcConnector'
|
|
42
42
|
import { registerExternalApiRpcMethods } from './registerExternalApiRpcMethods'
|
|
43
43
|
import { RemoteExternalApi } from './RemoteExternalApi'
|
|
44
|
-
import { UUID } from '../
|
|
45
|
-
import {
|
|
44
|
+
import { UUID } from '../helpers/UUID'
|
|
45
|
+
import { isBrowserEnvironment } from '../helpers/browser/isBrowserEnvironment'
|
|
46
46
|
import { sample } from 'lodash'
|
|
47
|
+
import { DefaultConnectorFacade, DefaultConnectorFacadeConfig } from '../connection/ConnectorFacade'
|
|
48
|
+
import { MarkRequired } from 'ts-essentials'
|
|
47
49
|
|
|
48
50
|
export interface DhtNodeEvents {
|
|
49
51
|
newContact: (peerDescriptor: PeerDescriptor, closestPeers: PeerDescriptor[]) => void
|
|
@@ -63,19 +65,21 @@ export interface DhtNodeOptions {
|
|
|
63
65
|
maxNeighborListSize?: number
|
|
64
66
|
numberOfNodesPerKBucket?: number
|
|
65
67
|
joinNoProgressLimit?: number
|
|
68
|
+
getClosestContactsLimit?: number // TODO better name?
|
|
66
69
|
dhtJoinTimeout?: number
|
|
67
70
|
metricsContext?: MetricsContext
|
|
68
71
|
storeHighestTtl?: number
|
|
69
72
|
storeMaxTtl?: number
|
|
73
|
+
networkConnectivityTimeout?: number
|
|
74
|
+
storeNumberOfCopies?: number // TODO better name?
|
|
70
75
|
|
|
71
|
-
|
|
76
|
+
transport?: ITransport
|
|
72
77
|
peerDescriptor?: PeerDescriptor
|
|
73
78
|
entryPoints?: PeerDescriptor[]
|
|
74
79
|
websocketHost?: string
|
|
75
80
|
websocketPortRange?: PortRange
|
|
76
81
|
peerId?: string
|
|
77
82
|
|
|
78
|
-
nodeName?: string
|
|
79
83
|
rpcRequestTimeout?: number
|
|
80
84
|
iceServers?: IceServer[]
|
|
81
85
|
webrtcAllowPrivateAddresses?: boolean
|
|
@@ -83,66 +87,41 @@ export interface DhtNodeOptions {
|
|
|
83
87
|
webrtcDatachannelBufferThresholdHigh?: number
|
|
84
88
|
webrtcNewConnectionTimeout?: number
|
|
85
89
|
webrtcPortRange?: PortRange
|
|
90
|
+
maxMessageSize?: number
|
|
86
91
|
maxConnections?: number
|
|
87
92
|
tlsCertificate?: TlsCertificate
|
|
88
93
|
externalIp?: string
|
|
89
94
|
}
|
|
90
95
|
|
|
91
|
-
|
|
92
|
-
serviceId
|
|
93
|
-
joinParallelism
|
|
94
|
-
maxNeighborListSize
|
|
95
|
-
numberOfNodesPerKBucket
|
|
96
|
-
joinNoProgressLimit
|
|
97
|
-
dhtJoinTimeout
|
|
98
|
-
getClosestContactsLimit
|
|
99
|
-
maxConnections
|
|
100
|
-
storeHighestTtl
|
|
101
|
-
storeMaxTtl
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
transportLayer?: ITransport
|
|
107
|
-
peerDescriptor?: PeerDescriptor
|
|
108
|
-
entryPoints?: PeerDescriptor[]
|
|
109
|
-
websocketHost?: string
|
|
110
|
-
websocketPortRange?: PortRange
|
|
111
|
-
nodeName?: string
|
|
112
|
-
rpcRequestTimeout?: number
|
|
113
|
-
iceServers?: IceServer[]
|
|
114
|
-
webrtcAllowPrivateAddresses?: boolean
|
|
115
|
-
webrtcDatachannelBufferThresholdLow?: number
|
|
116
|
-
webrtcDatachannelBufferThresholdHigh?: number
|
|
117
|
-
webrtcNewConnectionTimeout?: number
|
|
118
|
-
externalIp?: string
|
|
119
|
-
webrtcPortRange?: PortRange
|
|
120
|
-
tlsCertificate?: TlsCertificate
|
|
121
|
-
|
|
122
|
-
constructor(conf: Partial<DhtNodeOptions>) {
|
|
123
|
-
// assign given non-undefined config vars over defaults
|
|
124
|
-
let k: keyof typeof conf
|
|
125
|
-
for (k in conf) {
|
|
126
|
-
if (conf[k] === undefined) {
|
|
127
|
-
delete conf[k]
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
Object.assign(this, conf)
|
|
131
|
-
}
|
|
132
|
-
}
|
|
96
|
+
type StrictDhtNodeOptions = MarkRequired<DhtNodeOptions,
|
|
97
|
+
'serviceId' |
|
|
98
|
+
'joinParallelism' |
|
|
99
|
+
'maxNeighborListSize' |
|
|
100
|
+
'numberOfNodesPerKBucket' |
|
|
101
|
+
'joinNoProgressLimit' |
|
|
102
|
+
'dhtJoinTimeout' |
|
|
103
|
+
'getClosestContactsLimit' |
|
|
104
|
+
'maxConnections' |
|
|
105
|
+
'storeHighestTtl' |
|
|
106
|
+
'storeMaxTtl' |
|
|
107
|
+
'networkConnectivityTimeout' |
|
|
108
|
+
'storeNumberOfCopies' |
|
|
109
|
+
'metricsContext' |
|
|
110
|
+
'peerId'>
|
|
133
111
|
|
|
134
112
|
const logger = new Logger(module)
|
|
135
113
|
|
|
136
114
|
export type Events = TransportEvents & DhtNodeEvents
|
|
137
115
|
|
|
138
|
-
export const createPeerDescriptor = (msg?: ConnectivityResponse, peerId?: string
|
|
116
|
+
export const createPeerDescriptor = (msg?: ConnectivityResponse, peerId?: string): PeerDescriptor => {
|
|
139
117
|
let kademliaId: Uint8Array
|
|
140
118
|
if (msg) {
|
|
141
119
|
kademliaId = peerId ? hexToBinary(peerId) : PeerID.fromIp(msg.host).value
|
|
142
120
|
} else {
|
|
143
121
|
kademliaId = hexToBinary(peerId!)
|
|
144
122
|
}
|
|
145
|
-
const
|
|
123
|
+
const nodeType = isBrowserEnvironment() ? NodeType.BROWSER : NodeType.NODEJS
|
|
124
|
+
const ret: PeerDescriptor = { kademliaId, type: nodeType }
|
|
146
125
|
if (msg && msg.websocket) {
|
|
147
126
|
ret.websocket = { host: msg.websocket.host, port: msg.websocket.port, tls: msg.websocket.tls }
|
|
148
127
|
ret.openInternet = true
|
|
@@ -151,17 +130,16 @@ export const createPeerDescriptor = (msg?: ConnectivityResponse, peerId?: string
|
|
|
151
130
|
}
|
|
152
131
|
|
|
153
132
|
export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
154
|
-
private readonly config: DhtNodeConfig
|
|
155
133
|
|
|
156
|
-
private
|
|
157
|
-
private
|
|
158
|
-
private
|
|
159
|
-
private
|
|
160
|
-
private
|
|
134
|
+
private readonly config: StrictDhtNodeOptions
|
|
135
|
+
private bucket?: KBucket<RemoteDhtNode>
|
|
136
|
+
private connections: Map<PeerIDKey, RemoteDhtNode> = new Map()
|
|
137
|
+
private neighborList?: SortedContactList<RemoteDhtNode>
|
|
138
|
+
private openInternetPeers?: SortedContactList<RemoteDhtNode>
|
|
139
|
+
private randomPeers?: RandomContactList<RemoteDhtNode>
|
|
161
140
|
private rpcCommunicator?: RoutingRpcCommunicator
|
|
162
|
-
private
|
|
141
|
+
private transport?: ITransport
|
|
163
142
|
private ownPeerDescriptor?: PeerDescriptor
|
|
164
|
-
private ownPeerId?: PeerID
|
|
165
143
|
public router?: Router
|
|
166
144
|
public dataStore?: DataStore
|
|
167
145
|
private localDataStore = new LocalDataStore()
|
|
@@ -173,12 +151,25 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
173
151
|
private stopped = false
|
|
174
152
|
private entryPointDisconnectTimeout?: NodeJS.Timeout
|
|
175
153
|
|
|
176
|
-
|
|
177
|
-
public contactOnAddedCounter = 0
|
|
178
|
-
|
|
179
|
-
constructor(conf: Partial<DhtNodeConfig>) {
|
|
154
|
+
constructor(conf: DhtNodeOptions) {
|
|
180
155
|
super()
|
|
181
|
-
this.config =
|
|
156
|
+
this.config = {
|
|
157
|
+
serviceId: 'layer0',
|
|
158
|
+
joinParallelism: 3,
|
|
159
|
+
maxNeighborListSize: 200,
|
|
160
|
+
numberOfNodesPerKBucket: 8,
|
|
161
|
+
joinNoProgressLimit: 4,
|
|
162
|
+
dhtJoinTimeout: 60000,
|
|
163
|
+
getClosestContactsLimit: 5,
|
|
164
|
+
maxConnections: 80,
|
|
165
|
+
storeHighestTtl: 60000,
|
|
166
|
+
storeMaxTtl: 60000,
|
|
167
|
+
networkConnectivityTimeout: 10000,
|
|
168
|
+
storeNumberOfCopies: 5,
|
|
169
|
+
metricsContext: new MetricsContext(),
|
|
170
|
+
peerId: new UUID().toHex(),
|
|
171
|
+
...conf // TODO use merge() if we don't want that explicit undefined values override defaults?
|
|
172
|
+
}
|
|
182
173
|
this.send = this.send.bind(this)
|
|
183
174
|
}
|
|
184
175
|
|
|
@@ -189,68 +180,70 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
189
180
|
logger.trace(`Starting new Streamr Network DHT Node with serviceId ${this.config.serviceId}`)
|
|
190
181
|
this.started = true
|
|
191
182
|
|
|
192
|
-
if (
|
|
183
|
+
if (isBrowserEnvironment()) {
|
|
193
184
|
this.config.websocketPortRange = undefined
|
|
194
185
|
if (this.config.peerDescriptor) {
|
|
195
186
|
this.config.peerDescriptor.websocket = undefined
|
|
196
187
|
}
|
|
197
188
|
}
|
|
198
|
-
// If
|
|
199
|
-
if (this.config.
|
|
200
|
-
this.
|
|
201
|
-
this.ownPeerDescriptor = this.
|
|
202
|
-
if (this.config.
|
|
203
|
-
this.connectionManager = this.config.
|
|
189
|
+
// If transport is given, do not create a ConnectionManager
|
|
190
|
+
if (this.config.transport) {
|
|
191
|
+
this.transport = this.config.transport
|
|
192
|
+
this.ownPeerDescriptor = this.transport.getPeerDescriptor()
|
|
193
|
+
if (this.config.transport instanceof ConnectionManager) {
|
|
194
|
+
this.connectionManager = this.config.transport
|
|
204
195
|
}
|
|
205
196
|
} else {
|
|
206
|
-
const
|
|
207
|
-
|
|
197
|
+
const connectorFacadeConfig: DefaultConnectorFacadeConfig = {
|
|
198
|
+
transport: this,
|
|
208
199
|
entryPoints: this.config.entryPoints,
|
|
209
200
|
iceServers: this.config.iceServers,
|
|
210
|
-
metricsContext: this.config.metricsContext,
|
|
211
201
|
webrtcAllowPrivateAddresses: this.config.webrtcAllowPrivateAddresses,
|
|
212
202
|
webrtcDatachannelBufferThresholdLow: this.config.webrtcDatachannelBufferThresholdLow,
|
|
213
203
|
webrtcDatachannelBufferThresholdHigh: this.config.webrtcDatachannelBufferThresholdHigh,
|
|
214
204
|
webrtcNewConnectionTimeout: this.config.webrtcNewConnectionTimeout,
|
|
215
205
|
webrtcPortRange: this.config.webrtcPortRange,
|
|
216
|
-
|
|
206
|
+
maxMessageSize: this.config.maxMessageSize,
|
|
217
207
|
tlsCertificate: this.config.tlsCertificate,
|
|
218
|
-
externalIp: this.config.externalIp
|
|
208
|
+
externalIp: this.config.externalIp,
|
|
209
|
+
createOwnPeerDescriptor: (connectivityResponse: ConnectivityResponse) => this.generatePeerDescriptorCallBack(connectivityResponse),
|
|
219
210
|
}
|
|
220
211
|
// If own PeerDescriptor is given in config, create a ConnectionManager with ws server
|
|
221
212
|
if (this.config.peerDescriptor?.websocket) {
|
|
222
|
-
|
|
223
|
-
|
|
213
|
+
connectorFacadeConfig.websocketHost = this.config.peerDescriptor.websocket.host
|
|
214
|
+
connectorFacadeConfig.websocketPortRange = {
|
|
224
215
|
min: this.config.peerDescriptor.websocket.port,
|
|
225
216
|
max: this.config.peerDescriptor.websocket.port
|
|
226
217
|
}
|
|
227
218
|
// If websocketPortRange is given, create ws server using it, websocketHost can be undefined
|
|
228
219
|
} else if (this.config.websocketPortRange) {
|
|
229
|
-
|
|
230
|
-
|
|
220
|
+
connectorFacadeConfig.websocketHost = this.config.websocketHost
|
|
221
|
+
connectorFacadeConfig.websocketPortRange = this.config.websocketPortRange
|
|
231
222
|
}
|
|
232
223
|
|
|
233
|
-
const connectionManager = new ConnectionManager(
|
|
234
|
-
|
|
224
|
+
const connectionManager = new ConnectionManager({
|
|
225
|
+
createConnectorFacade: () => new DefaultConnectorFacade(connectorFacadeConfig),
|
|
226
|
+
maxConnections: this.config.maxConnections,
|
|
227
|
+
metricsContext: this.config.metricsContext
|
|
228
|
+
})
|
|
229
|
+
await connectionManager.start()
|
|
235
230
|
this.connectionManager = connectionManager
|
|
236
|
-
this.
|
|
231
|
+
this.transport = connectionManager
|
|
237
232
|
}
|
|
238
233
|
|
|
239
234
|
this.rpcCommunicator = new RoutingRpcCommunicator(
|
|
240
235
|
this.config.serviceId,
|
|
241
|
-
this.
|
|
236
|
+
this.transport.send,
|
|
242
237
|
{ rpcRequestTimeout: this.config.rpcRequestTimeout }
|
|
243
238
|
)
|
|
244
239
|
|
|
245
|
-
this.
|
|
240
|
+
this.transport.on('message', (message: Message) => this.handleMessage(message))
|
|
246
241
|
|
|
247
242
|
this.bindDefaultServerMethods()
|
|
248
|
-
this.
|
|
249
|
-
this.initKBuckets(this.ownPeerId)
|
|
243
|
+
this.initKBuckets(peerIdFromPeerDescriptor(this.ownPeerDescriptor!))
|
|
250
244
|
this.peerDiscovery = new PeerDiscovery({
|
|
251
245
|
rpcCommunicator: this.rpcCommunicator,
|
|
252
246
|
ownPeerDescriptor: this.ownPeerDescriptor!,
|
|
253
|
-
ownPeerId: this.ownPeerId,
|
|
254
247
|
bucket: this.bucket!,
|
|
255
248
|
connections: this.connections,
|
|
256
249
|
neighborList: this.neighborList!,
|
|
@@ -268,7 +261,6 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
268
261
|
rpcCommunicator: this.rpcCommunicator,
|
|
269
262
|
connections: this.connections,
|
|
270
263
|
ownPeerDescriptor: this.ownPeerDescriptor!,
|
|
271
|
-
ownPeerId: this.ownPeerId,
|
|
272
264
|
addContact: this.addNewContact.bind(this),
|
|
273
265
|
serviceId: this.config.serviceId,
|
|
274
266
|
connectionManager: this.connectionManager
|
|
@@ -280,7 +272,6 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
280
272
|
connections: this.connections,
|
|
281
273
|
ownPeerDescriptor: this.ownPeerDescriptor!,
|
|
282
274
|
serviceId: this.config.serviceId,
|
|
283
|
-
ownPeerId: this.ownPeerId,
|
|
284
275
|
addContact: this.addNewContact.bind(this),
|
|
285
276
|
isPeerCloserToIdThanSelf: this.isPeerCloserToIdThanSelf.bind(this),
|
|
286
277
|
localDataStore: this.localDataStore
|
|
@@ -301,112 +292,109 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
301
292
|
})
|
|
302
293
|
registerExternalApiRpcMethods(this)
|
|
303
294
|
if (this.connectionManager! && this.config.entryPoints && this.config.entryPoints.length > 0
|
|
304
|
-
&& !
|
|
295
|
+
&& !areEqualPeerDescriptors(this.config.entryPoints[0], this.ownPeerDescriptor!)) {
|
|
305
296
|
this.connectToEntryPoint(this.config.entryPoints[0])
|
|
306
297
|
}
|
|
307
298
|
}
|
|
308
299
|
|
|
309
300
|
private initKBuckets = (selfId: PeerID) => {
|
|
310
|
-
this.bucket = new KBucket<
|
|
301
|
+
this.bucket = new KBucket<RemoteDhtNode>({
|
|
311
302
|
localNodeId: selfId.value,
|
|
312
303
|
numberOfNodesPerKBucket: this.config.numberOfNodesPerKBucket,
|
|
313
304
|
numberOfNodesToPing: this.config.numberOfNodesPerKBucket
|
|
314
305
|
})
|
|
315
|
-
this.bucket.on('ping', (oldContacts:
|
|
316
|
-
this.bucket.on('removed', (contact:
|
|
317
|
-
this.bucket.on('added', (contact:
|
|
318
|
-
this.bucket.on('updated', (
|
|
306
|
+
this.bucket.on('ping', (oldContacts: RemoteDhtNode[], newContact: RemoteDhtNode) => this.onKBucketPing(oldContacts, newContact))
|
|
307
|
+
this.bucket.on('removed', (contact: RemoteDhtNode) => this.onKBucketRemoved(contact))
|
|
308
|
+
this.bucket.on('added', (contact: RemoteDhtNode) => this.onKBucketAdded(contact))
|
|
309
|
+
this.bucket.on('updated', () => {
|
|
319
310
|
// TODO: Update contact info to the connection manager and reconnect
|
|
320
311
|
})
|
|
321
312
|
this.neighborList = new SortedContactList(selfId, this.config.maxNeighborListSize)
|
|
322
|
-
this.neighborList.on('contactRemoved', (
|
|
313
|
+
this.neighborList.on('contactRemoved', (removedContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) => {
|
|
323
314
|
if (this.stopped) {
|
|
324
315
|
return
|
|
325
316
|
}
|
|
326
|
-
this.emit('contactRemoved',
|
|
317
|
+
this.emit('contactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
327
318
|
this.randomPeers!.addContact(
|
|
328
|
-
new
|
|
319
|
+
new RemoteDhtNode(
|
|
329
320
|
this.ownPeerDescriptor!,
|
|
330
|
-
|
|
321
|
+
removedContact.getPeerDescriptor(),
|
|
331
322
|
toProtoRpcClient(new DhtRpcServiceClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
332
323
|
this.config.serviceId
|
|
333
324
|
)
|
|
334
325
|
)
|
|
335
326
|
})
|
|
336
|
-
this.neighborList.on('newContact', (
|
|
337
|
-
this.emit('newContact',
|
|
327
|
+
this.neighborList.on('newContact', (newContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) =>
|
|
328
|
+
this.emit('newContact', newContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
338
329
|
)
|
|
339
330
|
this.openInternetPeers = new SortedContactList(selfId, this.config.maxNeighborListSize / 2)
|
|
340
|
-
this.openInternetPeers.on('contactRemoved', (
|
|
341
|
-
this.emit('openInternetContactRemoved',
|
|
331
|
+
this.openInternetPeers.on('contactRemoved', (removedContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) =>
|
|
332
|
+
this.emit('openInternetContactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
342
333
|
)
|
|
343
|
-
this.openInternetPeers.on('newContact', (
|
|
344
|
-
this.emit('newOpenInternetContact',
|
|
334
|
+
this.openInternetPeers.on('newContact', (newContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) =>
|
|
335
|
+
this.emit('newOpenInternetContact', newContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
345
336
|
)
|
|
346
|
-
this.
|
|
337
|
+
this.transport!.on('connected', (peerDescriptor: PeerDescriptor) => this.onTransportConnected(peerDescriptor))
|
|
347
338
|
|
|
348
|
-
this.
|
|
339
|
+
this.transport!.on('disconnected', (peerDescriptor: PeerDescriptor, disonnectionType: DisconnectionType) => {
|
|
349
340
|
this.onTransportDisconnected(peerDescriptor, disonnectionType)
|
|
350
341
|
})
|
|
351
342
|
|
|
352
|
-
this.
|
|
353
|
-
const
|
|
354
|
-
const dhtPeer = new DhtPeer(
|
|
343
|
+
this.transport!.getAllConnectionPeerDescriptors().forEach((peer) => {
|
|
344
|
+
const remoteDhtNode = new RemoteDhtNode(
|
|
355
345
|
this.ownPeerDescriptor!,
|
|
356
346
|
peer,
|
|
357
347
|
toProtoRpcClient(new DhtRpcServiceClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
358
348
|
this.config.serviceId
|
|
359
349
|
)
|
|
360
|
-
if (
|
|
350
|
+
if (areEqualPeerDescriptors(peer, this.ownPeerDescriptor!)) {
|
|
361
351
|
logger.error('own peerdescriptor added to connections in initKBucket')
|
|
362
352
|
}
|
|
363
|
-
this.connections.set(
|
|
353
|
+
this.connections.set(keyFromPeerDescriptor(peer), remoteDhtNode)
|
|
364
354
|
})
|
|
365
355
|
this.randomPeers = new RandomContactList(selfId, this.config.maxNeighborListSize)
|
|
366
|
-
this.randomPeers.on('contactRemoved', (
|
|
367
|
-
this.emit('randomContactRemoved',
|
|
356
|
+
this.randomPeers.on('contactRemoved', (removedContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) =>
|
|
357
|
+
this.emit('randomContactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
368
358
|
)
|
|
369
|
-
this.randomPeers.on('newContact', (
|
|
370
|
-
this.emit('newRandomContact',
|
|
359
|
+
this.randomPeers.on('newContact', (newContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) =>
|
|
360
|
+
this.emit('newRandomContact', newContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
371
361
|
)
|
|
372
362
|
}
|
|
373
363
|
|
|
374
364
|
private onTransportConnected(peerDescriptor: PeerDescriptor): void {
|
|
375
365
|
|
|
376
|
-
if (this.
|
|
366
|
+
if (areEqualPeerDescriptors(this.ownPeerDescriptor!, peerDescriptor)) {
|
|
377
367
|
logger.error('onTransportConnected() to self')
|
|
378
368
|
}
|
|
379
369
|
|
|
380
|
-
const
|
|
370
|
+
const remoteDhtNode = new RemoteDhtNode(
|
|
381
371
|
this.ownPeerDescriptor!,
|
|
382
372
|
peerDescriptor,
|
|
383
373
|
toProtoRpcClient(new DhtRpcServiceClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
384
374
|
this.config.serviceId
|
|
385
375
|
)
|
|
386
|
-
if (!this.connections.has(PeerID.fromValue(
|
|
387
|
-
this.connections.set(PeerID.fromValue(
|
|
388
|
-
logger.trace('
|
|
376
|
+
if (!this.connections.has(PeerID.fromValue(remoteDhtNode.id).toKey())) {
|
|
377
|
+
this.connections.set(PeerID.fromValue(remoteDhtNode.id).toKey(), remoteDhtNode)
|
|
378
|
+
logger.trace('connectionschange add ' + this.connections.size)
|
|
389
379
|
} else {
|
|
390
380
|
logger.trace('new connection not set to connections, there is already a connection with the peer ID')
|
|
391
381
|
}
|
|
392
|
-
|
|
393
|
-
logger.trace('connected: ' + this.ownPeerDescriptor!.nodeName + ', ' + peerDescriptor.nodeName + ' ' + this.connections.size)
|
|
394
|
-
}
|
|
382
|
+
logger.trace('connected: ' + keyFromPeerDescriptor(peerDescriptor) + ' ' + this.connections.size)
|
|
395
383
|
this.emit('connected', peerDescriptor)
|
|
396
384
|
}
|
|
397
385
|
|
|
398
386
|
private onTransportDisconnected(peerDescriptor: PeerDescriptor, dicsonnectionType: DisconnectionType): void {
|
|
399
|
-
logger.trace('disconnected: ' +
|
|
387
|
+
logger.trace('disconnected: ' + keyFromPeerDescriptor(peerDescriptor))
|
|
400
388
|
this.connections.delete(keyFromPeerDescriptor(peerDescriptor))
|
|
401
389
|
// only remove from bucket if we are on layer 0
|
|
402
390
|
if (this.connectionManager) {
|
|
403
391
|
this.bucket!.remove(peerDescriptor.kademliaId)
|
|
404
392
|
|
|
405
393
|
if (dicsonnectionType === 'OUTGOING_GRACEFUL_LEAVE' || dicsonnectionType === 'INCOMING_GRACEFUL_LEAVE') {
|
|
406
|
-
logger.trace(
|
|
394
|
+
logger.trace(keyFromPeerDescriptor(peerDescriptor) + ' ' + 'onTransportDisconnected with type ' + dicsonnectionType)
|
|
407
395
|
this.removeContact(peerDescriptor, true)
|
|
408
396
|
} else {
|
|
409
|
-
logger.trace(
|
|
397
|
+
logger.trace(keyFromPeerDescriptor(peerDescriptor) + ' ' + 'onTransportDisconnected with type ' + dicsonnectionType)
|
|
410
398
|
}
|
|
411
399
|
}
|
|
412
400
|
|
|
@@ -432,14 +420,13 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
432
420
|
return distance1 < distance2
|
|
433
421
|
}
|
|
434
422
|
|
|
435
|
-
|
|
423
|
+
private handleMessage(message: Message): void {
|
|
436
424
|
if (message.serviceId === this.config.serviceId) {
|
|
437
|
-
logger.trace('callig this.handleMessageFromPeer ' +
|
|
438
|
-
|
|
425
|
+
logger.trace('callig this.handleMessageFromPeer ' + keyFromPeerDescriptor(message.sourceDescriptor!)
|
|
426
|
+
+ ' ' + message.serviceId + ' ' + message.messageId)
|
|
439
427
|
this.rpcCommunicator?.handleMessageFromPeer(message)
|
|
440
428
|
} else {
|
|
441
|
-
logger.trace('emit "message" ' +
|
|
442
|
-
' ' + message.serviceId + ' ' + message.messageId)
|
|
429
|
+
logger.trace('emit "message" ' + keyFromPeerDescriptor(message.sourceDescriptor!) + ' ' + message.serviceId + ' ' + message.messageId)
|
|
443
430
|
this.emit('message', message)
|
|
444
431
|
}
|
|
445
432
|
}
|
|
@@ -448,23 +435,21 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
448
435
|
if (this.config.peerDescriptor) {
|
|
449
436
|
this.ownPeerDescriptor = this.config.peerDescriptor
|
|
450
437
|
} else {
|
|
451
|
-
this.ownPeerDescriptor = createPeerDescriptor(connectivityResponse,
|
|
452
|
-
this.config.peerId,
|
|
453
|
-
this.config.nodeName)
|
|
438
|
+
this.ownPeerDescriptor = createPeerDescriptor(connectivityResponse, this.config.peerId)
|
|
454
439
|
}
|
|
455
440
|
return this.ownPeerDescriptor
|
|
456
441
|
}
|
|
457
442
|
|
|
458
443
|
private getClosestPeerDescriptors(kademliaId: Uint8Array, limit: number): PeerDescriptor[] {
|
|
459
444
|
const closestPeers = this.bucket!.closest(kademliaId, limit)
|
|
460
|
-
return closestPeers.map((
|
|
445
|
+
return closestPeers.map((remoteDhtNode: RemoteDhtNode) => remoteDhtNode.getPeerDescriptor())
|
|
461
446
|
}
|
|
462
447
|
|
|
463
|
-
private onKBucketPing(oldContacts:
|
|
448
|
+
private onKBucketPing(oldContacts: RemoteDhtNode[], newContact: RemoteDhtNode): void {
|
|
464
449
|
if (this.stopped) {
|
|
465
450
|
return
|
|
466
451
|
}
|
|
467
|
-
const sortingList: SortedContactList<
|
|
452
|
+
const sortingList: SortedContactList<RemoteDhtNode> = new SortedContactList(this.getNodeId(), 100)
|
|
468
453
|
sortingList.addContacts(oldContacts)
|
|
469
454
|
const sortedContacts = sortingList.getAllContacts()
|
|
470
455
|
this.connectionManager?.weakUnlockConnection(sortedContacts[sortedContacts.length - 1].getPeerDescriptor())
|
|
@@ -472,12 +457,12 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
472
457
|
this.bucket!.add(newContact)
|
|
473
458
|
}
|
|
474
459
|
|
|
475
|
-
private onKBucketRemoved(contact:
|
|
460
|
+
private onKBucketRemoved(contact: RemoteDhtNode): void {
|
|
476
461
|
if (this.stopped) {
|
|
477
462
|
return
|
|
478
463
|
}
|
|
479
464
|
this.connectionManager?.weakUnlockConnection(contact.getPeerDescriptor())
|
|
480
|
-
logger.trace(`Removed contact ${contact.
|
|
465
|
+
logger.trace(`Removed contact ${keyFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
481
466
|
this.emit(
|
|
482
467
|
'kbucketContactRemoved',
|
|
483
468
|
contact.getPeerDescriptor()
|
|
@@ -495,33 +480,32 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
495
480
|
}
|
|
496
481
|
}
|
|
497
482
|
|
|
498
|
-
private onKBucketAdded(contact:
|
|
483
|
+
private onKBucketAdded(contact: RemoteDhtNode): void {
|
|
499
484
|
if (this.stopped) {
|
|
500
485
|
return
|
|
501
486
|
}
|
|
502
|
-
this.
|
|
503
|
-
if (!this.stopped && !contact.getPeerId().equals(this.ownPeerId!)) {
|
|
487
|
+
if (!this.stopped && !contact.getPeerId().equals(this.getNodeId())) {
|
|
504
488
|
// Important to lock here, before the ping result is known
|
|
505
489
|
this.connectionManager?.weakLockConnection(contact.getPeerDescriptor())
|
|
506
490
|
if (this.connections.has(contact.getPeerId().toKey())) {
|
|
507
|
-
logger.trace(`Added new contact ${contact.
|
|
491
|
+
logger.trace(`Added new contact ${keyFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
508
492
|
this.emit(
|
|
509
493
|
'newKbucketContact',
|
|
510
494
|
contact.getPeerDescriptor(),
|
|
511
495
|
this.neighborList!.getClosestContacts(this.config.getClosestContactsLimit).map((peer) => peer.getPeerDescriptor())
|
|
512
496
|
)
|
|
513
497
|
} else { // open connection by pinging
|
|
514
|
-
logger.trace('starting ping ' +
|
|
498
|
+
logger.trace('starting ping ' + keyFromPeerDescriptor(contact.getPeerDescriptor()))
|
|
515
499
|
contact.ping().then((result) => {
|
|
516
500
|
if (result) {
|
|
517
|
-
logger.trace(`Added new contact ${contact.
|
|
501
|
+
logger.trace(`Added new contact ${keyFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
518
502
|
this.emit(
|
|
519
503
|
'newKbucketContact',
|
|
520
504
|
contact.getPeerDescriptor(),
|
|
521
505
|
this.neighborList!.getClosestContacts(this.config.getClosestContactsLimit).map((peer) => peer.getPeerDescriptor())
|
|
522
506
|
)
|
|
523
507
|
} else {
|
|
524
|
-
logger.trace('ping failed ' +
|
|
508
|
+
logger.trace('ping failed ' + keyFromPeerDescriptor(contact.getPeerDescriptor()))
|
|
525
509
|
this.connectionManager?.weakUnlockConnection(contact.getPeerDescriptor())
|
|
526
510
|
this.removeContact(contact.getPeerDescriptor())
|
|
527
511
|
this.addClosestContactToBucket()
|
|
@@ -546,7 +530,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
546
530
|
}
|
|
547
531
|
}
|
|
548
532
|
|
|
549
|
-
private getClosestActiveContactNotInBucket():
|
|
533
|
+
private getClosestActiveContactNotInBucket(): RemoteDhtNode | undefined {
|
|
550
534
|
for (const contactId of this.neighborList!.getContactIds()) {
|
|
551
535
|
if (!this.bucket!.get(contactId.value) && this.neighborList!.isActive(contactId)) {
|
|
552
536
|
return this.neighborList!.getContact(contactId).contact
|
|
@@ -555,12 +539,12 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
555
539
|
return undefined
|
|
556
540
|
}
|
|
557
541
|
|
|
558
|
-
public
|
|
559
|
-
return this.neighborList
|
|
542
|
+
public getClosestContacts(maxCount?: number): PeerDescriptor[] {
|
|
543
|
+
return this.neighborList!.getClosestContacts(maxCount).map((c) => c.getPeerDescriptor())
|
|
560
544
|
}
|
|
561
545
|
|
|
562
546
|
public getNodeId(): PeerID {
|
|
563
|
-
return this.
|
|
547
|
+
return peerIdFromPeerDescriptor(this.ownPeerDescriptor!)
|
|
564
548
|
}
|
|
565
549
|
|
|
566
550
|
public getBucketSize(): number {
|
|
@@ -571,28 +555,27 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
571
555
|
if (!this.started || this.stopped) {
|
|
572
556
|
return
|
|
573
557
|
}
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
const dhtPeer = new DhtPeer(
|
|
558
|
+
if (!areEqualPeerDescriptors(contact, this.ownPeerDescriptor!)) {
|
|
559
|
+
logger.trace(`Adding new contact ${keyFromPeerDescriptor(contact)}`)
|
|
560
|
+
const remoteDhtNode = new RemoteDhtNode(
|
|
578
561
|
this.ownPeerDescriptor!,
|
|
579
562
|
contact,
|
|
580
563
|
toProtoRpcClient(new DhtRpcServiceClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
581
564
|
this.config.serviceId
|
|
582
565
|
)
|
|
583
566
|
if (!this.bucket!.get(contact.kademliaId) && !this.neighborList!.getContact(peerIdFromPeerDescriptor(contact))) {
|
|
584
|
-
this.neighborList!.addContact(
|
|
567
|
+
this.neighborList!.addContact(remoteDhtNode)
|
|
585
568
|
if (contact.openInternet) {
|
|
586
|
-
this.openInternetPeers!.addContact(
|
|
569
|
+
this.openInternetPeers!.addContact(remoteDhtNode)
|
|
587
570
|
}
|
|
588
571
|
if (setActive) {
|
|
572
|
+
const peerId = peerIdFromPeerDescriptor(contact)
|
|
589
573
|
this.neighborList!.setActive(peerId)
|
|
590
574
|
this.openInternetPeers!.setActive(peerId)
|
|
591
575
|
}
|
|
592
|
-
this.
|
|
593
|
-
this.bucket!.add(dhtPeer)
|
|
576
|
+
this.bucket!.add(remoteDhtNode)
|
|
594
577
|
} else {
|
|
595
|
-
this.randomPeers!.addContact(
|
|
578
|
+
this.randomPeers!.addContact(remoteDhtNode)
|
|
596
579
|
}
|
|
597
580
|
}
|
|
598
581
|
}
|
|
@@ -608,7 +591,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
608
591
|
if (!this.started || this.stopped) {
|
|
609
592
|
return
|
|
610
593
|
}
|
|
611
|
-
logger.trace(`Removing contact ${contact
|
|
594
|
+
logger.trace(`Removing contact ${keyFromPeerDescriptor(contact)}`)
|
|
612
595
|
const peerId = peerIdFromPeerDescriptor(contact)
|
|
613
596
|
this.bucket!.remove(peerId.value)
|
|
614
597
|
this.neighborList!.removeContact(peerId)
|
|
@@ -618,7 +601,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
618
601
|
}
|
|
619
602
|
}
|
|
620
603
|
|
|
621
|
-
public async send(msg: Message
|
|
604
|
+
public async send(msg: Message): Promise<void> {
|
|
622
605
|
if (!this.started || this.stopped) {
|
|
623
606
|
return
|
|
624
607
|
}
|
|
@@ -626,12 +609,12 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
626
609
|
await this.router!.send(msg, reachableThrough)
|
|
627
610
|
}
|
|
628
611
|
|
|
629
|
-
public async joinDht(entryPointDescriptors: PeerDescriptor[],
|
|
612
|
+
public async joinDht(entryPointDescriptors: PeerDescriptor[], doAdditionalRandomPeerDiscovery?: boolean, retry?: boolean): Promise<void> {
|
|
630
613
|
if (!this.started) {
|
|
631
614
|
throw new Error('Cannot join DHT before calling start() on DhtNode')
|
|
632
615
|
}
|
|
633
616
|
await Promise.all(entryPointDescriptors.map((entryPoint) =>
|
|
634
|
-
this.peerDiscovery!.joinDht(entryPoint,
|
|
617
|
+
this.peerDiscovery!.joinDht(entryPoint, doAdditionalRandomPeerDiscovery, retry)
|
|
635
618
|
))
|
|
636
619
|
}
|
|
637
620
|
|
|
@@ -640,7 +623,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
640
623
|
}
|
|
641
624
|
|
|
642
625
|
public async storeDataToDht(key: Uint8Array, data: Any): Promise<PeerDescriptor[]> {
|
|
643
|
-
if (this.isJoinOngoing() && this.config.entryPoints && this.config.entryPoints.length > 0) {
|
|
626
|
+
if (this.peerDiscovery!.isJoinOngoing() && this.config.entryPoints && this.config.entryPoints.length > 0) {
|
|
644
627
|
return this.storeDataViaPeer(key, data, sample(this.config.entryPoints)!)
|
|
645
628
|
}
|
|
646
629
|
return this.dataStore!.storeDataToDht(key, data)
|
|
@@ -656,8 +639,12 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
656
639
|
return await target.storeData(key, data)
|
|
657
640
|
}
|
|
658
641
|
|
|
659
|
-
public async getDataFromDht(idToFind: Uint8Array): Promise<
|
|
660
|
-
|
|
642
|
+
public async getDataFromDht(idToFind: Uint8Array): Promise<DataEntry[]> {
|
|
643
|
+
if (this.peerDiscovery!.isJoinOngoing() && this.config.entryPoints && this.config.entryPoints.length > 0) {
|
|
644
|
+
return this.findDataViaPeer(idToFind, sample(this.config.entryPoints)!)
|
|
645
|
+
}
|
|
646
|
+
const result = await this.recursiveFinder!.startRecursiveFind(idToFind, FindMode.DATA)
|
|
647
|
+
return result.dataEntries ?? []
|
|
661
648
|
}
|
|
662
649
|
|
|
663
650
|
public async deleteDataFromDht(idToDelete: Uint8Array): Promise<void> {
|
|
@@ -681,7 +668,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
681
668
|
}
|
|
682
669
|
|
|
683
670
|
public getTransport(): ITransport {
|
|
684
|
-
return this.
|
|
671
|
+
return this.transport!
|
|
685
672
|
}
|
|
686
673
|
|
|
687
674
|
public getPeerDescriptor(): PeerDescriptor {
|
|
@@ -692,16 +679,8 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
692
679
|
return Array.from(this.connections.values()).map((peer) => peer.getPeerDescriptor())
|
|
693
680
|
}
|
|
694
681
|
|
|
695
|
-
public getK(): number {
|
|
696
|
-
return this.config.numberOfNodesPerKBucket
|
|
697
|
-
}
|
|
698
|
-
|
|
699
682
|
public getKBucketPeers(): PeerDescriptor[] {
|
|
700
|
-
return this.bucket!.toArray().map((
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
public getOpenInternetPeerDescriptors(): PeerDescriptor[] {
|
|
704
|
-
return this.openInternetPeers!.getAllContacts().map((contact) => contact.getPeerDescriptor())
|
|
683
|
+
return this.bucket!.toArray().map((remoteDhtNode: RemoteDhtNode) => remoteDhtNode.getPeerDescriptor())
|
|
705
684
|
}
|
|
706
685
|
|
|
707
686
|
public getNumberOfConnections(): number {
|
|
@@ -720,18 +699,14 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
720
699
|
return this.connectionManager!.getNumberOfWeakLockedConnections()
|
|
721
700
|
}
|
|
722
701
|
|
|
723
|
-
public
|
|
724
|
-
|
|
702
|
+
public async waitForNetworkConnectivity(): Promise<void> {
|
|
703
|
+
await waitForCondition(() => this.connections.size > 0, this.config.networkConnectivityTimeout)
|
|
725
704
|
}
|
|
726
705
|
|
|
727
706
|
public hasJoined(): boolean {
|
|
728
707
|
return this.peerDiscovery!.isJoinCalled()
|
|
729
708
|
}
|
|
730
709
|
|
|
731
|
-
public getKnownEntryPoints(): PeerDescriptor[] {
|
|
732
|
-
return this.config.entryPoints || []
|
|
733
|
-
}
|
|
734
|
-
|
|
735
710
|
public async stop(): Promise<void> {
|
|
736
711
|
if (this.stopped || !this.started) {
|
|
737
712
|
return
|
|
@@ -742,7 +717,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
742
717
|
if (this.entryPointDisconnectTimeout) {
|
|
743
718
|
clearTimeout(this.entryPointDisconnectTimeout)
|
|
744
719
|
}
|
|
745
|
-
this.bucket!.toArray().
|
|
720
|
+
this.bucket!.toArray().forEach((remoteDhtNode: RemoteDhtNode) => this.bucket!.remove(remoteDhtNode.id))
|
|
746
721
|
this.bucket!.removeAllListeners()
|
|
747
722
|
this.localDataStore.clear()
|
|
748
723
|
this.neighborList!.stop()
|
|
@@ -755,7 +730,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
755
730
|
if (this.connectionManager) {
|
|
756
731
|
await this.connectionManager.stop()
|
|
757
732
|
}
|
|
758
|
-
this.
|
|
733
|
+
this.transport = undefined
|
|
759
734
|
this.connectionManager = undefined
|
|
760
735
|
this.connections.clear()
|
|
761
736
|
this.removeAllListeners()
|
|
@@ -773,7 +748,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
773
748
|
|
|
774
749
|
// IDHTRpcService implementation
|
|
775
750
|
private async ping(request: PingRequest, context: ServerCallContext): Promise<PingResponse> {
|
|
776
|
-
logger.trace('received ping request: ' +
|
|
751
|
+
logger.trace('received ping request: ' + keyFromPeerDescriptor((context as DhtCallContext).incomingSourceDescriptor!))
|
|
777
752
|
setImmediate(() => {
|
|
778
753
|
this.addNewContact((context as DhtCallContext).incomingSourceDescriptor!)
|
|
779
754
|
})
|
|
@@ -784,7 +759,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
784
759
|
}
|
|
785
760
|
|
|
786
761
|
// IDHTRpcService implementation
|
|
787
|
-
|
|
762
|
+
private async leaveNotice(request: LeaveNotice, context: ServerCallContext): Promise<Empty> {
|
|
788
763
|
// TODO check signature??
|
|
789
764
|
if (request.serviceId === this.config.serviceId) {
|
|
790
765
|
this.removeContact((context as DhtCallContext).incomingSourceDescriptor!)
|