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 for chainType validator #175

Merged
merged 16 commits into from
Oct 4, 2023
Merged
2 changes: 2 additions & 0 deletions packages/common-cosmos/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
- Fixed Missing chainTypes on deployment (#175)

## [2.5.1] - 2023-09-20
### Changed
Expand Down
4 changes: 2 additions & 2 deletions packages/common-cosmos/src/codegen/codegen-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import telescope from '@cosmology/telescope';
import cosmwasmCodegen from '@cosmwasm/ts-codegen';
import {makeTempDir} from '@subql/common';
import {CustomModule, SubqlCosmosRuntimeDatasource} from '@subql/types-cosmos';
import {CosmosChainTypes, CustomModule, SubqlCosmosRuntimeDatasource} from '@subql/types-cosmos';
import {Data} from 'ejs';
import {copySync} from 'fs-extra';
import {IDLObject} from 'wasm-ast-types';
Expand All @@ -29,7 +29,7 @@
messageNames: string[]; // all messages
path: string; // should process the file Path and concat with PROTO dir
}
type CosmosChainTypeDataType = Map<string, CustomModule> | Record<string, CustomModule>;
type CosmosChainTypeDataType = CosmosChainTypes | Record<string, CustomModule>;

interface CosmwasmRenderJobType {
contract: string;
Expand Down Expand Up @@ -223,7 +223,7 @@
}
);
console.log('* Cosmos message wrappers generated !');
} catch (e: any) {

Check warning on line 226 in packages/common-cosmos/src/codegen/codegen-controller.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
const errorMessage = e.message.startsWith('Dependency')
? `Please add the missing protobuf file to ./proto directory`
: '';
Expand Down
6 changes: 0 additions & 6 deletions packages/common-cosmos/src/project/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
case SubqlCosmosHandlerKind.Block:
return plainToClass(CosmosBlockHandler, handler);
default:
throw new Error(`handler ${(handler as any).kind} not supported`);

Check warning on line 143 in packages/common-cosmos/src/project/models.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
}
});
})
Expand Down Expand Up @@ -176,9 +176,6 @@
mapping: M;
@IsInt()
startBlock: number;
@Type(() => CosmosCustomModuleImpl)
@ValidateNested({each: true})
chainTypes: Map<string, CustomModule>;
@IsOptional()
@Validate(FileReferenceImp)
assets?: Map<string, FileReference>;
Expand All @@ -204,7 +201,7 @@
export class CosmosCustomDataSourceBase<
K extends string,
M extends SubqlCosmosMapping = SubqlCosmosMapping<SubqlCosmosCustomHandler>,
O = any

Check warning on line 204 in packages/common-cosmos/src/project/models.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
> implements SubqlCosmosCustomDatasource<K, M, O>
{
@IsString()
Expand All @@ -221,7 +218,4 @@
@Type(() => ProcessorImpl)
@IsObject()
processor: Processor<O>;
@Type(() => CosmosCustomModuleImpl)
@ValidateNested({each: true})
chainTypes: Map<string, CustomModule>;
}
12 changes: 12 additions & 0 deletions packages/common-cosmos/src/project/project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import path from 'path';
import {getManifestPath, loadFromJsonOrYaml} from '@subql/common';
import {validateCosmosManifest} from '../codegen/util';
import {parseCosmosProjectManifest} from './load';
import {CosmosProjectManifestVersioned, VersionedProjectManifest} from './versioned';

