This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
pinning.test.js
362 lines (334 loc) · 13.1 KB
/
pinning.test.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
jest.mock('3id-resolver', () => {
const { getMock3idResolver } = require('./mock3id')
return { getResolver: getMock3idResolver }
})
const Pinning = require('../pinning')
const EventEmitter = require('events')
const IPFS = require('ipfs')
const defaultsDeep = require('lodash.defaultsdeep')
const tmp = require('tmp-promise')
tmp.setGracefulCleanup()
jest.mock('redis', () => { return require('redis-mock') })
const TestClient = require('./testClient')
// Needed for ipfs spinup/teardown
jest.setTimeout(15000)
const pinningIpfsConfig = {
Bootstrap: [],
Addresses: {
Swarm: [
'/ip4/127.0.0.1/tcp/4002',
'/ip4/127.0.0.1/tcp/4003/ws'
]
}
}
const analyticsMock = {
trackPinDB: jest.fn(),
trackSyncDB: jest.fn(),
trackSpaceUpdate: jest.fn(),
trackPublicUpdate: jest.fn(),
trackRootUpdate: jest.fn(),
trackThreadUpdate: jest.fn(),
trackPrivateUpdate: jest.fn(),
trackPinDBAddress: jest.fn(),
trackSpaceUpdateByApp: jest.fn()
}
const mockProfileData = {
public: {
name: { timeStamp: 12000, value: 'very name' },
image: { timeStamp: 13000, value: 'such picture' }
},
private: {
shh: { timeStamp: 14000, value: 'many secret' },
quiet: { timeStamp: 15000, value: 'wow!' }
}
}
const mockThreadEntries = [
{ message: 'a great post' },
{ message: 'another great post' }
]
function addReplicatedEmitter (pinning) {
pinning.events = new EventEmitter()
const origOpenDB = pinning.openDB
function myOpenDB (address, responseFn, onReplicatedFn, rootStoreAddress, analyticsFn) {
const newReplicatedFn = (odbAddress) => {
const numEntries = pinning.openDBs[odbAddress].db._oplog.values.length
pinning.events.emit('replicated', { odbAddress, numEntries })
if (onReplicatedFn) {
onReplicatedFn(odbAddress)
}
}
origOpenDB.call(pinning, address, responseFn, newReplicatedFn, rootStoreAddress, analyticsFn)
}
pinning.openDB = myOpenDB
return pinning
}
describe('Pinning', () => {
let tmpDir
let pinning
let testClient
let clientIpfsOpts
const pinningRoom = 'test-pinning-room'
beforeEach(async () => {
tmpDir = await tmp.dir({ unsafeCleanup: true })
const orbitdbPath = tmpDir.path + '/orbitdb'
const ipfsPath = tmpDir.path + '/ipfs'
const ipfsOpts = {
config: pinningIpfsConfig,
repo: ipfsPath
}
const orbitCacheOpts = null
const pubSubConfig = null
const entriesNumCacheOpts = null
const pinWhitelistDids = null
const pinWhitelistSpaces = null
const pinSilent = null
const ipfs = await IPFS.create(ipfsOpts)
pinning = new Pinning(ipfs, orbitdbPath, analyticsMock, orbitCacheOpts, pubSubConfig, pinningRoom, entriesNumCacheOpts, pinWhitelistDids, pinWhitelistSpaces, pinSilent)
await pinning.start()
await pinning.entriesCache.store.flushall()
const pinningAddresses = await pinning.ipfs.swarm.localAddrs()
clientIpfsOpts = { config: { Bootstrap: pinningAddresses } }
testClient = new TestClient(clientIpfsOpts, pinningRoom)
await testClient.init()
pinning = addReplicatedEmitter(pinning)
})
afterEach(async () => {
await testClient.cleanup()
await pinning.stop()
await tmpDir.cleanup()
})
it('should sync db correctly from client', async () => {
await testClient.createDB(mockProfileData)
const pinningReplicatedPromise = new Promise((resolve) => {
const pinningStoreEntries = {}
const checkIfStoresReplicated = (data) => {
const storeType = data.odbAddress.split('.')[1]
if (!pinningStoreEntries[storeType] || data.numEntries > pinningStoreEntries[storeType]) {
pinningStoreEntries[storeType] = data.numEntries
}
if (Object.keys(pinningStoreEntries).length === 3 &&
pinningStoreEntries.root === 2 &&
pinningStoreEntries.public === Object.keys(mockProfileData.public).length &&
pinningStoreEntries.private === Object.keys(mockProfileData.private).length) {
pinning.events.off('replicated', checkIfStoresReplicated)
resolve()
}
}
pinning.events.on('replicated', checkIfStoresReplicated)
})
const responsesPromise = new Promise((resolve, reject) => {
const hasResponses = {}
testClient.onMsg = (topic, data) => {
if (data.type === 'HAS_ENTRIES') {
const storeType = data.odbAddress.split('.')[1]
if (!hasResponses[storeType] || data.numEntries > hasResponses[storeType]) {
hasResponses[storeType] = data.numEntries
}
}
if (['root', 'public', 'private'].every(storeType => storeType in hasResponses)) {
resolve()
}
}
})
await testClient.announceDB()
await pinningReplicatedPromise
await responsesPromise
})
it('should sync db correctly to client', async () => {
// -- Create databases on the pinning node using the test client
await testClient.createDB(mockProfileData)
const pinningReplicatedPromise = new Promise((resolve) => {
const pinningStoreEntries = {}
const checkIfStoresReplicated = (data) => {
const storeType = data.odbAddress.split('.')[1]
if (!pinningStoreEntries[storeType] || data.numEntries > pinningStoreEntries[storeType]) {
pinningStoreEntries[storeType] = data.numEntries
}
if (Object.keys(pinningStoreEntries).length === 3 &&
pinningStoreEntries.root === 2 &&
pinningStoreEntries.public === Object.keys(mockProfileData.public).length &&
pinningStoreEntries.private === Object.keys(mockProfileData.private).length) {
pinning.events.off('replicated', checkIfStoresReplicated)
resolve()
}
}
pinning.events.on('replicated', checkIfStoresReplicated)
})
await testClient.announceDB()
await pinningReplicatedPromise
// -- Create new client with no data
const client2IpfsOpts = defaultsDeep({
config: {
Addresses: {
Swarm: [
'/ip4/127.0.0.1/tcp/4106',
'/ip4/127.0.0.1/tcp/4107/ws'
]
}
}
}, clientIpfsOpts)
const testClient2 = new TestClient(client2IpfsOpts, pinningRoom)
await testClient2.init()
// -- Sync new client to pinning node
await testClient2.createDB()
await testClient2.announceDB()
await testClient2.storeSynced()
const expectedProfile = Object.keys(mockProfileData.public).reduce((acc, key) => {
acc[key] = mockProfileData.public[key].value
return acc
}, {})
const expectedPrivate = Object.keys(mockProfileData.private).reduce((acc, key) => {
acc[key] = mockProfileData.private[key].value
return acc
}, {})
expect(await testClient2.getProfile()).toEqual(expectedProfile)
expect(await testClient2.getPrivate()).toEqual(expectedPrivate)
testClient2.cleanup()
}, 30000)
it('dbs should close after 30 min, but not before', async () => {
await testClient.createDB(mockProfileData)
const pinningReplicatedPromise = new Promise((resolve) => {
const pinningStoreEntries = {}
const checkIfStoresReplicated = (data) => {
const storeType = data.odbAddress.split('.')[1]
if (!pinningStoreEntries[storeType] || data.numEntries > pinningStoreEntries[storeType]) {
pinningStoreEntries[storeType] = data.numEntries
}
if (Object.keys(pinningStoreEntries).length === 3 &&
pinningStoreEntries.root === 2 &&
pinningStoreEntries.public === Object.keys(mockProfileData.public).length &&
pinningStoreEntries.private === Object.keys(mockProfileData.private).length) {
pinning.events.off('replicated', checkIfStoresReplicated)
resolve()
}
}
pinning.events.on('replicated', checkIfStoresReplicated)
})
await testClient.announceDB()
await pinningReplicatedPromise
await pinning.checkAndCloseDBs()
let numOpenDBs = Object.keys(pinning.openDBs).length
expect(numOpenDBs).toEqual(3)
// make 20 min pass
// hacky way to get around Date.now()
Object.keys(pinning.openDBs).map(key => {
pinning.openDBs[key].latestTouch -= 20 * 60 * 1000
})
await pinning.checkAndCloseDBs()
numOpenDBs = Object.keys(pinning.openDBs).length
expect(numOpenDBs).toEqual(3)
// make additional 10 min pass
Object.keys(pinning.openDBs).map(key => {
pinning.openDBs[key].latestTouch -= 10 * 60 * 1000
})
await pinning.checkAndCloseDBs()
numOpenDBs = Object.keys(pinning.openDBs).length
expect(numOpenDBs).toEqual(0)
})
describe('Threads', () => {
beforeEach(async () => {
await testClient.createDB(mockProfileData)
const pinningReplicatedPromise = new Promise((resolve) => {
const pinningStoreEntries = {}
const checkIfStoresReplicated = (data) => {
const storeType = data.odbAddress.split('.')[1]
if (!pinningStoreEntries[storeType] || data.numEntries > pinningStoreEntries[storeType]) {
pinningStoreEntries[storeType] = data.numEntries
}
if (Object.keys(pinningStoreEntries).length === 3 &&
pinningStoreEntries.root === 2 &&
pinningStoreEntries.public === Object.keys(mockProfileData.public).length &&
pinningStoreEntries.private === Object.keys(mockProfileData.private).length) {
pinning.events.off('replicated', checkIfStoresReplicated)
resolve()
}
}
pinning.events.on('replicated', checkIfStoresReplicated)
})
await testClient.announceDB()
await pinningReplicatedPromise
})
// TODO: reproduce root failure of following tests (see https://github.com/3box/3box-pinning-node/issues/288)
it.skip('Test to reproduce error in retrieving the thread access node consecutive times', async () => {
await testClient.createThread(mockThreadEntries)
const CID = require('cids')
const cid = new CID('zdpuAqS4Qc9Ff3uuUyT6juCpsC7waWw6NDVqtdPYYL9EZRnYx')
console.log('STARTING')
for (let i = 0; i < 10; i++) {
console.log('fetching...', i)
console.log('MANIFEST', await pinning.ipfs.dag.get(cid))
// without this delay, consecutive calls fail
// await new Promise(resolve => setTimeout(resolve, 100))
}
})
// TODO: fix (see https://github.com/3box/3box-pinning-node/issues/288)
it.skip('should pin thread data correctly from client', async () => {
await testClient.createThread(mockThreadEntries)
const pinningThreadCreatedPromise = new Promise((resolve) => {
const pinningStoreEntries = {}
const checkIfThreadCreated = (data) => {
console.log('replicated', data)
const storeType = data.odbAddress.split('.')[1]
if (!pinningStoreEntries[storeType] || data.numEntries > pinningStoreEntries[storeType]) {
pinningStoreEntries[storeType] = data.numEntries
}
if (pinningStoreEntries.thread === 2) {
pinning.events.off('replicated', checkIfThreadCreated)
resolve()
}
}
pinning.events.on('replicated', checkIfThreadCreated)
})
await testClient.announceThread()
await pinningThreadCreatedPromise
})
// TODO: fix (see https://github.com/3box/3box-pinning-node/issues/288)
it.skip('should sync pinned thread to client', async () => {
// -- Create thread on the pinning node using the test client
await testClient.createThread(mockThreadEntries)
const pinningThreadCreatedPromise = new Promise((resolve) => {
const pinningStoreEntries = {}
const checkIfThreadCreated = (data) => {
console.log('replicated', data)
const storeType = data.odbAddress.split('.')[1]
if (!pinningStoreEntries[storeType] || data.numEntries > pinningStoreEntries[storeType]) {
pinningStoreEntries[storeType] = data.numEntries
}
if (pinningStoreEntries.thread === 2) {
pinning.events.off('replicated', checkIfThreadCreated)
resolve()
}
}
pinning.events.on('replicated', checkIfThreadCreated)
})
await testClient.announceThread()
await pinningThreadCreatedPromise
// -- Create new client with no data
const client2IpfsOpts = defaultsDeep({
config: {
Addresses: {
Swarm: [
'/ip4/127.0.0.1/tcp/4106',
'/ip4/127.0.0.1/tcp/4107/ws'
]
}
}
}, clientIpfsOpts)
const testClient2 = new TestClient(client2IpfsOpts, pinningRoom)
await testClient2.init()
await testClient2.createDB()
await testClient2.announceDB()
await testClient2.createThread()
let posts = await testClient2.getThreadPosts()
expect(posts).toHaveLength(0)
// -- Sync new client to pinning node
await testClient2.createThread()
await testClient2.announceThread()
await testClient2.storeSynced({ thread: true })
posts = await testClient2.getThreadPosts()
expect(posts[0].message).toEqual(mockThreadEntries[0].message)
expect(posts[1].message).toEqual(mockThreadEntries[1].message)
testClient2.cleanup()
})
})
})