@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
|
@@ -2,26 +2,26 @@ import { ContactState, Events } from './ContactList'
|
|
|
2
2
|
import { sortedIndexBy } from 'lodash'
|
|
3
3
|
import EventEmitter from 'eventemitter3'
|
|
4
4
|
import { getDistance } from '../PeerManager'
|
|
5
|
-
import {
|
|
5
|
+
import { DhtAddress, getRawFromDhtAddress } from '../../identifiers'
|
|
6
6
|
|
|
7
7
|
export interface SortedContactListConfig {
|
|
8
|
-
referenceId:
|
|
8
|
+
referenceId: DhtAddress // all contacts in this list are in sorted by the distance to this ID
|
|
9
9
|
allowToContainReferenceId: boolean
|
|
10
10
|
// TODO could maybe optimize this by removing the flag and then we'd check whether we have
|
|
11
11
|
// any listeners before we emit the event
|
|
12
12
|
emitEvents: boolean
|
|
13
13
|
maxSize?: number
|
|
14
14
|
// if set, the list can't contain any contacts which are futher away than this limit
|
|
15
|
-
nodeIdDistanceLimit?:
|
|
15
|
+
nodeIdDistanceLimit?: DhtAddress
|
|
16
16
|
// if set, the list can't contain contacts with these ids
|
|
17
|
-
excludedNodeIds?: Set<
|
|
17
|
+
excludedNodeIds?: Set<DhtAddress>
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export class SortedContactList<C extends { getNodeId: () =>
|
|
20
|
+
export class SortedContactList<C extends { getNodeId: () => DhtAddress }> extends EventEmitter<Events<C>> {
|
|
21
21
|
|
|
22
22
|
private config: SortedContactListConfig
|
|
23
|
-
private contactsById: Map<
|
|
24
|
-
private contactIds:
|
|
23
|
+
private contactsById: Map<DhtAddress, ContactState<C>> = new Map()
|
|
24
|
+
private contactIds: DhtAddress[] = []
|
|
25
25
|
|
|
26
26
|
constructor(
|
|
27
27
|
config: SortedContactListConfig
|
|
@@ -31,11 +31,11 @@ export class SortedContactList<C extends { getNodeId: () => NodeID }> extends Ev
|
|
|
31
31
|
this.compareIds = this.compareIds.bind(this)
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
public getClosestContactId():
|
|
34
|
+
public getClosestContactId(): DhtAddress {
|
|
35
35
|
return this.contactIds[0]
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
public getContactIds():
|
|
38
|
+
public getContactIds(): DhtAddress[] {
|
|
39
39
|
return this.contactIds
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -44,7 +44,7 @@ export class SortedContactList<C extends { getNodeId: () => NodeID }> extends Ev
|
|
|
44
44
|
return
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
if ((!this.config.allowToContainReferenceId &&
|
|
47
|
+
if ((!this.config.allowToContainReferenceId && (this.config.referenceId === contact.getNodeId())) ||
|
|
48
48
|
(this.config.nodeIdDistanceLimit !== undefined && this.compareIds(this.config.nodeIdDistanceLimit, contact.getNodeId()) < 0)) {
|
|
49
49
|
return
|
|
50
50
|
}
|
|
@@ -53,7 +53,7 @@ export class SortedContactList<C extends { getNodeId: () => NodeID }> extends Ev
|
|
|
53
53
|
this.contactsById.set(contact.getNodeId(), new ContactState(contact))
|
|
54
54
|
|
|
55
55
|
// eslint-disable-next-line max-len
|
|
56
|
-
const index = sortedIndexBy(this.contactIds, contact.getNodeId(), (id:
|
|
56
|
+
const index = sortedIndexBy(this.contactIds, contact.getNodeId(), (id: DhtAddress) => { return this.distanceToReferenceId(id) })
|
|
57
57
|
this.contactIds.splice(index, 0, contact.getNodeId())
|
|
58
58
|
} else if (this.compareIds(this.contactIds[this.config.maxSize - 1], contact.getNodeId()) > 0) {
|
|
59
59
|
const removedId = this.contactIds.pop()
|
|
@@ -62,7 +62,7 @@ export class SortedContactList<C extends { getNodeId: () => NodeID }> extends Ev
|
|
|
62
62
|
this.contactsById.set(contact.getNodeId(), new ContactState(contact))
|
|
63
63
|
|
|
64
64
|
// eslint-disable-next-line max-len
|
|
65
|
-
const index = sortedIndexBy(this.contactIds, contact.getNodeId(), (id:
|
|
65
|
+
const index = sortedIndexBy(this.contactIds, contact.getNodeId(), (id: DhtAddress) => { return this.distanceToReferenceId(id) })
|
|
66
66
|
this.contactIds.splice(index, 0, contact.getNodeId())
|
|
67
67
|
if (this.config.emitEvents) {
|
|
68
68
|
this.emit(
|
|
@@ -86,17 +86,17 @@ export class SortedContactList<C extends { getNodeId: () => NodeID }> extends Ev
|
|
|
86
86
|
contacts.forEach((contact) => this.addContact(contact))
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
public getContact(id:
|
|
89
|
+
public getContact(id: DhtAddress): ContactState<C> | undefined {
|
|
90
90
|
return this.contactsById.get(id)
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
public setContacted(contactId:
|
|
93
|
+
public setContacted(contactId: DhtAddress): void {
|
|
94
94
|
if (this.contactsById.has(contactId)) {
|
|
95
95
|
this.contactsById.get(contactId)!.contacted = true
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
public setActive(contactId:
|
|
99
|
+
public setActive(contactId: DhtAddress): void {
|
|
100
100
|
if (this.contactsById.has(contactId)) {
|
|
101
101
|
this.contactsById.get(contactId)!.active = true
|
|
102
102
|
}
|
|
@@ -146,23 +146,23 @@ export class SortedContactList<C extends { getNodeId: () => NodeID }> extends Ev
|
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
-
public compareIds(id1:
|
|
149
|
+
public compareIds(id1: DhtAddress, id2: DhtAddress): number {
|
|
150
150
|
const distance1 = this.distanceToReferenceId(id1)
|
|
151
151
|
const distance2 = this.distanceToReferenceId(id2)
|
|
152
152
|
return distance1 - distance2
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
// TODO inline this method?
|
|
156
|
-
private distanceToReferenceId(id:
|
|
157
|
-
// TODO maybe this class should store the referenceId also as
|
|
158
|
-
return getDistance(this.config.referenceId, id)
|
|
156
|
+
private distanceToReferenceId(id: DhtAddress): number {
|
|
157
|
+
// TODO maybe this class should store the referenceId also as DhtAddressRaw so that we don't need to convert it here?
|
|
158
|
+
return getDistance(getRawFromDhtAddress(this.config.referenceId), getRawFromDhtAddress(id))
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
public removeContact(id:
|
|
161
|
+
public removeContact(id: DhtAddress): boolean {
|
|
162
162
|
if (this.contactsById.has(id)) {
|
|
163
163
|
const removed = this.contactsById.get(id)!.contact
|
|
164
164
|
// TODO use sortedIndexBy?
|
|
165
|
-
const index = this.contactIds.findIndex((nodeId) =>
|
|
165
|
+
const index = this.contactIds.findIndex((nodeId) => (nodeId === id))
|
|
166
166
|
this.contactIds.splice(index, 1)
|
|
167
167
|
this.contactsById.delete(id)
|
|
168
168
|
if (this.config.emitEvents) {
|
|
@@ -177,7 +177,7 @@ export class SortedContactList<C extends { getNodeId: () => NodeID }> extends Ev
|
|
|
177
177
|
return false
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
public isActive(id:
|
|
180
|
+
public isActive(id: DhtAddress): boolean {
|
|
181
181
|
return this.contactsById.has(id) ? this.contactsById.get(id)!.active : false
|
|
182
182
|
}
|
|
183
183
|
|
|
@@ -5,7 +5,7 @@ import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
|
5
5
|
import { PeerManager, getDistance } from '../PeerManager'
|
|
6
6
|
import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
|
|
7
7
|
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
8
|
-
import {
|
|
8
|
+
import { DhtAddress, getRawFromDhtAddress } from '../../identifiers'
|
|
9
9
|
|
|
10
10
|
const logger = new Logger(module)
|
|
11
11
|
|
|
@@ -14,20 +14,21 @@ interface DiscoverySessionEvents {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
interface DiscoverySessionConfig {
|
|
17
|
-
targetId:
|
|
17
|
+
targetId: DhtAddress
|
|
18
18
|
parallelism: number
|
|
19
19
|
noProgressLimit: number
|
|
20
20
|
peerManager: PeerManager
|
|
21
|
+
// Note that contacted peers will be mutated by the DiscoverySession or other parallel sessions
|
|
22
|
+
contactedPeers: Set<DhtAddress>
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
export class DiscoverySession {
|
|
24
|
-
|
|
26
|
+
|
|
25
27
|
public readonly id = v4()
|
|
26
28
|
private stopped = false
|
|
27
29
|
private emitter = new EventEmitter<DiscoverySessionEvents>()
|
|
28
30
|
private noProgressCounter = 0
|
|
29
|
-
private ongoingClosestPeersRequests: Set<
|
|
30
|
-
private contactedPeers: Set<NodeID> = new Set()
|
|
31
|
+
private ongoingClosestPeersRequests: Set<DhtAddress> = new Set()
|
|
31
32
|
private readonly config: DiscoverySessionConfig
|
|
32
33
|
|
|
33
34
|
constructor(config: DiscoverySessionConfig) {
|
|
@@ -46,22 +47,23 @@ export class DiscoverySession {
|
|
|
46
47
|
return []
|
|
47
48
|
}
|
|
48
49
|
logger.trace(`Getting closest peers from contact: ${getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
49
|
-
this.contactedPeers.add(contact.getNodeId())
|
|
50
|
+
this.config.contactedPeers.add(contact.getNodeId())
|
|
50
51
|
const returnedContacts = await contact.getClosestPeers(this.config.targetId)
|
|
51
52
|
this.config.peerManager.handlePeerActive(contact.getNodeId())
|
|
52
53
|
return returnedContacts
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
private onClosestPeersRequestSucceeded(nodeId:
|
|
56
|
+
private onClosestPeersRequestSucceeded(nodeId: DhtAddress, contacts: PeerDescriptor[]) {
|
|
56
57
|
if (!this.ongoingClosestPeersRequests.has(nodeId)) {
|
|
57
58
|
return
|
|
58
59
|
}
|
|
59
60
|
this.ongoingClosestPeersRequests.delete(nodeId)
|
|
60
|
-
const
|
|
61
|
-
const
|
|
61
|
+
const targetId = getRawFromDhtAddress(this.config.targetId)
|
|
62
|
+
const oldClosestNeighbor = this.config.peerManager.getClosestNeighborsTo(this.config.targetId, 1)[0]
|
|
63
|
+
const oldClosestDistance = getDistance(targetId, getRawFromDhtAddress(oldClosestNeighbor.getNodeId()))
|
|
62
64
|
this.addNewContacts(contacts)
|
|
63
|
-
const newClosestNeighbor = this.config.peerManager.getClosestNeighborsTo(
|
|
64
|
-
const newClosestDistance = getDistance(
|
|
65
|
+
const newClosestNeighbor = this.config.peerManager.getClosestNeighborsTo(this.config.targetId, 1)[0]
|
|
66
|
+
const newClosestDistance = getDistance(targetId, getRawFromDhtAddress(newClosestNeighbor.getNodeId()))
|
|
65
67
|
if (newClosestDistance >= oldClosestDistance) {
|
|
66
68
|
this.noProgressCounter++
|
|
67
69
|
}
|
|
@@ -80,9 +82,9 @@ export class DiscoverySession {
|
|
|
80
82
|
return
|
|
81
83
|
}
|
|
82
84
|
const uncontacted = this.config.peerManager.getClosestContactsTo(
|
|
83
|
-
|
|
85
|
+
this.config.targetId,
|
|
84
86
|
this.config.parallelism,
|
|
85
|
-
this.contactedPeers
|
|
87
|
+
this.config.contactedPeers
|
|
86
88
|
)
|
|
87
89
|
if (uncontacted.length === 0 || this.noProgressCounter >= this.config.noProgressLimit) {
|
|
88
90
|
this.emitter.emit('discoveryCompleted')
|
|
@@ -105,7 +107,7 @@ export class DiscoverySession {
|
|
|
105
107
|
}
|
|
106
108
|
|
|
107
109
|
public async findClosestNodes(timeout: number): Promise<void> {
|
|
108
|
-
if (this.config.peerManager.getNumberOfContacts(this.contactedPeers) === 0) {
|
|
110
|
+
if (this.config.peerManager.getNumberOfContacts(this.config.contactedPeers) === 0) {
|
|
109
111
|
return
|
|
110
112
|
}
|
|
111
113
|
// TODO add abortController and signal it in stop()
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { DiscoverySession } from './DiscoverySession'
|
|
2
2
|
import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
|
|
3
|
-
import { areEqualPeerDescriptors, getNodeIdFromPeerDescriptor
|
|
3
|
+
import { areEqualPeerDescriptors, getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
4
4
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
5
5
|
import { Logger, scheduleAtInterval, setAbortableTimeout } from '@streamr/utils'
|
|
6
6
|
import { ConnectionManager } from '../../connection/ConnectionManager'
|
|
7
7
|
import { PeerManager } from '../PeerManager'
|
|
8
|
-
import {
|
|
8
|
+
import { DhtAddress, createRandomDhtAddress } from '../../identifiers'
|
|
9
9
|
import { ServiceID } from '../../types/ServiceID'
|
|
10
10
|
|
|
11
11
|
interface PeerDiscoveryConfig {
|
|
@@ -35,7 +35,27 @@ export class PeerDiscovery {
|
|
|
35
35
|
this.abortController = new AbortController()
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
async joinDht(
|
|
38
|
+
async joinDht(
|
|
39
|
+
entryPoints: PeerDescriptor[],
|
|
40
|
+
doAdditionalRandomPeerDiscovery = true,
|
|
41
|
+
retry = true
|
|
42
|
+
): Promise<void> {
|
|
43
|
+
const contactedPeers = new Set<DhtAddress>()
|
|
44
|
+
await Promise.all(entryPoints.map((entryPoint) => this.joinThroughEntryPoint(
|
|
45
|
+
entryPoint,
|
|
46
|
+
contactedPeers,
|
|
47
|
+
doAdditionalRandomPeerDiscovery,
|
|
48
|
+
retry
|
|
49
|
+
)))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async joinThroughEntryPoint(
|
|
53
|
+
entryPointDescriptor: PeerDescriptor,
|
|
54
|
+
// Note that this set is mutated by DiscoverySession
|
|
55
|
+
contactedPeers: Set<DhtAddress>,
|
|
56
|
+
doAdditionalRandomPeerDiscovery = true,
|
|
57
|
+
retry = true
|
|
58
|
+
): Promise<void> {
|
|
39
59
|
if (this.isStopped()) {
|
|
40
60
|
return
|
|
41
61
|
}
|
|
@@ -49,22 +69,23 @@ export class PeerDiscovery {
|
|
|
49
69
|
}
|
|
50
70
|
this.config.connectionManager?.lockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
|
|
51
71
|
this.config.peerManager.handleNewPeers([entryPointDescriptor])
|
|
52
|
-
const targetId =
|
|
53
|
-
const sessions = [this.createSession(targetId)]
|
|
72
|
+
const targetId = getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
|
|
73
|
+
const sessions = [this.createSession(targetId, contactedPeers)]
|
|
54
74
|
if (doAdditionalRandomPeerDiscovery) {
|
|
55
|
-
sessions.push(this.createSession(
|
|
75
|
+
sessions.push(this.createSession(createRandomDhtAddress(), contactedPeers))
|
|
56
76
|
}
|
|
57
77
|
await this.runSessions(sessions, entryPointDescriptor, retry)
|
|
58
78
|
this.config.connectionManager?.unlockConnection(entryPointDescriptor, `${this.config.serviceId}::joinDht`)
|
|
59
79
|
|
|
60
80
|
}
|
|
61
81
|
|
|
62
|
-
private createSession(targetId:
|
|
82
|
+
private createSession(targetId: DhtAddress, contactedPeers: Set<DhtAddress>): DiscoverySession {
|
|
63
83
|
const sessionOptions = {
|
|
64
84
|
targetId,
|
|
65
85
|
parallelism: this.config.parallelism,
|
|
66
86
|
noProgressLimit: this.config.joinNoProgressLimit,
|
|
67
|
-
peerManager: this.config.peerManager
|
|
87
|
+
peerManager: this.config.peerManager,
|
|
88
|
+
contactedPeers
|
|
68
89
|
}
|
|
69
90
|
return new DiscoverySession(sessionOptions)
|
|
70
91
|
}
|
|
@@ -100,7 +121,7 @@ export class PeerDiscovery {
|
|
|
100
121
|
logger.debug(`Rejoining DHT ${this.config.serviceId}`)
|
|
101
122
|
this.rejoinOngoing = true
|
|
102
123
|
try {
|
|
103
|
-
await this.
|
|
124
|
+
await this.joinThroughEntryPoint(entryPoint, new Set())
|
|
104
125
|
logger.debug(`Rejoined DHT successfully ${this.config.serviceId}!`)
|
|
105
126
|
} catch (err) {
|
|
106
127
|
logger.warn(`Rejoining DHT ${this.config.serviceId} failed`)
|
|
@@ -126,14 +147,15 @@ export class PeerDiscovery {
|
|
|
126
147
|
if (this.isStopped()) {
|
|
127
148
|
return
|
|
128
149
|
}
|
|
150
|
+
const localNodeId = getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
|
|
129
151
|
const nodes = this.config.peerManager.getClosestNeighborsTo(
|
|
130
|
-
|
|
152
|
+
localNodeId,
|
|
131
153
|
this.config.parallelism
|
|
132
154
|
)
|
|
133
155
|
await Promise.allSettled(
|
|
134
156
|
nodes.map(async (peer: DhtNodeRpcRemote) => {
|
|
135
|
-
const contacts = await peer.getClosestPeers(
|
|
136
|
-
this.config.peerManager.handleNewPeers(contacts)
|
|
157
|
+
const contacts = await peer.getClosestPeers(localNodeId)
|
|
158
|
+
this.config.peerManager.handleNewPeers(contacts)
|
|
137
159
|
})
|
|
138
160
|
)
|
|
139
161
|
}
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
import { Router } from '../routing/Router'
|
|
11
11
|
import { RoutingMode } from '../routing/RoutingSession'
|
|
12
12
|
import { areEqualPeerDescriptors, getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
13
|
-
import { Logger, areEqualBinaries,
|
|
13
|
+
import { Logger, areEqualBinaries, runAndWaitForEvents3, wait } from '@streamr/utils'
|
|
14
14
|
import { RoutingRpcCommunicator } from '../../transport/RoutingRpcCommunicator'
|
|
15
15
|
import { RecursiveOperationSessionRpcRemote } from './RecursiveOperationSessionRpcRemote'
|
|
16
16
|
import { RecursiveOperationSession, RecursiveOperationSessionEvents } from './RecursiveOperationSession'
|
|
@@ -24,13 +24,13 @@ import { getPreviousPeer } from '../routing/getPreviousPeer'
|
|
|
24
24
|
import { createRouteMessageAck } from '../routing/RouterRpcLocal'
|
|
25
25
|
import { ServiceID } from '../../types/ServiceID'
|
|
26
26
|
import { RecursiveOperationRpcLocal } from './RecursiveOperationRpcLocal'
|
|
27
|
-
import {
|
|
27
|
+
import { DhtAddress, getDhtAddressFromRaw, getRawFromDhtAddress } from '../../identifiers'
|
|
28
28
|
import { getDistance } from '../PeerManager'
|
|
29
29
|
|
|
30
30
|
interface RecursiveOperationManagerConfig {
|
|
31
31
|
rpcCommunicator: RoutingRpcCommunicator
|
|
32
32
|
sessionTransport: ITransport
|
|
33
|
-
connections: Map<
|
|
33
|
+
connections: Map<DhtAddress, DhtNodeRpcRemote>
|
|
34
34
|
router: Router
|
|
35
35
|
localPeerDescriptor: PeerDescriptor
|
|
36
36
|
serviceId: ServiceID
|
|
@@ -75,7 +75,7 @@ export class RecursiveOperationManager {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
public async execute(
|
|
78
|
-
targetId:
|
|
78
|
+
targetId: DhtAddress,
|
|
79
79
|
operation: RecursiveOperation,
|
|
80
80
|
excludedPeer?: PeerDescriptor,
|
|
81
81
|
waitForCompletion = true
|
|
@@ -96,11 +96,11 @@ export class RecursiveOperationManager {
|
|
|
96
96
|
}
|
|
97
97
|
})
|
|
98
98
|
if (this.config.connections.size === 0) {
|
|
99
|
-
const
|
|
99
|
+
const dataEntries = Array.from(this.config.localDataStore.values(targetId))
|
|
100
100
|
session.onResponseReceived(
|
|
101
101
|
[this.config.localPeerDescriptor],
|
|
102
102
|
[this.config.localPeerDescriptor],
|
|
103
|
-
|
|
103
|
+
dataEntries,
|
|
104
104
|
true
|
|
105
105
|
)
|
|
106
106
|
return session.getResults()
|
|
@@ -124,7 +124,7 @@ export class RecursiveOperationManager {
|
|
|
124
124
|
await wait(50)
|
|
125
125
|
}
|
|
126
126
|
if (operation === RecursiveOperation.FETCH_DATA) {
|
|
127
|
-
const dataEntries = Array.from(this.config.localDataStore.
|
|
127
|
+
const dataEntries = Array.from(this.config.localDataStore.values(targetId))
|
|
128
128
|
if (dataEntries.length > 0) {
|
|
129
129
|
this.sendResponse([], this.config.localPeerDescriptor, session.getId(), [], dataEntries, true)
|
|
130
130
|
}
|
|
@@ -154,7 +154,6 @@ export class RecursiveOperationManager {
|
|
|
154
154
|
const rpcRemote = new RecursiveOperationSessionRpcRemote(
|
|
155
155
|
this.config.localPeerDescriptor,
|
|
156
156
|
targetPeerDescriptor,
|
|
157
|
-
serviceId,
|
|
158
157
|
remoteCommunicator,
|
|
159
158
|
RecursiveOperationSessionRpcClient,
|
|
160
159
|
// TODO use config option or named constant?
|
|
@@ -169,15 +168,15 @@ export class RecursiveOperationManager {
|
|
|
169
168
|
if (this.stopped) {
|
|
170
169
|
return createRouteMessageAck(routedMessage, RouteMessageError.STOPPED)
|
|
171
170
|
}
|
|
172
|
-
const targetId =
|
|
171
|
+
const targetId = getDhtAddressFromRaw(routedMessage.target)
|
|
173
172
|
const request = (routedMessage.message!.body as { recursiveOperationRequest: RecursiveOperationRequest }).recursiveOperationRequest
|
|
174
173
|
// TODO use config option or named constant?
|
|
175
|
-
const closestPeersToDestination = this.getClosestConnections(
|
|
174
|
+
const closestPeersToDestination = this.getClosestConnections(targetId, 5)
|
|
176
175
|
const dataEntries = (request.operation === RecursiveOperation.FETCH_DATA)
|
|
177
|
-
? Array.from(this.config.localDataStore.
|
|
176
|
+
? Array.from(this.config.localDataStore.values(targetId))
|
|
178
177
|
: []
|
|
179
178
|
if (request.operation === RecursiveOperation.DELETE_DATA) {
|
|
180
|
-
this.config.localDataStore.markAsDeleted(
|
|
179
|
+
this.config.localDataStore.markAsDeleted(targetId, getNodeIdFromPeerDescriptor(routedMessage.sourcePeer!))
|
|
181
180
|
}
|
|
182
181
|
if (areEqualBinaries(this.config.localPeerDescriptor.nodeId, routedMessage.target)) {
|
|
183
182
|
// TODO this is also very similar case to what we do at line 255, could simplify the code paths?
|
|
@@ -212,10 +211,10 @@ export class RecursiveOperationManager {
|
|
|
212
211
|
}
|
|
213
212
|
}
|
|
214
213
|
|
|
215
|
-
private getClosestConnections(
|
|
214
|
+
private getClosestConnections(referenceId: DhtAddress, limit: number): PeerDescriptor[] {
|
|
216
215
|
const connectedPeers = Array.from(this.config.connections.values())
|
|
217
216
|
const closestPeers = new SortedContactList<DhtNodeRpcRemote>({
|
|
218
|
-
referenceId
|
|
217
|
+
referenceId,
|
|
219
218
|
maxSize: limit,
|
|
220
219
|
allowToContainReferenceId: true,
|
|
221
220
|
emitEvents: false
|
|
@@ -224,9 +223,10 @@ export class RecursiveOperationManager {
|
|
|
224
223
|
return closestPeers.getClosestContacts(limit).map((peer) => peer.getPeerDescriptor())
|
|
225
224
|
}
|
|
226
225
|
|
|
227
|
-
private isPeerCloserToIdThanSelf(peer: PeerDescriptor,
|
|
228
|
-
const
|
|
229
|
-
const
|
|
226
|
+
private isPeerCloserToIdThanSelf(peer: PeerDescriptor, nodeIdOrDataKey: DhtAddress): boolean {
|
|
227
|
+
const nodeIdOrDataKeyRaw = getRawFromDhtAddress(nodeIdOrDataKey)
|
|
228
|
+
const distance1 = getDistance(peer.nodeId, nodeIdOrDataKeyRaw)
|
|
229
|
+
const distance2 = getDistance(this.config.localPeerDescriptor.nodeId, nodeIdOrDataKeyRaw)
|
|
230
230
|
return distance1 < distance2
|
|
231
231
|
}
|
|
232
232
|
|
|
@@ -33,8 +33,8 @@ export class RecursiveOperationRpcRemote extends RpcRemote<RecursiveOperationRpc
|
|
|
33
33
|
const fromNode = previousPeer
|
|
34
34
|
? getNodeIdFromPeerDescriptor(previousPeer)
|
|
35
35
|
: getNodeIdFromPeerDescriptor(params.sourcePeer!)
|
|
36
|
-
|
|
37
|
-
logger.debug(`Failed to send routeRequest message from ${fromNode} to ${
|
|
36
|
+
const toNode = getNodeIdFromPeerDescriptor(this.getPeerDescriptor())
|
|
37
|
+
logger.debug(`Failed to send routeRequest message from ${fromNode} to ${toNode} with: ${err}`)
|
|
38
38
|
return false
|
|
39
39
|
}
|
|
40
40
|
return true
|
|
@@ -19,7 +19,7 @@ import { RecursiveOperationResult } from './RecursiveOperationManager'
|
|
|
19
19
|
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
20
20
|
import { ServiceID } from '../../types/ServiceID'
|
|
21
21
|
import { RecursiveOperationSessionRpcLocal } from './RecursiveOperationSessionRpcLocal'
|
|
22
|
-
import {
|
|
22
|
+
import { DhtAddress, getDhtAddressFromRaw, getRawFromDhtAddress } from '../../identifiers'
|
|
23
23
|
|
|
24
24
|
export interface RecursiveOperationSessionEvents {
|
|
25
25
|
completed: () => void
|
|
@@ -27,7 +27,7 @@ export interface RecursiveOperationSessionEvents {
|
|
|
27
27
|
|
|
28
28
|
export interface RecursiveOperationSessionConfig {
|
|
29
29
|
transport: ITransport
|
|
30
|
-
targetId:
|
|
30
|
+
targetId: DhtAddress
|
|
31
31
|
localPeerDescriptor: PeerDescriptor
|
|
32
32
|
waitedRoutingPathCompletions: number
|
|
33
33
|
operation: RecursiveOperation
|
|
@@ -39,9 +39,9 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
39
39
|
private readonly id = v4()
|
|
40
40
|
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
41
41
|
private results: SortedContactList<Contact>
|
|
42
|
-
private foundData: Map<
|
|
43
|
-
private allKnownHops: Set<
|
|
44
|
-
private reportedHops: Set<
|
|
42
|
+
private foundData: Map<DhtAddress, DataEntry> = new Map()
|
|
43
|
+
private allKnownHops: Set<DhtAddress> = new Set()
|
|
44
|
+
private reportedHops: Set<DhtAddress> = new Set()
|
|
45
45
|
private timeoutTask?: NodeJS.Timeout
|
|
46
46
|
private completionEventEmitted = false
|
|
47
47
|
private noCloserNodesReceivedCounter = 0
|
|
@@ -51,7 +51,7 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
51
51
|
super()
|
|
52
52
|
this.config = config
|
|
53
53
|
this.results = new SortedContactList({
|
|
54
|
-
referenceId:
|
|
54
|
+
referenceId: config.targetId,
|
|
55
55
|
maxSize: 10, // TODO use config option or named constant?
|
|
56
56
|
allowToContainReferenceId: true,
|
|
57
57
|
emitEvents: false
|
|
@@ -94,7 +94,7 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
94
94
|
const routeMessage: RouteMessageWrapper = {
|
|
95
95
|
message: msg,
|
|
96
96
|
requestId: v4(),
|
|
97
|
-
target: this.config.targetId,
|
|
97
|
+
target: getRawFromDhtAddress(this.config.targetId),
|
|
98
98
|
sourcePeer: this.config.localPeerDescriptor,
|
|
99
99
|
reachableThrough: [],
|
|
100
100
|
routingPath: []
|
|
@@ -103,7 +103,7 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
private isCompleted(): boolean {
|
|
106
|
-
const unreportedHops: Set<
|
|
106
|
+
const unreportedHops: Set<DhtAddress> = new Set(this.allKnownHops)
|
|
107
107
|
this.reportedHops.forEach((id) => {
|
|
108
108
|
unreportedHops.delete(id)
|
|
109
109
|
})
|
|
@@ -146,7 +146,7 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
146
146
|
const localNodeId = getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
|
|
147
147
|
routingPath.forEach((desc) => {
|
|
148
148
|
const newNodeId = getNodeIdFromPeerDescriptor(desc)
|
|
149
|
-
if (
|
|
149
|
+
if (localNodeId !== newNodeId) {
|
|
150
150
|
this.allKnownHops.add(newNodeId)
|
|
151
151
|
}
|
|
152
152
|
})
|
|
@@ -155,7 +155,7 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
155
155
|
private setHopAsReported(desc: PeerDescriptor) {
|
|
156
156
|
const localNodeId = getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
|
|
157
157
|
const newNodeId = getNodeIdFromPeerDescriptor(desc)
|
|
158
|
-
if (
|
|
158
|
+
if (localNodeId !== newNodeId) {
|
|
159
159
|
this.reportedHops.add(newNodeId)
|
|
160
160
|
}
|
|
161
161
|
if (this.isCompleted()) {
|
|
@@ -172,7 +172,7 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
172
172
|
|
|
173
173
|
private processFoundData(dataEntries: DataEntry[]): void {
|
|
174
174
|
dataEntries.forEach((entry) => {
|
|
175
|
-
const creatorNodeId =
|
|
175
|
+
const creatorNodeId = getDhtAddressFromRaw(entry.creator)
|
|
176
176
|
const existingEntry = this.foundData.get(creatorNodeId)
|
|
177
177
|
if (!existingEntry || existingEntry.createdAt! < entry.createdAt!
|
|
178
178
|
|| (existingEntry.createdAt! <= entry.createdAt! && entry.deleted)) {
|
|
@@ -11,12 +11,12 @@ import { ConnectionManager } from '../../connection/ConnectionManager'
|
|
|
11
11
|
import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
|
|
12
12
|
import { v4 } from 'uuid'
|
|
13
13
|
import { RouterRpcLocal, createRouteMessageAck } from './RouterRpcLocal'
|
|
14
|
-
import {
|
|
14
|
+
import { DhtAddress, getDhtAddressFromRaw } from '../../identifiers'
|
|
15
15
|
|
|
16
16
|
export interface RouterConfig {
|
|
17
17
|
rpcCommunicator: RoutingRpcCommunicator
|
|
18
18
|
localPeerDescriptor: PeerDescriptor
|
|
19
|
-
connections: Map<
|
|
19
|
+
connections: Map<DhtAddress, DhtNodeRpcRemote>
|
|
20
20
|
addContact: (contact: PeerDescriptor, setActive?: boolean) => void
|
|
21
21
|
connectionManager?: ConnectionManager
|
|
22
22
|
}
|
|
@@ -30,7 +30,7 @@ const logger = new Logger(module)
|
|
|
30
30
|
|
|
31
31
|
export class Router {
|
|
32
32
|
|
|
33
|
-
private readonly forwardingTable: Map<
|
|
33
|
+
private readonly forwardingTable: Map<DhtAddress, ForwardingTableEntry> = new Map()
|
|
34
34
|
private ongoingRoutingSessions: Map<string, RoutingSession> = new Map()
|
|
35
35
|
// TODO use config option or named constant?
|
|
36
36
|
private readonly duplicateRequestDetector: DuplicateDetector = new DuplicateDetector(100000, 100)
|
|
@@ -118,7 +118,7 @@ export class Router {
|
|
|
118
118
|
return createRouteMessageAck(routedMessage, RouteMessageError.STOPPED)
|
|
119
119
|
}
|
|
120
120
|
logger.trace(`Routing message ${routedMessage.requestId} from ${getNodeIdFromPeerDescriptor(routedMessage.sourcePeer!)} `
|
|
121
|
-
+ `to ${
|
|
121
|
+
+ `to ${getDhtAddressFromRaw(routedMessage.target)}`)
|
|
122
122
|
const session = this.createRoutingSession(routedMessage, mode, excludedPeer)
|
|
123
123
|
const contacts = session.updateAndGetRoutablePeers()
|
|
124
124
|
if (contacts.length > 0) {
|
|
@@ -154,7 +154,7 @@ export class Router {
|
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
private createRoutingSession(routedMessage: RouteMessageWrapper, mode: RoutingMode, excludedNode?: PeerDescriptor): RoutingSession {
|
|
157
|
-
const excludedNodeIds = new Set<
|
|
157
|
+
const excludedNodeIds = new Set<DhtAddress>(routedMessage.routingPath.map((descriptor) => getNodeIdFromPeerDescriptor(descriptor)))
|
|
158
158
|
if (excludedNode) {
|
|
159
159
|
excludedNodeIds.add(getNodeIdFromPeerDescriptor(excludedNode))
|
|
160
160
|
}
|
|
@@ -5,7 +5,7 @@ import { PeerDescriptor, RouteMessageAck, RouteMessageError, RouteMessageWrapper
|
|
|
5
5
|
import { IRouterRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
6
6
|
import { DuplicateDetector } from './DuplicateDetector'
|
|
7
7
|
import { RoutingMode } from './RoutingSession'
|
|
8
|
-
import {
|
|
8
|
+
import { getDhtAddressFromRaw } from '../../identifiers'
|
|
9
9
|
|
|
10
10
|
interface RouterRpcLocalConfig {
|
|
11
11
|
doRouteMessage: (routedMessage: RouteMessageWrapper, mode?: RoutingMode) => RouteMessageAck
|
|
@@ -37,7 +37,7 @@ export class RouterRpcLocal implements IRouterRpc {
|
|
|
37
37
|
async routeMessage(routedMessage: RouteMessageWrapper): Promise<RouteMessageAck> {
|
|
38
38
|
if (this.config.duplicateRequestDetector.isMostLikelyDuplicate(routedMessage.requestId)) {
|
|
39
39
|
logger.trace(`Routing message ${routedMessage.requestId} from ${getNodeIdFromPeerDescriptor(routedMessage.sourcePeer!)} `
|
|
40
|
-
+ `to ${
|
|
40
|
+
+ `to ${getDhtAddressFromRaw(routedMessage.target)} is likely a duplicate`)
|
|
41
41
|
return createRouteMessageAck(routedMessage, RouteMessageError.DUPLICATE)
|
|
42
42
|
}
|
|
43
43
|
logger.trace(`Processing received routeMessage ${routedMessage.requestId}`)
|
|
@@ -56,7 +56,7 @@ export class RouterRpcLocal implements IRouterRpc {
|
|
|
56
56
|
async forwardMessage(forwardMessage: RouteMessageWrapper): Promise<RouteMessageAck> {
|
|
57
57
|
if (this.config.duplicateRequestDetector.isMostLikelyDuplicate(forwardMessage.requestId)) {
|
|
58
58
|
logger.trace(`Forwarding message ${forwardMessage.requestId} from ${getNodeIdFromPeerDescriptor(forwardMessage.sourcePeer!)} `
|
|
59
|
-
+ `to ${
|
|
59
|
+
+ `to ${getDhtAddressFromRaw(forwardMessage.target)} is likely a duplicate`)
|
|
60
60
|
return createRouteMessageAck(forwardMessage, RouteMessageError.DUPLICATE)
|
|
61
61
|
}
|
|
62
62
|
logger.trace(`Processing received forward routeMessage ${forwardMessage.requestId}`)
|
|
@@ -39,7 +39,8 @@ export class RouterRpcRemote extends RpcRemote<RouterRpcClient> {
|
|
|
39
39
|
const fromNode = previousPeer
|
|
40
40
|
? getNodeIdFromPeerDescriptor(previousPeer)
|
|
41
41
|
: getNodeIdFromPeerDescriptor(params.sourcePeer!)
|
|
42
|
-
|
|
42
|
+
const toNode = getNodeIdFromPeerDescriptor(this.getPeerDescriptor())
|
|
43
|
+
logger.trace(`Failed to send routeMessage from ${fromNode} to ${toNode} with: ${err}`)
|
|
43
44
|
return false
|
|
44
45
|
}
|
|
45
46
|
return true
|
|
@@ -67,9 +68,8 @@ export class RouterRpcRemote extends RpcRemote<RouterRpcClient> {
|
|
|
67
68
|
const fromNode = previousPeer
|
|
68
69
|
? getNodeIdFromPeerDescriptor(previousPeer)
|
|
69
70
|
: getNodeIdFromPeerDescriptor(params.sourcePeer!)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
)
|
|
71
|
+
const toNode = getNodeIdFromPeerDescriptor(this.getPeerDescriptor())
|
|
72
|
+
logger.trace(`Failed to send forwardMessage from ${fromNode} to ${toNode} with: ${err}`)
|
|
73
73
|
return false
|
|
74
74
|
}
|
|
75
75
|
return true
|