From 2b678f817edb454559b238f2afb178cccbbb0a4d Mon Sep 17 00:00:00 2001 From: JQQQ Date: Mon, 11 Mar 2024 09:26:23 +1300 Subject: [PATCH] add test and queryMapValidByHeight --- .../src/indexer/dictionary/coreDictionary.ts | 3 ++- .../indexer/dictionary/dictionary.service.spec.ts | 1 + .../node-core/src/indexer/dictionary/types.ts | 6 ++++-- .../indexer/dictionary/v1/dictionaryV1.test.ts | 3 ++- .../src/indexer/dictionary/v1/dictionaryV1.ts | 6 +++++- .../indexer/dictionary/v2/dictionaryV2.test.ts | 15 ++++++++------- .../src/indexer/dictionary/v2/dictionaryV2.ts | 8 ++++++-- 7 files changed, 28 insertions(+), 14 deletions(-) diff --git a/packages/node-core/src/indexer/dictionary/coreDictionary.ts b/packages/node-core/src/indexer/dictionary/coreDictionary.ts index c7e92a5536..fae59ebdbb 100644 --- a/packages/node-core/src/indexer/dictionary/coreDictionary.ts +++ b/packages/node-core/src/indexer/dictionary/coreDictionary.ts @@ -60,7 +60,8 @@ export abstract class CoreDictionary): void { diff --git a/packages/node-core/src/indexer/dictionary/dictionary.service.spec.ts b/packages/node-core/src/indexer/dictionary/dictionary.service.spec.ts index 8085fcd00d..da0669a320 100644 --- a/packages/node-core/src/indexer/dictionary/dictionary.service.spec.ts +++ b/packages/node-core/src/indexer/dictionary/dictionary.service.spec.ts @@ -27,6 +27,7 @@ class TestDictionaryService extends DictionaryService { this.nodeConfig, this.eventEmitter ); + patchMockDictionary(mockDictionaryV2); const dictionariesV2 = [mockDictionaryV2]; this.init([...dictionariesV1, ...dictionariesV2]); diff --git a/packages/node-core/src/indexer/dictionary/types.ts b/packages/node-core/src/indexer/dictionary/types.ts index daa755c7d0..c0bdece3f0 100644 --- a/packages/node-core/src/indexer/dictionary/types.ts +++ b/packages/node-core/src/indexer/dictionary/types.ts @@ -1,6 +1,7 @@ // Copyright 2020-2024 SubQuery Pte Ltd authors & contributors // SPDX-License-Identifier: GPL-3.0 +import {FieldSelector} from '@subql/node-core/indexer'; import {BlockHeightMap} from '../../utils/blockHeightMap'; import {IBlock} from '../types'; @@ -13,8 +14,9 @@ export interface IDictionary { metadataValid: boolean | undefined; getData( startBlock: number, - queryEndBlock: number, - limit: number + endBlock: number, + limit: number, + fieldSelector?: FieldSelector ): Promise | number> | undefined>; queryMapValidByHeight(height: number): boolean; getQueryEndBlock(targetEndHeight: number, apiFinalizedHeight: number): number; diff --git a/packages/node-core/src/indexer/dictionary/v1/dictionaryV1.test.ts b/packages/node-core/src/indexer/dictionary/v1/dictionaryV1.test.ts index 269ac4ca24..855e47dd05 100644 --- a/packages/node-core/src/indexer/dictionary/v1/dictionaryV1.test.ts +++ b/packages/node-core/src/indexer/dictionary/v1/dictionaryV1.test.ts @@ -146,7 +146,8 @@ mockDS.forEach((ds, index, dataSources) => { m.set(ds.startBlock, dataSources.slice(0, index + 1)); }); -const dsMap = new BlockHeightMap(m); +// eslint-disable-next-line jest/no-export +export const dsMap = new BlockHeightMap(m); async function prepareDictionary( endpoint = DICTIONARY_ENDPOINT, diff --git a/packages/node-core/src/indexer/dictionary/v1/dictionaryV1.ts b/packages/node-core/src/indexer/dictionary/v1/dictionaryV1.ts index abecc68eb8..04ab2e6781 100644 --- a/packages/node-core/src/indexer/dictionary/v1/dictionaryV1.ts +++ b/packages/node-core/src/indexer/dictionary/v1/dictionaryV1.ts @@ -179,7 +179,11 @@ export abstract class DictionaryV1 extends CoreDictionary< } queryMapValidByHeight(height: number): boolean { - return !!this.queriesMap?.get(height)?.length; + try { + return !!this.queriesMap?.get(height)?.length; + } catch (e) { + return false; + } } protected dictionaryValidation(metaData?: DictionaryV1Metadata, startBlockHeight?: number): boolean { diff --git a/packages/node-core/src/indexer/dictionary/v2/dictionaryV2.test.ts b/packages/node-core/src/indexer/dictionary/v2/dictionaryV2.test.ts index 250658a0c7..cd8b5e8931 100644 --- a/packages/node-core/src/indexer/dictionary/v2/dictionaryV2.test.ts +++ b/packages/node-core/src/indexer/dictionary/v2/dictionaryV2.test.ts @@ -5,6 +5,7 @@ import {EventEmitter2} from '@nestjs/event-emitter'; import {DictionaryResponse, DictionaryV2, DictionaryV2QueryEntry, RawDictionaryResponseData} from '../'; import {NodeConfig} from '../../../configure'; import {IBlock} from '../../types'; +import {dsMap as mockedDsMap} from '../v1/dictionaryV1.test'; jest.setTimeout(50000); @@ -20,13 +21,6 @@ export class TestDictionaryV2 extends DictionaryV2 { buildDictionaryQueryEntries(dataSources: any[]): DictionaryV2QueryEntry { return {}; } - async getData( - startBlock: number, - queryEndBlock: number, - limit: number - ): Promise> | undefined> { - return Promise.resolve(undefined); - } convertResponseBlocks(result: RawDictionaryResponseData): DictionaryResponse> | undefined { return undefined; @@ -74,6 +68,13 @@ describe('Individual dictionary V2 test', () => { const queryEndBlock2 = dictionary.getQueryEndBlock(10000000000, 10000000000); expect(queryEndBlock2).toBe((dictionary as any)._metadata.end); }, 500000); + + it('can determine current dictionary query map is valid with block height', async () => { + await (dictionary as any).init(); + dictionary.updateQueriesMap(mockedDsMap); + expect(dictionary.queryMapValidByHeight(105)).toBeTruthy(); + expect(dictionary.queryMapValidByHeight(1)).toBeFalsy(); + }); }); describe('determine dictionary V2 version', () => { diff --git a/packages/node-core/src/indexer/dictionary/v2/dictionaryV2.ts b/packages/node-core/src/indexer/dictionary/v2/dictionaryV2.ts index 565fc5e90f..f9bb7e3f74 100644 --- a/packages/node-core/src/indexer/dictionary/v2/dictionaryV2.ts +++ b/packages/node-core/src/indexer/dictionary/v2/dictionaryV2.ts @@ -4,7 +4,7 @@ import {EventEmitter2} from '@nestjs/event-emitter'; import axios, {AxiosInstance} from 'axios'; import {utils} from 'ethers'; -import {FieldSelector, getBlockHeight} from '../'; +import {FieldSelector} from '../'; import {NodeConfig} from '../../../configure'; import {getLogger} from '../../../logger'; import {IBlock} from '../../types'; @@ -162,7 +162,11 @@ export abstract class DictionaryV2< queryMapValidByHeight(height: number): boolean { // we can not use map.has method here, has method only return true when value for corresponding key is set // but in BlockHeightMap it need to return any map <= key value, see `getDetails` method - return !!this.queriesMap?.get(height); + try { + return !!this.queriesMap?.get(height); + } catch (e) { + return false; + } } protected dictionaryValidation(metaData?: DictionaryV2Metadata, startBlockHeight?: number): boolean {