From 45784e11960c04ca38cb00a4b3bd30d6a2a5d28d Mon Sep 17 00:00:00 2001 From: Kurdistan Tech Ministry Date: Sat, 21 Feb 2026 17:30:34 +0300 Subject: [PATCH] fix: handle pruned state in event fetch to prevent crash loops When SubQuery restarts and the last processed block's state has been pruned by the RPC node, fetchEventsRange would crash the indexer in a loop. Now returns empty events for pruned blocks so the indexer can skip forward gracefully. --- docker/patches/pruned-state-fallback.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/docker/patches/pruned-state-fallback.js b/docker/patches/pruned-state-fallback.js index 644faba..8ad802e 100644 --- a/docker/patches/pruned-state-fallback.js +++ b/docker/patches/pruned-state-fallback.js @@ -53,9 +53,17 @@ initCode = initCode.replace( fs.writeFileSync(initFile, initCode); console.log(`[3/4] @polkadot/api Init.js: ${count} patches`); -// --- Patch 4: @polkadot/api events.js (system.events.at) --- -// fetchEventsRange calls api.query.system.events.at(hash) which may also fail -// for pruned blocks. Not patching this as it's a data fetch, not startup blocker. -// SubQuery will retry or skip those blocks. +// --- Patch 4: @subql/node utils/substrate.js (fetchEventsRange) --- +// When events.at(hash) fails with "State already discarded" for pruned blocks, +// return an empty events array instead of crashing. The block is processed with +// no events (data is lost anyway since state was pruned). +count = 0; +let utilsCode2 = fs.readFileSync(utilsFile, "utf8"); +utilsCode2 = utilsCode2.replace( + /api\.query\.system\.events\.at\(hash\)\.catch\(\(e\) => \{/g, + () => { count++; return "api.query.system.events.at(hash).catch((e) => { if (e.message && e.message.includes('State already discarded')) { const empty = []; empty.toArray = () => []; return empty; }"; } +); +fs.writeFileSync(utilsFile, utilsCode2); +console.log(`[4/5] utils/substrate.js fetchEventsRange: ${count} patches`); -console.log("[4/4] All pruned-state patches applied successfully."); +console.log("[5/5] All pruned-state patches applied successfully.");