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' import { circuitRelayTransport } from 'libp2p/circuit-relay'
/** /**
* A basic Libp2p configuration for node servers. * A basic Libp2p configuration for Node.js nodes.
*/ */
export const DefaultLibp2pOptions = { export const DefaultLibp2pOptions = {
addresses: { addresses: {
@ -24,13 +24,9 @@ export const DefaultLibp2pOptions = {
}) })
], ],
connectionEncryption: [noise()], connectionEncryption: [noise()],
streamMuxers: [ streamMuxers: [yamux()],
yamux()
],
connectionGater: { connectionGater: {
denyDialMultiaddr: () => { denyDialMultiaddr: () => false
return false
}
}, },
services: { services: {
identify: identifyService(), identify: identifyService(),
@ -55,13 +51,9 @@ export const DefaultLibp2pBrowserOptions = {
}) })
], ],
connectionEncryption: [noise()], connectionEncryption: [noise()],
streamMuxers: [ streamMuxers: [yamux()],
yamux()
],
connectionGater: { connectionGater: {
denyDialMultiaddr: () => { denyDialMultiaddr: () => false
return false
}
}, },
services: { services: {
identify: identifyService(), 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 (!ipfs) throw new Error('An instance of ipfs is required.')
if (!log) throw new Error('An instance of log 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 address = log.id
const headsSyncAddress = pathJoin('/orbitdb/heads/', address) const headsSyncAddress = pathJoin('/orbitdb/heads/', address)
@ -194,7 +197,7 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => {
const { signal } = timeoutController const { signal } = timeoutController
try { try {
peers.add(peerId) 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)) await pipe(sendHeads, stream, receiveHeads(peerId))
} catch (e) { } catch (e) {
console.error(e) console.error(e)
@ -218,18 +221,19 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => {
} }
const handleUpdateMessage = async message => { const handleUpdateMessage = async message => {
const { topic, data } = message.detail
const task = async () => { const task = async () => {
const messageHasData = message => message.detail.data !== undefined
try { try {
if (messageHasData(message) && onSynced) { if (data && onSynced) {
await onSynced(message.detail.data) await onSynced(data)
} }
} catch (e) { } catch (e) {
events.emit('error', e) events.emit('error', e)
} }
} }
if (message.detail.topic === address) { if (topic === address) {
queue.add(task) queue.add(task)
} }
} }
@ -243,7 +247,7 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => {
*/ */
const add = async (entry) => { const add = async (entry) => {
if (started) { 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) { if (started) {
started = false started = false
await queue.onIdle() await queue.onIdle()
ipfs.libp2p.services.pubsub.removeEventListener('subscription-change', handlePeerSubscribed) pubsub.removeEventListener('subscription-change', handlePeerSubscribed)
ipfs.libp2p.services.pubsub.removeEventListener('message', handleUpdateMessage) pubsub.removeEventListener('message', handleUpdateMessage)
await ipfs.libp2p.unhandle(headsSyncAddress) await libp2p.unhandle(headsSyncAddress)
await ipfs.libp2p.services.pubsub.unsubscribe(address) await pubsub.unsubscribe(address)
peers.clear() peers.clear()
} }
} }
@ -274,11 +278,11 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => {
const startSync = async () => { const startSync = async () => {
if (!started) { if (!started) {
// Exchange head entries with peers when connected // Exchange head entries with peers when connected
await ipfs.libp2p.handle(headsSyncAddress, handleReceiveHeads) await libp2p.handle(headsSyncAddress, handleReceiveHeads)
ipfs.libp2p.services.pubsub.addEventListener('subscription-change', handlePeerSubscribed) pubsub.addEventListener('subscription-change', handlePeerSubscribed)
ipfs.libp2p.services.pubsub.addEventListener('message', handleUpdateMessage) pubsub.addEventListener('message', handleUpdateMessage)
// Subscribe to the pubsub channel for this database through which updates are sent // 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 started = true
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -62,7 +62,7 @@ describe('Sync protocol', function () {
await sync.stop() await sync.stop()
} }
if (log) { 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}`)) await ipfs1.libp2p.dial(multiaddr(`/ip4/127.0.0.1/tcp/12345/ws/p2p/${relayId}`))
let a1 let address1
await waitFor(() => { await waitFor(() => {
a1 = ipfs1.libp2p.getMultiaddrs().filter(ma => WebRTC.matches(ma)).pop() address1 = ipfs1.libp2p.getMultiaddrs().filter(ma => WebRTC.matches(ma)).pop()
return address1 != null
if (a1 != null) {
return true
} else {
return false
}
}, () => true) }, () => true)
await ipfs2.libp2p.dial(a1) await ipfs2.libp2p.dial(address1)
} else { } else {
await ipfs2.libp2p.peerStore.save(ipfs1.libp2p.peerId, { multiaddrs: ipfs1.libp2p.getMultiaddrs().filter(options.filter) }) await ipfs2.libp2p.peerStore.save(ipfs1.libp2p.peerId, { multiaddrs: ipfs1.libp2p.getMultiaddrs().filter(options.filter) })
await ipfs2.libp2p.dial(ipfs1.libp2p.peerId) await ipfs2.libp2p.dial(ipfs1.libp2p.peerId)
} }
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 1000)
})
} }
export default connectIpfsNodes export default connectIpfsNodes