From d6b3d23a0ee7afef997f7188d25e8c169c551933 Mon Sep 17 00:00:00 2001 From: Nico Krause Date: Fri, 24 Jan 2025 07:33:08 +0500 Subject: [PATCH] fix: removing throw --- relay/package.json | 2 +- relay/src/pinner/pinningService.js | 2 +- relay/src/relay.js | 171 +++++++++++++++-------------- 3 files changed, 90 insertions(+), 85 deletions(-) diff --git a/relay/package.json b/relay/package.json index 83a16e6..4d34741 100644 --- a/relay/package.json +++ b/relay/package.json @@ -1,6 +1,6 @@ { "name": "libp2p-relay", - "version": "0.12.65", + "version": "0.12.66", "private": true, "scripts": { "start:no-restart": "node src/relay.js", diff --git a/relay/src/pinner/pinningService.js b/relay/src/pinner/pinningService.js index 6c7e8bb..b2b1e6d 100644 --- a/relay/src/pinner/pinningService.js +++ b/relay/src/pinner/pinningService.js @@ -220,7 +220,7 @@ export class PinningService { }; } catch (error) { logger.error(`Error pinning content ${cid}:`, error); - throw error; + // throw error; } }); } diff --git a/relay/src/relay.js b/relay/src/relay.js index 6feccc5..f7ed8bd 100644 --- a/relay/src/relay.js +++ b/relay/src/relay.js @@ -39,6 +39,7 @@ const relayLocalRegTest = process.env.RELAY_LOCAL_REGTTEST; let blockstore = new LevelBlockstore('./helia-blocks'); let datastore = new LevelDatastore('./helia-data'); +let orbitdb = null; let scoreThresholds = {}; if (relayDevMode) @@ -79,98 +80,102 @@ if (argv['generate-keypair']) { } } -const { helia, orbitdb, electrumClient } = await createNode( - privKeyHex, - datastore, - blockstore, - listenAddresses, - announceAddresses, - pubsubPeerDiscoveryTopics, - scoreThresholds, - network -); -logger.info('Helia and OrbitDB are running'); -const fsHelia = unixfs(helia); - -const tipWatcher = new TipWatcher(electrumClient); -const pinningService = new PinningService(helia, orbitdb, electrumClient); -setupPubsub( - helia, - orbitdb, - electrumClient, - fsHelia, - CONTENT_TOPIC, - tipWatcher, - pinningService -); -// createHttpServr(helia, orbitdb, electrumClient, tipWatcher); - - -let isScanning = false; - -tipWatcher.on('newTip', async (tip) => { - try { - console.log('newTip: ', tip); - // First scan for new name operations - if (isScanning) { - logger.info('Scan already in progress, skipping new tip'); - return; - } - - isScanning = true; - await scanBlockchainForNameOps(electrumClient, helia, orbitdb, tip, pinningService); - isScanning = false; - - // Then check for expired pins - const pinnedCids = []; - for await (const pin of helia.pins.ls()) { - pinnedCids.push(pin.cid.toString()); - } +// Main execution +(async () => { + const { helia, orbitdb, electrumClient } = await createNode( + privKeyHex, + datastore, + blockstore, + listenAddresses, + announceAddresses, + pubsubPeerDiscoveryTopics, + scoreThresholds, + network + ); + + logger.info('Helia and OrbitDB are running'); + const fsHelia = unixfs(helia); + + const tipWatcher = new TipWatcher(electrumClient); + const pinningService = new PinningService(helia, orbitdb, electrumClient); + + setupPubsub( + helia, + orbitdb, + electrumClient, + fsHelia, + CONTENT_TOPIC, + tipWatcher, + pinningService + ); + createHttpServer(helia, orbitdb, electrumClient, tipWatcher); + + let isScanning = false; + + tipWatcher.on('newTip', async (tip) => { + try { + console.log('newTip: ', tip); + // First scan for new name operations + if (isScanning) { + logger.info('Scan already in progress, skipping new tip'); + return; + } + + isScanning = true; + await scanBlockchainForNameOps(electrumClient, helia, orbitdb, tip, pinningService); + isScanning = false; + + // Then check for expired pins + const pinnedCids = []; + for await (const pin of helia.pins.ls()) { + pinnedCids.push(pin.cid.toString()); + } - for (const cid of pinnedCids) { - try { - const shouldRemain = await pinningService.shouldRemainPinned(cid); - - if (!shouldRemain) { - logger.info(`Unpinning expired content: ${cid}`); - await helia.pins.rm(CID.parse(cid)); + for (const cid of pinnedCids) { + try { + const shouldRemain = await pinningService.shouldRemainPinned(cid); - // Update metadata to mark as unpinned - try { - await pinningService.markAsUnpinned(cid); - logger.info(`Successfully updated metadata for unpinned content: ${cid}`); - } catch (error) { - logger.error(`Failed to update metadata for unpinned content ${cid}:`, error); + if (!shouldRemain) { + logger.info(`Unpinning expired content: ${cid}`); + await helia.pins.rm(CID.parse(cid)); + + // Update metadata to mark as unpinned + try { + await pinningService.markAsUnpinned(cid); + logger.info(`Successfully updated metadata for unpinned content: ${cid}`); + } catch (error) { + logger.error(`Failed to update metadata for unpinned content ${cid}:`, error); + } } + } catch (error) { + if (error.message.includes('No active pin metadata found')) { + // If metadata is not found, keep the content pinned as a safety measure + logger.warn(`No metadata found for CID ${cid}, keeping pinned as safety measure`); + continue; + } + logger.error(`Error processing CID ${cid}:`, error); } - } catch (error) { - if (error.message.includes('No active pin metadata found')) { - // If metadata is not found, keep the content pinned as a safety measure - logger.warn(`No metadata found for CID ${cid}, keeping pinned as safety measure`); - continue; - } - logger.error(`Error processing CID ${cid}:`, error); } + } catch (error) { + isScanning = false; + logger.error('Error processing new tip:', error); } - } catch (error) { - isScanning = false; - logger.error('Error processing new tip:', error); - } -}); -await tipWatcher.start(); + }); + await tipWatcher.start(); -if (!argv['disable-scanning']) { - logger.info('Starting blockchain scanning...'); - if (isScanning) { - logger.info('Scan already in progress, skipping initial scan'); + if (!argv['disable-scanning']) { + logger.info('Starting blockchain scanning...'); + if (isScanning) { + logger.info('Scan already in progress, skipping initial scan'); + } else { + isScanning = true; + await scanBlockchainForNameOps(electrumClient, helia, orbitdb, null, pinningService); + isScanning = false; + } } else { - isScanning = true; - await scanBlockchainForNameOps(electrumClient, helia, orbitdb, null, pinningService); - isScanning = false; + logger.info('Blockchain scanning is disabled'); } -} else { - logger.info('Blockchain scanning is disabled'); -} +})(); async function cleanup() { logger.info('Shutting down...');