From 4bcad5e1777befe61a1e6747ab42c6b610742375 Mon Sep 17 00:00:00 2001 From: Orion Date: Thu, 27 Oct 2022 17:22:33 +0800 Subject: [PATCH] fix: incomplete ds data in same block --- .../node/src/indexer/dynamic-ds.service.ts | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/node/src/indexer/dynamic-ds.service.ts b/packages/node/src/indexer/dynamic-ds.service.ts index 7d7d545d9d..98892fdc66 100644 --- a/packages/node/src/indexer/dynamic-ds.service.ts +++ b/packages/node/src/indexer/dynamic-ds.service.ts @@ -5,7 +5,7 @@ import assert from 'assert'; import { Injectable } from '@nestjs/common'; import { isCustomDs, isRuntimeDs } from '@subql/common-substrate'; import { getLogger, MetadataRepo } from '@subql/node-core'; -import { cloneDeep } from 'lodash'; +import { cloneDeep, isEqual, unionWith } from 'lodash'; import { Transaction } from 'sequelize/types'; import { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject'; import { DsProcessorService } from './ds-processor.service'; @@ -86,20 +86,26 @@ export class DynamicDsService { ): Promise { assert(this.metaDataRepo, `Model _metadata does not exist`); const record = await this.metaDataRepo.findByPk(METADATA_KEY); - let results = record?.value; - - if (!results || typeof results !== 'string') { - if (blockHeight !== undefined) { - results = this.tempDsRecords?.[TEMP_DS_PREFIX + blockHeight]; - if (!results || typeof results !== 'string') { - return []; - } - } else { - return []; + + let results: DatasourceParams[] = []; + + const metaResults: DatasourceParams[] = JSON.parse( + (record?.value as string) ?? '[]', + ); + if (metaResults.length) { + results = [...metaResults]; + } + + if (blockHeight !== undefined) { + const tempResults: DatasourceParams[] = JSON.parse( + this.tempDsRecords?.[TEMP_DS_PREFIX + blockHeight] ?? '[]', + ); + if (tempResults.length) { + results = unionWith(results, tempResults, isEqual); } } - return JSON.parse(results); + return results; } private async saveDynamicDatasourceParams( @@ -115,7 +121,7 @@ export class DynamicDsService { .then(() => { this.tempDsRecords = { ...this.tempDsRecords, - ...{ [TEMP_DS_PREFIX + dsParams.startBlock]: dsRecords }, + [TEMP_DS_PREFIX + dsParams.startBlock]: dsRecords, }; }); }