Skip to content

Commit

Permalink
Connect to gateway peers before pin instead of connect periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhaixian1984 committed Oct 17, 2024
1 parent 44d357f commit 649f947
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 43 deletions.
22 changes: 21 additions & 1 deletion src/ipfs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { BigNumber } from 'bignumber.js';
import { addrToHostPort } from '../utils';
import IpfsHttpClient from 'ipfs-http-client';
import { AbortController } from 'native-abort-controller';
import { CrustGWPeers, DefaultCrustGWPeers } from '../tasks/ipfs-update-peers-list-task';
import { createChildLogger } from '../utils/logger';

const CID = (IpfsHttpClient as any).CID; // eslint-disable-line

const logger = createChildLogger({ moduleId: 'ipfs-api' });

export default class IpfsApi {
private readonly ipfs: any; // eslint-disable-line

Expand All @@ -28,8 +32,9 @@ export default class IpfsApi {
pin(c: string, to: number): [AbortController, Promise<boolean>] {
const controller = new AbortController();
const signal = controller.signal;

const result = async () => {
await this.connectGatewayPeers();
const cid = new CID(c);
const pin = await this.ipfs.pin.add(cid, { timeout: to, signal });
return cid.equals(pin) as boolean;
Expand All @@ -38,6 +43,21 @@ export default class IpfsApi {
return [controller, result()];
}

async connectGatewayPeers(): Promise<void> {
const peersToConnect = CrustGWPeers.size > 0 ? Array.from(CrustGWPeers): DefaultCrustGWPeers;

let successCount = 0;
for (const peer of peersToConnect) {
try {
await this.ipfs.swarm.connect(peer);
successCount++;
} catch (error) {
logger.debug(`Failed to connect to peer: '${peer}'. Error: ${error}`);
}
}
logger.debug(`Connect to ${successCount} dedicated crust gateway peers.`);
}

/**
* NO USE
* Pin remove file by a given cid
Expand Down
2 changes: 0 additions & 2 deletions src/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { createSealCleanupTask } from './seal-cleanup-task';
import { createSealStatuUpdater } from './seal-status-updater-task';
import { createTelemetryReportTask } from './telemetry-task';
import { createIpfsUpdatePeersListTask } from './ipfs-update-peers-list-task';
import { createIpfsConnectPeersTask } from './ipfs-connect-peers-task';

/**
* create simpile tasks which only handle start/stop
Expand All @@ -32,7 +31,6 @@ export async function createSimpleTasks(
createNodeInfoUpdateTask,
createSealCleanupTask,
createIpfsUpdatePeersListTask,
createIpfsConnectPeersTask
];
return Bluebird.mapSeries(tasks, (t) => {
return t(context, logger);
Expand Down
40 changes: 0 additions & 40 deletions src/tasks/ipfs-connect-peers-task.ts

This file was deleted.

0 comments on commit 649f947

Please sign in to comment.