const projectsDir = path.join(__dirname, '../../test');
Expand Down Expand Up @@ -34,9 +35,20 @@
).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');
});
it('Ensure chainTypes existence on manifest deployment', () => {
const cosmosManifest = loadFromJsonOrYaml(path.join(projectsDir, './protoTest1', 'project.yaml')) as any;

Check warning on line 50 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);
});
});
1 change: 0 additions & 1 deletion packages/common-cosmos/src/project/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export {
SubqlCosmosHandler,
SubqlCosmosHandlerKind,
SubqlCosmosDatasource as SubqlCosmosDataSource,
SubqlCosmosCustomDatasource as SubqlCosmosCustomDataSource,
SubqlCosmosBlockFilter,
SubqlCosmosMessageFilter,
SubqlCosmosEventFilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: GPL-3.0

import {SubqlCosmosDatasource} from '@subql/types-cosmos';
import {plainToClass} from 'class-transformer';
import {plainToInstance} from 'class-transformer';
import {ICosmosProjectManifest} from '../types';
import {ProjectManifestV1_0_0Impl} from './v1_0_0';

Expand All @@ -28,7 +28,7 @@ export class CosmosProjectManifestVersioned implements ICosmosProjectManifest {
if (!klass) {
throw new Error('specVersion not supported for project manifest file');
}
this._impl = plainToClass<ProjectManifestImpls, VersionedProjectManifest>(klass, projectManifest);
this._impl = plainToInstance<ProjectManifestImpls, VersionedProjectManifest>(klass, projectManifest);
}

get asImpl(): ProjectManifestImpls {
Expand Down
21 changes: 8 additions & 13 deletions packages/common-cosmos/src/project/versioned/v1_0_0/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from '@subql/common';
import {NodeSpec, ParentProject, QuerySpec, RunnerSpecs} from '@subql/types-core';
import {
CustomModule,
SubqlCosmosCustomDatasource,
SubqlCosmosCustomHandler,
SubqlCosmosMapping,
Expand All @@ -22,18 +21,10 @@ import {
RuntimeDatasourceTemplate,
CustomDatasourceTemplate,
CosmosProjectManifestV1_0_0,
CosmosChainTypes,
} from '@subql/types-cosmos';
import {Transform, TransformFnParams, Type} from 'class-transformer';
import {
Equals,
IsObject,
IsString,
ValidateNested,
validateSync,
IsOptional,
IsArray,
IsNotEmpty,
} from 'class-validator';
import {Equals, IsObject, IsString, ValidateNested, IsOptional, IsArray, IsNotEmpty} from 'class-validator';
import {CosmosCustomDataSourceBase, CosmosCustomModuleImpl, CosmosRuntimeDataSourceBase} from '../../models';

const COSMOS_NODE_NAME = `@subql/node-cosmos`;
Expand Down Expand Up @@ -72,13 +63,17 @@ export class CosmosProjectNetworkDeployment {
@IsOptional()
@IsArray()
bypassBlocks?: (number | string)[];
@IsOptional()
@Type(() => CosmosCustomModuleImpl)
@ValidateNested({each: true})
chainTypes?: Map<string, CosmosCustomModuleImpl>;
}

export class CosmosProjectNetwork extends CommonProjectNetworkV1_0_0<FileType> {
export class CosmosProjectNetwork extends CommonProjectNetworkV1_0_0<CosmosChainTypes> {
@Type(() => CosmosCustomModuleImpl)
@IsOptional()
@ValidateNested({each: true})
chainTypes?: Map<string, CustomModule>;
chainTypes?: Map<string, CosmosCustomModuleImpl>;
}

export class RuntimeDatasourceTemplateImpl extends CosmosRuntimeDataSourceImpl implements RuntimeDatasourceTemplate {
Expand Down
40 changes: 40 additions & 0 deletions packages/common-cosmos/test/protoTest1/bad-chaintypes-project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
specVersion: 1.0.0
name: test
version: 0.0.1
runner:
node:
name: '@subql/node-cosmos'
version: '*'
query:
name: '@subql/query'
version: '*'
description: ''
repository: ''
schema:
file: ./schema.graphql
network:
chainId: osmosis-1
endpoint:
- https://osmosis.api.onfinality.io/public
dictionary: 'https://api.subquery.network/sq/subquery/cosmos-osmosis-dictionary'
chainTypes:
osmosis.gamm.v1beta1:
filePath: './proto/osmosis/gamm/v1beta1/tx.proto'
message:
- MsgSwapExactAmountIn
osmosis.poolmanager.v1beta1:
# needed by MsgSwapExactAmountIn
file: './proto/osmosis/poolmanager/v1beta1/swap_route.proto'
messages:
- SwapAmountInRoute

dataSources:
- kind: cosmos/Runtime
startBlock: 9798050
mapping:
file: ./dist/index.js
handlers:
- handler: handleMessage
kind: cosmos/MessageHandler
filter:
type: /osmosis.gamm.v1beta1.MsgSwapExactAmountIn
8 changes: 6 additions & 2 deletions packages/types/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export type RuntimeDatasourceTemplate = BaseTemplateDataSource<SubqlCosmosDataso
export type CustomDatasourceTemplate = BaseTemplateDataSource<SubqlCosmosCustomDatasource>;

export type CosmosProjectManifestV1_0_0 = ProjectManifestV1_0_0<
SubqlCosmosRuntimeDatasource | SubqlCosmosCustomDatasource
SubqlCosmosRuntimeDatasource | SubqlCosmosCustomDatasource,
RuntimeDatasourceTemplate | CustomDatasourceTemplate,
CosmosNetworkConfig
>;

export interface CustomModule extends FileReference {
Expand All @@ -35,6 +37,8 @@ export interface CustomModule extends FileReference {
messages: string[];
}

export type CosmosChainTypes = Map<string, CustomModule>;

export type CustomDataSourceAsset = FileReference;

export enum SubqlCosmosDatasourceKind {
Expand Down Expand Up @@ -101,7 +105,7 @@ export type CosmosNetworkConfig = IProjectNetworkConfig & {
}
}
* */
chainTypes?: Map<string, CustomModule>;
chainTypes?: CosmosChainTypes;
};

export type SubqlCosmosBlockFilter = BlockFilter;
Expand Down
11 changes: 10 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4221,7 +4221,7 @@ __metadata:
languageName: node
linkType: hard

"@subql/types-core@npm:0.1.0, @subql/types-core@npm:^0.1.0":
"@subql/types-core@npm:0.1.0":
version: 0.1.0
resolution: "@subql/types-core@npm:0.1.0"
dependencies:
Expand All @@ -4239,6 +4239,15 @@ __metadata:
languageName: node
linkType: hard

"@subql/types-core@npm:^0.1.0":
version: 0.1.1
resolution: "@subql/types-core@npm:0.1.1"
dependencies:
package-json-type: ^1.0.3
checksum: 6a88547e5091795d2f9f24b5373ce531066bc04602b18f05cad77c5d1953523be479801447cd85e4f03468997e988b8c853fdb409631f8a396525cc723978afa
languageName: node
linkType: hard

"@subql/types-cosmos@workspace:*, @subql/types-cosmos@workspace:packages/types":
version: 0.0.0-use.local
resolution: "@subql/types-cosmos@workspace:packages/types"
Expand Down
Loading