Skip to content

Commit

Permalink
Update new manifest version from 0.0.2 to 0.2.0, address other minor …
Browse files Browse the repository at this point in the history
…comments
  • Loading branch information
stwiname committed Sep 27, 2021
1 parent 1a58330 commit b3d5efc
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 86 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/controller/codegen-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface ProcessedField {
isJsonInterface: boolean;
}

export async function generateJsonInterfaces(projectPath: string, schema: string) {
export async function generateJsonInterfaces(projectPath: string, schema: string): Promise<void> {
const typesDir = path.join(projectPath, TYPE_ROOT_DIR);
const jsonObjects = getAllJsonObjects(schema);
const jsonInterfaces = jsonObjects.map((r) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/controller/init-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import fs from 'fs';
import * as path from 'path';
import {promisify} from 'util';
import {ProjectManifestV0_0_2 as ProjectManifest} from '@subql/common';
import {ProjectManifestV0_2_0 as ProjectManifest} from '@subql/common';
import yaml from 'js-yaml';
import rimraf from 'rimraf';
import simpleGit from 'simple-git';
Expand Down
16 changes: 6 additions & 10 deletions packages/common/src/project/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@ import path from 'path';
import {plainToClass} from 'class-transformer';
import {validateSync} from 'class-validator';
import yaml from 'js-yaml';
import parseJson from 'parse-json';
import {ChainTypes} from './models';
import {ProjectManifestVersioned, VersionedProjectManifest} from './versioned';

export function loadFromJsonOrYaml(file: string): unknown {
const fileInfo = path.parse(file);
const {ext} = path.parse(file);

const rawContent = fs.readFileSync(file, 'utf-8');

if (fileInfo.ext === '.json') {
return parseJson(rawContent, file);
} else if (fileInfo.ext === '.yaml' || fileInfo.ext === '.yml') {
return yaml.load(rawContent);
} else {
throw new Error(`Extension ${fileInfo.ext} not supported`);
if (ext !== '.yaml' && ext !== '.yml' && ext !== '.json') {
throw new Error(`Extension ${ext} not supported`);
}

const rawContent = fs.readFileSync(file, 'utf-8');
return yaml.load(rawContent);
}

function loadFromFile(file: string): unknown {
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/project/project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('project.yaml', () => {
expect(() => loadProjectManifest(path.join(projectsDir, 'project_falsy_array.yaml'))).toThrow();
});

it('can validate a v0.0.2 project.yaml', () => {
expect(() => loadProjectManifest(path.join(projectsDir, 'project_0.0.2.yaml'))).toBeTruthy();
it('can validate a v0.2.0 project.yaml', () => {
expect(() => loadProjectManifest(path.join(projectsDir, 'project_0.2.0.yaml'))).toBeTruthy();
});

it('can fail validation if version not supported', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {plainToClass} from 'class-transformer';
import {validateSync} from 'class-validator';
import {IProjectManifest, SubqlDataSource} from '../types';
import {ProjectManifestV0_0_1Impl} from './v0_0_1';
import {ProjectManifestV0_0_2Impl} from './v0_0_2';
import {ProjectManifestV0_2_0Impl} from './v0_2_0';

export type VersionedProjectManifest = {specVersion: string};

const SUPPORTED_VERSIONS = {
'0.0.1': ProjectManifestV0_0_1Impl,
'0.0.2': ProjectManifestV0_0_2Impl,
'0.2.0': ProjectManifestV0_2_0Impl,
};

type Versions = keyof typeof SUPPORTED_VERSIONS;
Expand All @@ -22,8 +22,8 @@ export function manifestIsV0_0_1(manifest: IProjectManifest): manifest is Projec
return manifest.specVersion === '0.0.1';
}

export function manifestIsV0_0_2(manifest: IProjectManifest): manifest is ProjectManifestV0_0_2Impl {
return manifest.specVersion === '0.0.2';
export function manifestIsV0_2_0(manifest: IProjectManifest): manifest is ProjectManifestV0_2_0Impl {
return manifest.specVersion === '0.2.0';
}

export class ProjectManifestVersioned implements IProjectManifest {
Expand All @@ -49,12 +49,12 @@ export class ProjectManifestVersioned implements IProjectManifest {
return this._impl as ProjectManifestV0_0_1Impl;
}

get isV0_0_2(): boolean {
return this.specVersion === '0.0.2';
get isV0_2_0(): boolean {
return this.specVersion === '0.2.0';
}

get asV0_0_2(): ProjectManifestV0_0_2Impl {
return this._impl as ProjectManifestV0_0_2Impl;
get asV0_2_0(): ProjectManifestV0_2_0Impl {
return this._impl as ProjectManifestV0_2_0Impl;
}

validate(): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/project/versioned/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

export * from './ProjectManifestVersioned';
export * from './v0_0_1';
export * from './v0_0_2';
export * from './v0_2_0';
31 changes: 0 additions & 31 deletions packages/common/src/project/versioned/v0_0_2/types.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {Type} from 'class-transformer';
import {Equals, IsArray, IsObject, IsOptional, IsString, ValidateNested} from 'class-validator';
import {Mapping, RuntimeDataSourceBase} from '../../models';
import {ProjectManifestBaseImpl} from '../base';
import {ProjectManifestV0_0_2, RuntimeDataSourceV0_0_2, SubqlMappingV0_0_2} from './types';
import {ProjectManifestV0_2_0, RuntimeDataSourceV0_2_0, SubqlMappingV0_2_0} from './types';

export class FileType {
@IsString()
file: string;
}

export class ProjectNetworkV0_0_2 {
export class ProjectNetworkV0_2_0 {
@IsString()
genesisHash: string;
@IsObject()
Expand All @@ -22,36 +22,36 @@ export class ProjectNetworkV0_0_2 {
chaintypes: FileType;
}

export class ProjectMappingV0_0_2 extends Mapping {
export class ProjectMappingV0_2_0 extends Mapping {
@IsString()
file: string;
}

export class RuntimeDataSourceV0_0_2Impl
extends RuntimeDataSourceBase<SubqlMappingV0_0_2>
implements RuntimeDataSourceV0_0_2
export class RuntimeDataSourceV0_2_0Impl
extends RuntimeDataSourceBase<SubqlMappingV0_2_0>
implements RuntimeDataSourceV0_2_0
{
@Type(() => ProjectMappingV0_0_2)
@Type(() => ProjectMappingV0_2_0)
@ValidateNested()
mapping: SubqlMappingV0_0_2;
mapping: SubqlMappingV0_2_0;
}

export class ProjectManifestV0_0_2Impl extends ProjectManifestBaseImpl implements ProjectManifestV0_0_2 {
@Equals('0.0.2')
export class ProjectManifestV0_2_0Impl extends ProjectManifestBaseImpl implements ProjectManifestV0_2_0 {
@Equals('0.2.0')
specVersion: string;
@IsString()
name: string;
@IsString()
version: string;
@IsObject()
@ValidateNested()
@Type(() => ProjectNetworkV0_0_2)
network: ProjectNetworkV0_0_2;
@Type(() => ProjectNetworkV0_2_0)
network: ProjectNetworkV0_2_0;
@ValidateNested()
@Type(() => FileType)
schema: FileType;
@IsArray()
@ValidateNested()
@Type(() => RuntimeDataSourceV0_0_2Impl)
dataSources: RuntimeDataSourceV0_0_2[];
@Type(() => RuntimeDataSourceV0_2_0Impl)
dataSources: RuntimeDataSourceV0_2_0[];
}
31 changes: 31 additions & 0 deletions packages/common/src/project/versioned/v0_2_0/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2020-2021 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import {IProjectManifest, SubqlDataSource, SubqlMapping} from '../../types';

export interface SubqlMappingV0_2_0 extends SubqlMapping {
file: string;
}

export type RuntimeDataSourceV0_2_0 = SubqlDataSource<SubqlMappingV0_2_0>;

export interface ProjectManifestV0_2_0 extends IProjectManifest {
name: string;
version: string;
schema: {
file: string;
};

network: {
genesisHash: string;
chaintypes?: {
file: string;
};
};

dataSources: RuntimeDataSourceV0_2_0[];
}

export function isRuntimeDataSourceV0_2_0(dataSource: SubqlDataSource): dataSource is RuntimeDataSourceV0_2_0 {
return !!(dataSource as RuntimeDataSourceV0_2_0).mapping.file;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# project.yaml
specVersion: "0.0.2"
specVersion: "0.2.0"
description: ""
repository: "https://github.com/subquery/subql-starter"

Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/configure/project.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ describe('SubqueryProject', () => {
});
});

describe('Manifest v0.0.2', () => {
describe('Manifest v0.2.0', () => {
beforeEach(async () => {
const projectDir = path.resolve(
__dirname,
'../../test/projectFixture/v0.0.2',
'../../test/projectFixture/v0.2.0',
);
project = await SubqueryProject.create(projectDir, {});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/node/src/configure/project.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ProjectNetworkConfig,
ProjectManifestVersioned,
manifestIsV0_0_1,
manifestIsV0_0_2,
manifestIsV0_2_0,
loadFromJsonOrYaml,
} from '@subql/common';
import { pick } from 'lodash';
Expand Down Expand Up @@ -71,7 +71,7 @@ export class SubqueryProject {
return impl.network;
}

if (manifestIsV0_0_2(impl)) {
if (manifestIsV0_2_0(impl)) {
const genesisHash = impl.network.genesisHash;

const network = this._networkRegistry[genesisHash];
Expand Down Expand Up @@ -118,7 +118,7 @@ export class SubqueryProject {
]);
}

if (manifestIsV0_0_2(impl)) {
if (manifestIsV0_2_0(impl)) {
if (!impl.network.chaintypes) {
return;
}
Expand Down
17 changes: 12 additions & 5 deletions packages/node/src/indexer/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { RpcInterface } from '@polkadot/rpc-core/types';
import { StorageKey } from '@polkadot/types';
import { BlockHash } from '@polkadot/types/interfaces';
import { AnyFunction, AnyTuple, Registry } from '@polkadot/types/types';
import { assign } from 'lodash';
import { combineLatest } from 'rxjs';
import { SubqueryProject } from '../configure/project.model';
import { IndexerEvent, NetworkMetadataPayload } from './events';
Expand All @@ -30,6 +29,7 @@ export class ApiService implements OnApplicationShutdown {
private api: ApiPromise;
private patchedApi: ApiPromise;
private currentBlockHash: BlockHash;
private apiOption: ApiOptions;
networkMeta: NetworkMetadataPayload;

constructor(
Expand All @@ -49,11 +49,11 @@ export class ApiService implements OnApplicationShutdown {
} else if (network.endpoint.startsWith('http')) {
provider = new HttpProvider(network.endpoint);
}
const apiOption: ApiOptions = {
this.apiOption = {
provider,
...chainTypes,
};
assign(apiOption, chainTypes);
this.api = await ApiPromise.create(apiOption);
this.api = await ApiPromise.create(this.apiOption);
this.networkMeta = {
chain: this.api.runtimeChain.toString(),
specName: this.api.runtimeVersion.specName.toString(),
Expand Down Expand Up @@ -92,7 +92,14 @@ export class ApiService implements OnApplicationShutdown {
if (this.patchedApi) {
return this.patchedApi;
}
const patchedApi = this.getApi().clone();

// TODO: remove once https://github.com/polkadot-js/api/pull/3949 is merged and released
const {
network: { endpoint },
} = this.project;
const patchedApi = endpoint.startsWith('ws')
? this.getApi().clone()
: new ApiPromise(this.apiOption);
Object.defineProperty(
(patchedApi as any)._rpcCore.provider,
'hasSubscriptions',
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/indexer/fetch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
import { Interval } from '@nestjs/schedule';
import { ApiPromise } from '@polkadot/api';
import {
isRuntimeDataSourceV0_0_2,
isRuntimeDataSourceV0_2_0,
RuntimeDataSrouceV0_0_1,
SubqlCallFilter,
SubqlEventFilter,
Expand Down Expand Up @@ -80,7 +80,7 @@ export class FetchService implements OnApplicationShutdown {
const extrinsicFilters: SubqlCallFilter[] = [];
const dataSources = this.project.dataSources.filter(
(ds) =>
isRuntimeDataSourceV0_0_2(ds) ||
isRuntimeDataSourceV0_2_0(ds) ||
!(ds as RuntimeDataSrouceV0_0_1).filter?.specName ||
(ds as RuntimeDataSrouceV0_0_1).filter.specName ===
this.api.runtimeVersion.specName.toString(),
Expand Down
6 changes: 3 additions & 3 deletions packages/node/src/indexer/indexer.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ApiPromise } from '@polkadot/api';
import {
buildSchema,
getAllEntitiesRelations,
isRuntimeDataSourceV0_0_2,
isRuntimeDataSourceV0_2_0,
RuntimeDataSrouceV0_0_1,
SubqlKind,
SubqlRuntimeDatasource,
Expand Down Expand Up @@ -329,14 +329,14 @@ export class IndexerManager {
const specName = this.api.runtimeVersion.specName.toString();
return this.project.dataSources.filter(
(ds) =>
isRuntimeDataSourceV0_0_2(ds) ||
isRuntimeDataSourceV0_2_0(ds) ||
!!(ds as RuntimeDataSrouceV0_0_1).filter?.specName ||
(ds as RuntimeDataSrouceV0_0_1).filter.specName === specName,
);
}

private getDataSourceEntry(dataSource: SubqlRuntimeDatasource): string {
if (isRuntimeDataSourceV0_0_2(dataSource)) {
if (isRuntimeDataSourceV0_2_0(dataSource)) {
return dataSource.mapping.file;
} else {
return getProjectEntry(this.project.path);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# project.yaml
specVersion: "0.0.2"
specVersion: "0.2.0"

name: ""
version: "0.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/validator/src/rules/require-valid-chaintypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class RequireValidChainTypes implements Rule {
async validate(ctx: Context): Promise<boolean> {
if (ctx.data.schema.isV0_0_1) return true;

const schema = ctx.data.schema.asV0_0_2;
const schema = ctx.data.schema.asV0_2_0;

// No chain types to validate
if (!schema.network.chaintypes?.file) return true;
Expand Down

0 comments on commit b3d5efc

Please sign in to comment.