@streamr/dht 0.0.1-tatum.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/.eslintignore +5 -0
- package/.eslintrc +3 -0
- package/README.md +38 -0
- package/dist/src/connection/Connection.d.ts +11 -0
- package/dist/src/connection/Connection.js +23 -0
- package/dist/src/connection/Connection.js.map +1 -0
- package/dist/src/connection/ConnectionLockHandler.d.ts +23 -0
- package/dist/src/connection/ConnectionLockHandler.js +94 -0
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -0
- package/dist/src/connection/ConnectionManager.d.ts +94 -0
- package/dist/src/connection/ConnectionManager.js +554 -0
- package/dist/src/connection/ConnectionManager.js.map +1 -0
- package/dist/src/connection/ConnectivityChecker.d.ts +17 -0
- package/dist/src/connection/ConnectivityChecker.js +187 -0
- package/dist/src/connection/ConnectivityChecker.js.map +1 -0
- package/dist/src/connection/Handshaker.d.ts +19 -0
- package/dist/src/connection/Handshaker.js +77 -0
- package/dist/src/connection/Handshaker.js.map +1 -0
- package/dist/src/connection/IConnection.d.ts +38 -0
- package/dist/src/connection/IConnection.js +19 -0
- package/dist/src/connection/IConnection.js.map +1 -0
- package/dist/src/connection/IConnectionSource.d.ts +4 -0
- package/dist/src/connection/IConnectionSource.js +3 -0
- package/dist/src/connection/IConnectionSource.js.map +1 -0
- package/dist/src/connection/ManagedConnection.d.ts +60 -0
- package/dist/src/connection/ManagedConnection.js +352 -0
- package/dist/src/connection/ManagedConnection.js.map +1 -0
- package/dist/src/connection/ManagedWebRtcConnection.d.ts +7 -0
- package/dist/src/connection/ManagedWebRtcConnection.js +20 -0
- package/dist/src/connection/ManagedWebRtcConnection.js.map +1 -0
- package/dist/src/connection/RemoteConnectionLocker.d.ts +14 -0
- package/dist/src/connection/RemoteConnectionLocker.js +93 -0
- package/dist/src/connection/RemoteConnectionLocker.js.map +1 -0
- package/dist/src/connection/Simulator/Simulator.d.ts +42 -0
- package/dist/src/connection/Simulator/Simulator.js +325 -0
- package/dist/src/connection/Simulator/Simulator.js.map +1 -0
- package/dist/src/connection/Simulator/SimulatorConnection.d.ts +19 -0
- package/dist/src/connection/Simulator/SimulatorConnection.js +118 -0
- package/dist/src/connection/Simulator/SimulatorConnection.js.map +1 -0
- package/dist/src/connection/Simulator/SimulatorConnector.d.ts +17 -0
- package/dist/src/connection/Simulator/SimulatorConnector.js +72 -0
- package/dist/src/connection/Simulator/SimulatorConnector.js.map +1 -0
- package/dist/src/connection/Simulator/SimulatorTransport.d.ts +6 -0
- package/dist/src/connection/Simulator/SimulatorTransport.js +11 -0
- package/dist/src/connection/Simulator/SimulatorTransport.js.map +1 -0
- package/dist/src/connection/Simulator/pings.d.ts +21 -0
- package/dist/src/connection/Simulator/pings.js +61 -0
- package/dist/src/connection/Simulator/pings.js.map +1 -0
- package/dist/src/connection/WebRTC/IWebRtcConnection.d.ts +20 -0
- package/dist/src/connection/WebRTC/IWebRtcConnection.js +9 -0
- package/dist/src/connection/WebRTC/IWebRtcConnection.js.map +1 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.d.ts +47 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js +233 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js.map +1 -0
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.d.ts +12 -0
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js +74 -0
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js.map +1 -0
- package/dist/src/connection/WebRTC/WebRtcConnector.d.ts +47 -0
- package/dist/src/connection/WebRTC/WebRtcConnector.js +227 -0
- package/dist/src/connection/WebRTC/WebRtcConnector.js.map +1 -0
- package/dist/src/connection/WebRTC/iceServerAsString.d.ts +2 -0
- package/dist/src/connection/WebRTC/iceServerAsString.js +18 -0
- package/dist/src/connection/WebRTC/iceServerAsString.js.map +1 -0
- package/dist/src/connection/WebSocket/ClientWebSocket.d.ts +15 -0
- package/dist/src/connection/WebSocket/ClientWebSocket.js +113 -0
- package/dist/src/connection/WebSocket/ClientWebSocket.js.map +1 -0
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.d.ts +9 -0
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js +63 -0
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js.map +1 -0
- package/dist/src/connection/WebSocket/ServerWebSocket.d.ts +18 -0
- package/dist/src/connection/WebSocket/ServerWebSocket.js +103 -0
- package/dist/src/connection/WebSocket/ServerWebSocket.js.map +1 -0
- package/dist/src/connection/WebSocket/WebSocketConnector.d.ts +31 -0
- package/dist/src/connection/WebSocket/WebSocketConnector.js +202 -0
- package/dist/src/connection/WebSocket/WebSocketConnector.js.map +1 -0
- package/dist/src/connection/WebSocket/WebSocketServer.d.ts +9 -0
- package/dist/src/connection/WebSocket/WebSocketServer.js +101 -0
- package/dist/src/connection/WebSocket/WebSocketServer.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +153 -0
- package/dist/src/dht/DhtNode.js +599 -0
- package/dist/src/dht/DhtNode.js.map +1 -0
- package/dist/src/dht/DhtPeer.d.ts +18 -0
- package/dist/src/dht/DhtPeer.js +74 -0
- package/dist/src/dht/DhtPeer.js.map +1 -0
- package/dist/src/dht/ExternalApi.d.ts +8 -0
- package/dist/src/dht/ExternalApi.js +26 -0
- package/dist/src/dht/ExternalApi.js.map +1 -0
- package/dist/src/dht/RemoteExternalApi.d.ts +6 -0
- package/dist/src/dht/RemoteExternalApi.js +26 -0
- package/dist/src/dht/RemoteExternalApi.js.map +1 -0
- package/dist/src/dht/contact/Contact.d.ts +22 -0
- package/dist/src/dht/contact/Contact.js +25 -0
- package/dist/src/dht/contact/Contact.js.map +1 -0
- package/dist/src/dht/contact/RandomContactList.d.ts +20 -0
- package/dist/src/dht/contact/RandomContactList.js +78 -0
- package/dist/src/dht/contact/RandomContactList.js.map +1 -0
- package/dist/src/dht/contact/Remote.d.ts +15 -0
- package/dist/src/dht/contact/Remote.js +24 -0
- package/dist/src/dht/contact/Remote.js.map +1 -0
- package/dist/src/dht/contact/SortedContactList.d.ts +35 -0
- package/dist/src/dht/contact/SortedContactList.js +156 -0
- package/dist/src/dht/contact/SortedContactList.js.map +1 -0
- package/dist/src/dht/discovery/DiscoverySession.d.ts +36 -0
- package/dist/src/dht/discovery/DiscoverySession.js +116 -0
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -0
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +42 -0
- package/dist/src/dht/discovery/PeerDiscovery.js +157 -0
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -0
- package/dist/src/dht/find/RecursiveFindSession.d.ts +46 -0
- package/dist/src/dht/find/RecursiveFindSession.js +142 -0
- package/dist/src/dht/find/RecursiveFindSession.js.map +1 -0
- package/dist/src/dht/find/RecursiveFinder.d.ts +54 -0
- package/dist/src/dht/find/RecursiveFinder.js +180 -0
- package/dist/src/dht/find/RecursiveFinder.js.map +1 -0
- package/dist/src/dht/find/RemoteRecursiveFindSession.d.ts +6 -0
- package/dist/src/dht/find/RemoteRecursiveFindSession.js +25 -0
- package/dist/src/dht/find/RemoteRecursiveFindSession.js.map +1 -0
- package/dist/src/dht/routing/DuplicateDetector.d.ts +13 -0
- package/dist/src/dht/routing/DuplicateDetector.js +41 -0
- package/dist/src/dht/routing/DuplicateDetector.js.map +1 -0
- package/dist/src/dht/routing/RemoteRouter.d.ts +8 -0
- package/dist/src/dht/routing/RemoteRouter.js +106 -0
- package/dist/src/dht/routing/RemoteRouter.js.map +1 -0
- package/dist/src/dht/routing/Router.d.ts +60 -0
- package/dist/src/dht/routing/Router.js +207 -0
- package/dist/src/dht/routing/Router.js.map +1 -0
- package/dist/src/dht/routing/RoutingSession.d.ts +42 -0
- package/dist/src/dht/routing/RoutingSession.js +178 -0
- package/dist/src/dht/routing/RoutingSession.js.map +1 -0
- package/dist/src/dht/store/DataStore.d.ts +45 -0
- package/dist/src/dht/store/DataStore.js +244 -0
- package/dist/src/dht/store/DataStore.js.map +1 -0
- package/dist/src/dht/store/LocalDataStore.d.ts +19 -0
- package/dist/src/dht/store/LocalDataStore.js +104 -0
- package/dist/src/dht/store/LocalDataStore.js.map +1 -0
- package/dist/src/dht/store/RemoteStore.d.ts +8 -0
- package/dist/src/dht/store/RemoteStore.js +44 -0
- package/dist/src/dht/store/RemoteStore.js.map +1 -0
- package/dist/src/exports.d.ts +19 -0
- package/dist/src/exports.js +41 -0
- package/dist/src/exports.js.map +1 -0
- package/dist/src/helpers/AddressTools.d.ts +2 -0
- package/dist/src/helpers/AddressTools.js +31 -0
- package/dist/src/helpers/AddressTools.js.map +1 -0
- package/dist/src/helpers/PeerID.d.ts +25 -0
- package/dist/src/helpers/PeerID.js +84 -0
- package/dist/src/helpers/PeerID.js.map +1 -0
- package/dist/src/helpers/UUID.d.ts +7 -0
- package/dist/src/helpers/UUID.js +32 -0
- package/dist/src/helpers/UUID.js.map +1 -0
- package/dist/src/helpers/debugHelpers.d.ts +3 -0
- package/dist/src/helpers/debugHelpers.js +11 -0
- package/dist/src/helpers/debugHelpers.js.map +1 -0
- package/dist/src/helpers/errors.d.ts +72 -0
- package/dist/src/helpers/errors.js +95 -0
- package/dist/src/helpers/errors.js.map +1 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +5 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +17 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -0
- package/dist/src/helpers/protoClasses.d.ts +2 -0
- package/dist/src/helpers/protoClasses.js +35 -0
- package/dist/src/helpers/protoClasses.js.map +1 -0
- package/dist/src/helpers/protoToString.d.ts +2 -0
- package/dist/src/helpers/protoToString.js +20 -0
- package/dist/src/helpers/protoToString.js.map +1 -0
- package/dist/src/proto/google/protobuf/any.d.ts +173 -0
- package/dist/src/proto/google/protobuf/any.js +155 -0
- package/dist/src/proto/google/protobuf/any.js.map +1 -0
- package/dist/src/proto/google/protobuf/empty.d.ts +32 -0
- package/dist/src/proto/google/protobuf/empty.js +34 -0
- package/dist/src/proto/google/protobuf/empty.js.map +1 -0
- package/dist/src/proto/google/protobuf/timestamp.d.ts +149 -0
- package/dist/src/proto/google/protobuf/timestamp.js +136 -0
- package/dist/src/proto/google/protobuf/timestamp.js.map +1 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +320 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +245 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +1089 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +710 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +145 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.js +3 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.js.map +1 -0
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.d.ts +87 -0
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +66 -0
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js.map +1 -0
- package/dist/src/proto/tests.d.ts +39 -0
- package/dist/src/proto/tests.js +34 -0
- package/dist/src/proto/tests.js.map +1 -0
- package/dist/src/rpc-protocol/DhtCallContext.d.ts +12 -0
- package/dist/src/rpc-protocol/DhtCallContext.js +8 -0
- package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -0
- package/dist/src/rpc-protocol/DhtRpcOptions.d.ts +8 -0
- package/dist/src/rpc-protocol/DhtRpcOptions.js +3 -0
- package/dist/src/rpc-protocol/DhtRpcOptions.js.map +1 -0
- package/dist/src/transport/ITransport.d.ts +22 -0
- package/dist/src/transport/ITransport.js +3 -0
- package/dist/src/transport/ITransport.js.map +1 -0
- package/dist/src/transport/ListeningRpcCommunicator.d.ts +6 -0
- package/dist/src/transport/ListeningRpcCommunicator.js +14 -0
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -0
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +8 -0
- package/dist/src/transport/RoutingRpcCommunicator.js +52 -0
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -0
- package/jest.config.js +2 -0
- package/karma.config.js +20 -0
- package/package.json +64 -0
- package/proto.sh +3 -0
- package/protos/DhtRpc.proto +330 -0
- package/protos/tests.proto +16 -0
- package/src/connection/Connection.ts +23 -0
- package/src/connection/ConnectionLockHandler.ts +105 -0
- package/src/connection/ConnectionManager.ts +676 -0
- package/src/connection/ConnectivityChecker.ts +173 -0
- package/src/connection/Handshaker.ts +92 -0
- package/src/connection/IConnection.ts +47 -0
- package/src/connection/IConnectionSource.ts +6 -0
- package/src/connection/ManagedConnection.ts +398 -0
- package/src/connection/ManagedWebRtcConnection.ts +27 -0
- package/src/connection/RemoteConnectionLocker.ts +88 -0
- package/src/connection/Simulator/Simulator.ts +399 -0
- package/src/connection/Simulator/SimulatorConnection.ts +137 -0
- package/src/connection/Simulator/SimulatorConnector.ts +104 -0
- package/src/connection/Simulator/SimulatorTransport.ts +9 -0
- package/src/connection/Simulator/pings.ts +42 -0
- package/src/connection/WebRTC/BrowserWebRtcConnection.ts +227 -0
- package/src/connection/WebRTC/IWebRtcConnection.ts +24 -0
- package/src/connection/WebRTC/NodeWebRtcConnection.ts +256 -0
- package/src/connection/WebRTC/RemoteWebrtcConnector.ts +93 -0
- package/src/connection/WebRTC/WebRtcConnector.ts +306 -0
- package/src/connection/WebRTC/iceServerAsString.ts +15 -0
- package/src/connection/WebSocket/ClientWebSocket.ts +118 -0
- package/src/connection/WebSocket/RemoteWebSocketConnector.ts +49 -0
- package/src/connection/WebSocket/ServerWebSocket.ts +119 -0
- package/src/connection/WebSocket/WebSocketConnector.ts +264 -0
- package/src/connection/WebSocket/WebSocketServer.ts +97 -0
- package/src/dht/DhtNode.ts +776 -0
- package/src/dht/DhtPeer.ts +96 -0
- package/src/dht/ExternalApi.ts +29 -0
- package/src/dht/RemoteExternalApi.ts +25 -0
- package/src/dht/contact/Contact.ts +36 -0
- package/src/dht/contact/RandomContactList.ts +92 -0
- package/src/dht/contact/Remote.ts +40 -0
- package/src/dht/contact/SortedContactList.ts +196 -0
- package/src/dht/discovery/DiscoverySession.ts +150 -0
- package/src/dht/discovery/PeerDiscovery.ts +162 -0
- package/src/dht/find/RecursiveFindSession.ts +178 -0
- package/src/dht/find/RecursiveFinder.ts +272 -0
- package/src/dht/find/RemoteRecursiveFindSession.ts +33 -0
- package/src/dht/routing/DuplicateDetector.ts +53 -0
- package/src/dht/routing/RemoteRouter.ts +115 -0
- package/src/dht/routing/Router.ts +266 -0
- package/src/dht/routing/RoutingSession.ts +222 -0
- package/src/dht/store/DataStore.ts +321 -0
- package/src/dht/store/LocalDataStore.ts +114 -0
- package/src/dht/store/RemoteStore.ts +58 -0
- package/src/exports.ts +19 -0
- package/src/helpers/AddressTools.ts +26 -0
- package/src/helpers/PeerID.ts +95 -0
- package/src/helpers/UUID.ts +30 -0
- package/src/helpers/debugHelpers.ts +9 -0
- package/src/helpers/errors.ts +49 -0
- package/src/helpers/peerIdFromPeerDescriptor.ts +14 -0
- package/src/helpers/protoClasses.ts +63 -0
- package/src/helpers/protoToString.ts +21 -0
- package/src/proto/google/protobuf/any.ts +319 -0
- package/src/proto/google/protobuf/empty.ts +84 -0
- package/src/proto/google/protobuf/timestamp.ts +281 -0
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +373 -0
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +148 -0
- package/src/proto/packages/dht/protos/DhtRpc.ts +1399 -0
- package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +108 -0
- package/src/proto/tests.ts +52 -0
- package/src/rpc-protocol/DhtCallContext.ts +15 -0
- package/src/rpc-protocol/DhtRpcOptions.ts +9 -0
- package/src/transport/ITransport.ts +31 -0
- package/src/transport/ListeningRpcCommunicator.ts +14 -0
- package/src/transport/RoutingRpcCommunicator.ts +59 -0
- package/src/types/glogal.d.ts +1 -0
- package/src/types/textencoding.d.ts +7 -0
- package/test/RandomGraphSimulation.ts +52 -0
- package/test/benchmark/KademliaCorrectness.test.ts +115 -0
- package/test/benchmark/RecursiveFind.test.ts +87 -0
- package/test/benchmark/any.test.ts +28 -0
- package/test/benchmark/kademlia-simulation/Contact.ts +32 -0
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +94 -0
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +125 -0
- package/test/data/generateGroundTruthData.ts +70 -0
- package/test/end-to-end/Layer0-Layer1.test.ts +87 -0
- package/test/end-to-end/Layer0.test.ts +60 -0
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +94 -0
- package/test/end-to-end/Layer0WebRTC-Layer1.test.ts +134 -0
- package/test/end-to-end/Layer0WebRTC.test.ts +98 -0
- package/test/end-to-end/Layer1-Scale-WebRTC.test.ts +69 -0
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +73 -0
- package/test/end-to-end/WebSocketConnectionRequest.test.ts +62 -0
- package/test/integration/ConnectionLocking.test.ts +166 -0
- package/test/integration/ConnectionManager.test.ts +291 -0
- package/test/integration/DhtNodeExternalAPI.test.ts +43 -0
- package/test/integration/DhtPeer.test.ts +73 -0
- package/test/integration/DhtRpc.test.ts +131 -0
- package/test/integration/DhtWithMockConnectionLatencies.test.ts +46 -0
- package/test/integration/DhtWithMockConnections.test.ts +46 -0
- package/test/integration/DhtWithRealConnectionLatencies.test.ts +47 -0
- package/test/integration/Layer1-scale.test.ts +200 -0
- package/test/integration/MigrateData.test.ts +203 -0
- package/test/integration/Mock-Layer1-Layer0.test.ts +106 -0
- package/test/integration/MultipleEntryPointJoining.test.ts +105 -0
- package/test/integration/RecursiveFind.test.ts +50 -0
- package/test/integration/RemoteRouter.test.ts +83 -0
- package/test/integration/RemoteStore.test.ts +66 -0
- package/test/integration/RouteMessage.test.ts +254 -0
- package/test/integration/RpcErrors.test.ts +153 -0
- package/test/integration/ScaleDownDht.test.ts +66 -0
- package/test/integration/SimultaneousConnections.test.ts +308 -0
- package/test/integration/Store.test.ts +72 -0
- package/test/integration/StoreAndDelete.test.ts +93 -0
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +71 -0
- package/test/integration/WebRtcConnectionManagement.test.ts +205 -0
- package/test/integration/WebRtcConnectorRpc.test.ts +145 -0
- package/test/integration/WebSocket.test.ts +64 -0
- package/test/integration/WebSocketConnectionManagement.test.ts +131 -0
- package/test/integration/WebSocketConnectorRpc.test.ts +86 -0
- package/test/kademlia-simulation/data/nodeids.json +13002 -0
- package/test/kademlia-simulation/data/orderedneighbors.json +1001 -0
- package/test/unit/AddressTools.test.ts +40 -0
- package/test/unit/DuplicateDetector.test.ts +29 -0
- package/test/unit/LocalDataStore.test.ts +107 -0
- package/test/unit/PeerID.test.ts +22 -0
- package/test/unit/ProtobufMessage.test.ts +21 -0
- package/test/unit/RandomContactList.test.ts +87 -0
- package/test/unit/RecursiveFinder.test.ts +112 -0
- package/test/unit/Router.test.ts +124 -0
- package/test/unit/SortedContactList.test.ts +127 -0
- package/test/unit/UUID.test.ts +49 -0
- package/test/unit/WebSocketServer.test.ts +42 -0
- package/test/utils/mock/RecursiveFinder.ts +19 -0
- package/test/utils/mock/Router.ts +53 -0
- package/test/utils/mock/Transport.ts +26 -0
- package/test/utils/utils.ts +311 -0
- package/tsconfig.browser.json +15 -0
- package/tsconfig.jest.json +19 -0
- package/tsconfig.json +3 -0
- package/tsconfig.node.json +18 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
|
|
3
|
+
import { SimulationNode } from './SimulationNode'
|
|
4
|
+
import fs from 'fs'
|
|
5
|
+
import { PeerID } from '../../../src/helpers/PeerID'
|
|
6
|
+
|
|
7
|
+
export class KademliaSimulation {
|
|
8
|
+
|
|
9
|
+
private static readonly NUM_NODES = 1000
|
|
10
|
+
private static readonly ID_LENGTH = 8
|
|
11
|
+
|
|
12
|
+
private readonly nodeNamesById: Record<string, number> = {}
|
|
13
|
+
private readonly nodes: SimulationNode[] = []
|
|
14
|
+
|
|
15
|
+
private readonly dhtIds: Array<{ type: string, data: Array<number> }>
|
|
16
|
+
private readonly groundTruth: Record<string, Array<{ name: string, distance: number, id: { type: string, data: Array<number> } }>>
|
|
17
|
+
|
|
18
|
+
constructor() {
|
|
19
|
+
if (!fs.existsSync('test/data/nodeids.json')) {
|
|
20
|
+
throw ('Cannot find test/data/nodeids.json, please run "npm run prepare-kademlia-simulation first"')
|
|
21
|
+
}
|
|
22
|
+
this.dhtIds = JSON.parse(fs.readFileSync('test/data/nodeids.json').toString())
|
|
23
|
+
this.groundTruth = JSON.parse(fs.readFileSync('test/data/orderedneighbors.json').toString())
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public run(): void {
|
|
27
|
+
for (let i = 0; i < KademliaSimulation.NUM_NODES; i++) {
|
|
28
|
+
const node = new SimulationNode(PeerID.fromValue(Buffer.from(this.dhtIds[i].data.slice(0, KademliaSimulation.ID_LENGTH))))
|
|
29
|
+
this.nodeNamesById[JSON.stringify(node.getContact().id)] = i
|
|
30
|
+
this.nodes.push(node)
|
|
31
|
+
node.joinDht(this.nodes[0])
|
|
32
|
+
|
|
33
|
+
process.stdout.write('.')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let minimumCorrectNeighbors = Number.MAX_SAFE_INTEGER
|
|
37
|
+
|
|
38
|
+
let sumCorrectNeighbors = 0
|
|
39
|
+
let sumKbucketSize = 1
|
|
40
|
+
let sumOutgoingRpcCalls = 0
|
|
41
|
+
let maxOutgoingRpcCalls = 0
|
|
42
|
+
|
|
43
|
+
for (let i = this.nodes.length - 1; i >= 0; i--) {
|
|
44
|
+
|
|
45
|
+
const numberOfOutgoingRpcCalls = this.nodes[i].getNumberOfOutgoingRpcCalls()
|
|
46
|
+
console.log('-----------')
|
|
47
|
+
console.log('Node: ' + i)
|
|
48
|
+
console.log('Kbucket size: ' + this.nodes[i].getKBucketSize())
|
|
49
|
+
console.log('Num incoming RPC calls: ' + this.nodes[i].getNumberOfIncomingRpcCalls())
|
|
50
|
+
console.log('Num outgoing RPC calls: ' + numberOfOutgoingRpcCalls)
|
|
51
|
+
|
|
52
|
+
sumOutgoingRpcCalls += numberOfOutgoingRpcCalls
|
|
53
|
+
|
|
54
|
+
if (maxOutgoingRpcCalls < numberOfOutgoingRpcCalls) {
|
|
55
|
+
maxOutgoingRpcCalls = numberOfOutgoingRpcCalls
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const kademliaNeighbors = this.nodes[i].getNeightborList().getContactIds()
|
|
59
|
+
|
|
60
|
+
let correctNeighbors = 0
|
|
61
|
+
for (let j = 0; j < this.groundTruth[i + ''].length; j++) {
|
|
62
|
+
if (this.groundTruth[i + ''][j].name != (this.nodeNamesById[JSON.stringify(kademliaNeighbors[j])] + '')) {
|
|
63
|
+
break
|
|
64
|
+
}
|
|
65
|
+
correctNeighbors++
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (correctNeighbors < minimumCorrectNeighbors) {
|
|
69
|
+
minimumCorrectNeighbors = correctNeighbors
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
console.log('Correct neighbors: ' + correctNeighbors)
|
|
73
|
+
|
|
74
|
+
if (i > 0) {
|
|
75
|
+
sumKbucketSize += this.nodes[i].getKBucketSize()
|
|
76
|
+
sumCorrectNeighbors += correctNeighbors
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const avgCorrectNeighbors = sumCorrectNeighbors / (KademliaSimulation.NUM_NODES - 1)
|
|
81
|
+
const avgKbucketSize = sumKbucketSize / (KademliaSimulation.NUM_NODES - 1)
|
|
82
|
+
const avgNumberOfOutgoingRpcCalls = sumOutgoingRpcCalls / (KademliaSimulation.NUM_NODES - 1)
|
|
83
|
+
|
|
84
|
+
console.log('----------- Simulation results ------------------')
|
|
85
|
+
console.log('Minimum correct neighbors: ' + minimumCorrectNeighbors)
|
|
86
|
+
console.log('Average correct neighbors: ' + avgCorrectNeighbors)
|
|
87
|
+
console.log('Average Kbucket size: ' + avgKbucketSize)
|
|
88
|
+
console.log('Average number of outgoing RPC calls: ' + avgNumberOfOutgoingRpcCalls)
|
|
89
|
+
console.log('MAX number of outgoing RPC calls: ' + maxOutgoingRpcCalls)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const simulation = new KademliaSimulation()
|
|
94
|
+
simulation.run()
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import KBucket from 'k-bucket'
|
|
2
|
+
import { Contact } from './Contact'
|
|
3
|
+
import { SortedContactList } from '../../../src/dht/contact/SortedContactList'
|
|
4
|
+
import { PeerID } from '../../../src/helpers/PeerID'
|
|
5
|
+
|
|
6
|
+
export class SimulationNode {
|
|
7
|
+
|
|
8
|
+
private numberOfNodesPerKBucket = 1
|
|
9
|
+
private K = 8
|
|
10
|
+
private ALPHA = 1
|
|
11
|
+
|
|
12
|
+
private bucket: KBucket<Contact>
|
|
13
|
+
private ownContact: Contact
|
|
14
|
+
|
|
15
|
+
private numberOfIncomingRpcCalls = 0
|
|
16
|
+
private numberOfOutgoingRpcCalls = 0
|
|
17
|
+
|
|
18
|
+
private neighborList: SortedContactList<Contact>
|
|
19
|
+
private ownId: PeerID
|
|
20
|
+
|
|
21
|
+
constructor(ownId: PeerID) {
|
|
22
|
+
this.ownId = ownId
|
|
23
|
+
this.ownContact = new Contact(this.ownId, this)
|
|
24
|
+
this.bucket = new KBucket({
|
|
25
|
+
localNodeId: this.ownId.value,
|
|
26
|
+
numberOfNodesPerKBucket: this.numberOfNodesPerKBucket
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
this.neighborList = new SortedContactList(this.ownId, 1000)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// For simulation use
|
|
33
|
+
|
|
34
|
+
public getNeightborList(): SortedContactList<Contact> {
|
|
35
|
+
return this.neighborList
|
|
36
|
+
}
|
|
37
|
+
public getContact(): Contact {
|
|
38
|
+
return this.ownContact
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public getKBucketSize(): number {
|
|
42
|
+
return this.bucket.count()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public getNumberOfIncomingRpcCalls(): number {
|
|
46
|
+
return this.numberOfIncomingRpcCalls
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public getNumberOfOutgoingRpcCalls(): number {
|
|
50
|
+
return this.numberOfOutgoingRpcCalls
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// RPC call
|
|
54
|
+
|
|
55
|
+
public getClosestNodesTo(id: PeerID, caller: SimulationNode): Contact[] {
|
|
56
|
+
this.numberOfIncomingRpcCalls++
|
|
57
|
+
const ret = this.bucket.closest(id.value)
|
|
58
|
+
|
|
59
|
+
if (!this.bucket.get(id.value)) {
|
|
60
|
+
const contact = new Contact(PeerID.fromValue(id.value), caller)
|
|
61
|
+
this.bucket.add(contact)
|
|
62
|
+
this.neighborList.addContact(contact)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return ret
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private findMoreContacts(contactList: Contact[], shortlist: SortedContactList<Contact>) {
|
|
69
|
+
contactList.forEach((contact) => {
|
|
70
|
+
shortlist.setContacted(contact.peerId)
|
|
71
|
+
shortlist.setActive(contact.peerId)
|
|
72
|
+
this.numberOfOutgoingRpcCalls++
|
|
73
|
+
const returnedContacts = contact.dhtNode!.getClosestNodesTo(this.ownId, this)
|
|
74
|
+
shortlist.addContacts(returnedContacts)
|
|
75
|
+
returnedContacts.forEach((returnedContact: Contact) => {
|
|
76
|
+
if (!this.bucket.get(returnedContact.id)) {
|
|
77
|
+
this.bucket.add(returnedContact)
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public joinDht(entryPoint: SimulationNode): void {
|
|
84
|
+
if (entryPoint.getContact().peerId.equals(this.ownId)) {
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this.bucket.add(entryPoint.getContact())
|
|
89
|
+
const closest = this.bucket.closest(this.ownId.value, this.ALPHA)
|
|
90
|
+
|
|
91
|
+
this.neighborList.addContacts(closest)
|
|
92
|
+
|
|
93
|
+
/* eslint-disable no-constant-condition */
|
|
94
|
+
while (true) {
|
|
95
|
+
let oldClosestContactId = this.neighborList.getClosestContactId()
|
|
96
|
+
let uncontacted = this.neighborList.getUncontactedContacts(this.ALPHA)
|
|
97
|
+
if (uncontacted.length < 1) {
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this.findMoreContacts(uncontacted, this.neighborList)
|
|
102
|
+
|
|
103
|
+
if (oldClosestContactId.equals(this.neighborList.getClosestContactId())) {
|
|
104
|
+
uncontacted = this.neighborList.getUncontactedContacts(this.K)
|
|
105
|
+
if (uncontacted.length < 1) {
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
while (true) {
|
|
110
|
+
oldClosestContactId = this.neighborList.getClosestContactId()
|
|
111
|
+
this.findMoreContacts(uncontacted, this.neighborList)
|
|
112
|
+
|
|
113
|
+
if (this.neighborList.getActiveContacts().length >= this.K ||
|
|
114
|
+
oldClosestContactId.equals(this.neighborList.getClosestContactId())) {
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
uncontacted = this.neighborList.getUncontactedContacts(this.ALPHA)
|
|
118
|
+
if (uncontacted.length < 1) {
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import crypto from 'crypto'
|
|
3
|
+
import KBucket from 'k-bucket'
|
|
4
|
+
|
|
5
|
+
const ID_LENGTH = 8
|
|
6
|
+
const NUM_NODES = 2000
|
|
7
|
+
const NUM_NEAREST = 10
|
|
8
|
+
|
|
9
|
+
const generateId = function(): Uint8Array {
|
|
10
|
+
return crypto.randomBytes(ID_LENGTH)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const findNNearestNeighbors = function(ownIndex: number, ownId: Uint8Array, nodes: Array<Uint8Array>, n: number): Array<number> {
|
|
14
|
+
const retIndex: Array<number> = []
|
|
15
|
+
|
|
16
|
+
for (let i = 0; i < n; i++) {
|
|
17
|
+
let closestIndex: number = Number.MAX_VALUE
|
|
18
|
+
let closestDistance: number = Number.MAX_VALUE
|
|
19
|
+
|
|
20
|
+
for (let j = 0; j < nodes.length; j++) {
|
|
21
|
+
if (j == ownIndex || retIndex.includes(j)) {
|
|
22
|
+
continue
|
|
23
|
+
}
|
|
24
|
+
const distance = KBucket.distance(ownId, nodes[j])
|
|
25
|
+
if (distance < closestDistance) {
|
|
26
|
+
closestDistance = distance
|
|
27
|
+
closestIndex = j
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
retIndex.push(closestIndex)
|
|
31
|
+
}
|
|
32
|
+
return retIndex
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const writer = fs.createWriteStream('nodeids.json', {})
|
|
36
|
+
const neighborWriter = fs.createWriteStream('orderedneighbors.json', {})
|
|
37
|
+
|
|
38
|
+
neighborWriter.write('{\n')
|
|
39
|
+
|
|
40
|
+
const nodes: Array<Uint8Array> = []
|
|
41
|
+
|
|
42
|
+
// generate nodeIds
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i < NUM_NODES; i++) {
|
|
45
|
+
const id = generateId()
|
|
46
|
+
nodes.push(id)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
writer.write(JSON.stringify(nodes, null, 4))
|
|
50
|
+
writer.end()
|
|
51
|
+
|
|
52
|
+
for (let i = 0; i < NUM_NODES; i++) {
|
|
53
|
+
|
|
54
|
+
const neighborIds = findNNearestNeighbors(i, nodes[i], nodes, NUM_NEAREST)
|
|
55
|
+
|
|
56
|
+
const neighborNames: Array<{ name: number, distance: number, id: Uint8Array }> = []
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
|
58
|
+
for (let j = 0; j < neighborIds.length; j++) {
|
|
59
|
+
neighborNames.push({ name: neighborIds[j], distance: KBucket.distance(nodes[i], nodes[neighborIds[j]]), id: nodes[neighborIds[j]] })
|
|
60
|
+
}
|
|
61
|
+
neighborWriter.write('"' + i + '": ' + JSON.stringify(neighborNames))
|
|
62
|
+
process.stdout.write('.')
|
|
63
|
+
|
|
64
|
+
if (i != NUM_NODES - 1) {
|
|
65
|
+
neighborWriter.write(',\n')
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
neighborWriter.write('}')
|
|
70
|
+
neighborWriter.end()
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
2
|
+
import { DhtNode } from '../../src/dht/DhtNode'
|
|
3
|
+
import { peerIdFromPeerDescriptor } from '../../src/helpers/peerIdFromPeerDescriptor'
|
|
4
|
+
|
|
5
|
+
describe('Layer0-Layer1', () => {
|
|
6
|
+
const epPeerDescriptor: PeerDescriptor = {
|
|
7
|
+
kademliaId: Uint8Array.from([1, 2, 3]),
|
|
8
|
+
type: NodeType.NODEJS,
|
|
9
|
+
websocket: { ip: '127.0.0.1', port: 10016 }
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const STREAM_ID1 = 'stream1'
|
|
13
|
+
const STREAM_ID2 = 'stream2'
|
|
14
|
+
|
|
15
|
+
let epDhtNode: DhtNode
|
|
16
|
+
let node1: DhtNode
|
|
17
|
+
let node2: DhtNode
|
|
18
|
+
|
|
19
|
+
let stream1Node1: DhtNode
|
|
20
|
+
let stream1Node2: DhtNode
|
|
21
|
+
let stream2Node1: DhtNode
|
|
22
|
+
let stream2Node2: DhtNode
|
|
23
|
+
|
|
24
|
+
beforeEach(async () => {
|
|
25
|
+
|
|
26
|
+
epDhtNode = new DhtNode({ peerDescriptor: epPeerDescriptor })
|
|
27
|
+
await epDhtNode.start()
|
|
28
|
+
await epDhtNode.joinDht([epPeerDescriptor])
|
|
29
|
+
|
|
30
|
+
node1 = new DhtNode({ peerIdString: '1', webSocketPort: 10017, entryPoints: [epPeerDescriptor] })
|
|
31
|
+
node2 = new DhtNode({ peerIdString: '2', webSocketPort: 10018, entryPoints: [epPeerDescriptor] })
|
|
32
|
+
|
|
33
|
+
await node1.start()
|
|
34
|
+
await node2.start()
|
|
35
|
+
|
|
36
|
+
stream1Node1 = new DhtNode({ transportLayer: epDhtNode, serviceId: STREAM_ID1 })
|
|
37
|
+
stream1Node2 = new DhtNode({ transportLayer: node1, serviceId: STREAM_ID1 })
|
|
38
|
+
|
|
39
|
+
stream2Node1 = new DhtNode({ transportLayer: epDhtNode, serviceId: STREAM_ID2 })
|
|
40
|
+
stream2Node2 = new DhtNode({ transportLayer: node2, serviceId: STREAM_ID2 })
|
|
41
|
+
|
|
42
|
+
await Promise.all([
|
|
43
|
+
stream1Node1.start(),
|
|
44
|
+
stream1Node2.start(),
|
|
45
|
+
stream2Node1.start(),
|
|
46
|
+
stream2Node2.start()
|
|
47
|
+
])
|
|
48
|
+
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
afterEach(async () => {
|
|
52
|
+
await Promise.all([
|
|
53
|
+
node1.stop(),
|
|
54
|
+
node2.stop(),
|
|
55
|
+
epDhtNode.stop(),
|
|
56
|
+
stream1Node1.stop(),
|
|
57
|
+
stream1Node2.stop(),
|
|
58
|
+
stream2Node1.stop(),
|
|
59
|
+
stream2Node2.stop()
|
|
60
|
+
])
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('Happy path', async () => {
|
|
64
|
+
await Promise.all([
|
|
65
|
+
node1.joinDht([epPeerDescriptor]),
|
|
66
|
+
node2.joinDht([epPeerDescriptor])
|
|
67
|
+
])
|
|
68
|
+
await Promise.all([
|
|
69
|
+
stream1Node1.joinDht([epPeerDescriptor]),
|
|
70
|
+
stream1Node2.joinDht([epPeerDescriptor])
|
|
71
|
+
])
|
|
72
|
+
|
|
73
|
+
await Promise.all([
|
|
74
|
+
stream2Node1.joinDht([epPeerDescriptor]),
|
|
75
|
+
stream2Node2.joinDht([epPeerDescriptor])
|
|
76
|
+
])
|
|
77
|
+
expect(stream1Node1.getNeighborList().getSize()).toEqual(1)
|
|
78
|
+
expect(stream1Node2.getNeighborList().getSize()).toEqual(1)
|
|
79
|
+
expect(stream2Node1.getNeighborList().getSize()).toEqual(1)
|
|
80
|
+
expect(stream2Node2.getNeighborList().getSize()).toEqual(1)
|
|
81
|
+
|
|
82
|
+
expect(stream1Node1.getNeighborList().getContactIds()[0].equals(peerIdFromPeerDescriptor(node1.getPeerDescriptor()))).toEqual(true)
|
|
83
|
+
expect(stream1Node2.getNeighborList().getContactIds()[0].equals(peerIdFromPeerDescriptor(epPeerDescriptor))).toEqual(true)
|
|
84
|
+
expect(stream2Node1.getNeighborList().getContactIds()[0].equals(peerIdFromPeerDescriptor(node2.getPeerDescriptor()))).toEqual(true)
|
|
85
|
+
expect(stream2Node2.getNeighborList().getContactIds()[0].equals(peerIdFromPeerDescriptor(epPeerDescriptor))).toEqual(true)
|
|
86
|
+
})
|
|
87
|
+
})
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
2
|
+
import { DhtNode } from '../../src/dht/DhtNode'
|
|
3
|
+
|
|
4
|
+
describe('Layer0', () => {
|
|
5
|
+
|
|
6
|
+
const epPeerDescriptor: PeerDescriptor = {
|
|
7
|
+
kademliaId: Uint8Array.from([1, 2, 3]),
|
|
8
|
+
type: NodeType.NODEJS,
|
|
9
|
+
websocket: { ip: '127.0.0.1', port: 10011 }
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let epDhtNode: DhtNode
|
|
13
|
+
let node1: DhtNode
|
|
14
|
+
let node2: DhtNode
|
|
15
|
+
let node3: DhtNode
|
|
16
|
+
let node4: DhtNode
|
|
17
|
+
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
|
|
20
|
+
epDhtNode = new DhtNode({ peerDescriptor: epPeerDescriptor })
|
|
21
|
+
await epDhtNode.start()
|
|
22
|
+
|
|
23
|
+
await epDhtNode.joinDht([epPeerDescriptor])
|
|
24
|
+
|
|
25
|
+
node1 = new DhtNode({ peerIdString: '1', webSocketPort: 10012, entryPoints: [epPeerDescriptor] })
|
|
26
|
+
node2 = new DhtNode({ peerIdString: '2', webSocketPort: 10013, entryPoints: [epPeerDescriptor] })
|
|
27
|
+
node3 = new DhtNode({ peerIdString: '3', webSocketPort: 10014, entryPoints: [epPeerDescriptor] })
|
|
28
|
+
node4 = new DhtNode({ peerIdString: '4', webSocketPort: 10015, entryPoints: [epPeerDescriptor] })
|
|
29
|
+
|
|
30
|
+
await node1.start()
|
|
31
|
+
await node2.start()
|
|
32
|
+
await node3.start()
|
|
33
|
+
await node4.start()
|
|
34
|
+
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
afterEach(async () => {
|
|
38
|
+
await Promise.all([
|
|
39
|
+
epDhtNode.stop(),
|
|
40
|
+
node1.stop(),
|
|
41
|
+
node2.stop(),
|
|
42
|
+
node3.stop(),
|
|
43
|
+
node4.stop()
|
|
44
|
+
])
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('Happy path', async () => {
|
|
48
|
+
await Promise.all([
|
|
49
|
+
node1.joinDht([epPeerDescriptor]),
|
|
50
|
+
node2.joinDht([epPeerDescriptor]),
|
|
51
|
+
node3.joinDht([epPeerDescriptor]),
|
|
52
|
+
node4.joinDht([epPeerDescriptor])
|
|
53
|
+
])
|
|
54
|
+
|
|
55
|
+
expect(node1.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
56
|
+
expect(node2.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
57
|
+
expect(node3.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
58
|
+
expect(node4.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
59
|
+
}, 10000)
|
|
60
|
+
})
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
2
|
+
import { DhtNode } from '../../src/dht/DhtNode'
|
|
3
|
+
import { waitForEvent3 } from '@streamr/utils'
|
|
4
|
+
import { ConnectionManager, Events as ConnectionManagerEvents } from '../../src/connection/ConnectionManager'
|
|
5
|
+
|
|
6
|
+
describe('Layer0MixedConnectionTypes', () => {
|
|
7
|
+
|
|
8
|
+
const epPeerDescriptor: PeerDescriptor = {
|
|
9
|
+
kademliaId: Uint8Array.from([1, 2, 3]),
|
|
10
|
+
type: NodeType.NODEJS,
|
|
11
|
+
websocket: { ip: '127.0.0.1', port: 11221 }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let epDhtNode: DhtNode
|
|
15
|
+
let node1: DhtNode
|
|
16
|
+
let node2: DhtNode
|
|
17
|
+
let node3: DhtNode
|
|
18
|
+
let node4: DhtNode
|
|
19
|
+
let node5: DhtNode
|
|
20
|
+
|
|
21
|
+
beforeEach(async () => {
|
|
22
|
+
|
|
23
|
+
epDhtNode = new DhtNode({ peerDescriptor: epPeerDescriptor, numberOfNodesPerKBucket: 2 })
|
|
24
|
+
await epDhtNode.start()
|
|
25
|
+
|
|
26
|
+
await epDhtNode.joinDht([epPeerDescriptor])
|
|
27
|
+
node1 = new DhtNode({ peerIdString: 'Peer1', webSocketPort: 11222, entryPoints: [epPeerDescriptor] })
|
|
28
|
+
node2 = new DhtNode({ peerIdString: 'Peer2', webSocketPort: 11228, entryPoints: [epPeerDescriptor] })
|
|
29
|
+
node3 = new DhtNode({ peerIdString: 'Peer3', entryPoints: [epPeerDescriptor] })
|
|
30
|
+
node4 = new DhtNode({ peerIdString: 'Peer4', entryPoints: [epPeerDescriptor] })
|
|
31
|
+
node5 = new DhtNode({ peerIdString: 'Peer5', entryPoints: [epPeerDescriptor] })
|
|
32
|
+
|
|
33
|
+
await Promise.all([
|
|
34
|
+
node1.start(),
|
|
35
|
+
node2.start(),
|
|
36
|
+
node3.start(),
|
|
37
|
+
node4.start(),
|
|
38
|
+
node5.start()
|
|
39
|
+
])
|
|
40
|
+
|
|
41
|
+
await epDhtNode.joinDht([epPeerDescriptor])
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
afterEach(async () => {
|
|
45
|
+
await Promise.all([
|
|
46
|
+
epDhtNode.stop(),
|
|
47
|
+
node1.stop(),
|
|
48
|
+
node2.stop(),
|
|
49
|
+
node3.stop(),
|
|
50
|
+
node4.stop(),
|
|
51
|
+
node5.stop()
|
|
52
|
+
])
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('2 non-server peers join first', async () => {
|
|
56
|
+
|
|
57
|
+
const promise = Promise.all([
|
|
58
|
+
waitForEvent3<ConnectionManagerEvents>((node3.getTransport() as ConnectionManager), 'newConnection'),
|
|
59
|
+
waitForEvent3<ConnectionManagerEvents>((node4.getTransport() as ConnectionManager), 'newConnection'),
|
|
60
|
+
])
|
|
61
|
+
|
|
62
|
+
node3.joinDht([epPeerDescriptor])
|
|
63
|
+
node4.joinDht([epPeerDescriptor])
|
|
64
|
+
|
|
65
|
+
await promise
|
|
66
|
+
await Promise.all([
|
|
67
|
+
node1.joinDht([epPeerDescriptor]),
|
|
68
|
+
node2.joinDht([epPeerDescriptor]),
|
|
69
|
+
node5.joinDht([epPeerDescriptor])
|
|
70
|
+
])
|
|
71
|
+
|
|
72
|
+
expect(node1.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
73
|
+
expect(node2.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
74
|
+
expect(node3.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
75
|
+
expect(node4.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
76
|
+
expect(node5.getBucketSize()).toBeGreaterThanOrEqual(1)
|
|
77
|
+
|
|
78
|
+
}, 15000)
|
|
79
|
+
|
|
80
|
+
it('Simultaneous joins', async () => {
|
|
81
|
+
await Promise.all([
|
|
82
|
+
node1.joinDht([epPeerDescriptor]),
|
|
83
|
+
node2.joinDht([epPeerDescriptor]),
|
|
84
|
+
node3.joinDht([epPeerDescriptor]),
|
|
85
|
+
node4.joinDht([epPeerDescriptor]),
|
|
86
|
+
node5.joinDht([epPeerDescriptor])
|
|
87
|
+
])
|
|
88
|
+
expect(node1.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
89
|
+
expect(node2.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
90
|
+
expect(node3.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
91
|
+
expect(node4.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
92
|
+
expect(node5.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
93
|
+
}, 30000)
|
|
94
|
+
})
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { DhtNode } from '../../src/dht/DhtNode'
|
|
2
|
+
import { NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
3
|
+
import { PeerID } from '../../src/exports'
|
|
4
|
+
|
|
5
|
+
describe('Layer 1 on Layer 0 with mocked connections', () => {
|
|
6
|
+
const entryPointId = '00000'
|
|
7
|
+
|
|
8
|
+
const entrypointDescriptor: PeerDescriptor = {
|
|
9
|
+
kademliaId: PeerID.fromString(entryPointId).value,
|
|
10
|
+
type: NodeType.NODEJS,
|
|
11
|
+
websocket: {
|
|
12
|
+
ip: '127.0.0.1',
|
|
13
|
+
port: 23232
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let layer0EntryPoint: DhtNode
|
|
18
|
+
let layer1Node1: DhtNode
|
|
19
|
+
|
|
20
|
+
let layer0Node1: DhtNode
|
|
21
|
+
let layer1EntryPoint: DhtNode
|
|
22
|
+
|
|
23
|
+
let layer0Node2: DhtNode
|
|
24
|
+
let layer1Node2: DhtNode
|
|
25
|
+
|
|
26
|
+
let layer0Node3: DhtNode
|
|
27
|
+
let layer1Node3: DhtNode
|
|
28
|
+
|
|
29
|
+
let layer0Node4: DhtNode
|
|
30
|
+
let layer1Node4: DhtNode
|
|
31
|
+
|
|
32
|
+
beforeEach(async () => {
|
|
33
|
+
|
|
34
|
+
layer0EntryPoint = new DhtNode({ peerDescriptor: entrypointDescriptor })
|
|
35
|
+
|
|
36
|
+
const layer0Node1Id = 'layer0Node1'
|
|
37
|
+
layer0Node1 = new DhtNode({
|
|
38
|
+
peerIdString: layer0Node1Id
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const layer0Node2Id = 'layer0Node2'
|
|
42
|
+
layer0Node2 = new DhtNode({
|
|
43
|
+
peerIdString: layer0Node2Id
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const layer0Node3Id = 'layer0Node3'
|
|
47
|
+
layer0Node3 = new DhtNode({
|
|
48
|
+
peerIdString: layer0Node3Id
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const layer0Node4Id = 'layer0Node4'
|
|
52
|
+
layer0Node4 = new DhtNode({
|
|
53
|
+
peerIdString: layer0Node4Id
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
layer1EntryPoint = new DhtNode({
|
|
57
|
+
peerIdString: entryPointId,
|
|
58
|
+
transportLayer: layer0EntryPoint,
|
|
59
|
+
serviceId: 'layer1'
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
layer1Node1 = new DhtNode({
|
|
63
|
+
peerIdString: layer0Node1Id,
|
|
64
|
+
transportLayer: layer0Node1,
|
|
65
|
+
serviceId: 'layer1'
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
layer1Node2 = new DhtNode({
|
|
69
|
+
peerIdString: layer0Node2Id,
|
|
70
|
+
transportLayer: layer0Node2,
|
|
71
|
+
serviceId: 'layer1'
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
layer1Node3 = new DhtNode({
|
|
75
|
+
peerIdString: layer0Node3Id,
|
|
76
|
+
transportLayer: layer0Node3,
|
|
77
|
+
serviceId: 'layer1'
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
layer1Node4 = new DhtNode({
|
|
81
|
+
peerIdString: layer0Node4Id,
|
|
82
|
+
transportLayer: layer0Node4,
|
|
83
|
+
serviceId: 'layer1'
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
await layer0EntryPoint.start()
|
|
87
|
+
await layer0Node1.start()
|
|
88
|
+
await layer0Node2.start()
|
|
89
|
+
await layer0Node3.start()
|
|
90
|
+
await layer0Node4.start()
|
|
91
|
+
await layer1EntryPoint.start()
|
|
92
|
+
await layer1Node1.start()
|
|
93
|
+
await layer1Node2.start()
|
|
94
|
+
await layer1Node3.start()
|
|
95
|
+
await layer1Node4.start()
|
|
96
|
+
|
|
97
|
+
await layer0EntryPoint.joinDht([entrypointDescriptor])
|
|
98
|
+
await layer1EntryPoint.joinDht([entrypointDescriptor])
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
afterEach(async () => {
|
|
102
|
+
await Promise.all([
|
|
103
|
+
layer0EntryPoint.stop(),
|
|
104
|
+
layer0Node1.stop(),
|
|
105
|
+
layer0Node2.stop(),
|
|
106
|
+
layer0Node3.stop(),
|
|
107
|
+
layer0Node4.stop(),
|
|
108
|
+
layer1EntryPoint.stop(),
|
|
109
|
+
layer1Node1.stop(),
|
|
110
|
+
layer1Node2.stop(),
|
|
111
|
+
layer1Node3.stop(),
|
|
112
|
+
layer1Node4.stop()
|
|
113
|
+
])
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('Happy Path', async () => {
|
|
117
|
+
await Promise.all([
|
|
118
|
+
layer0Node1.joinDht([entrypointDescriptor]),
|
|
119
|
+
layer0Node2.joinDht([entrypointDescriptor]),
|
|
120
|
+
layer0Node3.joinDht([entrypointDescriptor]),
|
|
121
|
+
layer0Node4.joinDht([entrypointDescriptor])
|
|
122
|
+
])
|
|
123
|
+
|
|
124
|
+
await layer1Node1.joinDht([entrypointDescriptor])
|
|
125
|
+
await layer1Node2.joinDht([entrypointDescriptor])
|
|
126
|
+
await layer1Node3.joinDht([entrypointDescriptor])
|
|
127
|
+
await layer1Node4.joinDht([entrypointDescriptor])
|
|
128
|
+
|
|
129
|
+
expect(layer1Node1.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
130
|
+
expect(layer1Node2.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
131
|
+
expect(layer1Node3.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
132
|
+
expect(layer1Node4.getBucketSize()).toBeGreaterThanOrEqual(2)
|
|
133
|
+
}, 60000)
|
|
134
|
+
})
|