@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
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Logger
|
|
3
|
+
} from '@streamr/utils'
|
|
4
|
+
import KBucket from 'k-bucket'
|
|
5
|
+
import {
|
|
6
|
+
PeerDescriptor
|
|
7
|
+
} from '../proto/packages/dht/protos/DhtRpc'
|
|
8
|
+
import { DhtNodeRpcRemote } from './DhtNodeRpcRemote'
|
|
9
|
+
import { RandomContactList } from './contact/RandomContactList'
|
|
10
|
+
import { SortedContactList } from './contact/SortedContactList'
|
|
11
|
+
import { ConnectionManager } from '../connection/ConnectionManager'
|
|
12
|
+
import EventEmitter from 'eventemitter3'
|
|
13
|
+
import { DhtAddress, DhtAddressRaw, getNodeIdFromPeerDescriptor, getRawFromDhtAddress } from '../identifiers'
|
|
14
|
+
import { RingContactList, RingContacts } from './contact/RingContactList'
|
|
15
|
+
import { RingIdRaw, getRingIdRawFromPeerDescriptor } from './contact/ringIdentifiers'
|
|
16
|
+
import { LockID } from '../connection/ConnectionLockHandler'
|
|
17
|
+
|
|
18
|
+
const logger = new Logger(module)
|
|
19
|
+
|
|
20
|
+
interface PeerManagerConfig {
|
|
21
|
+
numberOfNodesPerKBucket: number
|
|
22
|
+
maxContactListSize: number
|
|
23
|
+
peerDiscoveryQueryBatchSize: number
|
|
24
|
+
localNodeId: DhtAddress
|
|
25
|
+
localPeerDescriptor: PeerDescriptor
|
|
26
|
+
connectionManager: ConnectionManager
|
|
27
|
+
isLayer0: boolean
|
|
28
|
+
lockId: LockID
|
|
29
|
+
createDhtNodeRpcRemote: (peerDescriptor: PeerDescriptor) => DhtNodeRpcRemote
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface PeerManagerEvents {
|
|
33
|
+
contactAdded: (peerDescriptor: PeerDescriptor, closestPeers: PeerDescriptor[]) => void
|
|
34
|
+
contactRemoved: (peerDescriptor: PeerDescriptor, closestPeers: PeerDescriptor[]) => void
|
|
35
|
+
randomContactAdded: (peerDescriptor: PeerDescriptor, closestPeers: PeerDescriptor[]) => void
|
|
36
|
+
randomContactRemoved: (peerDescriptor: PeerDescriptor, closestPeers: PeerDescriptor[]) => void
|
|
37
|
+
ringContactAdded: (peerDescriptor: PeerDescriptor, closestPeers: RingContacts) => void
|
|
38
|
+
ringContactRemoved: (peerDescriptor: PeerDescriptor, closestPeers: RingContacts) => void
|
|
39
|
+
kBucketEmpty: () => void
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const getDistance = (nodeIdOrDataKeyRaw1: DhtAddressRaw, nodeIdOrDataKeyRaw2: DhtAddressRaw): number => {
|
|
43
|
+
return KBucket.distance(nodeIdOrDataKeyRaw1, nodeIdOrDataKeyRaw2)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class PeerManager extends EventEmitter<PeerManagerEvents> {
|
|
47
|
+
|
|
48
|
+
// Glossary:
|
|
49
|
+
// * 'neighbors' are the nodes that are our neighbors according to
|
|
50
|
+
// the protocol of the layer we are in
|
|
51
|
+
// * 'connections' are the nodes that are connected to this node on Layer0
|
|
52
|
+
// * 'contacts' are all non-unresponsive nodes that we know about
|
|
53
|
+
|
|
54
|
+
// The kademlia k-bucket
|
|
55
|
+
private bucket: KBucket<DhtNodeRpcRemote>
|
|
56
|
+
// Nodes that are connected to this node on Layer0
|
|
57
|
+
public readonly connections: Map<DhtAddress, DhtNodeRpcRemote> = new Map()
|
|
58
|
+
// All nodes that we know about
|
|
59
|
+
private contacts: SortedContactList<DhtNodeRpcRemote>
|
|
60
|
+
private randomPeers: RandomContactList<DhtNodeRpcRemote>
|
|
61
|
+
private ringContacts: RingContactList<DhtNodeRpcRemote>
|
|
62
|
+
private stopped: boolean = false
|
|
63
|
+
private readonly config: PeerManagerConfig
|
|
64
|
+
|
|
65
|
+
constructor(config: PeerManagerConfig) {
|
|
66
|
+
super()
|
|
67
|
+
this.config = config
|
|
68
|
+
this.bucket = new KBucket<DhtNodeRpcRemote>({
|
|
69
|
+
localNodeId: getRawFromDhtAddress(this.config.localNodeId),
|
|
70
|
+
numberOfNodesPerKBucket: this.config.numberOfNodesPerKBucket,
|
|
71
|
+
numberOfNodesToPing: this.config.numberOfNodesPerKBucket
|
|
72
|
+
})
|
|
73
|
+
this.ringContacts = new RingContactList<DhtNodeRpcRemote>(getRingIdRawFromPeerDescriptor(this.config.localPeerDescriptor), true)
|
|
74
|
+
this.ringContacts.on('ringContactAdded', (peerDescriptor: PeerDescriptor, closestPeers: RingContacts) => {
|
|
75
|
+
this.emit('ringContactAdded', peerDescriptor, closestPeers)
|
|
76
|
+
})
|
|
77
|
+
this.ringContacts.on('ringContactRemoved', (peerDescriptor: PeerDescriptor, closestPeers: RingContacts) => {
|
|
78
|
+
this.emit('ringContactRemoved', peerDescriptor, closestPeers)
|
|
79
|
+
})
|
|
80
|
+
this.bucket.on('ping', (oldContacts: DhtNodeRpcRemote[], newContact: DhtNodeRpcRemote) => this.onKBucketPing(oldContacts, newContact))
|
|
81
|
+
this.bucket.on('removed', (contact: DhtNodeRpcRemote) => this.onKBucketRemoved(getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())))
|
|
82
|
+
this.bucket.on('added', (contact: DhtNodeRpcRemote) => this.onKBucketAdded(contact))
|
|
83
|
+
this.bucket.on('updated', () => {
|
|
84
|
+
// TODO: Update contact info to the connection manager and reconnect
|
|
85
|
+
})
|
|
86
|
+
this.contacts = new SortedContactList({
|
|
87
|
+
referenceId: this.config.localNodeId,
|
|
88
|
+
maxSize: this.config.maxContactListSize,
|
|
89
|
+
allowToContainReferenceId: false,
|
|
90
|
+
emitEvents: true
|
|
91
|
+
})
|
|
92
|
+
this.contacts.on('contactRemoved', (removedContact: DhtNodeRpcRemote, activeContacts: DhtNodeRpcRemote[]) => {
|
|
93
|
+
if (this.stopped) {
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
this.emit('contactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
97
|
+
this.randomPeers.addContact(this.config.createDhtNodeRpcRemote(removedContact.getPeerDescriptor()))
|
|
98
|
+
})
|
|
99
|
+
this.contacts.on('contactAdded', (contactAdded: DhtNodeRpcRemote, activeContacts: DhtNodeRpcRemote[]) =>
|
|
100
|
+
this.emit('contactAdded', contactAdded.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
101
|
+
)
|
|
102
|
+
this.randomPeers = new RandomContactList(this.config.localNodeId, this.config.maxContactListSize)
|
|
103
|
+
this.randomPeers.on('contactRemoved', (removedContact: DhtNodeRpcRemote, activeContacts: DhtNodeRpcRemote[]) =>
|
|
104
|
+
this.emit('randomContactRemoved', removedContact.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
105
|
+
)
|
|
106
|
+
this.randomPeers.on('contactAdded', (contactAdded: DhtNodeRpcRemote, activeContacts: DhtNodeRpcRemote[]) =>
|
|
107
|
+
this.emit('randomContactAdded', contactAdded.getPeerDescriptor(), activeContacts.map((c) => c.getPeerDescriptor()))
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private onKBucketPing(oldContacts: DhtNodeRpcRemote[], newContact: DhtNodeRpcRemote): void {
|
|
112
|
+
if (this.stopped) {
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
const sortingList: SortedContactList<DhtNodeRpcRemote> = new SortedContactList({
|
|
116
|
+
referenceId: this.config.localNodeId,
|
|
117
|
+
maxSize: 100, // TODO use config option or named constant?
|
|
118
|
+
allowToContainReferenceId: false,
|
|
119
|
+
emitEvents: false
|
|
120
|
+
})
|
|
121
|
+
sortingList.addContacts(oldContacts)
|
|
122
|
+
const sortedContacts = sortingList.getAllContacts()
|
|
123
|
+
const removableNodeId = sortedContacts[sortedContacts.length - 1].getNodeId()
|
|
124
|
+
this.config.connectionManager?.weakUnlockConnection(removableNodeId, this.config.lockId)
|
|
125
|
+
this.bucket.remove(getRawFromDhtAddress(removableNodeId))
|
|
126
|
+
this.bucket.add(newContact)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private onKBucketRemoved(nodeId: DhtAddress): void {
|
|
130
|
+
if (this.stopped) {
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
this.config.connectionManager?.weakUnlockConnection(nodeId, this.config.lockId)
|
|
134
|
+
logger.trace(`Removed contact ${nodeId}`)
|
|
135
|
+
if (this.bucket.count() === 0) {
|
|
136
|
+
this.emit('kBucketEmpty')
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private onKBucketAdded(contact: DhtNodeRpcRemote): void {
|
|
141
|
+
if (this.stopped) {
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
if (contact.getNodeId() !== this.config.localNodeId) {
|
|
145
|
+
const peerDescriptor = contact.getPeerDescriptor()
|
|
146
|
+
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
147
|
+
// Important to lock here, before the ping result is known
|
|
148
|
+
this.config.connectionManager?.weakLockConnection(nodeId, this.config.lockId)
|
|
149
|
+
if (this.connections.has(contact.getNodeId())) {
|
|
150
|
+
logger.trace(`Added new contact ${nodeId}`)
|
|
151
|
+
} else { // open connection by pinging
|
|
152
|
+
logger.trace('starting ping ' + nodeId)
|
|
153
|
+
contact.ping().then((result) => {
|
|
154
|
+
if (result) {
|
|
155
|
+
logger.trace(`Added new contact ${nodeId}`)
|
|
156
|
+
} else {
|
|
157
|
+
logger.trace('ping failed ' + nodeId)
|
|
158
|
+
this.config.connectionManager?.weakUnlockConnection(nodeId, this.config.lockId)
|
|
159
|
+
this.removeContact(nodeId)
|
|
160
|
+
this.addClosestContactToBucket()
|
|
161
|
+
}
|
|
162
|
+
return
|
|
163
|
+
}).catch((_e) => {
|
|
164
|
+
this.config.connectionManager?.weakUnlockConnection(nodeId, this.config.lockId)
|
|
165
|
+
this.removeContact(nodeId)
|
|
166
|
+
this.addClosestContactToBucket()
|
|
167
|
+
})
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private addClosestContactToBucket(): void {
|
|
173
|
+
if (this.stopped) {
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
const closest = this.getClosestActiveContactNotInBucket()
|
|
177
|
+
if (closest) {
|
|
178
|
+
this.addContact([closest.getPeerDescriptor()])
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private getClosestActiveContactNotInBucket(): DhtNodeRpcRemote | undefined {
|
|
183
|
+
for (const contactId of this.contacts.getContactIds()) {
|
|
184
|
+
if (!this.bucket.get(getRawFromDhtAddress(contactId)) && this.contacts.isActive(contactId)) {
|
|
185
|
+
return this.contacts.getContact(contactId)!.contact
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return undefined
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
onContactConnected(peerDescriptor: PeerDescriptor): void {
|
|
192
|
+
const nodeId = getNodeIdFromPeerDescriptor(peerDescriptor)
|
|
193
|
+
if (nodeId === this.config.localNodeId) {
|
|
194
|
+
logger.error('handleConnected() to self')
|
|
195
|
+
}
|
|
196
|
+
const rpcRemote = this.config.createDhtNodeRpcRemote(peerDescriptor)
|
|
197
|
+
if (!this.connections.has(nodeId)) {
|
|
198
|
+
this.connections.set(nodeId, rpcRemote)
|
|
199
|
+
logger.trace('connectionschange add ' + this.connections.size)
|
|
200
|
+
} else {
|
|
201
|
+
logger.trace('new connection not set to connections, there is already a connection with the peer ID')
|
|
202
|
+
}
|
|
203
|
+
logger.trace('connected: ' + nodeId + ' ' + this.connections.size)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
onContactDisconnected(nodeId: DhtAddress, gracefulLeave: boolean): void {
|
|
207
|
+
logger.trace('disconnected: ' + nodeId)
|
|
208
|
+
this.connections.delete(nodeId)
|
|
209
|
+
if (this.config.isLayer0) {
|
|
210
|
+
this.bucket.remove(getRawFromDhtAddress(nodeId))
|
|
211
|
+
if (gracefulLeave === true) {
|
|
212
|
+
logger.trace(nodeId + ' ' + 'onTransportDisconnected with gracefulLeave ' + gracefulLeave)
|
|
213
|
+
this.removeContact(nodeId)
|
|
214
|
+
} else {
|
|
215
|
+
logger.trace(nodeId + ' ' + 'onTransportDisconnected with gracefulLeave ' + gracefulLeave)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
removeContact(nodeId: DhtAddress): void {
|
|
221
|
+
if (this.stopped) {
|
|
222
|
+
return
|
|
223
|
+
}
|
|
224
|
+
logger.trace(`Removing contact ${nodeId}`)
|
|
225
|
+
this.ringContacts.removeContact(this.contacts.getContact(nodeId)?.contact)
|
|
226
|
+
this.bucket.remove(getRawFromDhtAddress(nodeId))
|
|
227
|
+
this.contacts.removeContact(nodeId)
|
|
228
|
+
this.randomPeers.removeContact(nodeId)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
stop(): void {
|
|
232
|
+
this.stopped = true
|
|
233
|
+
this.bucket.toArray().forEach((rpcRemote: DhtNodeRpcRemote) => {
|
|
234
|
+
rpcRemote.leaveNotice()
|
|
235
|
+
this.bucket.remove(rpcRemote.id)
|
|
236
|
+
})
|
|
237
|
+
this.bucket.removeAllListeners()
|
|
238
|
+
this.ringContacts.getAllContacts().forEach((rpcRemote) => {
|
|
239
|
+
rpcRemote.leaveNotice()
|
|
240
|
+
this.ringContacts.removeContact(rpcRemote)
|
|
241
|
+
})
|
|
242
|
+
this.contacts.stop()
|
|
243
|
+
this.randomPeers.stop()
|
|
244
|
+
this.connections.clear()
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
getClosestNeighborsTo(referenceId: DhtAddress, limit?: number, excludedNodeIds?: Set<DhtAddress>): DhtNodeRpcRemote[] {
|
|
248
|
+
const closest = new SortedContactList<DhtNodeRpcRemote>({
|
|
249
|
+
referenceId,
|
|
250
|
+
allowToContainReferenceId: true,
|
|
251
|
+
emitEvents: false,
|
|
252
|
+
excludedNodeIds
|
|
253
|
+
})
|
|
254
|
+
this.bucket.toArray().forEach((contact) => closest.addContact(contact))
|
|
255
|
+
return closest.getClosestContacts(limit)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// TODO reduce copy-paste?
|
|
259
|
+
getClosestContactsTo(referenceId: DhtAddress, limit?: number, excludedNodeIds?: Set<DhtAddress>): DhtNodeRpcRemote[] {
|
|
260
|
+
const closest = new SortedContactList<DhtNodeRpcRemote>({
|
|
261
|
+
referenceId,
|
|
262
|
+
allowToContainReferenceId: true,
|
|
263
|
+
emitEvents: false,
|
|
264
|
+
excludedNodeIds
|
|
265
|
+
})
|
|
266
|
+
this.contacts.getAllContacts().map((contact) => closest.addContact(contact))
|
|
267
|
+
// TODO should set the excludeSet and limit to SortedContactList constructor and remove these line
|
|
268
|
+
return closest.getClosestContacts(limit)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
getClosestRingContactsTo(
|
|
272
|
+
ringIdRaw: RingIdRaw,
|
|
273
|
+
limit?: number,
|
|
274
|
+
excludedIds?: Set<DhtAddress>
|
|
275
|
+
): { left: DhtNodeRpcRemote[], right: DhtNodeRpcRemote[] } {
|
|
276
|
+
const closest = new RingContactList<DhtNodeRpcRemote>(ringIdRaw, false, excludedIds)
|
|
277
|
+
this.contacts.getAllContacts().map((contact) => closest.addContact(contact))
|
|
278
|
+
this.ringContacts.getAllContacts().map((contact) => closest.addContact(contact))
|
|
279
|
+
return closest.getClosestContacts(limit ?? 8)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
getContactCount(excludedNodeIds?: Set<DhtAddress>): number {
|
|
283
|
+
return this.contacts.getAllContacts().filter((contact) => {
|
|
284
|
+
if (!excludedNodeIds) {
|
|
285
|
+
return true
|
|
286
|
+
} else {
|
|
287
|
+
return !excludedNodeIds.has(contact.getNodeId())
|
|
288
|
+
}
|
|
289
|
+
}).length
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
getConnectionCount(): number {
|
|
293
|
+
return this.connections.size
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
getNeighborCount(): number {
|
|
297
|
+
return this.bucket.count()
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
getNeighbors(): PeerDescriptor[] {
|
|
301
|
+
return this.bucket.toArray().map((rpcRemote: DhtNodeRpcRemote) => rpcRemote.getPeerDescriptor())
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
setContactActive(nodeId: DhtAddress): void {
|
|
305
|
+
this.contacts.setActive(nodeId)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
addContact(peerDescriptors: PeerDescriptor[], setActive?: boolean): void {
|
|
309
|
+
if (this.stopped) {
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
peerDescriptors.forEach((contact) => {
|
|
313
|
+
const nodeId = getNodeIdFromPeerDescriptor(contact)
|
|
314
|
+
if (nodeId !== this.config.localNodeId) {
|
|
315
|
+
logger.trace(`Adding new contact ${nodeId}`)
|
|
316
|
+
const remote = this.config.createDhtNodeRpcRemote(contact)
|
|
317
|
+
const isInBucket = (this.bucket.get(contact.nodeId) !== null)
|
|
318
|
+
const isInContacts = (this.contacts.getContact(nodeId) !== undefined)
|
|
319
|
+
const isInRingContacts = this.ringContacts.getContact(contact) !== undefined
|
|
320
|
+
|
|
321
|
+
if (isInBucket || isInContacts) {
|
|
322
|
+
this.randomPeers.addContact(remote)
|
|
323
|
+
}
|
|
324
|
+
if (!isInBucket) {
|
|
325
|
+
this.bucket.add(remote)
|
|
326
|
+
}
|
|
327
|
+
if (!isInContacts) {
|
|
328
|
+
this.contacts.addContact(remote)
|
|
329
|
+
}
|
|
330
|
+
if (setActive) {
|
|
331
|
+
this.contacts.setActive(nodeId)
|
|
332
|
+
}
|
|
333
|
+
if (!isInRingContacts) {
|
|
334
|
+
this.ringContacts.addContact(remote)
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
})
|
|
338
|
+
}
|
|
339
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { PeerID } from '../../helpers/PeerID'
|
|
2
1
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
3
|
-
import {
|
|
2
|
+
import { DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
4
3
|
|
|
5
4
|
export class Contact {
|
|
5
|
+
|
|
6
6
|
private peerDescriptor: PeerDescriptor
|
|
7
7
|
|
|
8
8
|
constructor(peerDescriptor: PeerDescriptor) {
|
|
@@ -13,7 +13,7 @@ export class Contact {
|
|
|
13
13
|
return this.peerDescriptor
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
public
|
|
17
|
-
return
|
|
16
|
+
public getNodeId(): DhtAddress {
|
|
17
|
+
return getNodeIdFromPeerDescriptor(this.peerDescriptor)
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
2
1
|
import EventEmitter from 'eventemitter3'
|
|
2
|
+
import { DhtAddress } from '../../identifiers'
|
|
3
3
|
|
|
4
4
|
export class ContactState<C> {
|
|
5
5
|
public contacted = false
|
|
@@ -13,30 +13,31 @@ export class ContactState<C> {
|
|
|
13
13
|
|
|
14
14
|
export interface Events<C> {
|
|
15
15
|
contactRemoved: (removedContact: C, closestContacts: C[]) => void
|
|
16
|
-
|
|
16
|
+
contactAdded: (contactAdded: C, closestContacts: C[]) => void
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export class ContactList<C extends {
|
|
19
|
+
export class ContactList<C extends { getNodeId: () => DhtAddress }> extends EventEmitter<Events<C>> {
|
|
20
20
|
|
|
21
|
-
protected contactsById: Map<
|
|
22
|
-
|
|
23
|
-
protected
|
|
21
|
+
protected contactsById: Map<DhtAddress, ContactState<C>> = new Map()
|
|
22
|
+
// TODO move this to SortedContactList
|
|
23
|
+
protected contactIds: DhtAddress[] = []
|
|
24
|
+
protected localNodeId: DhtAddress
|
|
24
25
|
protected maxSize: number
|
|
25
26
|
protected defaultContactQueryLimit
|
|
26
27
|
|
|
27
28
|
constructor(
|
|
28
|
-
|
|
29
|
+
localNodeId: DhtAddress,
|
|
29
30
|
maxSize: number,
|
|
30
31
|
defaultContactQueryLimit = 20
|
|
31
32
|
) {
|
|
32
33
|
super()
|
|
33
|
-
this.
|
|
34
|
+
this.localNodeId = localNodeId
|
|
34
35
|
this.maxSize = maxSize
|
|
35
36
|
this.defaultContactQueryLimit = defaultContactQueryLimit
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
public getContact(id:
|
|
39
|
-
return this.contactsById.get(id
|
|
39
|
+
public getContact(id: DhtAddress): ContactState<C> | undefined {
|
|
40
|
+
return this.contactsById.get(id)
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
public getSize(): number {
|
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DhtAddress } from '../../identifiers'
|
|
2
2
|
import { ContactList, ContactState } from './ContactList'
|
|
3
3
|
|
|
4
|
-
export class RandomContactList<C extends {
|
|
4
|
+
export class RandomContactList<C extends { getNodeId: () => DhtAddress }> extends ContactList<C> {
|
|
5
5
|
|
|
6
6
|
private randomness: number
|
|
7
7
|
|
|
8
8
|
constructor(
|
|
9
|
-
|
|
9
|
+
localNodeId: DhtAddress,
|
|
10
10
|
maxSize: number,
|
|
11
11
|
randomness = 0.20,
|
|
12
12
|
defaultContactQueryLimit?: number
|
|
13
13
|
) {
|
|
14
|
-
super(
|
|
14
|
+
super(localNodeId, maxSize, defaultContactQueryLimit)
|
|
15
15
|
this.randomness = randomness
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
addContact(contact: C): void {
|
|
19
|
-
if (this.
|
|
19
|
+
if (this.localNodeId === contact.getNodeId()) {
|
|
20
20
|
return
|
|
21
21
|
}
|
|
22
|
-
if (!this.contactsById.has(contact.
|
|
22
|
+
if (!this.contactsById.has(contact.getNodeId())) {
|
|
23
23
|
const roll = Math.random()
|
|
24
24
|
if (roll < this.randomness) {
|
|
25
25
|
if (this.getSize() === this.maxSize && this.getSize() > 0) {
|
|
26
26
|
const toRemove = this.contactIds[0]
|
|
27
27
|
this.removeContact(toRemove)
|
|
28
28
|
}
|
|
29
|
-
this.contactIds.push(contact.
|
|
30
|
-
this.contactsById.set(contact.
|
|
29
|
+
this.contactIds.push(contact.getNodeId())
|
|
30
|
+
this.contactsById.set(contact.getNodeId(), new ContactState(contact))
|
|
31
31
|
this.emit(
|
|
32
|
-
'
|
|
32
|
+
'contactAdded',
|
|
33
33
|
contact,
|
|
34
34
|
this.getContacts()
|
|
35
35
|
)
|
|
@@ -37,12 +37,12 @@ export class RandomContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
removeContact(id:
|
|
41
|
-
if (this.contactsById.has(id
|
|
42
|
-
const removed = this.contactsById.get(id
|
|
43
|
-
const index = this.contactIds.findIndex((
|
|
40
|
+
removeContact(id: DhtAddress): boolean {
|
|
41
|
+
if (this.contactsById.has(id)) {
|
|
42
|
+
const removed = this.contactsById.get(id)!.contact
|
|
43
|
+
const index = this.contactIds.findIndex((nodeId) => (nodeId === id))
|
|
44
44
|
this.contactIds.splice(index, 1)
|
|
45
|
-
this.contactsById.delete(id
|
|
45
|
+
this.contactsById.delete(id)
|
|
46
46
|
this.emit('contactRemoved', removed, this.getContacts())
|
|
47
47
|
return true
|
|
48
48
|
}
|
|
@@ -52,7 +52,7 @@ export class RandomContactList<C extends { getPeerId: () => PeerID }> extends Co
|
|
|
52
52
|
public getContacts(limit = this.defaultContactQueryLimit): C[] {
|
|
53
53
|
const ret: C[] = []
|
|
54
54
|
this.contactIds.forEach((contactId) => {
|
|
55
|
-
const contact = this.contactsById.get(contactId
|
|
55
|
+
const contact = this.contactsById.get(contactId)
|
|
56
56
|
if (contact) {
|
|
57
57
|
ret.push(contact.contact)
|
|
58
58
|
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
2
|
+
import { OrderedMap } from '@js-sdsl/ordered-map'
|
|
3
|
+
import { RingDistance, RingId, RingIdRaw, getLeftDistance, getRightDistance, getRingIdFromPeerDescriptor, getRingIdFromRaw } from './ringIdentifiers'
|
|
4
|
+
import { DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
5
|
+
import EventEmitter from 'eventemitter3'
|
|
6
|
+
|
|
7
|
+
export interface RingContacts {
|
|
8
|
+
left: PeerDescriptor[]
|
|
9
|
+
right: PeerDescriptor[]
|
|
10
|
+
}
|
|
11
|
+
export interface RingContactListEvents {
|
|
12
|
+
ringContactAdded: (peerDescriptor: PeerDescriptor, closestPeers: RingContacts) => void
|
|
13
|
+
ringContactRemoved: (peerDescriptor: PeerDescriptor, closestPeers: RingContacts) => void
|
|
14
|
+
}
|
|
15
|
+
export class RingContactList<C extends { getPeerDescriptor(): PeerDescriptor }> extends EventEmitter<RingContactListEvents> {
|
|
16
|
+
|
|
17
|
+
private readonly numNeighborsPerSide = 5
|
|
18
|
+
private readonly referenceId: RingId
|
|
19
|
+
private readonly excludedIds: Set<DhtAddress>
|
|
20
|
+
private readonly leftNeighbors: OrderedMap<RingDistance, C>
|
|
21
|
+
private readonly rightNeighbors: OrderedMap<RingDistance, C>
|
|
22
|
+
private readonly emitEvents: boolean
|
|
23
|
+
|
|
24
|
+
constructor(rawReferenceId: RingIdRaw, emitEvents: boolean, excludedIds?: Set<DhtAddress>) {
|
|
25
|
+
super()
|
|
26
|
+
this.referenceId = getRingIdFromRaw(rawReferenceId)
|
|
27
|
+
this.emitEvents = emitEvents
|
|
28
|
+
this.excludedIds = excludedIds ?? new Set()
|
|
29
|
+
this.leftNeighbors = new OrderedMap<RingDistance, C>()
|
|
30
|
+
this.rightNeighbors = new OrderedMap<RingDistance, C>()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
addContact(contact: C): void {
|
|
34
|
+
const id = getRingIdFromPeerDescriptor(contact.getPeerDescriptor())
|
|
35
|
+
if (id === this.referenceId || this.excludedIds.has(getNodeIdFromPeerDescriptor(contact.getPeerDescriptor()))) {
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
let elementAdded = false
|
|
39
|
+
let elementRemoved = false
|
|
40
|
+
|
|
41
|
+
const leftDistance = getLeftDistance(this.referenceId, id)
|
|
42
|
+
const lastLeftNeighbor = this.leftNeighbors.back()
|
|
43
|
+
if (lastLeftNeighbor === undefined || leftDistance < lastLeftNeighbor[0]) {
|
|
44
|
+
this.leftNeighbors.setElement(leftDistance, contact)
|
|
45
|
+
elementAdded = true
|
|
46
|
+
if (this.leftNeighbors.size() > this.numNeighborsPerSide) {
|
|
47
|
+
this.leftNeighbors.eraseElementByIterator(this.leftNeighbors.rBegin())
|
|
48
|
+
elementRemoved = true
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const rightDistance = getRightDistance(this.referenceId, id)
|
|
53
|
+
const lastRightNeighbor = this.rightNeighbors.back()
|
|
54
|
+
if (lastRightNeighbor === undefined || rightDistance < lastRightNeighbor[0]) {
|
|
55
|
+
this.rightNeighbors.setElement(rightDistance, contact)
|
|
56
|
+
elementAdded = true
|
|
57
|
+
if (this.rightNeighbors.size() > this.numNeighborsPerSide) {
|
|
58
|
+
this.rightNeighbors.eraseElementByIterator(this.rightNeighbors.rBegin())
|
|
59
|
+
elementRemoved = true
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (this.emitEvents && (elementAdded || elementRemoved)) {
|
|
64
|
+
const closestContacts = this.getClosestContacts()
|
|
65
|
+
const closestDescriptors = {
|
|
66
|
+
left: closestContacts.left.map((c) => c.getPeerDescriptor()),
|
|
67
|
+
right: closestContacts.right.map((c) => c.getPeerDescriptor())
|
|
68
|
+
}
|
|
69
|
+
if (elementAdded) {
|
|
70
|
+
this.emit('ringContactAdded', contact.getPeerDescriptor(), closestDescriptors)
|
|
71
|
+
}
|
|
72
|
+
if (elementRemoved) {
|
|
73
|
+
this.emit('ringContactRemoved', contact.getPeerDescriptor(), closestDescriptors)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
removeContact(contact?: C): void {
|
|
79
|
+
if (contact === undefined) {
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const id = getRingIdFromPeerDescriptor(contact.getPeerDescriptor())
|
|
84
|
+
const leftDistance = getLeftDistance(this.referenceId, id)
|
|
85
|
+
const rightDistance = getRightDistance(this.referenceId, id)
|
|
86
|
+
|
|
87
|
+
let elementRemoved = false
|
|
88
|
+
if (this.leftNeighbors.eraseElementByKey(leftDistance)) {
|
|
89
|
+
elementRemoved = true
|
|
90
|
+
}
|
|
91
|
+
if (this.rightNeighbors.eraseElementByKey(rightDistance)) {
|
|
92
|
+
elementRemoved = true
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (this.emitEvents && elementRemoved) {
|
|
96
|
+
const closestContacts = this.getClosestContacts()
|
|
97
|
+
const closestDescriptors = { left: closestContacts.left.map((c) => c.getPeerDescriptor()),
|
|
98
|
+
right: closestContacts.right.map((c) => c.getPeerDescriptor()) }
|
|
99
|
+
this.emit('ringContactRemoved', contact.getPeerDescriptor(), closestDescriptors)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
getContact(peerDescriptor: PeerDescriptor): C | undefined {
|
|
104
|
+
const id = getRingIdFromPeerDescriptor(peerDescriptor)
|
|
105
|
+
const leftDistance = getLeftDistance(this.referenceId, id)
|
|
106
|
+
const rightDistance = getRightDistance(this.referenceId, id)
|
|
107
|
+
if (this.leftNeighbors.getElementByKey(leftDistance)) {
|
|
108
|
+
return this.leftNeighbors.getElementByKey(leftDistance)
|
|
109
|
+
}
|
|
110
|
+
if (this.rightNeighbors.getElementByKey(rightDistance)) {
|
|
111
|
+
return this.rightNeighbors.getElementByKey(rightDistance)
|
|
112
|
+
}
|
|
113
|
+
return undefined
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
getClosestContacts(limitPerSide?: number): { left: C[], right: C[] } {
|
|
117
|
+
const leftContacts: C[] = []
|
|
118
|
+
const rightContacts: C[] = []
|
|
119
|
+
|
|
120
|
+
let leftCount = 0
|
|
121
|
+
for (const item of this.leftNeighbors) {
|
|
122
|
+
if (limitPerSide != undefined && leftCount >= limitPerSide) {
|
|
123
|
+
break
|
|
124
|
+
}
|
|
125
|
+
leftContacts.push(item[1])
|
|
126
|
+
leftCount++
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let rightCount = 0
|
|
130
|
+
for (const item of this.rightNeighbors) {
|
|
131
|
+
if (limitPerSide != undefined && rightCount >= limitPerSide) {
|
|
132
|
+
break
|
|
133
|
+
}
|
|
134
|
+
rightContacts.push(item[1])
|
|
135
|
+
rightCount++
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return { left: leftContacts, right: rightContacts }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
getAllContacts(): C[] {
|
|
142
|
+
const ret: C[] = []
|
|
143
|
+
for (const item of this.leftNeighbors) {
|
|
144
|
+
ret.push(item[1])
|
|
145
|
+
}
|
|
146
|
+
for (const item of this.rightNeighbors) {
|
|
147
|
+
ret.push(item[1])
|
|
148
|
+
}
|
|
149
|
+
return ret
|
|
150
|
+
}
|
|
151
|
+
}
|