This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
replicate.spec.js
180 lines (152 loc) · 5.92 KB
/
replicate.spec.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
'use strict'
const assert = require('assert')
const rmrf = require('rimraf')
const fs = require('fs-extra')
const Log = require('../src/log')
const IdentityProvider = require('orbit-db-identity-provider')
const Keystore = require('orbit-db-keystore')
// Test utils
const {
config,
testAPIs,
startIpfs,
stopIpfs,
getIpfsPeerId,
waitForPeers,
MemStore,
connectPeers
} = require('orbit-db-test-utils')
Object.keys(testAPIs).forEach((IPFS) => {
describe('ipfs-log - Replication (' + IPFS + ')', function () {
this.timeout(config.timeout)
let ipfs1, ipfs2, id1, id2, testIdentity, testIdentity2
const { identityKeyFixtures, signingKeyFixtures, identityKeysPath, signingKeysPath } = config
const ipfsConfig1 = Object.assign({}, config.daemon1, {
repo: config.daemon1.repo + new Date().getTime()
})
const ipfsConfig2 = Object.assign({}, config.daemon2, {
repo: config.daemon2.repo + new Date().getTime()
})
let keystore, signingKeystore
before(async () => {
rmrf.sync(ipfsConfig1.repo)
rmrf.sync(ipfsConfig2.repo)
rmrf.sync(identityKeysPath)
rmrf.sync(signingKeysPath)
await fs.copy(identityKeyFixtures, identityKeysPath)
await fs.copy(signingKeyFixtures, signingKeysPath)
// Start two IPFS instances
ipfs1 = await startIpfs(IPFS, ipfsConfig1)
ipfs2 = await startIpfs(IPFS, ipfsConfig2)
await connectPeers(ipfs1, ipfs2)
// Get the peer IDs
id1 = await getIpfsPeerId(ipfs1)
id2 = await getIpfsPeerId(ipfs2)
// Use mem-store for faster testing (no disk IO)
const memstore = new MemStore()
ipfs1.dag.put = memstore.put.bind(memstore)
ipfs1.dag.get = memstore.get.bind(memstore)
ipfs2.dag.put = memstore.put.bind(memstore)
ipfs2.dag.get = memstore.get.bind(memstore)
keystore = new Keystore(identityKeysPath)
signingKeystore = new Keystore(signingKeysPath)
// Create an identity for each peers
testIdentity = await IdentityProvider.createIdentity({ id: 'userB', keystore, signingKeystore })
testIdentity2 = await IdentityProvider.createIdentity({ id: 'userA', keystore, signingKeystore })
})
after(async () => {
await stopIpfs(ipfs1)
await stopIpfs(ipfs2)
rmrf.sync(ipfsConfig1.repo)
rmrf.sync(ipfsConfig2.repo)
rmrf.sync(identityKeysPath)
rmrf.sync(signingKeysPath)
await keystore.close()
await signingKeystore.close()
})
describe('replicates logs deterministically', function () {
const amount = 128 + 1
const channel = 'XXX'
const logId = 'A'
let log1, log2, input1, input2
let buffer1 = []
let buffer2 = []
let processing = 0
const handleMessage = async (message) => {
if (id1 === message.from) {
return
}
buffer1.push(message.data.toString())
processing++
process.stdout.write('\r')
process.stdout.write(`> Buffer1: ${buffer1.length} - Buffer2: ${buffer2.length}`)
const log = await Log.fromMultihash(ipfs1, testIdentity, message.data.toString())
await log1.join(log)
processing--
}
const handleMessage2 = async (message) => {
if (id2 === message.from) {
return
}
buffer2.push(message.data.toString())
processing++
process.stdout.write('\r')
process.stdout.write(`> Buffer1: ${buffer1.length} - Buffer2: ${buffer2.length}`)
const log = await Log.fromMultihash(ipfs2, testIdentity2, message.data.toString())
await log2.join(log)
processing--
}
beforeEach(async () => {
log1 = new Log(ipfs1, testIdentity, { logId })
log2 = new Log(ipfs2, testIdentity2, { logId })
input1 = new Log(ipfs1, testIdentity, { logId })
input2 = new Log(ipfs2, testIdentity2, { logId })
await ipfs1.pubsub.subscribe(channel, handleMessage)
await ipfs2.pubsub.subscribe(channel, handleMessage2)
})
it('replicates logs', async () => {
await waitForPeers(ipfs1, [id2], channel)
for (let i = 1; i <= amount; i++) {
await input1.append('A' + i)
await input2.append('B' + i)
const hash1 = await input1.toMultihash()
const hash2 = await input2.toMultihash()
await ipfs1.pubsub.publish(channel, Buffer.from(hash1))
await ipfs2.pubsub.publish(channel, Buffer.from(hash2))
}
console.log('\nAll messages sent')
const whileProcessingMessages = (timeoutMs) => {
return new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('timeout')), timeoutMs)
const timer = setInterval(() => {
if (buffer1.length + buffer2.length === amount * 2 &&
processing === 0) {
console.log('\nAll messages received')
clearInterval(timer)
resolve()
}
}, 200)
})
}
console.log('Waiting for all to process')
await whileProcessingMessages(config.timeout)
let result = new Log(ipfs1, testIdentity, { logId })
await result.join(log1)
await result.join(log2)
assert.strictEqual(buffer1.length, amount)
assert.strictEqual(buffer2.length, amount)
assert.strictEqual(result.length, amount * 2)
assert.strictEqual(log1.length, amount)
assert.strictEqual(log2.length, amount)
assert.strictEqual(result.values[0].payload, 'A1')
assert.strictEqual(result.values[1].payload, 'B1')
assert.strictEqual(result.values[2].payload, 'A2')
assert.strictEqual(result.values[3].payload, 'B2')
assert.strictEqual(result.values[99].payload, 'B50')
assert.strictEqual(result.values[100].payload, 'A51')
assert.strictEqual(result.values[198].payload, 'A100')
assert.strictEqual(result.values[199].payload, 'B100')
})
})
})
})