This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathfind-peer.js
76 lines (58 loc) · 2.26 KB
/
find-peer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import testTimeout from '../utils/test-timeout.js'
import drain from 'it-drain'
import all from 'it-all'
import { ensureReachable } from './utils.js'
import { peerIdFromString } from '@libp2p/peer-id'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testFindPeer (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.dht.findPeer', function () {
this.timeout(80 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let nodeA
/** @type {import('ipfs-core-types').IPFS} */
let nodeB
before(async () => {
nodeA = (await factory.spawn()).api
nodeB = (await factory.spawn()).api
await ensureReachable(nodeA, nodeB)
})
after(() => factory.clean())
it('should respect timeout option when finding a peer on the DHT', async () => {
const nodeBId = await nodeB.id()
await testTimeout(() => drain(nodeA.dht.findPeer(nodeBId.id, {
timeout: 1
})))
})
it('should find other peers', async () => {
const nodeBId = await nodeB.id()
const results = await all(nodeA.dht.findPeer(nodeBId.id))
const finalPeer = results.filter(event => event.name === 'FINAL_PEER').pop()
if (!finalPeer || finalPeer.name !== 'FINAL_PEER') {
throw new Error('No finalPeer event received')
}
const id = finalPeer.peer.id
const nodeAddresses = nodeBId.addresses.map((addr) => addr.nodeAddress())
const peerAddresses = finalPeer.peer.multiaddrs.map(ma => ma.nodeAddress())
expect(id.toString()).to.equal(nodeBId.id.toString())
expect(peerAddresses).to.deep.include(nodeAddresses[0])
})
it('should fail to find other peer if peer does not exist', async () => {
const events = await all(nodeA.dht.findPeer(peerIdFromString('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ')))
// no finalPeer events found
expect(events.filter(event => event.name === 'FINAL_PEER')).to.be.empty()
// queryError events found
expect(events.filter(event => event.name === 'QUERY_ERROR')).to.not.be.empty()
})
})
}