Skip to content

Commit

Permalink
refactor: load into memory only data from the corresponding chain to …
Browse files Browse the repository at this point in the history
…memory cached strategy registry
  • Loading branch information
0xnigir1 committed Dec 24, 2024
1 parent 26b90ac commit 265aefc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
16 changes: 9 additions & 7 deletions apps/processing/src/services/processing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ export class ProcessingService {
const { eventRegistryRepository, strategyRegistryRepository } = registriesRepositories;
const orchestrators: Map<ChainId, Orchestrator> = 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" }),
Expand All @@ -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,
Expand All @@ -78,7 +80,7 @@ export class ProcessingService {
indexerClient,
{
eventsRegistry: cachedEventsRegistry,
strategyRegistry,
strategyRegistry: cachedStrategyRegistry,
},
chain.fetchLimit,
chain.fetchDelayMs,
Expand Down
2 changes: 1 addition & 1 deletion apps/processing/test/unit/processing.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<InMemoryCachedStrategyRegistry> {
const strategies = await strategyRegistry.getStrategies();
const cache = new Map<ChainId, Map<Address, Strategy>>();

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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe("InMemoryCachedStrategyRegistry", () => {
const registry = await InMemoryCachedStrategyRegistry.initialize(
logger,
mockStrategyRegistry,
chainId,
);
const cached = await registry.getStrategyId(chainId, strategyAddress);

Expand All @@ -65,6 +66,7 @@ describe("InMemoryCachedStrategyRegistry", () => {
const registry = await InMemoryCachedStrategyRegistry.initialize(
logger,
mockStrategyRegistry,
chainId,
);
const result = await registry.getStrategyId(chainId, strategyAddress);

Expand All @@ -78,6 +80,7 @@ describe("InMemoryCachedStrategyRegistry", () => {
const registry = await InMemoryCachedStrategyRegistry.initialize(
logger,
mockStrategyRegistry,
chainId,
);
await registry.saveStrategyId(chainId, strategyAddress, strategyId, true);

Expand Down Expand Up @@ -109,6 +112,7 @@ describe("InMemoryCachedStrategyRegistry", () => {
const registry = await InMemoryCachedStrategyRegistry.initialize(
logger,
mockStrategyRegistry,
chainId,
);
await registry.saveStrategyId(chainId, strategyAddress, strategyId, true);

Expand All @@ -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);
Expand Down

0 comments on commit 265aefc

Please sign in to comment.