@streamr/dht 100.0.0-testnet-two.1 → 100.0.0-testnet-two.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/package.json +5 -5
- 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/ConnectionManager.d.ts +5 -4
- package/dist/src/connection/ConnectionManager.js +32 -36
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +2 -2
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +21 -22
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.js +4 -3
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +11 -8
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js +1 -1
- 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 +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.js +6 -5
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +9 -8
- package/dist/src/dht/DhtNode.js +26 -22
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +2 -1
- package/dist/src/dht/DhtNodeRpcLocal.js +2 -1
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +6 -5
- package/dist/src/dht/DhtNodeRpcRemote.js +9 -7
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +3 -3
- package/dist/src/dht/ExternalApiRpcLocal.js +3 -2
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +3 -2
- package/dist/src/dht/ExternalApiRpcRemote.js +3 -2
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +9 -9
- package/dist/src/dht/PeerManager.js +35 -30
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/Contact.d.ts +2 -2
- package/dist/src/dht/contact/ContactList.d.ts +7 -7
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +4 -4
- package/dist/src/dht/contact/RandomContactList.js +2 -3
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/RpcRemote.d.ts +1 -4
- package/dist/src/dht/contact/RpcRemote.js +1 -5
- 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 +5 -5
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +3 -2
- package/dist/src/dht/discovery/DiscoverySession.js +9 -9
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +3 -1
- package/dist/src/dht/discovery/PeerDiscovery.js +18 -10
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +15 -14
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js +2 -2
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +2 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +6 -6
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +2 -2
- package/dist/src/dht/routing/Router.js +2 -2
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.js +3 -3
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcRemote.js +4 -2
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -3
- package/dist/src/dht/routing/RoutingSession.js +4 -4
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +6 -9
- package/dist/src/dht/store/LocalDataStore.js +27 -32
- package/dist/src/dht/store/LocalDataStore.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +3 -3
- package/dist/src/dht/store/StoreManager.js +26 -23
- package/dist/src/dht/store/StoreManager.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.d.ts +2 -1
- package/dist/src/dht/store/StoreRpcLocal.js +10 -8
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/exports.d.ts +2 -0
- package/dist/src/exports.js +6 -1
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/AddressTools.js +2 -0
- package/dist/src/helpers/AddressTools.js.map +1 -1
- package/dist/src/helpers/PeerID.d.ts +2 -2
- package/dist/src/helpers/PeerID.js +3 -3
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +2 -2
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +2 -3
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/identifiers.d.ts +6 -0
- package/dist/src/identifiers.js +23 -0
- package/dist/src/identifiers.js.map +1 -0
- package/package.json +5 -5
- package/src/connection/ConnectionLockHandler.ts +15 -15
- package/src/connection/ConnectionLockRpcLocal.ts +3 -3
- package/src/connection/ConnectionManager.ts +36 -46
- package/src/connection/ManagedConnection.ts +2 -2
- package/src/connection/simulator/Simulator.ts +2 -2
- package/src/connection/simulator/SimulatorConnection.ts +21 -23
- package/src/connection/simulator/SimulatorConnector.ts +6 -5
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -4
- package/src/connection/webrtc/NodeWebrtcConnection.ts +11 -10
- package/src/connection/webrtc/WebrtcConnector.ts +2 -3
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +2 -3
- package/src/connection/websocket/WebsocketConnector.ts +9 -9
- package/src/dht/DhtNode.ts +36 -35
- package/src/dht/DhtNodeRpcLocal.ts +3 -2
- package/src/dht/DhtNodeRpcRemote.ts +14 -12
- package/src/dht/ExternalApiRpcLocal.ts +15 -6
- package/src/dht/ExternalApiRpcRemote.ts +5 -4
- package/src/dht/PeerManager.ts +43 -38
- package/src/dht/contact/Contact.ts +2 -2
- package/src/dht/contact/ContactList.ts +7 -7
- package/src/dht/contact/RandomContactList.ts +6 -6
- package/src/dht/contact/RpcRemote.ts +0 -8
- package/src/dht/contact/SortedContactList.ts +22 -22
- package/src/dht/discovery/DiscoverySession.ts +16 -14
- package/src/dht/discovery/PeerDiscovery.ts +34 -12
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +17 -17
- package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +2 -2
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +11 -11
- package/src/dht/routing/Router.ts +5 -5
- package/src/dht/routing/RouterRpcLocal.ts +3 -3
- package/src/dht/routing/RouterRpcRemote.ts +4 -4
- package/src/dht/routing/RoutingSession.ts +6 -8
- package/src/dht/store/LocalDataStore.ts +31 -40
- package/src/dht/store/StoreManager.ts +31 -31
- package/src/dht/store/StoreRpcLocal.ts +11 -9
- package/src/exports.ts +2 -0
- package/src/helpers/AddressTools.ts +2 -0
- package/src/helpers/PeerID.ts +4 -4
- package/src/helpers/peerIdFromPeerDescriptor.ts +4 -6
- package/src/identifiers.ts +20 -0
- package/test/RandomGraphSimulation.ts +3 -2
- package/test/benchmark/Find.test.ts +4 -3
- package/test/benchmark/KademliaCorrectness.test.ts +4 -4
- package/test/benchmark/SortedContactListBenchmark.test.ts +15 -14
- package/test/benchmark/kademlia-simulation/Contact.ts +7 -8
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +2 -2
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +10 -11
- package/test/data/generateGroundTruthData.ts +5 -4
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +2 -2
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +1 -1
- package/test/end-to-end/memory-leak.test.ts +3 -2
- package/test/integration/ConnectionManager.test.ts +3 -3
- package/test/integration/DhtJoinPeerDiscovery.test.ts +2 -1
- package/test/integration/DhtNodeExternalAPI.test.ts +7 -7
- package/test/integration/DhtNodeRpcRemote.test.ts +3 -2
- package/test/integration/Find.test.ts +3 -3
- package/test/integration/Layer1-scale.test.ts +3 -3
- package/test/integration/Mock-Layer1-Layer0.test.ts +6 -5
- package/test/integration/ReplicateData.test.ts +38 -30
- package/test/integration/RouteMessage.test.ts +9 -10
- package/test/integration/RouterRpcRemote.test.ts +1 -1
- package/test/integration/ScaleDownDht.test.ts +4 -4
- package/test/integration/SimultaneousConnections.test.ts +7 -14
- package/test/integration/Store.test.ts +17 -7
- package/test/integration/StoreAndDelete.test.ts +11 -10
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +7 -6
- package/test/integration/StoreRpcRemote.test.ts +3 -5
- package/test/unit/AddressTools.test.ts +4 -0
- package/test/unit/LocalDataStore.test.ts +41 -41
- package/test/unit/PeerManager.test.ts +7 -8
- package/test/unit/RandomContactList.test.ts +3 -3
- package/test/unit/RecursiveOperationManager.test.ts +2 -1
- package/test/unit/RecursiveOperationSession.test.ts +2 -3
- package/test/unit/Router.test.ts +2 -2
- package/test/unit/RoutingSession.test.ts +2 -2
- package/test/unit/SortedContactList.test.ts +6 -6
- package/test/unit/StoreManager.test.ts +26 -23
- package/test/utils/customMatchers.ts +3 -2
- package/test/utils/mock/mockDataEntry.ts +8 -6
- package/test/utils/utils.ts +4 -4
- package/dist/src/helpers/nodeId.d.ts +0 -6
- package/dist/src/helpers/nodeId.js +0 -31
- package/dist/src/helpers/nodeId.js.map +0 -1
- package/src/helpers/nodeId.ts +0 -28
|
@@ -2,20 +2,15 @@ import { wait } from '@streamr/utils'
|
|
|
2
2
|
import { LocalDataStore } from '../../src/dht/store/LocalDataStore'
|
|
3
3
|
import {
|
|
4
4
|
getNodeIdFromPeerDescriptor,
|
|
5
|
-
peerIdFromPeerDescriptor
|
|
6
5
|
} from '../../src/helpers/peerIdFromPeerDescriptor'
|
|
7
6
|
import { createMockPeerDescriptor } from '../utils/utils'
|
|
8
|
-
import { createMockDataEntry
|
|
9
|
-
import {
|
|
7
|
+
import { createMockDataEntry } from '../utils/mock/mockDataEntry'
|
|
8
|
+
import { createRandomDhtAddress, getDhtAddressFromRaw } from '../../src/identifiers'
|
|
10
9
|
|
|
11
10
|
describe('LocalDataStore', () => {
|
|
12
11
|
|
|
13
12
|
let localDataStore: LocalDataStore
|
|
14
13
|
|
|
15
|
-
const getEntryArray = (key: Uint8Array) => {
|
|
16
|
-
return Array.from(localDataStore.getEntries(key).values())
|
|
17
|
-
}
|
|
18
|
-
|
|
19
14
|
beforeEach(() => {
|
|
20
15
|
localDataStore = new LocalDataStore(30 * 1000)
|
|
21
16
|
})
|
|
@@ -27,84 +22,89 @@ describe('LocalDataStore', () => {
|
|
|
27
22
|
it('can store', () => {
|
|
28
23
|
const storedEntry = createMockDataEntry()
|
|
29
24
|
localDataStore.storeEntry(storedEntry)
|
|
30
|
-
const fetchedEntries =
|
|
31
|
-
expect(fetchedEntries).
|
|
32
|
-
expectEqualData(fetchedEntries[0], storedEntry)
|
|
25
|
+
const fetchedEntries = Array.from(localDataStore.values(getDhtAddressFromRaw(storedEntry.key)))
|
|
26
|
+
expect(fetchedEntries).toIncludeSameMembers([storedEntry])
|
|
33
27
|
})
|
|
34
28
|
|
|
35
29
|
it('multiple storers behind one key', () => {
|
|
36
|
-
const creator1 =
|
|
37
|
-
const creator2 =
|
|
38
|
-
const key =
|
|
30
|
+
const creator1 = createRandomDhtAddress()
|
|
31
|
+
const creator2 = createRandomDhtAddress()
|
|
32
|
+
const key = createRandomDhtAddress()
|
|
39
33
|
const storedEntry1 = createMockDataEntry({ key, creator: creator1 })
|
|
40
34
|
const storedEntry2 = createMockDataEntry({ key, creator: creator2 })
|
|
41
35
|
localDataStore.storeEntry(storedEntry1)
|
|
42
36
|
localDataStore.storeEntry(storedEntry2)
|
|
43
|
-
const fetchedEntries = localDataStore.
|
|
44
|
-
expect(fetchedEntries
|
|
45
|
-
expectEqualData(fetchedEntries.get(getNodeIdFromBinary(creator1))!, storedEntry1)
|
|
46
|
-
expectEqualData(fetchedEntries.get(getNodeIdFromBinary(creator2))!, storedEntry2)
|
|
37
|
+
const fetchedEntries = Array.from(localDataStore.values(key))
|
|
38
|
+
expect(fetchedEntries).toIncludeSameMembers([storedEntry1, storedEntry2])
|
|
47
39
|
})
|
|
48
40
|
|
|
49
41
|
it('can remove data entries', () => {
|
|
50
|
-
const creator1 =
|
|
51
|
-
const creator2 =
|
|
52
|
-
const key =
|
|
42
|
+
const creator1 = createRandomDhtAddress()
|
|
43
|
+
const creator2 = createRandomDhtAddress()
|
|
44
|
+
const key = createRandomDhtAddress()
|
|
53
45
|
const storedEntry1 = createMockDataEntry({ key, creator: creator1 })
|
|
54
46
|
const storedEntry2 = createMockDataEntry({ key, creator: creator2 })
|
|
55
47
|
localDataStore.storeEntry(storedEntry1)
|
|
56
48
|
localDataStore.storeEntry(storedEntry2)
|
|
57
|
-
localDataStore.deleteEntry(key,
|
|
58
|
-
const fetchedEntries =
|
|
59
|
-
expect(fetchedEntries).
|
|
60
|
-
expectEqualData(fetchedEntries[0], storedEntry2)
|
|
49
|
+
localDataStore.deleteEntry(key, creator1)
|
|
50
|
+
const fetchedEntries = Array.from(localDataStore.values(key))
|
|
51
|
+
expect(fetchedEntries).toIncludeSameMembers([storedEntry2])
|
|
61
52
|
})
|
|
62
53
|
|
|
63
54
|
it('can remove all data entries', () => {
|
|
64
|
-
const creator1 =
|
|
65
|
-
const creator2 =
|
|
66
|
-
const key =
|
|
55
|
+
const creator1 = createRandomDhtAddress()
|
|
56
|
+
const creator2 = createRandomDhtAddress()
|
|
57
|
+
const key = createRandomDhtAddress()
|
|
67
58
|
const storedEntry1 = createMockDataEntry({ key, creator: creator1 })
|
|
68
59
|
const storedEntry2 = createMockDataEntry({ key, creator: creator2 })
|
|
69
60
|
localDataStore.storeEntry(storedEntry1)
|
|
70
61
|
localDataStore.storeEntry(storedEntry2)
|
|
71
|
-
localDataStore.deleteEntry(key,
|
|
72
|
-
localDataStore.deleteEntry(key,
|
|
73
|
-
expect(
|
|
62
|
+
localDataStore.deleteEntry(key, creator1)
|
|
63
|
+
localDataStore.deleteEntry(key, creator2)
|
|
64
|
+
expect(Array.from(localDataStore.values(key))).toHaveLength(0)
|
|
74
65
|
})
|
|
75
66
|
|
|
76
67
|
it('data is deleted after TTL', async () => {
|
|
77
68
|
const storedEntry = createMockDataEntry({ ttl: 1000 })
|
|
78
69
|
localDataStore.storeEntry(storedEntry)
|
|
79
|
-
expect(
|
|
70
|
+
expect(Array.from(localDataStore.values(getDhtAddressFromRaw(storedEntry.key)))).toHaveLength(1)
|
|
80
71
|
await wait(1100)
|
|
81
|
-
expect(
|
|
72
|
+
expect(Array.from(localDataStore.values(getDhtAddressFromRaw(storedEntry.key)))).toHaveLength(0)
|
|
82
73
|
})
|
|
83
74
|
|
|
84
75
|
describe('mark data as deleted', () => {
|
|
85
76
|
|
|
86
77
|
it('happy path', () => {
|
|
87
|
-
const creator1 =
|
|
78
|
+
const creator1 = createRandomDhtAddress()
|
|
88
79
|
const storedEntry = createMockDataEntry({ creator: creator1 })
|
|
89
80
|
localDataStore.storeEntry(storedEntry)
|
|
90
|
-
const notDeletedData = localDataStore.
|
|
91
|
-
expect(notDeletedData
|
|
92
|
-
const returnValue = localDataStore.markAsDeleted(
|
|
81
|
+
const notDeletedData = Array.from(localDataStore.values(getDhtAddressFromRaw(storedEntry.key)))
|
|
82
|
+
expect(notDeletedData[0]!.deleted).toBeFalse()
|
|
83
|
+
const returnValue = localDataStore.markAsDeleted(
|
|
84
|
+
getDhtAddressFromRaw(storedEntry.key),
|
|
85
|
+
creator1
|
|
86
|
+
)
|
|
93
87
|
expect(returnValue).toBe(true)
|
|
94
|
-
const deletedData = localDataStore.
|
|
95
|
-
expect(deletedData
|
|
88
|
+
const deletedData = Array.from(localDataStore.values(getDhtAddressFromRaw(storedEntry.key)))
|
|
89
|
+
expect(deletedData[0]!.deleted).toBeTrue()
|
|
96
90
|
})
|
|
97
91
|
|
|
98
92
|
it('data not stored', () => {
|
|
99
|
-
const
|
|
100
|
-
const returnValue = localDataStore.markAsDeleted(
|
|
93
|
+
const key = createRandomDhtAddress()
|
|
94
|
+
const returnValue = localDataStore.markAsDeleted(
|
|
95
|
+
key,
|
|
96
|
+
getNodeIdFromPeerDescriptor(createMockPeerDescriptor())
|
|
97
|
+
)
|
|
101
98
|
expect(returnValue).toBe(false)
|
|
102
99
|
})
|
|
103
100
|
|
|
104
101
|
it('data not stored by the given creator', () => {
|
|
105
102
|
const storedEntry = createMockDataEntry()
|
|
106
103
|
localDataStore.storeEntry(storedEntry)
|
|
107
|
-
const returnValue = localDataStore.markAsDeleted(
|
|
104
|
+
const returnValue = localDataStore.markAsDeleted(
|
|
105
|
+
getDhtAddressFromRaw(storedEntry.key),
|
|
106
|
+
getNodeIdFromPeerDescriptor(createMockPeerDescriptor())
|
|
107
|
+
)
|
|
108
108
|
expect(returnValue).toBe(false)
|
|
109
109
|
})
|
|
110
110
|
})
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { hexToBinary } from '@streamr/utils'
|
|
2
1
|
import { PeerManager, getDistance } from '../../src/dht/PeerManager'
|
|
3
|
-
import {
|
|
2
|
+
import { DhtAddress, createRandomDhtAddress, getRawFromDhtAddress } from '../../src/identifiers'
|
|
4
3
|
import { NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
5
4
|
import { range, sampleSize, sortBy, without } from 'lodash'
|
|
6
5
|
import { DhtNodeRpcRemote } from '../../src/dht/DhtNodeRpcRemote'
|
|
@@ -9,22 +8,22 @@ import { MockRpcCommunicator } from '../utils/mock/MockRpcCommunicator'
|
|
|
9
8
|
describe('PeerManager', () => {
|
|
10
9
|
|
|
11
10
|
it('getClosestContactsTo', () => {
|
|
12
|
-
const nodeIds = range(10).map(() =>
|
|
11
|
+
const nodeIds = range(10).map(() => createRandomDhtAddress())
|
|
13
12
|
const manager = new PeerManager({
|
|
14
|
-
localNodeId:
|
|
13
|
+
localNodeId: createRandomDhtAddress(),
|
|
15
14
|
createDhtNodeRpcRemote: (peerDescriptor: PeerDescriptor) => {
|
|
16
15
|
return new DhtNodeRpcRemote(undefined as any, peerDescriptor, undefined as any, new MockRpcCommunicator())
|
|
17
16
|
}
|
|
18
17
|
} as any)
|
|
19
|
-
manager.handleNewPeers(nodeIds.map((n) => ({ nodeId:
|
|
18
|
+
manager.handleNewPeers(nodeIds.map((n) => ({ nodeId: getRawFromDhtAddress(n), type: NodeType.NODEJS })))
|
|
20
19
|
|
|
21
|
-
const referenceId =
|
|
22
|
-
const excluded = new Set<
|
|
20
|
+
const referenceId = createRandomDhtAddress()
|
|
21
|
+
const excluded = new Set<DhtAddress>(sampleSize(nodeIds, 2)!)
|
|
23
22
|
const actual = manager.getClosestContactsTo(referenceId, 5, excluded)
|
|
24
23
|
|
|
25
24
|
const expected = sortBy(
|
|
26
25
|
without(nodeIds, ...Array.from(excluded.values())),
|
|
27
|
-
(n:
|
|
26
|
+
(n: DhtAddress) => getDistance(getRawFromDhtAddress(n), getRawFromDhtAddress(referenceId))
|
|
28
27
|
).slice(0, 5)
|
|
29
28
|
expect(actual.map((n) => n.getNodeId())).toEqual(expected)
|
|
30
29
|
})
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { RandomContactList } from '../../src/dht/contact/RandomContactList'
|
|
2
2
|
import { PeerID } from '../../src/helpers/PeerID'
|
|
3
|
-
import {
|
|
3
|
+
import { DhtAddress, DhtAddressRaw, getDhtAddressFromRaw } from '../../src/identifiers'
|
|
4
4
|
|
|
5
|
-
const createItem = (nodeId:
|
|
5
|
+
const createItem = (nodeId: DhtAddressRaw): { getNodeId: () => DhtAddress, getPeerId: () => PeerID } => {
|
|
6
6
|
return {
|
|
7
|
-
getNodeId: () =>
|
|
7
|
+
getNodeId: () => getDhtAddressFromRaw(nodeId),
|
|
8
8
|
getPeerId: () => PeerID.fromValue(nodeId)
|
|
9
9
|
}
|
|
10
10
|
}
|
|
@@ -21,6 +21,7 @@ import { areEqualPeerDescriptors } from '../../src/helpers/peerIdFromPeerDescrip
|
|
|
21
21
|
import { FakeRpcCommunicator } from '../utils/FakeRpcCommunicator'
|
|
22
22
|
import { Router } from '../../src/dht/routing/Router'
|
|
23
23
|
import { ITransport } from '../../src/exports'
|
|
24
|
+
import { getDhtAddressFromRaw } from '../../src/identifiers'
|
|
24
25
|
|
|
25
26
|
const createMockRouter = (error?: RouteMessageError): Partial<Router> => {
|
|
26
27
|
return {
|
|
@@ -85,7 +86,7 @@ describe('RecursiveOperationManager', () => {
|
|
|
85
86
|
|
|
86
87
|
it('startFind with mode Node returns self if no peers', async () => {
|
|
87
88
|
const recursiveOperationManager = createRecursiveOperationManager()
|
|
88
|
-
const res = await recursiveOperationManager.execute(PeerID.fromString('find').value, RecursiveOperation.FIND_NODE)
|
|
89
|
+
const res = await recursiveOperationManager.execute(getDhtAddressFromRaw(PeerID.fromString('find').value), RecursiveOperation.FIND_NODE)
|
|
89
90
|
expect(areEqualPeerDescriptors(res.closestNodes[0], peerDescriptor1)).toEqual(true)
|
|
90
91
|
recursiveOperationManager.stop()
|
|
91
92
|
})
|
|
@@ -3,7 +3,7 @@ import { range } from 'lodash'
|
|
|
3
3
|
import { RecursiveOperationSession } from '../../src/dht/recursive-operation/RecursiveOperationSession'
|
|
4
4
|
import { RecursiveOperationSessionRpcRemote } from '../../src/dht/recursive-operation/RecursiveOperationSessionRpcRemote'
|
|
5
5
|
import { ServiceID } from '../../src/exports'
|
|
6
|
-
import {
|
|
6
|
+
import { createRandomDhtAddress } from '../../src/identifiers'
|
|
7
7
|
import { Message, PeerDescriptor, RecursiveOperation } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
8
8
|
import { RecursiveOperationSessionRpcClient } from '../../src/proto/packages/dht/protos/DhtRpc.client'
|
|
9
9
|
import { RoutingRpcCommunicator } from '../../src/transport/RoutingRpcCommunicator'
|
|
@@ -21,7 +21,6 @@ describe('RecursiveOperationSession', () => {
|
|
|
21
21
|
return new RecursiveOperationSessionRpcRemote(
|
|
22
22
|
createMockPeerDescriptor(),
|
|
23
23
|
localPeerDescriptor,
|
|
24
|
-
serviceId,
|
|
25
24
|
new RoutingRpcCommunicator(serviceId, send),
|
|
26
25
|
RecursiveOperationSessionRpcClient
|
|
27
26
|
)
|
|
@@ -36,7 +35,7 @@ describe('RecursiveOperationSession', () => {
|
|
|
36
35
|
const doRouteRequest = jest.fn()
|
|
37
36
|
const session = new RecursiveOperationSession({
|
|
38
37
|
transport: environment.createTransport(),
|
|
39
|
-
targetId:
|
|
38
|
+
targetId: createRandomDhtAddress(),
|
|
40
39
|
localPeerDescriptor,
|
|
41
40
|
waitedRoutingPathCompletions: 3,
|
|
42
41
|
operation: RecursiveOperation.FIND_NODE,
|
package/test/unit/Router.test.ts
CHANGED
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
} from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
13
13
|
import { createMockPeerDescriptor, createWrappedClosestPeersRequest } from '../utils/utils'
|
|
14
14
|
import { FakeRpcCommunicator } from '../utils/FakeRpcCommunicator'
|
|
15
|
-
import {
|
|
15
|
+
import { DhtAddress } from '../../src/identifiers'
|
|
16
16
|
import { MockRpcCommunicator } from '../utils/mock/MockRpcCommunicator'
|
|
17
17
|
|
|
18
18
|
describe('Router', () => {
|
|
@@ -40,7 +40,7 @@ describe('Router', () => {
|
|
|
40
40
|
target: peerDescriptor1.nodeId,
|
|
41
41
|
sourcePeer: peerDescriptor2
|
|
42
42
|
}
|
|
43
|
-
let connections: Map<
|
|
43
|
+
let connections: Map<DhtAddress, DhtNodeRpcRemote>
|
|
44
44
|
const rpcCommunicator = new FakeRpcCommunicator()
|
|
45
45
|
|
|
46
46
|
const createMockDhtNodeRpcRemote = (destination: PeerDescriptor): DhtNodeRpcRemote => {
|
|
@@ -5,13 +5,13 @@ import { createMockPeerDescriptor, createWrappedClosestPeersRequest } from '../u
|
|
|
5
5
|
import { DhtNodeRpcRemote } from '../../src/dht/DhtNodeRpcRemote'
|
|
6
6
|
import { RoutingRpcCommunicator } from '../../src/transport/RoutingRpcCommunicator'
|
|
7
7
|
import { getNodeIdFromPeerDescriptor } from '../../src/helpers/peerIdFromPeerDescriptor'
|
|
8
|
-
import {
|
|
8
|
+
import { DhtAddress } from '../../src/identifiers'
|
|
9
9
|
import { MockRpcCommunicator } from '../utils/mock/MockRpcCommunicator'
|
|
10
10
|
|
|
11
11
|
describe('RoutingSession', () => {
|
|
12
12
|
|
|
13
13
|
let session: RoutingSession
|
|
14
|
-
let connections: Map<
|
|
14
|
+
let connections: Map<DhtAddress, DhtNodeRpcRemote>
|
|
15
15
|
let rpcCommunicator: RoutingRpcCommunicator
|
|
16
16
|
const mockPeerDescriptor1 = createMockPeerDescriptor()
|
|
17
17
|
const mockPeerDescriptor2 = createMockPeerDescriptor()
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { SortedContactList } from '../../src/dht/contact/SortedContactList'
|
|
2
2
|
import { PeerID } from '../../src/helpers/PeerID'
|
|
3
|
-
import {
|
|
3
|
+
import { DhtAddress, DhtAddressRaw, createRandomDhtAddress, getDhtAddressFromRaw } from '../../src/identifiers'
|
|
4
4
|
|
|
5
|
-
const createItem = (nodeId:
|
|
5
|
+
const createItem = (nodeId: DhtAddressRaw): { getNodeId: () => DhtAddress, getPeerId: () => PeerID } => {
|
|
6
6
|
return {
|
|
7
|
-
getNodeId: () =>
|
|
7
|
+
getNodeId: () => getDhtAddressFromRaw(nodeId),
|
|
8
8
|
getPeerId: () => PeerID.fromValue(nodeId)
|
|
9
9
|
}
|
|
10
10
|
}
|
|
@@ -72,7 +72,7 @@ describe('SortedContactList', () => {
|
|
|
72
72
|
const list = new SortedContactList({ referenceId: item0.getNodeId(), maxSize: 8, allowToContainReferenceId: false, emitEvents: true })
|
|
73
73
|
const onContactRemoved = jest.fn()
|
|
74
74
|
list.on('contactRemoved', onContactRemoved)
|
|
75
|
-
list.removeContact(
|
|
75
|
+
list.removeContact(createRandomDhtAddress())
|
|
76
76
|
list.addContact(item3)
|
|
77
77
|
list.removeContact(item3.getNodeId())
|
|
78
78
|
list.addContact(item4)
|
|
@@ -84,10 +84,10 @@ describe('SortedContactList', () => {
|
|
|
84
84
|
expect(list.getContact(item2.getNodeId())).toBeFalsy()
|
|
85
85
|
expect(list.getContactIds()).toEqual(list.getContactIds().sort(list.compareIds))
|
|
86
86
|
expect(list.getAllContacts()).toEqual([item1, item3, item4])
|
|
87
|
-
const ret = list.removeContact(
|
|
87
|
+
const ret = list.removeContact(getDhtAddressFromRaw(Buffer.from([0, 0, 0, 6])))
|
|
88
88
|
expect(ret).toEqual(false)
|
|
89
89
|
list.removeContact(item3.getNodeId())
|
|
90
|
-
list.removeContact(
|
|
90
|
+
list.removeContact(createRandomDhtAddress())
|
|
91
91
|
expect(list.getAllContacts()).toEqual([item1, item4])
|
|
92
92
|
expect(onContactRemoved).toHaveBeenNthCalledWith(1, item3, [])
|
|
93
93
|
expect(onContactRemoved).toHaveBeenNthCalledWith(2, item2, [item1, item3, item4])
|
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import { wait, waitForCondition } from '@streamr/utils'
|
|
2
|
-
import crypto from 'crypto'
|
|
3
2
|
import { range, sortBy } from 'lodash'
|
|
4
|
-
import { Key } from 'readline'
|
|
5
3
|
import { getDistance } from '../../src/dht/PeerManager'
|
|
6
4
|
import { StoreManager } from '../../src/dht/store/StoreManager'
|
|
7
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
DhtAddress,
|
|
7
|
+
createRandomDhtAddress,
|
|
8
|
+
getDhtAddressFromRaw,
|
|
9
|
+
getRawFromDhtAddress,
|
|
10
|
+
} from '../../src/identifiers'
|
|
8
11
|
import { NodeType, ReplicateDataRequest } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
9
12
|
|
|
10
13
|
const DATA_ENTRY = {
|
|
11
|
-
key:
|
|
12
|
-
creator:
|
|
14
|
+
key: getRawFromDhtAddress(createRandomDhtAddress()),
|
|
15
|
+
creator: getRawFromDhtAddress(createRandomDhtAddress())
|
|
13
16
|
}
|
|
14
17
|
const NODES_CLOSEST_TO_DATA = sortBy(
|
|
15
|
-
range(5).map(() =>
|
|
16
|
-
(id:
|
|
18
|
+
range(5).map(() => createRandomDhtAddress()),
|
|
19
|
+
(id: DhtAddress) => getDistance(getRawFromDhtAddress(id), DATA_ENTRY.key)
|
|
17
20
|
)
|
|
18
21
|
|
|
19
22
|
describe('StoreManager', () => {
|
|
@@ -21,13 +24,13 @@ describe('StoreManager', () => {
|
|
|
21
24
|
describe('new contact', () => {
|
|
22
25
|
|
|
23
26
|
const createStoreManager = (
|
|
24
|
-
localNodeId:
|
|
25
|
-
closestNeighbors:
|
|
27
|
+
localNodeId: DhtAddress,
|
|
28
|
+
closestNeighbors: DhtAddress[],
|
|
26
29
|
replicateData: (request: ReplicateDataRequest) => unknown,
|
|
27
|
-
setStale: (key:
|
|
30
|
+
setStale: (key: DhtAddress, creator: DhtAddress, stale: boolean) => unknown
|
|
28
31
|
): StoreManager => {
|
|
29
32
|
const getClosestNeighborsTo = () => {
|
|
30
|
-
return closestNeighbors.map((nodeId) => ({ nodeId, type: NodeType.NODEJS }))
|
|
33
|
+
return closestNeighbors.map((nodeId) => ({ nodeId: getRawFromDhtAddress(nodeId), type: NodeType.NODEJS }))
|
|
31
34
|
}
|
|
32
35
|
return new StoreManager({
|
|
33
36
|
rpcCommunicator: {
|
|
@@ -35,7 +38,7 @@ describe('StoreManager', () => {
|
|
|
35
38
|
registerRpcNotification: () => {}
|
|
36
39
|
} as any,
|
|
37
40
|
recursiveOperationManager: undefined as any,
|
|
38
|
-
localPeerDescriptor: { nodeId: localNodeId, type: NodeType.NODEJS },
|
|
41
|
+
localPeerDescriptor: { nodeId: getRawFromDhtAddress(localNodeId), type: NodeType.NODEJS },
|
|
39
42
|
localDataStore: { values: () => [DATA_ENTRY], setStale } as any,
|
|
40
43
|
serviceId: undefined as any,
|
|
41
44
|
highestTtl: undefined as any,
|
|
@@ -49,14 +52,14 @@ describe('StoreManager', () => {
|
|
|
49
52
|
|
|
50
53
|
it('new node is within redundancy factor', async () => {
|
|
51
54
|
const replicateData = jest.fn<undefined, [ReplicateDataRequest]>()
|
|
52
|
-
const setStale = jest.fn<undefined, [
|
|
55
|
+
const setStale = jest.fn<undefined, [DhtAddress, DhtAddress]>()
|
|
53
56
|
const manager = createStoreManager(
|
|
54
57
|
NODES_CLOSEST_TO_DATA[0],
|
|
55
58
|
[NODES_CLOSEST_TO_DATA[1], NODES_CLOSEST_TO_DATA[3], NODES_CLOSEST_TO_DATA[4]],
|
|
56
59
|
replicateData,
|
|
57
60
|
setStale
|
|
58
61
|
)
|
|
59
|
-
manager.onNewContact({ nodeId: NODES_CLOSEST_TO_DATA[2], type: NodeType.NODEJS })
|
|
62
|
+
manager.onNewContact({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[2]), type: NodeType.NODEJS })
|
|
60
63
|
await waitForCondition(() => replicateData.mock.calls.length === 1)
|
|
61
64
|
expect(replicateData).toHaveBeenCalledWith({
|
|
62
65
|
entry: DATA_ENTRY
|
|
@@ -66,14 +69,14 @@ describe('StoreManager', () => {
|
|
|
66
69
|
|
|
67
70
|
it('new node is not within redundancy factor', async () => {
|
|
68
71
|
const replicateData = jest.fn<undefined, [ReplicateDataRequest]>()
|
|
69
|
-
const setStale = jest.fn<undefined, [
|
|
72
|
+
const setStale = jest.fn<undefined, [DhtAddress, DhtAddress]>()
|
|
70
73
|
const manager = createStoreManager(
|
|
71
74
|
NODES_CLOSEST_TO_DATA[0],
|
|
72
75
|
[NODES_CLOSEST_TO_DATA[1], NODES_CLOSEST_TO_DATA[2], NODES_CLOSEST_TO_DATA[3]],
|
|
73
76
|
replicateData,
|
|
74
77
|
setStale
|
|
75
78
|
)
|
|
76
|
-
manager.onNewContact({ nodeId: NODES_CLOSEST_TO_DATA[4], type: NodeType.NODEJS })
|
|
79
|
+
manager.onNewContact({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
|
|
77
80
|
await wait(50)
|
|
78
81
|
expect(replicateData).not.toHaveBeenCalled()
|
|
79
82
|
expect(setStale).not.toHaveBeenCalled()
|
|
@@ -84,14 +87,14 @@ describe('StoreManager', () => {
|
|
|
84
87
|
|
|
85
88
|
it('this node is within redundancy factor', async () => {
|
|
86
89
|
const replicateData = jest.fn<undefined, [ReplicateDataRequest]>()
|
|
87
|
-
const setStale = jest.fn<undefined, [
|
|
90
|
+
const setStale = jest.fn<undefined, [DhtAddress, DhtAddress]>()
|
|
88
91
|
const manager = createStoreManager(
|
|
89
92
|
NODES_CLOSEST_TO_DATA[1],
|
|
90
93
|
[NODES_CLOSEST_TO_DATA[0], NODES_CLOSEST_TO_DATA[2], NODES_CLOSEST_TO_DATA[3]],
|
|
91
94
|
replicateData,
|
|
92
95
|
setStale
|
|
93
96
|
)
|
|
94
|
-
manager.onNewContact({ nodeId: NODES_CLOSEST_TO_DATA[4], type: NodeType.NODEJS })
|
|
97
|
+
manager.onNewContact({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
|
|
95
98
|
await wait(50)
|
|
96
99
|
expect(replicateData).not.toHaveBeenCalled()
|
|
97
100
|
expect(setStale).not.toHaveBeenCalled()
|
|
@@ -99,30 +102,30 @@ describe('StoreManager', () => {
|
|
|
99
102
|
|
|
100
103
|
it('this node is not within redundancy factor', async () => {
|
|
101
104
|
const replicateData = jest.fn<undefined, [ReplicateDataRequest]>()
|
|
102
|
-
const setStale = jest.fn<undefined, [
|
|
105
|
+
const setStale = jest.fn<undefined, [DhtAddress, DhtAddress]>()
|
|
103
106
|
const manager = createStoreManager(
|
|
104
107
|
NODES_CLOSEST_TO_DATA[3],
|
|
105
108
|
[NODES_CLOSEST_TO_DATA[0], NODES_CLOSEST_TO_DATA[1], NODES_CLOSEST_TO_DATA[2]],
|
|
106
109
|
replicateData,
|
|
107
110
|
setStale
|
|
108
111
|
)
|
|
109
|
-
manager.onNewContact({ nodeId: NODES_CLOSEST_TO_DATA[4], type: NodeType.NODEJS })
|
|
112
|
+
manager.onNewContact({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
|
|
110
113
|
await wait(50)
|
|
111
114
|
expect(replicateData).not.toHaveBeenCalled()
|
|
112
115
|
expect(setStale).toHaveBeenCalledTimes(1)
|
|
113
|
-
expect(setStale).toHaveBeenCalledWith(DATA_ENTRY.key,
|
|
116
|
+
expect(setStale).toHaveBeenCalledWith(getDhtAddressFromRaw(DATA_ENTRY.key), getDhtAddressFromRaw(DATA_ENTRY.creator), true)
|
|
114
117
|
})
|
|
115
118
|
|
|
116
119
|
it('this node has less than redundancyFactor neighbors', async () => {
|
|
117
120
|
const replicateData = jest.fn<undefined, [ReplicateDataRequest]>()
|
|
118
|
-
const setStale = jest.fn<undefined, [
|
|
121
|
+
const setStale = jest.fn<undefined, [DhtAddress, DhtAddress]>()
|
|
119
122
|
const manager = createStoreManager(
|
|
120
123
|
NODES_CLOSEST_TO_DATA[3],
|
|
121
124
|
[NODES_CLOSEST_TO_DATA[0], NODES_CLOSEST_TO_DATA[1]],
|
|
122
125
|
replicateData,
|
|
123
126
|
setStale
|
|
124
127
|
)
|
|
125
|
-
manager.onNewContact({ nodeId: NODES_CLOSEST_TO_DATA[4], type: NodeType.NODEJS })
|
|
128
|
+
manager.onNewContact({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
|
|
126
129
|
await wait(50)
|
|
127
130
|
expect(replicateData).not.toHaveBeenCalled()
|
|
128
131
|
expect(setStale).toHaveBeenCalledTimes(0)
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { areEqualBinaries
|
|
1
|
+
import { areEqualBinaries } from '@streamr/utils'
|
|
2
2
|
import { printExpected, printReceived } from 'jest-matcher-utils'
|
|
3
3
|
import { isEqual } from 'lodash'
|
|
4
4
|
import { ConnectivityMethod, NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
5
|
+
import { getDhtAddressFromRaw } from '../../src/identifiers'
|
|
5
6
|
|
|
6
7
|
// we could ES2015 module syntax (https://jestjs.io/docs/expect#expectextendmatchers),
|
|
7
8
|
// but the IDE doesn't find custom matchers if we do that
|
|
@@ -24,7 +25,7 @@ const toEqualPeerDescriptor = (
|
|
|
24
25
|
): jest.CustomMatcherResult => {
|
|
25
26
|
const messages: string[] = []
|
|
26
27
|
if (!areEqualBinaries(expected.nodeId, actual.nodeId)) {
|
|
27
|
-
messages.push(formErrorMessage('nodeId',
|
|
28
|
+
messages.push(formErrorMessage('nodeId', getDhtAddressFromRaw(expected.nodeId), getDhtAddressFromRaw(actual.nodeId)))
|
|
28
29
|
}
|
|
29
30
|
if (!isEqual(expected.type, actual.type)) {
|
|
30
31
|
const typeNames = { [NodeType.NODEJS]: 'NODEJS', [NodeType.BROWSER]: 'BROWSER' }
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { MessageType as MessageType$, ScalarType } from '@protobuf-ts/runtime'
|
|
2
2
|
import { randomString } from '@streamr/utils'
|
|
3
|
-
import crypto from 'crypto'
|
|
4
3
|
import { Timestamp } from '../../../src/proto/google/protobuf/timestamp'
|
|
5
4
|
import { Any } from '../../../src/proto/google/protobuf/any'
|
|
6
5
|
import { DataEntry } from '../../../src/proto/packages/dht/protos/DhtRpc'
|
|
7
|
-
import {
|
|
6
|
+
import { DhtAddress, createRandomDhtAddress, getRawFromDhtAddress } from '../../../src/identifiers'
|
|
7
|
+
import { omit } from 'lodash'
|
|
8
8
|
|
|
9
9
|
const MockData = new class extends MessageType$<{ foo: string }> {
|
|
10
10
|
constructor() {
|
|
@@ -14,16 +14,18 @@ const MockData = new class extends MessageType$<{ foo: string }> {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
export const createMockDataEntry = (
|
|
17
|
+
export const createMockDataEntry = (
|
|
18
|
+
entry: Partial<Omit<DataEntry, 'key' | 'creator'> & { key: DhtAddress, creator: DhtAddress }> = {}
|
|
19
|
+
): DataEntry => {
|
|
18
20
|
return {
|
|
19
|
-
key:
|
|
21
|
+
key: getRawFromDhtAddress(entry.key ?? createRandomDhtAddress()),
|
|
20
22
|
data: Any.pack({ foo: randomString(5) }, MockData),
|
|
21
|
-
creator: entry.creator ??
|
|
23
|
+
creator: getRawFromDhtAddress(entry.creator ?? createRandomDhtAddress()),
|
|
22
24
|
ttl: 10000,
|
|
23
25
|
stale: false,
|
|
24
26
|
deleted: false,
|
|
25
27
|
createdAt: Timestamp.now(),
|
|
26
|
-
...entry
|
|
28
|
+
...omit(entry, 'key', 'creator')
|
|
27
29
|
}
|
|
28
30
|
}
|
|
29
31
|
|
package/test/utils/utils.ts
CHANGED
|
@@ -28,11 +28,11 @@ import { Empty } from '../../src/proto/google/protobuf/empty'
|
|
|
28
28
|
import { Any } from '../../src/proto/google/protobuf/any'
|
|
29
29
|
import { wait, waitForCondition } from '@streamr/utils'
|
|
30
30
|
import { SimulatorTransport } from '../../src/connection/simulator/SimulatorTransport'
|
|
31
|
-
import {
|
|
31
|
+
import { DhtAddress, createRandomDhtAddress, getRawFromDhtAddress } from '../../src/identifiers'
|
|
32
32
|
|
|
33
33
|
export const createMockPeerDescriptor = (opts?: Partial<Omit<PeerDescriptor, 'nodeId'>>): PeerDescriptor => {
|
|
34
34
|
return {
|
|
35
|
-
nodeId:
|
|
35
|
+
nodeId: getRawFromDhtAddress(createRandomDhtAddress()),
|
|
36
36
|
type: NodeType.NODEJS,
|
|
37
37
|
...opts
|
|
38
38
|
}
|
|
@@ -40,13 +40,13 @@ export const createMockPeerDescriptor = (opts?: Partial<Omit<PeerDescriptor, 'no
|
|
|
40
40
|
|
|
41
41
|
export const createMockConnectionDhtNode = async (
|
|
42
42
|
simulator: Simulator,
|
|
43
|
-
nodeId?:
|
|
43
|
+
nodeId?: DhtAddress,
|
|
44
44
|
numberOfNodesPerKBucket?: number,
|
|
45
45
|
maxConnections = 80,
|
|
46
46
|
dhtJoinTimeout = 45000
|
|
47
47
|
): Promise<DhtNode> => {
|
|
48
48
|
const peerDescriptor: PeerDescriptor = {
|
|
49
|
-
nodeId: nodeId ??
|
|
49
|
+
nodeId: getRawFromDhtAddress(nodeId ?? createRandomDhtAddress()),
|
|
50
50
|
type: NodeType.NODEJS,
|
|
51
51
|
region: getRandomRegion()
|
|
52
52
|
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { BrandedString } from '@streamr/utils';
|
|
2
|
-
export declare const createRandomNodeId: () => Uint8Array;
|
|
3
|
-
export type NodeID = BrandedString<'NodeID'>;
|
|
4
|
-
export declare const areEqualNodeIds: (nodeId1: NodeID, nodeId2: NodeID) => boolean;
|
|
5
|
-
export declare const getNodeIdFromDataKey: (key: Uint8Array) => NodeID;
|
|
6
|
-
export declare const getNodeIdFromBinary: (id: Uint8Array) => NodeID;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getNodeIdFromBinary = exports.getNodeIdFromDataKey = exports.areEqualNodeIds = exports.createRandomNodeId = void 0;
|
|
7
|
-
const utils_1 = require("@streamr/utils");
|
|
8
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
-
// https://www.scs.stanford.edu/~dm/home/papers/kpos.pdf
|
|
10
|
-
const KADEMLIA_ID_LENGTH_IN_BYTES = 20;
|
|
11
|
-
// TODO this should return NodeID
|
|
12
|
-
const createRandomNodeId = () => {
|
|
13
|
-
return crypto_1.default.randomBytes(KADEMLIA_ID_LENGTH_IN_BYTES);
|
|
14
|
-
};
|
|
15
|
-
exports.createRandomNodeId = createRandomNodeId;
|
|
16
|
-
// TODO remove this or add support for UInt8Array parameters
|
|
17
|
-
const areEqualNodeIds = (nodeId1, nodeId2) => {
|
|
18
|
-
return nodeId1 === nodeId2;
|
|
19
|
-
};
|
|
20
|
-
exports.areEqualNodeIds = areEqualNodeIds;
|
|
21
|
-
// TODO maybe this is not needed and we can use just getNodeIdFromBinary?
|
|
22
|
-
const getNodeIdFromDataKey = (key) => {
|
|
23
|
-
return (0, exports.getNodeIdFromBinary)(key);
|
|
24
|
-
};
|
|
25
|
-
exports.getNodeIdFromDataKey = getNodeIdFromDataKey;
|
|
26
|
-
// TODO should we have similar method to convert nodeId to bucketId (which is just hexToBinary)
|
|
27
|
-
const getNodeIdFromBinary = (id) => {
|
|
28
|
-
return (0, utils_1.binaryToHex)(id);
|
|
29
|
-
};
|
|
30
|
-
exports.getNodeIdFromBinary = getNodeIdFromBinary;
|
|
31
|
-
//# sourceMappingURL=nodeId.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"nodeId.js","sourceRoot":"","sources":["../../../src/helpers/nodeId.ts"],"names":[],"mappings":";;;;;;AAAA,0CAA2D;AAC3D,oDAA2B;AAE3B,wDAAwD;AACxD,MAAM,2BAA2B,GAAG,EAAE,CAAA;AAEtC,iCAAiC;AAC1B,MAAM,kBAAkB,GAAG,GAAe,EAAE;IAC/C,OAAO,gBAAM,CAAC,WAAW,CAAC,2BAA2B,CAAC,CAAA;AAC1D,CAAC,CAAA;AAFY,QAAA,kBAAkB,sBAE9B;AAKD,4DAA4D;AACrD,MAAM,eAAe,GAAG,CAAC,OAAe,EAAE,OAAe,EAAW,EAAE;IACzE,OAAO,OAAO,KAAK,OAAO,CAAA;AAC9B,CAAC,CAAA;AAFY,QAAA,eAAe,mBAE3B;AAED,yEAAyE;AAClE,MAAM,oBAAoB,GAAG,CAAC,GAAe,EAAU,EAAE;IAC5D,OAAO,IAAA,2BAAmB,EAAC,GAAG,CAAC,CAAA;AACnC,CAAC,CAAA;AAFY,QAAA,oBAAoB,wBAEhC;AAED,+FAA+F;AACxF,MAAM,mBAAmB,GAAG,CAAC,EAAc,EAAU,EAAE;IAC1D,OAAO,IAAA,mBAAW,EAAC,EAAE,CAAsB,CAAA;AAC/C,CAAC,CAAA;AAFY,QAAA,mBAAmB,uBAE/B"}
|
package/src/helpers/nodeId.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { BrandedString, binaryToHex } from '@streamr/utils'
|
|
2
|
-
import crypto from 'crypto'
|
|
3
|
-
|
|
4
|
-
// https://www.scs.stanford.edu/~dm/home/papers/kpos.pdf
|
|
5
|
-
const KADEMLIA_ID_LENGTH_IN_BYTES = 20
|
|
6
|
-
|
|
7
|
-
// TODO this should return NodeID
|
|
8
|
-
export const createRandomNodeId = (): Uint8Array => {
|
|
9
|
-
return crypto.randomBytes(KADEMLIA_ID_LENGTH_IN_BYTES)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// TODO rename the file to be "NodeID.ts" instead of "nodeId.ts"
|
|
13
|
-
export type NodeID = BrandedString<'NodeID'>
|
|
14
|
-
|
|
15
|
-
// TODO remove this or add support for UInt8Array parameters
|
|
16
|
-
export const areEqualNodeIds = (nodeId1: NodeID, nodeId2: NodeID): boolean => {
|
|
17
|
-
return nodeId1 === nodeId2
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// TODO maybe this is not needed and we can use just getNodeIdFromBinary?
|
|
21
|
-
export const getNodeIdFromDataKey = (key: Uint8Array): NodeID => {
|
|
22
|
-
return getNodeIdFromBinary(key)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// TODO should we have similar method to convert nodeId to bucketId (which is just hexToBinary)
|
|
26
|
-
export const getNodeIdFromBinary = (id: Uint8Array): NodeID => {
|
|
27
|
-
return binaryToHex(id) as unknown as NodeID
|
|
28
|
-
}
|