@streamr/dht 100.0.0-testnet-one.4 → 100.0.0-testnet-two.1
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 +66 -0
- 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/ConnectionLockRpcLocal.js +4 -4
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +3 -5
- package/dist/src/connection/ConnectionLockRpcRemote.js +4 -7
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +4 -6
- package/dist/src/connection/ConnectionManager.js +48 -46
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +2 -2
- package/dist/src/connection/ConnectorFacade.js +2 -1
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +1 -1
- package/dist/src/connection/Handshaker.js +14 -5
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +4 -7
- package/dist/src/connection/ManagedConnection.js +9 -13
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/connectivityChecker.js +1 -0
- package/dist/src/connection/connectivityChecker.js.map +1 -1
- package/dist/src/connection/simulator/Simulator.d.ts +0 -3
- package/dist/src/connection/simulator/Simulator.js +3 -51
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.js +5 -5
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +2 -2
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +0 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js +7 -9
- 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 +21 -14
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +2 -5
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +1 -4
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +1 -2
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +3 -4
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +2 -7
- package/dist/src/connection/websocket/WebsocketConnector.js +48 -44
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +3 -2
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +8 -10
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +3 -6
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +0 -3
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketServer.d.ts +1 -4
- package/dist/src/connection/websocket/WebsocketServer.js +11 -12
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +10 -13
- package/dist/src/dht/DhtNode.js +41 -54
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +2 -3
- package/dist/src/dht/DhtNodeRpcLocal.js +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +7 -7
- package/dist/src/dht/DhtNodeRpcRemote.js +9 -11
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +5 -4
- package/dist/src/dht/ExternalApiRpcLocal.js +3 -2
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +2 -2
- package/dist/src/dht/ExternalApiRpcRemote.js +2 -0
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +13 -14
- package/dist/src/dht/PeerManager.js +45 -60
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/Contact.d.ts +2 -2
- package/dist/src/dht/contact/Contact.js +2 -2
- package/dist/src/dht/contact/Contact.js.map +1 -1
- package/dist/src/dht/contact/ContactList.d.ts +7 -7
- package/dist/src/dht/contact/ContactList.js +3 -3
- 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 +12 -11
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/RpcRemote.d.ts +7 -4
- package/dist/src/dht/contact/RpcRemote.js +5 -2
- 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 +33 -29
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +2 -4
- package/dist/src/dht/discovery/DiscoverySession.js +19 -23
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +2 -5
- package/dist/src/dht/discovery/PeerDiscovery.js +12 -20
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +36 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +165 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +14 -0
- package/dist/src/dht/{find/FindRpcLocal.js → recursive-operation/RecursiveOperationRpcLocal.js} +7 -7
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.d.ts +6 -0
- package/dist/src/dht/{routing/FindRpcRemote.js → recursive-operation/RecursiveOperationRpcRemote.js} +11 -11
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +42 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +182 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +12 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +17 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.d.ts +6 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js +21 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js.map +1 -0
- package/dist/src/dht/routing/Router.d.ts +4 -19
- package/dist/src/dht/routing/Router.js +41 -33
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.js +6 -5
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcRemote.d.ts +2 -2
- package/dist/src/dht/routing/RouterRpcRemote.js +7 -7
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +20 -16
- package/dist/src/dht/routing/RoutingSession.js +37 -45
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +11 -13
- package/dist/src/dht/store/LocalDataStore.js +36 -56
- package/dist/src/dht/store/LocalDataStore.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +33 -0
- package/dist/src/dht/store/StoreManager.js +186 -0
- package/dist/src/dht/store/StoreManager.js.map +1 -0
- package/dist/src/dht/store/StoreRpcLocal.d.ts +8 -40
- package/dist/src/dht/store/StoreRpcLocal.js +13 -229
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.d.ts +5 -5
- package/dist/src/dht/store/StoreRpcRemote.js +4 -5
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/exports.d.ts +2 -0
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/MapWithTtl.d.ts +14 -0
- package/dist/src/helpers/MapWithTtl.js +59 -0
- package/dist/src/helpers/MapWithTtl.js.map +1 -0
- package/dist/src/helpers/PeerID.d.ts +2 -0
- package/dist/src/helpers/PeerID.js +4 -0
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/nodeId.d.ts +5 -0
- package/dist/src/helpers/nodeId.js +18 -1
- package/dist/src/helpers/nodeId.js.map +1 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +2 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +2 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/helpers/protoClasses.js +2 -3
- package/dist/src/helpers/protoClasses.js.map +1 -1
- package/dist/src/helpers/versionCompatibility.d.ts +2 -0
- package/dist/src/helpers/versionCompatibility.js +18 -0
- package/dist/src/helpers/versionCompatibility.js.map +1 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +21 -22
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +18 -18
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +49 -65
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +43 -58
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +11 -12
- package/dist/src/rpc-protocol/DhtCallContext.d.ts +2 -1
- package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -1
- package/dist/src/rpc-protocol/DhtRpcOptions.d.ts +2 -1
- package/dist/src/transport/ITransport.d.ts +9 -1
- package/dist/src/transport/ITransport.js +5 -0
- package/dist/src/transport/ITransport.js.map +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.js +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -1
- package/dist/src/transport/RoutingRpcCommunicator.js +16 -11
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/jest.config.js +4 -1
- package/karma-setup.js +2 -0
- package/karma.config.js +11 -7
- package/package.json +6 -7
- package/protos/DhtRpc.proto +20 -24
- package/src/connection/ConnectionLockHandler.ts +15 -15
- package/src/connection/ConnectionLockRpcLocal.ts +8 -9
- package/src/connection/ConnectionLockRpcRemote.ts +7 -16
- package/src/connection/ConnectionManager.ts +58 -55
- package/src/connection/ConnectorFacade.ts +3 -0
- package/src/connection/Handshaker.ts +21 -6
- package/src/connection/ManagedConnection.ts +20 -34
- package/src/connection/connectivityChecker.ts +1 -0
- package/src/connection/simulator/Simulator.ts +5 -36
- package/src/connection/simulator/SimulatorConnector.ts +8 -8
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -3
- package/src/connection/webrtc/NodeWebrtcConnection.ts +3 -4
- package/src/connection/webrtc/WebrtcConnector.ts +11 -13
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +25 -18
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +3 -13
- package/src/connection/websocket/AutoCertifierClientFacade.ts +4 -6
- package/src/connection/websocket/ClientWebsocket.ts +1 -0
- package/src/connection/websocket/WebsocketConnector.ts +57 -61
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +11 -13
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +4 -15
- package/src/connection/websocket/WebsocketServer.ts +14 -17
- package/src/dht/DhtNode.ts +69 -71
- package/src/dht/DhtNodeRpcLocal.ts +1 -3
- package/src/dht/DhtNodeRpcRemote.ts +14 -18
- package/src/dht/ExternalApiRpcLocal.ts +16 -6
- package/src/dht/ExternalApiRpcRemote.ts +5 -3
- package/src/dht/PeerManager.ts +73 -90
- package/src/dht/contact/Contact.ts +5 -4
- package/src/dht/contact/ContactList.ts +9 -9
- package/src/dht/contact/RandomContactList.ts +14 -14
- package/src/dht/contact/RpcRemote.ts +10 -7
- package/src/dht/contact/SortedContactList.ts +48 -45
- package/src/dht/discovery/DiscoverySession.ts +25 -27
- package/src/dht/discovery/PeerDiscovery.ts +16 -37
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +240 -0
- package/src/dht/{find/FindRpcLocal.ts → recursive-operation/RecursiveOperationRpcLocal.ts} +9 -9
- package/src/dht/{routing/FindRpcRemote.ts → recursive-operation/RecursiveOperationRpcRemote.ts} +10 -10
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +224 -0
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +25 -0
- package/src/dht/{find/FindSessionRpcRemote.ts → recursive-operation/RecursiveOperationSessionRpcRemote.ts} +8 -8
- package/src/dht/routing/Router.ts +45 -64
- package/src/dht/routing/RouterRpcLocal.ts +7 -6
- package/src/dht/routing/RouterRpcRemote.ts +9 -10
- package/src/dht/routing/RoutingSession.ts +59 -68
- package/src/dht/store/LocalDataStore.ts +47 -66
- package/src/dht/store/StoreManager.ts +217 -0
- package/src/dht/store/StoreRpcLocal.ts +26 -302
- package/src/dht/store/StoreRpcRemote.ts +9 -11
- package/src/exports.ts +2 -0
- package/src/helpers/MapWithTtl.ts +71 -0
- package/src/helpers/PeerID.ts +5 -0
- package/src/helpers/nodeId.ts +20 -0
- package/src/helpers/peerIdFromPeerDescriptor.ts +5 -3
- package/src/helpers/protoClasses.ts +4 -6
- package/src/helpers/versionCompatibility.ts +13 -0
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +31 -32
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +11 -12
- package/src/proto/packages/dht/protos/DhtRpc.ts +67 -90
- package/src/rpc-protocol/DhtCallContext.ts +2 -1
- package/src/rpc-protocol/DhtRpcOptions.ts +2 -1
- package/src/transport/ITransport.ts +10 -1
- package/src/transport/ListeningRpcCommunicator.ts +1 -1
- package/src/transport/RoutingRpcCommunicator.ts +18 -12
- package/test/benchmark/Find.test.ts +8 -21
- package/test/benchmark/KademliaCorrectness.test.ts +11 -20
- package/test/benchmark/SortedContactListBenchmark.test.ts +9 -9
- package/test/benchmark/kademlia-simulation/Contact.ts +9 -8
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +2 -2
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +17 -17
- package/test/end-to-end/Layer0-Layer1.test.ts +10 -14
- package/test/end-to-end/Layer0.test.ts +15 -18
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +7 -9
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +4 -10
- package/test/end-to-end/Layer0Webrtc.test.ts +5 -6
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +8 -16
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +6 -13
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +5 -6
- package/test/end-to-end/memory-leak.test.ts +6 -8
- package/test/integration/ConnectionLocking.test.ts +6 -13
- package/test/integration/ConnectionManager.test.ts +26 -43
- package/test/integration/DhtJoinPeerDiscovery.test.ts +5 -10
- package/test/integration/DhtNodeExternalAPI.test.ts +16 -22
- package/test/integration/DhtNodeRpcRemote.test.ts +13 -22
- package/test/integration/DhtRpc.test.ts +13 -18
- package/test/integration/Find.test.ts +10 -10
- package/test/integration/Layer1-scale.test.ts +17 -28
- package/test/integration/Mock-Layer1-Layer0.test.ts +25 -43
- package/test/integration/MultipleEntryPointJoining.test.ts +7 -7
- package/test/integration/ReplicateData.test.ts +28 -30
- package/test/integration/RouteMessage.test.ts +30 -27
- package/test/integration/RouterRpcRemote.test.ts +12 -18
- package/test/integration/RpcErrors.test.ts +8 -21
- package/test/integration/ScaleDownDht.test.ts +11 -10
- package/test/integration/SimultaneousConnections.test.ts +43 -67
- package/test/integration/Store.test.ts +23 -36
- package/test/integration/StoreAndDelete.test.ts +30 -50
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +16 -37
- package/test/integration/StoreRpcRemote.test.ts +20 -30
- package/test/integration/WebrtcConnectionManagement.test.ts +3 -13
- package/test/integration/WebrtcConnectorRpc.test.ts +3 -9
- package/test/integration/WebsocketConnectionManagement.test.ts +7 -10
- package/test/integration/WebsocketConnectorRpc.test.ts +8 -17
- package/test/unit/ConnectivityHelpers.test.ts +9 -17
- package/test/unit/DuplicateDetector.test.ts +7 -4
- package/test/unit/LocalDataStore.test.ts +66 -77
- package/test/unit/PeerManager.test.ts +31 -0
- package/test/unit/RandomContactList.test.ts +13 -8
- package/test/unit/{Finder.test.ts → RecursiveOperationManager.test.ts} +38 -43
- package/test/unit/RecursiveOperationSession.test.ts +68 -0
- package/test/unit/Router.test.ts +15 -22
- package/test/unit/RoutingSession.test.ts +23 -25
- package/test/unit/SortedContactList.test.ts +41 -26
- package/test/unit/StoreManager.test.ts +132 -0
- package/test/unit/WebsocketConnector.test.ts +27 -35
- package/test/unit/customMatchers.test.ts +16 -0
- package/test/unit/versionCompatibility.test.ts +16 -0
- package/test/utils/FakeTransport.ts +44 -0
- package/test/utils/customMatchers.ts +70 -0
- package/test/utils/mock/MockRpcCommunicator.ts +7 -0
- package/test/utils/mock/Router.ts +3 -2
- package/test/utils/mock/mockDataEntry.ts +36 -0
- package/test/utils/utils.ts +49 -87
- package/tsconfig.browser.json +2 -1
- package/tsconfig.jest.json +2 -1
- package/tsconfig.node.json +2 -1
- package/dist/src/dht/find/FindRpcLocal.d.ts +0 -14
- package/dist/src/dht/find/FindRpcLocal.js.map +0 -1
- package/dist/src/dht/find/FindSession.d.ts +0 -44
- package/dist/src/dht/find/FindSession.js +0 -150
- package/dist/src/dht/find/FindSession.js.map +0 -1
- package/dist/src/dht/find/FindSessionRpcLocal.d.ts +0 -12
- package/dist/src/dht/find/FindSessionRpcLocal.js +0 -17
- package/dist/src/dht/find/FindSessionRpcLocal.js.map +0 -1
- package/dist/src/dht/find/FindSessionRpcRemote.d.ts +0 -6
- package/dist/src/dht/find/FindSessionRpcRemote.js +0 -21
- package/dist/src/dht/find/FindSessionRpcRemote.js.map +0 -1
- package/dist/src/dht/find/Finder.d.ts +0 -49
- package/dist/src/dht/find/Finder.js +0 -197
- package/dist/src/dht/find/Finder.js.map +0 -1
- package/dist/src/dht/routing/FindRpcRemote.d.ts +0 -6
- package/dist/src/dht/routing/FindRpcRemote.js.map +0 -1
- package/src/dht/find/FindSession.ts +0 -183
- package/src/dht/find/FindSessionRpcLocal.ts +0 -25
- package/src/dht/find/Finder.ts +0 -295
|
@@ -1,340 +1,64 @@
|
|
|
1
|
+
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
2
|
+
import { Logger } from '@streamr/utils'
|
|
3
|
+
import { Empty } from '../../proto/google/protobuf/empty'
|
|
4
|
+
import { Timestamp } from '../../proto/google/protobuf/timestamp'
|
|
1
5
|
import {
|
|
2
|
-
DataEntry,
|
|
6
|
+
DataEntry,
|
|
7
|
+
PeerDescriptor,
|
|
8
|
+
ReplicateDataRequest,
|
|
3
9
|
StoreDataRequest, StoreDataResponse
|
|
4
10
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
5
|
-
import {
|
|
6
|
-
import { Any } from '../../proto/google/protobuf/any'
|
|
7
|
-
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
11
|
+
import { IStoreRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
8
12
|
import { DhtCallContext } from '../../rpc-protocol/DhtCallContext'
|
|
9
|
-
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
10
|
-
import { StoreRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
11
|
-
import { RoutingRpcCommunicator } from '../../transport/RoutingRpcCommunicator'
|
|
12
|
-
import { IFinder } from '../find/Finder'
|
|
13
|
-
import { areEqualPeerDescriptors } from '../../helpers/peerIdFromPeerDescriptor'
|
|
14
|
-
import { Logger } from '@streamr/utils'
|
|
15
13
|
import { LocalDataStore } from './LocalDataStore'
|
|
16
|
-
import { IStoreRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
17
|
-
import { StoreRpcRemote } from './StoreRpcRemote'
|
|
18
|
-
import { Timestamp } from '../../proto/google/protobuf/timestamp'
|
|
19
|
-
import EventEmitter from 'eventemitter3'
|
|
20
|
-
import { Events } from '../DhtNode'
|
|
21
|
-
import { SortedContactList } from '../contact/SortedContactList'
|
|
22
|
-
import { Contact } from '../contact/Contact'
|
|
23
|
-
import { DhtNodeRpcRemote } from '../DhtNodeRpcRemote'
|
|
24
|
-
import { ServiceID } from '../../types/ServiceID'
|
|
25
|
-
import { Empty } from '../../proto/google/protobuf/empty'
|
|
26
14
|
|
|
27
|
-
interface
|
|
28
|
-
rpcCommunicator: RoutingRpcCommunicator
|
|
29
|
-
finder: IFinder
|
|
30
|
-
localPeerDescriptor: PeerDescriptor
|
|
15
|
+
interface StoreRpcLocalConfig {
|
|
31
16
|
localDataStore: LocalDataStore
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
highestTtl: number
|
|
35
|
-
redundancyFactor: number
|
|
36
|
-
dhtNodeEmitter: EventEmitter<Events>
|
|
37
|
-
getNodesClosestToIdFromBucket: (id: Uint8Array, n?: number) => DhtNodeRpcRemote[]
|
|
38
|
-
rpcRequestTimeout?: number
|
|
17
|
+
replicateDataToNeighbors: (incomingPeer: PeerDescriptor, dataEntry: DataEntry) => void
|
|
18
|
+
selfIsWithinRedundancyFactor: (key: Uint8Array) => boolean
|
|
39
19
|
}
|
|
40
20
|
|
|
41
21
|
const logger = new Logger(module)
|
|
42
22
|
|
|
43
23
|
export class StoreRpcLocal implements IStoreRpc {
|
|
44
24
|
|
|
45
|
-
private readonly
|
|
46
|
-
private readonly finder: IFinder
|
|
47
|
-
private readonly localPeerDescriptor: PeerDescriptor
|
|
48
|
-
private readonly localDataStore: LocalDataStore
|
|
49
|
-
private readonly serviceId: ServiceID
|
|
50
|
-
private readonly maxTtl: number
|
|
51
|
-
private readonly highestTtl: number
|
|
52
|
-
private readonly redundancyFactor: number
|
|
53
|
-
private readonly dhtNodeEmitter: EventEmitter<Events>
|
|
54
|
-
private readonly getNodesClosestToIdFromBucket: (id: Uint8Array, n?: number) => DhtNodeRpcRemote[]
|
|
55
|
-
private readonly rpcRequestTimeout?: number
|
|
56
|
-
|
|
57
|
-
constructor(config: DataStoreConfig) {
|
|
58
|
-
this.rpcCommunicator = config.rpcCommunicator
|
|
59
|
-
this.finder = config.finder
|
|
60
|
-
this.localPeerDescriptor = config.localPeerDescriptor
|
|
61
|
-
this.localDataStore = config.localDataStore
|
|
62
|
-
this.serviceId = config.serviceId
|
|
63
|
-
this.maxTtl = config.maxTtl
|
|
64
|
-
this.highestTtl = config.highestTtl
|
|
65
|
-
this.redundancyFactor = config.redundancyFactor
|
|
66
|
-
this.dhtNodeEmitter = config.dhtNodeEmitter
|
|
67
|
-
this.rpcRequestTimeout = config.rpcRequestTimeout
|
|
68
|
-
this.getNodesClosestToIdFromBucket = config.getNodesClosestToIdFromBucket
|
|
69
|
-
this.rpcCommunicator.registerRpcMethod(StoreDataRequest, StoreDataResponse, 'storeData',
|
|
70
|
-
(request: StoreDataRequest) => this.storeData(request))
|
|
71
|
-
this.rpcCommunicator.registerRpcNotification(ReplicateDataRequest, 'replicateData',
|
|
72
|
-
(request: ReplicateDataRequest, context: ServerCallContext) => this.replicateData(request, context))
|
|
73
|
-
|
|
74
|
-
this.dhtNodeEmitter.on('newContact', (peerDescriptor: PeerDescriptor) => {
|
|
75
|
-
this.localDataStore.getStore().forEach((dataMap, _dataKey) => {
|
|
76
|
-
dataMap.forEach(async (dataEntry) => {
|
|
77
|
-
if (this.shouldReplicateDataToNewNode(dataEntry.dataEntry, peerDescriptor)) {
|
|
78
|
-
try {
|
|
79
|
-
await this.replicateDataToContact(dataEntry.dataEntry, peerDescriptor)
|
|
80
|
-
} catch (e) {
|
|
81
|
-
logger.trace('replicateDataToContact() failed', { error: e })
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
})
|
|
85
|
-
})
|
|
86
|
-
})
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private shouldReplicateDataToNewNode(dataEntry: DataEntry, newNode: PeerDescriptor): boolean {
|
|
25
|
+
private readonly config: StoreRpcLocalConfig
|
|
90
26
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const localPeerId = PeerID.fromValue(this.localPeerDescriptor.nodeId)
|
|
94
|
-
|
|
95
|
-
const closestToData = this.getNodesClosestToIdFromBucket(dataEntry.key, 10)
|
|
96
|
-
const sortedList = new SortedContactList<Contact>({
|
|
97
|
-
referenceId: PeerID.fromValue(dataEntry.key),
|
|
98
|
-
maxSize: 20, // TODO use config option or named constant?
|
|
99
|
-
allowToContainReferenceId: true,
|
|
100
|
-
emitEvents: false
|
|
101
|
-
})
|
|
102
|
-
sortedList.addContact(new Contact(this.localPeerDescriptor))
|
|
103
|
-
|
|
104
|
-
closestToData.forEach((con) => {
|
|
105
|
-
if (!newNodeId.equals(PeerID.fromValue(con.getPeerDescriptor().nodeId))) {
|
|
106
|
-
sortedList.addContact(new Contact(con.getPeerDescriptor()))
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
if (!sortedList.getAllContacts()[0].getPeerId().equals(localPeerId)) {
|
|
111
|
-
// If we are not the closes node to the data, do not replicate
|
|
112
|
-
return false
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
this.localDataStore.setStale(dataId, dataEntry.creator!, false)
|
|
116
|
-
const newPeerId = PeerID.fromValue(newNode.nodeId)
|
|
117
|
-
sortedList.addContact(new Contact(newNode))
|
|
118
|
-
|
|
119
|
-
const sorted = sortedList.getAllContacts()
|
|
120
|
-
|
|
121
|
-
let index = 0
|
|
122
|
-
|
|
123
|
-
for (index = 0; index < sorted.length; index++) {
|
|
124
|
-
if (sorted[index].getPeerId().equals(newPeerId)) {
|
|
125
|
-
break
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// if new node is within the storageRedundancyFactor closest nodes to the data
|
|
130
|
-
// do replicate data to it
|
|
131
|
-
|
|
132
|
-
if (index < this.redundancyFactor) {
|
|
133
|
-
return true
|
|
134
|
-
} else {
|
|
135
|
-
return false
|
|
136
|
-
}
|
|
27
|
+
constructor(config: StoreRpcLocalConfig) {
|
|
28
|
+
this.config = config
|
|
137
29
|
}
|
|
138
30
|
|
|
139
|
-
private async replicateDataToContact(dataEntry: DataEntry, contact: PeerDescriptor, doNotConnect: boolean = false): Promise<void> {
|
|
140
|
-
const rpcRemote = new StoreRpcRemote(
|
|
141
|
-
this.localPeerDescriptor,
|
|
142
|
-
contact,
|
|
143
|
-
this.serviceId,
|
|
144
|
-
toProtoRpcClient(new StoreRpcClient(this.rpcCommunicator.getRpcClientTransport())),
|
|
145
|
-
this.rpcRequestTimeout
|
|
146
|
-
)
|
|
147
|
-
try {
|
|
148
|
-
await rpcRemote.replicateData({ entry: dataEntry }, doNotConnect)
|
|
149
|
-
} catch (e) {
|
|
150
|
-
logger.trace('replicateData() threw an exception ' + e)
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
public async storeDataToDht(key: Uint8Array, data: Any, creator: PeerDescriptor): Promise<PeerDescriptor[]> {
|
|
155
|
-
logger.debug(`Storing data to DHT ${this.serviceId}`)
|
|
156
|
-
const result = await this.finder.startFind(key)
|
|
157
|
-
const closestNodes = result.closestNodes
|
|
158
|
-
const successfulNodes: PeerDescriptor[] = []
|
|
159
|
-
const ttl = this.highestTtl // ToDo: make TTL decrease according to some nice curve
|
|
160
|
-
const createdAt = Timestamp.now()
|
|
161
|
-
for (let i = 0; i < closestNodes.length && successfulNodes.length < this.redundancyFactor; i++) {
|
|
162
|
-
if (areEqualPeerDescriptors(this.localPeerDescriptor, closestNodes[i])) {
|
|
163
|
-
this.localDataStore.storeEntry({
|
|
164
|
-
key,
|
|
165
|
-
data,
|
|
166
|
-
creator,
|
|
167
|
-
createdAt,
|
|
168
|
-
storedAt: Timestamp.now(),
|
|
169
|
-
ttl,
|
|
170
|
-
stale: false,
|
|
171
|
-
deleted: false,
|
|
172
|
-
})
|
|
173
|
-
successfulNodes.push(closestNodes[i])
|
|
174
|
-
continue
|
|
175
|
-
}
|
|
176
|
-
const rpcRemote = new StoreRpcRemote(
|
|
177
|
-
this.localPeerDescriptor,
|
|
178
|
-
closestNodes[i],
|
|
179
|
-
this.serviceId,
|
|
180
|
-
toProtoRpcClient(new StoreRpcClient(this.rpcCommunicator.getRpcClientTransport())),
|
|
181
|
-
this.rpcRequestTimeout
|
|
182
|
-
)
|
|
183
|
-
try {
|
|
184
|
-
const response = await rpcRemote.storeData({
|
|
185
|
-
key,
|
|
186
|
-
data,
|
|
187
|
-
creator,
|
|
188
|
-
createdAt,
|
|
189
|
-
ttl
|
|
190
|
-
})
|
|
191
|
-
if (!response.error) {
|
|
192
|
-
successfulNodes.push(closestNodes[i])
|
|
193
|
-
logger.trace('remote.storeData() returned success')
|
|
194
|
-
} else {
|
|
195
|
-
logger.trace('remote.storeData() returned error: ' + response.error)
|
|
196
|
-
}
|
|
197
|
-
} catch (e) {
|
|
198
|
-
logger.trace('remote.storeData() threw an exception ' + e)
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
return successfulNodes
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
private selfIsOneOfClosestPeers(dataId: Uint8Array): boolean {
|
|
205
|
-
const localPeerId = PeerID.fromValue(this.localPeerDescriptor.nodeId)
|
|
206
|
-
const closestPeers = this.getNodesClosestToIdFromBucket(dataId, this.redundancyFactor)
|
|
207
|
-
const sortedList = new SortedContactList<Contact>({
|
|
208
|
-
referenceId: localPeerId,
|
|
209
|
-
maxSize: this.redundancyFactor,
|
|
210
|
-
allowToContainReferenceId: true,
|
|
211
|
-
emitEvents: false
|
|
212
|
-
})
|
|
213
|
-
sortedList.addContact(new Contact(this.localPeerDescriptor))
|
|
214
|
-
closestPeers.forEach((con) => sortedList.addContact(new Contact(con.getPeerDescriptor())))
|
|
215
|
-
return sortedList.getClosestContacts().some((node) => node.getPeerId().equals(localPeerId))
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// RPC service implementation
|
|
219
31
|
async storeData(request: StoreDataRequest): Promise<StoreDataResponse> {
|
|
220
|
-
|
|
221
|
-
const { key, data, createdAt,
|
|
222
|
-
this.
|
|
32
|
+
logger.trace('storeData()')
|
|
33
|
+
const { key, data, creator, createdAt, ttl } = request
|
|
34
|
+
const selfIsOneOfClosestPeers = this.config.selfIsWithinRedundancyFactor(key)
|
|
35
|
+
this.config.localDataStore.storeEntry({
|
|
223
36
|
key,
|
|
224
37
|
data,
|
|
225
38
|
creator,
|
|
226
39
|
createdAt,
|
|
227
40
|
storedAt: Timestamp.now(),
|
|
228
41
|
ttl,
|
|
229
|
-
stale: !
|
|
42
|
+
stale: !selfIsOneOfClosestPeers,
|
|
230
43
|
deleted: false
|
|
231
44
|
})
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
this.localDataStore.setAllEntriesAsStale(PeerID.fromValue(key))
|
|
45
|
+
if (!selfIsOneOfClosestPeers) {
|
|
46
|
+
this.config.localDataStore.setAllEntriesAsStale(key)
|
|
235
47
|
}
|
|
236
|
-
|
|
237
|
-
logger.trace('storeData()')
|
|
238
|
-
return StoreDataResponse.create()
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
async destroy(): Promise<void> {
|
|
242
|
-
await this.replicateDataToClosestNodes()
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
private async replicateDataToClosestNodes(): Promise<void> {
|
|
246
|
-
const dataEntries = Array.from(this.localDataStore.getStore().values())
|
|
247
|
-
.flatMap((dataMap) => Array.from(dataMap.values()))
|
|
248
|
-
.map((localData) => localData.dataEntry)
|
|
249
|
-
|
|
250
|
-
await Promise.all(dataEntries.map(async (dataEntry) => {
|
|
251
|
-
const dhtNodeRemotes = this.getNodesClosestToIdFromBucket(dataEntry.key, this.redundancyFactor)
|
|
252
|
-
await Promise.all(dhtNodeRemotes.map(async (remoteDhtNode) => {
|
|
253
|
-
const rpcRemote = new StoreRpcRemote(
|
|
254
|
-
this.localPeerDescriptor,
|
|
255
|
-
remoteDhtNode.getPeerDescriptor(),
|
|
256
|
-
this.serviceId,
|
|
257
|
-
toProtoRpcClient(new StoreRpcClient(this.rpcCommunicator.getRpcClientTransport())),
|
|
258
|
-
this.rpcRequestTimeout
|
|
259
|
-
)
|
|
260
|
-
try {
|
|
261
|
-
await rpcRemote.replicateData({ entry: dataEntry })
|
|
262
|
-
} catch (err) {
|
|
263
|
-
logger.trace('Failed to replicate data in replicateDataToClosestNodes', { error: err })
|
|
264
|
-
}
|
|
265
|
-
}))
|
|
266
|
-
}))
|
|
48
|
+
return {}
|
|
267
49
|
}
|
|
268
50
|
|
|
269
|
-
// RPC service implementation
|
|
270
51
|
public async replicateData(request: ReplicateDataRequest, context: ServerCallContext): Promise<Empty> {
|
|
271
52
|
logger.trace('server-side replicateData()')
|
|
272
53
|
const dataEntry = request.entry!
|
|
273
|
-
|
|
274
|
-
const wasStored = this.localDataStore.storeEntry(dataEntry)
|
|
275
|
-
|
|
54
|
+
const wasStored = this.config.localDataStore.storeEntry(dataEntry)
|
|
276
55
|
if (wasStored) {
|
|
277
|
-
this.
|
|
56
|
+
this.config.replicateDataToNeighbors((context as DhtCallContext).incomingSourceDescriptor!, request.entry!)
|
|
278
57
|
}
|
|
279
|
-
if (!this.
|
|
280
|
-
this.localDataStore.setAllEntriesAsStale(
|
|
58
|
+
if (!this.config.selfIsWithinRedundancyFactor(dataEntry.key)) {
|
|
59
|
+
this.config.localDataStore.setAllEntriesAsStale(dataEntry.key)
|
|
281
60
|
}
|
|
282
61
|
logger.trace('server-side replicateData() at end')
|
|
283
62
|
return {}
|
|
284
63
|
}
|
|
285
|
-
|
|
286
|
-
private replicateDataToNeighborsIfNeeded(incomingPeer: PeerDescriptor, dataEntry: DataEntry): void {
|
|
287
|
-
|
|
288
|
-
// sort own contact list according to data id
|
|
289
|
-
const localPeerId = PeerID.fromValue(this.localPeerDescriptor.nodeId)
|
|
290
|
-
const dataId = PeerID.fromValue(dataEntry.key)
|
|
291
|
-
const incomingPeerId = PeerID.fromValue(incomingPeer.nodeId)
|
|
292
|
-
const closestToData = this.getNodesClosestToIdFromBucket(dataEntry.key, 10)
|
|
293
|
-
const sortedList = new SortedContactList<Contact>({
|
|
294
|
-
referenceId: dataId,
|
|
295
|
-
maxSize: this.redundancyFactor,
|
|
296
|
-
allowToContainReferenceId: true,
|
|
297
|
-
emitEvents: false
|
|
298
|
-
})
|
|
299
|
-
sortedList.addContact(new Contact(this.localPeerDescriptor))
|
|
300
|
-
|
|
301
|
-
closestToData.forEach((con) => {
|
|
302
|
-
sortedList.addContact(new Contact(con.getPeerDescriptor()))
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
if (!sortedList.getAllContacts()[0].getPeerId().equals(localPeerId)) {
|
|
306
|
-
// If we are not the closest node to the data, replicate only to the
|
|
307
|
-
// closest one to the data
|
|
308
|
-
|
|
309
|
-
const contact = sortedList.getAllContacts()[0]
|
|
310
|
-
const contactPeerId = PeerID.fromValue(contact.getPeerDescriptor().nodeId)
|
|
311
|
-
if (!incomingPeerId.equals(contactPeerId) && !localPeerId.equals(contactPeerId)) {
|
|
312
|
-
setImmediate(async () => {
|
|
313
|
-
try {
|
|
314
|
-
await this.replicateDataToContact(dataEntry, contact.getPeerDescriptor())
|
|
315
|
-
logger.trace('replicateDataToContact() returned when migrating to only the closest contact')
|
|
316
|
-
} catch (e) {
|
|
317
|
-
logger.error('replicating data to only the closest contact failed ' + e)
|
|
318
|
-
}
|
|
319
|
-
})
|
|
320
|
-
}
|
|
321
|
-
} else {
|
|
322
|
-
// if we are the closest to the data, replicate to all storageRedundancyFactor nearest
|
|
323
|
-
sortedList.getAllContacts().forEach((contact) => {
|
|
324
|
-
const contactPeerId = PeerID.fromValue(contact.getPeerDescriptor().nodeId)
|
|
325
|
-
if (!incomingPeerId.equals(contactPeerId) && !localPeerId.equals(contactPeerId)) {
|
|
326
|
-
if (!incomingPeerId.equals(contactPeerId) && !localPeerId.equals(contactPeerId)) {
|
|
327
|
-
setImmediate(async () => {
|
|
328
|
-
try {
|
|
329
|
-
await this.replicateDataToContact(dataEntry, contact.getPeerDescriptor())
|
|
330
|
-
logger.trace('replicateDataToContact() returned')
|
|
331
|
-
} catch (e) {
|
|
332
|
-
logger.error('replicating data to one of the closest contacts failed ' + e)
|
|
333
|
-
}
|
|
334
|
-
})
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
})
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
64
|
}
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IStoreRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
1
|
+
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
3
2
|
import {
|
|
4
3
|
ReplicateDataRequest,
|
|
5
|
-
StoreDataRequest
|
|
6
|
-
StoreDataResponse
|
|
4
|
+
StoreDataRequest
|
|
7
5
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
8
|
-
import {
|
|
6
|
+
import { StoreRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
7
|
+
import { EXISTING_CONNECTION_TIMEOUT, RpcRemote } from '../contact/RpcRemote'
|
|
9
8
|
|
|
10
|
-
export class StoreRpcRemote extends RpcRemote<
|
|
9
|
+
export class StoreRpcRemote extends RpcRemote<StoreRpcClient> {
|
|
11
10
|
|
|
12
|
-
async storeData(request: StoreDataRequest): Promise<
|
|
11
|
+
async storeData(request: StoreDataRequest): Promise<void> {
|
|
13
12
|
const options = this.formDhtRpcOptions()
|
|
14
13
|
try {
|
|
15
|
-
|
|
14
|
+
await this.getClient().storeData(request, options)
|
|
16
15
|
} catch (err) {
|
|
17
16
|
const to = getNodeIdFromPeerDescriptor(this.getPeerDescriptor())
|
|
18
17
|
const from = getNodeIdFromPeerDescriptor(this.getLocalPeerDescriptor())
|
|
@@ -20,10 +19,9 @@ export class StoreRpcRemote extends RpcRemote<IStoreRpcClient> {
|
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
async replicateData(request: ReplicateDataRequest
|
|
22
|
+
async replicateData(request: ReplicateDataRequest): Promise<void> {
|
|
24
23
|
const options = this.formDhtRpcOptions({
|
|
25
|
-
timeout: EXISTING_CONNECTION_TIMEOUT
|
|
26
|
-
doNotConnect
|
|
24
|
+
timeout: EXISTING_CONNECTION_TIMEOUT
|
|
27
25
|
})
|
|
28
26
|
return this.getClient().replicateData(request, options)
|
|
29
27
|
}
|
package/src/exports.ts
CHANGED
|
@@ -7,6 +7,7 @@ export { getRandomRegion, getRegionDelayMatrix } from './connection/simulator/pi
|
|
|
7
7
|
export { PeerDescriptor, Message, NodeType, DataEntry } from './proto/packages/dht/protos/DhtRpc'
|
|
8
8
|
export { ITransport } from './transport/ITransport'
|
|
9
9
|
export { ConnectionManager, ConnectionLocker, PortRange, TlsCertificate } from './connection/ConnectionManager'
|
|
10
|
+
export { LockID } from './connection/ConnectionLockHandler'
|
|
10
11
|
export { DefaultConnectorFacade } from './connection/ConnectorFacade'
|
|
11
12
|
export { DhtRpcOptions } from './rpc-protocol/DhtRpcOptions'
|
|
12
13
|
export { RpcRemote, EXISTING_CONNECTION_TIMEOUT } from './dht/contact/RpcRemote'
|
|
@@ -17,3 +18,4 @@ export { ClientWebsocket } from './connection/websocket/ClientWebsocket'
|
|
|
17
18
|
export { ManagedConnection } from './connection/ManagedConnection'
|
|
18
19
|
export { ConnectionType } from './connection/IConnection'
|
|
19
20
|
export { PeerID } from './helpers/PeerID'
|
|
21
|
+
export { ServiceID } from './types/ServiceID'
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
interface ValueWrapper<V> {
|
|
2
|
+
value: V
|
|
3
|
+
timeout: NodeJS.Timeout
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export class MapWithTtl<K, V> {
|
|
7
|
+
|
|
8
|
+
private readonly delegate: Map<K, ValueWrapper<V>> = new Map()
|
|
9
|
+
private readonly getTtl: (value: V) => number
|
|
10
|
+
|
|
11
|
+
constructor(getTtl: (value: V) => number) {
|
|
12
|
+
this.getTtl = getTtl
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
set(key: K, value: V): void {
|
|
16
|
+
const existing = this.delegate.get(key)
|
|
17
|
+
if (existing !== undefined) {
|
|
18
|
+
clearTimeout(existing.timeout)
|
|
19
|
+
}
|
|
20
|
+
this.delegate.set(key, {
|
|
21
|
+
value,
|
|
22
|
+
timeout: this.createTimeout(key, value)
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get(key: K): V | undefined {
|
|
27
|
+
const wrapper = this.delegate.get(key)
|
|
28
|
+
return wrapper?.value
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
has(key: K): boolean {
|
|
32
|
+
return this.delegate.has(key)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
delete(key: K): void {
|
|
36
|
+
const existing = this.delegate.get(key)
|
|
37
|
+
if (existing !== undefined) {
|
|
38
|
+
clearTimeout(existing.timeout)
|
|
39
|
+
this.delegate.delete(key)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
clear(): void {
|
|
44
|
+
this.delegate.forEach((value) => {
|
|
45
|
+
clearTimeout(value.timeout)
|
|
46
|
+
})
|
|
47
|
+
this.delegate.clear()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
size(): number {
|
|
51
|
+
return this.delegate.size
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
*values(): IterableIterator<V> {
|
|
55
|
+
for (const v of this.delegate.values()) {
|
|
56
|
+
yield v.value
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
forEach(cb: (value: V, key: K) => void): void {
|
|
61
|
+
this.delegate.forEach((valueWrapper: ValueWrapper<V>, key: K) => {
|
|
62
|
+
cb(valueWrapper.value, key)
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private createTimeout(key: K, value: V): NodeJS.Timeout {
|
|
67
|
+
return setTimeout(() => {
|
|
68
|
+
this.delete(key)
|
|
69
|
+
}, this.getTtl(value))
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/helpers/PeerID.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { BrandedString, binaryToHex } from '@streamr/utils'
|
|
|
2
2
|
import { UUID } from './UUID'
|
|
3
3
|
import { IllegalArguments } from './errors'
|
|
4
4
|
import crypto from 'crypto'
|
|
5
|
+
import { NodeID, getNodeIdFromBinary } from './nodeId'
|
|
5
6
|
|
|
6
7
|
export type PeerIDKey = BrandedString<'PeerIDKey'>
|
|
7
8
|
|
|
@@ -75,6 +76,10 @@ export class PeerID {
|
|
|
75
76
|
return this.key
|
|
76
77
|
}
|
|
77
78
|
|
|
79
|
+
toNodeId(): NodeID {
|
|
80
|
+
return getNodeIdFromBinary(this.data)
|
|
81
|
+
}
|
|
82
|
+
|
|
78
83
|
get value(): Uint8Array {
|
|
79
84
|
return this.data
|
|
80
85
|
}
|
package/src/helpers/nodeId.ts
CHANGED
|
@@ -1,8 +1,28 @@
|
|
|
1
|
+
import { BrandedString, binaryToHex } from '@streamr/utils'
|
|
1
2
|
import crypto from 'crypto'
|
|
2
3
|
|
|
3
4
|
// https://www.scs.stanford.edu/~dm/home/papers/kpos.pdf
|
|
4
5
|
const KADEMLIA_ID_LENGTH_IN_BYTES = 20
|
|
5
6
|
|
|
7
|
+
// TODO this should return NodeID
|
|
6
8
|
export const createRandomNodeId = (): Uint8Array => {
|
|
7
9
|
return crypto.randomBytes(KADEMLIA_ID_LENGTH_IN_BYTES)
|
|
8
10
|
}
|
|
11
|
+
|
|
12
|
+
// TODO rename the file to be "NodeID.ts" instead of "nodeId.ts"
|
|
13
|
+
export type NodeID = BrandedString<'NodeID'>
|
|
14
|
+
|
|
15
|
+
// TODO remove this or add support for UInt8Array parameters
|
|
16
|
+
export const areEqualNodeIds = (nodeId1: NodeID, nodeId2: NodeID): boolean => {
|
|
17
|
+
return nodeId1 === nodeId2
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// TODO maybe this is not needed and we can use just getNodeIdFromBinary?
|
|
21
|
+
export const getNodeIdFromDataKey = (key: Uint8Array): NodeID => {
|
|
22
|
+
return getNodeIdFromBinary(key)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// TODO should we have similar method to convert nodeId to bucketId (which is just hexToBinary)
|
|
26
|
+
export const getNodeIdFromBinary = (id: Uint8Array): NodeID => {
|
|
27
|
+
return binaryToHex(id) as unknown as NodeID
|
|
28
|
+
}
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { areEqualBinaries, binaryToHex } from '@streamr/utils'
|
|
2
2
|
import { PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
|
|
3
3
|
import { PeerID, PeerIDKey, createPeerIDKey } from './PeerID'
|
|
4
|
+
import { NodeID } from './nodeId'
|
|
4
5
|
|
|
5
6
|
export const peerIdFromPeerDescriptor = (peerDescriptor: PeerDescriptor): PeerID => {
|
|
6
7
|
return PeerID.fromValue(peerDescriptor.nodeId)
|
|
7
8
|
}
|
|
8
9
|
|
|
9
|
-
// TODO could
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
// TODO could use this in trackerless-network (instead of copy-pasted same implementation)
|
|
11
|
+
// and move this to nodeId.ts
|
|
12
|
+
export const getNodeIdFromPeerDescriptor = (peerDescriptor: PeerDescriptor): NodeID => {
|
|
13
|
+
return binaryToHex(peerDescriptor.nodeId) as unknown as NodeID
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
export const keyFromPeerDescriptor = (peerDescriptor: PeerDescriptor): PeerIDKey => {
|
|
@@ -13,12 +13,11 @@ import {
|
|
|
13
13
|
PeerDescriptor,
|
|
14
14
|
PingRequest,
|
|
15
15
|
PingResponse,
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
RecursiveOperationResponse,
|
|
17
|
+
RecursiveOperationRequest,
|
|
18
18
|
RouteMessageAck,
|
|
19
19
|
RouteMessageWrapper,
|
|
20
20
|
WebsocketConnectionRequest,
|
|
21
|
-
WebsocketConnectionResponse,
|
|
22
21
|
WebrtcConnectionRequest,
|
|
23
22
|
RtcOffer,
|
|
24
23
|
RtcAnswer,
|
|
@@ -32,8 +31,8 @@ import {
|
|
|
32
31
|
export const protoClasses: Array<IMessageType<any>> = [
|
|
33
32
|
ClosestPeersRequest,
|
|
34
33
|
ClosestPeersResponse,
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
RecursiveOperationRequest,
|
|
35
|
+
RecursiveOperationResponse,
|
|
37
36
|
PingRequest,
|
|
38
37
|
PingResponse,
|
|
39
38
|
LeaveNotice,
|
|
@@ -48,7 +47,6 @@ export const protoClasses: Array<IMessageType<any>> = [
|
|
|
48
47
|
HandshakeResponse,
|
|
49
48
|
Message,
|
|
50
49
|
WebsocketConnectionRequest,
|
|
51
|
-
WebsocketConnectionResponse,
|
|
52
50
|
WebrtcConnectionRequest,
|
|
53
51
|
RtcOffer,
|
|
54
52
|
RtcAnswer,
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Is able to compare versions such as 1.2.3 and 1.2.4
|
|
2
|
+
// can also compare versions such as 100.0.0-pretestnet.0 and 100.0.0-pretestnet.40
|
|
3
|
+
export const isCompatibleVersion = (version1: string, version2: string): boolean => {
|
|
4
|
+
const minorVersion1 = excludePatchVersion(version1)
|
|
5
|
+
const minorVersion2 = excludePatchVersion(version2)
|
|
6
|
+
return minorVersion1 === minorVersion2
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const excludePatchVersion = (version: string): string => {
|
|
10
|
+
const versionParts = version.split('.')
|
|
11
|
+
versionParts.pop()
|
|
12
|
+
return versionParts.join('.')
|
|
13
|
+
}
|