Skip to content

Commit

Permalink
add fields validation in get_logs rest endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Aug 14, 2024
1 parent 45eb5d5 commit b569c27
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
43 changes: 43 additions & 0 deletions packages/engine/paima-rest/src/controllers/BasicControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,49 @@ export class GetLogsController extends Controller {
@Post()
public async post(@Body() params: GetLogsParams): Promise<GetLogsResponse> {
const gameStateMachine = EngineService.INSTANCE.getSM();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const eventDefinition = ((): any => {
const appEvents = gameStateMachine.getAppEvents();

for (const defs of Object.values(appEvents)) {
for (const def of defs) {
if (def.topicHash === params.topic) {
return def;
}
}
}

return undefined;
})();

if (!eventDefinition) {
this.setStatus(StatusCodes.NOT_FOUND);
return {
success: false,
errorMessage: 'Topic not found',
};
}

if (params.filters) {
const indexedFields = new Set();
for (const field of eventDefinition.definition.fields) {
if (field.indexed) {
indexedFields.add(field.name);
}
}

for (const fieldName of Object.keys(params.filters)) {
if (!indexedFields.has(fieldName)) {
this.setStatus(StatusCodes.NOT_FOUND);
return {
success: false,
errorMessage: `Field is not indexed: ${fieldName}`,
};
}
}
}

try {
const DBConn = gameStateMachine.getReadonlyDbConn();
const base: Record<string, number | string | undefined> & { topic: string } = {
Expand Down
3 changes: 3 additions & 0 deletions packages/engine/paima-sm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ const SM: GameStateMachineInitializer = {
await blockHeightDone.run({ block_height: latestChainData.blockNumber }, dbTx);
return processedCount;
},
getAppEvents(): ReturnType<typeof generateAppEvents> {
return events;
},
};
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/engine/paima-sm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,4 +695,5 @@ export interface GameStateMachine {
presyncProcess: (dbTx: PoolClient, latestCdeData: PresyncChainData) => Promise<void>;
markPresyncMilestone: (blockHeight: number, network: string) => Promise<void>;
dryRun: (gameInput: string, userAddress: string) => Promise<boolean>;
getAppEvents: () => ReturnType<typeof generateAppEvents>;
}

0 comments on commit b569c27

Please sign in to comment.