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 pathpubsub.js
199 lines (153 loc) · 5.95 KB
/
pubsub.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import { peerIdFromString, peerIdFromKeys } from '@libp2p/peer-id'
import { isNode } from 'ipfs-utils/src/env.js'
import * as ipns from 'ipns'
import delay from 'delay'
import last from 'it-last'
import waitFor from '../utils/wait-for.js'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { ipnsValidator } from 'ipns/validator'
const namespace = '/record/'
const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'
const daemonsOptions = {
ipfsOptions: {
EXPERIMENTAL: {
ipnsPubsub: true
}
}
}
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
* @typedef {import('@libp2p/interface-pubsub').Message} Message
* @typedef {import('@libp2p/interfaces/events').EventHandler<Message>} EventHandler
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testPubsub (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.name.pubsub', () => {
// TODO make this work in the browser and between daemon and in-proc in nodes
if (!isNode) return
let nodes
/** @type {import('ipfs-core-types').IPFS} */
let nodeA
/** @type {import('ipfs-core-types').IPFS} */
let nodeB
/** @type {import('ipfs-core-types/src/root').IDResult} */
let idA
/** @type {import('ipfs-core-types/src/root').IDResult} */
let idB
before(async function () {
this.timeout(120 * 1000)
nodes = await Promise.all([
factory.spawn({ ...daemonsOptions }),
factory.spawn({ ...daemonsOptions })
])
nodeA = nodes[0].api
nodeB = nodes[1].api
const ids = await Promise.all([
nodeA.id(),
nodeB.id()
])
idA = ids[0]
idB = ids[1]
await nodeA.swarm.connect(idB.addresses[0])
await waitFor(async () => {
const res = await nodeB.swarm.peers()
return res.map(p => p.peer.toString()).includes(idA.id.toString())
}, { name: 'node A dialed node B' })
})
after(() => factory.clean())
it('should publish and then resolve correctly', async function () {
this.timeout(80 * 1000)
const routingKey = ipns.peerIdToRoutingKey(idA.id)
const topic = `${namespace}${uint8ArrayToString(routingKey, 'base64url')}`
await nodeB.pubsub.subscribe(topic, () => {})
// wait for nodeA to see nodeB's subscription
await waitFor(async () => {
const peers = await nodeA.pubsub.peers(topic)
return peers.map(p => p.toString()).includes(idB.id.toString())
})
await nodeA.name.publish(ipfsRef, { resolve: false })
await delay(1000) // guarantee record is written
const res = await last(nodeB.name.resolve(idA.id))
expect(res).to.equal(ipfsRef)
})
it('should self resolve, publish and then resolve correctly', async function () {
this.timeout(6000)
const emptyDirCid = '/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
const { path } = await nodeA.add(uint8ArrayFromString('pubsub records'))
const resolvesEmpty = await last(nodeB.name.resolve(idB.id))
expect(resolvesEmpty).to.be.eq(emptyDirCid)
const publish = await nodeB.name.publish(path)
expect(publish).to.be.eql({
name: idB.id.toString(),
value: `/ipfs/${path}`
})
const resolveB = await last(nodeB.name.resolve(idB.id))
expect(resolveB).to.be.eq(`/ipfs/${path}`)
await delay(1000)
const resolveA = await last(nodeA.name.resolve(idB.id))
expect(resolveA).to.be.eq(`/ipfs/${path}`)
})
it('should handle event on publish correctly', async function () {
this.timeout(80 * 1000)
const testAccountName = 'test-account'
/**
* @type {import('@libp2p/interface-pubsub').Message}
*/
let publishedMessage
/**
* @type {EventHandler}
*/
const checkMessage = (msg) => {
publishedMessage = msg
}
const alreadySubscribed = () => {
return Boolean(publishedMessage)
}
// Create account for publish
const testAccount = await nodeA.key.gen(testAccountName, {
type: 'rsa',
size: 2048,
'ipns-base': 'b58mh'
})
const routingKey = ipns.peerIdToRoutingKey(peerIdFromString(testAccount.id))
const topic = `${namespace}${uint8ArrayToString(routingKey, 'base64url')}`
await nodeB.pubsub.subscribe(topic, checkMessage)
// wait for nodeA to see nodeB's subscription
await waitFor(async () => {
const peers = await nodeA.pubsub.peers(topic)
return peers.map(p => p.toString()).includes(idB.id.toString())
})
await nodeA.name.publish(ipfsRef, { resolve: false, key: testAccountName })
await waitFor(alreadySubscribed)
// @ts-expect-error publishedMessage is set in handler
if (!publishedMessage) {
throw new Error('Pubsub handler not invoked')
}
const publishedMessageData = ipns.unmarshal(publishedMessage.data)
if (publishedMessage.type !== 'signed') {
throw new Error('Message was not signed')
}
if (publishedMessageData.pubKey == null) {
throw new Error('Public key was missing from published message data')
}
const messageKey = publishedMessage.from
const pubKeyPeerId = await peerIdFromKeys(publishedMessageData.pubKey)
expect(pubKeyPeerId.toString()).not.to.equal(messageKey.toString())
expect(pubKeyPeerId.toString()).to.equal(testAccount.id)
expect(publishedMessage.from.toString()).to.equal(idA.id.toString())
expect(messageKey.toString()).to.equal(idA.id.toString())
expect(uint8ArrayToString(publishedMessageData.value)).to.equal(ipfsRef)
// Verify the signature
await ipnsValidator(ipns.peerIdToRoutingKey(pubKeyPeerId), publishedMessage.data)
})
})
}