@streamr/dht 0.0.1-tatum.6 → 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 -40
- package/dist/src/connection/ConnectionManager.js +138 -204
- 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 +15 -51
- package/dist/src/dht/DhtNode.js +107 -129
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/{DhtPeer.d.ts → RemoteDhtNode.d.ts} +2 -3
- package/dist/src/dht/{DhtPeer.js → RemoteDhtNode.js} +21 -19
- package/dist/src/dht/RemoteDhtNode.js.map +1 -0
- package/dist/src/dht/contact/ContactList.d.ts +0 -1
- package/dist/src/dht/contact/ContactList.js +0 -3
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +0 -1
- package/dist/src/dht/contact/RandomContactList.js +0 -3
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +0 -3
- package/dist/src/dht/contact/SortedContactList.js +0 -9
- 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 +1 -1
- 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 -4
- package/dist/src/dht/routing/RoutingSession.js +4 -3
- 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 -7
- package/dist/src/exports.js +2 -14
- 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 +157 -254
- 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 +155 -181
- package/src/dht/{DhtPeer.ts → RemoteDhtNode.ts} +11 -9
- package/src/dht/contact/ContactList.ts +0 -4
- package/src/dht/contact/RandomContactList.ts +0 -4
- package/src/dht/contact/SortedContactList.ts +0 -12
- 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 +2 -2
- package/src/dht/routing/Router.ts +35 -39
- package/src/dht/routing/RoutingSession.ts +9 -9
- package/src/dht/store/DataStore.ts +11 -11
- package/src/exports.ts +1 -7
- 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 +4 -2
- package/test/benchmark/RecursiveFind.test.ts +6 -6
- package/test/end-to-end/Layer0-Layer1.test.ts +9 -9
- 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 +1 -1
- package/test/unit/RecursiveFinder.test.ts +8 -12
- package/test/unit/Router.test.ts +18 -21
- 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,67 +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 nodeType =
|
|
146
|
-
const ret: PeerDescriptor = { kademliaId,
|
|
123
|
+
const nodeType = isBrowserEnvironment() ? NodeType.BROWSER : NodeType.NODEJS
|
|
124
|
+
const ret: PeerDescriptor = { kademliaId, type: nodeType }
|
|
147
125
|
if (msg && msg.websocket) {
|
|
148
126
|
ret.websocket = { host: msg.websocket.host, port: msg.websocket.port, tls: msg.websocket.tls }
|
|
149
127
|
ret.openInternet = true
|
|
@@ -152,17 +130,16 @@ export const createPeerDescriptor = (msg?: ConnectivityResponse, peerId?: string
|
|
|
152
130
|
}
|
|
153
131
|
|
|
154
132
|
export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
155
|
-
private readonly config: DhtNodeConfig
|
|
156
133
|
|
|
157
|
-
private
|
|
158
|
-
private
|
|
159
|
-
private
|
|
160
|
-
private
|
|
161
|
-
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>
|
|
162
140
|
private rpcCommunicator?: RoutingRpcCommunicator
|
|
163
|
-
private
|
|
141
|
+
private transport?: ITransport
|
|
164
142
|
private ownPeerDescriptor?: PeerDescriptor
|
|
165
|
-
private ownPeerId?: PeerID
|
|
166
143
|
public router?: Router
|
|
167
144
|
public dataStore?: DataStore
|
|
168
145
|
private localDataStore = new LocalDataStore()
|
|
@@ -174,12 +151,25 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
174
151
|
private stopped = false
|
|
175
152
|
private entryPointDisconnectTimeout?: NodeJS.Timeout
|
|
176
153
|
|
|
177
|
-
|
|
178
|
-
public contactOnAddedCounter = 0
|
|
179
|
-
|
|
180
|
-
constructor(conf: Partial<DhtNodeConfig>) {
|
|
154
|
+
constructor(conf: DhtNodeOptions) {
|
|
181
155
|
super()
|
|
182
|
-
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
|
+
}
|
|
183
173
|
this.send = this.send.bind(this)
|
|
184
174
|
}
|
|
185
175
|
|
|
@@ -190,68 +180,70 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
190
180
|
logger.trace(`Starting new Streamr Network DHT Node with serviceId ${this.config.serviceId}`)
|
|
191
181
|
this.started = true
|
|
192
182
|
|
|
193
|
-
if (
|
|
183
|
+
if (isBrowserEnvironment()) {
|
|
194
184
|
this.config.websocketPortRange = undefined
|
|
195
185
|
if (this.config.peerDescriptor) {
|
|
196
186
|
this.config.peerDescriptor.websocket = undefined
|
|
197
187
|
}
|
|
198
188
|
}
|
|
199
|
-
// If
|
|
200
|
-
if (this.config.
|
|
201
|
-
this.
|
|
202
|
-
this.ownPeerDescriptor = this.
|
|
203
|
-
if (this.config.
|
|
204
|
-
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
|
|
205
195
|
}
|
|
206
196
|
} else {
|
|
207
|
-
const
|
|
208
|
-
|
|
197
|
+
const connectorFacadeConfig: DefaultConnectorFacadeConfig = {
|
|
198
|
+
transport: this,
|
|
209
199
|
entryPoints: this.config.entryPoints,
|
|
210
200
|
iceServers: this.config.iceServers,
|
|
211
|
-
metricsContext: this.config.metricsContext,
|
|
212
201
|
webrtcAllowPrivateAddresses: this.config.webrtcAllowPrivateAddresses,
|
|
213
202
|
webrtcDatachannelBufferThresholdLow: this.config.webrtcDatachannelBufferThresholdLow,
|
|
214
203
|
webrtcDatachannelBufferThresholdHigh: this.config.webrtcDatachannelBufferThresholdHigh,
|
|
215
204
|
webrtcNewConnectionTimeout: this.config.webrtcNewConnectionTimeout,
|
|
216
205
|
webrtcPortRange: this.config.webrtcPortRange,
|
|
217
|
-
|
|
206
|
+
maxMessageSize: this.config.maxMessageSize,
|
|
218
207
|
tlsCertificate: this.config.tlsCertificate,
|
|
219
|
-
externalIp: this.config.externalIp
|
|
208
|
+
externalIp: this.config.externalIp,
|
|
209
|
+
createOwnPeerDescriptor: (connectivityResponse: ConnectivityResponse) => this.generatePeerDescriptorCallBack(connectivityResponse),
|
|
220
210
|
}
|
|
221
211
|
// If own PeerDescriptor is given in config, create a ConnectionManager with ws server
|
|
222
212
|
if (this.config.peerDescriptor?.websocket) {
|
|
223
|
-
|
|
224
|
-
|
|
213
|
+
connectorFacadeConfig.websocketHost = this.config.peerDescriptor.websocket.host
|
|
214
|
+
connectorFacadeConfig.websocketPortRange = {
|
|
225
215
|
min: this.config.peerDescriptor.websocket.port,
|
|
226
216
|
max: this.config.peerDescriptor.websocket.port
|
|
227
217
|
}
|
|
228
218
|
// If websocketPortRange is given, create ws server using it, websocketHost can be undefined
|
|
229
219
|
} else if (this.config.websocketPortRange) {
|
|
230
|
-
|
|
231
|
-
|
|
220
|
+
connectorFacadeConfig.websocketHost = this.config.websocketHost
|
|
221
|
+
connectorFacadeConfig.websocketPortRange = this.config.websocketPortRange
|
|
232
222
|
}
|
|
233
223
|
|
|
234
|
-
const connectionManager = new ConnectionManager(
|
|
235
|
-
|
|
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()
|
|
236
230
|
this.connectionManager = connectionManager
|
|
237
|
-
this.
|
|
231
|
+
this.transport = connectionManager
|
|
238
232
|
}
|
|
239
233
|
|
|
240
234
|
this.rpcCommunicator = new RoutingRpcCommunicator(
|
|
241
235
|
this.config.serviceId,
|
|
242
|
-
this.
|
|
236
|
+
this.transport.send,
|
|
243
237
|
{ rpcRequestTimeout: this.config.rpcRequestTimeout }
|
|
244
238
|
)
|
|
245
239
|
|
|
246
|
-
this.
|
|
240
|
+
this.transport.on('message', (message: Message) => this.handleMessage(message))
|
|
247
241
|
|
|
248
242
|
this.bindDefaultServerMethods()
|
|
249
|
-
this.
|
|
250
|
-
this.initKBuckets(this.ownPeerId)
|
|
243
|
+
this.initKBuckets(peerIdFromPeerDescriptor(this.ownPeerDescriptor!))
|
|
251
244
|
this.peerDiscovery = new PeerDiscovery({
|
|
252
245
|
rpcCommunicator: this.rpcCommunicator,
|
|
253
246
|
ownPeerDescriptor: this.ownPeerDescriptor!,
|
|
254
|
-
ownPeerId: this.ownPeerId,
|
|
255
247
|
bucket: this.bucket!,
|
|
256
248
|
connections: this.connections,
|
|
257
249
|
neighborList: this.neighborList!,
|
|
@@ -269,7 +261,6 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
269
261
|
rpcCommunicator: this.rpcCommunicator,
|
|
270
262
|
connections: this.connections,
|
|
271
263
|
ownPeerDescriptor: this.ownPeerDescriptor!,
|
|
272
|
-
ownPeerId: this.ownPeerId,
|
|
273
264
|
addContact: this.addNewContact.bind(this),
|
|
274
265
|
serviceId: this.config.serviceId,
|
|
275
266
|
connectionManager: this.connectionManager
|
|
@@ -281,7 +272,6 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
281
272
|
connections: this.connections,
|
|
282
273
|
ownPeerDescriptor: this.ownPeerDescriptor!,
|
|
283
274
|
serviceId: this.config.serviceId,
|
|
284
|
-
ownPeerId: this.ownPeerId,
|
|
285
275
|
addContact: this.addNewContact.bind(this),
|
|
286
276
|
isPeerCloserToIdThanSelf: this.isPeerCloserToIdThanSelf.bind(this),
|
|
287
277
|
localDataStore: this.localDataStore
|
|
@@ -302,31 +292,31 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
302
292
|
})
|
|
303
293
|
registerExternalApiRpcMethods(this)
|
|
304
294
|
if (this.connectionManager! && this.config.entryPoints && this.config.entryPoints.length > 0
|
|
305
|
-
&& !
|
|
295
|
+
&& !areEqualPeerDescriptors(this.config.entryPoints[0], this.ownPeerDescriptor!)) {
|
|
306
296
|
this.connectToEntryPoint(this.config.entryPoints[0])
|
|
307
297
|
}
|
|
308
298
|
}
|
|
309
299
|
|
|
310
300
|
private initKBuckets = (selfId: PeerID) => {
|
|
311
|
-
this.bucket = new KBucket<
|
|
301
|
+
this.bucket = new KBucket<RemoteDhtNode>({
|
|
312
302
|
localNodeId: selfId.value,
|
|
313
303
|
numberOfNodesPerKBucket: this.config.numberOfNodesPerKBucket,
|
|
314
304
|
numberOfNodesToPing: this.config.numberOfNodesPerKBucket
|
|
315
305
|
})
|
|
316
|
-
this.bucket.on('ping', (oldContacts:
|
|
317
|
-
this.bucket.on('removed', (contact:
|
|
318
|
-
this.bucket.on('added', (contact:
|
|
319
|
-
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', () => {
|
|
320
310
|
// TODO: Update contact info to the connection manager and reconnect
|
|
321
311
|
})
|
|
322
312
|
this.neighborList = new SortedContactList(selfId, this.config.maxNeighborListSize)
|
|
323
|
-
this.neighborList.on('contactRemoved', (removedContact:
|
|
313
|
+
this.neighborList.on('contactRemoved', (removedContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) => {
|
|
324
314
|
if (this.stopped) {
|
|
325
315
|
return
|
|
326
316
|
}
|
|
327
317
|
this.emit('contactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
328
318
|
this.randomPeers!.addContact(
|
|
329
|
-
new
|
|
319
|
+
new RemoteDhtNode(
|
|
330
320
|
this.ownPeerDescriptor!,
|
|
331
321
|
removedContact.getPeerDescriptor(),
|
|
332
322
|
toProtoRpcClient(new DhtRpcServiceClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
@@ -334,80 +324,77 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
334
324
|
)
|
|
335
325
|
)
|
|
336
326
|
})
|
|
337
|
-
this.neighborList.on('newContact', (newContact:
|
|
327
|
+
this.neighborList.on('newContact', (newContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) =>
|
|
338
328
|
this.emit('newContact', newContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
339
329
|
)
|
|
340
330
|
this.openInternetPeers = new SortedContactList(selfId, this.config.maxNeighborListSize / 2)
|
|
341
|
-
this.openInternetPeers.on('contactRemoved', (removedContact:
|
|
331
|
+
this.openInternetPeers.on('contactRemoved', (removedContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) =>
|
|
342
332
|
this.emit('openInternetContactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
343
333
|
)
|
|
344
|
-
this.openInternetPeers.on('newContact', (newContact:
|
|
334
|
+
this.openInternetPeers.on('newContact', (newContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) =>
|
|
345
335
|
this.emit('newOpenInternetContact', newContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
346
336
|
)
|
|
347
|
-
this.
|
|
337
|
+
this.transport!.on('connected', (peerDescriptor: PeerDescriptor) => this.onTransportConnected(peerDescriptor))
|
|
348
338
|
|
|
349
|
-
this.
|
|
339
|
+
this.transport!.on('disconnected', (peerDescriptor: PeerDescriptor, disonnectionType: DisconnectionType) => {
|
|
350
340
|
this.onTransportDisconnected(peerDescriptor, disonnectionType)
|
|
351
341
|
})
|
|
352
342
|
|
|
353
|
-
this.
|
|
354
|
-
const
|
|
355
|
-
const dhtPeer = new DhtPeer(
|
|
343
|
+
this.transport!.getAllConnectionPeerDescriptors().forEach((peer) => {
|
|
344
|
+
const remoteDhtNode = new RemoteDhtNode(
|
|
356
345
|
this.ownPeerDescriptor!,
|
|
357
346
|
peer,
|
|
358
347
|
toProtoRpcClient(new DhtRpcServiceClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
359
348
|
this.config.serviceId
|
|
360
349
|
)
|
|
361
|
-
if (
|
|
350
|
+
if (areEqualPeerDescriptors(peer, this.ownPeerDescriptor!)) {
|
|
362
351
|
logger.error('own peerdescriptor added to connections in initKBucket')
|
|
363
352
|
}
|
|
364
|
-
this.connections.set(
|
|
353
|
+
this.connections.set(keyFromPeerDescriptor(peer), remoteDhtNode)
|
|
365
354
|
})
|
|
366
355
|
this.randomPeers = new RandomContactList(selfId, this.config.maxNeighborListSize)
|
|
367
|
-
this.randomPeers.on('contactRemoved', (removedContact:
|
|
356
|
+
this.randomPeers.on('contactRemoved', (removedContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) =>
|
|
368
357
|
this.emit('randomContactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
369
358
|
)
|
|
370
|
-
this.randomPeers.on('newContact', (newContact:
|
|
359
|
+
this.randomPeers.on('newContact', (newContact: RemoteDhtNode, activeContacts: RemoteDhtNode[]) =>
|
|
371
360
|
this.emit('newRandomContact', newContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
372
361
|
)
|
|
373
362
|
}
|
|
374
363
|
|
|
375
364
|
private onTransportConnected(peerDescriptor: PeerDescriptor): void {
|
|
376
365
|
|
|
377
|
-
if (this.
|
|
366
|
+
if (areEqualPeerDescriptors(this.ownPeerDescriptor!, peerDescriptor)) {
|
|
378
367
|
logger.error('onTransportConnected() to self')
|
|
379
368
|
}
|
|
380
369
|
|
|
381
|
-
const
|
|
370
|
+
const remoteDhtNode = new RemoteDhtNode(
|
|
382
371
|
this.ownPeerDescriptor!,
|
|
383
372
|
peerDescriptor,
|
|
384
373
|
toProtoRpcClient(new DhtRpcServiceClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
385
374
|
this.config.serviceId
|
|
386
375
|
)
|
|
387
|
-
if (!this.connections.has(PeerID.fromValue(
|
|
388
|
-
this.connections.set(PeerID.fromValue(
|
|
389
|
-
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)
|
|
390
379
|
} else {
|
|
391
380
|
logger.trace('new connection not set to connections, there is already a connection with the peer ID')
|
|
392
381
|
}
|
|
393
|
-
|
|
394
|
-
logger.trace('connected: ' + this.ownPeerDescriptor!.nodeName + ', ' + peerDescriptor.nodeName + ' ' + this.connections.size)
|
|
395
|
-
}
|
|
382
|
+
logger.trace('connected: ' + keyFromPeerDescriptor(peerDescriptor) + ' ' + this.connections.size)
|
|
396
383
|
this.emit('connected', peerDescriptor)
|
|
397
384
|
}
|
|
398
385
|
|
|
399
386
|
private onTransportDisconnected(peerDescriptor: PeerDescriptor, dicsonnectionType: DisconnectionType): void {
|
|
400
|
-
logger.trace('disconnected: ' +
|
|
387
|
+
logger.trace('disconnected: ' + keyFromPeerDescriptor(peerDescriptor))
|
|
401
388
|
this.connections.delete(keyFromPeerDescriptor(peerDescriptor))
|
|
402
389
|
// only remove from bucket if we are on layer 0
|
|
403
390
|
if (this.connectionManager) {
|
|
404
391
|
this.bucket!.remove(peerDescriptor.kademliaId)
|
|
405
392
|
|
|
406
393
|
if (dicsonnectionType === 'OUTGOING_GRACEFUL_LEAVE' || dicsonnectionType === 'INCOMING_GRACEFUL_LEAVE') {
|
|
407
|
-
logger.trace(
|
|
394
|
+
logger.trace(keyFromPeerDescriptor(peerDescriptor) + ' ' + 'onTransportDisconnected with type ' + dicsonnectionType)
|
|
408
395
|
this.removeContact(peerDescriptor, true)
|
|
409
396
|
} else {
|
|
410
|
-
logger.trace(
|
|
397
|
+
logger.trace(keyFromPeerDescriptor(peerDescriptor) + ' ' + 'onTransportDisconnected with type ' + dicsonnectionType)
|
|
411
398
|
}
|
|
412
399
|
}
|
|
413
400
|
|
|
@@ -433,14 +420,13 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
433
420
|
return distance1 < distance2
|
|
434
421
|
}
|
|
435
422
|
|
|
436
|
-
|
|
423
|
+
private handleMessage(message: Message): void {
|
|
437
424
|
if (message.serviceId === this.config.serviceId) {
|
|
438
|
-
logger.trace('callig this.handleMessageFromPeer ' +
|
|
439
|
-
|
|
425
|
+
logger.trace('callig this.handleMessageFromPeer ' + keyFromPeerDescriptor(message.sourceDescriptor!)
|
|
426
|
+
+ ' ' + message.serviceId + ' ' + message.messageId)
|
|
440
427
|
this.rpcCommunicator?.handleMessageFromPeer(message)
|
|
441
428
|
} else {
|
|
442
|
-
logger.trace('emit "message" ' +
|
|
443
|
-
' ' + message.serviceId + ' ' + message.messageId)
|
|
429
|
+
logger.trace('emit "message" ' + keyFromPeerDescriptor(message.sourceDescriptor!) + ' ' + message.serviceId + ' ' + message.messageId)
|
|
444
430
|
this.emit('message', message)
|
|
445
431
|
}
|
|
446
432
|
}
|
|
@@ -449,23 +435,21 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
449
435
|
if (this.config.peerDescriptor) {
|
|
450
436
|
this.ownPeerDescriptor = this.config.peerDescriptor
|
|
451
437
|
} else {
|
|
452
|
-
this.ownPeerDescriptor = createPeerDescriptor(connectivityResponse,
|
|
453
|
-
this.config.peerId,
|
|
454
|
-
this.config.nodeName)
|
|
438
|
+
this.ownPeerDescriptor = createPeerDescriptor(connectivityResponse, this.config.peerId)
|
|
455
439
|
}
|
|
456
440
|
return this.ownPeerDescriptor
|
|
457
441
|
}
|
|
458
442
|
|
|
459
443
|
private getClosestPeerDescriptors(kademliaId: Uint8Array, limit: number): PeerDescriptor[] {
|
|
460
444
|
const closestPeers = this.bucket!.closest(kademliaId, limit)
|
|
461
|
-
return closestPeers.map((
|
|
445
|
+
return closestPeers.map((remoteDhtNode: RemoteDhtNode) => remoteDhtNode.getPeerDescriptor())
|
|
462
446
|
}
|
|
463
447
|
|
|
464
|
-
private onKBucketPing(oldContacts:
|
|
448
|
+
private onKBucketPing(oldContacts: RemoteDhtNode[], newContact: RemoteDhtNode): void {
|
|
465
449
|
if (this.stopped) {
|
|
466
450
|
return
|
|
467
451
|
}
|
|
468
|
-
const sortingList: SortedContactList<
|
|
452
|
+
const sortingList: SortedContactList<RemoteDhtNode> = new SortedContactList(this.getNodeId(), 100)
|
|
469
453
|
sortingList.addContacts(oldContacts)
|
|
470
454
|
const sortedContacts = sortingList.getAllContacts()
|
|
471
455
|
this.connectionManager?.weakUnlockConnection(sortedContacts[sortedContacts.length - 1].getPeerDescriptor())
|
|
@@ -473,12 +457,12 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
473
457
|
this.bucket!.add(newContact)
|
|
474
458
|
}
|
|
475
459
|
|
|
476
|
-
private onKBucketRemoved(contact:
|
|
460
|
+
private onKBucketRemoved(contact: RemoteDhtNode): void {
|
|
477
461
|
if (this.stopped) {
|
|
478
462
|
return
|
|
479
463
|
}
|
|
480
464
|
this.connectionManager?.weakUnlockConnection(contact.getPeerDescriptor())
|
|
481
|
-
logger.trace(`Removed contact ${contact.
|
|
465
|
+
logger.trace(`Removed contact ${keyFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
482
466
|
this.emit(
|
|
483
467
|
'kbucketContactRemoved',
|
|
484
468
|
contact.getPeerDescriptor()
|
|
@@ -496,33 +480,32 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
496
480
|
}
|
|
497
481
|
}
|
|
498
482
|
|
|
499
|
-
private onKBucketAdded(contact:
|
|
483
|
+
private onKBucketAdded(contact: RemoteDhtNode): void {
|
|
500
484
|
if (this.stopped) {
|
|
501
485
|
return
|
|
502
486
|
}
|
|
503
|
-
this.
|
|
504
|
-
if (!this.stopped && !contact.getPeerId().equals(this.ownPeerId!)) {
|
|
487
|
+
if (!this.stopped && !contact.getPeerId().equals(this.getNodeId())) {
|
|
505
488
|
// Important to lock here, before the ping result is known
|
|
506
489
|
this.connectionManager?.weakLockConnection(contact.getPeerDescriptor())
|
|
507
490
|
if (this.connections.has(contact.getPeerId().toKey())) {
|
|
508
|
-
logger.trace(`Added new contact ${contact.
|
|
491
|
+
logger.trace(`Added new contact ${keyFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
509
492
|
this.emit(
|
|
510
493
|
'newKbucketContact',
|
|
511
494
|
contact.getPeerDescriptor(),
|
|
512
495
|
this.neighborList!.getClosestContacts(this.config.getClosestContactsLimit).map((peer) => peer.getPeerDescriptor())
|
|
513
496
|
)
|
|
514
497
|
} else { // open connection by pinging
|
|
515
|
-
logger.trace('starting ping ' +
|
|
498
|
+
logger.trace('starting ping ' + keyFromPeerDescriptor(contact.getPeerDescriptor()))
|
|
516
499
|
contact.ping().then((result) => {
|
|
517
500
|
if (result) {
|
|
518
|
-
logger.trace(`Added new contact ${contact.
|
|
501
|
+
logger.trace(`Added new contact ${keyFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
519
502
|
this.emit(
|
|
520
503
|
'newKbucketContact',
|
|
521
504
|
contact.getPeerDescriptor(),
|
|
522
505
|
this.neighborList!.getClosestContacts(this.config.getClosestContactsLimit).map((peer) => peer.getPeerDescriptor())
|
|
523
506
|
)
|
|
524
507
|
} else {
|
|
525
|
-
logger.trace('ping failed ' +
|
|
508
|
+
logger.trace('ping failed ' + keyFromPeerDescriptor(contact.getPeerDescriptor()))
|
|
526
509
|
this.connectionManager?.weakUnlockConnection(contact.getPeerDescriptor())
|
|
527
510
|
this.removeContact(contact.getPeerDescriptor())
|
|
528
511
|
this.addClosestContactToBucket()
|
|
@@ -547,7 +530,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
547
530
|
}
|
|
548
531
|
}
|
|
549
532
|
|
|
550
|
-
private getClosestActiveContactNotInBucket():
|
|
533
|
+
private getClosestActiveContactNotInBucket(): RemoteDhtNode | undefined {
|
|
551
534
|
for (const contactId of this.neighborList!.getContactIds()) {
|
|
552
535
|
if (!this.bucket!.get(contactId.value) && this.neighborList!.isActive(contactId)) {
|
|
553
536
|
return this.neighborList!.getContact(contactId).contact
|
|
@@ -561,7 +544,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
561
544
|
}
|
|
562
545
|
|
|
563
546
|
public getNodeId(): PeerID {
|
|
564
|
-
return this.
|
|
547
|
+
return peerIdFromPeerDescriptor(this.ownPeerDescriptor!)
|
|
565
548
|
}
|
|
566
549
|
|
|
567
550
|
public getBucketSize(): number {
|
|
@@ -572,28 +555,27 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
572
555
|
if (!this.started || this.stopped) {
|
|
573
556
|
return
|
|
574
557
|
}
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
const dhtPeer = new DhtPeer(
|
|
558
|
+
if (!areEqualPeerDescriptors(contact, this.ownPeerDescriptor!)) {
|
|
559
|
+
logger.trace(`Adding new contact ${keyFromPeerDescriptor(contact)}`)
|
|
560
|
+
const remoteDhtNode = new RemoteDhtNode(
|
|
579
561
|
this.ownPeerDescriptor!,
|
|
580
562
|
contact,
|
|
581
563
|
toProtoRpcClient(new DhtRpcServiceClient(this.rpcCommunicator!.getRpcClientTransport())),
|
|
582
564
|
this.config.serviceId
|
|
583
565
|
)
|
|
584
566
|
if (!this.bucket!.get(contact.kademliaId) && !this.neighborList!.getContact(peerIdFromPeerDescriptor(contact))) {
|
|
585
|
-
this.neighborList!.addContact(
|
|
567
|
+
this.neighborList!.addContact(remoteDhtNode)
|
|
586
568
|
if (contact.openInternet) {
|
|
587
|
-
this.openInternetPeers!.addContact(
|
|
569
|
+
this.openInternetPeers!.addContact(remoteDhtNode)
|
|
588
570
|
}
|
|
589
571
|
if (setActive) {
|
|
572
|
+
const peerId = peerIdFromPeerDescriptor(contact)
|
|
590
573
|
this.neighborList!.setActive(peerId)
|
|
591
574
|
this.openInternetPeers!.setActive(peerId)
|
|
592
575
|
}
|
|
593
|
-
this.
|
|
594
|
-
this.bucket!.add(dhtPeer)
|
|
576
|
+
this.bucket!.add(remoteDhtNode)
|
|
595
577
|
} else {
|
|
596
|
-
this.randomPeers!.addContact(
|
|
578
|
+
this.randomPeers!.addContact(remoteDhtNode)
|
|
597
579
|
}
|
|
598
580
|
}
|
|
599
581
|
}
|
|
@@ -609,7 +591,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
609
591
|
if (!this.started || this.stopped) {
|
|
610
592
|
return
|
|
611
593
|
}
|
|
612
|
-
logger.trace(`Removing contact ${contact
|
|
594
|
+
logger.trace(`Removing contact ${keyFromPeerDescriptor(contact)}`)
|
|
613
595
|
const peerId = peerIdFromPeerDescriptor(contact)
|
|
614
596
|
this.bucket!.remove(peerId.value)
|
|
615
597
|
this.neighborList!.removeContact(peerId)
|
|
@@ -619,7 +601,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
619
601
|
}
|
|
620
602
|
}
|
|
621
603
|
|
|
622
|
-
public async send(msg: Message
|
|
604
|
+
public async send(msg: Message): Promise<void> {
|
|
623
605
|
if (!this.started || this.stopped) {
|
|
624
606
|
return
|
|
625
607
|
}
|
|
@@ -627,12 +609,12 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
627
609
|
await this.router!.send(msg, reachableThrough)
|
|
628
610
|
}
|
|
629
611
|
|
|
630
|
-
public async joinDht(entryPointDescriptors: PeerDescriptor[],
|
|
612
|
+
public async joinDht(entryPointDescriptors: PeerDescriptor[], doAdditionalRandomPeerDiscovery?: boolean, retry?: boolean): Promise<void> {
|
|
631
613
|
if (!this.started) {
|
|
632
614
|
throw new Error('Cannot join DHT before calling start() on DhtNode')
|
|
633
615
|
}
|
|
634
616
|
await Promise.all(entryPointDescriptors.map((entryPoint) =>
|
|
635
|
-
this.peerDiscovery!.joinDht(entryPoint,
|
|
617
|
+
this.peerDiscovery!.joinDht(entryPoint, doAdditionalRandomPeerDiscovery, retry)
|
|
636
618
|
))
|
|
637
619
|
}
|
|
638
620
|
|
|
@@ -641,7 +623,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
641
623
|
}
|
|
642
624
|
|
|
643
625
|
public async storeDataToDht(key: Uint8Array, data: Any): Promise<PeerDescriptor[]> {
|
|
644
|
-
if (this.isJoinOngoing() && this.config.entryPoints && this.config.entryPoints.length > 0) {
|
|
626
|
+
if (this.peerDiscovery!.isJoinOngoing() && this.config.entryPoints && this.config.entryPoints.length > 0) {
|
|
645
627
|
return this.storeDataViaPeer(key, data, sample(this.config.entryPoints)!)
|
|
646
628
|
}
|
|
647
629
|
return this.dataStore!.storeDataToDht(key, data)
|
|
@@ -657,8 +639,12 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
657
639
|
return await target.storeData(key, data)
|
|
658
640
|
}
|
|
659
641
|
|
|
660
|
-
public async getDataFromDht(idToFind: Uint8Array): Promise<
|
|
661
|
-
|
|
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 ?? []
|
|
662
648
|
}
|
|
663
649
|
|
|
664
650
|
public async deleteDataFromDht(idToDelete: Uint8Array): Promise<void> {
|
|
@@ -682,7 +668,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
682
668
|
}
|
|
683
669
|
|
|
684
670
|
public getTransport(): ITransport {
|
|
685
|
-
return this.
|
|
671
|
+
return this.transport!
|
|
686
672
|
}
|
|
687
673
|
|
|
688
674
|
public getPeerDescriptor(): PeerDescriptor {
|
|
@@ -693,16 +679,8 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
693
679
|
return Array.from(this.connections.values()).map((peer) => peer.getPeerDescriptor())
|
|
694
680
|
}
|
|
695
681
|
|
|
696
|
-
public getK(): number {
|
|
697
|
-
return this.config.numberOfNodesPerKBucket
|
|
698
|
-
}
|
|
699
|
-
|
|
700
682
|
public getKBucketPeers(): PeerDescriptor[] {
|
|
701
|
-
return this.bucket!.toArray().map((
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
public getOpenInternetPeerDescriptors(): PeerDescriptor[] {
|
|
705
|
-
return this.openInternetPeers!.getAllContacts().map((contact) => contact.getPeerDescriptor())
|
|
683
|
+
return this.bucket!.toArray().map((remoteDhtNode: RemoteDhtNode) => remoteDhtNode.getPeerDescriptor())
|
|
706
684
|
}
|
|
707
685
|
|
|
708
686
|
public getNumberOfConnections(): number {
|
|
@@ -721,18 +699,14 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
721
699
|
return this.connectionManager!.getNumberOfWeakLockedConnections()
|
|
722
700
|
}
|
|
723
701
|
|
|
724
|
-
public
|
|
725
|
-
|
|
702
|
+
public async waitForNetworkConnectivity(): Promise<void> {
|
|
703
|
+
await waitForCondition(() => this.connections.size > 0, this.config.networkConnectivityTimeout)
|
|
726
704
|
}
|
|
727
705
|
|
|
728
706
|
public hasJoined(): boolean {
|
|
729
707
|
return this.peerDiscovery!.isJoinCalled()
|
|
730
708
|
}
|
|
731
709
|
|
|
732
|
-
public getKnownEntryPoints(): PeerDescriptor[] {
|
|
733
|
-
return this.config.entryPoints || []
|
|
734
|
-
}
|
|
735
|
-
|
|
736
710
|
public async stop(): Promise<void> {
|
|
737
711
|
if (this.stopped || !this.started) {
|
|
738
712
|
return
|
|
@@ -743,7 +717,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
743
717
|
if (this.entryPointDisconnectTimeout) {
|
|
744
718
|
clearTimeout(this.entryPointDisconnectTimeout)
|
|
745
719
|
}
|
|
746
|
-
this.bucket!.toArray().
|
|
720
|
+
this.bucket!.toArray().forEach((remoteDhtNode: RemoteDhtNode) => this.bucket!.remove(remoteDhtNode.id))
|
|
747
721
|
this.bucket!.removeAllListeners()
|
|
748
722
|
this.localDataStore.clear()
|
|
749
723
|
this.neighborList!.stop()
|
|
@@ -756,7 +730,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
756
730
|
if (this.connectionManager) {
|
|
757
731
|
await this.connectionManager.stop()
|
|
758
732
|
}
|
|
759
|
-
this.
|
|
733
|
+
this.transport = undefined
|
|
760
734
|
this.connectionManager = undefined
|
|
761
735
|
this.connections.clear()
|
|
762
736
|
this.removeAllListeners()
|
|
@@ -774,7 +748,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
774
748
|
|
|
775
749
|
// IDHTRpcService implementation
|
|
776
750
|
private async ping(request: PingRequest, context: ServerCallContext): Promise<PingResponse> {
|
|
777
|
-
logger.trace('received ping request: ' +
|
|
751
|
+
logger.trace('received ping request: ' + keyFromPeerDescriptor((context as DhtCallContext).incomingSourceDescriptor!))
|
|
778
752
|
setImmediate(() => {
|
|
779
753
|
this.addNewContact((context as DhtCallContext).incomingSourceDescriptor!)
|
|
780
754
|
})
|
|
@@ -785,7 +759,7 @@ export class DhtNode extends EventEmitter<Events> implements ITransport {
|
|
|
785
759
|
}
|
|
786
760
|
|
|
787
761
|
// IDHTRpcService implementation
|
|
788
|
-
|
|
762
|
+
private async leaveNotice(request: LeaveNotice, context: ServerCallContext): Promise<Empty> {
|
|
789
763
|
// TODO check signature??
|
|
790
764
|
if (request.serviceId === this.config.serviceId) {
|
|
791
765
|
this.removeContact((context as DhtCallContext).incomingSourceDescriptor!)
|