@streamr/dht 100.0.0-testnet-three.1 → 100.0.0-testnet-three.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.
Files changed (61) hide show
  1. package/dist/package.json +5 -5
  2. package/dist/src/connection/connectivityRequestHandler.js +25 -21
  3. package/dist/src/connection/connectivityRequestHandler.js.map +1 -1
  4. package/dist/src/connection/websocket/WebsocketConnector.js +10 -1
  5. package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
  6. package/dist/src/dht/DhtNode.d.ts +3 -2
  7. package/dist/src/dht/DhtNode.js +28 -11
  8. package/dist/src/dht/DhtNode.js.map +1 -1
  9. package/dist/src/dht/DhtNodeRpcLocal.d.ts +1 -1
  10. package/dist/src/dht/DhtNodeRpcLocal.js +2 -2
  11. package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
  12. package/dist/src/dht/PeerManager.d.ts +7 -9
  13. package/dist/src/dht/PeerManager.js +7 -14
  14. package/dist/src/dht/PeerManager.js.map +1 -1
  15. package/dist/src/dht/contact/ContactList.d.ts +1 -1
  16. package/dist/src/dht/contact/RandomContactList.js +1 -1
  17. package/dist/src/dht/contact/RandomContactList.js.map +1 -1
  18. package/dist/src/dht/contact/SortedContactList.js +18 -17
  19. package/dist/src/dht/contact/SortedContactList.js.map +1 -1
  20. package/dist/src/dht/discovery/DiscoverySession.d.ts +1 -1
  21. package/dist/src/dht/discovery/DiscoverySession.js +5 -5
  22. package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
  23. package/dist/src/dht/discovery/PeerDiscovery.js +2 -2
  24. package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
  25. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js +0 -1
  26. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -1
  27. package/dist/src/dht/routing/RouterRpcLocal.js +2 -3
  28. package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
  29. package/dist/src/dht/routing/RoutingSession.js +2 -1
  30. package/dist/src/dht/routing/RoutingSession.js.map +1 -1
  31. package/dist/src/dht/store/LocalDataStore.d.ts +1 -1
  32. package/dist/src/dht/store/LocalDataStore.js +2 -5
  33. package/dist/src/dht/store/LocalDataStore.js.map +1 -1
  34. package/dist/src/dht/store/StoreManager.d.ts +1 -1
  35. package/dist/src/dht/store/StoreManager.js +10 -17
  36. package/dist/src/dht/store/StoreManager.js.map +1 -1
  37. package/dist/src/identifiers.d.ts +1 -0
  38. package/dist/src/identifiers.js +3 -3
  39. package/dist/src/identifiers.js.map +1 -1
  40. package/package.json +5 -5
  41. package/src/connection/connectivityRequestHandler.ts +26 -22
  42. package/src/connection/websocket/WebsocketConnector.ts +9 -1
  43. package/src/dht/DhtNode.ts +33 -16
  44. package/src/dht/DhtNodeRpcLocal.ts +3 -3
  45. package/src/dht/PeerManager.ts +12 -21
  46. package/src/dht/contact/ContactList.ts +1 -1
  47. package/src/dht/contact/RandomContactList.ts +1 -1
  48. package/src/dht/contact/SortedContactList.ts +26 -24
  49. package/src/dht/discovery/DiscoverySession.ts +5 -5
  50. package/src/dht/discovery/PeerDiscovery.ts +2 -2
  51. package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +0 -1
  52. package/src/dht/routing/RouterRpcLocal.ts +2 -3
  53. package/src/dht/routing/RoutingSession.ts +2 -1
  54. package/src/dht/store/LocalDataStore.ts +2 -5
  55. package/src/dht/store/StoreManager.ts +10 -17
  56. package/src/identifiers.ts +1 -1
  57. package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +5 -5
  58. package/test/end-to-end/Layer0Webrtc.test.ts +0 -2
  59. package/test/unit/PeerManager.test.ts +1 -1
  60. package/test/unit/SortedContactList.test.ts +13 -0
  61. package/test/unit/StoreManager.test.ts +26 -23
@@ -1,5 +1,5 @@
1
1
  import { DhtNode } from '../../src/dht/DhtNode'
2
- import { DhtAddress, getNodeIdFromPeerDescriptor } from '../../src/identifiers'
2
+ import { createRandomDhtAddress, getNodeIdFromPeerDescriptor } from '../../src/identifiers'
3
3
  import { createMockPeerDescriptor } from '../utils/utils'
4
4
 
