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

ipfs fallback urls #567

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ DATABASE_URL=postgres://postgres:postgres@localhost:5432/grants_stack_indexer
#LUKSO_TESTNET_RPC_URL

#COINGECKO_API_KEY=
#IPFS_GATEWAY=
#IPFS_GATEWAY=https://gateway1.com,https://gateway2.com

# optional, enable the Postgraphile Pro plugin: https://www.npmjs.com/package/@graphile/pro
#GRAPHILE_LICENSE
63 changes: 41 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,38 +448,57 @@ async function catchupAndWatchChain(
const { db, priceProvider, blockTimestampInMs } = config;

try {
const cachedIpfsGet = async <T>(cid: string): Promise<T | undefined> => {
async function cachedIpfsGet<T>(cid: string): Promise<T | undefined> {
const cidRegex = /^(Qm[1-9A-HJ-NP-Za-km-z]{44}|baf[0-9A-Za-z]{50,})$/;
if (!cidRegex.test(cid)) {
chainLogger.warn(`Invalid IPFS CID: ${cid}`);
return undefined;
}

const url = `${config.ipfsGateway}/ipfs/${cid}`;

// chainLogger.trace(`Fetching ${url}`);
const gatewayList: string[] = [];
if (
typeof config.ipfsGateway === "string" &&
config.ipfsGateway.includes(",")
) {
gatewayList.push(...config.ipfsGateway.split(","));
} else if (typeof config.ipfsGateway === "string") {
gatewayList.push(config.ipfsGateway);
} else {
// Handle invalid configuration format (optional)
chainLogger.error("Invalid IPFS Gateway configuration format");
return undefined;
}

const res = await fetch(url, {
timeout: 2000,
onRetry(cause) {
chainLogger.debug({
msg: "Retrying IPFS request",
url: url,
err: cause,
for (const gateway of gatewayList) {
const url = `${gateway}/ipfs/${cid}`;

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,
});
},
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,
});

return (await res.json()) as T;
};
return (await res.json()) as T;
} catch (error) {
chainLogger.error(`Failed to fetch data from ${gateway}:`, error);
}
}

throw new Error("Failed to fetch data from any gateway");
}
chainLogger.info("catching up with blockchain events");

const indexerLogger = chainLogger.child({ subsystem: "DataUpdater" });
Expand Down
Loading