@streamr/dht 102.0.0-beta.1 → 102.0.0-beta.3
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/generated/google/protobuf/any.d.ts +180 -0
- package/dist/generated/google/protobuf/any.js +155 -0
- package/dist/generated/google/protobuf/any.js.map +1 -0
- package/dist/generated/google/protobuf/empty.d.ts +31 -0
- package/dist/generated/google/protobuf/empty.js +32 -0
- package/dist/generated/google/protobuf/empty.js.map +1 -0
- package/dist/generated/google/protobuf/timestamp.d.ts +155 -0
- package/dist/generated/google/protobuf/timestamp.js +136 -0
- package/dist/generated/google/protobuf/timestamp.js.map +1 -0
- package/dist/generated/packages/dht/protos/DhtRpc.client.d.ts +361 -0
- package/dist/generated/packages/dht/protos/DhtRpc.client.js +285 -0
- package/dist/generated/packages/dht/protos/DhtRpc.client.js.map +1 -0
- package/dist/generated/packages/dht/protos/DhtRpc.d.ts +999 -0
- package/dist/generated/packages/dht/protos/DhtRpc.js +677 -0
- package/dist/generated/packages/dht/protos/DhtRpc.js.map +1 -0
- package/dist/generated/packages/dht/protos/DhtRpc.server.d.ts +162 -0
- package/dist/generated/packages/dht/protos/DhtRpc.server.js +3 -0
- package/dist/generated/packages/dht/protos/DhtRpc.server.js.map +1 -0
- package/dist/generated/packages/proto-rpc/protos/ProtoRpc.d.ts +87 -0
- package/dist/generated/packages/proto-rpc/protos/ProtoRpc.js +66 -0
- package/dist/generated/packages/proto-rpc/protos/ProtoRpc.js.map +1 -0
- package/dist/package.json +7 -7
- package/package.json +7 -7
- package/eslint.config.mjs +0 -12
- package/src/connection/Connection.ts +0 -28
- package/src/connection/ConnectionLockRpcLocal.ts +0 -78
- package/src/connection/ConnectionLockRpcRemote.ts +0 -64
- package/src/connection/ConnectionLockStates.ts +0 -131
- package/src/connection/ConnectionManager.ts +0 -661
- package/src/connection/ConnectionsView.ts +0 -8
- package/src/connection/ConnectorFacade.ts +0 -217
- package/src/connection/Handshaker.ts +0 -209
- package/src/connection/IConnection.ts +0 -40
- package/src/connection/ManagedConnection.ts +0 -113
- package/src/connection/OutputBuffer.ts +0 -28
- package/src/connection/PendingConnection.ts +0 -68
- package/src/connection/connectivityChecker.ts +0 -108
- package/src/connection/connectivityRequestHandler.ts +0 -116
- package/src/connection/simulator/Simulator.ts +0 -369
- package/src/connection/simulator/SimulatorConnection.ts +0 -137
- package/src/connection/simulator/SimulatorConnector.ts +0 -98
- package/src/connection/simulator/SimulatorTransport.ts +0 -15
- package/src/connection/simulator/pings.ts +0 -42
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -242
- package/src/connection/webrtc/IWebrtcConnection.ts +0 -24
- package/src/connection/webrtc/NodeWebrtcConnection.ts +0 -245
- package/src/connection/webrtc/WebrtcConnector.ts +0 -234
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +0 -108
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +0 -60
- package/src/connection/webrtc/iceServerAsString.ts +0 -15
- package/src/connection/websocket/AbstractWebsocketClientConnection.ts +0 -122
- package/src/connection/websocket/AutoCertifierClientFacade.ts +0 -89
- package/src/connection/websocket/BrowserWebsocketClientConnection.ts +0 -44
- package/src/connection/websocket/NodeWebsocketClientConnection.ts +0 -39
- package/src/connection/websocket/WebsocketClientConnector.ts +0 -119
- package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +0 -38
- package/src/connection/websocket/WebsocketClientConnectorRpcRemote.ts +0 -19
- package/src/connection/websocket/WebsocketServer.ts +0 -164
- package/src/connection/websocket/WebsocketServerConnection.ts +0 -109
- package/src/connection/websocket/WebsocketServerConnector.ts +0 -290
- package/src/dht/DhtNode.ts +0 -683
- package/src/dht/DhtNodeRpcLocal.ts +0 -84
- package/src/dht/DhtNodeRpcRemote.ts +0 -107
- package/src/dht/ExternalApiRpcLocal.ts +0 -58
- package/src/dht/ExternalApiRpcRemote.ts +0 -41
- package/src/dht/PeerManager.ts +0 -305
- package/src/dht/contact/Contact.ts +0 -19
- package/src/dht/contact/ContactList.ts +0 -43
- package/src/dht/contact/RandomContactList.ts +0 -56
- package/src/dht/contact/RingContactList.ts +0 -143
- package/src/dht/contact/RpcRemote.ts +0 -72
- package/src/dht/contact/SortedContactList.ts +0 -173
- package/src/dht/contact/getClosestNodes.ts +0 -24
- package/src/dht/contact/ringIdentifiers.ts +0 -62
- package/src/dht/discovery/DiscoverySession.ts +0 -129
- package/src/dht/discovery/PeerDiscovery.ts +0 -244
- package/src/dht/discovery/RingDiscoverySession.ts +0 -148
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +0 -251
- package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +0 -34
- package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +0 -43
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +0 -231
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +0 -35
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.ts +0 -30
- package/src/dht/routing/DuplicateDetector.ts +0 -34
- package/src/dht/routing/Router.ts +0 -246
- package/src/dht/routing/RouterRpcLocal.ts +0 -78
- package/src/dht/routing/RouterRpcRemote.ts +0 -80
- package/src/dht/routing/RoutingSession.ts +0 -243
- package/src/dht/routing/RoutingTablesCache.ts +0 -60
- package/src/dht/routing/getPreviousPeer.ts +0 -6
- package/src/dht/store/LocalDataStore.ts +0 -84
- package/src/dht/store/StoreManager.ts +0 -170
- package/src/dht/store/StoreRpcLocal.ts +0 -89
- package/src/dht/store/StoreRpcRemote.ts +0 -32
- package/src/exports.ts +0 -33
- package/src/helpers/AddressTools.ts +0 -28
- package/src/helpers/Connectivity.ts +0 -19
- package/src/helpers/browser/isBrowserEnvironment.ts +0 -1
- package/src/helpers/browser/isBrowserEnvironment_override.ts +0 -3
- package/src/helpers/createPeerDescriptor.ts +0 -57
- package/src/helpers/createPeerDescriptorSignaturePayload.ts +0 -28
- package/src/helpers/debugHelpers.ts +0 -9
- package/src/helpers/errors.ts +0 -49
- package/src/helpers/offering.ts +0 -15
- package/src/helpers/protoClasses.ts +0 -57
- package/src/helpers/protoToString.ts +0 -21
- package/src/helpers/version.ts +0 -32
- package/src/identifiers.ts +0 -29
- package/src/rpc-protocol/DhtCallContext.ts +0 -14
- package/src/rpc-protocol/DhtRpcOptions.ts +0 -10
- package/src/transport/ITransport.ts +0 -37
- package/src/transport/ListeningRpcCommunicator.ts +0 -32
- package/src/transport/RoutingRpcCommunicator.ts +0 -66
- package/src/types/ServiceID.ts +0 -1
- package/src/types/textencoding.d.ts +0 -6
- package/test/benchmark/Find.test.ts +0 -72
- package/test/benchmark/KademliaCorrectness.test.ts +0 -114
- package/test/benchmark/RingCorrectness.test.ts +0 -157
- package/test/benchmark/SortedContactListBenchmark.test.ts +0 -108
- package/test/benchmark/WebsocketServerMemoryLeak.test.ts +0 -41
- package/test/benchmark/hybrid-network-simulation/RingContactList.test.ts +0 -71
- package/test/end-to-end/GeoIpLayer0.test.ts +0 -55
- package/test/end-to-end/Layer0-Layer1.test.ts +0 -93
- package/test/end-to-end/Layer0.test.ts +0 -76
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +0 -110
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +0 -137
- package/test/end-to-end/Layer0Webrtc.test.ts +0 -85
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +0 -82
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +0 -76
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +0 -52
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +0 -69
- package/test/end-to-end/memory-leak.test.ts +0 -80
- package/test/integration/ConnectionLocking.test.ts +0 -192
- package/test/integration/ConnectionManager.test.ts +0 -528
- package/test/integration/ConnectivityChecking.test.ts +0 -53
- package/test/integration/DhtJoinPeerDiscovery.test.ts +0 -49
- package/test/integration/DhtNode.test.ts +0 -66
- package/test/integration/DhtNodeExternalAPI.test.ts +0 -48
- package/test/integration/DhtNodeRpcRemote.test.ts +0 -66
- package/test/integration/DhtRpc.test.ts +0 -121
- package/test/integration/Find.test.ts +0 -45
- package/test/integration/GeoIpConnectivityChecking.test.ts +0 -72
- package/test/integration/Layer1-scale.test.ts +0 -189
- package/test/integration/Mock-Layer1-Layer0.test.ts +0 -85
- package/test/integration/MultipleEntryPointJoining.test.ts +0 -105
- package/test/integration/ReplicateData.test.ts +0 -104
- package/test/integration/RouteMessage.test.ts +0 -230
- package/test/integration/RouterRpcRemote.test.ts +0 -77
- package/test/integration/SimultaneousConnections.test.ts +0 -316
- package/test/integration/Store.test.ts +0 -85
- package/test/integration/StoreAndDelete.test.ts +0 -77
- package/test/integration/StoreOnDhtWithThreeNodes.test.ts +0 -59
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +0 -51
- package/test/integration/StoreRpcRemote.test.ts +0 -54
- package/test/integration/WebrtcConnectionManagement.test.ts +0 -191
- package/test/integration/WebrtcConnectorRpc.test.ts +0 -125
- package/test/integration/Websocket.test.ts +0 -65
- package/test/integration/WebsocketClientConnectorRpc.test.ts +0 -69
- package/test/integration/WebsocketConnectionManagement.test.ts +0 -191
- package/test/integration/rpc-connections-over-webrtc.test.ts +0 -123
- package/test/kademlia-simulation/data/nodeids.json +0 -13002
- package/test/kademlia-simulation/data/orderedneighbors.json +0 -1001
- package/test/types/global.d.ts +0 -1
- package/test/unit/AddressTools.test.ts +0 -44
- package/test/unit/AutoCertifierClientFacade.test.ts +0 -58
- package/test/unit/ConnectionManager.test.ts +0 -65
- package/test/unit/ConnectivityHelpers.test.ts +0 -61
- package/test/unit/DiscoverySession.test.ts +0 -87
- package/test/unit/DuplicateDetector.test.ts +0 -31
- package/test/unit/Handshaker.test.ts +0 -169
- package/test/unit/ListeningRpcCommunicator.test.ts +0 -52
- package/test/unit/LocalDataStore.test.ts +0 -108
- package/test/unit/ManagedConnection.test.ts +0 -58
- package/test/unit/PeerManager.test.ts +0 -93
- package/test/unit/PendingConnection.test.ts +0 -57
- package/test/unit/ProtobufMessage.test.ts +0 -21
- package/test/unit/RandomContactList.test.ts +0 -58
- package/test/unit/RecursiveOperationManager.test.ts +0 -161
- package/test/unit/RecursiveOperationSession.test.ts +0 -68
- package/test/unit/Router.test.ts +0 -137
- package/test/unit/RoutingSession.test.ts +0 -86
- package/test/unit/SortedContactList.test.ts +0 -115
- package/test/unit/StoreManager.test.ts +0 -146
- package/test/unit/StoreRpcLocal.test.ts +0 -167
- package/test/unit/WebrtcConnection.test.ts +0 -29
- package/test/unit/WebrtcConnector.test.ts +0 -56
- package/test/unit/WebsocketClientConnector.test.ts +0 -101
- package/test/unit/WebsocketServer.test.ts +0 -66
- package/test/unit/WebsocketServerConnector.test.ts +0 -102
- package/test/unit/connectivityRequestHandler.test.ts +0 -104
- package/test/unit/createPeerDescriptor.test.ts +0 -69
- package/test/unit/customMatchers.test.ts +0 -34
- package/test/unit/getClosestNodes.test.ts +0 -30
- package/test/unit/version.test.ts +0 -18
- package/test/unit/webrtcReplaceInternalIpWithExternalIp.test.ts +0 -18
- package/test/utils/FakeConnectorFacade.ts +0 -41
- package/test/utils/FakeRpcCommunicator.ts +0 -23
- package/test/utils/FakeTransport.ts +0 -79
- package/test/utils/customMatchers.ts +0 -71
- package/test/utils/mock/MockConnection.ts +0 -26
- package/test/utils/mock/MockConnectionsView.ts +0 -18
- package/test/utils/mock/MockRouter.ts +0 -62
- package/test/utils/mock/MockRpcCommunicator.ts +0 -7
- package/test/utils/mock/MockTransport.ts +0 -26
- package/test/utils/mock/mockDataEntry.ts +0 -38
- package/test/utils/topology.ts +0 -79
- package/test/utils/utils.ts +0 -268
- package/tsconfig.browser.json +0 -17
- package/tsconfig.jest.json +0 -25
- package/tsconfig.json +0 -3
- package/tsconfig.node.json +0 -24
package/src/identifiers.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { BrandedString, areEqualBinaries, binaryToHex, hexToBinary } from '@streamr/utils'
|
|
2
|
-
import crypto from 'crypto'
|
|
3
|
-
import { PeerDescriptor } from '../generated/packages/dht/protos/DhtRpc'
|
|
4
|
-
|
|
5
|
-
// https://www.scs.stanford.edu/~dm/home/papers/kpos.pdf
|
|
6
|
-
export const KADEMLIA_ID_LENGTH_IN_BYTES = 20
|
|
7
|
-
|
|
8
|
-
export type DhtAddress = BrandedString<'DhtAddress'>
|
|
9
|
-
export type DhtAddressRaw = Uint8Array
|
|
10
|
-
|
|
11
|
-
export const toDhtAddress = (raw: DhtAddressRaw): DhtAddress => {
|
|
12
|
-
return binaryToHex(raw) as unknown as DhtAddress
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const toDhtAddressRaw = (address: DhtAddress): DhtAddressRaw => {
|
|
16
|
-
return hexToBinary(address) as unknown as DhtAddressRaw
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export const toNodeId = (peerDescriptor: PeerDescriptor): DhtAddress => {
|
|
20
|
-
return toDhtAddress(peerDescriptor.nodeId)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const areEqualPeerDescriptors = (peerDescriptor1: PeerDescriptor, peerDescriptor2: PeerDescriptor): boolean => {
|
|
24
|
-
return areEqualBinaries(peerDescriptor1.nodeId, peerDescriptor2.nodeId)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const randomDhtAddress = (): DhtAddress => {
|
|
28
|
-
return toDhtAddress(crypto.randomBytes(KADEMLIA_ID_LENGTH_IN_BYTES))
|
|
29
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { ProtoCallContext } from '@streamr/proto-rpc'
|
|
2
|
-
import { PeerDescriptor } from '../../generated/packages/dht/protos/DhtRpc'
|
|
3
|
-
import { DhtRpcOptions } from './DhtRpcOptions'
|
|
4
|
-
|
|
5
|
-
export class DhtCallContext extends ProtoCallContext implements DhtRpcOptions {
|
|
6
|
-
// used by client
|
|
7
|
-
targetDescriptor?: PeerDescriptor
|
|
8
|
-
sourceDescriptor?: PeerDescriptor
|
|
9
|
-
clientId?: number
|
|
10
|
-
connect?: boolean
|
|
11
|
-
sendIfStopped?: boolean
|
|
12
|
-
//used in incoming calls
|
|
13
|
-
incomingSourceDescriptor?: PeerDescriptor
|
|
14
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ProtoRpcOptions } from '@streamr/proto-rpc'
|
|
2
|
-
import { PeerDescriptor } from '../../generated/packages/dht/protos/DhtRpc'
|
|
3
|
-
|
|
4
|
-
export interface DhtRpcOptions extends ProtoRpcOptions {
|
|
5
|
-
targetDescriptor?: PeerDescriptor
|
|
6
|
-
sourceDescriptor?: PeerDescriptor
|
|
7
|
-
clientId?: number
|
|
8
|
-
connect?: boolean
|
|
9
|
-
sendIfStopped?: boolean
|
|
10
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { Message, PeerDescriptor } from '../../generated/packages/dht/protos/DhtRpc'
|
|
2
|
-
|
|
3
|
-
export interface TransportEvents {
|
|
4
|
-
disconnected: (peerDescriptor: PeerDescriptor, gracefulLeave: boolean) => void
|
|
5
|
-
message: (message: Message) => void
|
|
6
|
-
connected: (peerDescriptor: PeerDescriptor) => void
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface SendOptions {
|
|
10
|
-
connect: boolean
|
|
11
|
-
sendIfStopped: boolean
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const DEFAULT_SEND_OPTIONS = {
|
|
15
|
-
connect: true,
|
|
16
|
-
sendIfStopped: false
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface ITransport {
|
|
20
|
-
on<T extends keyof TransportEvents>(eventName: T, listener: (message: Message) => void): void
|
|
21
|
-
on<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor) => void): void
|
|
22
|
-
on<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor, gracefulLeave: boolean) => void): void
|
|
23
|
-
|
|
24
|
-
once<T extends keyof TransportEvents>(eventName: T, listener: (message: Message) => void): void
|
|
25
|
-
once<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor) => void): void
|
|
26
|
-
once<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor,
|
|
27
|
-
gracefulLeave: boolean) => void): void
|
|
28
|
-
|
|
29
|
-
off<T extends keyof TransportEvents>(eventName: T, listener: (message: Message) => void): void
|
|
30
|
-
off<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor) => void): void
|
|
31
|
-
off<T extends keyof TransportEvents>(eventName: T, listener: (peerDescriptor: PeerDescriptor, gracefulLeave: boolean) => void): void
|
|
32
|
-
|
|
33
|
-
send(msg: Message, opts?: SendOptions): Promise<void>
|
|
34
|
-
getLocalPeerDescriptor(): PeerDescriptor
|
|
35
|
-
stop(): void | Promise<void>
|
|
36
|
-
getDiagnosticInfo(): Record<string, unknown>
|
|
37
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { ITransport } from './ITransport'
|
|
2
|
-
import { RoutingRpcCommunicator } from './RoutingRpcCommunicator'
|
|
3
|
-
import { RpcCommunicatorOptions, RpcError } from '@streamr/proto-rpc'
|
|
4
|
-
import { Message, PeerDescriptor } from '../../generated/packages/dht/protos/DhtRpc'
|
|
5
|
-
import { ServiceID } from '../types/ServiceID'
|
|
6
|
-
import { areEqualPeerDescriptors } from '../identifiers'
|
|
7
|
-
|
|
8
|
-
export class ListeningRpcCommunicator extends RoutingRpcCommunicator {
|
|
9
|
-
private readonly transport: ITransport
|
|
10
|
-
private readonly messageListener: (msg: Message) => void
|
|
11
|
-
private readonly disconnectedListener: (peerDescriptor: PeerDescriptor) => void
|
|
12
|
-
|
|
13
|
-
constructor(ownServiceId: ServiceID, transport: ITransport, options?: RpcCommunicatorOptions) {
|
|
14
|
-
super(ownServiceId, (msg, opts) => transport.send(msg, opts), options)
|
|
15
|
-
this.messageListener = (msg: Message) => {
|
|
16
|
-
this.handleMessageFromPeer(msg)
|
|
17
|
-
}
|
|
18
|
-
this.disconnectedListener = (peerDescriptor: PeerDescriptor) => {
|
|
19
|
-
const requests = this.getRequestIds((request) => areEqualPeerDescriptors(peerDescriptor, request.getCallContext().targetDescriptor!))
|
|
20
|
-
requests.forEach((id) => this.handleClientError(id, new RpcError.Disconnected('Peer disconnected')))
|
|
21
|
-
}
|
|
22
|
-
this.transport = transport
|
|
23
|
-
transport.on('message', this.messageListener)
|
|
24
|
-
transport.on('disconnected', this.disconnectedListener)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
destroy(): void {
|
|
28
|
-
this.transport.off('message', this.messageListener)
|
|
29
|
-
this.transport.off('disconnected', this.disconnectedListener)
|
|
30
|
-
this.stop()
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { Message, PeerDescriptor } from '../../generated/packages/dht/protos/DhtRpc'
|
|
2
|
-
import { v4 } from 'uuid'
|
|
3
|
-
import { RpcCommunicator, RpcCommunicatorOptions } from '@streamr/proto-rpc'
|
|
4
|
-
import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
|
|
5
|
-
import { RpcMessage } from '../../generated/packages/proto-rpc/protos/ProtoRpc'
|
|
6
|
-
import { ServiceID } from '../types/ServiceID'
|
|
7
|
-
import { DEFAULT_SEND_OPTIONS, SendOptions } from './ITransport'
|
|
8
|
-
|
|
9
|
-
export class RoutingRpcCommunicator extends RpcCommunicator<DhtCallContext> {
|
|
10
|
-
private ownServiceId: ServiceID
|
|
11
|
-
private sendFn: (msg: Message, opts: SendOptions) => Promise<void>
|
|
12
|
-
|
|
13
|
-
constructor(
|
|
14
|
-
ownServiceId: ServiceID,
|
|
15
|
-
sendFn: (msg: Message, opts: SendOptions) => Promise<void>,
|
|
16
|
-
options?: RpcCommunicatorOptions
|
|
17
|
-
) {
|
|
18
|
-
super(options)
|
|
19
|
-
this.ownServiceId = ownServiceId
|
|
20
|
-
this.sendFn = sendFn
|
|
21
|
-
|
|
22
|
-
this.setOutgoingMessageListener((msg: RpcMessage, _requestId: string, callContext?: DhtCallContext) => {
|
|
23
|
-
let targetDescriptor: PeerDescriptor
|
|
24
|
-
// rpc call message
|
|
25
|
-
if (callContext!.targetDescriptor) {
|
|
26
|
-
targetDescriptor = callContext!.targetDescriptor!
|
|
27
|
-
} else { // rpc reply message
|
|
28
|
-
targetDescriptor = callContext!.incomingSourceDescriptor!
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const message: Message = {
|
|
32
|
-
messageId: v4(),
|
|
33
|
-
serviceId: this.ownServiceId,
|
|
34
|
-
body: {
|
|
35
|
-
oneofKind: 'rpcMessage',
|
|
36
|
-
rpcMessage: msg
|
|
37
|
-
},
|
|
38
|
-
targetDescriptor
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// TODO maybe sendOptions could be a separate block inside callContext
|
|
42
|
-
const sendOpts = (msg.header.response !== undefined)
|
|
43
|
-
? {
|
|
44
|
-
// typically we already have a connection, but if it has disconnected for some reason
|
|
45
|
-
// the receiver could have gone offline (or it is no longer a neighbor) and therefore there
|
|
46
|
-
// is no point in trying form a new connection
|
|
47
|
-
connect: false,
|
|
48
|
-
// TODO maybe this options could be removed?
|
|
49
|
-
sendIfStopped: true
|
|
50
|
-
} : {
|
|
51
|
-
connect: callContext?.connect ?? DEFAULT_SEND_OPTIONS.connect,
|
|
52
|
-
sendIfStopped: callContext?.sendIfStopped ?? DEFAULT_SEND_OPTIONS.sendIfStopped
|
|
53
|
-
}
|
|
54
|
-
return this.sendFn(message, sendOpts)
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public handleMessageFromPeer(message: Message): void {
|
|
59
|
-
if (message.serviceId === this.ownServiceId && message.body.oneofKind === 'rpcMessage') {
|
|
60
|
-
const context = new DhtCallContext()
|
|
61
|
-
context.incomingSourceDescriptor = message.sourceDescriptor
|
|
62
|
-
// TODO should we have some handling for this floating promise?
|
|
63
|
-
this.handleIncomingMessage(message.body.rpcMessage, context)
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
package/src/types/ServiceID.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type ServiceID = string
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import { LatencyType, Simulator } from '../../src/connection/simulator/Simulator'
|
|
3
|
-
import { DhtNode } from '../../src/dht/DhtNode'
|
|
4
|
-
import { createMockConnectionDhtNode } from '../utils/utils'
|
|
5
|
-
import { execSync } from 'child_process'
|
|
6
|
-
import fs from 'fs'
|
|
7
|
-
import { Logger, wait } from '@streamr/utils'
|
|
8
|
-
import { debugVars } from '../../src/helpers/debugHelpers'
|
|
9
|
-
import { toDhtAddress, toNodeId } from '../../src/identifiers'
|
|
10
|
-
|
|
11
|
-
const logger = new Logger(module)
|
|
12
|
-
|
|
13
|
-
describe('Find correctness', () => {
|
|
14
|
-
let entryPoint: DhtNode
|
|
15
|
-
let nodes: DhtNode[]
|
|
16
|
-
const simulator = new Simulator(LatencyType.NONE)
|
|
17
|
-
const NUM_NODES = 1000
|
|
18
|
-
|
|
19
|
-
if (!fs.existsSync('test/data/nodeids.json')) {
|
|
20
|
-
console.log('ground truth data does not exist yet, generating..')
|
|
21
|
-
execSync('npm run prepare-kademlia-simulation')
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const dhtIds: { type: string, data: number[] }[] = JSON.parse(fs.readFileSync('test/data/nodeids.json').toString())
|
|
25
|
-
|
|
26
|
-
beforeEach(async () => {
|
|
27
|
-
|
|
28
|
-
nodes = []
|
|
29
|
-
entryPoint = await createMockConnectionDhtNode(simulator, toDhtAddress(Uint8Array.from(dhtIds[0].data)), undefined)
|
|
30
|
-
|
|
31
|
-
for (let i = 1; i < NUM_NODES; i++) {
|
|
32
|
-
const node = await createMockConnectionDhtNode(simulator, toDhtAddress(Uint8Array.from(dhtIds[i].data)), undefined)
|
|
33
|
-
nodes.push(node)
|
|
34
|
-
}
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
afterEach(async () => {
|
|
38
|
-
await Promise.all([
|
|
39
|
-
entryPoint.stop(),
|
|
40
|
-
...nodes.map(async (node) => await node.stop())
|
|
41
|
-
])
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
it('Entrypoint can find a node from the network (exact match)', async () => {
|
|
45
|
-
await entryPoint.joinDht([entryPoint.getLocalPeerDescriptor()])
|
|
46
|
-
|
|
47
|
-
await Promise.all(
|
|
48
|
-
nodes.map((node) => node.joinDht([entryPoint.getLocalPeerDescriptor()]))
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
logger.info('waiting 120s')
|
|
52
|
-
debugVars.waiting = true
|
|
53
|
-
|
|
54
|
-
await wait(120000)
|
|
55
|
-
debugVars.waiting = false
|
|
56
|
-
logger.info('waiting over')
|
|
57
|
-
|
|
58
|
-
nodes.forEach((node) => logger.info(toNodeId(node.getLocalPeerDescriptor()) + ': connections:' +
|
|
59
|
-
node.getConnectionsView().getConnectionCount() + ', kbucket: ' + node.getNeighborCount()
|
|
60
|
-
+ ', localLocked: ' + node.getLocalLockedConnectionCount()
|
|
61
|
-
+ ', remoteLocked: ' + node.getRemoteLockedConnectionCount()
|
|
62
|
-
+ ', weakLocked: ' + node.getWeakLockedConnectionCount()))
|
|
63
|
-
|
|
64
|
-
logger.info('starting find')
|
|
65
|
-
const targetId = Uint8Array.from(dhtIds[9].data)
|
|
66
|
-
const closestNodes = await nodes[159].findClosestNodesFromDht(toDhtAddress(targetId))
|
|
67
|
-
logger.info('find over')
|
|
68
|
-
expect(closestNodes).toBeGreaterThanOrEqual(5)
|
|
69
|
-
expect(toDhtAddress(targetId)).toEqual(toNodeId(closestNodes[0]))
|
|
70
|
-
|
|
71
|
-
}, 180000)
|
|
72
|
-
})
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import { Simulator } from '../../src/connection/simulator/Simulator'
|
|
3
|
-
import { DhtNode } from '../../src/dht/DhtNode'
|
|
4
|
-
import { createMockConnectionDhtNode } from '../utils/utils'
|
|
5
|
-
import { execSync } from 'child_process'
|
|
6
|
-
import fs from 'fs'
|
|
7
|
-
import { DhtAddress, toDhtAddress, toNodeId } from '../../src/identifiers'
|
|
8
|
-
import { Logger } from '@streamr/utils'
|
|
9
|
-
|
|
10
|
-
const logger = new Logger(module)
|
|
11
|
-
|
|
12
|
-
describe('Kademlia correctness', () => {
|
|
13
|
-
let entryPoint: DhtNode
|
|
14
|
-
let nodes: DhtNode[]
|
|
15
|
-
const simulator = new Simulator()
|
|
16
|
-
const NUM_NODES = 200
|
|
17
|
-
|
|
18
|
-
const nodeIndicesById: Record<DhtAddress, number> = {}
|
|
19
|
-
|
|
20
|
-
if (!fs.existsSync('test/data/nodeids.json')) {
|
|
21
|
-
console.log('gound truth data does not exist yet, generating..')
|
|
22
|
-
execSync('npm run prepare-kademlia-simulation')
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const dhtIds: { type: string, data: number[] }[] = JSON.parse(fs.readFileSync('test/data/nodeids.json').toString())
|
|
26
|
-
const groundTruth: Record<string, { name: string, distance: number, id: { type: string, data: number[] } }[]>
|
|
27
|
-
= JSON.parse(fs.readFileSync('test/data/orderedneighbors.json').toString())
|
|
28
|
-
|
|
29
|
-
beforeEach(async () => {
|
|
30
|
-
nodes = []
|
|
31
|
-
entryPoint = await createMockConnectionDhtNode(simulator, toDhtAddress(Uint8Array.from(dhtIds[0].data)), 8)
|
|
32
|
-
nodes.push(entryPoint)
|
|
33
|
-
nodeIndicesById[entryPoint.getNodeId()] = 0
|
|
34
|
-
|
|
35
|
-
for (let i = 1; i < NUM_NODES; i++) {
|
|
36
|
-
const node = await createMockConnectionDhtNode(simulator, toDhtAddress(Uint8Array.from(dhtIds[i].data)))
|
|
37
|
-
nodeIndicesById[node.getNodeId()] = i
|
|
38
|
-
nodes.push(node)
|
|
39
|
-
}
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
afterEach(async () => {
|
|
43
|
-
await Promise.all([
|
|
44
|
-
entryPoint.stop(),
|
|
45
|
-
...nodes.map((node) => node.stop())
|
|
46
|
-
])
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('Can find correct neighbors', async () => {
|
|
50
|
-
await entryPoint.joinDht([entryPoint.getLocalPeerDescriptor()])
|
|
51
|
-
|
|
52
|
-
for (let i = 1; i < NUM_NODES; i++) {
|
|
53
|
-
// time to join the network
|
|
54
|
-
const startTimestamp = Date.now()
|
|
55
|
-
await nodes[i].joinDht([entryPoint.getLocalPeerDescriptor()])
|
|
56
|
-
const endTimestamp = Date.now()
|
|
57
|
-
logger.info('Node ' + i + ' joined in ' + (endTimestamp - startTimestamp) + ' ms')
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
let minimumCorrectNeighbors = Number.MAX_SAFE_INTEGER
|
|
61
|
-
let sumCorrectNeighbors = 0
|
|
62
|
-
let sumKbucketSize = 1
|
|
63
|
-
|
|
64
|
-
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
65
|
-
let groundTruthString = 'groundTruthNeighb: '
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
|
67
|
-
for (let j = 0; j < groundTruth[i + ''].length; j++) {
|
|
68
|
-
groundTruthString += groundTruth[i + ''][j].name + ','
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const kademliaNeighbors = nodes[i].getClosestContacts(8).map((p) => toNodeId(p))
|
|
72
|
-
|
|
73
|
-
let kadString = 'kademliaNeighbors: '
|
|
74
|
-
kademliaNeighbors.forEach((neighbor) => {
|
|
75
|
-
kadString += nodeIndicesById[neighbor] + ','
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
let correctNeighbors = 0
|
|
79
|
-
try {
|
|
80
|
-
for (let j = 0; j < groundTruth[i + ''].length; j++) {
|
|
81
|
-
if (groundTruth[i + ''][j].name != (nodeIndicesById[kademliaNeighbors[j]] + '')) {
|
|
82
|
-
break
|
|
83
|
-
}
|
|
84
|
-
correctNeighbors++
|
|
85
|
-
}
|
|
86
|
-
} catch {
|
|
87
|
-
console.error('Node ' + toNodeId(nodes[i].getLocalPeerDescriptor()) + ' had only '
|
|
88
|
-
+ kademliaNeighbors.length + ' kademlia neighbors')
|
|
89
|
-
}
|
|
90
|
-
if (correctNeighbors === 0) {
|
|
91
|
-
console.log('No correct neighbors found for node ' + i)
|
|
92
|
-
console.log(groundTruthString)
|
|
93
|
-
console.log(kadString)
|
|
94
|
-
}
|
|
95
|
-
if (correctNeighbors < minimumCorrectNeighbors) {
|
|
96
|
-
console.log('NEW MIN', i, correctNeighbors)
|
|
97
|
-
minimumCorrectNeighbors = correctNeighbors
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (i > 0) {
|
|
101
|
-
sumKbucketSize += nodes[i].getNeighborCount()
|
|
102
|
-
sumCorrectNeighbors += correctNeighbors
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const avgKbucketSize = sumKbucketSize / (NUM_NODES - 1)
|
|
107
|
-
const avgCorrectNeighbors = sumCorrectNeighbors / (NUM_NODES - 1)
|
|
108
|
-
|
|
109
|
-
console.log('----------- Simulation results ------------------')
|
|
110
|
-
console.log('Minimum correct neighbors: ' + minimumCorrectNeighbors)
|
|
111
|
-
console.log('Average correct neighbors: ' + avgCorrectNeighbors)
|
|
112
|
-
console.log('Average Kbucket size: ' + avgKbucketSize)
|
|
113
|
-
}, 120000)
|
|
114
|
-
})
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import { Simulator } from '../../src/connection/simulator/Simulator'
|
|
3
|
-
import { DhtNode } from '../../src/dht/DhtNode'
|
|
4
|
-
import { createMockRingNode } from '../utils/utils'
|
|
5
|
-
import { execSync } from 'child_process'
|
|
6
|
-
import fs from 'fs'
|
|
7
|
-
import { DhtAddress, toDhtAddress, toNodeId } from '../../src/identifiers'
|
|
8
|
-
import { Logger } from '@streamr/utils'
|
|
9
|
-
import { getRingIdRawFromPeerDescriptor } from '../../src/dht/contact/ringIdentifiers'
|
|
10
|
-
|
|
11
|
-
const logger = new Logger(module)
|
|
12
|
-
|
|
13
|
-
describe('Ring correctness', () => {
|
|
14
|
-
let entryPoint: DhtNode
|
|
15
|
-
let nodes: DhtNode[]
|
|
16
|
-
const simulator = new Simulator()
|
|
17
|
-
|
|
18
|
-
const NUM_NODES = 900
|
|
19
|
-
const nodeIndicesById: Record<DhtAddress, number> = {}
|
|
20
|
-
|
|
21
|
-
const regions: number[] = []
|
|
22
|
-
for (let i = 0; i < (NUM_NODES + 1); i++) {
|
|
23
|
-
regions.push(i)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Shuffle the regions
|
|
27
|
-
for (let i = regions.length - 1; i > 0; i--) {
|
|
28
|
-
const j = Math.floor(Math.random() * (i + 1))
|
|
29
|
-
const temp = regions[i]
|
|
30
|
-
regions[i] = regions[j]
|
|
31
|
-
regions[j] = temp
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (!fs.existsSync('test/data/nodeids.json')) {
|
|
35
|
-
console.log('gound truth data does not exist yet, generating..')
|
|
36
|
-
execSync('npm run prepare-kademlia-simulation')
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const dhtIds: { type: string, data: number[] }[] = JSON.parse(fs.readFileSync('test/data/nodeids.json').toString())
|
|
40
|
-
const groundTruth: Record<string, { name: string, distance: number, id: { type: string, data: number[] } }[]>
|
|
41
|
-
= JSON.parse(fs.readFileSync('test/data/orderedneighbors.json').toString())
|
|
42
|
-
|
|
43
|
-
beforeEach(async () => {
|
|
44
|
-
jest.setTimeout(60000)
|
|
45
|
-
nodes = []
|
|
46
|
-
entryPoint = await createMockRingNode(simulator, toDhtAddress(Uint8Array.from(dhtIds[0].data)), regions[0])
|
|
47
|
-
nodes.push(entryPoint)
|
|
48
|
-
nodeIndicesById[entryPoint.getNodeId()] = 0
|
|
49
|
-
|
|
50
|
-
for (let i = 1; i < NUM_NODES; i++) {
|
|
51
|
-
const node = await createMockRingNode(simulator, toDhtAddress(Uint8Array.from(dhtIds[i].data)), regions[i + 1])
|
|
52
|
-
nodeIndicesById[node.getNodeId()] = i
|
|
53
|
-
nodes.push(node)
|
|
54
|
-
}
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
afterEach(async () => {
|
|
58
|
-
await Promise.all([
|
|
59
|
-
entryPoint.stop(),
|
|
60
|
-
...nodes.map((node) => node.stop())
|
|
61
|
-
])
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
it('Can find correct neighbors', async () => {
|
|
65
|
-
await entryPoint.joinDht([entryPoint.getLocalPeerDescriptor()])
|
|
66
|
-
|
|
67
|
-
//await Promise.all(
|
|
68
|
-
// nodes.map((node) => node.joinDht([entryPoint.getLocalPeerDescriptor()]))
|
|
69
|
-
//)
|
|
70
|
-
|
|
71
|
-
for (let i = 1; i < NUM_NODES; i++) {
|
|
72
|
-
// time to join the network
|
|
73
|
-
const startTimestamp = Date.now()
|
|
74
|
-
await nodes[i].joinDht([entryPoint.getLocalPeerDescriptor()])
|
|
75
|
-
const endTimestamp = Date.now()
|
|
76
|
-
logger.info('Node ' + i + ' joined in ' + (endTimestamp - startTimestamp) + ' ms')
|
|
77
|
-
const ringStartTimestamp = Date.now()
|
|
78
|
-
await nodes[i].joinRing()
|
|
79
|
-
const ringEndTimestamp = Date.now()
|
|
80
|
-
logger.info('Node ' + i + ' joined ring in ' + (ringEndTimestamp - ringStartTimestamp) + ' ms')
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/*
|
|
85
|
-
for (let i = 1; i < NUM_NODES; i++) {
|
|
86
|
-
// time to join the network
|
|
87
|
-
const ringStartTimestamp = Date.now()
|
|
88
|
-
await nodes[i].joinRing()
|
|
89
|
-
const ringEndTimestamp = Date.now()
|
|
90
|
-
logger.info('Node ' + i + ' joined ring in ' + (ringEndTimestamp - ringStartTimestamp) + ' ms')
|
|
91
|
-
}*/
|
|
92
|
-
|
|
93
|
-
for (let i = 1; i < NUM_NODES; i++) {
|
|
94
|
-
logger.info('Node ' + i + ', own region: ' + nodes[i].getLocalPeerDescriptor().region
|
|
95
|
-
+ '. Regions of closest ring peers, left: '
|
|
96
|
-
+ nodes[i].getClosestRingContactsTo(
|
|
97
|
-
getRingIdRawFromPeerDescriptor(nodes[i].getLocalPeerDescriptor()), 10).left.map((p) => p.region)
|
|
98
|
-
+ ', right: ' + nodes[i].getClosestRingContactsTo(getRingIdRawFromPeerDescriptor(
|
|
99
|
-
nodes[i].getLocalPeerDescriptor()), 10).right.map((p) => p.region)
|
|
100
|
-
)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
let minimumCorrectNeighbors = Number.MAX_SAFE_INTEGER
|
|
104
|
-
let sumCorrectNeighbors = 0
|
|
105
|
-
let sumKbucketSize = 1
|
|
106
|
-
|
|
107
|
-
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
108
|
-
let groundTruthString = 'groundTruthNeighb: '
|
|
109
|
-
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
|
110
|
-
for (let j = 0; j < groundTruth[i + ''].length; j++) {
|
|
111
|
-
groundTruthString += groundTruth[i + ''][j].name + ','
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const kademliaNeighbors = nodes[i].getClosestContacts(8).map((p) => toNodeId(p))
|
|
115
|
-
|
|
116
|
-
let kadString = 'kademliaNeighbors: '
|
|
117
|
-
kademliaNeighbors.forEach((neighbor) => {
|
|
118
|
-
kadString += nodeIndicesById[neighbor] + ','
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
let correctNeighbors = 0
|
|
122
|
-
try {
|
|
123
|
-
for (let j = 0; j < groundTruth[i + ''].length; j++) {
|
|
124
|
-
if (groundTruth[i + ''][j].name != (nodeIndicesById[kademliaNeighbors[j]] + '')) {
|
|
125
|
-
break
|
|
126
|
-
}
|
|
127
|
-
correctNeighbors++
|
|
128
|
-
}
|
|
129
|
-
} catch {
|
|
130
|
-
console.error('Node ' + toNodeId(nodes[i].getLocalPeerDescriptor()) + ' had only '
|
|
131
|
-
+ kademliaNeighbors.length + ' kademlia neighbors')
|
|
132
|
-
}
|
|
133
|
-
if (correctNeighbors === 0) {
|
|
134
|
-
console.log('No correct neighbors found for node ' + i)
|
|
135
|
-
console.log(groundTruthString)
|
|
136
|
-
console.log(kadString)
|
|
137
|
-
}
|
|
138
|
-
if (correctNeighbors < minimumCorrectNeighbors) {
|
|
139
|
-
console.log('NEW MIN', i, correctNeighbors)
|
|
140
|
-
minimumCorrectNeighbors = correctNeighbors
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (i > 0) {
|
|
144
|
-
sumKbucketSize += nodes[i].getNeighborCount()
|
|
145
|
-
sumCorrectNeighbors += correctNeighbors
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const avgKbucketSize = sumKbucketSize / (NUM_NODES - 1)
|
|
150
|
-
const avgCorrectNeighbors = sumCorrectNeighbors / (NUM_NODES - 1)
|
|
151
|
-
|
|
152
|
-
console.log('----------- Simulation results ------------------')
|
|
153
|
-
console.log('Minimum correct neighbors: ' + minimumCorrectNeighbors)
|
|
154
|
-
console.log('Average correct neighbors: ' + avgCorrectNeighbors)
|
|
155
|
-
console.log('Average Kbucket size: ' + avgKbucketSize)
|
|
156
|
-
}, 240000)
|
|
157
|
-
})
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
|
|
3
|
-
import KBucket from 'k-bucket'
|
|
4
|
-
import { SortedContactList } from '../../src/dht/contact/SortedContactList'
|
|
5
|
-
import { DhtAddress, DhtAddressRaw, randomDhtAddress, toDhtAddressRaw } from '../../src/identifiers'
|
|
6
|
-
|
|
7
|
-
const NUM_ADDS = 1000
|
|
8
|
-
|
|
9
|
-
interface Item {
|
|
10
|
-
id: DhtAddressRaw
|
|
11
|
-
vectorClock: number
|
|
12
|
-
getNodeId: () => DhtAddress
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const createRandomItem = (index: number): Item => {
|
|
16
|
-
const nodeId = randomDhtAddress()
|
|
17
|
-
const nodeIdRaw = toDhtAddressRaw(nodeId)
|
|
18
|
-
return {
|
|
19
|
-
getNodeId: () => nodeId,
|
|
20
|
-
id: nodeIdRaw,
|
|
21
|
-
vectorClock: index
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function shuffleArray<T>(array: T[]): T[] {
|
|
26
|
-
for (let i = array.length - 1; i > 0; i--) {
|
|
27
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
28
|
-
[array[i], array[j]] = [array[j], array[i]]
|
|
29
|
-
}
|
|
30
|
-
return array
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
describe('SortedContactListBenchmark', () => {
|
|
34
|
-
|
|
35
|
-
it('adds ' + NUM_ADDS + ' random nodeIds', async () => {
|
|
36
|
-
const randomIds = []
|
|
37
|
-
for (let i = 0; i < NUM_ADDS; i++) {
|
|
38
|
-
randomIds.push(createRandomItem(i))
|
|
39
|
-
}
|
|
40
|
-
const list = new SortedContactList({
|
|
41
|
-
referenceId: randomDhtAddress(),
|
|
42
|
-
allowToContainReferenceId: true
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
console.time('SortedContactList.addContact()')
|
|
46
|
-
for (let i = 0; i < NUM_ADDS; i++) {
|
|
47
|
-
list.addContact(randomIds[i])
|
|
48
|
-
}
|
|
49
|
-
console.timeEnd('SortedContactList.addContact()')
|
|
50
|
-
|
|
51
|
-
const kBucket = new KBucket<Item>({ localNodeId: toDhtAddressRaw(randomDhtAddress()) })
|
|
52
|
-
console.time('KBucket.add()')
|
|
53
|
-
for (let i = 0; i < NUM_ADDS; i++) {
|
|
54
|
-
kBucket.add(randomIds[i])
|
|
55
|
-
}
|
|
56
|
-
console.timeEnd('KBucket.add()')
|
|
57
|
-
|
|
58
|
-
console.time('kBucket toArray()')
|
|
59
|
-
|
|
60
|
-
for (let i = 0; i < NUM_ADDS; i++) {
|
|
61
|
-
kBucket.toArray()
|
|
62
|
-
}
|
|
63
|
-
console.timeEnd('kBucket toArray()')
|
|
64
|
-
|
|
65
|
-
console.time('kBucket closest()')
|
|
66
|
-
for (let i = 0; i < NUM_ADDS; i++) {
|
|
67
|
-
kBucket.closest(toDhtAddressRaw(randomDhtAddress()), 20)
|
|
68
|
-
}
|
|
69
|
-
console.timeEnd('kBucket closest()')
|
|
70
|
-
|
|
71
|
-
console.time('SortedContactList.getClosestContacts()')
|
|
72
|
-
for (let i = 0; i < NUM_ADDS; i++) {
|
|
73
|
-
const closest = new SortedContactList<Item>({
|
|
74
|
-
referenceId: randomDhtAddress(),
|
|
75
|
-
allowToContainReferenceId: true
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
const arrayFromBucket = kBucket.toArray()
|
|
79
|
-
arrayFromBucket.forEach((contact) => closest.addContact(contact))
|
|
80
|
-
closest.getClosestContacts(20)
|
|
81
|
-
}
|
|
82
|
-
console.timeEnd('SortedContactList.getClosestContacts()')
|
|
83
|
-
|
|
84
|
-
console.time('SortedContactList.getClosestContacts() and addContacts()')
|
|
85
|
-
for (let i = 0; i < NUM_ADDS; i++) {
|
|
86
|
-
const closest = new SortedContactList<Item>({
|
|
87
|
-
referenceId: randomDhtAddress(),
|
|
88
|
-
allowToContainReferenceId: true
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
const arrayFromBucket = kBucket.toArray()
|
|
92
|
-
closest.addContacts(arrayFromBucket)
|
|
93
|
-
closest.getClosestContacts(20)
|
|
94
|
-
}
|
|
95
|
-
console.timeEnd('SortedContactList.getClosestContacts() and addContacts()')
|
|
96
|
-
|
|
97
|
-
const shuffled = shuffleArray(kBucket.toArray())
|
|
98
|
-
console.time('kbucket add and closest')
|
|
99
|
-
for (let i = 0; i < NUM_ADDS; i++) {
|
|
100
|
-
const bucket2 = new KBucket<Item>({ localNodeId: toDhtAddressRaw(randomDhtAddress()) })
|
|
101
|
-
|
|
102
|
-
shuffled.forEach((contact) => bucket2.add(contact))
|
|
103
|
-
bucket2.closest(toDhtAddressRaw(randomDhtAddress()), 20)
|
|
104
|
-
}
|
|
105
|
-
console.timeEnd('kbucket add and closest')
|
|
106
|
-
|
|
107
|
-
})
|
|
108
|
-
})
|