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

Add PostIndex Handler Support and Enhance CosmosEvent with Event Source Context #304

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
51 changes: 51 additions & 0 deletions packages/common-cosmos/src/project/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import {
CosmosMessageHandler,
CustomModule,
CosmosTxFilter,
CosmosPostIndexBlockHandler,
BatchCosmosTransactionHandler,
BatchCosmosMessageHandler,
BatchCosmosEventHandler,
} from '@subql/types-cosmos';
import {plainToClass, Transform, Type} from 'class-transformer';
import {
Expand Down Expand Up @@ -115,6 +119,45 @@ export class EventHandler implements CosmosEventHandler {
handler!: string;
}

export class BatchTransactionHandler implements BatchCosmosTransactionHandler {
@IsEnum(CosmosHandlerKind, {groups: [CosmosHandlerKind.BatchTransaction]})
kind!: CosmosHandlerKind.BatchTransaction;
@IsString()
handler!: string;
}

export class BatchMessageHandler implements BatchCosmosMessageHandler {
@IsEnum(CosmosHandlerKind, {groups: [CosmosHandlerKind.BatchMessage]})
kind!: CosmosHandlerKind.BatchMessage;
@IsString()
handler!: string;
@IsOptional()
@ValidateNested()
@Type(() => MessageFilter)
filter?: CosmosMessageFilter;
}

export class BatchEventHandler implements BatchCosmosEventHandler {
@IsOptional()
@ValidateNested()
@Type(() => EventFilter)
filter?: CosmosEventFilter;
@IsEnum(CosmosHandlerKind, {groups: [CosmosHandlerKind.BatchEvent]})
kind!: CosmosHandlerKind.BatchEvent;
@IsString()
handler!: string;
}

export class PostIndexHandler implements CosmosPostIndexBlockHandler {
@IsEnum(CosmosHandlerKind, {groups: [CosmosHandlerKind.PostIndex]})
kind!: CosmosHandlerKind.PostIndex;
@IsString()
handler!: string;
@IsOptional()
@Type(() => BlockFilter)
filter?: CosmosBlockFilter;
}

export class CustomHandler implements CosmosCustomHandler {
@IsString()
kind!: string;
Expand All @@ -138,6 +181,14 @@ export class RuntimeMapping implements CosmosMapping {
return plainToClass(TransactionHandler, handler);
case CosmosHandlerKind.Block:
return plainToClass(BlockHandler, handler);
case CosmosHandlerKind.PostIndex:
return plainToClass(PostIndexHandler, handler);
case CosmosHandlerKind.BatchTransaction:
return plainToClass(BatchTransactionHandler, handler);
case CosmosHandlerKind.BatchEvent:
return plainToClass(BatchEventHandler, handler);
case CosmosHandlerKind.BatchMessage:
return plainToClass(BatchMessageHandler, handler);
default:
throw new Error(`handler ${(handler as any).kind} not supported`);
}
Expand Down
24 changes: 24 additions & 0 deletions packages/common-cosmos/src/project/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ export function isEventHandlerProcessor<E>(
return hp.baseHandlerKind === CosmosHandlerKind.Event;
}

export function isPostIndexHandlerProcessor<E>(
hp: SecondLayerHandlerProcessorArray<CosmosHandlerKind, DefaultFilter, unknown>
): hp is SecondLayerHandlerProcessor<CosmosHandlerKind.PostIndex, DefaultFilter, E> {
return hp.baseHandlerKind === CosmosHandlerKind.PostIndex;
}

export function isBatchEventHandlerProcessor<E>(
hp: SecondLayerHandlerProcessorArray<CosmosHandlerKind, DefaultFilter, unknown>
): hp is SecondLayerHandlerProcessor<CosmosHandlerKind.BatchEvent, DefaultFilter, E> {
return hp.baseHandlerKind === CosmosHandlerKind.BatchEvent;
}

export function isBatchTransactionHandlerProcessor<E>(
hp: SecondLayerHandlerProcessorArray<CosmosHandlerKind, DefaultFilter, unknown>
): hp is SecondLayerHandlerProcessor<CosmosHandlerKind.BatchTransaction, DefaultFilter, E> {
return hp.baseHandlerKind === CosmosHandlerKind.BatchTransaction;
}

export function isBatchMessageHandlerProcessor<E>(
hp: SecondLayerHandlerProcessorArray<CosmosHandlerKind, DefaultFilter, unknown>
): hp is SecondLayerHandlerProcessor<CosmosHandlerKind.BatchMessage, DefaultFilter, E> {
return hp.baseHandlerKind === CosmosHandlerKind.BatchMessage;
}

export function isCosmosTemplates(
templatesData: any,
specVersion: string
Expand Down
11 changes: 7 additions & 4 deletions packages/node/src/indexer/dictionary/v1/dictionaryV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ function getBaseHandlerFilters<T extends CosmosHandlerFilter>(
}
}

// Adding batches move complexity over 22 and the eslint rule is 20
// eslint-disable-next-line complexity
function buildDictionaryQueryEntries(
dataSources: CosmosDatasource[],
getDsProcessor: GetDsProcessor,
Expand Down Expand Up @@ -132,13 +134,15 @@ function buildDictionaryQueryEntries(
if (!filterList.length) return [];
switch (baseHandlerKind) {
case CosmosHandlerKind.Block:
case CosmosHandlerKind.PostIndex:
for (const filter of filterList as CosmosBlockFilter[]) {
if (filter.modulo === undefined) {
return [];
}
}
break;
case CosmosHandlerKind.Message: {
case CosmosHandlerKind.Message:
case CosmosHandlerKind.BatchMessage:
for (const filter of filterList as CosmosMessageFilter[]) {
if (filter.type !== undefined) {
queryEntries.push(messageFilterToQueryEntry(filter));
Expand All @@ -147,8 +151,8 @@ function buildDictionaryQueryEntries(
}
}
break;
}
case CosmosHandlerKind.Event: {
case CosmosHandlerKind.Event:
case CosmosHandlerKind.BatchEvent:
for (const filter of filterList as CosmosEventFilter[]) {
if (filter.type !== undefined) {
queryEntries.push(eventFilterToQueryEntry(filter));
Expand All @@ -157,7 +161,6 @@ function buildDictionaryQueryEntries(
}
}
break;
}
default:
}
}
Expand Down
8 changes: 6 additions & 2 deletions packages/node/src/indexer/dynamic-ds.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
DynamicDsService as BaseDynamicDsService,
} from '@subql/node-core';
import { CosmosDatasource, CosmosHandlerKind } from '@subql/types-cosmos';
import { plainToClass, ClassConstructor } from 'class-transformer';
import { validateSync, IsOptional, IsObject } from 'class-validator';
import { ClassConstructor, plainToClass } from 'class-transformer';
import { IsObject, IsOptional, validateSync } from 'class-validator';
import { SubqueryProject } from '../configure/SubqueryProject';
import { DsProcessorService } from './ds-processor.service';

Expand Down Expand Up @@ -83,6 +83,7 @@ export class DynamicDsService extends BaseDynamicDsService<
dsObj.mapping.handlers = dsObj.mapping.handlers.map((handler) => {
switch (handler.kind) {
case CosmosHandlerKind.Event:
case CosmosHandlerKind.BatchEvent:
assert(
handler.filter,
'Dynamic datasources must have some predfined filter',
Expand All @@ -106,6 +107,7 @@ export class DynamicDsService extends BaseDynamicDsService<
}
return handler;
case CosmosHandlerKind.Message:
case CosmosHandlerKind.BatchMessage:
assert(
handler.filter,
'Dynamic datasources must have some predfined filter',
Expand All @@ -123,7 +125,9 @@ export class DynamicDsService extends BaseDynamicDsService<
}
return handler;
case CosmosHandlerKind.Transaction:
case CosmosHandlerKind.BatchTransaction:
case CosmosHandlerKind.Block:
case CosmosHandlerKind.PostIndex:
default:
return handler;
}
Expand Down
Loading