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

enable ts strict in node #2450

Merged
merged 30 commits into from
Jul 2, 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
2 changes: 2 additions & 0 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Enable ts strict model
## [10.10.0] - 2024-07-01
### Changed
- Bump version with `@subql/common`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export abstract class BaseBlockDispatcher<Q extends IQueue, DS, B> implements IB
protected _processedBlockCount = 0;
protected _latestProcessedHeight = 0;
protected currentProcessingHeight = 0;
private _onDynamicDsCreated?: (height: number) => Promise<void>;
private _onDynamicDsCreated?: (height: number) => void;
private _pendingRewindHeight?: number;

protected smartBatchService: SmartBatchService;
Expand All @@ -72,7 +72,7 @@ export abstract class BaseBlockDispatcher<Q extends IQueue, DS, B> implements IB

abstract enqueueBlocks(heights: (IBlock<B> | number)[], latestBufferHeight?: number): void | Promise<void>;

async init(onDynamicDsCreated: (height: number) => Promise<void>): Promise<void> {
async init(onDynamicDsCreated: (height: number) => void): Promise<void> {
this._onDynamicDsCreated = onDynamicDsCreated;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.setProcessedBlockCount((await this.storeCacheService.metadata.find('processedBlockCount', 0))!);
Expand Down Expand Up @@ -103,7 +103,7 @@ export abstract class BaseBlockDispatcher<Q extends IQueue, DS, B> implements IB
this._latestProcessedHeight = height;
}

protected get onDynamicDsCreated(): (height: number) => Promise<void> {
protected get onDynamicDsCreated(): (height: number) => void {
if (!this._onDynamicDsCreated) {
throw new Error('BaseBlockDispatcher has not been initialized');
}
Expand Down Expand Up @@ -204,7 +204,7 @@ export abstract class BaseBlockDispatcher<Q extends IQueue, DS, B> implements IB
this.createPOI(height, blockHash, operationHash);

if (dynamicDsCreated) {
await this.onDynamicDsCreated(height);
this.onDynamicDsCreated(height);
}
assert(
!this.latestProcessedHeight || height > this.latestProcessedHeight,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export abstract class WorkerBlockDispatcher<DS, W extends Worker, B>
this.numWorkers = nodeConfig.workers!;
}

async init(onDynamicDsCreated: (height: number) => Promise<void>): Promise<void> {
async init(onDynamicDsCreated: (height: number) => void): Promise<void> {
this.workers = await Promise.all(new Array(this.numWorkers).fill(0).map(() => this.createIndexerWorker()));
return super.init(onDynamicDsCreated);
}
Expand Down
9 changes: 6 additions & 3 deletions packages/node-core/src/indexer/connectionPool.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export class ConnectionPoolService<T extends IApiConnectionSpecific<any, any, an
private cacheSizeThreshold = 10;
private cacheFlushInterval = 60 * 100;

constructor(private nodeConfig: NodeConfig, private poolStateManager: ConnectionPoolStateManager<T>) {
constructor(
private nodeConfig: NodeConfig,
private poolStateManager: ConnectionPoolStateManager<T>
) {
this.cacheSizeThreshold = this.nodeConfig.batchSize;
}

Expand Down Expand Up @@ -112,8 +115,8 @@ export class ConnectionPoolService<T extends IApiConnectionSpecific<any, any, an
await this.handleApiSuccess(endpoint, end - start);
await this.poolStateManager.setFieldValue(endpoint, 'lastRequestTime', end); // Update the last request time
return result;
} catch (error) {
await this.handleApiError(endpoint, target.handleError(error as Error));
} catch (error: any) {
await this.handleApiError(endpoint, target.handleError(error));
throw error;
}
};
Expand Down
8 changes: 6 additions & 2 deletions packages/node-core/src/indexer/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export class Sandbox extends NodeVM {
private entry: string;
private sourceMap: any | undefined;

constructor(option: SandboxOption, protected readonly script: VMScript, protected config: NodeConfig) {
constructor(
option: SandboxOption,
protected readonly script: VMScript,
protected config: NodeConfig
) {
super(
merge(DEFAULT_OPTION(config.unsafe), {
require: {
Expand Down Expand Up @@ -155,7 +159,7 @@ export class IndexerSandbox extends Sandbox {
try {
await this.runTimeout(this.config.timeout);
} catch (e: any) {
const newStack = await this.convertStack((e as Error).stack);
const newStack = await this.convertStack(e.stack);
e.stack = newStack;
e.handler = funcName;
if (this.config.logLevel && levelFilter('debug', this.config.logLevel)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/node-core/src/utils/fetchHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ export async function retryOnFail<T>(
): Promise<T> {
try {
return await request();
} catch (e) {
} catch (e: any) {
if (!shouldRetry(e)) throw e;
if (retries > 1) {
await delay(RETRY_DELAY);
return retryOnFail(request, shouldRetry, --retries);
} else {
logger.error(e as Error, `Retries failed after ${RETRY_COUNT}`);
logger.error(e, `Retries failed after ${RETRY_COUNT}`);
throw e;
}
}
Expand Down
26 changes: 18 additions & 8 deletions packages/node-core/src/utils/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
import {DEFAULT_PORT, findAvailablePort, GithubReader, IPFSReader, LocalReader} from '@subql/common';
import {BaseAssetsDataSource, BaseCustomDataSource, BaseDataSource, Reader, TemplateBase} from '@subql/types-core';
import {
BaseAssetsDataSource,
BaseCustomDataSource,
BaseDataSource,
BaseTemplateDataSource,
Reader,
TemplateBase,
} from '@subql/types-core';
import {getAllEntitiesRelations} from '@subql/utils';
import {QueryTypes, Sequelize} from '@subql/x-sequelize';
import {stringToArray, getSchedule} from 'cron-converter';
Expand All @@ -17,7 +24,7 @@ import {exitWithError} from '../process';

const logger = getLogger('Project-Utils');

export async function getValidPort(argvPort: number): Promise<number> {
export async function getValidPort(argvPort?: number): Promise<number> {
const validate = (x: any) => {
const p = parseInt(x);
return isNaN(p) ? null : p;
Expand Down Expand Up @@ -229,13 +236,13 @@ export async function initHotSchemaReload(schema: string, storeService: StoreSer
await storeService.initHotSchemaReloadQueries(schema);
}

type IsRuntimeDs = (ds: BaseDataSource) => boolean;
type IsRuntimeDs<DS> = (ds: DS) => ds is DS;

// eslint-disable-next-line @typescript-eslint/require-await
export async function insertBlockFiltersCronSchedules<DS extends BaseDataSource = BaseDataSource>(
dataSources: DS[],
getBlockTimestamp: (height: number) => Promise<Date>,
isRuntimeDs: IsRuntimeDs,
isRuntimeDs: IsRuntimeDs<DS>,
blockHandlerKind: string
): Promise<DS[]> {
dataSources = await Promise.all(
Expand Down Expand Up @@ -278,20 +285,23 @@ export async function insertBlockFiltersCronSchedules<DS extends BaseDataSource
return dataSources;
}

export async function loadProjectTemplates<T extends BaseDataSource & TemplateBase>(
export async function loadProjectTemplates<T extends BaseTemplateDataSource>(
templates: T[] | undefined,
root: string,
reader: Reader,
isCustomDs: IsCustomDs<BaseDataSource, BaseCustomDataSource>
isCustomDs: IsCustomDs<T, Omit<T & BaseCustomDataSource, keyof TemplateBase>>
): Promise<T[]> {
if (!templates || !templates.length) {
return [];
}
const dsTemplates = await updateDataSourcesV1_0_0(templates, reader, root, isCustomDs);

const templateIsCustomDs = (template: T): template is T & BaseCustomDataSource =>
isCustomDs(template) && 'name' in template;
const dsTemplates = await updateDataSourcesV1_0_0(templates, reader, root, templateIsCustomDs);
return dsTemplates.map((ds, index) => ({
...ds,
name: templates[index].name,
})) as T[]; // How to get rid of cast here?
}));
}

export function getStartHeight(dataSources: BaseDataSource[]): number {
Expand Down
2 changes: 2 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Enable ts strict model
## [4.7.0] - 2024-07-01
### Changed
- Update with `@subql/node-core`, `@subql/common-substrate`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('SchemaMigration integration tests', () => {
`SELECT table_name FROM information_schema.tables WHERE table_schema= :schema;`,
{ type: QueryTypes.SELECT, replacements: { schema: schemaName } },
);
const tableNames: string[] = dbResults.map((row: string[]) => {
const tableNames: string[] = dbResults.map((row) => {
return row[0];
});

Expand Down
12 changes: 4 additions & 8 deletions packages/node/src/configure/SubqueryProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,10 @@ async function loadProjectFromManifestBase(
),
);

let schemaString: string;
try {
schemaString = await reader.getFile(projectManifest.schema.file);
} catch (e) {
throw new Error(
`unable to fetch the schema from ${projectManifest.schema.file}`,
);
}
const schemaString: string = await reader.getFile(
projectManifest.schema.file,
);
assert(schemaString, 'Schema file is empty');
const schema = buildSchemaFromString(schemaString);

const chainTypes = projectManifest.network.chaintypes
Expand Down
19 changes: 9 additions & 10 deletions packages/node/src/indexer/api.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('ApiService', () => {
const blockhash = await api.rpc.chain.getBlockHash(2);
const validators = await api.query.session.validators.at(blockhash);
const block = await api.rpc.chain.getBlock(blockhash);
const mockBlock = wrapBlock(block, []) as unknown as SubstrateBlock;
const mockBlock = wrapBlock(block, [], 1) as unknown as SubstrateBlock;
const runtimeVersion = { specVersion: 1 } as unknown as RuntimeVersion;
const patchedApi = await apiService.getPatchedApi(
mockBlock.block.header,
Expand All @@ -120,7 +120,7 @@ describe('ApiService', () => {
const api = apiService.api;
const blockhash = await api.rpc.chain.getBlockHash(6721189);
const block = await api.rpc.chain.getBlock(blockhash);
const mockBlock = wrapBlock(block, []) as unknown as SubstrateBlock;
const mockBlock = wrapBlock(block, [], 13) as unknown as SubstrateBlock;
const runtimeVersion = { specVersion: 13 } as unknown as RuntimeVersion;
const patchedApi = await apiService.getPatchedApi(
mockBlock.block.header,
Expand All @@ -147,7 +147,7 @@ describe('ApiService', () => {
const api = apiService.api;
const blockhash = await api.rpc.chain.getBlockHash(6721195);
const block = await api.rpc.chain.getBlock(blockhash);
const mockBlock = wrapBlock(block, []) as unknown as SubstrateBlock;
const mockBlock = wrapBlock(block, [], 9090) as unknown as SubstrateBlock;
const runtimeVersion = { specVersion: 9090 } as unknown as RuntimeVersion;
// step 1, get early block, original polkadot api query result
const earlyBlockhash = await api.rpc.chain.getBlockHash(5661443);
Expand All @@ -157,9 +157,8 @@ describe('ApiService', () => {
mockBlock.block.header,
runtimeVersion,
);
const patchedResult = await patchedApi.rpc.state.getRuntimeVersion(
earlyBlockhash,
);
const patchedResult =
await patchedApi.rpc.state.getRuntimeVersion(earlyBlockhash);
expect(apiResults).toEqual(patchedResult);
// patchedApi without input blockHash, will return runtimeVersion at 6721195
const patchedResult2 = await patchedApi.rpc.state.getRuntimeVersion();
Expand All @@ -177,7 +176,7 @@ describe('ApiService', () => {
const api = apiService.api;
const blockhash = await api.rpc.chain.getBlockHash(5661443);
const block = await api.rpc.chain.getBlock(blockhash);
const mockBlock = wrapBlock(block, []) as unknown as SubstrateBlock;
const mockBlock = wrapBlock(block, [], 9050) as unknown as SubstrateBlock;
const runtimeVersion = { specVersion: 9050 } as unknown as RuntimeVersion;
// step 1, get future block, original polkadot api query result
const futureBlockhash = await api.rpc.chain.getBlockHash(6721195);
Expand Down Expand Up @@ -209,7 +208,7 @@ describe('ApiService', () => {
}

const block = await api.rpc.chain.getBlock(blockhash);
const mockBlock = wrapBlock(block, []) as unknown as SubstrateBlock;
const mockBlock = wrapBlock(block, [], 28) as unknown as SubstrateBlock;
const runtimeVersion = { specVersion: 28 } as unknown as RuntimeVersion;
const patchedApi = await apiService.getPatchedApi(
mockBlock.block.header,
Expand Down Expand Up @@ -383,7 +382,7 @@ describe('ApiService', () => {
const api = apiService.api;
const blockhash = await api.rpc.chain.getBlockHash(1);
const block = await api.rpc.chain.getBlock(blockhash);
const mockBlock = wrapBlock(block, []) as unknown as SubstrateBlock;
const mockBlock = wrapBlock(block, [], 1) as unknown as SubstrateBlock;
const runtimeVersion = { specVersion: 1 } as unknown as RuntimeVersion;

const patchedApi = await apiService.getPatchedApi(
Expand All @@ -404,7 +403,7 @@ describe('ApiService', () => {
const blockNumber = 1545235;
const blockhash = await api.rpc.chain.getBlockHash(blockNumber);
const block = await api.rpc.chain.getBlock(blockhash);
const mockBlock = wrapBlock(block, []) as unknown as SubstrateBlock;
const mockBlock = wrapBlock(block, [], 1103) as unknown as SubstrateBlock;
const runtimeVersion = { specVersion: 1103 } as unknown as RuntimeVersion;

const patchedApi = await apiService.getPatchedApi(
Expand Down
Loading
Loading