From 6b188858db82785ccd02d5438c2421a9a7880c87 Mon Sep 17 00:00:00 2001 From: Kurdistan Tech Ministry Date: Tue, 10 Mar 2026 00:40:45 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20prevent=20crash=20loop=20on=20pruned=20s?= =?UTF-8?q?tate=20=E2=80=94=20patch=20getExtrinsicSuccess=20and=20events.t?= =?UTF-8?q?oArray?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When SubQuery restarts and hits pruned blocks, events are empty (from patch 4). This caused two downstream crashes: - getExtrinsicSuccess(undefined) → TypeError on findIndex - wrapBlock with events.toArray() → TypeError on undefined Patches 5-6 handle undefined events gracefully. --- docker/patches/pruned-state-fallback.js | 29 ++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/docker/patches/pruned-state-fallback.js b/docker/patches/pruned-state-fallback.js index 8ad802e..b7ef19d 100644 --- a/docker/patches/pruned-state-fallback.js +++ b/docker/patches/pruned-state-fallback.js @@ -66,4 +66,31 @@ utilsCode2 = utilsCode2.replace( fs.writeFileSync(utilsFile, utilsCode2); console.log(`[4/5] utils/substrate.js fetchEventsRange: ${count} patches`); -console.log("[5/5] All pruned-state patches applied successfully."); +// --- Patch 5: @subql/node utils/substrate.js (wrapExtrinsics / getExtrinsicSuccess) --- +// When events are empty (pruned block), groupEventsByExtrinsic returns {}. +// Then groupedEvents[idx] is undefined for each extrinsic, and +// getExtrinsicSuccess(undefined) crashes with "Cannot read properties of undefined +// (reading 'findIndex')". Fix: default to empty array when events is undefined. +count = 0; +let utilsCode3 = fs.readFileSync(utilsFile, "utf8"); + +// Patch getExtrinsicSuccess to handle undefined events +utilsCode3 = utilsCode3.replace( + /function getExtrinsicSuccess\(events\) \{/g, + () => { count++; return "function getExtrinsicSuccess(events) { if (!events || !events.findIndex) return false;"; } +); +fs.writeFileSync(utilsFile, utilsCode3); +console.log(`[5/7] utils/substrate.js getExtrinsicSuccess: ${count} patches`); + +// --- Patch 6: @subql/node utils/substrate.js (events.toArray fallback) --- +// When events is undefined or has no toArray method, default to empty array. +count = 0; +let utilsCode4 = fs.readFileSync(utilsFile, "utf8"); +utilsCode4 = utilsCode4.replace( + /const wrappedBlock = wrapBlock\(block, events\.toArray\(\), parentSpecVersion\);/g, + () => { count++; return "const wrappedBlock = wrapBlock(block, (events && events.toArray) ? events.toArray() : [], parentSpecVersion);"; } +); +fs.writeFileSync(utilsFile, utilsCode4); +console.log(`[6/7] utils/substrate.js events.toArray: ${count} patches`); + +console.log("[7/7] All pruned-state patches applied successfully.");