Skip to content

Commit

Permalink
have fallback IPFS
Browse files Browse the repository at this point in the history
  • Loading branch information
thelostone-mc committed Sep 5, 2024
1 parent b6eb77b commit 8ff7884
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 29 deletions.
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
15 changes: 8 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type CoingeckoSupportedChainId =
| 42220
| 1088;

const CHAIN_DATA_VERSION = "81";
const CHAIN_DATA_VERSION = "82";

export type Token = {
code: string;
Expand Down 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: 999,
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
69 changes: 49 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,29 +465,58 @@ 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}: ${err}`,
{ stack: 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}`);

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

return (await res.json()) as T;
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

0 comments on commit 8ff7884

Please sign in to comment.