mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
188 lines
4.8 KiB
JavaScript
188 lines
4.8 KiB
JavaScript
import { deepStrictEqual } from 'assert'
|
|
import rmrf from 'rimraf'
|
|
import { copy } from 'fs-extra'
|
|
import * as IPFS from 'ipfs'
|
|
import { Log, Entry, Database, KeyStore, Identities } from '../../../src/index.js'
|
|
import { EventStore } from '../../../src/db/index.js'
|
|
import config from '../../config.js'
|
|
import testKeysPath from '../../fixtures/test-keys-path.js '
|
|
import connectPeers from '../../utils/connect-nodes.js'
|
|
import waitFor from '../../utils/wait-for.js'
|
|
|
|
const OpLog = { Log, Entry }
|
|
const keysPath = './testkeys'
|
|
|
|
describe('Events Database Replication', function () {
|
|
this.timeout(30000)
|
|
|
|
let ipfs1, ipfs2
|
|
let keystore
|
|
let identities
|
|
let testIdentity1, testIdentity2
|
|
let db1, db2
|
|
|
|
const databaseId = 'events-AAA'
|
|
|
|
const accessController = {
|
|
canAppend: async (entry) => {
|
|
const identity = await identities.getIdentity(entry.identity)
|
|
return identity.id === testIdentity1.id
|
|
}
|
|
}
|
|
|
|
const expected = [
|
|
'init',
|
|
true,
|
|
'hello',
|
|
'friend',
|
|
12345,
|
|
'empty',
|
|
'',
|
|
'friend33'
|
|
]
|
|
|
|
before(async () => {
|
|
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
|
|
ipfs2 = await IPFS.create({ ...config.daemon2, repo: './ipfs2' })
|
|
await connectPeers(ipfs1, ipfs2)
|
|
|
|
await copy(testKeysPath, keysPath)
|
|
keystore = await KeyStore({ path: keysPath })
|
|
identities = await Identities({ keystore })
|
|
testIdentity1 = await identities.createIdentity({ id: 'userA' })
|
|
testIdentity2 = await identities.createIdentity({ id: 'userB' })
|
|
})
|
|
|
|
after(async () => {
|
|
if (ipfs1) {
|
|
await ipfs1.stop()
|
|
}
|
|
|
|
if (ipfs2) {
|
|
await ipfs2.stop()
|
|
}
|
|
|
|
if (keystore) {
|
|
await keystore.close()
|
|
}
|
|
|
|
await rmrf(keysPath)
|
|
await rmrf('./orbitdb1')
|
|
await rmrf('./orbitdb2')
|
|
await rmrf('./ipfs1')
|
|
await rmrf('./ipfs2')
|
|
})
|
|
|
|
afterEach(async () => {
|
|
if (db1) {
|
|
await db1.drop()
|
|
await db1.close()
|
|
}
|
|
if (db2) {
|
|
await db2.drop()
|
|
await db2.close()
|
|
}
|
|
})
|
|
|
|
it('replicates a database', async () => {
|
|
let connected = false
|
|
let updateCount = 0
|
|
|
|
const onConnected = async (peerId) => {
|
|
connected = true
|
|
}
|
|
|
|
const onUpdate = async (peerId) => {
|
|
++updateCount
|
|
}
|
|
|
|
const onError = (err) => {
|
|
console.error(err)
|
|
}
|
|
|
|
db1 = await EventStore({ OpLog, Database, ipfs: ipfs1, identity: testIdentity1, address: databaseId, accessController, directory: './orbitdb1' })
|
|
db2 = await EventStore({ OpLog, Database, ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2' })
|
|
|
|
db2.events.on('join', onConnected)
|
|
db1.events.on('join', onConnected)
|
|
db2.events.on('update', onUpdate)
|
|
db2.events.on('error', onError)
|
|
db1.events.on('error', onError)
|
|
|
|
await db1.add(expected[0])
|
|
await db1.add(expected[1])
|
|
await db1.add(expected[2])
|
|
await db1.add(expected[3])
|
|
await db1.add(expected[4])
|
|
await db1.add(expected[5])
|
|
await db1.add(expected[6])
|
|
await db1.add(expected[7])
|
|
|
|
await waitFor(() => connected, () => true)
|
|
await waitFor(() => updateCount > 0, () => true)
|
|
|
|
const all2 = []
|
|
for await (const event of db2.iterator()) {
|
|
all2.unshift(event)
|
|
}
|
|
deepStrictEqual(all2, expected)
|
|
|
|
const all1 = await db2.all()
|
|
deepStrictEqual(all1, expected)
|
|
})
|
|
|
|
it('loads the database after replication', async () => {
|
|
db1 = await EventStore({ OpLog, Database, ipfs: ipfs1, identity: testIdentity1, address: databaseId, accessController, directory: './orbitdb1' })
|
|
db2 = await EventStore({ OpLog, Database, ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2' })
|
|
|
|
let connected = false
|
|
let updateCount = 0
|
|
|
|
const onConnected = async (peerId) => {
|
|
connected = true
|
|
}
|
|
|
|
const onUpdate = async (peerId) => {
|
|
++updateCount
|
|
}
|
|
|
|
const onError = (err) => {
|
|
console.error(err)
|
|
}
|
|
|
|
db2.events.on('join', onConnected)
|
|
db2.events.on('update', onUpdate)
|
|
db2.events.on('error', onError)
|
|
db1.events.on('error', onError)
|
|
|
|
await db1.add(expected[0])
|
|
await db1.add(expected[1])
|
|
await db1.add(expected[2])
|
|
await db1.add(expected[3])
|
|
await db1.add(expected[4])
|
|
await db1.add(expected[5])
|
|
await db1.add(expected[6])
|
|
await db1.add(expected[7])
|
|
|
|
await waitFor(() => connected, () => true)
|
|
await waitFor(() => updateCount > 0, () => true)
|
|
|
|
await db1.drop()
|
|
await db1.close()
|
|
db1 = null
|
|
|
|
await db2.close()
|
|
|
|
db2 = await EventStore({ OpLog, Database, ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2' })
|
|
|
|
const all2 = []
|
|
for await (const event of db2.iterator()) {
|
|
all2.unshift(event)
|
|
}
|
|
deepStrictEqual(all2, expected)
|
|
|
|
const all1 = await db2.all()
|
|
deepStrictEqual(all1, expected)
|
|
})
|
|
})
|