@streamr/dht 102.0.0-beta.1 → 102.0.0-beta.2
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/generated/google/protobuf/any.d.ts +180 -0
- package/dist/generated/google/protobuf/any.js +155 -0
- package/dist/generated/google/protobuf/any.js.map +1 -0
- package/dist/generated/google/protobuf/empty.d.ts +31 -0
- package/dist/generated/google/protobuf/empty.js +32 -0
- package/dist/generated/google/protobuf/empty.js.map +1 -0
- package/dist/generated/google/protobuf/timestamp.d.ts +155 -0
- package/dist/generated/google/protobuf/timestamp.js +136 -0
- package/dist/generated/google/protobuf/timestamp.js.map +1 -0
- package/dist/generated/packages/dht/protos/DhtRpc.client.d.ts +361 -0
- package/dist/generated/packages/dht/protos/DhtRpc.client.js +285 -0
- package/dist/generated/packages/dht/protos/DhtRpc.client.js.map +1 -0
- package/dist/generated/packages/dht/protos/DhtRpc.d.ts +999 -0
- package/dist/generated/packages/dht/protos/DhtRpc.js +677 -0
- package/dist/generated/packages/dht/protos/DhtRpc.js.map +1 -0
- package/dist/generated/packages/dht/protos/DhtRpc.server.d.ts +162 -0
- package/dist/generated/packages/dht/protos/DhtRpc.server.js +3 -0
- package/dist/generated/packages/dht/protos/DhtRpc.server.js.map +1 -0
- package/dist/generated/packages/proto-rpc/protos/ProtoRpc.d.ts +87 -0
- package/dist/generated/packages/proto-rpc/protos/ProtoRpc.js +66 -0
- package/dist/generated/packages/proto-rpc/protos/ProtoRpc.js.map +1 -0
- package/dist/package.json +7 -7
- package/package.json +7 -7
- package/src/connection/Connection.ts +0 -28
- package/src/connection/ConnectionLockRpcLocal.ts +0 -78
- package/src/connection/ConnectionLockRpcRemote.ts +0 -64
- package/src/connection/ConnectionLockStates.ts +0 -131
- package/src/connection/ConnectionManager.ts +0 -661
- package/src/connection/ConnectionsView.ts +0 -8
- package/src/connection/ConnectorFacade.ts +0 -217
- package/src/connection/Handshaker.ts +0 -209
- package/src/connection/IConnection.ts +0 -40
- package/src/connection/ManagedConnection.ts +0 -113
- package/src/connection/OutputBuffer.ts +0 -28
- package/src/connection/PendingConnection.ts +0 -68
- package/src/connection/connectivityChecker.ts +0 -108
- package/src/connection/connectivityRequestHandler.ts +0 -116
- package/src/connection/simulator/Simulator.ts +0 -369
- package/src/connection/simulator/SimulatorConnection.ts +0 -137
- package/src/connection/simulator/SimulatorConnector.ts +0 -98
- package/src/connection/simulator/SimulatorTransport.ts +0 -15
- package/src/connection/simulator/pings.ts +0 -42
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -242
- package/src/connection/webrtc/IWebrtcConnection.ts +0 -24
- package/src/connection/webrtc/NodeWebrtcConnection.ts +0 -245
- package/src/connection/webrtc/WebrtcConnector.ts +0 -234
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +0 -108
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +0 -60
- package/src/connection/webrtc/iceServerAsString.ts +0 -15
- package/src/connection/websocket/AbstractWebsocketClientConnection.ts +0 -122
- package/src/connection/websocket/AutoCertifierClientFacade.ts +0 -89
- package/src/connection/websocket/BrowserWebsocketClientConnection.ts +0 -44
- package/src/connection/websocket/NodeWebsocketClientConnection.ts +0 -39
- package/src/connection/websocket/WebsocketClientConnector.ts +0 -119
- package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +0 -38
- package/src/connection/websocket/WebsocketClientConnectorRpcRemote.ts +0 -19
- package/src/connection/websocket/WebsocketServer.ts +0 -164
- package/src/connection/websocket/WebsocketServerConnection.ts +0 -109
- package/src/connection/websocket/WebsocketServerConnector.ts +0 -290
- package/src/dht/DhtNode.ts +0 -683
- package/src/dht/DhtNodeRpcLocal.ts +0 -84
- package/src/dht/DhtNodeRpcRemote.ts +0 -107
- package/src/dht/ExternalApiRpcLocal.ts +0 -58
- package/src/dht/ExternalApiRpcRemote.ts +0 -41
- package/src/dht/PeerManager.ts +0 -305
- package/src/dht/contact/Contact.ts +0 -19
- package/src/dht/contact/ContactList.ts +0 -43
- package/src/dht/contact/RandomContactList.ts +0 -56
- package/src/dht/contact/RingContactList.ts +0 -143
- package/src/dht/contact/RpcRemote.ts +0 -72
- package/src/dht/contact/SortedContactList.ts +0 -173
- package/src/dht/contact/getClosestNodes.ts +0 -24
- package/src/dht/contact/ringIdentifiers.ts +0 -62
- package/src/dht/discovery/DiscoverySession.ts +0 -129
- package/src/dht/discovery/PeerDiscovery.ts +0 -244
- package/src/dht/discovery/RingDiscoverySession.ts +0 -148
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +0 -251
- package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +0 -34
- package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +0 -43
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +0 -231
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +0 -35
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.ts +0 -30
- package/src/dht/routing/DuplicateDetector.ts +0 -34
- package/src/dht/routing/Router.ts +0 -246
- package/src/dht/routing/RouterRpcLocal.ts +0 -78
- package/src/dht/routing/RouterRpcRemote.ts +0 -80
- package/src/dht/routing/RoutingSession.ts +0 -243
- package/src/dht/routing/RoutingTablesCache.ts +0 -60
- package/src/dht/routing/getPreviousPeer.ts +0 -6
- package/src/dht/store/LocalDataStore.ts +0 -84
- package/src/dht/store/StoreManager.ts +0 -170
- package/src/dht/store/StoreRpcLocal.ts +0 -89
- package/src/dht/store/StoreRpcRemote.ts +0 -32
- package/src/exports.ts +0 -33
- package/src/helpers/AddressTools.ts +0 -28
- package/src/helpers/Connectivity.ts +0 -19
- package/src/helpers/browser/isBrowserEnvironment.ts +0 -1
- package/src/helpers/browser/isBrowserEnvironment_override.ts +0 -3
- package/src/helpers/createPeerDescriptor.ts +0 -57
- package/src/helpers/createPeerDescriptorSignaturePayload.ts +0 -28
- package/src/helpers/debugHelpers.ts +0 -9
- package/src/helpers/errors.ts +0 -49
- package/src/helpers/offering.ts +0 -15
- package/src/helpers/protoClasses.ts +0 -57
- package/src/helpers/protoToString.ts +0 -21
- package/src/helpers/version.ts +0 -32
- package/src/identifiers.ts +0 -29
- package/src/rpc-protocol/DhtCallContext.ts +0 -14
- package/src/rpc-protocol/DhtRpcOptions.ts +0 -10
- package/src/transport/ITransport.ts +0 -37
- package/src/transport/ListeningRpcCommunicator.ts +0 -32
- package/src/transport/RoutingRpcCommunicator.ts +0 -66
- package/src/types/ServiceID.ts +0 -1
- package/src/types/textencoding.d.ts +0 -6
- package/test/benchmark/Find.test.ts +0 -72
- package/test/benchmark/KademliaCorrectness.test.ts +0 -114
- package/test/benchmark/RingCorrectness.test.ts +0 -157
- package/test/benchmark/SortedContactListBenchmark.test.ts +0 -108
- package/test/benchmark/WebsocketServerMemoryLeak.test.ts +0 -41
- package/test/benchmark/hybrid-network-simulation/RingContactList.test.ts +0 -71
- package/test/end-to-end/GeoIpLayer0.test.ts +0 -55
- package/test/end-to-end/Layer0-Layer1.test.ts +0 -93
- package/test/end-to-end/Layer0.test.ts +0 -76
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +0 -110
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +0 -137
- package/test/end-to-end/Layer0Webrtc.test.ts +0 -85
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +0 -82
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +0 -76
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +0 -52
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +0 -69
- package/test/end-to-end/memory-leak.test.ts +0 -80
- package/test/integration/ConnectionLocking.test.ts +0 -192
- package/test/integration/ConnectionManager.test.ts +0 -528
- package/test/integration/ConnectivityChecking.test.ts +0 -53
- package/test/integration/DhtJoinPeerDiscovery.test.ts +0 -49
- package/test/integration/DhtNode.test.ts +0 -66
- package/test/integration/DhtNodeExternalAPI.test.ts +0 -48
- package/test/integration/DhtNodeRpcRemote.test.ts +0 -66
- package/test/integration/DhtRpc.test.ts +0 -121
- package/test/integration/Find.test.ts +0 -45
- package/test/integration/GeoIpConnectivityChecking.test.ts +0 -72
- package/test/integration/Layer1-scale.test.ts +0 -189
- package/test/integration/Mock-Layer1-Layer0.test.ts +0 -85
- package/test/integration/MultipleEntryPointJoining.test.ts +0 -105
- package/test/integration/ReplicateData.test.ts +0 -104
- package/test/integration/RouteMessage.test.ts +0 -230
- package/test/integration/RouterRpcRemote.test.ts +0 -77
- package/test/integration/SimultaneousConnections.test.ts +0 -316
- package/test/integration/Store.test.ts +0 -85
- package/test/integration/StoreAndDelete.test.ts +0 -77
- package/test/integration/StoreOnDhtWithThreeNodes.test.ts +0 -59
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +0 -51
- package/test/integration/StoreRpcRemote.test.ts +0 -54
- package/test/integration/WebrtcConnectionManagement.test.ts +0 -191
- package/test/integration/WebrtcConnectorRpc.test.ts +0 -125
- package/test/integration/Websocket.test.ts +0 -65
- package/test/integration/WebsocketClientConnectorRpc.test.ts +0 -69
- package/test/integration/WebsocketConnectionManagement.test.ts +0 -191
- package/test/integration/rpc-connections-over-webrtc.test.ts +0 -123
- package/test/kademlia-simulation/data/nodeids.json +0 -13002
- package/test/kademlia-simulation/data/orderedneighbors.json +0 -1001
- package/test/types/global.d.ts +0 -1
- package/test/unit/AddressTools.test.ts +0 -44
- package/test/unit/AutoCertifierClientFacade.test.ts +0 -58
- package/test/unit/ConnectionManager.test.ts +0 -65
- package/test/unit/ConnectivityHelpers.test.ts +0 -61
- package/test/unit/DiscoverySession.test.ts +0 -87
- package/test/unit/DuplicateDetector.test.ts +0 -31
- package/test/unit/Handshaker.test.ts +0 -169
- package/test/unit/ListeningRpcCommunicator.test.ts +0 -52
- package/test/unit/LocalDataStore.test.ts +0 -108
- package/test/unit/ManagedConnection.test.ts +0 -58
- package/test/unit/PeerManager.test.ts +0 -93
- package/test/unit/PendingConnection.test.ts +0 -57
- package/test/unit/ProtobufMessage.test.ts +0 -21
- package/test/unit/RandomContactList.test.ts +0 -58
- package/test/unit/RecursiveOperationManager.test.ts +0 -161
- package/test/unit/RecursiveOperationSession.test.ts +0 -68
- package/test/unit/Router.test.ts +0 -137
- package/test/unit/RoutingSession.test.ts +0 -86
- package/test/unit/SortedContactList.test.ts +0 -115
- package/test/unit/StoreManager.test.ts +0 -146
- package/test/unit/StoreRpcLocal.test.ts +0 -167
- package/test/unit/WebrtcConnection.test.ts +0 -29
- package/test/unit/WebrtcConnector.test.ts +0 -56
- package/test/unit/WebsocketClientConnector.test.ts +0 -101
- package/test/unit/WebsocketServer.test.ts +0 -66
- package/test/unit/WebsocketServerConnector.test.ts +0 -102
- package/test/unit/connectivityRequestHandler.test.ts +0 -104
- package/test/unit/createPeerDescriptor.test.ts +0 -69
- package/test/unit/customMatchers.test.ts +0 -34
- package/test/unit/getClosestNodes.test.ts +0 -30
- package/test/unit/version.test.ts +0 -18
- package/test/unit/webrtcReplaceInternalIpWithExternalIp.test.ts +0 -18
- package/test/utils/FakeConnectorFacade.ts +0 -41
- package/test/utils/FakeRpcCommunicator.ts +0 -23
- package/test/utils/FakeTransport.ts +0 -79
- package/test/utils/customMatchers.ts +0 -71
- package/test/utils/mock/MockConnection.ts +0 -26
- package/test/utils/mock/MockConnectionsView.ts +0 -18
- package/test/utils/mock/MockRouter.ts +0 -62
- package/test/utils/mock/MockRpcCommunicator.ts +0 -7
- package/test/utils/mock/MockTransport.ts +0 -26
- package/test/utils/mock/mockDataEntry.ts +0 -38
- package/test/utils/topology.ts +0 -79
- package/test/utils/utils.ts +0 -268
- package/tsconfig.browser.json +0 -17
- package/tsconfig.jest.json +0 -25
- package/tsconfig.json +0 -3
- package/tsconfig.node.json +0 -24
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
2
|
-
import { Logger } from '@streamr/utils'
|
|
3
|
-
import {
|
|
4
|
-
DhtAddress,
|
|
5
|
-
areEqualPeerDescriptors,
|
|
6
|
-
toDhtAddress,
|
|
7
|
-
toNodeId,
|
|
8
|
-
toDhtAddressRaw
|
|
9
|
-
} from '../../identifiers'
|
|
10
|
-
import { Any } from '../../../generated/google/protobuf/any'
|
|
11
|
-
import { Timestamp } from '../../../generated/google/protobuf/timestamp'
|
|
12
|
-
import {
|
|
13
|
-
DataEntry,
|
|
14
|
-
PeerDescriptor,
|
|
15
|
-
RecursiveOperation,
|
|
16
|
-
ReplicateDataRequest,
|
|
17
|
-
StoreDataRequest, StoreDataResponse
|
|
18
|
-
} from '../../../generated/packages/dht/protos/DhtRpc'
|
|
19
|
-
import { RoutingRpcCommunicator } from '../../transport/RoutingRpcCommunicator'
|
|
20
|
-
import { ServiceID } from '../../types/ServiceID'
|
|
21
|
-
import { getClosestNodes } from '../contact/getClosestNodes'
|
|
22
|
-
import { RecursiveOperationManager } from '../recursive-operation/RecursiveOperationManager'
|
|
23
|
-
import { LocalDataStore } from './LocalDataStore'
|
|
24
|
-
import { StoreRpcLocal } from './StoreRpcLocal'
|
|
25
|
-
import { StoreRpcRemote } from './StoreRpcRemote'
|
|
26
|
-
|
|
27
|
-
interface StoreManagerOptions {
|
|
28
|
-
rpcCommunicator: RoutingRpcCommunicator
|
|
29
|
-
recursiveOperationManager: RecursiveOperationManager
|
|
30
|
-
localPeerDescriptor: PeerDescriptor
|
|
31
|
-
localDataStore: LocalDataStore
|
|
32
|
-
serviceId: ServiceID
|
|
33
|
-
highestTtl: number
|
|
34
|
-
redundancyFactor: number
|
|
35
|
-
getNeighbors: () => readonly PeerDescriptor[]
|
|
36
|
-
createRpcRemote: (contact: PeerDescriptor) => StoreRpcRemote
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const logger = new Logger(module)
|
|
40
|
-
|
|
41
|
-
export class StoreManager {
|
|
42
|
-
|
|
43
|
-
private readonly options: StoreManagerOptions
|
|
44
|
-
|
|
45
|
-
constructor(options: StoreManagerOptions) {
|
|
46
|
-
this.options = options
|
|
47
|
-
this.registerLocalRpcMethods()
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
private registerLocalRpcMethods() {
|
|
51
|
-
const rpcLocal = new StoreRpcLocal({
|
|
52
|
-
localPeerDescriptor: this.options.localPeerDescriptor,
|
|
53
|
-
localDataStore: this.options.localDataStore,
|
|
54
|
-
replicateDataToContact: (dataEntry: DataEntry, contact: PeerDescriptor) => this.replicateDataToContact(dataEntry, contact),
|
|
55
|
-
getStorers: (dataKey: DhtAddress) => this.getStorers(dataKey)
|
|
56
|
-
})
|
|
57
|
-
this.options.rpcCommunicator.registerRpcMethod(StoreDataRequest, StoreDataResponse, 'storeData',
|
|
58
|
-
(request: StoreDataRequest) => rpcLocal.storeData(request))
|
|
59
|
-
this.options.rpcCommunicator.registerRpcNotification(ReplicateDataRequest, 'replicateData',
|
|
60
|
-
(request: ReplicateDataRequest, context: ServerCallContext) => rpcLocal.replicateData(request, context))
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
onContactAdded(peerDescriptor: PeerDescriptor): void {
|
|
64
|
-
for (const key of this.options.localDataStore.keys()) {
|
|
65
|
-
this.replicateAndUpdateStaleState(key, peerDescriptor)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
private replicateAndUpdateStaleState(dataKey: DhtAddress, newNode: PeerDescriptor): void {
|
|
70
|
-
const storers = this.getStorers(dataKey)
|
|
71
|
-
const storersBeforeContactAdded = storers.filter((p) => !areEqualPeerDescriptors(p, newNode))
|
|
72
|
-
const selfWasPrimaryStorer = areEqualPeerDescriptors(storersBeforeContactAdded[0], this.options.localPeerDescriptor)
|
|
73
|
-
if (selfWasPrimaryStorer) {
|
|
74
|
-
if (storers.some((p) => areEqualPeerDescriptors(p, newNode))) {
|
|
75
|
-
setImmediate(async () => {
|
|
76
|
-
const dataEntries = Array.from(this.options.localDataStore.values(dataKey))
|
|
77
|
-
await Promise.all(dataEntries.map(async (dataEntry) => this.replicateDataToContact(dataEntry, newNode)))
|
|
78
|
-
})
|
|
79
|
-
}
|
|
80
|
-
} else if (!storers.some((p) => areEqualPeerDescriptors(p, this.options.localPeerDescriptor))) {
|
|
81
|
-
this.options.localDataStore.setAllEntriesAsStale(dataKey)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private async replicateDataToContact(dataEntry: DataEntry, contact: PeerDescriptor): Promise<void> {
|
|
86
|
-
const rpcRemote = this.options.createRpcRemote(contact)
|
|
87
|
-
try {
|
|
88
|
-
await rpcRemote.replicateData({ entry: dataEntry }, true)
|
|
89
|
-
} catch (e) {
|
|
90
|
-
logger.trace('replicateData() threw an exception ' + e)
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public async storeDataToDht(key: DhtAddress, data: Any, creator: DhtAddress): Promise<PeerDescriptor[]> {
|
|
95
|
-
logger.debug(`Storing data to DHT ${this.options.serviceId}`)
|
|
96
|
-
const result = await this.options.recursiveOperationManager.execute(key, RecursiveOperation.FIND_CLOSEST_NODES)
|
|
97
|
-
const closestNodes = result.closestNodes
|
|
98
|
-
const successfulNodes: PeerDescriptor[] = []
|
|
99
|
-
const ttl = this.options.highestTtl // ToDo: make TTL decrease according to some nice curve
|
|
100
|
-
const createdAt = Timestamp.now()
|
|
101
|
-
for (let i = 0; i < closestNodes.length && successfulNodes.length < this.options.redundancyFactor; i++) {
|
|
102
|
-
const keyRaw = toDhtAddressRaw(key)
|
|
103
|
-
const creatorRaw = toDhtAddressRaw(creator)
|
|
104
|
-
if (areEqualPeerDescriptors(this.options.localPeerDescriptor, closestNodes[i])) {
|
|
105
|
-
this.options.localDataStore.storeEntry({
|
|
106
|
-
key: keyRaw,
|
|
107
|
-
data,
|
|
108
|
-
creator: creatorRaw,
|
|
109
|
-
createdAt,
|
|
110
|
-
storedAt: Timestamp.now(),
|
|
111
|
-
ttl,
|
|
112
|
-
stale: false,
|
|
113
|
-
deleted: false,
|
|
114
|
-
})
|
|
115
|
-
successfulNodes.push(closestNodes[i])
|
|
116
|
-
continue
|
|
117
|
-
}
|
|
118
|
-
const rpcRemote = this.options.createRpcRemote(closestNodes[i])
|
|
119
|
-
try {
|
|
120
|
-
await rpcRemote.storeData({
|
|
121
|
-
key: keyRaw,
|
|
122
|
-
data,
|
|
123
|
-
creator: creatorRaw,
|
|
124
|
-
createdAt,
|
|
125
|
-
ttl
|
|
126
|
-
})
|
|
127
|
-
successfulNodes.push(closestNodes[i])
|
|
128
|
-
logger.trace('remote.storeData() success')
|
|
129
|
-
} catch (e) {
|
|
130
|
-
logger.trace('remote.storeData() threw an exception ' + e)
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
return successfulNodes
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
private async replicateDataToClosestNodes(): Promise<void> {
|
|
137
|
-
const dataEntries = Array.from(this.options.localDataStore.values())
|
|
138
|
-
await Promise.all(dataEntries.map(async (dataEntry) => {
|
|
139
|
-
const dataKey = toDhtAddress(dataEntry.key)
|
|
140
|
-
const neighbors = getClosestNodes(
|
|
141
|
-
dataKey,
|
|
142
|
-
this.options.getNeighbors(),
|
|
143
|
-
{ maxCount: this.options.redundancyFactor }
|
|
144
|
-
)
|
|
145
|
-
await Promise.all(neighbors.map(async (neighbor) => {
|
|
146
|
-
const rpcRemote = this.options.createRpcRemote(neighbor)
|
|
147
|
-
try {
|
|
148
|
-
await rpcRemote.replicateData({ entry: dataEntry }, false)
|
|
149
|
-
} catch (err) {
|
|
150
|
-
logger.trace('Failed to replicate data in replicateDataToClosestNodes', { err })
|
|
151
|
-
}
|
|
152
|
-
}))
|
|
153
|
-
}))
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
private getStorers(dataKey: DhtAddress, excludedNode?: PeerDescriptor): PeerDescriptor[] {
|
|
157
|
-
return getClosestNodes(
|
|
158
|
-
dataKey,
|
|
159
|
-
[...this.options.getNeighbors(), this.options.localPeerDescriptor],
|
|
160
|
-
{
|
|
161
|
-
maxCount: this.options.redundancyFactor,
|
|
162
|
-
excludedNodeIds: excludedNode !== undefined ? new Set([toNodeId(excludedNode)]) : undefined
|
|
163
|
-
}
|
|
164
|
-
)
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
async destroy(): Promise<void> {
|
|
168
|
-
await this.replicateDataToClosestNodes()
|
|
169
|
-
}
|
|
170
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
2
|
-
import { Logger, executeSafePromise } from '@streamr/utils'
|
|
3
|
-
import { Empty } from '../../../generated/google/protobuf/empty'
|
|
4
|
-
import { Timestamp } from '../../../generated/google/protobuf/timestamp'
|
|
5
|
-
import {
|
|
6
|
-
DataEntry,
|
|
7
|
-
PeerDescriptor,
|
|
8
|
-
ReplicateDataRequest,
|
|
9
|
-
StoreDataRequest, StoreDataResponse
|
|
10
|
-
} from '../../../generated/packages/dht/protos/DhtRpc'
|
|
11
|
-
import { IStoreRpc } from '../../../generated/packages/dht/protos/DhtRpc.server'
|
|
12
|
-
import { DhtCallContext } from '../../rpc-protocol/DhtCallContext'
|
|
13
|
-
import { LocalDataStore } from './LocalDataStore'
|
|
14
|
-
import { areEqualPeerDescriptors, DhtAddress, toDhtAddress } from '../../identifiers'
|
|
15
|
-
|
|
16
|
-
interface StoreRpcLocalOptions {
|
|
17
|
-
localDataStore: LocalDataStore
|
|
18
|
-
localPeerDescriptor: PeerDescriptor
|
|
19
|
-
replicateDataToContact: (dataEntry: DataEntry, contact: PeerDescriptor) => Promise<void>
|
|
20
|
-
getStorers: (key: DhtAddress) => readonly PeerDescriptor[]
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const logger = new Logger(module)
|
|
24
|
-
|
|
25
|
-
export class StoreRpcLocal implements IStoreRpc {
|
|
26
|
-
|
|
27
|
-
private readonly options: StoreRpcLocalOptions
|
|
28
|
-
|
|
29
|
-
constructor(options: StoreRpcLocalOptions) {
|
|
30
|
-
this.options = options
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async storeData(request: StoreDataRequest): Promise<StoreDataResponse> {
|
|
34
|
-
logger.trace('storeData()')
|
|
35
|
-
const key = toDhtAddress(request.key)
|
|
36
|
-
const isLocalNodeStorer = this.isLocalNodeStorer(key)
|
|
37
|
-
this.options.localDataStore.storeEntry({
|
|
38
|
-
key: request.key,
|
|
39
|
-
data: request.data,
|
|
40
|
-
creator: request.creator,
|
|
41
|
-
createdAt: request.createdAt,
|
|
42
|
-
storedAt: Timestamp.now(),
|
|
43
|
-
ttl: request.ttl,
|
|
44
|
-
stale: !isLocalNodeStorer,
|
|
45
|
-
deleted: false
|
|
46
|
-
})
|
|
47
|
-
if (!isLocalNodeStorer) {
|
|
48
|
-
this.options.localDataStore.setAllEntriesAsStale(key)
|
|
49
|
-
}
|
|
50
|
-
return {}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public async replicateData(request: ReplicateDataRequest, context: ServerCallContext): Promise<Empty> {
|
|
54
|
-
logger.trace('server-side replicateData()')
|
|
55
|
-
const dataEntry = request.entry!
|
|
56
|
-
const wasStored = this.options.localDataStore.storeEntry(dataEntry)
|
|
57
|
-
if (wasStored) {
|
|
58
|
-
this.replicateDataToNeighbors((context as DhtCallContext).incomingSourceDescriptor!, request.entry!)
|
|
59
|
-
}
|
|
60
|
-
const key = toDhtAddress(dataEntry.key)
|
|
61
|
-
if (!this.isLocalNodeStorer(key)) {
|
|
62
|
-
this.options.localDataStore.setAllEntriesAsStale(key)
|
|
63
|
-
}
|
|
64
|
-
logger.trace('server-side replicateData() at end')
|
|
65
|
-
return {}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
private isLocalNodeStorer(dataKey: DhtAddress): boolean {
|
|
69
|
-
return this.options.getStorers(dataKey).some((p) => areEqualPeerDescriptors(p, this.options.localPeerDescriptor))
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
private replicateDataToNeighbors(requestor: PeerDescriptor, dataEntry: DataEntry): void {
|
|
73
|
-
const dataKey = toDhtAddress(dataEntry.key)
|
|
74
|
-
const storers = this.options.getStorers(dataKey)
|
|
75
|
-
const isLocalNodePrimaryStorer = areEqualPeerDescriptors(storers[0], this.options.localPeerDescriptor)
|
|
76
|
-
// If we are the closest to the data, get storageRedundancyFactor - 1 nearest node to the data, and
|
|
77
|
-
// replicate to all those node. Otherwise replicate only to the one closest one. And never replicate
|
|
78
|
-
// to the requestor nor to itself.
|
|
79
|
-
const targets = (isLocalNodePrimaryStorer ? storers : [storers[0]]).filter(
|
|
80
|
-
(p) => !areEqualPeerDescriptors(p, requestor) && !areEqualPeerDescriptors(p, this.options.localPeerDescriptor)
|
|
81
|
-
)
|
|
82
|
-
targets.forEach((target) => {
|
|
83
|
-
setImmediate(() => {
|
|
84
|
-
executeSafePromise(() => this.options.replicateDataToContact(dataEntry, target))
|
|
85
|
-
})
|
|
86
|
-
})
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { toNodeId } from '../../identifiers'
|
|
2
|
-
import {
|
|
3
|
-
ReplicateDataRequest,
|
|
4
|
-
StoreDataRequest
|
|
5
|
-
} from '../../../generated/packages/dht/protos/DhtRpc'
|
|
6
|
-
import { StoreRpcClient } from '../../../generated/packages/dht/protos/DhtRpc.client'
|
|
7
|
-
import { EXISTING_CONNECTION_TIMEOUT, RpcRemote } from '../contact/RpcRemote'
|
|
8
|
-
|
|
9
|
-
export class StoreRpcRemote extends RpcRemote<StoreRpcClient> {
|
|
10
|
-
|
|
11
|
-
async storeData(request: StoreDataRequest): Promise<void> {
|
|
12
|
-
const options = this.formDhtRpcOptions()
|
|
13
|
-
try {
|
|
14
|
-
await this.getClient().storeData(request, options)
|
|
15
|
-
} catch (err) {
|
|
16
|
-
const to = toNodeId(this.getPeerDescriptor())
|
|
17
|
-
const from = toNodeId(this.getLocalPeerDescriptor())
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
19
|
-
throw new Error(`Could not store data to ${to} from ${from} ${err}`)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async replicateData(request: ReplicateDataRequest, connect: boolean): Promise<void> {
|
|
24
|
-
const options = this.formDhtRpcOptions({
|
|
25
|
-
timeout: EXISTING_CONNECTION_TIMEOUT,
|
|
26
|
-
notification: true,
|
|
27
|
-
connect
|
|
28
|
-
})
|
|
29
|
-
return this.getClient().replicateData(request, options)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
}
|
package/src/exports.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
export { DhtNode, DhtNodeEvents, DhtNodeOptions } from './dht/DhtNode'
|
|
2
|
-
export { ListeningRpcCommunicator } from './transport/ListeningRpcCommunicator'
|
|
3
|
-
export { RoutingRpcCommunicator } from './transport/RoutingRpcCommunicator'
|
|
4
|
-
export { Simulator, LatencyType } from './connection/simulator/Simulator'
|
|
5
|
-
export { SimulatorTransport } from './connection/simulator/SimulatorTransport'
|
|
6
|
-
export { getRandomRegion, getRegionDelayMatrix } from './connection/simulator/pings'
|
|
7
|
-
export { PeerDescriptor, Message, NodeType, DataEntry } from '../generated/packages/dht/protos/DhtRpc'
|
|
8
|
-
export { ITransport, TransportEvents } from './transport/ITransport'
|
|
9
|
-
export { ConnectionManager, ConnectionLocker, PortRange, TlsCertificate } from './connection/ConnectionManager'
|
|
10
|
-
export { ConnectionsView } from './connection/ConnectionsView'
|
|
11
|
-
export { LockID } from './connection/ConnectionLockStates'
|
|
12
|
-
export { DefaultConnectorFacade } from './connection/ConnectorFacade'
|
|
13
|
-
export { DhtRpcOptions } from './rpc-protocol/DhtRpcOptions'
|
|
14
|
-
export { RpcRemote, EXISTING_CONNECTION_TIMEOUT } from './dht/contact/RpcRemote'
|
|
15
|
-
export { IceServer } from './connection/webrtc/WebrtcConnector'
|
|
16
|
-
export { DhtCallContext } from './rpc-protocol/DhtCallContext'
|
|
17
|
-
export { WebsocketClientConnection } from './connection/websocket/NodeWebsocketClientConnection'
|
|
18
|
-
export { ManagedConnection } from './connection/ManagedConnection'
|
|
19
|
-
export { PendingConnection } from './connection/PendingConnection'
|
|
20
|
-
export { IConnection } from './connection/IConnection'
|
|
21
|
-
export { ConnectionType } from './connection/IConnection'
|
|
22
|
-
export { ServiceID } from './types/ServiceID'
|
|
23
|
-
export { RingContacts } from './dht/contact/RingContactList'
|
|
24
|
-
export { createOutgoingHandshaker } from './connection/Handshaker'
|
|
25
|
-
export {
|
|
26
|
-
DhtAddress,
|
|
27
|
-
DhtAddressRaw,
|
|
28
|
-
toDhtAddress,
|
|
29
|
-
toDhtAddressRaw,
|
|
30
|
-
randomDhtAddress,
|
|
31
|
-
areEqualPeerDescriptors,
|
|
32
|
-
toNodeId
|
|
33
|
-
} from './identifiers'
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import ipaddr from 'ipaddr.js'
|
|
2
|
-
|
|
3
|
-
// IPv4 private address ranges as specified by RFC 1918
|
|
4
|
-
// and private loopback addresses
|
|
5
|
-
const IPv4PrivateRanges = [
|
|
6
|
-
'10.0.0.0/8',
|
|
7
|
-
'172.16.0.0/12',
|
|
8
|
-
'192.168.0.0/16',
|
|
9
|
-
'127.0.0.0/8'
|
|
10
|
-
].map((a) => ipaddr.parseCIDR(a))
|
|
11
|
-
|
|
12
|
-
export function isPrivateIPv4(address: string): boolean {
|
|
13
|
-
if (ipaddr.IPv4.isValid(address)) {
|
|
14
|
-
const ip = ipaddr.IPv4.parse(address)
|
|
15
|
-
for (const range of IPv4PrivateRanges) {
|
|
16
|
-
if (ip.match(range)) {
|
|
17
|
-
return true
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return false
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function getAddressFromIceCandidate(candidate: string): string | undefined {
|
|
26
|
-
const fields = candidate.split(' ').filter((field) => field.length > 0)
|
|
27
|
-
return fields.length >= 5 && ipaddr.isValid(fields[4]) ? fields[4] : undefined
|
|
28
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { ConnectionType } from '../connection/IConnection'
|
|
2
|
-
import { ConnectivityMethod, NodeType, PeerDescriptor } from '../../generated/packages/dht/protos/DhtRpc'
|
|
3
|
-
import { isPrivateIPv4 } from './AddressTools'
|
|
4
|
-
|
|
5
|
-
export const canOpenConnectionFromBrowser = (websocketServer: ConnectivityMethod): boolean => {
|
|
6
|
-
const hasPrivateAddress = ((websocketServer.host === 'localhost') || isPrivateIPv4(websocketServer.host))
|
|
7
|
-
return websocketServer.tls || hasPrivateAddress
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const expectedConnectionType = (localPeerDescriptor: PeerDescriptor, remotePeerDescriptor: PeerDescriptor): ConnectionType => {
|
|
11
|
-
if (remotePeerDescriptor.websocket
|
|
12
|
-
&& (localPeerDescriptor.type !== NodeType.BROWSER || canOpenConnectionFromBrowser(remotePeerDescriptor.websocket))) {
|
|
13
|
-
return ConnectionType.WEBSOCKET_CLIENT
|
|
14
|
-
} else if (localPeerDescriptor.websocket
|
|
15
|
-
&& (remotePeerDescriptor.type !== NodeType.BROWSER || canOpenConnectionFromBrowser(localPeerDescriptor.websocket))) {
|
|
16
|
-
return ConnectionType.WEBSOCKET_SERVER
|
|
17
|
-
}
|
|
18
|
-
return ConnectionType.WEBRTC
|
|
19
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const isBrowserEnvironment = (): boolean => false
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createSignature,
|
|
3
|
-
hash
|
|
4
|
-
} from '@streamr/utils'
|
|
5
|
-
import crypto from 'crypto'
|
|
6
|
-
import { isBrowserEnvironment } from '../helpers/browser/isBrowserEnvironment'
|
|
7
|
-
import { createPeerDescriptorSignaturePayload } from '../helpers/createPeerDescriptorSignaturePayload'
|
|
8
|
-
import { DhtAddress, DhtAddressRaw, toDhtAddressRaw } from '../identifiers'
|
|
9
|
-
import {
|
|
10
|
-
ConnectivityResponse,
|
|
11
|
-
NodeType,
|
|
12
|
-
PeerDescriptor
|
|
13
|
-
} from '../../generated/packages/dht/protos/DhtRpc'
|
|
14
|
-
|
|
15
|
-
const calculateNodeIdRaw = (ipAddress: number, privateKey: Uint8Array): DhtAddressRaw => {
|
|
16
|
-
// nodeId is calculated as
|
|
17
|
-
// concatenate(
|
|
18
|
-
// get104leastSignificatBits(hash(ipAddress)),
|
|
19
|
-
// get56leastSignificatBits(sign(ipAddress))
|
|
20
|
-
// )
|
|
21
|
-
const ipAsBuffer = Buffer.alloc(4)
|
|
22
|
-
ipAsBuffer.writeUInt32BE(ipAddress)
|
|
23
|
-
const ipHash = hash(ipAsBuffer)
|
|
24
|
-
const signature = createSignature(ipAsBuffer, privateKey)
|
|
25
|
-
const nodeIdRaw = Buffer.concat([
|
|
26
|
-
ipHash.subarray(ipHash.length - 13, ipHash.length),
|
|
27
|
-
signature.subarray(signature.length - 7, signature.length)
|
|
28
|
-
])
|
|
29
|
-
return nodeIdRaw
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const createPeerDescriptor = (connectivityResponse: ConnectivityResponse, region: number, nodeId?: DhtAddress): PeerDescriptor => {
|
|
33
|
-
const privateKey = crypto.randomBytes(32)
|
|
34
|
-
const publicKey = crypto.randomBytes(20) // TODO calculate publicKey from privateKey
|
|
35
|
-
let nodeIdRaw: DhtAddressRaw
|
|
36
|
-
if (nodeId !== undefined) {
|
|
37
|
-
nodeIdRaw = toDhtAddressRaw(nodeId)
|
|
38
|
-
} else {
|
|
39
|
-
nodeIdRaw = calculateNodeIdRaw(connectivityResponse.ipAddress, privateKey)
|
|
40
|
-
}
|
|
41
|
-
const ret: PeerDescriptor = {
|
|
42
|
-
nodeId: nodeIdRaw,
|
|
43
|
-
type: isBrowserEnvironment() ? NodeType.BROWSER : NodeType.NODEJS,
|
|
44
|
-
ipAddress: connectivityResponse.ipAddress,
|
|
45
|
-
region,
|
|
46
|
-
publicKey
|
|
47
|
-
}
|
|
48
|
-
if (connectivityResponse.websocket) {
|
|
49
|
-
ret.websocket = {
|
|
50
|
-
host: connectivityResponse.websocket.host,
|
|
51
|
-
port: connectivityResponse.websocket.port,
|
|
52
|
-
tls: connectivityResponse.websocket.tls
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
ret.signature = createSignature(createPeerDescriptorSignaturePayload(ret), privateKey)
|
|
56
|
-
return ret
|
|
57
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ConnectivityMethod, PeerDescriptor } from '../../generated/packages/dht/protos/DhtRpc'
|
|
2
|
-
|
|
3
|
-
function convertUnsignedIntegerToBuffer(number: number): Buffer {
|
|
4
|
-
const buffer = Buffer.alloc(4)
|
|
5
|
-
buffer.writeUInt32BE(number)
|
|
6
|
-
return buffer
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const createPeerDescriptorSignaturePayload = (peerDescriptor: PeerDescriptor): Uint8Array => {
|
|
10
|
-
const separator = Buffer.from(',')
|
|
11
|
-
const buffers = [
|
|
12
|
-
peerDescriptor.type !== undefined ? convertUnsignedIntegerToBuffer(peerDescriptor.type) : new Uint8Array(0),
|
|
13
|
-
separator,
|
|
14
|
-
peerDescriptor.udp !== undefined ? ConnectivityMethod.toBinary(peerDescriptor.udp) : new Uint8Array(0),
|
|
15
|
-
separator,
|
|
16
|
-
peerDescriptor.tcp !== undefined ? ConnectivityMethod.toBinary(peerDescriptor.tcp) : new Uint8Array(0),
|
|
17
|
-
separator,
|
|
18
|
-
peerDescriptor.websocket !== undefined ? ConnectivityMethod.toBinary(peerDescriptor.websocket) : new Uint8Array(0),
|
|
19
|
-
separator,
|
|
20
|
-
peerDescriptor.region !== undefined ? convertUnsignedIntegerToBuffer(peerDescriptor.region) : new Uint8Array(0),
|
|
21
|
-
separator,
|
|
22
|
-
peerDescriptor.ipAddress !== undefined ? convertUnsignedIntegerToBuffer(peerDescriptor.ipAddress) : new Uint8Array(0),
|
|
23
|
-
separator,
|
|
24
|
-
peerDescriptor.publicKey !== undefined ? Buffer.from(peerDescriptor.publicKey) : new Uint8Array(0)
|
|
25
|
-
]
|
|
26
|
-
return Buffer.concat(buffers)
|
|
27
|
-
}
|
|
28
|
-
|
package/src/helpers/errors.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/* eslint-disable max-len */
|
|
2
|
-
|
|
3
|
-
export enum ErrorCode {
|
|
4
|
-
CONNECTION_FAILED = 'CONNECTION_FAILED',
|
|
5
|
-
COULD_NOT_ROUTE = 'COULD_NOT_ROUTE',
|
|
6
|
-
STARTING_WEBSOCKET_SERVER_FAILED = 'STARTING_WEBSOCKET_SERVER_FAILED',
|
|
7
|
-
WEBSOCKET_CONNECTION_REQUEST_REJECTED = 'WEBSOCKET_CONNECTION_REQUEST_REJECTED',
|
|
8
|
-
COULD_NOT_START = 'COULD_NOT_START',
|
|
9
|
-
COULD_NOT_STOP = 'COULD_NOT_STOP',
|
|
10
|
-
CANNOT_CONNECT_TO_SELF = 'CANNOT_CONNECT_TO_SELF',
|
|
11
|
-
NOT_IMPLEMENTED = 'NOT_IMPLEMENTED',
|
|
12
|
-
ILLEGAL_RTC_PEER_CONNECTION_STATE = 'ILLEGAL_RTC_PEER_CONNECTION_STATE',
|
|
13
|
-
ILLEGAL_ARGUMENTS = 'ILLEGAL_ARGUMENTS',
|
|
14
|
-
CONNECTIVITY_RESPONSE_NOT_RECEIVED_BEFORE_TIMEOUT = 'CONNECTIVITY_RESPONSE_NOT_RECEIVED_BEFORE_TIMEOUT',
|
|
15
|
-
CONNECTION_LOCKER = 'CONNECTION_LOCKER',
|
|
16
|
-
DHT_JOIN_TIMEOUT = 'DHT_JOIN_TIMEOUT',
|
|
17
|
-
SEND_FAILED = 'SEND_FAILED',
|
|
18
|
-
GETTING_DATA_FAILED = 'GETTING_DATA_FAILED',
|
|
19
|
-
CONNECTION_NOT_OPEN = 'CONNECTION_NOT_OPEN'
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
class Err extends Error {
|
|
23
|
-
|
|
24
|
-
public code: ErrorCode
|
|
25
|
-
public originalError?: Error | string
|
|
26
|
-
|
|
27
|
-
constructor(code: ErrorCode, message?: string, originalError?: Error | string) {
|
|
28
|
-
super(message)
|
|
29
|
-
this.code = code
|
|
30
|
-
this.originalError = originalError
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export class ConnectionFailed extends Err { constructor( message?: string, originalError?: Error | string) { super(ErrorCode.CONNECTION_FAILED, message, originalError) } }
|
|
35
|
-
export class CouldNotRoute extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.COULD_NOT_ROUTE, message, originalError) } }
|
|
36
|
-
export class WebsocketServerStartError extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.STARTING_WEBSOCKET_SERVER_FAILED, message, originalError) } }
|
|
37
|
-
export class WebsocketConnectionRequestRejected extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.WEBSOCKET_CONNECTION_REQUEST_REJECTED, message, originalError) } }
|
|
38
|
-
export class CouldNotStart extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.COULD_NOT_START, message, originalError) } }
|
|
39
|
-
export class CouldNotStop extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.COULD_NOT_STOP, message, originalError) } }
|
|
40
|
-
export class CannotConnectToSelf extends Err { constructor(message?: string, originalError?: Error | string) {super(ErrorCode.CANNOT_CONNECT_TO_SELF, message, originalError) } }
|
|
41
|
-
export class NotImplemented extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.NOT_IMPLEMENTED, message, originalError) } }
|
|
42
|
-
export class IllegalRtcPeerConnectionState extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.ILLEGAL_RTC_PEER_CONNECTION_STATE, message, originalError) } }
|
|
43
|
-
export class IllegalArguments extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.ILLEGAL_ARGUMENTS, message, originalError) } }
|
|
44
|
-
export class ConnectivityResponseTimeout extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.CONNECTIVITY_RESPONSE_NOT_RECEIVED_BEFORE_TIMEOUT, message, originalError) } }
|
|
45
|
-
export class ConnectionLocker extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.CONNECTION_LOCKER, message, originalError) } }
|
|
46
|
-
export class DhtJoinTimeout extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.DHT_JOIN_TIMEOUT, message, originalError) } }
|
|
47
|
-
export class SendFailed extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.SEND_FAILED, message, originalError) } }
|
|
48
|
-
export class GettingDataFailed extends Err { constructor(message?: string, originalError?: Error | string) { super(ErrorCode.GETTING_DATA_FAILED, message, originalError) } }
|
|
49
|
-
export class ConnectionNotOpen extends Err { constructor( message?: string, originalError?: Error | string) { super(ErrorCode.CONNECTION_NOT_OPEN, message, originalError) } }
|
package/src/helpers/offering.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import crypto from 'crypto'
|
|
2
|
-
import { DhtAddress } from '../identifiers'
|
|
3
|
-
|
|
4
|
-
type Offerer = 'local' | 'remote'
|
|
5
|
-
|
|
6
|
-
export const getOfferer = (localNodeId: DhtAddress, remoteNodeId: DhtAddress): Offerer => {
|
|
7
|
-
return getOfferingHash(localNodeId + ',' + remoteNodeId) < getOfferingHash(remoteNodeId + ',' + localNodeId)
|
|
8
|
-
? 'local'
|
|
9
|
-
: 'remote'
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const getOfferingHash = (idPair: string): number => {
|
|
13
|
-
const buffer = crypto.createHash('md5').update(idPair).digest()
|
|
14
|
-
return buffer.readInt32LE(0)
|
|
15
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { IMessageType } from '@protobuf-ts/runtime'
|
|
2
|
-
import {
|
|
3
|
-
ClosestPeersRequest,
|
|
4
|
-
ClosestPeersResponse,
|
|
5
|
-
ConnectivityMethod,
|
|
6
|
-
ConnectivityRequest,
|
|
7
|
-
ConnectivityResponse,
|
|
8
|
-
DisconnectNotice,
|
|
9
|
-
HandshakeRequest,
|
|
10
|
-
HandshakeResponse,
|
|
11
|
-
LeaveNotice,
|
|
12
|
-
Message,
|
|
13
|
-
PeerDescriptor,
|
|
14
|
-
PingRequest,
|
|
15
|
-
PingResponse,
|
|
16
|
-
RecursiveOperationResponse,
|
|
17
|
-
RecursiveOperationRequest,
|
|
18
|
-
RouteMessageAck,
|
|
19
|
-
RouteMessageWrapper,
|
|
20
|
-
WebsocketConnectionRequest,
|
|
21
|
-
WebrtcConnectionRequest,
|
|
22
|
-
RtcOffer,
|
|
23
|
-
RtcAnswer,
|
|
24
|
-
IceCandidate,
|
|
25
|
-
LockRequest,
|
|
26
|
-
UnlockRequest,
|
|
27
|
-
LockResponse
|
|
28
|
-
|
|
29
|
-
} from '../../generated/packages/dht/protos/DhtRpc'
|
|
30
|
-
|
|
31
|
-
export const protoClasses: IMessageType<any>[] = [
|
|
32
|
-
ClosestPeersRequest,
|
|
33
|
-
ClosestPeersResponse,
|
|
34
|
-
RecursiveOperationRequest,
|
|
35
|
-
RecursiveOperationResponse,
|
|
36
|
-
PingRequest,
|
|
37
|
-
PingResponse,
|
|
38
|
-
LeaveNotice,
|
|
39
|
-
PeerDescriptor,
|
|
40
|
-
ConnectivityMethod,
|
|
41
|
-
DisconnectNotice,
|
|
42
|
-
RouteMessageWrapper,
|
|
43
|
-
RouteMessageAck,
|
|
44
|
-
ConnectivityRequest,
|
|
45
|
-
ConnectivityResponse,
|
|
46
|
-
HandshakeRequest,
|
|
47
|
-
HandshakeResponse,
|
|
48
|
-
Message,
|
|
49
|
-
WebsocketConnectionRequest,
|
|
50
|
-
WebrtcConnectionRequest,
|
|
51
|
-
RtcOffer,
|
|
52
|
-
RtcAnswer,
|
|
53
|
-
IceCandidate,
|
|
54
|
-
LockRequest,
|
|
55
|
-
UnlockRequest,
|
|
56
|
-
LockResponse
|
|
57
|
-
]
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { IMessageType } from '@protobuf-ts/runtime'
|
|
2
|
-
|
|
3
|
-
import { protoClasses } from './protoClasses'
|
|
4
|
-
import { protoClasses as rpcProtoClasses } from '@streamr/proto-rpc'
|
|
5
|
-
|
|
6
|
-
const typeRegistry = protoClasses.concat(rpcProtoClasses)
|
|
7
|
-
|
|
8
|
-
export function protoToString<T extends object, ClassType extends IMessageType<T>>(protoObj: T,
|
|
9
|
-
objectType: ClassType): string {
|
|
10
|
-
|
|
11
|
-
let ret = ''
|
|
12
|
-
try {
|
|
13
|
-
ret = objectType.toJsonString(protoObj, {
|
|
14
|
-
typeRegistry
|
|
15
|
-
})
|
|
16
|
-
} catch (_e) {
|
|
17
|
-
ret = '[type not in type registry]'
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return ret
|
|
21
|
-
}
|
package/src/helpers/version.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export const LOCAL_PROTOCOL_VERSION = '1.1'
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* When two nodes negotiate whether they are compatible or not, it is up to the
|
|
5
|
-
* newer version to decide if it supports the old version or not.
|
|
6
|
-
*
|
|
7
|
-
* The older version assumes optimistically that it may be supported by the newer
|
|
8
|
-
* version. It can't know for sure, but the other node will tell if it is not
|
|
9
|
-
* supported (e.g. rejecting the handshake with UNSUPPORTED_PROTOCOL_VERSION error).
|
|
10
|
-
*/
|
|
11
|
-
export const isMaybeSupportedProtocolVersion = (remoteVersion: string): boolean => {
|
|
12
|
-
const localMajor = parseVersion(LOCAL_PROTOCOL_VERSION)!.major
|
|
13
|
-
const remoteMajor = parseVersion(remoteVersion)?.major
|
|
14
|
-
if ((remoteMajor === undefined) || (remoteMajor < localMajor)) {
|
|
15
|
-
return false
|
|
16
|
-
} else {
|
|
17
|
-
// TODO implement proper checking when there are new protocol versions
|
|
18
|
-
return true
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const parseVersion = (version: string): { major: number, minor: number } | undefined => {
|
|
23
|
-
const parts = version.split('.')
|
|
24
|
-
if (parts.length === 2) {
|
|
25
|
-
const values = parts.map((p) => Number(p))
|
|
26
|
-
if (!values.some((v) => isNaN(v))) {
|
|
27
|
-
return { major: values[0], minor: values[1] }
|
|
28
|
-
}
|
|
29
|
-
} else {
|
|
30
|
-
return undefined
|
|
31
|
-
}
|
|
32
|
-
}
|