Skip to content

Commit

Permalink
fix: metadata not found, will retry
Browse files Browse the repository at this point in the history
  • Loading branch information
silkroadnomad committed Jan 23, 2025
1 parent ef7c5ad commit fd17d8b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 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.56",
"version": "0.12.57",
"private": true,
"scripts": {
"start:no-restart": "node src/relay.js",
Expand Down
4 changes: 3 additions & 1 deletion relay/src/libp2p-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ export function createLibp2pConfig({
canRelayMessage: true,
scoreThresholds,
}),
circuitRelay: circuitRelayServer(),
circuitRelay: circuitRelayServer({
topic: 'doichain._peer-discovery._p2p._pubsub',
}),
// circuitRelay: circuitRelayServer({
// reservations: {
// maxReservations: 1000
Expand Down
38 changes: 27 additions & 11 deletions relay/src/pinner/pinningService.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,40 +231,56 @@ export class PinningService {
async shouldRemainPinned(cid) {
if(!this.db) await this.initializeDatabase()
return this.queue.add(async () => {

// Clean up any stale lock files before opening
const MAX_RETRIES = 3;
const RETRY_DELAY = 1000; // 1 second delay between retries
const RETRY_DELAY = 1000;

for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
logger.info(`Attempt ${attempt}/${MAX_RETRIES} to check pin status for ${cid}`);

// Verify database is properly opened
if (!this.db || typeof this.db.query !== 'function') {
throw new Error('Database not properly initialized');
// Add database health check
if (!this.db || !this.db.query) {
logger.error('Database connection is not properly initialized');
throw new Error('Database connection error');
}

const allDocs = await this.db.query((doc) => doc.cid === cid);
const metadata = allDocs[0];
// Add CID validation
if (!cid || typeof cid !== 'string') {
logger.error('Invalid CID format');
throw new Error('Invalid CID format');
}

if (!metadata) {
// Add timeout protection for database query
const queryPromise = this.db.query((doc) => doc.cid === cid);
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Database query timeout')), 5000)
);

const allDocs = await Promise.race([queryPromise, timeoutPromise]);

if (!allDocs || allDocs.length === 0) {
logger.info(`No metadata found for CID ${cid}`);
// Only return false on last attempt if no metadata found
if (attempt === MAX_RETRIES) {
return false;
}
throw new Error('Metadata not found, will retry');
}

const metadata = allDocs[0];

// Add metadata validation
if (!metadata || !metadata.expirationDate) {
logger.error('Invalid metadata format');
throw new Error('Invalid metadata format');
}

const now = Date.now();
const shouldKeepPinned = (
now < metadata.expirationDate ||
!metadata.requirePayment ||
metadata.paymentSufficient
);

// Enhanced logging for pin status decisions
logger.info(`Pin status check for ${cid}:`, {
currentTime: new Date(now).toISOString(),
expirationDate: new Date(metadata.expirationDate).toISOString(),
Expand Down
1 change: 1 addition & 0 deletions relay/src/pubsubHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function setupPubsub(
) {
const pinningService = new PinningService(helia, orbitdb, electrumClient);
helia.libp2p.services.pubsub.subscribe(contentTopic);
helia.libp2p.services.pubsub.subscribe("doichain._peer-discovery._p2p._pubsub")

helia.libp2p.services.pubsub.addEventListener('message', async (event) => {
// logger.info(`Received pubsub message from ${event.detail.from} on topic ${event.detail.topic}`);
Expand Down
6 changes: 6 additions & 0 deletions relay/tests/relay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { expect } from 'chai';
import { createHelia } from 'helia';
import { unixfs } from '@helia/unixfs';
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery';
import { identify } from '@libp2p/identify';
import { tcp } from '@libp2p/tcp';
import { webSockets } from '@libp2p/websockets';
Expand Down Expand Up @@ -153,6 +154,9 @@ describe('Doichain Relay Pinning Service Test', function () {
bootstrap({
list: [`/ip4/127.0.0.1/tcp/9090/p2p/${targetPeerId.toString()}`],
}),
pubsubPeerDiscovery({
topic: 'doichain._peer-discovery._p2p._pubsub',
}),
],
},
});
Expand All @@ -166,6 +170,8 @@ describe('Doichain Relay Pinning Service Test', function () {
await pubsub.subscribe(CONTENT_TOPIC);
console.log('✅ Subscription complete!');

// await pubsub.subscribe("doichain._peer-discovery._p2p._pubsub']")

console.log('⏳ Waiting for any relay peer...');
await waitForAnyPeer(helia.libp2p);
console.log('✅ Peer setup complete!');
Expand Down

0 comments on commit fd17d8b

Please sign in to comment.