Skip to content

Commit

Permalink
add test and queryMapValidByHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
jiqiang90 committed Mar 10, 2024
1 parent 5fe37aa commit 2b678f8
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/node-core/src/indexer/dictionary/coreDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export abstract class CoreDictionary<DS, FB, M /* Metadata */, E /* DictionaryQu

// filter dictionary with start height
heightValidation(height: number): boolean {
return this.dictionaryValidation(this._metadata, height);
// validate metadata, and then validate query maps
return this.dictionaryValidation(this._metadata, height) && this.queryMapValidByHeight(height);
}

updateQueriesMap(dataSources: BlockHeightMap<DS[]>): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TestDictionaryService extends DictionaryService<any, TestFB> {
this.nodeConfig,
this.eventEmitter
);

patchMockDictionary(mockDictionaryV2);
const dictionariesV2 = [mockDictionaryV2];
this.init([...dictionariesV1, ...dictionariesV2]);
Expand Down
6 changes: 4 additions & 2 deletions packages/node-core/src/indexer/dictionary/types.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 {FieldSelector} from '@subql/node-core/indexer';
import {BlockHeightMap} from '../../utils/blockHeightMap';
import {IBlock} from '../types';

Expand All @@ -13,8 +14,9 @@ export interface IDictionary<DS, FB> {
metadataValid: boolean | undefined;
getData(
startBlock: number,
queryEndBlock: number,
limit: number
endBlock: number,
limit: number,
fieldSelector?: FieldSelector
): Promise<DictionaryResponse<IBlock<FB> | number> | undefined>;
queryMapValidByHeight(height: number): boolean;
getQueryEndBlock(targetEndHeight: number, apiFinalizedHeight: number): number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion packages/node-core/src/indexer/dictionary/v1/dictionaryV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ export abstract class DictionaryV1<DS> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -20,13 +21,6 @@ export class TestDictionaryV2 extends DictionaryV2<TestFB, any, any> {
buildDictionaryQueryEntries(dataSources: any[]): DictionaryV2QueryEntry {
return {};
}
async getData(
startBlock: number,
queryEndBlock: number,
limit: number
): Promise<DictionaryResponse<IBlock<TestFB>> | undefined> {
return Promise.resolve(undefined);
}

convertResponseBlocks<RFB>(result: RawDictionaryResponseData<RFB>): DictionaryResponse<IBlock<TestFB>> | undefined {
return undefined;
Expand Down Expand Up @@ -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', () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/node-core/src/indexer/dictionary/v2/dictionaryV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2b678f8

Please sign in to comment.