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

Fix dynamic ds creating emtpy values #268

Merged
merged 5 commits into from
Jun 30, 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
4 changes: 3 additions & 1 deletion packages/common-cosmos/src/project/project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,21 @@
).toThrow('- property dataSources[0].assets has failed the following constraints: isFileReference');
});
it('Ensure correctness on Cosmos Manifest validate', () => {
const cosmosManifest = loadFromJsonOrYaml(path.join(projectsDir, './protoTest1', 'project.yaml')) as any;

Check warning on line 38 in packages/common-cosmos/src/project/project.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
const ethManifest = loadFromJsonOrYaml(path.join(projectsDir, 'project_1.0.0_bad_runner.yaml')) as any;

Check warning on line 39 in packages/common-cosmos/src/project/project.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
expect(validateCosmosManifest(cosmosManifest)).toBe(true);
expect(validateCosmosManifest(ethManifest)).toBe(false);
});
it('Validate incorrect chaintypes', () => {
const cosmosManifest = loadFromJsonOrYaml(
path.join(projectsDir, './protoTest1', 'bad-chaintypes-project.yaml')
) as any;

Check warning on line 46 in packages/common-cosmos/src/project/project.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
expect(() => parseCosmosProjectManifest(cosmosManifest)).toThrow('failed to parse project.yaml');
expect(() => parseCosmosProjectManifest(cosmosManifest)).toThrow(
'Failed to parse project. Please see below for more information'
);
});
it('Ensure chaintypes existence on manifest deployment', () => {
const cosmosManifest = loadFromJsonOrYaml(path.join(projectsDir, './protoTest1', 'project.yaml')) as any;

Check warning on line 52 in packages/common-cosmos/src/project/project.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
const manifest = parseCosmosProjectManifest(cosmosManifest);
expect(manifest.asImpl.network.chaintypes.size).toBeGreaterThan(0);
});
Expand Down
2 changes: 2 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Error creating dynamic ds if filter values are undefined (#268)

## [3.12.0] - 2024-06-21
### Added
Expand Down
125 changes: 125 additions & 0 deletions packages/node/src/indexer/dynamic-ds.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ describe('Creating dynamic ds', () => {
],
},
},
{
name: 'cosmos2',
kind: CosmosDatasourceKind.Runtime,
mapping: {
file: '',
handlers: [
{
handler: 'handleEvent',
kind: CosmosHandlerKind.Event,
filter: {
type: 'execute',
},
},
],
},
},
],
null,
null,
Expand Down Expand Up @@ -150,4 +166,113 @@ describe('Creating dynamic ds', () => {
type: '/cosmwasm.wasm.v1.MsgExecuteContract',
});
});

it('should not add empty properties to dynamic ds', async () => {
const ds = await dynamiDsService.createDynamicDatasource({
templateName: 'cosmos',
startBlock: 100,
args: {
attributes: { _contract_address: 'cosmos_wasm' },
},
});

expect(ds).toEqual({
kind: CosmosDatasourceKind.Runtime,
startBlock: 100,
mapping: {
file: '',
handlers: [
{
handler: 'handleEvent',
kind: CosmosHandlerKind.Event,
filter: {
type: 'execute',
messageFilter: {
type: '/cosmwasm.wasm.v1.MsgExecuteContract',
},
attributes: {
_contract_address: 'cosmos_wasm',
},
},
},
{
handler: 'handleMessage',
kind: CosmosHandlerKind.Message,
filter: {
type: '/cosmwasm.wasm.v1.MsgExecuteContract',
},
},
],
},
});

const ds2 = await dynamiDsService.createDynamicDatasource({
templateName: 'cosmos',
startBlock: 100,
args: {
values: { contract: 'cosmos1' },
},
});

expect(ds2).toEqual({
kind: CosmosDatasourceKind.Runtime,
startBlock: 100,
mapping: {
file: '',
handlers: [
{
handler: 'handleEvent',
kind: CosmosHandlerKind.Event,
filter: {
type: 'execute',
messageFilter: {
type: '/cosmwasm.wasm.v1.MsgExecuteContract',
values: {
contract: 'cosmos1',
},
},
},
},
{
handler: 'handleMessage',
kind: CosmosHandlerKind.Message,
filter: {
type: '/cosmwasm.wasm.v1.MsgExecuteContract',
values: {
contract: 'cosmos1',
},
},
},
],
},
});

const ds3 = await dynamiDsService.createDynamicDatasource({
templateName: 'cosmos2',
startBlock: 100,
args: {
attributes: { _contract_address: 'cosmos_wasm' },
},
});

expect(ds3).toEqual({
kind: CosmosDatasourceKind.Runtime,
startBlock: 100,
mapping: {
file: '',
handlers: [
{
handler: 'handleEvent',
kind: CosmosHandlerKind.Event,
filter: {
type: 'execute',
attributes: {
_contract_address: 'cosmos_wasm',
},
},
},
],
},
});
});
});
60 changes: 33 additions & 27 deletions packages/node/src/indexer/dynamic-ds.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import assert from 'assert';
import { Inject, Injectable } from '@nestjs/common';
import {
CosmosRuntimeDataSourceImpl,
Expand Down Expand Up @@ -82,37 +83,42 @@ export class DynamicDsService extends BaseDynamicDsService<
dsObj.mapping.handlers = dsObj.mapping.handlers.map((handler) => {
switch (handler.kind) {
case CosmosHandlerKind.Event:
if (handler.filter) {
return {
...handler,
filter: {
...handler.filter,
messageFilter: {
...handler.filter.messageFilter,
values: {
...handler.filter.messageFilter.values,
...(params.args.values as Record<string, string>),
},
},
attributes: {
...handler.filter.attributes,
...(params.args.attributes as Record<string, string>),
},
},
assert(
handler.filter,
'Dynamic datasources must have some predfined filter',
);
if (params.args.values) {
if (!handler.filter.messageFilter) {
throw new Error(
'Cannot set values on handler without predefined messageFilter type',
);
}
handler.filter.messageFilter.values = {
...handler.filter.messageFilter.values,
...(params.args.values as Record<string, string>),
};
}
if (params.args.attributes) {
handler.filter.attributes = {
...handler.filter.attributes,
...(params.args.attributes as Record<string, string>),
};
}
return handler;
case CosmosHandlerKind.Message:
if (handler.filter) {
return {
...handler,
filter: {
...handler.filter,
values: {
...handler.filter.values,
...(params.args.values as Record<string, string>),
},
},
assert(
handler.filter,
'Dynamic datasources must have some predfined filter',
);
if (params.args.values) {
if (!handler.filter) {
throw new Error(
'Cannot set values on handler without predefined messageFilter type',
);
}
handler.filter.values = {
...handler.filter.values,
...(params.args.values as Record<string, string>),
};
}
return handler;
Expand Down
Loading