-
Notifications
You must be signed in to change notification settings - Fork 2
/
test-replica.ts
323 lines (280 loc) · 11.8 KB
/
test-replica.ts
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
/* eslint-disable max-nested-callbacks */
import { ShardBlock, type ShardBlockView } from '@alanshaw/pail/shard'
import { start, stop } from '@libp2p/interfaces/startable'
import { assert, expect } from 'aegir/chai'
import { NamespaceDatastore } from 'datastore-core'
import { Key } from 'interface-datastore'
import { type TestPaths, getTestPaths, names, tempPath } from './utils/constants.js'
import defaultManifest from './utils/default-manifest.js'
import { singleEntry } from './utils/entries.js'
import { getTestIdentities, getTestIdentity } from './utils/identities.js'
import { getTestIpfs, offlineIpfsOptions } from './utils/ipfs.js'
import getDatastore from './utils/level-datastore.js'
import { getTestLibp2p } from './utils/libp2p.js'
import type { GossipHelia } from '@/interface.js'
import type { LevelDatastore } from 'datastore-level'
import type { Blockstore } from 'interface-blockstore'
import type { CID } from 'multiformats/cid'
import { StaticAccess } from '@/access/static/index.js'
import { basalEntry } from '@/entry/basal/index.js'
import { type Identity, basalIdentity } from '@/identity/basal/index.js'
import { Manifest } from '@/manifest/index.js'
import { Replica } from '@/replica/index.js'
import { encodeCbor } from '@/utils/block.js'
import { cidstring, decodedcid } from '@/utils/index.js'
const testName = 'replica'
describe(testName, () => {
let ipfs: GossipHelia,
tempIpfs: GossipHelia,
blockstore: Blockstore,
replica: Replica,
manifest: Manifest,
access: StaticAccess,
identity: Identity,
tempIdentity: Identity,
testPaths: TestPaths,
emptyShard: ShardBlockView,
datastore: LevelDatastore
const entryModule = basalEntry()
const identityModule = basalIdentity()
before(async () => {
testPaths = getTestPaths(tempPath, testName)
datastore = await getDatastore(testPaths.replica)
await datastore.open()
ipfs = await getTestIpfs(testPaths, offlineIpfsOptions)
blockstore = ipfs.blockstore
const identities = await getTestIdentities(testPaths)
const libp2p = await getTestLibp2p(ipfs)
const keychain = libp2p.services.keychain
identity = await getTestIdentity(identities, keychain, names.name0)
await blockstore.put(identity.block.cid, identity.block.bytes)
manifest = await Manifest.create({
...defaultManifest('name', identity),
tag: new Uint8Array()
})
access = new StaticAccess({ manifest })
await start(access)
const testPaths1 = getTestPaths(tempPath, testName + '1')
tempIpfs = await getTestIpfs(testPaths1, offlineIpfsOptions)
const tempLibp2p = await getTestLibp2p(tempIpfs)
const tempIdentities = await getTestIdentities(testPaths1)
tempIdentity = await getTestIdentity(
tempIdentities,
tempLibp2p.services.keychain,
names.name1
)
emptyShard = await ShardBlock.create()
})
after(async () => {
await datastore.close()
await stop(ipfs)
await stop(tempIpfs)
})
describe('class', () => {
describe('open', () => {
it('returns a new instance of a replica', async () => {
replica = new Replica({
datastore: new NamespaceDatastore(datastore, new Key(`${testPaths.replica}/temp`)),
blockstore,
manifest,
access,
identity,
components: {
entry: entryModule,
identity: identityModule
}
})
await start(replica)
})
})
})
describe('instance', () => {
const cids: CID[] = []
const payload = {}
it('exposes instance properties', () => {
assert.isOk(replica.manifest)
assert.isOk(replica.access)
assert.isOk(replica.identity)
assert.isOk(replica.components.entry)
assert.isOk(replica.components.identity)
assert.isOk(replica.events)
assert.isOk(replica.heads)
assert.isOk(replica.tails)
assert.isOk(replica.missing)
assert.isOk(replica.denied)
assert.isOk(replica.traverse)
assert.isOk(replica.has)
assert.isOk(replica.known)
assert.isOk(replica.add)
assert.isOk(replica.write)
})
describe('add', () => {
beforeEach(async () => {
await start(replica)
})
afterEach(async () => {
await stop(replica)
})
it('does not add entry with mismatched tag', async () => {
const tag = new Uint8Array([7])
const entry = await entryModule.create({
identity,
tag,
payload,
next: [],
refs: []
})
const cid = entry.cid
await replica.add([entry])
// assert.strictEqual(await replica.size(), 0)
assert.strictEqual(await replica.has(cid), false)
assert.strictEqual(await replica.known(cid), false)
assert.strictEqual(await replica.heads.has(new Key(cidstring(cid))), false)
assert.strictEqual(replica.graph.root.heads.equals(emptyShard.cid), true)
// assert.strictEqual(await replica.heads.size(), 0)
assert.strictEqual(await replica.tails.has(new Key(cidstring(cid))), false)
assert.strictEqual(replica.graph.root.tails.equals(emptyShard.cid), true)
assert.strictEqual(replica.graph.root.missing.equals(emptyShard.cid), true)
assert.strictEqual(replica.graph.root.denied.equals(emptyShard.cid), true)
// assert.strictEqual(await replica.tails.size(), 0)
// assert.strictEqual(await replica.missing.size(), 0)
// assert.strictEqual(await replica.denied.size(), 0)
})
it('does not add entry without access', async () => {
const entry = await singleEntry(tempIdentity)()
const cid = entry.cid
await replica.add([entry])
// assert.strictEqual(await replica.size(), 0)
assert.strictEqual(await replica.has(cid), false)
assert.strictEqual(await replica.known(cid), false)
assert.strictEqual(await replica.heads.has(new Key(cidstring(cid))), false)
assert.strictEqual(replica.graph.root.heads.equals(emptyShard.cid), true)
// assert.strictEqual(await replica.heads.size(), 0)
assert.strictEqual(await replica.tails.has(new Key(cidstring(cid))), false)
assert.strictEqual(replica.graph.root.tails.equals(emptyShard.cid), true)
assert.strictEqual(replica.graph.root.missing.equals(emptyShard.cid), true)
assert.strictEqual(replica.graph.root.denied.equals(emptyShard.cid), true)
// assert.strictEqual(await replica.tails.size(), 0)
// assert.strictEqual(await replica.missing.size(), 0)
// assert.strictEqual(await replica.denied.size(), 0)
})
it('adds an entry to the replica', async () => {
const entry = await singleEntry(identity)()
const cid = entry.cid
await replica.add([entry])
// assert.strictEqual(await replica.size(), 1)
assert.strictEqual(await replica.has(cid), true)
assert.strictEqual(await replica.known(cid), true)
assert.strictEqual(await replica.heads.has(new Key(cidstring(cid))), true)
// assert.strictEqual(await replica.heads.size(), 1)
assert.strictEqual(await replica.tails.has(new Key(cidstring(cid))), true)
assert.strictEqual(replica.graph.root.missing.equals(emptyShard.cid), true)
assert.strictEqual(replica.graph.root.denied.equals(emptyShard.cid), true)
// assert.strictEqual(await replica.tails.size(), 1)
// assert.strictEqual(await replica.missing.size(), 0)
// assert.strictEqual(await replica.denied.size(), 0)
})
})
describe('write', () => {
before(async () => {
replica = new Replica({
datastore: new NamespaceDatastore(datastore, new Key(testPaths.replica)),
blockstore,
manifest,
access,
identity,
components: {
entry: entryModule,
identity: identityModule
}
})
await start(replica)
})
it('writes an entry to the replica', async () => {
const entry = await replica.write(payload)
const cid = entry.cid
cids.push(cid)
// assert.strictEqual(await replica.size(), 1)
assert.strictEqual(await replica.has(cid), true)
assert.strictEqual(await replica.known(cid), true)
assert.strictEqual(await replica.heads.has(new Key(cidstring(cid))), true)
// assert.strictEqual(await replica.heads.size(), 1)
assert.strictEqual(await replica.tails.has(new Key(cidstring(cid))), true)
// assert.strictEqual(await replica.tails.size(), 1)
assert.strictEqual(replica.graph.root.missing.equals(emptyShard.cid), true)
assert.strictEqual(replica.graph.root.denied.equals(emptyShard.cid), true)
// assert.strictEqual(await replica.missing.size(), 0)
// assert.strictEqual(await replica.denied.size(), 0)
})
})
describe('traverse', () => {
it('descends the replica entry set', async () => {
const entries = await replica.traverse()
assert.deepEqual(
entries.map((entry) => entry.cid),
cids.slice().reverse()
)
})
it('ascends the replica entry set', async () => {
const direction = 'ascend'
const entries = await replica.traverse({ direction })
assert.deepEqual(
entries.map((entry) => entry.cid),
cids
)
})
it('rejects when invalid direction is given', async () => {
const direction = 'not a real direction'
// @ts-expect-error Ignore error to test an invalid direction.
const promise = replica.traverse({ direction })
await expect(promise).to.eventually.be.rejected()
})
})
describe('data persistence', () => {
it('writes the graph root to disk on update', async () => {
const rootHashKey = new Key('rootHash')
const block = await encodeCbor(replica.graph.root)
await datastore.close()
await stop(replica)
const newDatastore = await getDatastore(testPaths.replica)
await newDatastore.open()
const storage = new NamespaceDatastore(newDatastore, new Key(testPaths.replica))
assert.strictEqual(await storage.has(rootHashKey), true)
assert.deepEqual(decodedcid(await storage.get(rootHashKey)), block.cid)
await newDatastore.close()
})
it('loads the graph root from disk on start', async () => {
const entry = await singleEntry(identity)()
const cid = entry.cid
const newDatastore = await getDatastore(testPaths.replica)
await newDatastore.open()
const replica = new Replica({
datastore: new NamespaceDatastore(newDatastore, new Key(testPaths.replica)),
blockstore,
manifest,
access,
identity,
components: {
entry: entryModule,
identity: identityModule
}
})
await start(replica)
// assert.strictEqual(await replica.size(), 1)
assert.strictEqual(await replica.has(cid), true)
assert.strictEqual(await replica.known(cid), true)
assert.strictEqual(await replica.heads.has(new Key(cidstring(cid))), true)
// assert.strictEqual(await replica.heads.size(), 1)
assert.strictEqual(await replica.tails.has(new Key(cidstring(cid))), true)
// assert.strictEqual(await replica.tails.size(), 1)
assert.strictEqual(replica.graph.root.missing.equals(emptyShard.cid), true)
assert.strictEqual(replica.graph.root.denied.equals(emptyShard.cid), true)
// assert.strictEqual(await replica.missing.size(), 0)
// assert.strictEqual(await replica.denied.size(), 0)
assert.strictEqual(replica.graph.root.missing.equals(emptyShard.cid), true)
assert.strictEqual(replica.graph.root.denied.equals(emptyShard.cid), true)
await stop(replica)
})
})
})
})