Merge pull request #1137 from orbitdb/fix/helia-5

Fix tests and clean up
This commit is contained in:
Hayden Young 2023-12-16 00:21:00 +08:00 committed by GitHub
commit e384e9275a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 29 additions and 54 deletions

View File

@ -8,7 +8,7 @@ import { gossipsub } from '@chainsafe/libp2p-gossipsub'
import { circuitRelayTransport } from 'libp2p/circuit-relay'
/**
* A basic Libp2p configuration for node servers.
* A basic Libp2p configuration for Node.js nodes.
*/
export const DefaultLibp2pOptions = {
addresses: {
@ -24,13 +24,9 @@ export const DefaultLibp2pOptions = {
})
],
connectionEncryption: [noise()],
streamMuxers: [
yamux()
],
streamMuxers: [yamux()],
connectionGater: {
denyDialMultiaddr: () => {
return false
}
denyDialMultiaddr: () => false
},
services: {
identify: identifyService(),
@ -55,13 +51,9 @@ export const DefaultLibp2pBrowserOptions = {
})
],
connectionEncryption: [noise()],
streamMuxers: [
yamux()
],
streamMuxers: [yamux()],
connectionGater: {
denyDialMultiaddr: () => {
return false
}
denyDialMultiaddr: () => false
},
services: {
identify: identifyService(),

View File

@ -115,6 +115,9 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => {
if (!ipfs) throw new Error('An instance of ipfs is required.')
if (!log) throw new Error('An instance of log is required.')
const libp2p = ipfs.libp2p
const pubsub = ipfs.libp2p.services.pubsub
const address = log.id
const headsSyncAddress = pathJoin('/orbitdb/heads/', address)
@ -194,7 +197,7 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => {
const { signal } = timeoutController
try {
peers.add(peerId)
const stream = await ipfs.libp2p.dialProtocol(remotePeer, headsSyncAddress, { signal })
const stream = await libp2p.dialProtocol(remotePeer, headsSyncAddress, { signal })
await pipe(sendHeads, stream, receiveHeads(peerId))
} catch (e) {
console.error(e)
@ -218,18 +221,19 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => {
}
const handleUpdateMessage = async message => {
const { topic, data } = message.detail
const task = async () => {
const messageHasData = message => message.detail.data !== undefined
try {
if (messageHasData(message) && onSynced) {
await onSynced(message.detail.data)
if (data && onSynced) {
await onSynced(data)
}
} catch (e) {
events.emit('error', e)
}
}
if (message.detail.topic === address) {
if (topic === address) {
queue.add(task)
}
}
@ -243,7 +247,7 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => {
*/
const add = async (entry) => {
if (started) {
await ipfs.libp2p.services.pubsub.publish(address, entry.bytes)
await pubsub.publish(address, entry.bytes)
}
}
@ -257,10 +261,10 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => {
if (started) {
started = false
await queue.onIdle()
ipfs.libp2p.services.pubsub.removeEventListener('subscription-change', handlePeerSubscribed)
ipfs.libp2p.services.pubsub.removeEventListener('message', handleUpdateMessage)
await ipfs.libp2p.unhandle(headsSyncAddress)
await ipfs.libp2p.services.pubsub.unsubscribe(address)
pubsub.removeEventListener('subscription-change', handlePeerSubscribed)
pubsub.removeEventListener('message', handleUpdateMessage)
await libp2p.unhandle(headsSyncAddress)
await pubsub.unsubscribe(address)
peers.clear()
}
}
@ -274,11 +278,11 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => {
const startSync = async () => {
if (!started) {
// Exchange head entries with peers when connected
await ipfs.libp2p.handle(headsSyncAddress, handleReceiveHeads)
ipfs.libp2p.services.pubsub.addEventListener('subscription-change', handlePeerSubscribed)
ipfs.libp2p.services.pubsub.addEventListener('message', handleUpdateMessage)
await libp2p.handle(headsSyncAddress, handleReceiveHeads)
pubsub.addEventListener('subscription-change', handlePeerSubscribed)
pubsub.addEventListener('message', handleUpdateMessage)
// Subscribe to the pubsub channel for this database through which updates are sent
await ipfs.libp2p.services.pubsub.subscribe(address)
await pubsub.subscribe(address)
started = true
}
}

View File

@ -30,7 +30,6 @@ describe('Database', function () {
}
before(async () => {
// ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs = await createHelia()
await copy(testKeysPath, keysPath)
keystore = await KeyStore({ path: keysPath })

View File

@ -19,7 +19,6 @@ describe('Documents Database', function () {
const databaseId = 'documents-AAA'
before(async () => {
// ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs = await createHelia()
await copy(testKeysPath, keysPath)

View File

@ -19,7 +19,6 @@ describe('Events Database', function () {
const databaseId = 'events-AAA'
before(async () => {
// ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs = await createHelia()
await copy(testKeysPath, keysPath)

View File

@ -21,7 +21,6 @@ describe('KeyValueIndexed Database', function () {
const databaseId = 'keyvalue-AAA'
before(async () => {
// ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs = await createHelia()
await copy(testKeysPath, keysPath)

View File

@ -19,7 +19,6 @@ describe('KeyValue Database', function () {
const databaseId = 'keyvalue-AAA'
before(async () => {
// ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs = await createHelia()
await copy(testKeysPath, keysPath)

View File

@ -9,7 +9,6 @@ describe('Manifest', () => {
let manifestStore
before(async () => {
// ipfs = await IPFS.create({ ...config.daemon1, repo })
ipfs = await createHelia()
manifestStore = await ManifestStore({ ipfs })
})

View File

@ -26,7 +26,6 @@ describe('Add a custom database type', function () {
let orbitdb
before(async () => {
// ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs = await createHelia()
orbitdb = await createOrbitDB({ ipfs })
})

View File

@ -11,7 +11,6 @@ describe('Add a custom identity provider', function () {
let ipfs
before(async () => {
// ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs = await createHelia()
})

View File

@ -11,7 +11,6 @@ describe('Drop databases', function () {
let db
before(async () => {
// ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs' })
ipfs = await createHelia()
})

View File

@ -50,7 +50,7 @@ describe('OrbitDB', function () {
it('has the IPFS instance given as a parameter', async () => {
const { id: expectedId } = ipfs1.libp2p.peerId
const { id: resultId } = ipfs1.libp2p.peerId
const { id: resultId } = orbitdb1.ipfs.libp2p.peerId
strictEqual(expectedId, resultId)
})

View File

@ -16,7 +16,6 @@ describe('Storages', function () {
let testIdentity
before(async () => {
// ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs = await createHelia()
await copy(testKeysPath, keysPath)
keystore = await KeyStore({ path: keysPath })

View File

@ -62,7 +62,7 @@ describe('Sync protocol', function () {
await sync.stop()
}
if (log) {
await log.close
await log.close()
}
})

View File

@ -14,29 +14,18 @@ const connectIpfsNodes = async (ipfs1, ipfs2, options = {
await ipfs1.libp2p.dial(multiaddr(`/ip4/127.0.0.1/tcp/12345/ws/p2p/${relayId}`))
let a1
let address1
await waitFor(() => {
a1 = ipfs1.libp2p.getMultiaddrs().filter(ma => WebRTC.matches(ma)).pop()
if (a1 != null) {
return true
} else {
return false
}
address1 = ipfs1.libp2p.getMultiaddrs().filter(ma => WebRTC.matches(ma)).pop()
return address1 != null
}, () => true)
await ipfs2.libp2p.dial(a1)
await ipfs2.libp2p.dial(address1)
} else {
await ipfs2.libp2p.peerStore.save(ipfs1.libp2p.peerId, { multiaddrs: ipfs1.libp2p.getMultiaddrs().filter(options.filter) })
await ipfs2.libp2p.dial(ipfs1.libp2p.peerId)
}
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 1000)
})
}
export default connectIpfsNodes