@streamr/dht 100.0.0-testnet-two.1 → 100.0.0-testnet-two.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package.json +5 -5
- package/dist/src/connection/ConnectionLockHandler.d.ts +11 -11
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionManager.d.ts +5 -4
- package/dist/src/connection/ConnectionManager.js +32 -36
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +2 -2
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +21 -22
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.js +4 -3
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +11 -8
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.js +6 -5
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +9 -8
- package/dist/src/dht/DhtNode.js +26 -22
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +2 -1
- package/dist/src/dht/DhtNodeRpcLocal.js +2 -1
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +6 -5
- package/dist/src/dht/DhtNodeRpcRemote.js +9 -7
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +3 -3
- package/dist/src/dht/ExternalApiRpcLocal.js +3 -2
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +3 -2
- package/dist/src/dht/ExternalApiRpcRemote.js +3 -2
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +9 -9
- package/dist/src/dht/PeerManager.js +35 -30
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/Contact.d.ts +2 -2
- package/dist/src/dht/contact/ContactList.d.ts +7 -7
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +4 -4
- package/dist/src/dht/contact/RandomContactList.js +2 -3
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/RpcRemote.d.ts +1 -4
- package/dist/src/dht/contact/RpcRemote.js +1 -5
- package/dist/src/dht/contact/RpcRemote.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +13 -13
- package/dist/src/dht/contact/SortedContactList.js +5 -5
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +3 -2
- package/dist/src/dht/discovery/DiscoverySession.js +9 -9
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +3 -1
- package/dist/src/dht/discovery/PeerDiscovery.js +18 -10
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +15 -14
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js +2 -2
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +2 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +6 -6
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +2 -2
- package/dist/src/dht/routing/Router.js +2 -2
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.js +3 -3
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcRemote.js +4 -2
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -3
- package/dist/src/dht/routing/RoutingSession.js +4 -4
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +6 -9
- package/dist/src/dht/store/LocalDataStore.js +27 -32
- package/dist/src/dht/store/LocalDataStore.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +3 -3
- package/dist/src/dht/store/StoreManager.js +26 -23
- package/dist/src/dht/store/StoreManager.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.d.ts +2 -1
- package/dist/src/dht/store/StoreRpcLocal.js +10 -8
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/exports.d.ts +2 -0
- package/dist/src/exports.js +6 -1
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/AddressTools.js +2 -0
- package/dist/src/helpers/AddressTools.js.map +1 -1
- package/dist/src/helpers/PeerID.d.ts +2 -2
- package/dist/src/helpers/PeerID.js +3 -3
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +2 -2
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +2 -3
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/identifiers.d.ts +6 -0
- package/dist/src/identifiers.js +23 -0
- package/dist/src/identifiers.js.map +1 -0
- package/package.json +5 -5
- package/src/connection/ConnectionLockHandler.ts +15 -15
- package/src/connection/ConnectionLockRpcLocal.ts +3 -3
- package/src/connection/ConnectionManager.ts +36 -46
- package/src/connection/ManagedConnection.ts +2 -2
- package/src/connection/simulator/Simulator.ts +2 -2
- package/src/connection/simulator/SimulatorConnection.ts +21 -23
- package/src/connection/simulator/SimulatorConnector.ts +6 -5
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -4
- package/src/connection/webrtc/NodeWebrtcConnection.ts +11 -10
- package/src/connection/webrtc/WebrtcConnector.ts +2 -3
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +2 -3
- package/src/connection/websocket/WebsocketConnector.ts +9 -9
- package/src/dht/DhtNode.ts +36 -35
- package/src/dht/DhtNodeRpcLocal.ts +3 -2
- package/src/dht/DhtNodeRpcRemote.ts +14 -12
- package/src/dht/ExternalApiRpcLocal.ts +15 -6
- package/src/dht/ExternalApiRpcRemote.ts +5 -4
- package/src/dht/PeerManager.ts +43 -38
- package/src/dht/contact/Contact.ts +2 -2
- package/src/dht/contact/ContactList.ts +7 -7
- package/src/dht/contact/RandomContactList.ts +6 -6
- package/src/dht/contact/RpcRemote.ts +0 -8
- package/src/dht/contact/SortedContactList.ts +22 -22
- package/src/dht/discovery/DiscoverySession.ts +16 -14
- package/src/dht/discovery/PeerDiscovery.ts +34 -12
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +17 -17
- package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +2 -2
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +11 -11
- package/src/dht/routing/Router.ts +5 -5
- package/src/dht/routing/RouterRpcLocal.ts +3 -3
- package/src/dht/routing/RouterRpcRemote.ts +4 -4
- package/src/dht/routing/RoutingSession.ts +6 -8
- package/src/dht/store/LocalDataStore.ts +31 -40
- package/src/dht/store/StoreManager.ts +31 -31
- package/src/dht/store/StoreRpcLocal.ts +11 -9
- package/src/exports.ts +2 -0
- package/src/helpers/AddressTools.ts +2 -0
- package/src/helpers/PeerID.ts +4 -4
- package/src/helpers/peerIdFromPeerDescriptor.ts +4 -6
- package/src/identifiers.ts +20 -0
- package/test/RandomGraphSimulation.ts +3 -2
- package/test/benchmark/Find.test.ts +4 -3
- package/test/benchmark/KademliaCorrectness.test.ts +4 -4
- package/test/benchmark/SortedContactListBenchmark.test.ts +15 -14
- package/test/benchmark/kademlia-simulation/Contact.ts +7 -8
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +2 -2
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +10 -11
- package/test/data/generateGroundTruthData.ts +5 -4
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +2 -2
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +1 -1
- package/test/end-to-end/memory-leak.test.ts +3 -2
- package/test/integration/ConnectionManager.test.ts +3 -3
- package/test/integration/DhtJoinPeerDiscovery.test.ts +2 -1
- package/test/integration/DhtNodeExternalAPI.test.ts +7 -7
- package/test/integration/DhtNodeRpcRemote.test.ts +3 -2
- package/test/integration/Find.test.ts +3 -3
- package/test/integration/Layer1-scale.test.ts +3 -3
- package/test/integration/Mock-Layer1-Layer0.test.ts +6 -5
- package/test/integration/ReplicateData.test.ts +38 -30
- package/test/integration/RouteMessage.test.ts +9 -10
- package/test/integration/RouterRpcRemote.test.ts +1 -1
- package/test/integration/ScaleDownDht.test.ts +4 -4
- package/test/integration/SimultaneousConnections.test.ts +7 -14
- package/test/integration/Store.test.ts +17 -7
- package/test/integration/StoreAndDelete.test.ts +11 -10
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +7 -6
- package/test/integration/StoreRpcRemote.test.ts +3 -5
- package/test/unit/AddressTools.test.ts +4 -0
- package/test/unit/LocalDataStore.test.ts +41 -41
- package/test/unit/PeerManager.test.ts +7 -8
- package/test/unit/RandomContactList.test.ts +3 -3
- package/test/unit/RecursiveOperationManager.test.ts +2 -1
- package/test/unit/RecursiveOperationSession.test.ts +2 -3
- package/test/unit/Router.test.ts +2 -2
- package/test/unit/RoutingSession.test.ts +2 -2
- package/test/unit/SortedContactList.test.ts +6 -6
- package/test/unit/StoreManager.test.ts +26 -23
- package/test/utils/customMatchers.ts +3 -2
- package/test/utils/mock/mockDataEntry.ts +8 -6
- package/test/utils/utils.ts +4 -4
- package/dist/src/helpers/nodeId.d.ts +0 -6
- package/dist/src/helpers/nodeId.js +0 -31
- package/dist/src/helpers/nodeId.js.map +0 -1
- package/src/helpers/nodeId.ts +0 -28
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RpcCommunicator } from '@streamr/proto-rpc'
|
|
2
2
|
import { Logger } from '@streamr/utils'
|
|
3
3
|
import { v4 } from 'uuid'
|
|
4
|
-
import {
|
|
4
|
+
import { DhtAddress, DhtAddressRaw, getRawFromDhtAddress } from '../identifiers'
|
|
5
5
|
import { getNodeIdFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
6
6
|
import {
|
|
7
7
|
ClosestPeersRequest,
|
|
@@ -16,7 +16,7 @@ const logger = new Logger(module)
|
|
|
16
16
|
|
|
17
17
|
// Fields required by objects stored in the k-bucket library
|
|
18
18
|
export interface KBucketContact {
|
|
19
|
-
id:
|
|
19
|
+
id: DhtAddressRaw
|
|
20
20
|
vectorClock: number
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -24,7 +24,8 @@ export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBu
|
|
|
24
24
|
|
|
25
25
|
private static counter = 0
|
|
26
26
|
public vectorClock: number
|
|
27
|
-
public readonly id:
|
|
27
|
+
public readonly id: DhtAddressRaw
|
|
28
|
+
private readonly serviceId: ServiceID
|
|
28
29
|
|
|
29
30
|
constructor(
|
|
30
31
|
localPeerDescriptor: PeerDescriptor,
|
|
@@ -33,28 +34,29 @@ export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBu
|
|
|
33
34
|
rpcCommunicator: RpcCommunicator,
|
|
34
35
|
rpcRequestTimeout?: number
|
|
35
36
|
) {
|
|
36
|
-
super(localPeerDescriptor, peerDescriptor,
|
|
37
|
+
super(localPeerDescriptor, peerDescriptor, rpcCommunicator, DhtNodeRpcClient, rpcRequestTimeout)
|
|
37
38
|
this.id = this.getPeerDescriptor().nodeId
|
|
38
39
|
this.vectorClock = DhtNodeRpcRemote.counter++
|
|
40
|
+
this.serviceId = serviceId
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
async getClosestPeers(nodeId:
|
|
42
|
-
logger.trace(`Requesting getClosestPeers on ${this.
|
|
43
|
+
async getClosestPeers(nodeId: DhtAddress): Promise<PeerDescriptor[]> {
|
|
44
|
+
logger.trace(`Requesting getClosestPeers on ${this.serviceId} from ${this.getNodeId()}`)
|
|
43
45
|
const request: ClosestPeersRequest = {
|
|
44
|
-
nodeId,
|
|
46
|
+
nodeId: getRawFromDhtAddress(nodeId),
|
|
45
47
|
requestId: v4()
|
|
46
48
|
}
|
|
47
49
|
try {
|
|
48
50
|
const peers = await this.getClient().getClosestPeers(request, this.formDhtRpcOptions())
|
|
49
51
|
return peers.peers
|
|
50
52
|
} catch (err) {
|
|
51
|
-
logger.trace(`getClosestPeers error ${this.
|
|
53
|
+
logger.trace(`getClosestPeers error ${this.serviceId}`, { err })
|
|
52
54
|
throw err
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
async ping(): Promise<boolean> {
|
|
57
|
-
logger.trace(`Requesting ping on ${this.
|
|
59
|
+
logger.trace(`Requesting ping on ${this.serviceId} from ${this.getNodeId()}`)
|
|
58
60
|
const request: PingRequest = {
|
|
59
61
|
requestId: v4()
|
|
60
62
|
}
|
|
@@ -65,13 +67,13 @@ export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBu
|
|
|
65
67
|
return true
|
|
66
68
|
}
|
|
67
69
|
} catch (err) {
|
|
68
|
-
logger.trace(`ping failed on ${this.
|
|
70
|
+
logger.trace(`ping failed on ${this.serviceId} to ${this.getNodeId()}: ${err}`)
|
|
69
71
|
}
|
|
70
72
|
return false
|
|
71
73
|
}
|
|
72
74
|
|
|
73
75
|
leaveNotice(): void {
|
|
74
|
-
logger.trace(`Sending leaveNotice on ${this.
|
|
76
|
+
logger.trace(`Sending leaveNotice on ${this.serviceId} from ${this.getNodeId()}`)
|
|
75
77
|
const options = this.formDhtRpcOptions({
|
|
76
78
|
notification: true
|
|
77
79
|
})
|
|
@@ -80,7 +82,7 @@ export class DhtNodeRpcRemote extends RpcRemote<DhtNodeRpcClient> implements KBu
|
|
|
80
82
|
})
|
|
81
83
|
}
|
|
82
84
|
|
|
83
|
-
getNodeId():
|
|
85
|
+
getNodeId(): DhtAddress {
|
|
84
86
|
return getNodeIdFromPeerDescriptor(this.getPeerDescriptor())
|
|
85
87
|
}
|
|
86
88
|
}
|
|
@@ -11,19 +11,20 @@ import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
|
11
11
|
import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
|
|
12
12
|
import { RecursiveOperationResult } from './recursive-operation/RecursiveOperationManager'
|
|
13
13
|
import { Any } from '../proto/google/protobuf/any'
|
|
14
|
-
import {
|
|
14
|
+
import { DhtAddress } from '../identifiers'
|
|
15
15
|
import { getNodeIdFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
16
|
+
import { getDhtAddressFromRaw } from '../identifiers'
|
|
16
17
|
|
|
17
18
|
interface ExternalApiRpcLocalConfig {
|
|
18
19
|
executeRecursiveOperation: (
|
|
19
|
-
targetId:
|
|
20
|
+
targetId: DhtAddress,
|
|
20
21
|
operation: RecursiveOperation,
|
|
21
22
|
excludedPeer: PeerDescriptor
|
|
22
23
|
) => Promise<RecursiveOperationResult>
|
|
23
24
|
storeDataToDht: (
|
|
24
|
-
key:
|
|
25
|
+
key: DhtAddress,
|
|
25
26
|
data: Any,
|
|
26
|
-
creator:
|
|
27
|
+
creator: DhtAddress
|
|
27
28
|
) => Promise<PeerDescriptor[]>
|
|
28
29
|
}
|
|
29
30
|
|
|
@@ -37,13 +38,21 @@ export class ExternalApiRpcLocal implements IExternalApiRpc {
|
|
|
37
38
|
|
|
38
39
|
async externalFindData(findDataRequest: ExternalFindDataRequest, context: ServerCallContext): Promise<ExternalFindDataResponse> {
|
|
39
40
|
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
40
|
-
const result = await this.config.executeRecursiveOperation(
|
|
41
|
+
const result = await this.config.executeRecursiveOperation(
|
|
42
|
+
getDhtAddressFromRaw(findDataRequest.key),
|
|
43
|
+
RecursiveOperation.FETCH_DATA,
|
|
44
|
+
senderPeerDescriptor
|
|
45
|
+
)
|
|
41
46
|
return ExternalFindDataResponse.create({ entries: result.dataEntries ?? [] })
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
async externalStoreData(request: ExternalStoreDataRequest, context: ServerCallContext): Promise<ExternalStoreDataResponse> {
|
|
45
50
|
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
46
|
-
const result = await this.config.storeDataToDht(
|
|
51
|
+
const result = await this.config.storeDataToDht(
|
|
52
|
+
getDhtAddressFromRaw(request.key),
|
|
53
|
+
request.data!,
|
|
54
|
+
getNodeIdFromPeerDescriptor(senderPeerDescriptor)
|
|
55
|
+
)
|
|
47
56
|
return ExternalStoreDataResponse.create({
|
|
48
57
|
storers: result
|
|
49
58
|
})
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DhtAddress, getRawFromDhtAddress } from '../identifiers'
|
|
1
2
|
import { Any } from '../proto/google/protobuf/any'
|
|
2
3
|
import { DataEntry, ExternalFindDataRequest, ExternalStoreDataRequest, PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
|
|
3
4
|
import { ExternalApiRpcClient } from '../proto/packages/dht/protos/DhtRpc.client'
|
|
@@ -5,9 +6,9 @@ import { RpcRemote } from './contact/RpcRemote'
|
|
|
5
6
|
|
|
6
7
|
export class ExternalApiRpcRemote extends RpcRemote<ExternalApiRpcClient> {
|
|
7
8
|
|
|
8
|
-
async externalFindData(key:
|
|
9
|
+
async externalFindData(key: DhtAddress): Promise<DataEntry[]> {
|
|
9
10
|
const request: ExternalFindDataRequest = {
|
|
10
|
-
key
|
|
11
|
+
key: getRawFromDhtAddress(key)
|
|
11
12
|
}
|
|
12
13
|
const options = this.formDhtRpcOptions({
|
|
13
14
|
// TODO use config option or named constant?
|
|
@@ -21,9 +22,9 @@ export class ExternalApiRpcRemote extends RpcRemote<ExternalApiRpcClient> {
|
|
|
21
22
|
}
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
async storeData(key:
|
|
25
|
+
async storeData(key: DhtAddress, data: Any): Promise<PeerDescriptor[]> {
|
|
25
26
|
const request: ExternalStoreDataRequest = {
|
|
26
|
-
key,
|
|
27
|
+
key: getRawFromDhtAddress(key),
|
|
27
28
|
data
|
|
28
29
|
}
|
|
29
30
|
const options = this.formDhtRpcOptions({
|
package/src/dht/PeerManager.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
Logger
|
|
2
|
+
Logger
|
|
3
3
|
} from '@streamr/utils'
|
|
4
4
|
import KBucket from 'k-bucket'
|
|
5
5
|
import {
|
|
@@ -13,7 +13,7 @@ import { RandomContactList } from './contact/RandomContactList'
|
|
|
13
13
|
import { SortedContactList } from './contact/SortedContactList'
|
|
14
14
|
import { ConnectionManager } from '../connection/ConnectionManager'
|
|
15
15
|
import EventEmitter from 'eventemitter3'
|
|
16
|
-
import {
|
|
16
|
+
import { DhtAddress, DhtAddressRaw, getRawFromDhtAddress } from '../identifiers'
|
|
17
17
|
|
|
18
18
|
const logger = new Logger(module)
|
|
19
19
|
|
|
@@ -21,7 +21,7 @@ interface PeerManagerConfig {
|
|
|
21
21
|
numberOfNodesPerKBucket: number
|
|
22
22
|
maxContactListSize: number
|
|
23
23
|
peerDiscoveryQueryBatchSize: number
|
|
24
|
-
localNodeId:
|
|
24
|
+
localNodeId: DhtAddress
|
|
25
25
|
connectionManager: ConnectionManager
|
|
26
26
|
isLayer0: boolean
|
|
27
27
|
createDhtNodeRpcRemote: (peerDescriptor: PeerDescriptor) => DhtNodeRpcRemote
|
|
@@ -35,8 +35,8 @@ export interface PeerManagerEvents {
|
|
|
35
35
|
kBucketEmpty: () => void
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
export const getDistance = (
|
|
39
|
-
return KBucket.distance(
|
|
38
|
+
export const getDistance = (nodeIdOrDataKeyRaw1: DhtAddressRaw, nodeIdOrDataKeyRaw2: DhtAddressRaw): number => {
|
|
39
|
+
return KBucket.distance(nodeIdOrDataKeyRaw1, nodeIdOrDataKeyRaw2)
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
@@ -50,7 +50,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
50
50
|
// The kademlia k-bucket
|
|
51
51
|
private bucket: KBucket<DhtNodeRpcRemote>
|
|
52
52
|
// Nodes that are connected to this node on Layer0
|
|
53
|
-
public readonly connections: Map<
|
|
53
|
+
public readonly connections: Map<DhtAddress, DhtNodeRpcRemote> = new Map()
|
|
54
54
|
// All nodes that we know about
|
|
55
55
|
private contacts: SortedContactList<DhtNodeRpcRemote>
|
|
56
56
|
private randomPeers: RandomContactList<DhtNodeRpcRemote>
|
|
@@ -61,7 +61,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
61
61
|
super()
|
|
62
62
|
this.config = config
|
|
63
63
|
this.bucket = new KBucket<DhtNodeRpcRemote>({
|
|
64
|
-
localNodeId:
|
|
64
|
+
localNodeId: getRawFromDhtAddress(this.config.localNodeId),
|
|
65
65
|
numberOfNodesPerKBucket: this.config.numberOfNodesPerKBucket,
|
|
66
66
|
numberOfNodesToPing: this.config.numberOfNodesPerKBucket
|
|
67
67
|
})
|
|
@@ -108,8 +108,9 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
108
108
|
})
|
|
109
109
|
sortingList.addContacts(oldContacts)
|
|
110
110
|
const sortedContacts = sortingList.getAllContacts()
|
|
111
|
-
|
|
112
|
-
this.
|
|
111
|
+
const removableNodeId = sortedContacts[sortedContacts.length - 1].getNodeId()
|
|
112
|
+
this.config.connectionManager?.weakUnlockConnection(removableNodeId)
|
|
113
|
+
this.bucket.remove(getRawFromDhtAddress(removableNodeId))
|
|
113
114
|
this.bucket.add(newContact)
|
|
114
115
|
}
|
|
115
116
|
|
|
@@ -117,8 +118,9 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
117
118
|
if (this.stopped) {
|
|
118
119
|
return
|
|
119
120
|
}
|
|
120
|
-
|
|
121
|
-
|
|
121
|
+
const nodeId = getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())
|
|
122
|
+
this.config.connectionManager?.weakUnlockConnection(nodeId)
|
|
123
|
+
logger.trace(`Removed contact ${nodeId}`)
|
|
122
124
|
if (this.bucket.count() === 0) {
|
|
123
125
|
this.emit('kBucketEmpty')
|
|
124
126
|
}
|
|
@@ -128,26 +130,28 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
128
130
|
if (this.stopped) {
|
|
129
131
|
return
|
|
130
132
|
}
|
|
131
|
-
if (
|
|
133
|
+
if (contact.getNodeId() !== this.config.localNodeId) {
|
|
134
|
+
const peerDescriptor = contact.getPeerDescriptor()
|
|
135
|
+
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
132
136
|
// Important to lock here, before the ping result is known
|
|
133
|
-
this.config.connectionManager?.weakLockConnection(
|
|
137
|
+
this.config.connectionManager?.weakLockConnection(nodeId)
|
|
134
138
|
if (this.connections.has(contact.getNodeId())) {
|
|
135
|
-
logger.trace(`Added new contact ${
|
|
139
|
+
logger.trace(`Added new contact ${nodeId}`)
|
|
136
140
|
} else { // open connection by pinging
|
|
137
|
-
logger.trace('starting ping ' +
|
|
141
|
+
logger.trace('starting ping ' + nodeId)
|
|
138
142
|
contact.ping().then((result) => {
|
|
139
143
|
if (result) {
|
|
140
|
-
logger.trace(`Added new contact ${
|
|
144
|
+
logger.trace(`Added new contact ${nodeId}`)
|
|
141
145
|
} else {
|
|
142
|
-
logger.trace('ping failed ' +
|
|
143
|
-
this.config.connectionManager?.weakUnlockConnection(
|
|
144
|
-
this.removeContact(
|
|
146
|
+
logger.trace('ping failed ' + nodeId)
|
|
147
|
+
this.config.connectionManager?.weakUnlockConnection(nodeId)
|
|
148
|
+
this.removeContact(peerDescriptor)
|
|
145
149
|
this.addClosestContactToBucket()
|
|
146
150
|
}
|
|
147
151
|
return
|
|
148
152
|
}).catch((_e) => {
|
|
149
|
-
this.config.connectionManager?.weakUnlockConnection(
|
|
150
|
-
this.removeContact(
|
|
153
|
+
this.config.connectionManager?.weakUnlockConnection(nodeId)
|
|
154
|
+
this.removeContact(peerDescriptor)
|
|
151
155
|
this.addClosestContactToBucket()
|
|
152
156
|
})
|
|
153
157
|
}
|
|
@@ -166,7 +170,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
166
170
|
|
|
167
171
|
private getClosestActiveContactNotInBucket(): DhtNodeRpcRemote | undefined {
|
|
168
172
|
for (const contactId of this.contacts.getContactIds()) {
|
|
169
|
-
if (!this.bucket.get(
|
|
173
|
+
if (!this.bucket.get(getRawFromDhtAddress(contactId)) && this.contacts.isActive(contactId)) {
|
|
170
174
|
return this.contacts.getContact(contactId)!.contact
|
|
171
175
|
}
|
|
172
176
|
}
|
|
@@ -175,7 +179,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
175
179
|
|
|
176
180
|
handleConnected(peerDescriptor: PeerDescriptor): void {
|
|
177
181
|
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
178
|
-
if (
|
|
182
|
+
if (nodeId === this.config.localNodeId) {
|
|
179
183
|
logger.error('handleConnected() to self')
|
|
180
184
|
}
|
|
181
185
|
const rpcRemote = this.config.createDhtNodeRpcRemote(peerDescriptor)
|
|
@@ -185,19 +189,20 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
185
189
|
} else {
|
|
186
190
|
logger.trace('new connection not set to connections, there is already a connection with the peer ID')
|
|
187
191
|
}
|
|
188
|
-
logger.trace('connected: ' +
|
|
192
|
+
logger.trace('connected: ' + nodeId + ' ' + this.connections.size)
|
|
189
193
|
}
|
|
190
194
|
|
|
191
195
|
handleDisconnected(peerDescriptor: PeerDescriptor, gracefulLeave: boolean): void {
|
|
192
|
-
|
|
193
|
-
|
|
196
|
+
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
197
|
+
logger.trace('disconnected: ' + nodeId)
|
|
198
|
+
this.connections.delete(nodeId)
|
|
194
199
|
if (this.config.isLayer0) {
|
|
195
200
|
this.bucket.remove(peerDescriptor.nodeId)
|
|
196
201
|
if (gracefulLeave === true) {
|
|
197
|
-
logger.trace(
|
|
202
|
+
logger.trace(nodeId + ' ' + 'onTransportDisconnected with gracefulLeave ' + gracefulLeave)
|
|
198
203
|
this.removeContact(peerDescriptor)
|
|
199
204
|
} else {
|
|
200
|
-
logger.trace(
|
|
205
|
+
logger.trace(nodeId + ' ' + 'onTransportDisconnected with gracefulLeave ' + gracefulLeave)
|
|
201
206
|
}
|
|
202
207
|
}
|
|
203
208
|
}
|
|
@@ -210,9 +215,9 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
210
215
|
if (this.stopped) {
|
|
211
216
|
return
|
|
212
217
|
}
|
|
213
|
-
logger.trace(`Removing contact ${getNodeIdFromPeerDescriptor(contact)}`)
|
|
214
218
|
const nodeId = getNodeIdFromPeerDescriptor(contact)
|
|
215
|
-
|
|
219
|
+
logger.trace(`Removing contact ${nodeId}`)
|
|
220
|
+
this.bucket.remove(getRawFromDhtAddress(nodeId))
|
|
216
221
|
this.contacts.removeContact(nodeId)
|
|
217
222
|
this.randomPeers.removeContact(nodeId)
|
|
218
223
|
}
|
|
@@ -229,19 +234,19 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
229
234
|
this.connections.clear()
|
|
230
235
|
}
|
|
231
236
|
|
|
232
|
-
getClosestNeighborsTo(referenceId:
|
|
237
|
+
getClosestNeighborsTo(referenceId: DhtAddress, limit?: number, excludedNodeIds?: Set<DhtAddress>): DhtNodeRpcRemote[] {
|
|
233
238
|
const closest = new SortedContactList<DhtNodeRpcRemote>({
|
|
234
239
|
referenceId,
|
|
235
240
|
allowToContainReferenceId: true,
|
|
236
241
|
emitEvents: false,
|
|
237
242
|
excludedNodeIds
|
|
238
243
|
})
|
|
239
|
-
this.bucket.toArray().
|
|
244
|
+
this.bucket.toArray().forEach((contact) => closest.addContact(contact))
|
|
240
245
|
return closest.getClosestContacts(limit)
|
|
241
246
|
}
|
|
242
247
|
|
|
243
248
|
// TODO reduce copy-paste?
|
|
244
|
-
getClosestContactsTo(referenceId:
|
|
249
|
+
getClosestContactsTo(referenceId: DhtAddress, limit?: number, excludedNodeIds?: Set<DhtAddress>): DhtNodeRpcRemote[] {
|
|
245
250
|
const closest = new SortedContactList<DhtNodeRpcRemote>({
|
|
246
251
|
referenceId,
|
|
247
252
|
allowToContainReferenceId: true,
|
|
@@ -253,7 +258,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
253
258
|
return closest.getClosestContacts(limit)
|
|
254
259
|
}
|
|
255
260
|
|
|
256
|
-
getNumberOfContacts(excludedNodeIds?: Set<
|
|
261
|
+
getNumberOfContacts(excludedNodeIds?: Set<DhtAddress>): number {
|
|
257
262
|
return this.contacts.getAllContacts().filter((contact) => {
|
|
258
263
|
if (!excludedNodeIds) {
|
|
259
264
|
return true
|
|
@@ -275,12 +280,12 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
275
280
|
return this.bucket.toArray().map((rpcRemote: DhtNodeRpcRemote) => rpcRemote.getPeerDescriptor())
|
|
276
281
|
}
|
|
277
282
|
|
|
278
|
-
handlePeerActive(nodeId:
|
|
283
|
+
handlePeerActive(nodeId: DhtAddress): void {
|
|
279
284
|
this.contacts.setActive(nodeId)
|
|
280
285
|
}
|
|
281
286
|
|
|
282
|
-
handlePeerUnresponsive(nodeId:
|
|
283
|
-
this.bucket.remove(
|
|
287
|
+
handlePeerUnresponsive(nodeId: DhtAddress): void {
|
|
288
|
+
this.bucket.remove(getRawFromDhtAddress(nodeId))
|
|
284
289
|
this.contacts.removeContact(nodeId)
|
|
285
290
|
}
|
|
286
291
|
|
|
@@ -290,7 +295,7 @@ export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
|
290
295
|
}
|
|
291
296
|
peerDescriptors.forEach((contact) => {
|
|
292
297
|
const nodeId = getNodeIdFromPeerDescriptor(contact)
|
|
293
|
-
if (
|
|
298
|
+
if (nodeId !== this.config.localNodeId) {
|
|
294
299
|
logger.trace(`Adding new contact ${nodeId}`)
|
|
295
300
|
const remote = this.config.createDhtNodeRpcRemote(contact)
|
|
296
301
|
const isInBucket = (this.bucket.get(contact.nodeId) !== null)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
2
2
|
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
3
|
-
import {
|
|
3
|
+
import { DhtAddress } from '../../identifiers'
|
|
4
4
|
|
|
5
5
|
export class Contact {
|
|
6
6
|
|
|
@@ -14,7 +14,7 @@ export class Contact {
|
|
|
14
14
|
return this.peerDescriptor
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
public getNodeId():
|
|
17
|
+
public getNodeId(): DhtAddress {
|
|
18
18
|
return getNodeIdFromPeerDescriptor(this.peerDescriptor)
|
|
19
19
|
}
|
|
20
20
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import EventEmitter from 'eventemitter3'
|
|
2
|
-
import {
|
|
2
|
+
import { DhtAddress } from '../../identifiers'
|
|
3
3
|
|
|
4
4
|
export class ContactState<C> {
|
|
5
5
|
public contacted = false
|
|
@@ -16,17 +16,17 @@ export interface Events<C> {
|
|
|
16
16
|
newContact: (newContact: C, closestContacts: C[]) => void
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export class ContactList<C extends { getNodeId: () =>
|
|
19
|
+
export class ContactList<C extends { getNodeId: () => DhtAddress }> extends EventEmitter<Events<C>> {
|
|
20
20
|
|
|
21
|
-
protected contactsById: Map<
|
|
21
|
+
protected contactsById: Map<DhtAddress, ContactState<C>> = new Map()
|
|
22
22
|
// TODO move this to SortedContactList
|
|
23
|
-
protected contactIds:
|
|
24
|
-
protected localNodeId:
|
|
23
|
+
protected contactIds: DhtAddress[] = []
|
|
24
|
+
protected localNodeId: DhtAddress
|
|
25
25
|
protected maxSize: number
|
|
26
26
|
protected defaultContactQueryLimit
|
|
27
27
|
|
|
28
28
|
constructor(
|
|
29
|
-
localNodeId:
|
|
29
|
+
localNodeId: DhtAddress,
|
|
30
30
|
maxSize: number,
|
|
31
31
|
defaultContactQueryLimit = 20
|
|
32
32
|
) {
|
|
@@ -36,7 +36,7 @@ export class ContactList<C extends { getNodeId: () => NodeID }> extends EventEmi
|
|
|
36
36
|
this.defaultContactQueryLimit = defaultContactQueryLimit
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
public getContact(id:
|
|
39
|
+
public getContact(id: DhtAddress): ContactState<C> | undefined {
|
|
40
40
|
return this.contactsById.get(id)
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DhtAddress } from '../../identifiers'
|
|
2
2
|
import { ContactList, ContactState } from './ContactList'
|
|
3
3
|
|
|
4
|
-
export class RandomContactList<C extends { getNodeId: () =>
|
|
4
|
+
export class RandomContactList<C extends { getNodeId: () => DhtAddress }> extends ContactList<C> {
|
|
5
5
|
|
|
6
6
|
private randomness: number
|
|
7
7
|
|
|
8
8
|
constructor(
|
|
9
|
-
localNodeId:
|
|
9
|
+
localNodeId: DhtAddress,
|
|
10
10
|
maxSize: number,
|
|
11
11
|
randomness = 0.20,
|
|
12
12
|
defaultContactQueryLimit?: number
|
|
@@ -16,7 +16,7 @@ export class RandomContactList<C extends { getNodeId: () => NodeID }> extends Co
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
addContact(contact: C): void {
|
|
19
|
-
if (
|
|
19
|
+
if (this.localNodeId === contact.getNodeId()) {
|
|
20
20
|
return
|
|
21
21
|
}
|
|
22
22
|
if (!this.contactsById.has(contact.getNodeId())) {
|
|
@@ -37,10 +37,10 @@ export class RandomContactList<C extends { getNodeId: () => NodeID }> extends Co
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
removeContact(id:
|
|
40
|
+
removeContact(id: DhtAddress): boolean {
|
|
41
41
|
if (this.contactsById.has(id)) {
|
|
42
42
|
const removed = this.contactsById.get(id)!.contact
|
|
43
|
-
const index = this.contactIds.findIndex((nodeId) =>
|
|
43
|
+
const index = this.contactIds.findIndex((nodeId) => (nodeId === id))
|
|
44
44
|
this.contactIds.splice(index, 1)
|
|
45
45
|
this.contactsById.delete(id)
|
|
46
46
|
this.emit('contactRemoved', removed, this.getContacts())
|
|
@@ -4,7 +4,6 @@ import { ConnectionType } from '../../connection/IConnection'
|
|
|
4
4
|
import { expectedConnectionType } from '../../helpers/Connectivity'
|
|
5
5
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
6
6
|
import { DhtRpcOptions } from '../../rpc-protocol/DhtRpcOptions'
|
|
7
|
-
import { ServiceID } from '../../types/ServiceID'
|
|
8
7
|
|
|
9
8
|
// Should connect directly to the server, timeout can be low
|
|
10
9
|
const WEBSOCKET_CLIENT_TIMEOUT = 5000
|
|
@@ -32,14 +31,12 @@ export abstract class RpcRemote<T extends ServiceInfo & ClassType> {
|
|
|
32
31
|
|
|
33
32
|
private readonly localPeerDescriptor: PeerDescriptor
|
|
34
33
|
private readonly remotePeerDescriptor: PeerDescriptor
|
|
35
|
-
private readonly serviceId: ServiceID
|
|
36
34
|
private readonly client: ProtoRpcClient<T>
|
|
37
35
|
private readonly timeout?: number
|
|
38
36
|
|
|
39
37
|
constructor(
|
|
40
38
|
localPeerDescriptor: PeerDescriptor,
|
|
41
39
|
remotePeerDescriptor: PeerDescriptor,
|
|
42
|
-
serviceId: ServiceID,
|
|
43
40
|
rpcCommunicator: RpcCommunicator,
|
|
44
41
|
// eslint-disable-next-line @typescript-eslint/prefer-function-type
|
|
45
42
|
clientClass: { new (clientTransport: ClientTransport): T },
|
|
@@ -48,7 +45,6 @@ export abstract class RpcRemote<T extends ServiceInfo & ClassType> {
|
|
|
48
45
|
this.localPeerDescriptor = localPeerDescriptor
|
|
49
46
|
this.remotePeerDescriptor = remotePeerDescriptor
|
|
50
47
|
this.client = toProtoRpcClient(new clientClass(rpcCommunicator.getRpcClientTransport()))
|
|
51
|
-
this.serviceId = serviceId
|
|
52
48
|
this.timeout = timeout
|
|
53
49
|
}
|
|
54
50
|
|
|
@@ -60,10 +56,6 @@ export abstract class RpcRemote<T extends ServiceInfo & ClassType> {
|
|
|
60
56
|
return this.localPeerDescriptor
|
|
61
57
|
}
|
|
62
58
|
|
|
63
|
-
getServiceId(): ServiceID {
|
|
64
|
-
return this.serviceId
|
|
65
|
-
}
|
|
66
|
-
|
|
67
59
|
getClient(): ProtoRpcClient<T> {
|
|
68
60
|
return this.client
|
|
69
61
|
}
|