From 0fd5287c8c2b208515e731f0e6940b8ecd3965fc Mon Sep 17 00:00:00 2001 From: haad Date: Fri, 15 Dec 2023 10:27:02 +0200 Subject: [PATCH 1/2] Remove obsolete timeout, fix couple of typos in tests --- test/orbitdb.test.js | 2 +- test/sync.test.js | 2 +- test/utils/connect-nodes.js | 19 ++++--------------- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/test/orbitdb.test.js b/test/orbitdb.test.js index 05f1f88..d0ab970 100644 --- a/test/orbitdb.test.js +++ b/test/orbitdb.test.js @@ -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) }) diff --git a/test/sync.test.js b/test/sync.test.js index cf26abe..edc7a40 100644 --- a/test/sync.test.js +++ b/test/sync.test.js @@ -62,7 +62,7 @@ describe('Sync protocol', function () { await sync.stop() } if (log) { - await log.close + await log.close() } }) diff --git a/test/utils/connect-nodes.js b/test/utils/connect-nodes.js index 93b0d1f..82eff47 100644 --- a/test/utils/connect-nodes.js +++ b/test/utils/connect-nodes.js @@ -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 From 5b0bc171d10952392fdbe278241aec807099b808 Mon Sep 17 00:00:00 2001 From: haad Date: Fri, 15 Dec 2023 10:28:58 +0200 Subject: [PATCH 2/2] Clean up --- src/config/libp2p/index.js | 18 +++-------- src/sync.js | 32 +++++++++++-------- test/database.test.js | 1 - test/databases/documents.test.js | 1 - test/databases/events.test.js | 1 - test/databases/keyvalue-indexed.js | 1 - test/databases/keyvalue.test.js | 1 - test/manifest-store.test.js | 1 - test/orbitdb-custom-database-types.test.js | 1 - .../orbitdb-custom-identity-providers.test.js | 1 - test/orbitdb-drop.test.js | 1 - test/storage.test.js | 1 - 12 files changed, 23 insertions(+), 37 deletions(-) diff --git a/src/config/libp2p/index.js b/src/config/libp2p/index.js index 6f30831..fd7b7dc 100644 --- a/src/config/libp2p/index.js +++ b/src/config/libp2p/index.js @@ -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(), diff --git a/src/sync.js b/src/sync.js index 9123011..9d9faa8 100644 --- a/src/sync.js +++ b/src/sync.js @@ -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 } } diff --git a/test/database.test.js b/test/database.test.js index 28b4fde..5677b58 100644 --- a/test/database.test.js +++ b/test/database.test.js @@ -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 }) diff --git a/test/databases/documents.test.js b/test/databases/documents.test.js index c5c2fdf..56395a2 100644 --- a/test/databases/documents.test.js +++ b/test/databases/documents.test.js @@ -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) diff --git a/test/databases/events.test.js b/test/databases/events.test.js index 55a1a46..ef8aefa 100644 --- a/test/databases/events.test.js +++ b/test/databases/events.test.js @@ -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) diff --git a/test/databases/keyvalue-indexed.js b/test/databases/keyvalue-indexed.js index f178470..0fbe532 100644 --- a/test/databases/keyvalue-indexed.js +++ b/test/databases/keyvalue-indexed.js @@ -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) diff --git a/test/databases/keyvalue.test.js b/test/databases/keyvalue.test.js index 8318ef9..6a1cd3e 100644 --- a/test/databases/keyvalue.test.js +++ b/test/databases/keyvalue.test.js @@ -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) diff --git a/test/manifest-store.test.js b/test/manifest-store.test.js index c122d7d..9339351 100644 --- a/test/manifest-store.test.js +++ b/test/manifest-store.test.js @@ -9,7 +9,6 @@ describe('Manifest', () => { let manifestStore before(async () => { - // ipfs = await IPFS.create({ ...config.daemon1, repo }) ipfs = await createHelia() manifestStore = await ManifestStore({ ipfs }) }) diff --git a/test/orbitdb-custom-database-types.test.js b/test/orbitdb-custom-database-types.test.js index 387807e..19292e4 100644 --- a/test/orbitdb-custom-database-types.test.js +++ b/test/orbitdb-custom-database-types.test.js @@ -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 }) }) diff --git a/test/orbitdb-custom-identity-providers.test.js b/test/orbitdb-custom-identity-providers.test.js index b06c89d..6bc9554 100644 --- a/test/orbitdb-custom-identity-providers.test.js +++ b/test/orbitdb-custom-identity-providers.test.js @@ -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() }) diff --git a/test/orbitdb-drop.test.js b/test/orbitdb-drop.test.js index 25933d2..68edf98 100644 --- a/test/orbitdb-drop.test.js +++ b/test/orbitdb-drop.test.js @@ -11,7 +11,6 @@ describe('Drop databases', function () { let db before(async () => { - // ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs' }) ipfs = await createHelia() }) diff --git a/test/storage.test.js b/test/storage.test.js index 9f19ed3..5e8ca82 100644 --- a/test/storage.test.js +++ b/test/storage.test.js @@ -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 })