Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

have fallback IPFS #658

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions indexer-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:
ENABLE_RESOURCE_MONITOR: ${ENABLE_RESOURCE_MONITOR}
ESTIMATES_LINEARQF_WORKER_POOL_SIZE: ${ESTIMATES_LINEARQF_WORKER_POOL_SIZE}
PINO_PRETTY: ${PINO_PRETTY}
IPFS_GATEWAY: ${IPFS_GATEWAY}
IPFS_GATEWAYS: ${IPFS_GATEWAYS}
COINGECKO_API_KEY: ${COINGECKO_API_KEY}
GRAPHILE_LICENSE: ${GRAPHILE_LICENSE}
SEPOLIA_RPC_URL: ${SEPOLIA_RPC_URL}
Expand Down Expand Up @@ -62,7 +62,7 @@ services:
ENABLE_RESOURCE_MONITOR: ${ENABLE_RESOURCE_MONITOR}
ESTIMATES_LINEARQF_WORKER_POOL_SIZE: ${ESTIMATES_LINEARQF_WORKER_POOL_SIZE}
PINO_PRETTY: ${PINO_PRETTY}
IPFS_GATEWAY: ${IPFS_GATEWAY}
IPFS_GATEWAYS: ${IPFS_GATEWAYS}
COINGECKO_API_KEY: ${COINGECKO_API_KEY}
GRAPHILE_LICENSE: ${GRAPHILE_LICENSE}
SEPOLIA_RPC_URL: ${SEPOLIA_RPC_URL}
Expand Down
13 changes: 7 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ const CHAINS: Chain[] = [
.default("https://evm-rpc.sei-apis.com")
.parse(process.env.SEI_MAINNET_RPC_URL),
pricesFromTimestamp: Date.UTC(2024, 0, 1, 0, 0, 0),
maxGetLogsRange: 10000,
maxGetLogsRange: 1000,
tokens: [
{
code: "SEI",
Expand Down Expand Up @@ -1818,7 +1818,7 @@ export type Config = {
httpServerWaitForSync: boolean;
httpServerEnabled: boolean;
indexerEnabled: boolean;
ipfsGateway: string;
ipfsGateways: string[];
coingeckoApiKey: string | null;
coingeckoApiUrl: string;
chains: Chain[];
Expand Down Expand Up @@ -1968,10 +1968,11 @@ export function getConfig(): Config {

const runOnce = z.boolean().default(false).parse(args["run-once"]);

const ipfsGateway = z
const ipfsGateways = z
.string()
.default("https://ipfs.io")
.parse(process.env.IPFS_GATEWAY);
.array()
.default(["https://ipfs.io"])
.parse(JSON.parse(process.env.IPFS_GATEWAYS!));

const sentryDsn = z
.union([z.string(), z.null()])
Expand Down Expand Up @@ -2028,7 +2029,7 @@ export function getConfig(): Config {
cacheDir,
logLevel,
runOnce,
ipfsGateway,
ipfsGateways,
passportScorerId,
apiHttpPort,
pinoPretty,
Expand Down
74 changes: 54 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,29 +465,63 @@ async function catchupAndWatchChain(
return undefined;
}

const url = `${config.ipfsGateway}/ipfs/${cid}`;
// Fetch from a single IPFS gateway
const fetchFromGateway = async (url: string): Promise<T | undefined> => {
try {
const res = await fetch(url, {
timeout: 2000,
onRetry(cause) {
chainLogger.debug({
msg: "Retrying IPFS request",
url: url,
err: cause,
});
},
retry: { retries: 3, minTimeout: 2000, maxTimeout: 60 * 10000 },
// IPFS data is immutable, we can rely entirely on the cache when present
cache: "force-cache",
cachePath:
config.cacheDir !== null
? path.join(config.cacheDir, "ipfs")
: undefined,
});

// chainLogger.trace(`Fetching ${url}`);
if (res.ok) {
return (await res.json()) as T; // Return the fetched data
} else {
chainLogger.warn(
`Failed to fetch from ${url}, status: ${res.status} ${res.statusText}`
);
}
} catch (err) {
chainLogger.error(
`Error fetching from gateway ${url}: ${String(err)}`
);
}
};

const res = await fetch(url, {
timeout: 2000,
onRetry(cause) {
chainLogger.debug({
msg: "Retrying IPFS request",
url: url,
err: cause,
});
},
retry: { retries: 3, minTimeout: 2000, maxTimeout: 60 * 10000 },
// IPFS data is immutable, we can rely entirely on the cache when present
cache: "force-cache",
cachePath:
config.cacheDir !== null
? path.join(config.cacheDir, "ipfs")
: undefined,
});
// Iterate through each gateway and attempt to fetch data
for (const gateway of config.ipfsGateways) {
const url = `${gateway}/ipfs/${cid}`;
chainLogger.info(`Trying IPFS gateway: ${gateway} for CID: ${cid}`);

return (await res.json()) as T;
const result = await fetchFromGateway(url);
if (result !== undefined) {
chainLogger.info(
`Fetch successful from gateway: ${gateway} for CID: ${cid}`
);
return result; // Return the result if fetched successfully
} else {
chainLogger.warn(
`IPFS fetch failed for gateway ${gateway} for CID ${cid}`
);
}
}

chainLogger.error(
`Failed to fetch IPFS data for CID ${cid} from all gateways.`
);
return undefined; // Return undefined if all gateways fail
};

chainLogger.info("DEBUG: catching up with blockchain events");
Expand Down