helia-coord 1.7.2 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/adapters/encryption-adapter.js +3 -0
- package/lib/adapters/pubsub-adapter/messaging.js +2 -3
- package/lib/use-cases/peer-use-cases.js +2 -12
- package/lib/use-cases/relay-use-cases.js +3 -1
- package/package.json +1 -1
- package/test/unit/adapters/encryption-adapter-unit.js +39 -0
- package/test/unit/adapters/gist.unit.adapters.js +35 -0
- package/test/unit/adapters/pubsub/messaging-adapter-unit.js +16 -0
- package/test/unit/adapters/pubsub/pubsub-adapter-unit.js +42 -13
- package/test/unit/controllers/pubsub-controller.unit.js +11 -2
- package/test/unit/use-cases/peer.unit.use-cases.js +211 -0
- package/test/unit/use-cases/pubsub.unit.use-cases.js +16 -0
- package/test/unit/use-cases/relay-use-cases-unit.js +58 -2
|
@@ -72,6 +72,9 @@ class EncryptionAdapter {
|
|
|
72
72
|
if (!peer || !peer.data) {
|
|
73
73
|
throw new Error('Peer public key is not available yet.')
|
|
74
74
|
}
|
|
75
|
+
if (!msg || typeof msg !== 'string') {
|
|
76
|
+
throw new Error('Message is required.')
|
|
77
|
+
}
|
|
75
78
|
|
|
76
79
|
const pubKey = peer.data.encryptPubKey
|
|
77
80
|
// console.log('msg to encrypt: ', msg)
|
|
@@ -237,10 +237,9 @@ class Messaging {
|
|
|
237
237
|
}
|
|
238
238
|
|
|
239
239
|
const peerData = thisNode.peerData.filter(x => x.from === receiver)
|
|
240
|
-
|
|
241
240
|
// 3/24/24 CT - Debugging issue with passing data between ipfs-bch-wallet
|
|
242
241
|
// -service and -consumer.
|
|
243
|
-
if (!peerData || !peerData[0].data) {
|
|
242
|
+
if (!peerData || !peerData[0] || !peerData[0].data) {
|
|
244
243
|
console.log(`peerData[0]: ${JSON.stringify(peerData[0])}`)
|
|
245
244
|
throw new Error('Required encryption information for peer is not available.')
|
|
246
245
|
}
|
|
@@ -275,7 +274,7 @@ class Messaging {
|
|
|
275
274
|
|
|
276
275
|
return outMsgObj
|
|
277
276
|
} catch (err) {
|
|
278
|
-
console.error('Error in generateAckMsg()')
|
|
277
|
+
console.error('Error in generateAckMsg()', err)
|
|
279
278
|
throw err
|
|
280
279
|
}
|
|
281
280
|
}
|
|
@@ -240,7 +240,6 @@ class PeerUseCases {
|
|
|
240
240
|
if (!thisPeerData) return false
|
|
241
241
|
|
|
242
242
|
const dataIndex = this.thisNode.peerData.indexOf(thisPeerData)
|
|
243
|
-
// console.log(`dataIndex: ${dataIndex}`)
|
|
244
243
|
|
|
245
244
|
// If the new announceObj is older than the last announceObj, then
|
|
246
245
|
// ignore it.
|
|
@@ -386,24 +385,19 @@ class PeerUseCases {
|
|
|
386
385
|
// the multiaddrs in the announcement object.
|
|
387
386
|
const filteredMultiaddrs = this.utils.filterMultiaddrs(peerData.data.ipfsMultiaddrs)
|
|
388
387
|
this.adapters.log.statusLog(1, 'filteredMultiaddrs: ', filteredMultiaddrs)
|
|
389
|
-
|
|
390
388
|
for (let j = 0; j < filteredMultiaddrs.length; j++) {
|
|
391
389
|
const multiaddr = filteredMultiaddrs[j]
|
|
392
390
|
this.adapters.log.statusLog(1,
|
|
393
391
|
`Trying a direct connection to peer ${thisPeer} with this multiaddr: ${multiaddr}.`
|
|
394
392
|
)
|
|
395
|
-
|
|
396
393
|
// Attempt to connect to the node through a circuit relay.
|
|
397
394
|
connected = await this.adapters.ipfs.connectToPeer({ multiaddr })
|
|
398
|
-
// console.log('direct connection connected: ', connected)
|
|
399
|
-
|
|
400
395
|
// If the connection was successful, break out of the relay loop.
|
|
401
396
|
// Otherwise try to connect through the next relay.
|
|
402
397
|
if (connected.success) {
|
|
403
398
|
this.adapters.log.statusLog(1,
|
|
404
399
|
`Successfully connected to peer ${thisPeer} through direct connection: ${multiaddr}.`
|
|
405
400
|
)
|
|
406
|
-
|
|
407
401
|
// Add the connection multiaddr for this peer to the thisNode object.
|
|
408
402
|
this.updatePeerConnectionInfo({ thisPeer })
|
|
409
403
|
|
|
@@ -422,7 +416,6 @@ class PeerUseCases {
|
|
|
422
416
|
}
|
|
423
417
|
}
|
|
424
418
|
}
|
|
425
|
-
|
|
426
419
|
if (connected.success) {
|
|
427
420
|
continue
|
|
428
421
|
}
|
|
@@ -443,7 +436,6 @@ class PeerUseCases {
|
|
|
443
436
|
for (let j = 0; j < filteredMultiaddrs.length; j++) {
|
|
444
437
|
const multiaddr = filteredMultiaddrs[j]
|
|
445
438
|
this.adapters.log.statusLog(1, `Trying to connect to peer ${thisPeer} through webRTC multiaddr: ${multiaddr}.`)
|
|
446
|
-
|
|
447
439
|
connected = await this.adapters.ipfs.connectToPeer({ multiaddr })
|
|
448
440
|
|
|
449
441
|
// If the connection was successful, break out of the relay loop.
|
|
@@ -545,7 +537,7 @@ class PeerUseCases {
|
|
|
545
537
|
|
|
546
538
|
return true
|
|
547
539
|
} catch (err) {
|
|
548
|
-
console.error('Error in refreshPeerConnections()')
|
|
540
|
+
console.error('Error in refreshPeerConnections()', err)
|
|
549
541
|
throw err
|
|
550
542
|
}
|
|
551
543
|
}
|
|
@@ -694,20 +686,18 @@ class PeerUseCases {
|
|
|
694
686
|
|
|
695
687
|
// Get multiaddrs that can be used to connect to this node.
|
|
696
688
|
const addrs = this.adapters.ipfs.ipfs.libp2p.getMultiaddrs()
|
|
697
|
-
|
|
698
689
|
const webRtcAddrs = addrs.filter(x => WebRTC.matches(x))
|
|
699
|
-
// console.log('webRTC addrs: ', webRtcAddrs)
|
|
700
690
|
|
|
701
691
|
// If there are any webRTC multiaddrs, add them to the announcement object.
|
|
702
692
|
if (webRtcAddrs.length) {
|
|
703
693
|
// Remove any previous webRTC multiaddrs from the announcement object.
|
|
704
694
|
const existingMultiaddrs = thisNode.ipfsMultiaddrs
|
|
705
695
|
const noRtcAddrs = existingMultiaddrs.filter(x => !x.includes('/p2p-circuit/webrtc'))
|
|
706
|
-
|
|
707
696
|
// Add the new webRTC multiaddrs to the announcement object.
|
|
708
697
|
webRtcAddrs.forEach(x => {
|
|
709
698
|
noRtcAddrs.push(x.toString())
|
|
710
699
|
})
|
|
700
|
+
console.log('noRtcAddrs: ', noRtcAddrs)
|
|
711
701
|
|
|
712
702
|
// Add the new webRTC multiaddrs to the announcement object.
|
|
713
703
|
thisNode.ipfsMultiaddrs = noRtcAddrs
|
|
@@ -204,7 +204,7 @@ class RelayUseCases {
|
|
|
204
204
|
}
|
|
205
205
|
} else {
|
|
206
206
|
// Not connected. Connect to the relay.
|
|
207
|
-
|
|
207
|
+
console.log('Handling zero connected peers')
|
|
208
208
|
// console.log('peerData: ', peerData)
|
|
209
209
|
|
|
210
210
|
// If this node is not connected, try to connect again.
|
|
@@ -216,6 +216,7 @@ class RelayUseCases {
|
|
|
216
216
|
// console.log(`thisRelay.connected: ${thisRelay.connected}`)
|
|
217
217
|
|
|
218
218
|
// Debugging: Trying to figure out the best way to select the multiaddr
|
|
219
|
+
console.log('relay multiaddr: ', thisRelay.multiaddr)
|
|
219
220
|
if (thisRelay.connected && !thisRelay.multiaddr.includes('/tcp/')) {
|
|
220
221
|
console.log('DEBUG: thisRelay.multiaddr: ', thisRelay.multiaddr)
|
|
221
222
|
|
|
@@ -477,6 +478,7 @@ class RelayUseCases {
|
|
|
477
478
|
let peerData = thisNode.peerData.filter(x => {
|
|
478
479
|
if (!x.data) {
|
|
479
480
|
console.log('broken peer data: ', x)
|
|
481
|
+
return false
|
|
480
482
|
}
|
|
481
483
|
// console.log(`x.data.ipfsId: ${x.data.ipfsId}`)
|
|
482
484
|
// console.log(`thisRelay.ipfsId: ${thisRelay.ipfsId}`)
|
package/package.json
CHANGED
|
@@ -126,6 +126,45 @@ describe('#Adapters - Encryption', () => {
|
|
|
126
126
|
assert.include(err.message, 'pubkey must be a hex string')
|
|
127
127
|
}
|
|
128
128
|
})
|
|
129
|
+
it('should throw an error is peer is not provided', async () => {
|
|
130
|
+
try {
|
|
131
|
+
await uut.encryptMsg()
|
|
132
|
+
// console.log('result: ', result)
|
|
133
|
+
|
|
134
|
+
assert.fail('Unexpected code path')
|
|
135
|
+
} catch (err) {
|
|
136
|
+
assert.include(err.message, 'Peer public key is not available yet.')
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
it('should throw an error is msg is not provided', async () => {
|
|
140
|
+
try {
|
|
141
|
+
const peer = {
|
|
142
|
+
data: {
|
|
143
|
+
encryptionKey: 'abc123'
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
await uut.encryptMsg(peer)
|
|
148
|
+
// console.log('result: ', result)
|
|
149
|
+
|
|
150
|
+
assert.fail('Unexpected code path')
|
|
151
|
+
} catch (err) {
|
|
152
|
+
assert.include(err.message, 'Message is required.')
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
it('should throw an error is peer data is not provided', async () => {
|
|
156
|
+
try {
|
|
157
|
+
const peer = {
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
await uut.encryptMsg(peer, 'testMsg')
|
|
161
|
+
// console.log('result: ', result)
|
|
162
|
+
|
|
163
|
+
assert.fail('Unexpected code path')
|
|
164
|
+
} catch (err) {
|
|
165
|
+
assert.include(err.message, 'Peer public key is not available yet.')
|
|
166
|
+
}
|
|
167
|
+
})
|
|
129
168
|
|
|
130
169
|
it('should encrypt a string', async () => {
|
|
131
170
|
// Mock dependencies
|
|
@@ -49,6 +49,41 @@ describe('#Adapter-Gist', () => {
|
|
|
49
49
|
|
|
50
50
|
await uut.getCRList()
|
|
51
51
|
|
|
52
|
+
assert.fail('Unexpected result')
|
|
53
|
+
} catch (err) {
|
|
54
|
+
assert.include(err.message, 'test error')
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
describe('#getCRList2', () => {
|
|
59
|
+
it('should get data from GitHub', async () => {
|
|
60
|
+
// Mock network dependencies
|
|
61
|
+
sandbox.stub(uut.axios, 'get').resolves({
|
|
62
|
+
data: {
|
|
63
|
+
browser: [],
|
|
64
|
+
node: [],
|
|
65
|
+
recommendedPeers: []
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
const result = await uut.getCRList2()
|
|
70
|
+
// console.log('result: ', result)
|
|
71
|
+
|
|
72
|
+
assert.property(result, 'browser')
|
|
73
|
+
assert.property(result, 'node')
|
|
74
|
+
assert.property(result, 'recommendedPeers')
|
|
75
|
+
assert.isArray(result.browser)
|
|
76
|
+
assert.isArray(result.node)
|
|
77
|
+
assert.isArray(result.recommendedPeers)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('should catch and throw errors', async () => {
|
|
81
|
+
try {
|
|
82
|
+
// Force desired code path
|
|
83
|
+
sandbox.stub(uut.axios, 'get').rejects(new Error('test error'))
|
|
84
|
+
|
|
85
|
+
await uut.getCRList2()
|
|
86
|
+
|
|
52
87
|
assert.fail('Unexpected result')
|
|
53
88
|
} catch (err) {
|
|
54
89
|
assert.include(err.message, 'test error')
|
|
@@ -166,6 +166,22 @@ describe('#messaging-adapter', () => {
|
|
|
166
166
|
assert.property(result, 'payload')
|
|
167
167
|
})
|
|
168
168
|
})
|
|
169
|
+
it('should throws error if sender is not found', async () => {
|
|
170
|
+
try {
|
|
171
|
+
const ipfsId = 'unknowPeerId'
|
|
172
|
+
const data = {
|
|
173
|
+
sender: ipfsId,
|
|
174
|
+
uuid: 'fake-uuid'
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
await uut.generateAckMsg(data, thisNode)
|
|
178
|
+
|
|
179
|
+
assert.fail('Unexpected code path')
|
|
180
|
+
} catch (err) {
|
|
181
|
+
console.log(err)
|
|
182
|
+
assert.include(err.message, 'Required encryption information for peer is not available.')
|
|
183
|
+
}
|
|
184
|
+
})
|
|
169
185
|
|
|
170
186
|
it('should catch and throw errors', async () => {
|
|
171
187
|
try {
|
|
@@ -113,7 +113,7 @@ describe('#Adapter - Pubsub', () => {
|
|
|
113
113
|
// Mock dependencies
|
|
114
114
|
sandbox.stub(uut, 'checkForDuplicateMsg').returns(true)
|
|
115
115
|
|
|
116
|
-
const handler = () => {}
|
|
116
|
+
const handler = () => { }
|
|
117
117
|
|
|
118
118
|
const msg = {
|
|
119
119
|
detail: {
|
|
@@ -133,7 +133,7 @@ describe('#Adapter - Pubsub', () => {
|
|
|
133
133
|
// Mock dependencies
|
|
134
134
|
sandbox.stub(uut, 'checkForDuplicateMsg').returns(true)
|
|
135
135
|
|
|
136
|
-
const handler = () => {}
|
|
136
|
+
const handler = () => { }
|
|
137
137
|
|
|
138
138
|
const msg = {
|
|
139
139
|
detail: {
|
|
@@ -197,7 +197,7 @@ describe('#Adapter - Pubsub', () => {
|
|
|
197
197
|
})
|
|
198
198
|
|
|
199
199
|
it('should throw an error if data can not be parsed', async () => {
|
|
200
|
-
const handler = () => {}
|
|
200
|
+
const handler = () => { }
|
|
201
201
|
|
|
202
202
|
// Force desired code path
|
|
203
203
|
mockData.mockMsg.data = Buffer.from('54234', 'hex')
|
|
@@ -212,7 +212,7 @@ describe('#Adapter - Pubsub', () => {
|
|
|
212
212
|
// Mock dependencies
|
|
213
213
|
sandbox.stub(uut, 'checkForDuplicateMsg').returns(false)
|
|
214
214
|
|
|
215
|
-
const handler = () => {}
|
|
215
|
+
const handler = () => { }
|
|
216
216
|
|
|
217
217
|
const msg = {
|
|
218
218
|
detail: {
|
|
@@ -362,8 +362,8 @@ describe('#Adapter - Pubsub', () => {
|
|
|
362
362
|
// sandbox.stub(uut.ipfs.ipfs.pubsub, 'subscribe').resolves()
|
|
363
363
|
|
|
364
364
|
// const chanName = 'test'
|
|
365
|
-
const handler = () => {}
|
|
366
|
-
const parsePubsubMessage = async () => {}
|
|
365
|
+
const handler = () => { }
|
|
366
|
+
const parsePubsubMessage = async () => { }
|
|
367
367
|
|
|
368
368
|
// Instantiate the Broadcast message router library
|
|
369
369
|
const bRouterOptions = {
|
|
@@ -388,8 +388,8 @@ describe('#Adapter - Pubsub', () => {
|
|
|
388
388
|
// sandbox.stub(uut.ipfs.ipfs.pubsub, 'subscribe').resolves()
|
|
389
389
|
|
|
390
390
|
// const chanName = 'test'
|
|
391
|
-
const handler = () => {}
|
|
392
|
-
const parsePubsubMessage = async () => {}
|
|
391
|
+
const handler = () => { }
|
|
392
|
+
const parsePubsubMessage = async () => { }
|
|
393
393
|
|
|
394
394
|
// Instantiate the Broadcast message router library
|
|
395
395
|
const bRouterOptions = {
|
|
@@ -411,7 +411,7 @@ describe('#Adapter - Pubsub', () => {
|
|
|
411
411
|
|
|
412
412
|
it('should catch and handle errors, and return false', async () => {
|
|
413
413
|
// Force and error
|
|
414
|
-
const handler = () => {}
|
|
414
|
+
const handler = () => { }
|
|
415
415
|
const parsePubsubMessage = async () => {
|
|
416
416
|
throw new Error('test error')
|
|
417
417
|
}
|
|
@@ -440,7 +440,7 @@ describe('#Adapter - Pubsub', () => {
|
|
|
440
440
|
const pRouterOptions = {
|
|
441
441
|
thisNode,
|
|
442
442
|
messaging: uut.messaging,
|
|
443
|
-
handleNewMessage: async () => {}
|
|
443
|
+
handleNewMessage: async () => { }
|
|
444
444
|
}
|
|
445
445
|
const privateRouter = new PrivateChannelRouter(pRouterOptions)
|
|
446
446
|
|
|
@@ -460,7 +460,7 @@ describe('#Adapter - Pubsub', () => {
|
|
|
460
460
|
const pRouterOptions = {
|
|
461
461
|
thisNode,
|
|
462
462
|
messaging: uut.messaging,
|
|
463
|
-
handleNewMessage: async () => {}
|
|
463
|
+
handleNewMessage: async () => { }
|
|
464
464
|
}
|
|
465
465
|
const privateRouter = new PrivateChannelRouter(pRouterOptions)
|
|
466
466
|
|
|
@@ -480,7 +480,7 @@ describe('#Adapter - Pubsub', () => {
|
|
|
480
480
|
const pRouterOptions = {
|
|
481
481
|
thisNode,
|
|
482
482
|
messaging: uut.messaging,
|
|
483
|
-
handleNewMessage: async () => {}
|
|
483
|
+
handleNewMessage: async () => { }
|
|
484
484
|
}
|
|
485
485
|
const privateRouter = new PrivateChannelRouter(pRouterOptions)
|
|
486
486
|
|
|
@@ -516,7 +516,7 @@ describe('#Adapter - Pubsub', () => {
|
|
|
516
516
|
it('should subscribe to the coordination channel', async () => {
|
|
517
517
|
const inObj = {
|
|
518
518
|
chanName: globalConfig.DEFAULT_COORDINATION_ROOM,
|
|
519
|
-
handler: () => {}
|
|
519
|
+
handler: () => { }
|
|
520
520
|
}
|
|
521
521
|
|
|
522
522
|
const result = await uut.subscribeToCoordChannel(inObj)
|
|
@@ -538,4 +538,33 @@ describe('#Adapter - Pubsub', () => {
|
|
|
538
538
|
}
|
|
539
539
|
})
|
|
540
540
|
})
|
|
541
|
+
describe('checkForDuplicateMsg', () => {
|
|
542
|
+
it('should return true if msg is not duplicated', () => {
|
|
543
|
+
const msgMock = { detail: { sequenceNumber: 1 } }
|
|
544
|
+
const res = uut.checkForDuplicateMsg(msgMock)
|
|
545
|
+
assert.isTrue(res)
|
|
546
|
+
assert.equal(uut.trackedMsgs.length, 1)
|
|
547
|
+
})
|
|
548
|
+
it('should return falase if msg is duplicated', () => {
|
|
549
|
+
const msgMock = { detail: { sequenceNumber: 1 } }
|
|
550
|
+
|
|
551
|
+
uut.trackedMsgs = [msgMock.detail.sequenceNumber]
|
|
552
|
+
const res = uut.checkForDuplicateMsg(msgMock)
|
|
553
|
+
assert.isFalse(res)
|
|
554
|
+
})
|
|
555
|
+
})
|
|
556
|
+
describe('manageMsgCache', () => {
|
|
557
|
+
it('should shift the trackedMsgs if it exceed the limit', () => {
|
|
558
|
+
uut.TRACKED_MSG_SIZE = 2
|
|
559
|
+
uut.trackedMsgs = [1, 2, 3]
|
|
560
|
+
uut.manageMsgCache()
|
|
561
|
+
assert.equal(uut.trackedMsgs.length, 2)
|
|
562
|
+
})
|
|
563
|
+
it('should keep the trackedMsgs length if it does not exceed the limit', () => {
|
|
564
|
+
uut.TRACKED_MSG_SIZE = 4
|
|
565
|
+
uut.trackedMsgs = [1, 2, 3]
|
|
566
|
+
uut.manageMsgCache()
|
|
567
|
+
assert.equal(uut.trackedMsgs.length, 3)
|
|
568
|
+
})
|
|
569
|
+
})
|
|
541
570
|
})
|
|
@@ -59,9 +59,11 @@ describe('#Pubsub-Controller', () => {
|
|
|
59
59
|
})
|
|
60
60
|
|
|
61
61
|
it('should overwrite default coinjoin channel handler', () => {
|
|
62
|
-
const coinjoinPubsubHandler = ()
|
|
62
|
+
const coinjoinPubsubHandler = sandbox.spy()
|
|
63
63
|
|
|
64
|
-
uut = new PubsubController({ adapters, useCases, coinjoinPubsubHandler })
|
|
64
|
+
const uut = new PubsubController({ adapters, useCases, coinjoinPubsubHandler })
|
|
65
|
+
uut.coinjoinPubsubHandler()
|
|
66
|
+
assert.isTrue(coinjoinPubsubHandler.called)
|
|
65
67
|
})
|
|
66
68
|
})
|
|
67
69
|
|
|
@@ -104,4 +106,11 @@ describe('#Pubsub-Controller', () => {
|
|
|
104
106
|
assert.equal(result, false)
|
|
105
107
|
})
|
|
106
108
|
})
|
|
109
|
+
describe('#coinjoinPubsubHandler (default)', () => {
|
|
110
|
+
it('should return true', async () => {
|
|
111
|
+
const result = await uut.coinjoinPubsubHandler()
|
|
112
|
+
|
|
113
|
+
assert.isTrue(result)
|
|
114
|
+
})
|
|
115
|
+
})
|
|
107
116
|
})
|
|
@@ -262,6 +262,34 @@ describe('#Use-Cases-Peer', () => {
|
|
|
262
262
|
// peerData array should only have one peer.
|
|
263
263
|
assert.equal(uut.thisNode.peerData.length, 1)
|
|
264
264
|
})
|
|
265
|
+
it('should persit the multiaddr if it exist', async () => {
|
|
266
|
+
// Mock dependencies
|
|
267
|
+
sandbox.stub(uut, 'isFreshPeer').returns(true)
|
|
268
|
+
|
|
269
|
+
const announceObj = {
|
|
270
|
+
from: 'peerId',
|
|
271
|
+
data: {
|
|
272
|
+
jsonLd: {
|
|
273
|
+
name: 'test'
|
|
274
|
+
},
|
|
275
|
+
orbitdb: 'orbitdbId'
|
|
276
|
+
},
|
|
277
|
+
multiaddr: ['/ip4/123.45.6.7/p2p/ipfs-id']
|
|
278
|
+
}
|
|
279
|
+
uut.updateThisNode({ thisNode })
|
|
280
|
+
|
|
281
|
+
// Add the new peer
|
|
282
|
+
uut.thisNode.peerList.push(announceObj.from)
|
|
283
|
+
uut.thisNode.peerData.push(announceObj)
|
|
284
|
+
|
|
285
|
+
const result = await uut.addSubnetPeer(announceObj)
|
|
286
|
+
// console.log('result: ', result)
|
|
287
|
+
|
|
288
|
+
assert.equal(result, true)
|
|
289
|
+
|
|
290
|
+
// peerData array should only have one peer.
|
|
291
|
+
assert.equal(uut.thisNode.peerData.length, 1)
|
|
292
|
+
})
|
|
265
293
|
|
|
266
294
|
it('should return false if existing peer can not be found', async () => {
|
|
267
295
|
// Mock dependencies
|
|
@@ -399,6 +427,13 @@ describe('#Use-Cases-Peer', () => {
|
|
|
399
427
|
|
|
400
428
|
assert.equal(result, true)
|
|
401
429
|
})
|
|
430
|
+
it('should handle Error', () => {
|
|
431
|
+
try {
|
|
432
|
+
uut.isFreshPeer(null)
|
|
433
|
+
} catch (error) {
|
|
434
|
+
assert.include(error.message, 'Cannot read properties of null')
|
|
435
|
+
}
|
|
436
|
+
})
|
|
402
437
|
})
|
|
403
438
|
|
|
404
439
|
describe('#refreshPeerConnections', () => {
|
|
@@ -622,6 +657,84 @@ describe('#Use-Cases-Peer', () => {
|
|
|
622
657
|
assert.include(err.message, 'test error')
|
|
623
658
|
}
|
|
624
659
|
})
|
|
660
|
+
it('should update logs on fails direct connection', async () => {
|
|
661
|
+
// Add a circuit relay peer with advertised IP and port.
|
|
662
|
+
const ipfsId = 'QmbyYXKbnAmMbMGo8LRBZ58jYs58anqUzY1m4jxDmhDsje'
|
|
663
|
+
uut.thisNode.peerList = [ipfsId]
|
|
664
|
+
uut.thisNode.peerData = [{ from: ipfsId, data: { ipfsMultiaddrs: [] } }]
|
|
665
|
+
|
|
666
|
+
// Add a peer
|
|
667
|
+
await uut.addSubnetPeer(mockData.announceObj)
|
|
668
|
+
|
|
669
|
+
// Force circuit relay to be used.
|
|
670
|
+
uut.thisNode.relayData = mockData.mockRelayData
|
|
671
|
+
|
|
672
|
+
// Mock dependencies
|
|
673
|
+
sandbox.stub(uut.adapters.ipfs, 'getPeers').resolves(mockData.swarmPeers)
|
|
674
|
+
sandbox.stub(uut, 'isFreshPeer').returns(true)
|
|
675
|
+
sandbox.stub(uut.adapters.ipfs, 'connectToPeer')
|
|
676
|
+
.onCall(0).resolves({ success: false })
|
|
677
|
+
.resolves({ success: true })
|
|
678
|
+
sandbox.stub(uut.utils, 'filterMultiaddrs').returns(['/ip4/123.45.6.7/p2p/ipfs-id'])
|
|
679
|
+
sandbox.stub(uut, 'updatePeerConnectionInfo').returns()
|
|
680
|
+
|
|
681
|
+
// Connect to that peer.
|
|
682
|
+
const result = await uut.refreshPeerConnections()
|
|
683
|
+
|
|
684
|
+
assert.equal(result, true)
|
|
685
|
+
})
|
|
686
|
+
it('should connect to circuit relay ', async () => {
|
|
687
|
+
// Add a circuit relay peer with advertised IP and port.
|
|
688
|
+
const ipfsId = 'QmbyYXKbnAmMbMGo8LRBZ58jYs58anqUzY1m4jxDmhDsje'
|
|
689
|
+
uut.thisNode.peerList = [ipfsId]
|
|
690
|
+
uut.thisNode.peerData = [{ from: ipfsId, data: { ipfsMultiaddrs: ['/p2p-circuit/ip4/123.45.6.7/tcp/4001/p2p/ipfs-id'] } }]
|
|
691
|
+
|
|
692
|
+
// Add a peer
|
|
693
|
+
await uut.addSubnetPeer(mockData.announceObj)
|
|
694
|
+
|
|
695
|
+
// Force circuit relay to be used.
|
|
696
|
+
uut.thisNode.relayData = mockData.mockRelayData
|
|
697
|
+
|
|
698
|
+
// Mock dependencies
|
|
699
|
+
sandbox.stub(uut.adapters.ipfs, 'getPeers').resolves(mockData.swarmPeers)
|
|
700
|
+
sandbox.stub(uut, 'isFreshPeer').returns(true)
|
|
701
|
+
sandbox.stub(uut.adapters.ipfs, 'connectToPeer')
|
|
702
|
+
.onCall(0).resolves({ success: false })
|
|
703
|
+
.resolves({ success: true })
|
|
704
|
+
sandbox.stub(uut.utils, 'filterMultiaddrs').returns(['/ip4/123.45.6.7/p2p/ipfs-id'])
|
|
705
|
+
sandbox.stub(uut, 'updatePeerConnectionInfo').returns()
|
|
706
|
+
|
|
707
|
+
// Connect to that peer.
|
|
708
|
+
const result = await uut.refreshPeerConnections()
|
|
709
|
+
|
|
710
|
+
assert.equal(result, true)
|
|
711
|
+
})
|
|
712
|
+
it('should update logs on fails circuit relay connection', async () => {
|
|
713
|
+
// Add a circuit relay peer with advertised IP and port.
|
|
714
|
+
const ipfsId = 'QmbyYXKbnAmMbMGo8LRBZ58jYs58anqUzY1m4jxDmhDsje'
|
|
715
|
+
uut.thisNode.peerList = [ipfsId]
|
|
716
|
+
uut.thisNode.peerData = [{ from: ipfsId, data: { ipfsMultiaddrs: ['/p2p-circuit/ip4/123.45.6.7/tcp/4001/p2p/ipfs-id'] } }]
|
|
717
|
+
|
|
718
|
+
// Add a peer
|
|
719
|
+
await uut.addSubnetPeer(mockData.announceObj)
|
|
720
|
+
|
|
721
|
+
// Force circuit relay to be used.
|
|
722
|
+
uut.thisNode.relayData = mockData.mockRelayData
|
|
723
|
+
|
|
724
|
+
// Mock dependencies
|
|
725
|
+
sandbox.stub(uut.adapters.ipfs, 'getPeers').resolves(mockData.swarmPeers)
|
|
726
|
+
sandbox.stub(uut, 'isFreshPeer').returns(true)
|
|
727
|
+
sandbox.stub(uut.adapters.ipfs, 'connectToPeer')
|
|
728
|
+
.onCall(0).resolves({ success: false })
|
|
729
|
+
.resolves({ success: false })
|
|
730
|
+
sandbox.stub(uut.utils, 'filterMultiaddrs').returns(['/ip4/123.45.6.7/p2p/ipfs-id'])
|
|
731
|
+
sandbox.stub(uut, 'updatePeerConnectionInfo').returns()
|
|
732
|
+
|
|
733
|
+
// Connect to that peer.
|
|
734
|
+
const result = await uut.refreshPeerConnections()
|
|
735
|
+
|
|
736
|
+
assert.equal(result, true)
|
|
737
|
+
})
|
|
625
738
|
})
|
|
626
739
|
|
|
627
740
|
describe('#sendRPC', () => {
|
|
@@ -733,4 +846,102 @@ describe('#Use-Cases-Peer', () => {
|
|
|
733
846
|
assert.equal(result, false)
|
|
734
847
|
})
|
|
735
848
|
})
|
|
849
|
+
|
|
850
|
+
describe('#updatePeerConnectionInfo', () => {
|
|
851
|
+
it('should update the peer connection info', async () => {
|
|
852
|
+
uut.thisNode.peerData = [{ from: 'peerId' }]
|
|
853
|
+
sandbox.stub(uut.adapters.ipfs.ipfs.libp2p, 'getConnections').returns([{ remoteAddr: '/ip4/123.45.6.7/p2p/ipfs-id' }])
|
|
854
|
+
const result = await uut.updatePeerConnectionInfo({ thisPeer: 'peerId' })
|
|
855
|
+
assert.equal(result, true)
|
|
856
|
+
assert.equal(uut.thisNode.peerData[0].multiaddr, '/ip4/123.45.6.7/p2p/ipfs-id')
|
|
857
|
+
})
|
|
858
|
+
it('should return false if the peer is not connected', async () => {
|
|
859
|
+
uut.thisNode.peerData = [{ from: 'peerId' }]
|
|
860
|
+
sandbox.stub(uut.adapters.ipfs.ipfs.libp2p, 'getConnections').returns([])
|
|
861
|
+
const result = await uut.updatePeerConnectionInfo({ thisPeer: 'peerId' })
|
|
862
|
+
assert.equal(result, false)
|
|
863
|
+
})
|
|
864
|
+
it('should return false if the peer is not found', async () => {
|
|
865
|
+
uut.thisNode.peerData = [{ from: 'peerId' }]
|
|
866
|
+
sandbox.stub(uut.adapters.ipfs.ipfs.libp2p, 'getConnections').returns([{ remoteAddr: '/ip4/123.45.6.7/p2p/ipfs-id' }])
|
|
867
|
+
const result = await uut.updatePeerConnectionInfo({ thisPeer: 'peerId2' })
|
|
868
|
+
assert.equal(result, false)
|
|
869
|
+
})
|
|
870
|
+
it('should handle errors', async () => {
|
|
871
|
+
try {
|
|
872
|
+
uut.thisNode.peerData = [{ from: 'peerId' }]
|
|
873
|
+
sandbox.stub(uut.adapters.ipfs.ipfs.libp2p, 'getConnections').throws(new Error('test error'))
|
|
874
|
+
await uut.updatePeerConnectionInfo({ thisPeer: 'peerId' })
|
|
875
|
+
assert.fail('Unexpected code path')
|
|
876
|
+
} catch (error) {
|
|
877
|
+
assert.include(error.message, 'test error')
|
|
878
|
+
}
|
|
879
|
+
})
|
|
880
|
+
})
|
|
881
|
+
describe('#getWebRtcMultiaddr', () => {
|
|
882
|
+
it('should return the webRTC multiaddr', async () => {
|
|
883
|
+
const thisNodeMock = thisNode
|
|
884
|
+
const webRtcMultiaddr1 = '/ip4/127.0.0.1/tcp/5000/ws/p2p-circuit/webrtc/p2p/ipfs-id'
|
|
885
|
+
const webRtcMultiaddr2 = '/ip4/127.0.0.1/tcp/5001/ws/p2p-circuit/webrtc/p2p/ipfs-id2'
|
|
886
|
+
|
|
887
|
+
thisNodeMock.ipfsMultiaddrs = ['/ip4/123.45.6.7/p2p/ipfs-id', webRtcMultiaddr1, webRtcMultiaddr2]
|
|
888
|
+
sandbox.stub(uut.adapters.ipfs.ipfs.libp2p, 'getMultiaddrs').returns([webRtcMultiaddr2])
|
|
889
|
+
|
|
890
|
+
await uut.getWebRtcMultiaddr({ thisNode: thisNodeMock })
|
|
891
|
+
|
|
892
|
+
assert.equal(thisNodeMock.ipfsMultiaddrs.length, 2)
|
|
893
|
+
assert.equal(thisNodeMock.ipfsMultiaddrs[0], '/ip4/123.45.6.7/p2p/ipfs-id')
|
|
894
|
+
assert.equal(thisNodeMock.ipfsMultiaddrs[1], webRtcMultiaddr2, 'expected webRTC multiaddr to be updated')
|
|
895
|
+
})
|
|
896
|
+
it('should return false on error', async () => {
|
|
897
|
+
const result = await uut.getWebRtcMultiaddr(null)
|
|
898
|
+
assert.equal(result, false)
|
|
899
|
+
})
|
|
900
|
+
})
|
|
901
|
+
describe('#getMultiaddrs', () => {
|
|
902
|
+
it('should filter multiaddrs with a low chance of success', async () => {
|
|
903
|
+
const webRtcMultiaddr1 = '/ip4/157.178.192.100/tcp/5000/ws/p2p-circuit/webrtc/p2p/ipfs-id'
|
|
904
|
+
const lowChanceMultiaddrsExamples = [
|
|
905
|
+
'/ip4/127.0.0.1/tcp/4001/p2p/QmHash', // localhost
|
|
906
|
+
'/ip4/192.168.1.100/tcp/4001/p2p/QmHash', // private network (192.168.x.x)
|
|
907
|
+
'/ip4/172.16.0.100/tcp/4001/p2p/QmHash', // private network (172.16-31.x.x)
|
|
908
|
+
'/ip4/10.0.0.100/tcp/4001/p2p/QmHash', // private network (10.x.x.x)
|
|
909
|
+
'/ip4/1.2.3.4/p2p/4001/quic/p2p/QmHash', // QUIC protocol
|
|
910
|
+
'/ip4/1.2.3.4/udp/4001/p2p/QmHash', // UDP protocol
|
|
911
|
+
'/ip4/192.168.0.100/udp/4001/quic/p2p/QmHash', // private network with QUIC
|
|
912
|
+
'/ip4/172.20.0.100/udp/4001/p2p/QmHash' // private network with UDP
|
|
913
|
+
|
|
914
|
+
]
|
|
915
|
+
sandbox.stub(uut.adapters.ipfs.ipfs.libp2p, 'getMultiaddrs').returns([...lowChanceMultiaddrsExamples, webRtcMultiaddr1])
|
|
916
|
+
const result = await uut.getMultiaddrs()
|
|
917
|
+
assert.equal(result.length, 1)
|
|
918
|
+
assert.equal(result[0], webRtcMultiaddr1)
|
|
919
|
+
})
|
|
920
|
+
it('should return empty array on error', async () => {
|
|
921
|
+
sandbox.stub(uut.adapters.ipfs.ipfs.libp2p, 'getMultiaddrs').throws(new Error('test error'))
|
|
922
|
+
const result = await uut.getMultiaddrs()
|
|
923
|
+
assert.equal(result.length, 0)
|
|
924
|
+
})
|
|
925
|
+
})
|
|
926
|
+
describe('relayMetricsHandler', () => {
|
|
927
|
+
it('should return true', async () => {
|
|
928
|
+
uut.incomingData = 'data'
|
|
929
|
+
await uut.relayMetricsHandler('updated data')
|
|
930
|
+
assert.equal(uut.incomingData, 'updated data')
|
|
931
|
+
})
|
|
932
|
+
})
|
|
933
|
+
|
|
934
|
+
describe('updateThisNode', () => {
|
|
935
|
+
it('should update the thisNode object', async () => {
|
|
936
|
+
const thisNodeMock = { data: 'mock node' }
|
|
937
|
+
const result = await uut.updateThisNode({ thisNode: thisNodeMock })
|
|
938
|
+
assert.equal(result, true)
|
|
939
|
+
assert.equal(uut.thisNode.data, 'mock node')
|
|
940
|
+
})
|
|
941
|
+
it('should keep as default if no data is provided', async () => {
|
|
942
|
+
const result = await uut.updateThisNode()
|
|
943
|
+
assert.equal(result, true)
|
|
944
|
+
assert.isUndefined(uut.thisNode)
|
|
945
|
+
})
|
|
946
|
+
})
|
|
736
947
|
})
|
|
@@ -107,6 +107,22 @@ describe('#pubsub-Use-Cases', () => {
|
|
|
107
107
|
|
|
108
108
|
assert.equal(result, true)
|
|
109
109
|
})
|
|
110
|
+
it('should throw an error if controllers are not provided', async () => {
|
|
111
|
+
try {
|
|
112
|
+
sandbox.stub(uut.adapters.pubsub, 'subscribeToCoordChannel').resolves()
|
|
113
|
+
|
|
114
|
+
uut.updateThisNode({
|
|
115
|
+
pubsubChannels: []
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
await uut.initializePubsub()
|
|
119
|
+
|
|
120
|
+
assert.fail('Unexpected code path')
|
|
121
|
+
} catch (err) {
|
|
122
|
+
// console.log(err)
|
|
123
|
+
assert.include(err.message, 'Instance of controllers must be passed to initializePubsub()')
|
|
124
|
+
}
|
|
125
|
+
})
|
|
110
126
|
|
|
111
127
|
it('should catch and throw an error', async () => {
|
|
112
128
|
try {
|
|
@@ -172,6 +172,35 @@ describe('#relay-Use-Cases', () => {
|
|
|
172
172
|
// Assert function executed successfully.
|
|
173
173
|
assert.equal(result, true)
|
|
174
174
|
})
|
|
175
|
+
it('Should debug when relay connection is not over tcp', async () => {
|
|
176
|
+
// Add a relay that is not over tcp
|
|
177
|
+
thisNode.relayData.push(
|
|
178
|
+
{
|
|
179
|
+
multiaddr:
|
|
180
|
+
'/ip4/139.162.76.54/udp/5269/ws/p2p/QmaKzQTAtoJWYMiG5ATx41uWsMajr1kSxRdtg919s8fK77',
|
|
181
|
+
connected: true,
|
|
182
|
+
updatedAt: '2021-09-20T15:59:12.961Z',
|
|
183
|
+
ipfsId: 'QmaKzQTAtoJWYMiG5ATx41uWsMajr1kSxRdtg919s8fK77',
|
|
184
|
+
isBootstrap: false,
|
|
185
|
+
metrics: { aboutLatency: [] },
|
|
186
|
+
latencyScore: 10000
|
|
187
|
+
}
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
// Mock dependencies
|
|
191
|
+
sandbox.stub(uut, 'sortRelays').returns(thisNode.relayData)
|
|
192
|
+
sandbox.stub(uut.adapters.ipfs, 'getPeers').resolves(mockData.connectedPeerList01)
|
|
193
|
+
sandbox.stub(uut.adapters.ipfs, 'connectToPeer').resolves({ success: true })
|
|
194
|
+
sandbox.stub(uut.adapters.ipfs.ipfs.libp2p, 'getConnections').returns([{
|
|
195
|
+
remoteAddr: '/ip4/116.203.193.74/udp/4001/ipfs/QmNZktxkfScScnHCFSGKELH3YRqdxHQ3Le9rAoRLhZ6vgL/p2p-circuit'
|
|
196
|
+
}])
|
|
197
|
+
|
|
198
|
+
// uut.state.relays = crMockData.circuitRelays
|
|
199
|
+
const result = await uut.connectToCRs(thisNode)
|
|
200
|
+
|
|
201
|
+
// Assert function executed successfully.
|
|
202
|
+
assert.equal(result, true)
|
|
203
|
+
})
|
|
175
204
|
|
|
176
205
|
it('Should skip relays that are already connected', async () => {
|
|
177
206
|
// Force circuit relay to be used.
|
|
@@ -246,7 +275,7 @@ describe('#relay-Use-Cases', () => {
|
|
|
246
275
|
sandbox.stub(uut, 'sortRelays').returns(thisNode.relayData)
|
|
247
276
|
sandbox.stub(uut.adapters.ipfs, 'getPeers').resolves(mockData.connectedPeerList01)
|
|
248
277
|
sandbox.stub(uut.adapters.ipfs.ipfs.libp2p, 'getConnections').returns([{
|
|
249
|
-
remoteAddr: '/ip4/116.203.193.74/tcp/4001/ipfs/QmNZktxkfScScnHCFSGKELH3YRqdxHQ3Le9rAoRLhZ6vgL'
|
|
278
|
+
remoteAddr: '/ip4/116.203.193.74/tcp/4001/ipfs/QmNZktxkfScScnHCFSGKELH3YRqdxHQ3Le9rAoRLhZ6vgL/p2p-circuit'
|
|
250
279
|
}])
|
|
251
280
|
|
|
252
281
|
// uut.state.relays = crMockData.circuitRelays
|
|
@@ -628,7 +657,7 @@ describe('#relay-Use-Cases', () => {
|
|
|
628
657
|
|
|
629
658
|
// Mock dependencies
|
|
630
659
|
sandbox.stub(uut.adapters.bch.bchjs.Util, 'sleep').resolves()
|
|
631
|
-
sandbox.stub(uut.
|
|
660
|
+
sandbox.stub(uut.peerUseCases, 'queryAbout').resolves(true)
|
|
632
661
|
|
|
633
662
|
await uut.measureRelays(thisNode)
|
|
634
663
|
// console.log(
|
|
@@ -674,6 +703,33 @@ describe('#relay-Use-Cases', () => {
|
|
|
674
703
|
// First element of '1' should have been shifted out and replaced by '2'
|
|
675
704
|
assert.equal(thisNode.relayData[0].metrics.aboutLatency[0], 2)
|
|
676
705
|
})
|
|
706
|
+
|
|
707
|
+
it('should ignore a broken peer data', async () => {
|
|
708
|
+
// Mock test data
|
|
709
|
+
const thisNode = {
|
|
710
|
+
relayData: [
|
|
711
|
+
{
|
|
712
|
+
ipfsId: 'testId',
|
|
713
|
+
isBootstrap: false,
|
|
714
|
+
connected: false,
|
|
715
|
+
metrics: {
|
|
716
|
+
aboutLatency: []
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
],
|
|
720
|
+
peerData: [
|
|
721
|
+
|
|
722
|
+
{
|
|
723
|
+
data: null
|
|
724
|
+
}
|
|
725
|
+
]
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
await uut.measureRelays(thisNode)
|
|
729
|
+
// console.log('thisNode: ', thisNode)
|
|
730
|
+
|
|
731
|
+
assert.equal(thisNode.relayData[0].metrics.aboutLatency.length, 0)
|
|
732
|
+
})
|
|
677
733
|
})
|
|
678
734
|
|
|
679
735
|
describe('#sortRelays', () => {
|