Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: chain specific cached strategy registry #46

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading