-
Notifications
You must be signed in to change notification settings - Fork 2
/
test-zzzync.ts
203 lines (179 loc) · 6.56 KB
/
test-zzzync.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
/* eslint-disable no-console */
import { start, stop } from '@libp2p/interfaces/startable'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { assert } from 'aegir/chai'
import { NamespaceDatastore } from 'datastore-core'
import { Key } from 'interface-datastore'
import { type Libp2pOptions, createLibp2p } from 'libp2p'
import { CID } from 'multiformats'
import { Web3Storage } from 'web3.storage'
import { isBrowser } from 'wherearewe'
import { type TestPaths, getTestPaths, tempPath } from './utils/constants.js'
import { getTestIdentities, getTestIdentity } from './utils/identities.js'
import { getTestIpfs, localIpfsOptions } from './utils/ipfs.js'
import getDatastore from './utils/level-datastore.js'
import { createLibp2pOptions } from './utils/libp2p-options.js'
import { getTestManifest } from './utils/manifest.js'
import type { GossipHelia, GossipLibp2p } from '@/interface'
import type { Ed25519PeerId, PeerId } from '@libp2p/interface/peer-id'
import type { CreateEphemeralKadDHT } from '@tabcat/zzzync/dist/src/advertisers/dht.js'
import type { LevelDatastore } from 'datastore-level'
import { StaticAccess as Access } from '@/access/static/index.js'
import staticAccessProtocol from '@/access/static/protocol.js'
import { basalEntry } from '@/entry/basal/index.js'
import { basalIdentity } from '@/identity/basal/index.js'
import { Replica } from '@/replica/index.js'
import { zzzyncReplicator, type ZzzyncReplicator } from '@/replicator/zzzync/index.js'
const testName = 'zzzync-replicator'
const token = process.env.W3_TOKEN
const noToken = typeof token === 'string' && token.length === 0
let _describe: Mocha.SuiteFunction | Mocha.PendingSuiteFunction
if (noToken) {
// eslint-disable-next-line no-console
console.log('no web3.storage token found at .w3_token. skipping zzzync replicator tests')
_describe = describe.skip
} else {
_describe = describe.skip
}
if (isBrowser) {
_describe = describe.skip
}
_describe(testName, () => {
let
ipfs1: GossipHelia,
ipfs2: GossipHelia,
libp2p1: GossipLibp2p,
libp2p2: GossipLibp2p,
replica1: Replica,
replica2: Replica,
replicator1: ZzzyncReplicator,
replicator2: ZzzyncReplicator,
testPaths1: TestPaths,
testPaths2: TestPaths,
access: Access,
datastore: LevelDatastore,
datastore1: NamespaceDatastore,
datastore2: NamespaceDatastore
before(async () => {
testPaths1 = getTestPaths(tempPath, testName + '/1')
testPaths2 = getTestPaths(tempPath, testName + '/2')
datastore = await getDatastore(testPaths1.replica)
await datastore.open()
datastore1 = new NamespaceDatastore(datastore, new Key(testPaths1.replica))
datastore2 = new NamespaceDatastore(datastore, new Key(testPaths2.replica))
const peerId1 = await createEd25519PeerId()
const peerId2 = await createEd25519PeerId()
// blocks peering so block fetching happens over web3.storage
const getLibp2pOptions = (peerId: Ed25519PeerId, neighbor: Ed25519PeerId): Libp2pOptions => ({
peerId,
connectionGater: {
denyDialPeer: async () => false,
denyDialMultiaddr: async () => false,
denyInboundConnection: async () => false,
denyOutboundConnection: async () => false,
denyInboundEncryptedConnection: async () => false,
denyOutboundEncryptedConnection: async () => false,
denyInboundUpgradedConnection: async () => false,
denyOutboundUpgradedConnection: async () => false,
filterMultiaddrForPeer: async (peerId: PeerId) => !peerId.equals(neighbor)
}
})
ipfs1 = await getTestIpfs(testPaths1, localIpfsOptions, getLibp2pOptions(peerId1, peerId2))
ipfs2 = await getTestIpfs(testPaths2, localIpfsOptions, getLibp2pOptions(peerId2, peerId1))
libp2p1 = ipfs1.libp2p
libp2p2 = ipfs2.libp2p
const identities1 = await getTestIdentities(testPaths1)
const identities2 = await getTestIdentities(testPaths2)
const identity1 = await getTestIdentity(
identities1,
libp2p1.services.keychain,
testName
)
const identity2 = await getTestIdentity(
identities2,
libp2p2.services.keychain,
testName
)
const write = [identity1.id, identity2.id]
const accessConfig = {
access: { protocol: staticAccessProtocol, config: { write } }
}
const manifest = await getTestManifest(testName, accessConfig)
access = new Access({ manifest })
await start(access)
replica1 = new Replica({
manifest,
datastore: datastore1,
blockstore: ipfs1.blockstore,
access,
identity: identity1,
components: {
entry: basalEntry(),
identity: basalIdentity()
}
})
replica2 = new Replica({
manifest,
datastore: datastore2,
blockstore: ipfs2.blockstore,
access,
identity: identity2,
components: {
entry: basalEntry(),
identity: basalIdentity()
}
})
await start(replica1, replica2)
if (token == null) {
throw new Error('w3 token is undefined')
}
const client = new Web3Storage({ token })
const createEphemeralLibp2p: CreateEphemeralKadDHT = async (peerId: PeerId) => {
const libp2p = await createLibp2p(await createLibp2pOptions({ peerId }))
return libp2p.services
}
const replicator = zzzyncReplicator({ w3: { client }, createEphemeralLibp2p, scope: 'lan' })
replicator1 = replicator.create({
ipfs: ipfs1,
replica: replica1,
datastore: datastore1,
blockstore: ipfs1.blockstore
})
replicator2 = replicator.create({
ipfs: ipfs2,
replica: replica2,
datastore: datastore2,
blockstore: ipfs2.blockstore
})
})
after(async () => {
await stop(access)
await stop(replicator1, replicator2)
await stop(replica1, replica2)
await stop(ipfs1)
await stop(ipfs2)
await datastore.close()
})
describe('instance', () => {
before(async () => {
await start(replicator1, replicator2)
})
it('exposes instance properties', () => {
const replicator = replicator1
assert.isOk(replicator.download)
assert.isOk(replicator.upload)
})
it('uploads and advertises replica data', async () => {
await replica1.write(new Uint8Array())
await replicator1.upload()
})
it('downloads and merges replica data', async () => {
await new Promise(resolve => setTimeout(resolve, 2000))
await replicator2.download()
if (!(replica1.root instanceof CID) || !(replica2.root instanceof CID)) {
throw new Error()
}
assert.equal(replica1.root.toString(), replica2.root.toString())
})
})
})