Skip to content

Commit

Permalink
fix: removing throw
Browse files Browse the repository at this point in the history
  • Loading branch information
silkroadnomad committed Jan 24, 2025
1 parent 30f0225 commit d6b3d23
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 85 deletions.
2 changes: 1 addition & 1 deletion relay/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion relay/src/pinner/pinningService.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class PinningService {
};
} catch (error) {
logger.error(`Error pinning content ${cid}:`, error);
throw error;
// throw error;
}
});
}
Expand Down
171 changes: 88 additions & 83 deletions relay/src/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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...');
Expand Down

0 comments on commit d6b3d23

Please sign in to comment.