5
5
  describe('Layer 1 on Layer 0 with mocked connections', () => {
@@ -26,22 +26,22 @@ describe('Layer 1 on Layer 0 with mocked connections', () => {
26
26
 
27
27
  layer0EntryPoint = new DhtNode({ peerDescriptor: entrypointDescriptor, websocketServerEnableTls: false })
28
28
 
29
- const layer0Node1Id = '11' as DhtAddress
29
+ const layer0Node1Id = createRandomDhtAddress()
30
30
  layer0Node1 = new DhtNode({
31
31
  nodeId: layer0Node1Id
32
32
  })
33
33
 
34
- const layer0Node2Id = '22' as DhtAddress
34
+ const layer0Node2Id = createRandomDhtAddress()
35
35
  layer0Node2 = new DhtNode({
36
36
  nodeId: layer0Node2Id
37
37
  })
38
38
 
39
- const layer0Node3Id = '33' as DhtAddress
39
+ const layer0Node3Id = createRandomDhtAddress()
40
40
  layer0Node3 = new DhtNode({
41
41
  nodeId: layer0Node3Id
42
42
  })
43
43
 
44
- const layer0Node4Id = '44' as DhtAddress
44
+ const layer0Node4Id = createRandomDhtAddress()
45
45
  layer0Node4 = new DhtNode({
46
46
  nodeId: layer0Node4Id
47
47
  })
@@ -39,8 +39,6 @@ describe('Layer0 with WebRTC connections', () => {
39
39
  node3.start(),
40
40
  node4.start()
41
41
  ])
42
-
43
- await epDhtNode.joinDht([epPeerDescriptor])
44
42
  })
45
43
 
46
44
  afterEach(async () => {
@@ -15,7 +15,7 @@ describe('PeerManager', () => {
15
15
  return new DhtNodeRpcRemote(undefined as any, peerDescriptor, undefined as any, new MockRpcCommunicator())
16
16
  }
17
17
  } as any)
18
- manager.handleNewPeers(nodeIds.map((n) => ({ nodeId: getRawFromDhtAddress(n), type: NodeType.NODEJS })))
18
+ manager.addContact(nodeIds.map((n) => ({ nodeId: getRawFromDhtAddress(n), type: NodeType.NODEJS })))
19
19
 
20
20
  const referenceId = createRandomDhtAddress()
21
21
  const excluded = new Set<DhtAddress>(sampleSize(nodeIds, 2)!)
@@ -118,4 +118,17 @@ describe('SortedContactList', () => {
118
118
  list.setActive(item4.getNodeId())
119
119
  expect(list.getActiveContacts()).toEqual([item2, item3, item4])
120
120
  })
121
+
122
+ it('does not emit contactAdded if contact did not fit the structure', () => {
123
+ const list = new SortedContactList({ referenceId: item0.getNodeId(), maxSize: 2, allowToContainReferenceId: false, emitEvents: true })
124
+ const onContactAdded = jest.fn()
125
+ list.on('contactAdded', onContactAdded)
126
+ list.addContact(item1)
127
+ list.addContact(item2)
128
+ expect(onContactAdded).toBeCalledTimes(2)
129
+ list.addContact(item3)
130
+ expect(onContactAdded).toBeCalledTimes(2)
131
+ expect(list.getAllContacts().length).toEqual(2)
132
+ })
133
+
121
134
  })
@@ -27,7 +27,7 @@ describe('StoreManager', () => {
27
27
  localNodeId: DhtAddress,
28
28
  closestNeighbors: DhtAddress[],
29
29
  replicateData: (request: ReplicateDataRequest) => unknown,
30
- setStale: (key: DhtAddress, creator: DhtAddress, stale: boolean) => unknown
30
+ setAllEntriesAsStale: (key: DhtAddress) => unknown
31
31
  ): StoreManager => {
32
32
  const getClosestNeighborsTo = () => {
33
33
  return closestNeighbors.map((nodeId) => ({ nodeId: getRawFromDhtAddress(nodeId), type: NodeType.NODEJS }))
@@ -39,7 +39,11 @@ describe('StoreManager', () => {
39
39
  } as any,
40
40
  recursiveOperationManager: undefined as any,
41
41
  localPeerDescriptor: { nodeId: getRawFromDhtAddress(localNodeId), type: NodeType.NODEJS },
42
- localDataStore: { values: () => [DATA_ENTRY], setStale } as any,
42
+ localDataStore: {
43
+ keys: () => [getDhtAddressFromRaw(DATA_ENTRY.key)],
44
+ values: () => [DATA_ENTRY],
45
+ setAllEntriesAsStale
46
+ } as any,
43
47
  serviceId: undefined as any,
44
48
  highestTtl: undefined as any,
45
49
  redundancyFactor: 3,
@@ -52,34 +56,34 @@ describe('StoreManager', () => {
52
56
 
53
57
  it('new node is within redundancy factor', async () => {
54
58
  const replicateData = jest.fn<undefined, [ReplicateDataRequest]>()
55
- const setStale = jest.fn<undefined, [DhtAddress, DhtAddress]>()
59
+ const setAllEntriesAsStale = jest.fn<undefined, [DhtAddress]>()
56
60
  const manager = createStoreManager(
57
61
  NODES_CLOSEST_TO_DATA[0],
58
62
  [NODES_CLOSEST_TO_DATA[1], NODES_CLOSEST_TO_DATA[3], NODES_CLOSEST_TO_DATA[4]],
59
63
  replicateData,
60
- setStale
64
+ setAllEntriesAsStale
61
65
  )
62
- manager.onNewContact({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[2]), type: NodeType.NODEJS })
66
+ manager.onContactAdded({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[2]), type: NodeType.NODEJS })
63
67
  await waitForCondition(() => replicateData.mock.calls.length === 1)
64
68
  expect(replicateData).toHaveBeenCalledWith({
65
69
  entry: DATA_ENTRY
66
70
  })
67
- expect(setStale).not.toHaveBeenCalled()
71
+ expect(setAllEntriesAsStale).not.toHaveBeenCalled()
68
72
  })
69
73
 
70
74
  it('new node is not within redundancy factor', async () => {
71
75
  const replicateData = jest.fn<undefined, [ReplicateDataRequest]>()
72
- const setStale = jest.fn<undefined, [DhtAddress, DhtAddress]>()
76
+ const setAllEntriesAsStale = jest.fn<undefined, [DhtAddress]>()
73
77
  const manager = createStoreManager(
74
78
  NODES_CLOSEST_TO_DATA[0],
75
79
  [NODES_CLOSEST_TO_DATA[1], NODES_CLOSEST_TO_DATA[2], NODES_CLOSEST_TO_DATA[3]],
76
80
  replicateData,
77
- setStale
81
+ setAllEntriesAsStale
78
82
  )
79
- manager.onNewContact({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
83
+ manager.onContactAdded({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
80
84
  await wait(50)
81
85
  expect(replicateData).not.toHaveBeenCalled()
82
- expect(setStale).not.toHaveBeenCalled()
86
+ expect(setAllEntriesAsStale).not.toHaveBeenCalled()
83
87
  })
84
88
  })
85
89
 
@@ -87,48 +91,47 @@ describe('StoreManager', () => {
87
91
 
88
92
  it('this node is within redundancy factor', async () => {
89
93
  const replicateData = jest.fn<undefined, [ReplicateDataRequest]>()
90
- const setStale = jest.fn<undefined, [DhtAddress, DhtAddress]>()
94
+ const setAllEntriesAsStale = jest.fn<undefined, [DhtAddress]>()
91
95
  const manager = createStoreManager(
92
96
  NODES_CLOSEST_TO_DATA[1],
93
97
  [NODES_CLOSEST_TO_DATA[0], NODES_CLOSEST_TO_DATA[2], NODES_CLOSEST_TO_DATA[3]],
94
98
  replicateData,
95
- setStale
99
+ setAllEntriesAsStale
96
100
  )
97
- manager.onNewContact({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
101
+ manager.onContactAdded({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
98
102
  await wait(50)
99
103
  expect(replicateData).not.toHaveBeenCalled()
100
- expect(setStale).not.toHaveBeenCalled()
101
104
  })
102
105
 
103
106
  it('this node is not within redundancy factor', async () => {
104
107
  const replicateData = jest.fn<undefined, [ReplicateDataRequest]>()
105
- const setStale = jest.fn<undefined, [DhtAddress, DhtAddress]>()
108
+ const setAllEntriesAsStale = jest.fn<undefined, [DhtAddress]>()
106
109
  const manager = createStoreManager(
107
110
  NODES_CLOSEST_TO_DATA[3],
108
111
  [NODES_CLOSEST_TO_DATA[0], NODES_CLOSEST_TO_DATA[1], NODES_CLOSEST_TO_DATA[2]],
109
112
  replicateData,
110
- setStale
113
+ setAllEntriesAsStale
111
114
  )
112
- manager.onNewContact({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
115
+ manager.onContactAdded({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
113
116
  await wait(50)
114
117
  expect(replicateData).not.toHaveBeenCalled()
115
- expect(setStale).toHaveBeenCalledTimes(1)
116
- expect(setStale).toHaveBeenCalledWith(getDhtAddressFromRaw(DATA_ENTRY.key), getDhtAddressFromRaw(DATA_ENTRY.creator), true)
118
+ expect(setAllEntriesAsStale).toHaveBeenCalledTimes(1)
119
+ expect(setAllEntriesAsStale).toHaveBeenCalledWith(getDhtAddressFromRaw(DATA_ENTRY.key))
117
120
  })
118
121
 
119
122
  it('this node has less than redundancyFactor neighbors', async () => {
120
123
  const replicateData = jest.fn<undefined, [ReplicateDataRequest]>()
121
- const setStale = jest.fn<undefined, [DhtAddress, DhtAddress]>()
124
+ const setAllEntriesAsStale = jest.fn<undefined, [DhtAddress]>()
122
125
  const manager = createStoreManager(
123
126
  NODES_CLOSEST_TO_DATA[3],
124
127
  [NODES_CLOSEST_TO_DATA[0], NODES_CLOSEST_TO_DATA[1]],
125
128
  replicateData,
126
- setStale
129
+ setAllEntriesAsStale
127
130
  )
128
- manager.onNewContact({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
131
+ manager.onContactAdded({ nodeId: getRawFromDhtAddress(NODES_CLOSEST_TO_DATA[4]), type: NodeType.NODEJS })
129
132
  await wait(50)
130
133
  expect(replicateData).not.toHaveBeenCalled()
131
- expect(setStale).toHaveBeenCalledTimes(0)
134
+ expect(setAllEntriesAsStale).toHaveBeenCalledTimes(0)
132
135
  })
133
136
  })
134
137
  })