From 265aefcd55352b8f21f6211149e7f0147e72647c Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Mon, 23 Dec 2024 22:30:12 -0300 Subject: [PATCH] refactor: load into memory only data from the corresponding chain to memory cached strategy registry --- .../src/services/processing.service.ts | 16 +++++++++------- .../test/unit/processing.service.spec.ts | 2 +- .../strategy/cachedStrategyRegistry.ts | 7 +++++-- .../registries/cachedStrategyRegistry.spec.ts | 5 +++++ 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/apps/processing/src/services/processing.service.ts b/apps/processing/src/services/processing.service.ts index 0cbe94a..2d5e7b2 100644 --- a/apps/processing/src/services/processing.service.ts +++ b/apps/processing/src/services/processing.service.ts @@ -46,12 +46,9 @@ export class ProcessingService { const { eventRegistryRepository, strategyRegistryRepository } = registriesRepositories; const orchestrators: Map = new Map(); - const strategyRegistry = await InMemoryCachedStrategyRegistry.initialize( - new Logger({ className: "InMemoryCachedStrategyRegistry" }), - new DatabaseStrategyRegistry( - new Logger({ className: "DatabaseStrategyRegistry" }), - strategyRegistryRepository, - ), + const strategyRegistry = new DatabaseStrategyRegistry( + new Logger({ className: "DatabaseStrategyRegistry" }), + strategyRegistryRepository, ); const eventsRegistry = new DatabaseEventRegistry( new Logger({ className: "DatabaseEventRegistry" }), @@ -69,6 +66,11 @@ export class ProcessingService { eventsRegistry, [chain.id as ChainId], ); + const cachedStrategyRegistry = await InMemoryCachedStrategyRegistry.initialize( + new Logger({ className: "InMemoryCachedStrategyRegistry" }), + strategyRegistry, + chain.id as ChainId, + ); orchestrators.set( chain.id as ChainId, @@ -78,7 +80,7 @@ export class ProcessingService { indexerClient, { eventsRegistry: cachedEventsRegistry, - strategyRegistry, + strategyRegistry: cachedStrategyRegistry, }, chain.fetchLimit, chain.fetchDelayMs, diff --git a/apps/processing/test/unit/processing.service.spec.ts b/apps/processing/test/unit/processing.service.spec.ts index 15b59ff..c35d26c 100644 --- a/apps/processing/test/unit/processing.service.spec.ts +++ b/apps/processing/test/unit/processing.service.spec.ts @@ -101,10 +101,10 @@ describe("ProcessingService", () => { }); it("initializes multiple orchestrators correctly", () => { - expect(InMemoryCachedStrategyRegistry.initialize).toHaveBeenCalledTimes(1); expect(DatabaseStrategyRegistry).toHaveBeenCalledTimes(1); expect(DatabaseEventRegistry).toHaveBeenCalledTimes(1); expect(EvmProvider).toHaveBeenCalledTimes(2); + expect(InMemoryCachedStrategyRegistry.initialize).toHaveBeenCalledTimes(2); expect(InMemoryCachedEventRegistry.initialize).toHaveBeenCalledTimes(2); // Verify orchestrators were created with correct parameters diff --git a/packages/data-flow/src/registries/strategy/cachedStrategyRegistry.ts b/packages/data-flow/src/registries/strategy/cachedStrategyRegistry.ts index 05cfec8..65ddda5 100644 --- a/packages/data-flow/src/registries/strategy/cachedStrategyRegistry.ts +++ b/packages/data-flow/src/registries/strategy/cachedStrategyRegistry.ts @@ -28,16 +28,19 @@ export class InMemoryCachedStrategyRegistry implements IStrategyRegistry { * * @param logger - The logger instance * @param strategyRegistry - The strategy registry instance + * @param chainId - The chain ID to load strategies for * @returns The initialized cached strategy registry */ static async initialize( logger: ILogger, strategyRegistry: IStrategyRegistry, + chainId: ChainId, ): Promise { - const strategies = await strategyRegistry.getStrategies(); const cache = new Map>(); - logger.debug(`Loading strategies into memory...`); + logger.debug(`Loading strategies into memory for chain ID: ${chainId}...`); + + const strategies = await strategyRegistry.getStrategies({ chainId }); for (const strategy of strategies) { if (!cache.has(strategy.chainId)) { diff --git a/packages/data-flow/test/registries/cachedStrategyRegistry.spec.ts b/packages/data-flow/test/registries/cachedStrategyRegistry.spec.ts index d3edecb..439095b 100644 --- a/packages/data-flow/test/registries/cachedStrategyRegistry.spec.ts +++ b/packages/data-flow/test/registries/cachedStrategyRegistry.spec.ts @@ -44,6 +44,7 @@ describe("InMemoryCachedStrategyRegistry", () => { const registry = await InMemoryCachedStrategyRegistry.initialize( logger, mockStrategyRegistry, + chainId, ); const cached = await registry.getStrategyId(chainId, strategyAddress); @@ -65,6 +66,7 @@ describe("InMemoryCachedStrategyRegistry", () => { const registry = await InMemoryCachedStrategyRegistry.initialize( logger, mockStrategyRegistry, + chainId, ); const result = await registry.getStrategyId(chainId, strategyAddress); @@ -78,6 +80,7 @@ describe("InMemoryCachedStrategyRegistry", () => { const registry = await InMemoryCachedStrategyRegistry.initialize( logger, mockStrategyRegistry, + chainId, ); await registry.saveStrategyId(chainId, strategyAddress, strategyId, true); @@ -109,6 +112,7 @@ describe("InMemoryCachedStrategyRegistry", () => { const registry = await InMemoryCachedStrategyRegistry.initialize( logger, mockStrategyRegistry, + chainId, ); await registry.saveStrategyId(chainId, strategyAddress, strategyId, true); @@ -130,6 +134,7 @@ describe("InMemoryCachedStrategyRegistry", () => { const registry = await InMemoryCachedStrategyRegistry.initialize( logger, mockStrategyRegistry, + chainId, ); const params = { handled: true, chainId }; const result = await registry.getStrategies(params);