This repository has been 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.3k
/
Copy pathpeers.js
158 lines (132 loc) · 4.97 KB
/
peers.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* eslint-env mocha */
import { isMultiaddr } from '@multiformats/multiaddr'
import delay from 'delay'
import { isBrowser, isWebWorker } from 'ipfs-utils/src/env.js'
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {import('ipfs-core-types/src/swarm').PeersResult[]} peers
*/
function peersAreUnique (peers) {
const peerSet = new Set()
peers.forEach(peer => {
peerSet.add(peer.peer.toString())
})
expect(peerSet).to.have.lengthOf(peers.length)
}
/**
* @param {Factory} factory
* @param {object} options
*/
export function testPeers (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.swarm.peers', function () {
this.timeout(80 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let ipfsA
/** @type {import('ipfs-core-types').IPFS} */
let ipfsB
/** @type {import('ipfs-core-types/src/root').IDResult} */
let ipfsBId
before(async () => {
ipfsA = (await factory.spawn({ type: 'proc' })).api
ipfsB = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api
ipfsBId = await ipfsB.id()
await ipfsA.swarm.connect(ipfsBId.addresses[0])
/* TODO: Seen if we still need this after this is fixed
/~https://github.com/ipfs/js-ipfs/issues/2601 gets resolved */
// await delay(60 * 1000) // wait for open streams in the connection available
})
after(() => factory.clean())
it('should list peers this node is connected to', async () => {
const peers = await ipfsA.swarm.peers()
expect(peers).to.have.length.above(0)
const peer = peers[0]
expect(peer).to.have.a.property('addr')
expect(isMultiaddr(peer.addr)).to.equal(true)
expect(peer).to.have.a.property('peer')
expect(peer.peer).to.be.ok()
expect(peer).to.not.have.a.property('latency')
/* TODO: These assertions must be uncommented as soon as
/~https://github.com/ipfs/js-ipfs/issues/2601 gets resolved */
// expect(peer).to.have.a.property('muxer')
// expect(peer).to.not.have.a.property('streams')
})
it('should list peers this node is connected to with verbose option', async () => {
const peers = await ipfsA.swarm.peers({ verbose: true })
expect(peers).to.have.length.above(0)
const peer = peers[0]
expect(peer).to.have.a.property('addr')
expect(isMultiaddr(peer.addr)).to.equal(true)
expect(peer).to.have.a.property('peer')
expect(peer).to.have.a.property('latency')
expect(peer.latency).to.match(/n\/a|[0-9]+[mµ]?s/) // n/a or 3ms or 3µs or 3s
/* TODO: These assertions must be uncommented as soon as
/~https://github.com/ipfs/js-ipfs/issues/2601 gets resolved */
// expect(peer).to.have.a.property('muxer')
// expect(peer).to.have.a.property('streams')
})
/**
* @param {string | string[]} addrs
* @returns
*/
function getConfig (addrs) {
addrs = Array.isArray(addrs) ? addrs : [addrs]
return {
Addresses: {
Swarm: addrs,
API: '/ip4/127.0.0.1/tcp/0',
Gateway: '/ip4/127.0.0.1/tcp/0'
},
Bootstrap: [],
Discovery: {
MDNS: {
Enabled: false
}
}
}
}
it('should list peers only once', async () => {
const nodeA = (await factory.spawn({ type: 'proc' })).api
const nodeB = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api
const nodeBId = await nodeB.id()
await nodeA.swarm.connect(nodeBId.addresses[0])
await delay(1000)
peersAreUnique(await nodeA.swarm.peers())
peersAreUnique(await nodeB.swarm.peers())
})
it('should list peers only once even if they have multiple addresses', async () => {
// TODO: Change to port 0, needs: /~https://github.com/ipfs/interface-ipfs-core/issues/152
const config = getConfig(isBrowser && factory.opts.type !== 'go'
? [
`${process.env.SIGNALA_SERVER}`,
`${process.env.SIGNALB_SERVER}`
]
: [
'/ip4/127.0.0.1/tcp/26545/ws',
'/ip4/127.0.0.1/tcp/26546/ws'
])
const nodeA = (await factory.spawn({
// browser nodes have webrtc-star addresses which can't be dialled by go so make the other
// peer a js-ipfs node to get a tcp address that can be dialled. Also, webworkers are not
// diable so don't use a in-proc node for webworkers
type: ((isBrowser && factory.opts.type === 'go') || isWebWorker) ? 'js' : 'proc'
})).api
const nodeAId = await nodeA.id()
const nodeB = (await factory.spawn({
type: isWebWorker ? 'go' : undefined,
ipfsOptions: {
config
}
})).api
await nodeB.swarm.connect(nodeAId.addresses[0])
await delay(1000)
peersAreUnique(await nodeA.swarm.peers())
peersAreUnique(await nodeB.swarm.peers())
})
})
}