@streamr/dht 100.0.0-pretestnet.6 → 100.0.0-rc.0
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/README.md +90 -8
- package/dist/package.json +70 -0
- package/dist/src/connection/Connection.d.ts +1 -0
- package/dist/src/connection/Connection.js +10 -3
- package/dist/src/connection/Connection.js.map +1 -1
- package/dist/src/connection/ConnectionLockHandler.d.ts +14 -14
- package/dist/src/connection/ConnectionLockHandler.js +22 -16
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionLockRpcLocal.js +7 -6
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +4 -6
- package/dist/src/connection/ConnectionLockRpcRemote.js +10 -37
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +19 -20
- package/dist/src/connection/ConnectionManager.js +132 -117
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +3 -2
- package/dist/src/connection/ConnectorFacade.js +10 -3
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +3 -1
- package/dist/src/connection/Handshaker.js +18 -9
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/IConnection.d.ts +2 -7
- package/dist/src/connection/IConnection.js +1 -8
- package/dist/src/connection/IConnection.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +5 -7
- package/dist/src/connection/ManagedConnection.js +42 -26
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/ManagedWebrtcConnection.js.map +1 -1
- package/dist/src/connection/connectivityChecker.d.ts +9 -0
- package/dist/src/connection/connectivityChecker.js +130 -0
- package/dist/src/connection/connectivityChecker.js.map +1 -0
- package/dist/src/connection/connectivityRequestHandler.d.ts +3 -0
- package/dist/src/connection/connectivityRequestHandler.js +101 -0
- package/dist/src/connection/connectivityRequestHandler.js.map +1 -0
- package/dist/src/connection/simulator/Simulator.d.ts +0 -3
- package/dist/src/connection/simulator/Simulator.js +34 -69
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +27 -25
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.js +16 -12
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/simulator/pings.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.d.ts +2 -3
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +40 -27
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +0 -2
- package/dist/src/connection/webrtc/WebrtcConnector.js +24 -25
- 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 +27 -20
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +3 -6
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +2 -5
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +1 -2
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +6 -4
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/ClientWebsocket.d.ts +1 -0
- package/dist/src/connection/websocket/ClientWebsocket.js +14 -7
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +3 -9
- package/dist/src/connection/websocket/WebsocketConnector.js +142 -74
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +5 -3
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +9 -12
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +4 -7
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +7 -44
- 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 +49 -40
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/connection/websocket/{ServerWebsocket.d.ts → WebsocketServerConnection.d.ts} +8 -4
- package/dist/src/connection/websocket/WebsocketServerConnection.js +97 -0
- package/dist/src/connection/websocket/WebsocketServerConnection.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +45 -49
- package/dist/src/dht/DhtNode.js +229 -317
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +10 -9
- package/dist/src/dht/DhtNodeRpcLocal.js +19 -11
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +15 -10
- package/dist/src/dht/DhtNodeRpcRemote.js +38 -21
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +7 -6
- package/dist/src/dht/ExternalApiRpcLocal.js +9 -13
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +6 -5
- package/dist/src/dht/ExternalApiRpcRemote.js +10 -7
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +61 -0
- package/dist/src/dht/PeerManager.js +288 -0
- package/dist/src/dht/PeerManager.js.map +1 -0
- package/dist/src/dht/contact/Contact.d.ts +2 -2
- package/dist/src/dht/contact/Contact.js +4 -3
- package/dist/src/dht/contact/Contact.js.map +1 -1
- package/dist/src/dht/contact/ContactList.d.ts +8 -8
- package/dist/src/dht/contact/ContactList.js +12 -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 +13 -12
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/RingContactList.d.ts +31 -0
- package/dist/src/dht/contact/RingContactList.js +133 -0
- package/dist/src/dht/contact/RingContactList.js.map +1 -0
- package/dist/src/dht/contact/{Remote.d.ts → RpcRemote.d.ts} +8 -7
- package/dist/src/dht/contact/{Remote.js → RpcRemote.js} +16 -13
- package/dist/src/dht/contact/RpcRemote.js.map +1 -0
- package/dist/src/dht/contact/SortedContactList.d.ts +29 -15
- package/dist/src/dht/contact/SortedContactList.js +79 -42
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/contact/ringIdentifiers.d.ts +16 -0
- package/dist/src/dht/contact/ringIdentifiers.js +54 -0
- package/dist/src/dht/contact/ringIdentifiers.js.map +1 -0
- package/dist/src/dht/discovery/DiscoverySession.d.ts +8 -18
- package/dist/src/dht/discovery/DiscoverySession.js +32 -48
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +16 -12
- package/dist/src/dht/discovery/PeerDiscovery.js +82 -39
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/discovery/RingDiscoverySession.d.ts +29 -0
- package/dist/src/dht/discovery/RingDiscoverySession.js +123 -0
- package/dist/src/dht/discovery/RingDiscoverySession.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +36 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +166 -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/recursive-operation/RecursiveOperationRpcLocal.js +26 -0
- 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/recursive-operation/RecursiveOperationRpcRemote.js +44 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +44 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +187 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +14 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +20 -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/DuplicateDetector.d.ts +2 -4
- package/dist/src/dht/routing/DuplicateDetector.js +10 -15
- package/dist/src/dht/routing/DuplicateDetector.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +11 -27
- package/dist/src/dht/routing/Router.js +92 -58
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +3 -4
- package/dist/src/dht/routing/RouterRpcLocal.js +17 -16
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcRemote.d.ts +3 -3
- package/dist/src/dht/routing/RouterRpcRemote.js +29 -20
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +29 -21
- package/dist/src/dht/routing/RoutingSession.js +93 -68
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/routing/RoutingTablesCache.d.ts +24 -0
- package/dist/src/dht/routing/RoutingTablesCache.js +46 -0
- package/dist/src/dht/routing/RoutingTablesCache.js.map +1 -0
- package/dist/src/dht/routing/getPreviousPeer.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +9 -14
- package/dist/src/dht/store/LocalDataStore.js +46 -72
- 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 +182 -0
- package/dist/src/dht/store/StoreManager.js.map +1 -0
- package/dist/src/dht/store/StoreRpcLocal.d.ts +11 -41
- package/dist/src/dht/store/StoreRpcLocal.js +27 -234
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.d.ts +6 -7
- package/dist/src/dht/store/StoreRpcRemote.js +10 -20
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/exports.d.ts +5 -3
- package/dist/src/exports.js +10 -8
- 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/Connectivity.js.map +1 -1
- package/dist/src/helpers/MapWithTtl.d.ts +14 -0
- package/dist/src/helpers/MapWithTtl.js +60 -0
- package/dist/src/helpers/MapWithTtl.js.map +1 -0
- package/dist/src/helpers/createPeerDescriptor.d.ts +3 -0
- package/dist/src/helpers/createPeerDescriptor.js +57 -0
- package/dist/src/helpers/createPeerDescriptor.js.map +1 -0
- package/dist/src/helpers/createPeerDescriptorSignaturePayload.d.ts +2 -0
- package/dist/src/helpers/createPeerDescriptorSignaturePayload.js +30 -0
- package/dist/src/helpers/createPeerDescriptorSignaturePayload.js.map +1 -0
- package/dist/src/helpers/debugHelpers.js.map +1 -1
- package/dist/src/helpers/errors.js +2 -0
- package/dist/src/helpers/errors.js.map +1 -1
- package/dist/src/helpers/offering.d.ts +4 -0
- package/dist/src/helpers/offering.js +18 -0
- package/dist/src/helpers/offering.js.map +1 -0
- package/dist/src/helpers/protoClasses.js +2 -3
- package/dist/src/helpers/protoClasses.js.map +1 -1
- package/dist/src/helpers/protoToString.js.map +1 -1
- package/dist/src/helpers/version.d.ts +6 -0
- package/dist/src/helpers/version.js +38 -0
- package/dist/src/helpers/version.js.map +1 -0
- package/dist/src/identifiers.d.ts +10 -0
- package/dist/src/identifiers.js +31 -0
- package/dist/src/identifiers.js.map +1 -0
- package/dist/src/proto/google/protobuf/any.js +8 -8
- package/dist/src/proto/google/protobuf/any.js.map +1 -1
- package/dist/src/proto/google/protobuf/empty.js +2 -4
- package/dist/src/proto/google/protobuf/empty.js.map +1 -1
- package/dist/src/proto/google/protobuf/timestamp.js +10 -10
- package/dist/src/proto/google/protobuf/timestamp.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +46 -49
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +63 -54
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +226 -232
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +146 -168
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +26 -29
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +1 -1
- package/dist/src/rpc-protocol/DhtCallContext.d.ts +2 -2
- package/dist/src/rpc-protocol/DhtCallContext.js +8 -0
- 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 +10 -2
- package/dist/src/transport/ITransport.js +5 -0
- package/dist/src/transport/ITransport.js.map +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.js +3 -1
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +4 -2
- package/dist/src/transport/RoutingRpcCommunicator.js +19 -12
- 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 +13 -9
- package/package.json +16 -13
- package/protos/DhtRpc.proto +76 -76
- package/src/connection/Connection.ts +6 -1
- package/src/connection/ConnectionLockHandler.ts +30 -22
- package/src/connection/ConnectionLockRpcLocal.ts +7 -12
- package/src/connection/ConnectionLockRpcRemote.ts +9 -19
- package/src/connection/ConnectionManager.ts +132 -138
- package/src/connection/ConnectorFacade.ts +10 -9
- package/src/connection/Handshaker.ts +23 -11
- package/src/connection/IConnection.ts +2 -8
- package/src/connection/ManagedConnection.ts +34 -35
- package/src/connection/connectivityChecker.ts +109 -0
- package/src/connection/connectivityRequestHandler.ts +103 -0
- package/src/connection/simulator/Simulator.ts +6 -37
- package/src/connection/simulator/SimulatorConnection.ts +23 -25
- package/src/connection/simulator/SimulatorConnector.ts +11 -11
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +17 -25
- package/src/connection/webrtc/NodeWebrtcConnection.ts +24 -26
- package/src/connection/webrtc/WebrtcConnector.ts +18 -29
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +29 -24
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +4 -14
- package/src/connection/webrtc/iceServerAsString.ts +1 -1
- package/src/connection/websocket/AutoCertifierClientFacade.ts +4 -6
- package/src/connection/websocket/ClientWebsocket.ts +10 -4
- package/src/connection/websocket/WebsocketConnector.ts +120 -97
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +13 -15
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +7 -27
- package/src/connection/websocket/WebsocketServer.ts +50 -53
- package/src/connection/websocket/WebsocketServerConnection.ts +104 -0
- package/src/dht/DhtNode.ts +289 -410
- package/src/dht/DhtNodeRpcLocal.ts +25 -17
- package/src/dht/DhtNodeRpcRemote.ts +43 -27
- package/src/dht/ExternalApiRpcLocal.ts +30 -17
- package/src/dht/ExternalApiRpcRemote.ts +14 -11
- package/src/dht/PeerManager.ts +339 -0
- package/src/dht/contact/Contact.ts +4 -4
- package/src/dht/contact/ContactList.ts +11 -10
- package/src/dht/contact/RandomContactList.ts +15 -15
- package/src/dht/contact/RingContactList.ts +151 -0
- package/src/dht/contact/{Remote.ts → RpcRemote.ts} +16 -19
- package/src/dht/contact/SortedContactList.ts +120 -72
- package/src/dht/contact/ringIdentifiers.ts +62 -0
- package/src/dht/discovery/DiscoverySession.ts +36 -61
- package/src/dht/discovery/PeerDiscovery.ts +98 -43
- package/src/dht/discovery/RingDiscoverySession.ts +160 -0
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +246 -0
- package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +34 -0
- package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +43 -0
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +232 -0
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +35 -0
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.ts +30 -0
- package/src/dht/routing/DuplicateDetector.ts +9 -21
- package/src/dht/routing/Router.ts +95 -92
- package/src/dht/routing/RouterRpcLocal.ts +16 -18
- package/src/dht/routing/RouterRpcRemote.ts +26 -24
- package/src/dht/routing/RoutingSession.ts +119 -98
- package/src/dht/routing/RoutingTablesCache.ts +58 -0
- package/src/dht/routing/getPreviousPeer.ts +1 -1
- package/src/dht/store/LocalDataStore.ts +47 -77
- package/src/dht/store/StoreManager.ts +209 -0
- package/src/dht/store/StoreRpcLocal.ts +39 -308
- package/src/dht/store/StoreRpcRemote.ts +13 -31
- package/src/exports.ts +13 -3
- package/src/helpers/AddressTools.ts +2 -0
- package/src/helpers/MapWithTtl.ts +71 -0
- package/src/helpers/createPeerDescriptor.ts +57 -0
- package/src/helpers/createPeerDescriptorSignaturePayload.ts +28 -0
- package/src/helpers/offering.ts +15 -0
- package/src/helpers/protoClasses.ts +4 -6
- package/src/helpers/version.ts +32 -0
- package/src/identifiers.ts +29 -0
- package/src/proto/google/protobuf/any.ts +4 -4
- package/src/proto/google/protobuf/empty.ts +2 -4
- package/src/proto/google/protobuf/timestamp.ts +4 -4
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +65 -68
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +27 -30
- package/src/proto/packages/dht/protos/DhtRpc.ts +297 -313
- package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +1 -1
- package/src/rpc-protocol/DhtCallContext.ts +2 -2
- package/src/rpc-protocol/DhtRpcOptions.ts +2 -1
- package/src/transport/ITransport.ts +11 -2
- package/src/transport/ListeningRpcCommunicator.ts +1 -1
- package/src/transport/RoutingRpcCommunicator.ts +21 -14
- package/test/RandomGraphSimulation.ts +3 -2
- package/test/benchmark/Find.test.ts +13 -28
- package/test/benchmark/KademliaCorrectness.test.ts +24 -28
- package/test/benchmark/RingCorrectness.test.ts +157 -0
- package/test/benchmark/SortedContactListBenchmark.test.ts +151 -0
- package/test/benchmark/WebsocketServerMemoryLeak.test.ts +41 -0
- package/test/benchmark/hybrid-network-simulation/RingContactList.test.ts +72 -0
- package/test/benchmark/kademlia-simulation/Contact.ts +9 -9
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +11 -11
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +29 -25
- package/test/data/generateGroundTruthData.ts +7 -6
- package/test/end-to-end/Layer0-Layer1.test.ts +11 -15
- package/test/end-to-end/Layer0.test.ts +19 -22
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +19 -21
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +26 -28
- package/test/end-to-end/Layer0Webrtc.test.ts +19 -19
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +10 -18
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +8 -15
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +2 -2
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +8 -9
- package/test/end-to-end/memory-leak.test.ts +19 -24
- package/test/integration/ConnectionLocking.test.ts +66 -60
- package/test/integration/ConnectionManager.test.ts +43 -63
- package/test/integration/ConnectivityChecking.test.ts +52 -0
- package/test/integration/DhtJoinPeerDiscovery.test.ts +8 -12
- package/test/integration/DhtNodeExternalAPI.test.ts +17 -21
- package/test/integration/DhtNodeRpcRemote.test.ts +19 -26
- package/test/integration/DhtRpc.test.ts +20 -24
- package/test/integration/Find.test.ts +10 -12
- package/test/integration/Layer1-scale.test.ts +25 -37
- package/test/integration/Mock-Layer1-Layer0.test.ts +39 -59
- package/test/integration/MultipleEntryPointJoining.test.ts +14 -14
- package/test/integration/ReplicateData.test.ts +106 -0
- package/test/integration/RouteMessage.test.ts +42 -68
- package/test/integration/RouterRpcRemote.test.ts +19 -24
- package/test/integration/ScaleDownDht.test.ts +14 -12
- package/test/integration/SimultaneousConnections.test.ts +112 -111
- package/test/integration/Store.test.ts +43 -27
- package/test/integration/StoreAndDelete.test.ts +36 -48
- package/test/integration/StoreOnDhtWithThreeNodes.test.ts +59 -0
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +17 -37
- package/test/integration/StoreRpcRemote.test.ts +20 -32
- package/test/integration/WebrtcConnectionManagement.test.ts +39 -25
- package/test/integration/WebrtcConnectorRpc.test.ts +6 -11
- package/test/integration/WebsocketConnectionManagement.test.ts +87 -22
- package/test/integration/WebsocketConnectorRpc.test.ts +14 -24
- package/test/integration/{RpcErrors.test.ts → rpc-connections-over-webrpc.test.ts} +15 -26
- package/test/unit/AddressTools.test.ts +4 -0
- package/test/unit/ConnectivityHelpers.test.ts +9 -17
- package/test/unit/DuplicateDetector.test.ts +8 -5
- package/test/unit/LocalDataStore.test.ts +78 -75
- package/test/unit/PeerManager.test.ts +33 -0
- package/test/unit/RandomContactList.test.ts +12 -9
- package/test/unit/RecursiveOperationManager.test.ts +157 -0
- package/test/unit/RecursiveOperationSession.test.ts +68 -0
- package/test/unit/Router.test.ts +52 -35
- package/test/unit/RoutingSession.test.ts +79 -0
- package/test/unit/SortedContactList.test.ts +61 -30
- package/test/unit/StoreManager.test.ts +138 -0
- package/test/unit/WebsocketConnector.test.ts +27 -35
- package/test/unit/connectivityRequestHandler.test.ts +104 -0
- package/test/unit/createPeerDescriptor.test.ts +69 -0
- package/test/unit/customMatchers.test.ts +16 -0
- package/test/unit/version.test.ts +18 -0
- package/test/utils/FakeTransport.ts +47 -0
- package/test/utils/customMatchers.ts +71 -0
- package/test/utils/mock/MockRpcCommunicator.ts +7 -0
- package/test/utils/mock/Router.ts +13 -3
- package/test/utils/mock/Transport.ts +1 -1
- package/test/utils/mock/mockDataEntry.ts +38 -0
- package/test/utils/utils.ts +104 -107
- package/tsconfig.browser.json +2 -1
- package/tsconfig.jest.json +4 -2
- package/tsconfig.node.json +4 -2
- package/dist/src/connection/ConnectivityChecker.d.ts +0 -17
- package/dist/src/connection/ConnectivityChecker.js +0 -208
- package/dist/src/connection/ConnectivityChecker.js.map +0 -1
- package/dist/src/connection/websocket/ServerWebsocket.js +0 -100
- package/dist/src/connection/websocket/ServerWebsocket.js.map +0 -1
- package/dist/src/dht/contact/Remote.js.map +0 -1
- package/dist/src/dht/find/FindRpcLocal.d.ts +0 -14
- package/dist/src/dht/find/FindRpcLocal.js +0 -25
- 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 -145
- 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 -184
- 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 +0 -41
- package/dist/src/dht/routing/FindRpcRemote.js.map +0 -1
- package/dist/src/helpers/PeerID.d.ts +0 -24
- package/dist/src/helpers/PeerID.js +0 -78
- package/dist/src/helpers/PeerID.js.map +0 -1
- package/dist/src/helpers/UUID.d.ts +0 -8
- package/dist/src/helpers/UUID.js +0 -36
- package/dist/src/helpers/UUID.js.map +0 -1
- package/dist/src/helpers/kademliaId.d.ts +0 -1
- package/dist/src/helpers/kademliaId.js +0 -14
- package/dist/src/helpers/kademliaId.js.map +0 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +0 -6
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +0 -23
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +0 -1
- package/src/connection/ConnectivityChecker.ts +0 -199
- package/src/connection/websocket/ServerWebsocket.ts +0 -114
- package/src/dht/find/FindRpcLocal.ts +0 -35
- package/src/dht/find/FindSession.ts +0 -178
- package/src/dht/find/FindSessionRpcLocal.ts +0 -25
- package/src/dht/find/FindSessionRpcRemote.ts +0 -30
- package/src/dht/find/Finder.ts +0 -275
- package/src/dht/routing/FindRpcRemote.ts +0 -40
- package/src/helpers/PeerID.ts +0 -88
- package/src/helpers/UUID.ts +0 -35
- package/src/helpers/kademliaId.ts +0 -8
- package/src/helpers/peerIdFromPeerDescriptor.ts +0 -20
- package/test/integration/MigrateData.test.ts +0 -204
- package/test/unit/Finder.test.ts +0 -110
- package/test/unit/PeerID.test.ts +0 -22
- package/test/unit/UUID.test.ts +0 -55
package/dist/src/dht/DhtNode.js
CHANGED
|
@@ -1,64 +1,53 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.DhtNode =
|
|
7
|
-
const DhtNodeRpcRemote_1 = require("./DhtNodeRpcRemote");
|
|
8
|
-
const k_bucket_1 = __importDefault(require("k-bucket"));
|
|
9
|
-
const eventemitter3_1 = require("eventemitter3");
|
|
10
|
-
const SortedContactList_1 = require("./contact/SortedContactList");
|
|
11
|
-
const RoutingRpcCommunicator_1 = require("../transport/RoutingRpcCommunicator");
|
|
12
|
-
const PeerID_1 = require("../helpers/PeerID");
|
|
13
|
-
const DhtRpc_1 = require("../proto/packages/dht/protos/DhtRpc");
|
|
14
|
-
const ConnectionManager_1 = require("../connection/ConnectionManager");
|
|
15
|
-
const DhtRpc_client_1 = require("../proto/packages/dht/protos/DhtRpc.client");
|
|
3
|
+
exports.DhtNode = void 0;
|
|
16
4
|
const utils_1 = require("@streamr/utils");
|
|
17
|
-
const
|
|
18
|
-
const RandomContactList_1 = require("./contact/RandomContactList");
|
|
19
|
-
const peerIdFromPeerDescriptor_1 = require("../helpers/peerIdFromPeerDescriptor");
|
|
20
|
-
const Router_1 = require("./routing/Router");
|
|
21
|
-
const Finder_1 = require("./find/Finder");
|
|
22
|
-
const StoreRpcLocal_1 = require("./store/StoreRpcLocal");
|
|
23
|
-
const PeerDiscovery_1 = require("./discovery/PeerDiscovery");
|
|
24
|
-
const LocalDataStore_1 = require("./store/LocalDataStore");
|
|
25
|
-
const ExternalApiRpcRemote_1 = require("./ExternalApiRpcRemote");
|
|
26
|
-
const UUID_1 = require("../helpers/UUID");
|
|
27
|
-
const isBrowserEnvironment_1 = require("../helpers/browser/isBrowserEnvironment");
|
|
5
|
+
const eventemitter3_1 = require("eventemitter3");
|
|
28
6
|
const lodash_1 = require("lodash");
|
|
7
|
+
const ConnectionManager_1 = require("../connection/ConnectionManager");
|
|
29
8
|
const ConnectorFacade_1 = require("../connection/ConnectorFacade");
|
|
9
|
+
const isBrowserEnvironment_1 = require("../helpers/browser/isBrowserEnvironment");
|
|
10
|
+
const identifiers_1 = require("../identifiers");
|
|
11
|
+
const DhtRpc_1 = require("../proto/packages/dht/protos/DhtRpc");
|
|
12
|
+
const DhtRpc_client_1 = require("../proto/packages/dht/protos/DhtRpc.client");
|
|
13
|
+
const RoutingRpcCommunicator_1 = require("../transport/RoutingRpcCommunicator");
|
|
30
14
|
const DhtNodeRpcLocal_1 = require("./DhtNodeRpcLocal");
|
|
15
|
+
const DhtNodeRpcRemote_1 = require("./DhtNodeRpcRemote");
|
|
31
16
|
const ExternalApiRpcLocal_1 = require("./ExternalApiRpcLocal");
|
|
17
|
+
const ExternalApiRpcRemote_1 = require("./ExternalApiRpcRemote");
|
|
18
|
+
const PeerManager_1 = require("./PeerManager");
|
|
19
|
+
const PeerDiscovery_1 = require("./discovery/PeerDiscovery");
|
|
20
|
+
const RecursiveOperationManager_1 = require("./recursive-operation/RecursiveOperationManager");
|
|
21
|
+
const Router_1 = require("./routing/Router");
|
|
22
|
+
const LocalDataStore_1 = require("./store/LocalDataStore");
|
|
23
|
+
const StoreManager_1 = require("./store/StoreManager");
|
|
24
|
+
const StoreRpcRemote_1 = require("./store/StoreRpcRemote");
|
|
25
|
+
const createPeerDescriptor_1 = require("../helpers/createPeerDescriptor");
|
|
26
|
+
const cdn_location_1 = require("@streamr/cdn-location");
|
|
32
27
|
const logger = new utils_1.Logger(module);
|
|
33
|
-
const createPeerDescriptor = (msg, peerId) => {
|
|
34
|
-
let kademliaId;
|
|
35
|
-
if (msg) {
|
|
36
|
-
kademliaId = peerId ? (0, utils_1.hexToBinary)(peerId) : PeerID_1.PeerID.fromIp(msg.host).value;
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
kademliaId = (0, utils_1.hexToBinary)(peerId);
|
|
40
|
-
}
|
|
41
|
-
const nodeType = (0, isBrowserEnvironment_1.isBrowserEnvironment)() ? DhtRpc_1.NodeType.BROWSER : DhtRpc_1.NodeType.NODEJS;
|
|
42
|
-
const ret = { kademliaId, type: nodeType };
|
|
43
|
-
if (msg && msg.websocket) {
|
|
44
|
-
ret.websocket = { host: msg.websocket.host, port: msg.websocket.port, tls: msg.websocket.tls };
|
|
45
|
-
}
|
|
46
|
-
return ret;
|
|
47
|
-
};
|
|
48
|
-
exports.createPeerDescriptor = createPeerDescriptor;
|
|
49
28
|
class DhtNode extends eventemitter3_1.EventEmitter {
|
|
29
|
+
config;
|
|
30
|
+
rpcCommunicator;
|
|
31
|
+
transport;
|
|
32
|
+
localPeerDescriptor;
|
|
33
|
+
router;
|
|
34
|
+
storeManager;
|
|
35
|
+
localDataStore;
|
|
36
|
+
recursiveOperationManager;
|
|
37
|
+
peerDiscovery;
|
|
38
|
+
peerManager;
|
|
39
|
+
connectionManager;
|
|
40
|
+
region;
|
|
41
|
+
started = false;
|
|
42
|
+
abortController = new AbortController();
|
|
50
43
|
constructor(conf) {
|
|
51
44
|
super();
|
|
52
|
-
this.connections = new Map();
|
|
53
|
-
this.localDataStore = new LocalDataStore_1.LocalDataStore();
|
|
54
|
-
this.started = false;
|
|
55
|
-
this.stopped = false;
|
|
56
45
|
this.config = (0, utils_1.merge)({
|
|
57
46
|
serviceId: 'layer0',
|
|
58
47
|
joinParallelism: 3,
|
|
59
48
|
maxNeighborListSize: 200,
|
|
60
49
|
numberOfNodesPerKBucket: 8,
|
|
61
|
-
joinNoProgressLimit:
|
|
50
|
+
joinNoProgressLimit: 5,
|
|
62
51
|
dhtJoinTimeout: 60000,
|
|
63
52
|
peerDiscoveryQueryBatchSize: 5,
|
|
64
53
|
maxConnections: 80,
|
|
@@ -66,13 +55,30 @@ class DhtNode extends eventemitter3_1.EventEmitter {
|
|
|
66
55
|
storeMaxTtl: 60000,
|
|
67
56
|
networkConnectivityTimeout: 10000,
|
|
68
57
|
storageRedundancyFactor: 5,
|
|
69
|
-
metricsContext: new utils_1.MetricsContext()
|
|
70
|
-
peerId: new UUID_1.UUID().toHex()
|
|
58
|
+
metricsContext: new utils_1.MetricsContext()
|
|
71
59
|
}, conf);
|
|
60
|
+
this.validateConfig();
|
|
61
|
+
this.localDataStore = new LocalDataStore_1.LocalDataStore(this.config.storeMaxTtl);
|
|
72
62
|
this.send = this.send.bind(this);
|
|
73
63
|
}
|
|
64
|
+
validateConfig() {
|
|
65
|
+
const expectedNodeIdLength = identifiers_1.KADEMLIA_ID_LENGTH_IN_BYTES * 2;
|
|
66
|
+
if (this.config.nodeId !== undefined) {
|
|
67
|
+
if (!/^[0-9a-fA-F]+$/.test(this.config.nodeId)) {
|
|
68
|
+
throw new Error('Invalid nodeId, the nodeId should be a hex string');
|
|
69
|
+
}
|
|
70
|
+
else if (this.config.nodeId.length !== expectedNodeIdLength) {
|
|
71
|
+
throw new Error(`Invalid nodeId, the length of the nodeId should be ${expectedNodeIdLength}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (this.config.peerDescriptor !== undefined) {
|
|
75
|
+
if (this.config.peerDescriptor.nodeId.length !== identifiers_1.KADEMLIA_ID_LENGTH_IN_BYTES) {
|
|
76
|
+
throw new Error(`Invalid peerDescriptor, the length of the nodeId should be ${identifiers_1.KADEMLIA_ID_LENGTH_IN_BYTES} bytes`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
74
80
|
async start() {
|
|
75
|
-
if (this.started || this.
|
|
81
|
+
if (this.started || this.abortController.signal.aborted) {
|
|
76
82
|
return;
|
|
77
83
|
}
|
|
78
84
|
logger.trace(`Starting new Streamr Network DHT Node with serviceId ${this.config.serviceId}`);
|
|
@@ -83,6 +89,15 @@ class DhtNode extends eventemitter3_1.EventEmitter {
|
|
|
83
89
|
this.config.peerDescriptor.websocket = undefined;
|
|
84
90
|
}
|
|
85
91
|
}
|
|
92
|
+
if (this.region !== undefined) {
|
|
93
|
+
this.region = this.config.region;
|
|
94
|
+
}
|
|
95
|
+
else if (this.config.peerDescriptor?.region !== undefined) {
|
|
96
|
+
this.region = this.config.peerDescriptor.region;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
this.region = await (0, cdn_location_1.getLocalRegion)();
|
|
100
|
+
}
|
|
86
101
|
// If transport is given, do not create a ConnectionManager
|
|
87
102
|
if (this.config.transport) {
|
|
88
103
|
this.transport = this.config.transport;
|
|
@@ -132,338 +147,234 @@ class DhtNode extends eventemitter3_1.EventEmitter {
|
|
|
132
147
|
this.transport = connectionManager;
|
|
133
148
|
}
|
|
134
149
|
this.rpcCommunicator = new RoutingRpcCommunicator_1.RoutingRpcCommunicator(this.config.serviceId, this.transport.send, { rpcRequestTimeout: this.config.rpcRequestTimeout });
|
|
135
|
-
this.transport.on('message', (message) => this.
|
|
136
|
-
this.
|
|
150
|
+
this.transport.on('message', (message) => this.handleMessageFromTransport(message));
|
|
151
|
+
this.initPeerManager();
|
|
137
152
|
this.peerDiscovery = new PeerDiscovery_1.PeerDiscovery({
|
|
138
|
-
rpcCommunicator: this.rpcCommunicator,
|
|
139
153
|
localPeerDescriptor: this.localPeerDescriptor,
|
|
140
|
-
bucket: this.bucket,
|
|
141
|
-
neighborList: this.neighborList,
|
|
142
154
|
joinNoProgressLimit: this.config.joinNoProgressLimit,
|
|
143
155
|
peerDiscoveryQueryBatchSize: this.config.peerDiscoveryQueryBatchSize,
|
|
144
156
|
joinTimeout: this.config.dhtJoinTimeout,
|
|
145
157
|
serviceId: this.config.serviceId,
|
|
146
158
|
parallelism: this.config.joinParallelism,
|
|
147
|
-
addContact: this.addNewContact.bind(this),
|
|
148
159
|
connectionManager: this.connectionManager,
|
|
149
|
-
|
|
160
|
+
peerManager: this.peerManager
|
|
150
161
|
});
|
|
151
162
|
this.router = new Router_1.Router({
|
|
152
163
|
rpcCommunicator: this.rpcCommunicator,
|
|
153
|
-
connections: this.connections,
|
|
164
|
+
connections: this.peerManager.connections,
|
|
154
165
|
localPeerDescriptor: this.localPeerDescriptor,
|
|
155
|
-
addContact: this.
|
|
156
|
-
|
|
157
|
-
connectionManager: this.connectionManager
|
|
166
|
+
addContact: (contact, setActive) => this.peerManager.addContact([contact], setActive),
|
|
167
|
+
handleMessage: (message) => this.handleMessageFromRouter(message),
|
|
158
168
|
});
|
|
159
|
-
this.
|
|
169
|
+
this.recursiveOperationManager = new RecursiveOperationManager_1.RecursiveOperationManager({
|
|
160
170
|
rpcCommunicator: this.rpcCommunicator,
|
|
161
171
|
router: this.router,
|
|
162
172
|
sessionTransport: this,
|
|
163
|
-
connections: this.connections,
|
|
173
|
+
connections: this.peerManager.connections,
|
|
164
174
|
localPeerDescriptor: this.localPeerDescriptor,
|
|
165
175
|
serviceId: this.config.serviceId,
|
|
166
|
-
addContact: this.
|
|
167
|
-
isPeerCloserToIdThanSelf: this.isPeerCloserToIdThanSelf.bind(this),
|
|
176
|
+
addContact: (contact) => this.peerManager.addContact([contact]),
|
|
168
177
|
localDataStore: this.localDataStore
|
|
169
178
|
});
|
|
170
|
-
this.
|
|
179
|
+
this.storeManager = new StoreManager_1.StoreManager({
|
|
171
180
|
rpcCommunicator: this.rpcCommunicator,
|
|
172
|
-
|
|
181
|
+
recursiveOperationManager: this.recursiveOperationManager,
|
|
173
182
|
localPeerDescriptor: this.localPeerDescriptor,
|
|
174
183
|
serviceId: this.config.serviceId,
|
|
175
184
|
highestTtl: this.config.storeHighestTtl,
|
|
176
|
-
maxTtl: this.config.storeMaxTtl,
|
|
177
185
|
redundancyFactor: this.config.storageRedundancyFactor,
|
|
178
186
|
localDataStore: this.localDataStore,
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
return this.bucket.closest(id, n);
|
|
187
|
+
getClosestNeighborsTo: (key, n) => {
|
|
188
|
+
return this.peerManager.getClosestNeighborsTo(key, n).map((n) => n.getPeerDescriptor());
|
|
182
189
|
},
|
|
183
|
-
|
|
190
|
+
createRpcRemote: (contact) => {
|
|
191
|
+
return new StoreRpcRemote_1.StoreRpcRemote(this.localPeerDescriptor, contact, this.rpcCommunicator, DhtRpc_client_1.StoreRpcClient, this.config.rpcRequestTimeout);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
this.on('contactAdded', (peerDescriptor) => {
|
|
195
|
+
this.storeManager.onContactAdded(peerDescriptor);
|
|
184
196
|
});
|
|
185
197
|
this.bindRpcLocalMethods();
|
|
186
|
-
if (this.connectionManager && this.config.entryPoints && this.config.entryPoints.length > 0
|
|
187
|
-
&& !(0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(this.config.entryPoints[0], this.localPeerDescriptor)) {
|
|
188
|
-
this.connectToEntryPoint(this.config.entryPoints[0]);
|
|
189
|
-
}
|
|
190
198
|
}
|
|
191
|
-
|
|
192
|
-
this.
|
|
193
|
-
localNodeId: selfId.value,
|
|
199
|
+
initPeerManager() {
|
|
200
|
+
this.peerManager = new PeerManager_1.PeerManager({
|
|
194
201
|
numberOfNodesPerKBucket: this.config.numberOfNodesPerKBucket,
|
|
195
|
-
|
|
202
|
+
maxContactListSize: this.config.maxNeighborListSize,
|
|
203
|
+
localNodeId: this.getNodeId(),
|
|
204
|
+
localPeerDescriptor: this.localPeerDescriptor,
|
|
205
|
+
connectionManager: this.connectionManager,
|
|
206
|
+
peerDiscoveryQueryBatchSize: this.config.peerDiscoveryQueryBatchSize,
|
|
207
|
+
isLayer0: (this.connectionManager !== undefined),
|
|
208
|
+
createDhtNodeRpcRemote: (peerDescriptor) => this.createDhtNodeRpcRemote(peerDescriptor),
|
|
209
|
+
lockId: this.config.serviceId
|
|
210
|
+
});
|
|
211
|
+
this.peerManager.on('contactRemoved', (peerDescriptor, activeContacts) => {
|
|
212
|
+
this.emit('contactRemoved', peerDescriptor, activeContacts);
|
|
196
213
|
});
|
|
197
|
-
this.
|
|
198
|
-
this.
|
|
199
|
-
this.
|
|
200
|
-
this.
|
|
201
|
-
|
|
214
|
+
this.peerManager.on('contactAdded', (peerDescriptor, activeContacts) => this.emit('contactAdded', peerDescriptor, activeContacts));
|
|
215
|
+
this.peerManager.on('randomContactRemoved', (peerDescriptor, activeContacts) => this.emit('randomContactRemoved', peerDescriptor, activeContacts));
|
|
216
|
+
this.peerManager.on('randomContactAdded', (peerDescriptor, activeContacts) => this.emit('randomContactAdded', peerDescriptor, activeContacts));
|
|
217
|
+
this.peerManager.on('ringContactRemoved', (peerDescriptor, activeContacts) => {
|
|
218
|
+
this.emit('ringContactRemoved', peerDescriptor, activeContacts);
|
|
202
219
|
});
|
|
203
|
-
this.
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
220
|
+
this.peerManager.on('ringContactAdded', (peerDescriptor, activeContacts) => {
|
|
221
|
+
this.emit('ringContactAdded', peerDescriptor, activeContacts);
|
|
222
|
+
});
|
|
223
|
+
this.peerManager.on('kBucketEmpty', () => {
|
|
224
|
+
if (!this.peerDiscovery.isJoinOngoing()
|
|
225
|
+
&& this.config.entryPoints
|
|
226
|
+
&& this.config.entryPoints.length > 0) {
|
|
227
|
+
setImmediate(async () => {
|
|
228
|
+
// TODO should we catch possible promise rejection?
|
|
229
|
+
await Promise.all(this.config.entryPoints.map((entryPoint) => this.peerDiscovery.rejoinDht(entryPoint)));
|
|
230
|
+
});
|
|
207
231
|
}
|
|
208
|
-
this.emit('contactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()));
|
|
209
|
-
this.randomPeers.addContact(new DhtNodeRpcRemote_1.DhtNodeRpcRemote(this.localPeerDescriptor, removedContact.getPeerDescriptor(), (0, proto_rpc_1.toProtoRpcClient)(new DhtRpc_client_1.DhtNodeRpcClient(this.rpcCommunicator.getRpcClientTransport())), this.config.serviceId, this.config.rpcRequestTimeout));
|
|
210
232
|
});
|
|
211
|
-
this.
|
|
212
|
-
|
|
233
|
+
this.transport.on('connected', (peerDescriptor) => {
|
|
234
|
+
this.peerManager.onContactConnected(peerDescriptor);
|
|
235
|
+
this.router.onNodeConnected(peerDescriptor);
|
|
236
|
+
this.emit('connected', peerDescriptor);
|
|
237
|
+
});
|
|
213
238
|
this.transport.on('disconnected', (peerDescriptor, gracefulLeave) => {
|
|
214
|
-
this.
|
|
239
|
+
this.peerManager.onContactDisconnected((0, identifiers_1.getNodeIdFromPeerDescriptor)(peerDescriptor), gracefulLeave);
|
|
240
|
+
this.router.onNodeDisconnected(peerDescriptor);
|
|
241
|
+
this.emit('disconnected', peerDescriptor, gracefulLeave);
|
|
215
242
|
});
|
|
216
|
-
this.transport.
|
|
217
|
-
|
|
218
|
-
if ((0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(peer, this.localPeerDescriptor)) {
|
|
219
|
-
logger.error('own peerdescriptor added to connections in initKBucket');
|
|
220
|
-
}
|
|
221
|
-
this.connections.set((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peer), rpcRemote);
|
|
243
|
+
this.transport.getConnections().forEach((peer) => {
|
|
244
|
+
this.peerManager.onContactConnected(peer);
|
|
222
245
|
});
|
|
223
|
-
this.randomPeers = new RandomContactList_1.RandomContactList(selfId, this.config.maxNeighborListSize);
|
|
224
|
-
this.randomPeers.on('contactRemoved', (removedContact, activeContacts) => this.emit('randomContactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor())));
|
|
225
|
-
this.randomPeers.on('newContact', (newContact, activeContacts) => this.emit('newRandomContact', newContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor())));
|
|
226
|
-
}
|
|
227
|
-
onTransportConnected(peerDescriptor) {
|
|
228
|
-
if ((0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(this.localPeerDescriptor, peerDescriptor)) {
|
|
229
|
-
logger.error('onTransportConnected() to self');
|
|
230
|
-
}
|
|
231
|
-
const rpcRemote = new DhtNodeRpcRemote_1.DhtNodeRpcRemote(this.localPeerDescriptor, peerDescriptor, (0, proto_rpc_1.toProtoRpcClient)(new DhtRpc_client_1.DhtNodeRpcClient(this.rpcCommunicator.getRpcClientTransport())), this.config.serviceId, this.config.rpcRequestTimeout);
|
|
232
|
-
if (!this.connections.has(PeerID_1.PeerID.fromValue(rpcRemote.id).toKey())) {
|
|
233
|
-
this.connections.set(PeerID_1.PeerID.fromValue(rpcRemote.id).toKey(), rpcRemote);
|
|
234
|
-
logger.trace('connectionschange add ' + this.connections.size);
|
|
235
|
-
}
|
|
236
|
-
else {
|
|
237
|
-
logger.trace('new connection not set to connections, there is already a connection with the peer ID');
|
|
238
|
-
}
|
|
239
|
-
logger.trace('connected: ' + (0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(peerDescriptor) + ' ' + this.connections.size);
|
|
240
|
-
this.emit('connected', peerDescriptor);
|
|
241
|
-
}
|
|
242
|
-
onTransportDisconnected(peerDescriptor, gracefulLeave) {
|
|
243
|
-
logger.trace('disconnected: ' + (0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(peerDescriptor));
|
|
244
|
-
this.connections.delete((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor));
|
|
245
|
-
// only remove from bucket if we are on layer 0
|
|
246
|
-
if (this.connectionManager) {
|
|
247
|
-
this.bucket.remove(peerDescriptor.kademliaId);
|
|
248
|
-
if (gracefulLeave === true) {
|
|
249
|
-
logger.trace((0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(peerDescriptor) + ' ' + 'onTransportDisconnected with gracefulLeave ' + gracefulLeave);
|
|
250
|
-
this.removeContact(peerDescriptor);
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
logger.trace((0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(peerDescriptor) + ' ' + 'onTransportDisconnected with gracefulLeave ' + gracefulLeave);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
this.emit('disconnected', peerDescriptor, gracefulLeave);
|
|
257
246
|
}
|
|
258
247
|
bindRpcLocalMethods() {
|
|
259
|
-
if (!this.started || this.
|
|
248
|
+
if (!this.started || this.abortController.signal.aborted) {
|
|
260
249
|
return;
|
|
261
250
|
}
|
|
262
251
|
const dhtNodeRpcLocal = new DhtNodeRpcLocal_1.DhtNodeRpcLocal({
|
|
263
|
-
bucket: this.bucket,
|
|
264
|
-
serviceId: this.config.serviceId,
|
|
265
252
|
peerDiscoveryQueryBatchSize: this.config.peerDiscoveryQueryBatchSize,
|
|
266
|
-
|
|
267
|
-
|
|
253
|
+
getClosestPeersTo: (nodeId, limit) => {
|
|
254
|
+
return this.peerManager.getClosestNeighborsTo(nodeId, limit)
|
|
255
|
+
.map((dhtPeer) => dhtPeer.getPeerDescriptor());
|
|
256
|
+
},
|
|
257
|
+
getClosestRingPeersTo: (ringIdRaw, limit) => {
|
|
258
|
+
return this.getClosestRingContactsTo(ringIdRaw, limit);
|
|
259
|
+
},
|
|
260
|
+
addContact: (contact) => this.peerManager.addContact([contact]),
|
|
261
|
+
removeContact: (nodeId) => this.removeContact(nodeId)
|
|
268
262
|
});
|
|
269
263
|
this.rpcCommunicator.registerRpcMethod(DhtRpc_1.ClosestPeersRequest, DhtRpc_1.ClosestPeersResponse, 'getClosestPeers', (req, context) => dhtNodeRpcLocal.getClosestPeers(req, context));
|
|
264
|
+
this.rpcCommunicator.registerRpcMethod(DhtRpc_1.ClosestRingPeersRequest, DhtRpc_1.ClosestRingPeersResponse, 'getClosestRingPeers', (req, context) => dhtNodeRpcLocal.getClosestRingPeers(req, context));
|
|
270
265
|
this.rpcCommunicator.registerRpcMethod(DhtRpc_1.PingRequest, DhtRpc_1.PingResponse, 'ping', (req, context) => dhtNodeRpcLocal.ping(req, context));
|
|
271
|
-
this.rpcCommunicator.registerRpcNotification(DhtRpc_1.LeaveNotice, 'leaveNotice', (
|
|
266
|
+
this.rpcCommunicator.registerRpcNotification(DhtRpc_1.LeaveNotice, 'leaveNotice', (_req, context) => dhtNodeRpcLocal.leaveNotice(context));
|
|
272
267
|
const externalApiRpcLocal = new ExternalApiRpcLocal_1.ExternalApiRpcLocal({
|
|
273
|
-
|
|
274
|
-
return this.
|
|
268
|
+
executeRecursiveOperation: (key, operation, excludedPeer) => {
|
|
269
|
+
return this.recursiveOperationManager.execute(key, operation, excludedPeer);
|
|
275
270
|
},
|
|
276
|
-
storeDataToDht: (key, data) => this.storeDataToDht(key, data)
|
|
271
|
+
storeDataToDht: (key, data, creator) => this.storeDataToDht(key, data, creator)
|
|
277
272
|
});
|
|
278
|
-
this.rpcCommunicator.registerRpcMethod(DhtRpc_1.
|
|
279
|
-
|
|
273
|
+
this.rpcCommunicator.registerRpcMethod(DhtRpc_1.ExternalFetchDataRequest, DhtRpc_1.ExternalFetchDataResponse, 'externalFetchData', (req, context) => externalApiRpcLocal.externalFetchData(req, context), { timeout: 10000 } // TODO use config option or named constant?
|
|
274
|
+
);
|
|
275
|
+
this.rpcCommunicator.registerRpcMethod(DhtRpc_1.ExternalStoreDataRequest, DhtRpc_1.ExternalStoreDataResponse, 'externalStoreData', (req, context) => externalApiRpcLocal.externalStoreData(req, context), { timeout: 10000 } // TODO use config option or named constant?
|
|
276
|
+
);
|
|
280
277
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
278
|
+
handleMessageFromTransport(message) {
|
|
279
|
+
if (message.serviceId === this.config.serviceId) {
|
|
280
|
+
this.rpcCommunicator?.handleMessageFromPeer(message);
|
|
281
|
+
}
|
|
285
282
|
}
|
|
286
|
-
|
|
283
|
+
handleMessageFromRouter(message) {
|
|
287
284
|
if (message.serviceId === this.config.serviceId) {
|
|
288
|
-
logger.trace('callig this.handleMessageFromPeer ' + (0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(message.sourceDescriptor)
|
|
289
|
-
+ ' ' + message.serviceId + ' ' + message.messageId);
|
|
290
285
|
this.rpcCommunicator?.handleMessageFromPeer(message);
|
|
291
286
|
}
|
|
287
|
+
else if (this.connectionManager?.handleIncomingMessage(message)) {
|
|
288
|
+
// message was handled by connectionManager
|
|
289
|
+
}
|
|
292
290
|
else {
|
|
293
|
-
logger.trace('emit "message" ' + (0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(message.sourceDescriptor)
|
|
294
|
-
+ ' ' + message.serviceId + ' ' + message.messageId);
|
|
295
291
|
this.emit('message', message);
|
|
296
292
|
}
|
|
297
293
|
}
|
|
298
294
|
generatePeerDescriptorCallBack(connectivityResponse) {
|
|
299
|
-
if (this.config.peerDescriptor) {
|
|
295
|
+
if (this.config.peerDescriptor !== undefined) {
|
|
300
296
|
this.localPeerDescriptor = this.config.peerDescriptor;
|
|
301
297
|
}
|
|
302
298
|
else {
|
|
303
|
-
this.localPeerDescriptor = (0,
|
|
299
|
+
this.localPeerDescriptor = (0, createPeerDescriptor_1.createPeerDescriptor)(connectivityResponse, this.region, this.config.nodeId);
|
|
304
300
|
}
|
|
305
301
|
return this.localPeerDescriptor;
|
|
306
302
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
const sortingList = new SortedContactList_1.SortedContactList(this.getNodeId(), 100);
|
|
312
|
-
sortingList.addContacts(oldContacts);
|
|
313
|
-
const sortedContacts = sortingList.getAllContacts();
|
|
314
|
-
this.connectionManager?.weakUnlockConnection(sortedContacts[sortedContacts.length - 1].getPeerDescriptor());
|
|
315
|
-
this.bucket?.remove(sortedContacts[sortedContacts.length - 1].getPeerId().value);
|
|
316
|
-
this.bucket.add(newContact);
|
|
317
|
-
}
|
|
318
|
-
onKBucketRemoved(contact) {
|
|
319
|
-
if (this.stopped) {
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
this.connectionManager?.weakUnlockConnection(contact.getPeerDescriptor());
|
|
323
|
-
logger.trace(`Removed contact ${(0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(contact.getPeerDescriptor())}`);
|
|
324
|
-
if (this.bucket.count() === 0
|
|
325
|
-
&& !this.peerDiscovery.isJoinOngoing()
|
|
326
|
-
&& this.config.entryPoints
|
|
327
|
-
&& this.config.entryPoints.length > 0) {
|
|
328
|
-
setImmediate(async () => {
|
|
329
|
-
await Promise.all(this.config.entryPoints.map((entryPoint) => this.peerDiscovery.rejoinDht(entryPoint)));
|
|
330
|
-
});
|
|
331
|
-
}
|
|
303
|
+
getClosestContacts(limit) {
|
|
304
|
+
return this.peerManager.getClosestContactsTo(this.getNodeId(), limit).map((peer) => peer.getPeerDescriptor());
|
|
332
305
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
this.connectionManager?.weakLockConnection(contact.getPeerDescriptor());
|
|
340
|
-
if (this.connections.has(contact.getPeerId().toKey())) {
|
|
341
|
-
logger.trace(`Added new contact ${(0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(contact.getPeerDescriptor())}`);
|
|
342
|
-
}
|
|
343
|
-
else { // open connection by pinging
|
|
344
|
-
logger.trace('starting ping ' + (0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(contact.getPeerDescriptor()));
|
|
345
|
-
contact.ping().then((result) => {
|
|
346
|
-
if (result) {
|
|
347
|
-
logger.trace(`Added new contact ${(0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(contact.getPeerDescriptor())}`);
|
|
348
|
-
}
|
|
349
|
-
else {
|
|
350
|
-
logger.trace('ping failed ' + (0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(contact.getPeerDescriptor()));
|
|
351
|
-
this.connectionManager?.weakUnlockConnection(contact.getPeerDescriptor());
|
|
352
|
-
this.removeContact(contact.getPeerDescriptor());
|
|
353
|
-
this.addClosestContactToBucket();
|
|
354
|
-
}
|
|
355
|
-
return;
|
|
356
|
-
}).catch((_e) => {
|
|
357
|
-
this.connectionManager?.weakUnlockConnection(contact.getPeerDescriptor());
|
|
358
|
-
this.removeContact(contact.getPeerDescriptor());
|
|
359
|
-
this.addClosestContactToBucket();
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
addClosestContactToBucket() {
|
|
365
|
-
if (!this.started || this.stopped) {
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
const closest = this.getClosestActiveContactNotInBucket();
|
|
369
|
-
if (closest) {
|
|
370
|
-
this.addNewContact(closest.getPeerDescriptor());
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
getClosestActiveContactNotInBucket() {
|
|
374
|
-
for (const contactId of this.neighborList.getContactIds()) {
|
|
375
|
-
if (!this.bucket.get(contactId.value) && this.neighborList.isActive(contactId)) {
|
|
376
|
-
return this.neighborList.getContact(contactId).contact;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
return undefined;
|
|
380
|
-
}
|
|
381
|
-
getClosestContacts(maxCount) {
|
|
382
|
-
return this.neighborList.getClosestContacts(maxCount).map((c) => c.getPeerDescriptor());
|
|
306
|
+
getClosestRingContactsTo(ringIdRaw, limit) {
|
|
307
|
+
const closest = this.peerManager.getClosestRingContactsTo(ringIdRaw, limit);
|
|
308
|
+
return {
|
|
309
|
+
left: closest.left.map((dhtPeer) => dhtPeer.getPeerDescriptor()),
|
|
310
|
+
right: closest.right.map((dhtPeer) => dhtPeer.getPeerDescriptor())
|
|
311
|
+
};
|
|
383
312
|
}
|
|
384
313
|
getNodeId() {
|
|
385
|
-
return (0,
|
|
386
|
-
}
|
|
387
|
-
getBucketSize() {
|
|
388
|
-
return this.bucket.count();
|
|
389
|
-
}
|
|
390
|
-
addNewContact(contact, setActive = false) {
|
|
391
|
-
if (!this.started || this.stopped) {
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
|
-
if (!(0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(contact, this.localPeerDescriptor)) {
|
|
395
|
-
logger.trace(`Adding new contact ${(0, peerIdFromPeerDescriptor_1.getNodeIdFromPeerDescriptor)(contact)}`);
|
|
396
|
-
const rpcRemote = new DhtNodeRpcRemote_1.DhtNodeRpcRemote(this.localPeerDescriptor, contact, (0, proto_rpc_1.toProtoRpcClient)(new DhtRpc_client_1.DhtNodeRpcClient(this.rpcCommunicator.getRpcClientTransport())), this.config.serviceId, this.config.rpcRequestTimeout);
|
|
397
|
-
if (!this.bucket.get(contact.kademliaId) && !this.neighborList.getContact((0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(contact))) {
|
|
398
|
-
this.neighborList.addContact(rpcRemote);
|
|
399
|
-
if (setActive) {
|
|
400
|
-
const peerId = (0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(contact);
|
|
401
|
-
this.neighborList.setActive(peerId);
|
|
402
|
-
}
|
|
403
|
-
this.bucket.add(rpcRemote);
|
|
404
|
-
}
|
|
405
|
-
else {
|
|
406
|
-
this.randomPeers.addContact(rpcRemote);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
314
|
+
return (0, identifiers_1.getNodeIdFromPeerDescriptor)(this.localPeerDescriptor);
|
|
409
315
|
}
|
|
410
|
-
|
|
411
|
-
this.
|
|
412
|
-
this.entryPointDisconnectTimeout = setTimeout(() => {
|
|
413
|
-
this.connectionManager.unlockConnection(entryPoint, 'temporary-layer0-connection');
|
|
414
|
-
}, 10 * 1000);
|
|
316
|
+
getNeighborCount() {
|
|
317
|
+
return this.peerManager.getNeighborCount();
|
|
415
318
|
}
|
|
416
|
-
removeContact(
|
|
417
|
-
if (!this.started
|
|
319
|
+
removeContact(nodeId) {
|
|
320
|
+
if (!this.started) { // the stopped state is checked in PeerManager
|
|
418
321
|
return;
|
|
419
322
|
}
|
|
420
|
-
|
|
421
|
-
const peerId = (0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(contact);
|
|
422
|
-
this.bucket.remove(peerId.value);
|
|
423
|
-
this.neighborList.removeContact(peerId);
|
|
424
|
-
this.randomPeers.removeContact(peerId);
|
|
323
|
+
this.peerManager.removeContact(nodeId);
|
|
425
324
|
}
|
|
426
325
|
async send(msg) {
|
|
427
|
-
if (!this.started || this.
|
|
326
|
+
if (!this.started || this.abortController.signal.aborted) {
|
|
428
327
|
return;
|
|
429
328
|
}
|
|
430
|
-
const reachableThrough = this.peerDiscovery.isJoinOngoing() ? this.
|
|
431
|
-
|
|
329
|
+
const reachableThrough = this.peerDiscovery.isJoinOngoing() ? this.getConnectedEntryPoints() : [];
|
|
330
|
+
this.router.send(msg, reachableThrough);
|
|
331
|
+
}
|
|
332
|
+
getConnectedEntryPoints() {
|
|
333
|
+
return this.config.entryPoints !== undefined ? this.config.entryPoints.filter((entryPoint) => this.peerManager.connections.has((0, identifiers_1.getNodeIdFromPeerDescriptor)(entryPoint))) : [];
|
|
432
334
|
}
|
|
433
|
-
async joinDht(entryPointDescriptors,
|
|
335
|
+
async joinDht(entryPointDescriptors, doAdditionalDistantPeerDiscovery, retry) {
|
|
434
336
|
if (!this.started) {
|
|
435
337
|
throw new Error('Cannot join DHT before calling start() on DhtNode');
|
|
436
338
|
}
|
|
437
|
-
await
|
|
339
|
+
await this.peerDiscovery.joinDht(entryPointDescriptors, doAdditionalDistantPeerDiscovery, retry);
|
|
438
340
|
}
|
|
439
|
-
async
|
|
440
|
-
|
|
341
|
+
async joinRing() {
|
|
342
|
+
if (!this.started) {
|
|
343
|
+
throw new Error('Cannot join ring before calling start() on DhtNode');
|
|
344
|
+
}
|
|
345
|
+
await this.peerDiscovery.joinRing();
|
|
441
346
|
}
|
|
442
|
-
async storeDataToDht(key, data) {
|
|
443
|
-
|
|
444
|
-
|
|
347
|
+
async storeDataToDht(key, data, creator) {
|
|
348
|
+
const connectedEntryPoints = this.getConnectedEntryPoints();
|
|
349
|
+
if (this.peerDiscovery.isJoinOngoing() && connectedEntryPoints.length > 0) {
|
|
350
|
+
return this.storeDataToDhtViaPeer(key, data, (0, lodash_1.sample)(connectedEntryPoints));
|
|
445
351
|
}
|
|
446
|
-
return this.
|
|
352
|
+
return this.storeManager.storeDataToDht(key, data, creator ?? this.getNodeId());
|
|
447
353
|
}
|
|
448
|
-
async
|
|
449
|
-
const rpcRemote = new ExternalApiRpcRemote_1.ExternalApiRpcRemote(this.localPeerDescriptor, peer, this.
|
|
354
|
+
async storeDataToDhtViaPeer(key, data, peer) {
|
|
355
|
+
const rpcRemote = new ExternalApiRpcRemote_1.ExternalApiRpcRemote(this.localPeerDescriptor, peer, this.rpcCommunicator, DhtRpc_client_1.ExternalApiRpcClient);
|
|
450
356
|
return await rpcRemote.storeData(key, data);
|
|
451
357
|
}
|
|
452
|
-
async
|
|
453
|
-
|
|
454
|
-
|
|
358
|
+
async fetchDataFromDht(key) {
|
|
359
|
+
const connectedEntryPoints = this.getConnectedEntryPoints();
|
|
360
|
+
if (this.peerDiscovery.isJoinOngoing() && connectedEntryPoints.length > 0) {
|
|
361
|
+
return this.fetchDataFromDhtViaPeer(key, (0, lodash_1.sample)(connectedEntryPoints));
|
|
455
362
|
}
|
|
456
|
-
const result = await this.
|
|
457
|
-
return result.dataEntries ?? [];
|
|
363
|
+
const result = await this.recursiveOperationManager.execute(key, DhtRpc_1.RecursiveOperation.FETCH_DATA);
|
|
364
|
+
return result.dataEntries ?? []; // TODO is this fallback needed?
|
|
365
|
+
}
|
|
366
|
+
async fetchDataFromDhtViaPeer(key, peer) {
|
|
367
|
+
const rpcRemote = new ExternalApiRpcRemote_1.ExternalApiRpcRemote(this.localPeerDescriptor, peer, this.rpcCommunicator, DhtRpc_client_1.ExternalApiRpcClient);
|
|
368
|
+
return await rpcRemote.externalFetchData(key);
|
|
458
369
|
}
|
|
459
|
-
async deleteDataFromDht(
|
|
460
|
-
if (!this.
|
|
461
|
-
|
|
370
|
+
async deleteDataFromDht(key, waitForCompletion) {
|
|
371
|
+
if (!this.abortController.signal.aborted) {
|
|
372
|
+
await this.recursiveOperationManager.execute(key, DhtRpc_1.RecursiveOperation.DELETE_DATA, undefined, waitForCompletion);
|
|
462
373
|
}
|
|
463
374
|
}
|
|
464
|
-
async
|
|
465
|
-
const
|
|
466
|
-
return
|
|
375
|
+
async findClosestNodesFromDht(key) {
|
|
376
|
+
const result = await this.recursiveOperationManager.execute(key, DhtRpc_1.RecursiveOperation.FIND_CLOSEST_NODES);
|
|
377
|
+
return result.closestNodes;
|
|
467
378
|
}
|
|
468
379
|
getTransport() {
|
|
469
380
|
return this.transport;
|
|
@@ -471,61 +382,62 @@ class DhtNode extends eventemitter3_1.EventEmitter {
|
|
|
471
382
|
getLocalPeerDescriptor() {
|
|
472
383
|
return this.localPeerDescriptor;
|
|
473
384
|
}
|
|
474
|
-
|
|
475
|
-
return Array.from(this.connections.values()).map((peer) => peer.getPeerDescriptor());
|
|
385
|
+
getConnections() {
|
|
386
|
+
return Array.from(this.peerManager.connections.values()).map((peer) => peer.getPeerDescriptor());
|
|
476
387
|
}
|
|
477
|
-
|
|
478
|
-
return this.
|
|
388
|
+
getNeighbors() {
|
|
389
|
+
return this.started ? this.peerManager.getNeighbors() : [];
|
|
479
390
|
}
|
|
480
|
-
|
|
481
|
-
return this.
|
|
391
|
+
getConnectionCount() {
|
|
392
|
+
return this.peerManager.getConnectionCount();
|
|
482
393
|
}
|
|
483
|
-
|
|
484
|
-
return this.connectionManager.
|
|
394
|
+
getLocalLockedConnectionCount() {
|
|
395
|
+
return this.connectionManager.getLocalLockedConnectionCount();
|
|
485
396
|
}
|
|
486
|
-
|
|
487
|
-
return this.connectionManager.
|
|
397
|
+
getRemoteLockedConnectionCount() {
|
|
398
|
+
return this.connectionManager.getRemoteLockedConnectionCount();
|
|
488
399
|
}
|
|
489
|
-
|
|
490
|
-
return this.connectionManager.
|
|
400
|
+
getWeakLockedConnectionCount() {
|
|
401
|
+
return this.connectionManager.getWeakLockedConnectionCount();
|
|
491
402
|
}
|
|
492
403
|
async waitForNetworkConnectivity() {
|
|
493
|
-
await (0, utils_1.waitForCondition)(() =>
|
|
404
|
+
await (0, utils_1.waitForCondition)(() => {
|
|
405
|
+
if (!this.peerManager) {
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
return (this.peerManager.getConnectionCount() > 0);
|
|
410
|
+
}
|
|
411
|
+
}, this.config.networkConnectivityTimeout, 100, this.abortController.signal);
|
|
494
412
|
}
|
|
495
413
|
hasJoined() {
|
|
496
414
|
return this.peerDiscovery.isJoinCalled();
|
|
497
415
|
}
|
|
498
416
|
async stop() {
|
|
499
|
-
if (this.
|
|
417
|
+
if (this.abortController.signal.aborted || !this.started) {
|
|
500
418
|
return;
|
|
501
419
|
}
|
|
502
420
|
logger.trace('stop()');
|
|
503
|
-
this.
|
|
504
|
-
|
|
505
|
-
clearTimeout(this.entryPointDisconnectTimeout);
|
|
506
|
-
}
|
|
507
|
-
this.bucket.toArray().forEach((rpcRemote) => {
|
|
508
|
-
rpcRemote.leaveNotice();
|
|
509
|
-
this.bucket.remove(rpcRemote.id);
|
|
510
|
-
});
|
|
511
|
-
this.bucket.removeAllListeners();
|
|
421
|
+
this.abortController.abort();
|
|
422
|
+
await this.storeManager.destroy();
|
|
512
423
|
this.localDataStore.clear();
|
|
513
|
-
this.
|
|
514
|
-
this.randomPeers.stop();
|
|
424
|
+
this.peerManager?.stop();
|
|
515
425
|
this.rpcCommunicator.stop();
|
|
516
426
|
this.router.stop();
|
|
517
|
-
this.
|
|
427
|
+
this.recursiveOperationManager.stop();
|
|
518
428
|
this.peerDiscovery.stop();
|
|
519
429
|
if (this.config.transport === undefined) {
|
|
520
|
-
// if the transport was not given in config, the instance was created in start() and
|
|
430
|
+
// if the transport was not given in config, the instance was created in start() and
|
|
521
431
|
// this component is responsible for stopping it
|
|
522
432
|
await this.transport.stop();
|
|
523
433
|
}
|
|
524
434
|
this.transport = undefined;
|
|
525
435
|
this.connectionManager = undefined;
|
|
526
|
-
this.connections.clear();
|
|
527
436
|
this.removeAllListeners();
|
|
528
437
|
}
|
|
438
|
+
createDhtNodeRpcRemote(peerDescriptor) {
|
|
439
|
+
return new DhtNodeRpcRemote_1.DhtNodeRpcRemote(this.localPeerDescriptor, peerDescriptor, this.config.serviceId, this.rpcCommunicator, this.config.rpcRequestTimeout);
|
|
440
|
+
}
|
|
529
441
|
}
|
|
530
442
|
exports.DhtNode = DhtNode;
|
|
531
443
|
//# sourceMappingURL=DhtNode.js.map
|