@streamr/dht 100.0.0-testnet-one.3 → 100.0.0-testnet-two.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/dist/src/connection/ConnectionLockHandler.d.ts +11 -11
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionLockRpcLocal.js +4 -4
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +3 -5
- package/dist/src/connection/ConnectionLockRpcRemote.js +4 -7
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +4 -6
- package/dist/src/connection/ConnectionManager.js +48 -46
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +2 -2
- package/dist/src/connection/ConnectorFacade.js +2 -1
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +3 -6
- package/dist/src/connection/ManagedConnection.js +6 -10
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/connectivityChecker.js +1 -0
- package/dist/src/connection/connectivityChecker.js.map +1 -1
- package/dist/src/connection/simulator/Simulator.d.ts +0 -3
- package/dist/src/connection/simulator/Simulator.js +3 -51
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.js +5 -5
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +2 -2
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +0 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js +7 -9
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +11 -12
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +2 -5
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +1 -4
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +1 -2
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +3 -4
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +2 -7
- package/dist/src/connection/websocket/WebsocketConnector.js +32 -38
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +3 -2
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +8 -10
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +3 -6
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +0 -3
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketServer.d.ts +1 -4
- package/dist/src/connection/websocket/WebsocketServer.js +11 -12
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +10 -13
- package/dist/src/dht/DhtNode.js +39 -52
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +0 -1
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +7 -7
- package/dist/src/dht/DhtNodeRpcRemote.js +8 -7
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +5 -4
- package/dist/src/dht/ExternalApiRpcLocal.js +3 -2
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +2 -2
- package/dist/src/dht/ExternalApiRpcRemote.js +2 -0
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +13 -14
- package/dist/src/dht/PeerManager.js +45 -60
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/Contact.d.ts +2 -2
- package/dist/src/dht/contact/Contact.js +2 -2
- package/dist/src/dht/contact/Contact.js.map +1 -1
- package/dist/src/dht/contact/ContactList.d.ts +7 -7
- package/dist/src/dht/contact/ContactList.js +3 -3
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +4 -4
- package/dist/src/dht/contact/RandomContactList.js +12 -11
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/RpcRemote.d.ts +7 -4
- package/dist/src/dht/contact/RpcRemote.js +5 -2
- package/dist/src/dht/contact/RpcRemote.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +13 -13
- package/dist/src/dht/contact/SortedContactList.js +33 -29
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +2 -4
- package/dist/src/dht/discovery/DiscoverySession.js +19 -20
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +2 -5
- package/dist/src/dht/discovery/PeerDiscovery.js +12 -20
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +36 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +165 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +14 -0
- package/dist/src/dht/{find/FindRpcLocal.js → recursive-operation/RecursiveOperationRpcLocal.js} +7 -7
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.d.ts +6 -0
- package/dist/src/dht/{routing/FindRpcRemote.js → recursive-operation/RecursiveOperationRpcRemote.js} +11 -11
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +42 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +182 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +12 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +17 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.d.ts +6 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js +21 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js.map +1 -0
- package/dist/src/dht/routing/Router.d.ts +4 -19
- package/dist/src/dht/routing/Router.js +41 -33
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.js +6 -5
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcRemote.d.ts +2 -2
- package/dist/src/dht/routing/RouterRpcRemote.js +7 -7
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +20 -16
- package/dist/src/dht/routing/RoutingSession.js +37 -45
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +11 -13
- package/dist/src/dht/store/LocalDataStore.js +36 -56
- package/dist/src/dht/store/LocalDataStore.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +33 -0
- package/dist/src/dht/store/StoreManager.js +186 -0
- package/dist/src/dht/store/StoreManager.js.map +1 -0
- package/dist/src/dht/store/StoreRpcLocal.d.ts +8 -40
- package/dist/src/dht/store/StoreRpcLocal.js +13 -229
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.d.ts +5 -5
- package/dist/src/dht/store/StoreRpcRemote.js +4 -5
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/exports.d.ts +2 -0
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/MapWithTtl.d.ts +14 -0
- package/dist/src/helpers/MapWithTtl.js +59 -0
- package/dist/src/helpers/MapWithTtl.js.map +1 -0
- package/dist/src/helpers/PeerID.d.ts +2 -0
- package/dist/src/helpers/PeerID.js +4 -0
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/nodeId.d.ts +5 -0
- package/dist/src/helpers/nodeId.js +18 -1
- package/dist/src/helpers/nodeId.js.map +1 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +2 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +2 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/helpers/protoClasses.js +2 -3
- package/dist/src/helpers/protoClasses.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +21 -22
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +18 -18
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +36 -60
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +34 -53
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +11 -12
- package/dist/src/rpc-protocol/DhtCallContext.d.ts +2 -1
- package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -1
- package/dist/src/rpc-protocol/DhtRpcOptions.d.ts +2 -1
- package/dist/src/transport/ITransport.d.ts +9 -1
- package/dist/src/transport/ITransport.js +5 -0
- package/dist/src/transport/ITransport.js.map +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.js +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -1
- package/dist/src/transport/RoutingRpcCommunicator.js +16 -11
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/jest.config.js +4 -1
- package/karma-setup.js +2 -0
- package/karma.config.js +11 -7
- package/package.json +6 -7
- package/protos/DhtRpc.proto +17 -23
- package/src/connection/ConnectionLockHandler.ts +15 -15
- package/src/connection/ConnectionLockRpcLocal.ts +8 -9
- package/src/connection/ConnectionLockRpcRemote.ts +7 -16
- package/src/connection/ConnectionManager.ts +58 -55
- package/src/connection/ConnectorFacade.ts +3 -0
- package/src/connection/ManagedConnection.ts +12 -30
- package/src/connection/connectivityChecker.ts +1 -0
- package/src/connection/simulator/Simulator.ts +5 -36
- package/src/connection/simulator/SimulatorConnector.ts +8 -8
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -3
- package/src/connection/webrtc/NodeWebrtcConnection.ts +3 -4
- package/src/connection/webrtc/WebrtcConnector.ts +11 -13
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +16 -16
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +3 -13
- package/src/connection/websocket/AutoCertifierClientFacade.ts +4 -6
- package/src/connection/websocket/ClientWebsocket.ts +1 -0
- package/src/connection/websocket/WebsocketConnector.ts +43 -56
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +11 -13
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +4 -15
- package/src/connection/websocket/WebsocketServer.ts +14 -17
- package/src/dht/DhtNode.ts +67 -69
- package/src/dht/DhtNodeRpcLocal.ts +0 -1
- package/src/dht/DhtNodeRpcRemote.ts +13 -13
- package/src/dht/ExternalApiRpcLocal.ts +16 -6
- package/src/dht/ExternalApiRpcRemote.ts +5 -3
- package/src/dht/PeerManager.ts +73 -90
- package/src/dht/contact/Contact.ts +5 -4
- package/src/dht/contact/ContactList.ts +9 -9
- package/src/dht/contact/RandomContactList.ts +14 -14
- package/src/dht/contact/RpcRemote.ts +10 -7
- package/src/dht/contact/SortedContactList.ts +48 -45
- package/src/dht/discovery/DiscoverySession.ts +25 -25
- package/src/dht/discovery/PeerDiscovery.ts +16 -37
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +240 -0
- package/src/dht/{find/FindRpcLocal.ts → recursive-operation/RecursiveOperationRpcLocal.ts} +9 -9
- package/src/dht/{routing/FindRpcRemote.ts → recursive-operation/RecursiveOperationRpcRemote.ts} +10 -10
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +224 -0
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +25 -0
- package/src/dht/{find/FindSessionRpcRemote.ts → recursive-operation/RecursiveOperationSessionRpcRemote.ts} +8 -8
- package/src/dht/routing/Router.ts +45 -64
- package/src/dht/routing/RouterRpcLocal.ts +7 -6
- package/src/dht/routing/RouterRpcRemote.ts +9 -10
- package/src/dht/routing/RoutingSession.ts +59 -68
- package/src/dht/store/LocalDataStore.ts +47 -66
- package/src/dht/store/StoreManager.ts +217 -0
- package/src/dht/store/StoreRpcLocal.ts +26 -302
- package/src/dht/store/StoreRpcRemote.ts +9 -11
- package/src/exports.ts +2 -0
- package/src/helpers/MapWithTtl.ts +71 -0
- package/src/helpers/PeerID.ts +5 -0
- package/src/helpers/nodeId.ts +20 -0
- package/src/helpers/peerIdFromPeerDescriptor.ts +5 -3
- package/src/helpers/protoClasses.ts +4 -6
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +31 -32
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +11 -12
- package/src/proto/packages/dht/protos/DhtRpc.ts +49 -80
- package/src/rpc-protocol/DhtCallContext.ts +2 -1
- package/src/rpc-protocol/DhtRpcOptions.ts +2 -1
- package/src/transport/ITransport.ts +10 -1
- package/src/transport/ListeningRpcCommunicator.ts +1 -1
- package/src/transport/RoutingRpcCommunicator.ts +18 -12
- package/test/benchmark/Find.test.ts +8 -21
- package/test/benchmark/KademliaCorrectness.test.ts +11 -20
- package/test/benchmark/SortedContactListBenchmark.test.ts +9 -9
- package/test/benchmark/kademlia-simulation/Contact.ts +9 -8
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +2 -2
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +17 -17
- package/test/end-to-end/Layer0-Layer1.test.ts +10 -14
- package/test/end-to-end/Layer0.test.ts +15 -18
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +7 -9
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +4 -10
- package/test/end-to-end/Layer0Webrtc.test.ts +5 -6
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +8 -16
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +6 -13
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +5 -6
- package/test/end-to-end/memory-leak.test.ts +6 -8
- package/test/integration/ConnectionLocking.test.ts +6 -13
- package/test/integration/ConnectionManager.test.ts +26 -43
- package/test/integration/DhtJoinPeerDiscovery.test.ts +5 -10
- package/test/integration/DhtNodeExternalAPI.test.ts +16 -22
- package/test/integration/DhtNodeRpcRemote.test.ts +13 -22
- package/test/integration/DhtRpc.test.ts +13 -18
- package/test/integration/Find.test.ts +10 -10
- package/test/integration/Layer1-scale.test.ts +17 -28
- package/test/integration/Mock-Layer1-Layer0.test.ts +25 -43
- package/test/integration/MultipleEntryPointJoining.test.ts +7 -7
- package/test/integration/ReplicateData.test.ts +28 -30
- package/test/integration/RouteMessage.test.ts +30 -27
- package/test/integration/RouterRpcRemote.test.ts +12 -18
- package/test/integration/RpcErrors.test.ts +8 -21
- package/test/integration/ScaleDownDht.test.ts +11 -10
- package/test/integration/SimultaneousConnections.test.ts +43 -67
- package/test/integration/Store.test.ts +23 -36
- package/test/integration/StoreAndDelete.test.ts +30 -50
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +16 -37
- package/test/integration/StoreRpcRemote.test.ts +20 -30
- package/test/integration/WebrtcConnectionManagement.test.ts +3 -13
- package/test/integration/WebrtcConnectorRpc.test.ts +3 -9
- package/test/integration/WebsocketConnectionManagement.test.ts +7 -10
- package/test/integration/WebsocketConnectorRpc.test.ts +8 -17
- package/test/unit/ConnectivityHelpers.test.ts +9 -17
- package/test/unit/DuplicateDetector.test.ts +7 -4
- package/test/unit/LocalDataStore.test.ts +66 -77
- package/test/unit/PeerManager.test.ts +31 -0
- package/test/unit/RandomContactList.test.ts +13 -8
- package/test/unit/{Finder.test.ts → RecursiveOperationManager.test.ts} +38 -43
- package/test/unit/RecursiveOperationSession.test.ts +68 -0
- package/test/unit/Router.test.ts +15 -22
- package/test/unit/RoutingSession.test.ts +23 -25
- package/test/unit/SortedContactList.test.ts +41 -26
- package/test/unit/StoreManager.test.ts +132 -0
- package/test/unit/WebsocketConnector.test.ts +27 -35
- package/test/unit/customMatchers.test.ts +16 -0
- package/test/utils/FakeTransport.ts +44 -0
- package/test/utils/customMatchers.ts +70 -0
- package/test/utils/mock/MockRpcCommunicator.ts +7 -0
- package/test/utils/mock/Router.ts +3 -2
- package/test/utils/mock/mockDataEntry.ts +36 -0
- package/test/utils/utils.ts +49 -87
- package/dist/src/dht/find/FindRpcLocal.d.ts +0 -14
- package/dist/src/dht/find/FindRpcLocal.js.map +0 -1
- package/dist/src/dht/find/FindSession.d.ts +0 -44
- package/dist/src/dht/find/FindSession.js +0 -150
- package/dist/src/dht/find/FindSession.js.map +0 -1
- package/dist/src/dht/find/FindSessionRpcLocal.d.ts +0 -12
- package/dist/src/dht/find/FindSessionRpcLocal.js +0 -17
- package/dist/src/dht/find/FindSessionRpcLocal.js.map +0 -1
- package/dist/src/dht/find/FindSessionRpcRemote.d.ts +0 -6
- package/dist/src/dht/find/FindSessionRpcRemote.js +0 -21
- package/dist/src/dht/find/FindSessionRpcRemote.js.map +0 -1
- package/dist/src/dht/find/Finder.d.ts +0 -49
- package/dist/src/dht/find/Finder.js +0 -197
- package/dist/src/dht/find/Finder.js.map +0 -1
- package/dist/src/dht/routing/FindRpcRemote.d.ts +0 -6
- package/dist/src/dht/routing/FindRpcRemote.js.map +0 -1
- package/src/dht/find/FindSession.ts +0 -183
- package/src/dht/find/FindSessionRpcLocal.ts +0 -25
- package/src/dht/find/Finder.ts +0 -295
|
@@ -1,115 +1,96 @@
|
|
|
1
1
|
import { PeerID, PeerIDKey } from '../../helpers/PeerID'
|
|
2
|
-
import { DataEntry
|
|
3
|
-
import {
|
|
2
|
+
import { DataEntry } from '../../proto/packages/dht/protos/DhtRpc'
|
|
3
|
+
import { MapWithTtl } from '../../helpers/MapWithTtl'
|
|
4
|
+
import { NodeID, getNodeIdFromBinary } from '../../helpers/nodeId'
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
const MAX_TTL = 300 * 1000
|
|
6
|
+
type Key = Uint8Array
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
if (ttl < MIN_TTL) {
|
|
10
|
-
return MIN_TTL
|
|
11
|
-
} else if (ttl > MAX_TTL) {
|
|
12
|
-
return MAX_TTL
|
|
13
|
-
} else {
|
|
14
|
-
return ttl
|
|
15
|
-
}
|
|
16
|
-
}
|
|
8
|
+
export class LocalDataStore {
|
|
17
9
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
10
|
+
private readonly maxTtl: number
|
|
11
|
+
|
|
12
|
+
constructor(maxTtl: number) {
|
|
13
|
+
this.maxTtl = maxTtl
|
|
14
|
+
}
|
|
22
15
|
|
|
23
|
-
export class LocalDataStore {
|
|
24
16
|
// A map into which each node can store one value per data key
|
|
25
17
|
// The first key is the key of the data, the second key is the
|
|
26
|
-
//
|
|
27
|
-
private store: Map<PeerIDKey,
|
|
18
|
+
// NodeID of the creator of the data
|
|
19
|
+
private store: Map<PeerIDKey, MapWithTtl<NodeID, DataEntry>> = new Map()
|
|
28
20
|
|
|
29
21
|
public storeEntry(dataEntry: DataEntry): boolean {
|
|
30
|
-
const publisherKey = PeerID.fromValue(dataEntry.creator!.nodeId).toKey()
|
|
31
22
|
const dataKey = PeerID.fromValue(dataEntry.key).toKey()
|
|
32
|
-
|
|
23
|
+
const creatorNodeId = getNodeIdFromBinary(dataEntry.creator)
|
|
33
24
|
if (!this.store.has(dataKey)) {
|
|
34
|
-
this.store.set(dataKey, new
|
|
25
|
+
this.store.set(dataKey, new MapWithTtl((e) => Math.min(e.ttl, this.maxTtl)))
|
|
35
26
|
}
|
|
36
|
-
|
|
37
|
-
if (this.store.get(dataKey)!.has(publisherKey)) {
|
|
27
|
+
if (this.store.get(dataKey)!.has(creatorNodeId)) {
|
|
38
28
|
const storedMillis = (dataEntry.createdAt!.seconds * 1000) + (dataEntry.createdAt!.nanos / 1000000)
|
|
39
|
-
const oldLocalEntry = this.store.get(dataKey)!.get(
|
|
40
|
-
const oldStoredMillis = (oldLocalEntry.
|
|
41
|
-
|
|
29
|
+
const oldLocalEntry = this.store.get(dataKey)!.get(creatorNodeId)!
|
|
30
|
+
const oldStoredMillis = (oldLocalEntry.createdAt!.seconds * 1000) + (oldLocalEntry.createdAt!.nanos / 1000000)
|
|
42
31
|
// do nothing if old entry is newer than the one being replicated
|
|
43
32
|
if (oldStoredMillis >= storedMillis) {
|
|
44
33
|
return false
|
|
45
|
-
} else {
|
|
46
|
-
clearTimeout(oldLocalEntry.ttlTimeout)
|
|
47
34
|
}
|
|
48
35
|
}
|
|
49
|
-
this.store.get(dataKey)!.set(
|
|
50
|
-
dataEntry,
|
|
51
|
-
ttlTimeout: setTimeout(() => {
|
|
52
|
-
this.deleteEntry(PeerID.fromValue(dataEntry.key), dataEntry.creator!)
|
|
53
|
-
}, createTtlValue(dataEntry.ttl))
|
|
54
|
-
})
|
|
36
|
+
this.store.get(dataKey)!.set(creatorNodeId, dataEntry)
|
|
55
37
|
return true
|
|
56
38
|
}
|
|
57
39
|
|
|
58
|
-
public markAsDeleted(
|
|
59
|
-
const dataKey = PeerID.fromValue(
|
|
40
|
+
public markAsDeleted(key: Key, creator: NodeID): boolean {
|
|
41
|
+
const dataKey = PeerID.fromValue(key).toKey()
|
|
60
42
|
const item = this.store.get(dataKey)
|
|
61
|
-
if ((item === undefined) || !item.has(creator
|
|
43
|
+
if ((item === undefined) || !item.has(creator)) {
|
|
62
44
|
return false
|
|
63
45
|
}
|
|
64
|
-
const storedEntry = item.get(creator
|
|
65
|
-
storedEntry!.
|
|
46
|
+
const storedEntry = item.get(creator)
|
|
47
|
+
storedEntry!.deleted = true
|
|
66
48
|
return true
|
|
67
49
|
}
|
|
68
50
|
|
|
69
|
-
public
|
|
70
|
-
|
|
51
|
+
public* values(): IterableIterator<DataEntry> {
|
|
52
|
+
for (const v of this.store.values()) {
|
|
53
|
+
yield* v.values()
|
|
54
|
+
}
|
|
71
55
|
}
|
|
72
56
|
|
|
73
|
-
public
|
|
74
|
-
const dataEntries = new Map<
|
|
75
|
-
|
|
76
|
-
|
|
57
|
+
public getEntries(key: Key): Map<NodeID, DataEntry> {
|
|
58
|
+
const dataEntries = new Map<NodeID, DataEntry>
|
|
59
|
+
const mapKey = PeerID.fromValue(key).toKey()
|
|
60
|
+
this.store.get(mapKey)?.forEach((value, key) => {
|
|
61
|
+
dataEntries.set(key, value)
|
|
77
62
|
})
|
|
78
63
|
return dataEntries
|
|
79
64
|
}
|
|
80
65
|
|
|
81
|
-
public setStale(key:
|
|
82
|
-
const
|
|
83
|
-
const storedEntry = this.store.get(
|
|
66
|
+
public setStale(key: Key, creator: NodeID, stale: boolean): void {
|
|
67
|
+
const mapKey = PeerID.fromValue(key).toKey()
|
|
68
|
+
const storedEntry = this.store.get(mapKey)?.get(creator)
|
|
84
69
|
if (storedEntry) {
|
|
85
|
-
storedEntry.
|
|
70
|
+
storedEntry.stale = stale
|
|
86
71
|
}
|
|
87
72
|
}
|
|
88
73
|
|
|
89
|
-
public setAllEntriesAsStale(key:
|
|
90
|
-
|
|
91
|
-
|
|
74
|
+
public setAllEntriesAsStale(key: Key): void {
|
|
75
|
+
const mapKey = PeerID.fromValue(key).toKey()
|
|
76
|
+
this.store.get(mapKey)?.forEach((value) => {
|
|
77
|
+
value.stale = true
|
|
92
78
|
})
|
|
93
79
|
}
|
|
94
80
|
|
|
95
|
-
public deleteEntry(key:
|
|
96
|
-
const
|
|
97
|
-
const storedEntry = this.store.get(
|
|
81
|
+
public deleteEntry(key: Key, creator: NodeID): void {
|
|
82
|
+
const mapKey = PeerID.fromValue(key).toKey()
|
|
83
|
+
const storedEntry = this.store.get(mapKey)?.get(creator)
|
|
98
84
|
if (storedEntry) {
|
|
99
|
-
|
|
100
|
-
this.store.get(
|
|
101
|
-
|
|
102
|
-
this.store.delete(key.toKey())
|
|
85
|
+
this.store.get(mapKey)?.delete(creator)
|
|
86
|
+
if (this.store.get(mapKey)?.size() === 0) {
|
|
87
|
+
this.store.delete(mapKey)
|
|
103
88
|
}
|
|
104
89
|
}
|
|
105
90
|
}
|
|
106
91
|
|
|
107
92
|
public clear(): void {
|
|
108
|
-
this.store.forEach((value) =>
|
|
109
|
-
value.forEach((value) => {
|
|
110
|
-
clearTimeout(value.ttlTimeout)
|
|
111
|
-
})
|
|
112
|
-
})
|
|
93
|
+
this.store.forEach((value) => value.clear())
|
|
113
94
|
this.store.clear()
|
|
114
95
|
}
|
|
115
96
|
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataEntry, ReplicateDataRequest, PeerDescriptor,
|
|
3
|
+
StoreDataRequest, StoreDataResponse, RecursiveOperation
|
|
4
|
+
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
5
|
+
import { Any } from '../../proto/google/protobuf/any'
|
|
6
|
+
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
7
|
+
import { RoutingRpcCommunicator } from '../../transport/RoutingRpcCommunicator'
|
|
8
|
+
import { RecursiveOperationManager } from '../recursive-operation/RecursiveOperationManager'
|
|
9
|
+
import { areEqualPeerDescriptors, getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
10
|
+
import { Logger, executeSafePromise, hexToBinary } from '@streamr/utils'
|
|
11
|
+
import { LocalDataStore } from './LocalDataStore'
|
|
12
|
+
import { StoreRpcRemote } from './StoreRpcRemote'
|
|
13
|
+
import { Timestamp } from '../../proto/google/protobuf/timestamp'
|
|
14
|
+
import { SortedContactList } from '../contact/SortedContactList'
|
|
15
|
+
import { Contact } from '../contact/Contact'
|
|
16
|
+
import { ServiceID } from '../../types/ServiceID'
|
|
17
|
+
import { findIndex } from 'lodash'
|
|
18
|
+
import { NodeID, areEqualNodeIds, getNodeIdFromBinary, getNodeIdFromDataKey } from '../../helpers/nodeId'
|
|
19
|
+
import { StoreRpcLocal } from './StoreRpcLocal'
|
|
20
|
+
import { getDistance } from '../PeerManager'
|
|
21
|
+
|
|
22
|
+
interface StoreManagerConfig {
|
|
23
|
+
rpcCommunicator: RoutingRpcCommunicator
|
|
24
|
+
recursiveOperationManager: RecursiveOperationManager
|
|
25
|
+
localPeerDescriptor: PeerDescriptor
|
|
26
|
+
localDataStore: LocalDataStore
|
|
27
|
+
serviceId: ServiceID
|
|
28
|
+
highestTtl: number
|
|
29
|
+
redundancyFactor: number
|
|
30
|
+
getClosestNeighborsTo: (id: Uint8Array, n?: number) => PeerDescriptor[]
|
|
31
|
+
createRpcRemote: (contact: PeerDescriptor) => StoreRpcRemote
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const logger = new Logger(module)
|
|
35
|
+
|
|
36
|
+
export class StoreManager {
|
|
37
|
+
|
|
38
|
+
private readonly config: StoreManagerConfig
|
|
39
|
+
|
|
40
|
+
constructor(config: StoreManagerConfig) {
|
|
41
|
+
this.config = config
|
|
42
|
+
this.registerLocalRpcMethods()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private registerLocalRpcMethods() {
|
|
46
|
+
const rpcLocal = new StoreRpcLocal({
|
|
47
|
+
localDataStore: this.config.localDataStore,
|
|
48
|
+
replicateDataToNeighbors: (incomingPeer: PeerDescriptor, dataEntry: DataEntry) => this.replicateDataToNeighbors(incomingPeer, dataEntry),
|
|
49
|
+
selfIsWithinRedundancyFactor: (key: Uint8Array): boolean => this.selfIsWithinRedundancyFactor(key)
|
|
50
|
+
})
|
|
51
|
+
this.config.rpcCommunicator.registerRpcMethod(StoreDataRequest, StoreDataResponse, 'storeData',
|
|
52
|
+
(request: StoreDataRequest) => rpcLocal.storeData(request))
|
|
53
|
+
this.config.rpcCommunicator.registerRpcNotification(ReplicateDataRequest, 'replicateData',
|
|
54
|
+
(request: ReplicateDataRequest, context: ServerCallContext) => rpcLocal.replicateData(request, context))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
onNewContact(peerDescriptor: PeerDescriptor): void {
|
|
58
|
+
for (const dataEntry of this.config.localDataStore.values()) {
|
|
59
|
+
this.replicateAndUpdateStaleState(dataEntry, peerDescriptor)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private replicateAndUpdateStaleState(dataEntry: DataEntry, newNode: PeerDescriptor): void {
|
|
64
|
+
const newNodeId = getNodeIdFromPeerDescriptor(newNode)
|
|
65
|
+
// TODO use config option or named constant?
|
|
66
|
+
const closestToData = this.config.getClosestNeighborsTo(dataEntry.key, 10)
|
|
67
|
+
const sortedList = new SortedContactList<Contact>({
|
|
68
|
+
referenceId: getNodeIdFromDataKey(dataEntry.key),
|
|
69
|
+
maxSize: 20, // TODO use config option or named constant?
|
|
70
|
+
allowToContainReferenceId: true,
|
|
71
|
+
emitEvents: false
|
|
72
|
+
})
|
|
73
|
+
sortedList.addContact(new Contact(this.config.localPeerDescriptor))
|
|
74
|
+
closestToData.forEach((neighbor) => {
|
|
75
|
+
if (!areEqualNodeIds(newNodeId, getNodeIdFromPeerDescriptor(neighbor))) {
|
|
76
|
+
sortedList.addContact(new Contact(neighbor))
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
const selfIsPrimaryStorer = areEqualNodeIds(
|
|
80
|
+
sortedList.getClosestContactId(),
|
|
81
|
+
getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
|
|
82
|
+
)
|
|
83
|
+
if (selfIsPrimaryStorer) {
|
|
84
|
+
sortedList.addContact(new Contact(newNode))
|
|
85
|
+
const sorted = sortedList.getContactIds()
|
|
86
|
+
// findIndex should never return -1 here because we just added the new node to the list
|
|
87
|
+
const index = findIndex(sorted, (nodeId) => areEqualNodeIds(nodeId, newNodeId))
|
|
88
|
+
// if new node is within the storageRedundancyFactor closest nodes to the data
|
|
89
|
+
// do replicate data to it
|
|
90
|
+
if (index < this.config.redundancyFactor) {
|
|
91
|
+
setImmediate(async () => {
|
|
92
|
+
await this.replicateDataToContact(dataEntry, newNode)
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
} else if (!this.selfIsWithinRedundancyFactor(dataEntry.key)) {
|
|
96
|
+
this.config.localDataStore.setStale(dataEntry.key, getNodeIdFromBinary(dataEntry.creator), true)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private async replicateDataToContact(dataEntry: DataEntry, contact: PeerDescriptor): Promise<void> {
|
|
101
|
+
const rpcRemote = this.config.createRpcRemote(contact)
|
|
102
|
+
try {
|
|
103
|
+
await rpcRemote.replicateData({ entry: dataEntry })
|
|
104
|
+
} catch (e) {
|
|
105
|
+
logger.trace('replicateData() threw an exception ' + e)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public async storeDataToDht(key: Uint8Array, data: Any, creator: NodeID): Promise<PeerDescriptor[]> {
|
|
110
|
+
logger.debug(`Storing data to DHT ${this.config.serviceId}`)
|
|
111
|
+
const result = await this.config.recursiveOperationManager.execute(key, RecursiveOperation.FIND_NODE)
|
|
112
|
+
const closestNodes = result.closestNodes
|
|
113
|
+
const successfulNodes: PeerDescriptor[] = []
|
|
114
|
+
const ttl = this.config.highestTtl // ToDo: make TTL decrease according to some nice curve
|
|
115
|
+
const createdAt = Timestamp.now()
|
|
116
|
+
for (let i = 0; i < closestNodes.length && successfulNodes.length < this.config.redundancyFactor; i++) {
|
|
117
|
+
if (areEqualPeerDescriptors(this.config.localPeerDescriptor, closestNodes[i])) {
|
|
118
|
+
this.config.localDataStore.storeEntry({
|
|
119
|
+
key,
|
|
120
|
+
data,
|
|
121
|
+
creator: hexToBinary(creator),
|
|
122
|
+
createdAt,
|
|
123
|
+
storedAt: Timestamp.now(),
|
|
124
|
+
ttl,
|
|
125
|
+
stale: false,
|
|
126
|
+
deleted: false,
|
|
127
|
+
})
|
|
128
|
+
successfulNodes.push(closestNodes[i])
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
const rpcRemote = this.config.createRpcRemote(closestNodes[i])
|
|
132
|
+
try {
|
|
133
|
+
await rpcRemote.storeData({
|
|
134
|
+
key,
|
|
135
|
+
data,
|
|
136
|
+
creator: hexToBinary(creator),
|
|
137
|
+
createdAt,
|
|
138
|
+
ttl
|
|
139
|
+
})
|
|
140
|
+
successfulNodes.push(closestNodes[i])
|
|
141
|
+
logger.trace('remote.storeData() success')
|
|
142
|
+
} catch (e) {
|
|
143
|
+
logger.trace('remote.storeData() threw an exception ' + e)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return successfulNodes
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private selfIsWithinRedundancyFactor(dataKey: Uint8Array): boolean {
|
|
150
|
+
const closestNeighbors = this.config.getClosestNeighborsTo(dataKey, this.config.redundancyFactor)
|
|
151
|
+
if (closestNeighbors.length < this.config.redundancyFactor) {
|
|
152
|
+
return true
|
|
153
|
+
} else {
|
|
154
|
+
const localNodeId = getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
|
|
155
|
+
const furthestCloseNeighbor = getNodeIdFromPeerDescriptor(closestNeighbors[closestNeighbors.length - 1])
|
|
156
|
+
const dataId = getNodeIdFromDataKey(dataKey)
|
|
157
|
+
return getDistance(dataId, localNodeId) < getDistance(dataId, furthestCloseNeighbor)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private async replicateDataToClosestNodes(): Promise<void> {
|
|
162
|
+
const dataEntries = Array.from(this.config.localDataStore.values())
|
|
163
|
+
await Promise.all(dataEntries.map(async (dataEntry) => {
|
|
164
|
+
const neighbors = this.config.getClosestNeighborsTo(dataEntry.key, this.config.redundancyFactor)
|
|
165
|
+
await Promise.all(neighbors.map(async (neighbor) => {
|
|
166
|
+
const rpcRemote = this.config.createRpcRemote(neighbor)
|
|
167
|
+
try {
|
|
168
|
+
await rpcRemote.replicateData({ entry: dataEntry })
|
|
169
|
+
} catch (err) {
|
|
170
|
+
logger.trace('Failed to replicate data in replicateDataToClosestNodes', { error: err })
|
|
171
|
+
}
|
|
172
|
+
}))
|
|
173
|
+
}))
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private replicateDataToNeighbors(incomingPeer: PeerDescriptor, dataEntry: DataEntry): void {
|
|
177
|
+
// sort own contact list according to data id
|
|
178
|
+
const localNodeId = getNodeIdFromPeerDescriptor(this.config.localPeerDescriptor)
|
|
179
|
+
const incomingNodeId = getNodeIdFromPeerDescriptor(incomingPeer)
|
|
180
|
+
// TODO use config option or named constant?
|
|
181
|
+
const closestToData = this.config.getClosestNeighborsTo(dataEntry.key, 10)
|
|
182
|
+
const sortedList = new SortedContactList<Contact>({
|
|
183
|
+
referenceId: getNodeIdFromDataKey(dataEntry.key),
|
|
184
|
+
maxSize: this.config.redundancyFactor,
|
|
185
|
+
allowToContainReferenceId: true,
|
|
186
|
+
emitEvents: false
|
|
187
|
+
})
|
|
188
|
+
sortedList.addContact(new Contact(this.config.localPeerDescriptor))
|
|
189
|
+
closestToData.forEach((neighbor) => {
|
|
190
|
+
sortedList.addContact(new Contact(neighbor))
|
|
191
|
+
})
|
|
192
|
+
const selfIsPrimaryStorer = areEqualNodeIds(sortedList.getClosestContactId(), localNodeId)
|
|
193
|
+
const targets = selfIsPrimaryStorer
|
|
194
|
+
// if we are the closest to the data, replicate to all storageRedundancyFactor nearest
|
|
195
|
+
? sortedList.getAllContacts()
|
|
196
|
+
// if we are not the closest node to the data, replicate only to the closest one to the data
|
|
197
|
+
: [sortedList.getAllContacts()[0]]
|
|
198
|
+
targets.forEach((contact) => {
|
|
199
|
+
const contactNodeId = getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())
|
|
200
|
+
if (!areEqualNodeIds(incomingNodeId, contactNodeId) && !areEqualNodeIds(localNodeId, contactNodeId)) {
|
|
201
|
+
setImmediate(() => {
|
|
202
|
+
executeSafePromise(async () => {
|
|
203
|
+
await this.replicateDataToContact(dataEntry, contact.getPeerDescriptor())
|
|
204
|
+
logger.trace('replicateDataToContact() returned', {
|
|
205
|
+
node: getNodeIdFromPeerDescriptor(contact.getPeerDescriptor()),
|
|
206
|
+
replicateOnlyToClosest: !selfIsPrimaryStorer
|
|
207
|
+
})
|
|
208
|
+
})
|
|
209
|
+
})
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async destroy(): Promise<void> {
|
|
215
|
+
await this.replicateDataToClosestNodes()
|
|
216
|
+
}
|
|
217
|
+
}
|