From a0221df6361989775d78ef9589e159dc0e2d15a3 Mon Sep 17 00:00:00 2001 From: Scott Twiname Date: Tue, 26 Nov 2024 12:36:00 +1300 Subject: [PATCH 1/3] Fix monitor write with workers, fix events iteration --- .../src/indexer/worker/worker.monitor.service.ts | 8 +++++--- packages/node/src/indexer/indexer.manager.ts | 8 ++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/node-core/src/indexer/worker/worker.monitor.service.ts b/packages/node-core/src/indexer/worker/worker.monitor.service.ts index 62be0f64fb..57fa1b5add 100644 --- a/packages/node-core/src/indexer/worker/worker.monitor.service.ts +++ b/packages/node-core/src/indexer/worker/worker.monitor.service.ts @@ -14,7 +14,7 @@ export const hostMonitorKeys: (keyof HostMonitorService)[] = ['hostMonitorServic export function monitorHostFunctions(host: MonitorServiceInterface): HostMonitorService { return { - hostMonitorServiceWrite: host.write.bind(host), + hostMonitorServiceWrite: host?.write.bind(host), }; } @@ -27,8 +27,10 @@ export class WorkerMonitorService implements MonitorServiceInterface { setMonitorService(this); } - write(blockData: string): void { - return this.host?.hostMonitorServiceWrite ? this.host.hostMonitorServiceWrite(blockData) : undefined; + write(blockData: string | (() => string)): void { + return this.host?.hostMonitorServiceWrite + ? this.host.hostMonitorServiceWrite(typeof blockData === 'string' ? blockData : blockData()) + : undefined; } createBlockFork(blockHeight: number): void { diff --git a/packages/node/src/indexer/indexer.manager.ts b/packages/node/src/indexer/indexer.manager.ts index dde11c5be7..bc19f5ffed 100644 --- a/packages/node/src/indexer/indexer.manager.ts +++ b/packages/node/src/indexer/indexer.manager.ts @@ -119,7 +119,7 @@ export class IndexerManager extends BaseIndexerManager< const idx = evt.extrinsic.idx; acc[idx] ??= []; acc[idx].push(evt); - } else { + } else if (!evt.phase.isApplyExtrinsic) { logger.warn( `Unrecognized event type, skipping. block="${block.block.header.number.toNumber()}" eventIdx="${idx}"`, ); @@ -141,9 +141,9 @@ export class IndexerManager extends BaseIndexerManager< await this.indexExtrinsic(extrinsic, dataSources, getVM); // Process extrinsic events - const extrinsicEvents = events - .filter((e) => e.extrinsic?.idx === extrinsic.idx) - .sort((a, b) => a.idx - b.idx); + const extrinsicEvents = (groupedEvents[extrinsic.idx] ?? []).sort( + (a, b) => a.idx - b.idx, + ); for (const event of extrinsicEvents) { await this.indexEvent(event, dataSources, getVM); From f8f43b0622a3129fa96cf57fa1c52103e5b7a585 Mon Sep 17 00:00:00 2001 From: Scott Twiname Date: Tue, 26 Nov 2024 12:40:34 +1300 Subject: [PATCH 2/3] Update changelogs --- packages/node-core/CHANGELOG.md | 2 ++ packages/node/CHANGELOG.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/packages/node-core/CHANGELOG.md b/packages/node-core/CHANGELOG.md index 656ecf2a1e..6a33c8f2a2 100644 --- a/packages/node-core/CHANGELOG.md +++ b/packages/node-core/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Workers crashing because of lazy monitor write (#2607) ## [15.0.2] - 2024-11-26 ### Fixed diff --git a/packages/node/CHANGELOG.md b/packages/node/CHANGELOG.md index cbaf87e81c..6e2f3343f8 100644 --- a/packages/node/CHANGELOG.md +++ b/packages/node/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Not using grouped events (#2607) ## [5.4.1] - 2024-11-26 ### Fixed From 25c4928dc79797954c5812d11c73032c4b856e10 Mon Sep 17 00:00:00 2001 From: Scott Twiname Date: Tue, 26 Nov 2024 14:45:42 +1300 Subject: [PATCH 3/3] Fix optional value --- .../node-core/src/indexer/worker/worker.monitor.service.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/node-core/src/indexer/worker/worker.monitor.service.ts b/packages/node-core/src/indexer/worker/worker.monitor.service.ts index 57fa1b5add..9b766e92c4 100644 --- a/packages/node-core/src/indexer/worker/worker.monitor.service.ts +++ b/packages/node-core/src/indexer/worker/worker.monitor.service.ts @@ -7,12 +7,12 @@ import {setMonitorService} from '../../process'; import {MonitorServiceInterface} from '../monitor.service'; type HostMonitorService = { - hostMonitorServiceWrite: (blockData: string) => void; + hostMonitorServiceWrite?: (blockData: string) => void; }; export const hostMonitorKeys: (keyof HostMonitorService)[] = ['hostMonitorServiceWrite']; -export function monitorHostFunctions(host: MonitorServiceInterface): HostMonitorService { +export function monitorHostFunctions(host?: MonitorServiceInterface): HostMonitorService { return { hostMonitorServiceWrite: host?.write.bind(host), }; @@ -20,7 +20,7 @@ export function monitorHostFunctions(host: MonitorServiceInterface): HostMonitor @Injectable() export class WorkerMonitorService implements MonitorServiceInterface { - constructor(private host: HostMonitorService) { + constructor(private host?: HostMonitorService) { if (isMainThread) { throw new Error('Expected to be worker thread'); }