@subql/node-ethereum 0.4.1-7 → 0.4.1-9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"fetch.service.js","sourceRoot":"","sources":["../../src/indexer/fetch.service.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;AAEtC,2CAA2E;AAC3E,yDAAsD;AACtD,+CAA+D;AAC/D,4DAIgC;AAChC,gDAU0B;AAU1B,mCAAsE;AACtE,kEAA+E;AAC/E,+DAA0D;AAC1D,4CAAkE;AAElE,6DAAyD;AACzD,6DAAwD;AACxD,2EAAuE;AAEvE,MAAM,MAAM,GAAG,IAAA,qBAAS,EAAC,OAAO,CAAC,CAAC;AAClC,IAAI,mBAAmB,GAAG,IAAI,CAAC;AAC/B,MAAM,yBAAyB,GAAG,KAAK,CAAC;AACxC,MAAM,qBAAqB,GAAG,KAAK,CAAC;AACpC,MAAM,kBAAkB,GAAG,CAAC,CAAC;AAC7B,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAE/B,SAAS,uBAAuB,CAC9B,MAAyB,EACzB,SAA0E;IAE1E,MAAM,UAAU,GAAG,EAAE,CAAC;IAEtB,IAAI,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE;QAC5B,MAAM,SAAS,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAE5E,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,IAAI,SAAS,CAAC,MAAM,IAAI,mBAAmB,EAAE;YACrE,UAAU,CAAC,IAAI,CAAC;gBACd,KAAK,EAAE,SAAS;gBAChB,KAAK,EAAE,SAAS;gBAChB,OAAO,EAAE,eAAe;aACzB,CAAC,CAAC;SACJ;KAC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AAY,CAAC,cAAc,CAAC,CAAC;oBACpD,OAAO,KAAK,CAAC;iBACd;aACF;YACD,OAAO,IAAI,CAAC;SACb;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,WAAmB;QACrC,MAAM,IAAI,CAAC,+BAA+B,EAAE,CAAC;QAC7C,IAAI,CAAC,gBAAgB,CAAC,mBAAmB,CAAC,WAAW,CAAC,CAAC;QACvD,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACxB,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;IAC/C,CAAC;IAED,KAAK,CAAC,0BAA0B,CAAC,WAAmB;QAClD,MAAM,IAAI,CAAC,+BAA+B,EAAE,CAAC;QAC7C,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACxB,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;IAC/C,CAAC;CACF,CAAA;AAvUC;IAAC,IAAA,mBAAQ,EAAC,qBAAqB,CAAC;;;;mDAS/B;AAGK;IADL,IAAA,mBAAQ,EAAC,mBAAmB,GAAG,IAAI,CAAC;;;;yDA0BpC;AAGK;IADL,IAAA,mBAAQ,EAAC,mBAAmB,GAAG,IAAI,CAAC;;;;oDAwBpC;AAzPU,YAAY;IADxB,IAAA,mBAAU,GAAE;IAcR,WAAA,IAAA,eAAM,EAAC,kBAAkB,CAAC,CAAA;IAC1B,WAAA,IAAA,eAAM,EAAC,kBAAkB,CAAC,CAAA;qCAHP,sBAAU;QACV,sBAAU;QACe,iCAAe,UAEjC,sCAAiB;QAClB,qCAAgB;QACR,oDAAwB;QACpC,6BAAa;QACR,4BAAiB;GAnBnC,YAAY,CAigBxB;AAjgBY,oCAAY","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';\nimport { EventEmitter2 } from '@nestjs/event-emitter';\nimport { Interval, SchedulerRegistry } from '@nestjs/schedule';\nimport {\n isCustomDs,\n EthereumHandlerKind,\n SubqlHandlerFilter,\n} from '@subql/common-ethereum';\nimport {\n ApiService,\n Dictionary,\n checkMemoryUsage,\n delay,\n getLogger,\n IndexerEvent,\n NodeConfig,\n transformBypassBlocks,\n cleanedBatchBlocks,\n} from '@subql/node-core';\nimport {\n DictionaryQueryEntry,\n ApiWrapper,\n EthereumLogFilter,\n EthereumTransactionFilter,\n SubqlEthereumProcessorOptions,\n DictionaryQueryCondition,\n} from '@subql/types-ethereum';\nimport { MetaData } from '@subql/utils';\nimport { range, sortBy, uniqBy, without, groupBy, add } from 'lodash';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { calcInterval } from '../ethereum/utils.ethereum';\nimport { eventToTopic, functionToSighash } from '../utils/string';\nimport { IBlockDispatcher } from './blockDispatcher';\nimport { DictionaryService } from './dictionary.service';\nimport { DynamicDsService } from './dynamic-ds.service';\nimport { UnfinalizedBlocksService } from './unfinalizedBlocks.service';\n\nconst logger = getLogger('fetch');\nlet BLOCK_TIME_VARIANCE = 5000;\nconst DICTIONARY_MAX_QUERY_SIZE = 10000;\nconst CHECK_MEMORY_INTERVAL = 60000;\nconst MINIMUM_BATCH_SIZE = 5;\nconst INTERVAL_PERCENT = 0.9;\nconst QUERY_ADDRESS_LIMIT = 50;\n\nfunction eventFilterToQueryEntry(\n filter: EthereumLogFilter,\n dsOptions: SubqlEthereumProcessorOptions | SubqlEthereumProcessorOptions[],\n): DictionaryQueryEntry {\n const conditions = [];\n\n if (Array.isArray(dsOptions)) {\n const addresses = dsOptions.map((option) => option.address).filter(Boolean);\n\n if (addresses.length !== 0 && addresses.length <= QUERY_ADDRESS_LIMIT) {\n conditions.push({\n field: 'address',\n value: addresses,\n matcher: 'inInsensitive',\n });\n }\n } else {\n if (dsOptions.address) {\n conditions.push({\n field: 'address',\n value: dsOptions.address.toLowerCase(),\n });\n }\n }\n if (filter.topics) {\n for (let i = 0; i < Math.min(filter.topics.length, 4); i++) {\n const topic = filter.topics[i];\n if (!topic) {\n continue;\n }\n const field = `topics${i}`;\n conditions.push({ field, value: eventToTopic(topic) });\n }\n }\n return {\n entity: 'evmLogs',\n conditions,\n };\n}\n\nfunction callFilterToQueryEntry(\n filter: EthereumTransactionFilter,\n): DictionaryQueryEntry {\n const conditions = [];\n if (filter.from) {\n conditions.push({\n field: 'from',\n value: filter.from.toLowerCase(),\n });\n }\n if (filter.to) {\n conditions.push({\n field: 'to',\n value: filter.to.toLowerCase(),\n });\n }\n if (filter.function) {\n conditions.push({\n field: 'func',\n value: functionToSighash(filter.function),\n });\n }\n return {\n entity: 'evmTransactions',\n conditions,\n };\n}\n\n@Injectable()\nexport class FetchService implements OnApplicationShutdown {\n private latestBestHeight: number;\n private latestFinalizedHeight: number;\n private isShutdown = false;\n private batchSizeScale: number;\n private templateDynamicDatasouces: SubqlProjectDs[];\n private dictionaryGenesisMatches = true;\n private evmChainId: string;\n private bypassBlocks: number[] = [];\n\n constructor(\n private apiService: ApiService,\n private nodeConfig: NodeConfig,\n @Inject('ISubqueryProject') private project: SubqueryProject,\n @Inject('IBlockDispatcher') private blockDispatcher: IBlockDispatcher,\n private dictionaryService: DictionaryService,\n private dynamicDsService: DynamicDsService,\n private unfinalizedBlocksService: UnfinalizedBlocksService,\n private eventEmitter: EventEmitter2,\n private schedulerRegistry: SchedulerRegistry,\n ) {\n this.batchSizeScale = 1;\n }\n\n onApplicationShutdown(): void {\n this.isShutdown = true;\n }\n\n get api(): ApiWrapper {\n return this.apiService.api;\n }\n\n async syncDynamicDatascourcesFromMeta(): Promise<void> {\n this.templateDynamicDatasouces =\n await this.dynamicDsService.getDynamicDatasources();\n }\n\n buildDictionaryQueryEntries(startBlock: number): DictionaryQueryEntry[] {\n const queryEntries: DictionaryQueryEntry[] = [];\n\n type GroupedSubqlProjectDs = SubqlProjectDs & {\n groupedOptions?: SubqlEthereumProcessorOptions[];\n };\n\n const groupdDynamicDs: GroupedSubqlProjectDs[] = Object.values(\n groupBy(this.templateDynamicDatasouces, (ds) => ds.name),\n ).map((grouped: SubqlProjectDs[]) => {\n const options = grouped.map((ds) => ds.options);\n const ref = grouped[0];\n\n return {\n ...ref,\n groupedOptions: options,\n };\n });\n\n // Only run the ds that is equal or less than startBlock\n // sort array from lowest ds.startBlock to highest\n const filteredDs: GroupedSubqlProjectDs[] = this.project.dataSources\n .concat(groupdDynamicDs)\n .filter((ds) => ds.startBlock <= startBlock)\n .sort((a, b) => a.startBlock - b.startBlock);\n\n for (const ds of filteredDs) {\n for (const handler of ds.mapping.handlers) {\n let filterList: SubqlHandlerFilter[];\n filterList = [handler.filter];\n filterList = filterList.filter((f) => f);\n if (!filterList.length) return [];\n switch (handler.kind) {\n case EthereumHandlerKind.Block:\n return [];\n case EthereumHandlerKind.Call: {\n for (const filter of filterList as EthereumTransactionFilter[]) {\n if (\n filter.from !== undefined ||\n filter.to !== undefined ||\n filter.function\n ) {\n queryEntries.push(callFilterToQueryEntry(filter));\n } else {\n return [];\n }\n }\n break;\n }\n case EthereumHandlerKind.Event: {\n for (const filter of filterList as EthereumLogFilter[]) {\n if (ds.groupedOptions) {\n queryEntries.push(\n eventFilterToQueryEntry(filter, ds.groupedOptions),\n );\n } else if (ds.options?.address || filter.topics) {\n queryEntries.push(eventFilterToQueryEntry(filter, ds.options));\n } else {\n return [];\n }\n }\n break;\n }\n default:\n }\n }\n }\n\n return uniqBy(\n queryEntries,\n (item) =>\n `${item.entity}|${JSON.stringify(\n sortBy(item.conditions, (c) => c.field),\n )}`,\n );\n }\n\n updateDictionary(): void {\n this.dictionaryService.buildDictionaryEntryMap<SubqlProjectDs>(\n this.project.dataSources.concat(this.templateDynamicDatasouces),\n this.buildDictionaryQueryEntries.bind(this),\n );\n }\n\n private get useDictionary(): boolean {\n return (\n !!this.project.network.dictionary &&\n this.dictionaryGenesisMatches &&\n !!this.dictionaryService.getDictionaryQueryEntries(\n this.blockDispatcher.latestBufferedHeight ??\n Math.min(...this.project.dataSources.map((ds) => ds.startBlock)),\n ).length\n );\n }\n\n async init(startHeight: number): Promise<void> {\n if (this.project.network?.bypassBlocks !== undefined) {\n this.bypassBlocks = transformBypassBlocks(\n this.project.network.bypassBlocks,\n ).filter((blk) => blk >= startHeight);\n }\n if (this.api) {\n const CHAIN_INTERVAL = calcInterval(this.api) * INTERVAL_PERCENT;\n\n BLOCK_TIME_VARIANCE = Math.min(BLOCK_TIME_VARIANCE, CHAIN_INTERVAL);\n\n this.schedulerRegistry.addInterval(\n 'getLatestBlockHead',\n setInterval(() => void this.getBestBlockHead(), BLOCK_TIME_VARIANCE),\n );\n }\n await this.syncDynamicDatascourcesFromMeta();\n\n this.updateDictionary();\n this.eventEmitter.emit(IndexerEvent.UsingDictionary, {\n value: Number(this.useDictionary),\n });\n\n if (this.project.network.dictionary) {\n this.evmChainId = await this.dictionaryService.getEvmChainId();\n }\n\n await this.getFinalizedBlockHead();\n await this.getBestBlockHead();\n\n // Call metadata here, other network should align with this\n // For substrate, we might use the specVersion metadata in future if we have same error handling as in node-core\n const metadata = await this.dictionaryService.getMetadata();\n\n const validChecker = this.dictionaryValidation(metadata);\n\n if (validChecker) {\n this.dictionaryService.setDictionaryStartHeight(\n metadata._metadata.startHeight,\n );\n }\n\n await this.blockDispatcher.init(this.resetForNewDs.bind(this));\n void this.startLoop(startHeight);\n }\n\n getUseDictionary(): boolean {\n return this.useDictionary;\n }\n\n getLatestFinalizedHeight(): number {\n return this.latestFinalizedHeight;\n }\n\n @Interval(CHECK_MEMORY_INTERVAL)\n checkBatchScale(): void {\n if (this.nodeConfig['scale-batch-size']) {\n const scale = checkMemoryUsage(this.batchSizeScale, this.nodeConfig);\n\n if (this.batchSizeScale !== scale) {\n this.batchSizeScale = scale;\n }\n }\n }\n\n @Interval(BLOCK_TIME_VARIANCE * 1000)\n async getFinalizedBlockHead(): Promise<void> {\n if (!this.api) {\n logger.debug(`Skip fetch finalized block until API is ready`);\n return;\n }\n try {\n const currentFinalizedHeight = await this.api.getFinalizedBlockHeight();\n logger.debug(`finalized:${currentFinalizedHeight.toString()}`);\n const finalizedHeader = await this.api.getBlockByHeightOrHash(\n currentFinalizedHeight,\n );\n if (this.latestFinalizedHeight !== currentFinalizedHeight) {\n this.latestFinalizedHeight = currentFinalizedHeight;\n this.unfinalizedBlocksService.registerFinalizedBlock(finalizedHeader);\n if (!this.nodeConfig.unfinalizedBlocks) {\n if (!this.nodeConfig.unfinalizedBlocks) {\n this.eventEmitter.emit(IndexerEvent.BlockTarget, {\n height: this.latestFinalizedHeight,\n });\n }\n }\n }\n } catch (e) {\n logger.warn(e, `Having a problem when get finalized block`);\n }\n }\n\n @Interval(BLOCK_TIME_VARIANCE * 1000)\n async getBestBlockHead(): Promise<void> {\n if (!this.api) {\n logger.debug(`Skip fetch best block until API is ready`);\n return;\n }\n try {\n const currentBestHeight = await this.api.getBestBlockHeight();\n logger.debug(`best:${currentBestHeight.toString()}`);\n if (this.latestBestHeight !== currentBestHeight) {\n this.latestBestHeight = currentBestHeight;\n this.eventEmitter.emit(IndexerEvent.BlockBest, {\n height: this.latestBestHeight,\n });\n\n if (this.nodeConfig.unfinalizedBlocks) {\n this.eventEmitter.emit(IndexerEvent.BlockTarget, {\n height: this.latestBestHeight,\n });\n }\n }\n } catch (e) {\n logger.warn(e, `Having a problem when get best block`);\n }\n }\n private async startLoop(initBlockHeight: number): Promise<void> {\n await this.fillNextBlockBuffer(initBlockHeight);\n }\n\n getModulos(): number[] {\n const modulos: number[] = [];\n for (const ds of this.project.dataSources) {\n if (isCustomDs(ds)) {\n continue;\n }\n for (const handler of ds.mapping.handlers) {\n if (\n handler.kind === EthereumHandlerKind.Block &&\n handler.filter &&\n handler.filter.modulo\n ) {\n modulos.push(handler.filter.modulo);\n }\n }\n }\n return modulos;\n }\n\n getModuloBlocks(startHeight: number, endHeight: number): number[] {\n const modulos = this.getModulos();\n const moduloBlocks: number[] = [];\n for (let i = startHeight; i < endHeight; i++) {\n if (modulos.find((m) => i % m === 0)) {\n moduloBlocks.push(i);\n }\n }\n return moduloBlocks;\n }\n\n getEnqueuedModuloBlocks(startBlockHeight: number): number[] {\n return this.getModuloBlocks(\n startBlockHeight,\n this.nodeConfig.batchSize * Math.max(...this.getModulos()) +\n startBlockHeight,\n ).slice(0, this.nodeConfig.batchSize);\n }\n\n async fillNextBlockBuffer(initBlockHeight: number): Promise<void> {\n let startBlockHeight: number;\n let scaledBatchSize: number;\n const handlers = [].concat(\n ...this.project.dataSources.map((ds) => ds.mapping.handlers),\n );\n\n const getStartBlockHeight = (): number => {\n return this.blockDispatcher.latestBufferedHeight\n ? this.blockDispatcher.latestBufferedHeight + 1\n : initBlockHeight;\n };\n\n if (this.dictionaryService.startHeight > getStartBlockHeight()) {\n logger.warn(\n `Dictionary start height ${\n this.dictionaryService.startHeight\n } is beyond indexing height ${getStartBlockHeight()}, skipping dictionary for now`,\n );\n }\n\n while (!this.isShutdown) {\n startBlockHeight = getStartBlockHeight();\n\n scaledBatchSize = Math.max(\n Math.round(this.batchSizeScale * this.nodeConfig.batchSize),\n Math.min(MINIMUM_BATCH_SIZE, this.nodeConfig.batchSize * 3),\n );\n const latestHeight = this.nodeConfig.unfinalizedBlocks\n ? this.latestBestHeight\n : this.latestFinalizedHeight;\n\n if (\n this.blockDispatcher.freeSize < scaledBatchSize ||\n startBlockHeight > latestHeight\n ) {\n await delay(1);\n continue;\n }\n\n if (\n this.useDictionary &&\n startBlockHeight >= this.dictionaryService.startHeight\n ) {\n const queryEndBlock = startBlockHeight + DICTIONARY_MAX_QUERY_SIZE;\n const moduloBlocks = this.getModuloBlocks(\n startBlockHeight,\n queryEndBlock,\n );\n\n try {\n const dictionary =\n await this.dictionaryService.scopedDictionaryEntries(\n startBlockHeight,\n queryEndBlock,\n scaledBatchSize,\n );\n\n if (startBlockHeight !== getStartBlockHeight()) {\n logger.debug(\n `Queue was reset for new DS, discarding dictionary query result`,\n );\n continue;\n }\n\n if (\n dictionary &&\n this.dictionaryValidation(dictionary, startBlockHeight)\n ) {\n let { batchBlocks } = dictionary;\n\n batchBlocks = batchBlocks\n .concat(moduloBlocks)\n .sort((a, b) => a - b);\n if (batchBlocks.length === 0) {\n // There we're no blocks in this query range, we can set a new height we're up to\n this.blockDispatcher.enqueueBlocks(\n [],\n Math.min(\n queryEndBlock - 1,\n dictionary._metadata.lastProcessedHeight,\n ),\n );\n } else {\n const maxBlockSize = Math.min(\n batchBlocks.length,\n this.blockDispatcher.freeSize,\n );\n const enqueuingBlocks = batchBlocks.slice(0, maxBlockSize);\n const cleanedBatchBlocks =\n this.filteredBlockBatch(enqueuingBlocks);\n\n this.blockDispatcher.enqueueBlocks(\n cleanedBatchBlocks,\n this.getLatestBufferHeight(cleanedBatchBlocks, enqueuingBlocks),\n );\n }\n continue; // skip nextBlockRange() way\n }\n // else use this.nextBlockRange()\n } catch (e) {\n logger.debug(`Fetch dictionary stopped: ${e.message}`);\n this.eventEmitter.emit(IndexerEvent.SkipDictionary);\n }\n }\n\n const endHeight = this.nextEndBlockHeight(\n startBlockHeight,\n scaledBatchSize,\n );\n\n if (handlers.length && this.getModulos().length === handlers.length) {\n const enqueuingBlocks = this.getEnqueuedModuloBlocks(startBlockHeight);\n const cleanedBatchBlocks = this.filteredBlockBatch(enqueuingBlocks);\n this.blockDispatcher.enqueueBlocks(\n cleanedBatchBlocks,\n this.getLatestBufferHeight(cleanedBatchBlocks, enqueuingBlocks),\n );\n } else {\n const enqueuingBlocks = range(startBlockHeight, endHeight + 1);\n const cleanedBatchBlocks = this.filteredBlockBatch(enqueuingBlocks);\n this.blockDispatcher.enqueueBlocks(\n cleanedBatchBlocks,\n this.getLatestBufferHeight(cleanedBatchBlocks, enqueuingBlocks),\n );\n }\n }\n }\n\n private getLatestBufferHeight(\n cleanedBatchBlocks: number[],\n rawBatchBlocks: number[],\n ): number {\n return Math.max(...cleanedBatchBlocks, ...rawBatchBlocks);\n }\n private filteredBlockBatch(currentBatchBlocks: number[]): number[] {\n if (!this.bypassBlocks.length || !currentBatchBlocks) {\n return currentBatchBlocks;\n }\n\n const cleanedBatch = cleanedBatchBlocks(\n this.bypassBlocks,\n currentBatchBlocks,\n );\n\n const pollutedBlocks = this.bypassBlocks.filter(\n (b) => b < Math.max(...currentBatchBlocks),\n );\n if (pollutedBlocks.length) {\n logger.info(`Bypassing blocks: ${pollutedBlocks}`);\n }\n this.bypassBlocks = without(this.bypassBlocks, ...pollutedBlocks);\n return cleanedBatch;\n }\n\n private nextEndBlockHeight(\n startBlockHeight: number,\n scaledBatchSize: number,\n ): number {\n let endBlockHeight = startBlockHeight + scaledBatchSize - 1;\n\n if (endBlockHeight > this.latestFinalizedHeight) {\n if (this.nodeConfig.unfinalizedBlocks) {\n if (endBlockHeight >= this.latestBestHeight) {\n endBlockHeight = this.latestBestHeight;\n }\n } else {\n endBlockHeight = this.latestFinalizedHeight;\n }\n }\n return endBlockHeight;\n }\n\n private dictionaryValidation(\n dictionary: { _metadata: MetaData },\n startBlockHeight?: number,\n ): boolean {\n if (dictionary !== undefined) {\n const { _metadata: metaData } = dictionary;\n\n if (\n metaData.genesisHash !== this.api.getGenesisHash() &&\n this.evmChainId !== this.api.getChainId().toString()\n ) {\n logger.error(\n 'The dictionary that you have specified does not match the chain you are indexing, it will be ignored. Please update your project manifest to reference the correct dictionary',\n );\n this.dictionaryGenesisMatches = false;\n this.eventEmitter.emit(IndexerEvent.UsingDictionary, {\n value: Number(this.useDictionary),\n });\n this.eventEmitter.emit(IndexerEvent.SkipDictionary);\n return false;\n }\n\n if (startBlockHeight !== undefined) {\n if (metaData.lastProcessedHeight < startBlockHeight) {\n logger.warn(\n `Dictionary indexed block is behind current indexing block height`,\n );\n this.eventEmitter.emit(IndexerEvent.SkipDictionary);\n return false;\n }\n }\n return true;\n }\n return false;\n }\n\n async resetForNewDs(blockHeight: number): Promise<void> {\n await this.syncDynamicDatascourcesFromMeta();\n this.dynamicDsService.deleteTempDsRecords(blockHeight);\n this.updateDictionary();\n this.blockDispatcher.flushQueue(blockHeight);\n }\n\n async resetForIncorrectBestBlock(blockHeight: number): Promise<void> {\n await this.syncDynamicDatascourcesFromMeta();\n this.updateDictionary();\n this.blockDispatcher.flushQueue(blockHeight);\n }\n}\n"]}
1
+ {"version":3,"file":"fetch.service.js","sourceRoot":"","sources":["../../src/indexer/fetch.service.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;AAEtC,2CAA2E;AAC3E,yDAAsD;AACtD,+CAA+D;AAC/D,4DAIgC;AAChC,gDAU0B;AAU1B,mCAAsE;AACtE,kEAA+E;AAC/E,+DAA0D;AAC1D,4CAAkE;AAElE,6DAAyD;AACzD,6DAAwD;AACxD,2EAAuE;AAEvE,MAAM,MAAM,GAAG,IAAA,qBAAS,EAAC,OAAO,CAAC,CAAC;AAClC,IAAI,mBAAmB,GAAG,IAAI,CAAC;AAC/B,MAAM,yBAAyB,GAAG,KAAK,CAAC;AACxC,MAAM,qBAAqB,GAAG,KAAK,CAAC;AACpC,MAAM,kBAAkB,GAAG,CAAC,CAAC;AAC7B,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAE/B,SAAS,uBAAuB,CAC9B,MAAyB,EACzB,SAA0E;IAE1E,MAAM,UAAU,GAAG,EAAE,CAAC;IAEtB,IAAI,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE;QAC5B,MAAM,SAAS,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAE5E,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,IAAI,SAAS,CAAC,MAAM,IAAI,mBAAmB,EAAE;YACrE,UAAU,CAAC,IAAI,CAAC;gBACd,KAAK,EAAE,SAAS;gBAChB,KAAK,EAAE,SAAS;gBAChB,OAAO,EAAE,eAAe;aACzB,CAAC,CAAC;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,CAAC,YAAY,CAAC,IAAI,CAAC,wBAAY,CAAC,cAAc,CAAC,CAAC;oBACpD,OAAO,KAAK,CAAC;iBACd;aACF;YACD,OAAO,IAAI,CAAC;SACb;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,WAAmB;QACrC,MAAM,IAAI,CAAC,+BAA+B,EAAE,CAAC;QAC7C,IAAI,CAAC,gBAAgB,CAAC,mBAAmB,CAAC,WAAW,CAAC,CAAC;QACvD,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACxB,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;IAC/C,CAAC;IAED,KAAK,CAAC,0BAA0B,CAAC,WAAmB;QAClD,MAAM,IAAI,CAAC,+BAA+B,EAAE,CAAC;QAC7C,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACxB,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;IAC/C,CAAC;CACF,CAAA;AAvUC;IAAC,IAAA,mBAAQ,EAAC,qBAAqB,CAAC;;;;mDAS/B;AAGK;IADL,IAAA,mBAAQ,EAAC,mBAAmB,GAAG,IAAI,CAAC;;;;yDA0BpC;AAGK;IADL,IAAA,mBAAQ,EAAC,mBAAmB,GAAG,IAAI,CAAC;;;;oDAwBpC;AAzPU,YAAY;IADxB,IAAA,mBAAU,GAAE;IAcR,WAAA,IAAA,eAAM,EAAC,kBAAkB,CAAC,CAAA;IAC1B,WAAA,IAAA,eAAM,EAAC,kBAAkB,CAAC,CAAA;qCAHP,sBAAU;QACV,sBAAU;QACe,iCAAe,UAEjC,sCAAiB;QAClB,qCAAgB;QACR,oDAAwB;QACpC,6BAAa;QACR,4BAAiB;GAnBnC,YAAY,CAigBxB;AAjgBY,oCAAY","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';\nimport { EventEmitter2 } from '@nestjs/event-emitter';\nimport { Interval, SchedulerRegistry } from '@nestjs/schedule';\nimport {\n isCustomDs,\n EthereumHandlerKind,\n SubqlHandlerFilter,\n} from '@subql/common-ethereum';\nimport {\n ApiService,\n Dictionary,\n checkMemoryUsage,\n delay,\n getLogger,\n IndexerEvent,\n NodeConfig,\n transformBypassBlocks,\n cleanedBatchBlocks,\n} from '@subql/node-core';\nimport {\n DictionaryQueryEntry,\n ApiWrapper,\n EthereumLogFilter,\n EthereumTransactionFilter,\n SubqlEthereumProcessorOptions,\n DictionaryQueryCondition,\n} from '@subql/types-ethereum';\nimport { MetaData } from '@subql/utils';\nimport { range, sortBy, uniqBy, without, groupBy, add } from 'lodash';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { calcInterval } from '../ethereum/utils.ethereum';\nimport { eventToTopic, functionToSighash } from '../utils/string';\nimport { IBlockDispatcher } from './blockDispatcher';\nimport { DictionaryService } from './dictionary.service';\nimport { DynamicDsService } from './dynamic-ds.service';\nimport { UnfinalizedBlocksService } from './unfinalizedBlocks.service';\n\nconst logger = getLogger('fetch');\nlet BLOCK_TIME_VARIANCE = 5000;\nconst DICTIONARY_MAX_QUERY_SIZE = 10000;\nconst CHECK_MEMORY_INTERVAL = 60000;\nconst MINIMUM_BATCH_SIZE = 5;\nconst INTERVAL_PERCENT = 0.9;\nconst QUERY_ADDRESS_LIMIT = 50;\n\nfunction eventFilterToQueryEntry(\n filter: EthereumLogFilter,\n dsOptions: SubqlEthereumProcessorOptions | SubqlEthereumProcessorOptions[],\n): DictionaryQueryEntry {\n const conditions = [];\n\n if (Array.isArray(dsOptions)) {\n const addresses = dsOptions.map((option) => option.address).filter(Boolean);\n\n if (addresses.length !== 0 && addresses.length <= QUERY_ADDRESS_LIMIT) {\n conditions.push({\n field: 'address',\n value: addresses,\n matcher: 'inInsensitive',\n });\n }\n } else {\n if (dsOptions?.address) {\n conditions.push({\n field: 'address',\n value: dsOptions.address.toLowerCase(),\n });\n }\n }\n if (filter.topics) {\n for (let i = 0; i < Math.min(filter.topics.length, 4); i++) {\n const topic = filter.topics[i];\n if (!topic) {\n continue;\n }\n const field = `topics${i}`;\n conditions.push({ field, value: eventToTopic(topic) });\n }\n }\n return {\n entity: 'evmLogs',\n conditions,\n };\n}\n\nfunction callFilterToQueryEntry(\n filter: EthereumTransactionFilter,\n): DictionaryQueryEntry {\n const conditions = [];\n if (filter.from) {\n conditions.push({\n field: 'from',\n value: filter.from.toLowerCase(),\n });\n }\n if (filter.to) {\n conditions.push({\n field: 'to',\n value: filter.to.toLowerCase(),\n });\n }\n if (filter.function) {\n conditions.push({\n field: 'func',\n value: functionToSighash(filter.function),\n });\n }\n return {\n entity: 'evmTransactions',\n conditions,\n };\n}\n\n@Injectable()\nexport class FetchService implements OnApplicationShutdown {\n private latestBestHeight: number;\n private latestFinalizedHeight: number;\n private isShutdown = false;\n private batchSizeScale: number;\n private templateDynamicDatasouces: SubqlProjectDs[];\n private dictionaryGenesisMatches = true;\n private evmChainId: string;\n private bypassBlocks: number[] = [];\n\n constructor(\n private apiService: ApiService,\n private nodeConfig: NodeConfig,\n @Inject('ISubqueryProject') private project: SubqueryProject,\n @Inject('IBlockDispatcher') private blockDispatcher: IBlockDispatcher,\n private dictionaryService: DictionaryService,\n private dynamicDsService: DynamicDsService,\n private unfinalizedBlocksService: UnfinalizedBlocksService,\n private eventEmitter: EventEmitter2,\n private schedulerRegistry: SchedulerRegistry,\n ) {\n this.batchSizeScale = 1;\n }\n\n onApplicationShutdown(): void {\n this.isShutdown = true;\n }\n\n get api(): ApiWrapper {\n return this.apiService.api;\n }\n\n async syncDynamicDatascourcesFromMeta(): Promise<void> {\n this.templateDynamicDatasouces =\n await this.dynamicDsService.getDynamicDatasources();\n }\n\n buildDictionaryQueryEntries(startBlock: number): DictionaryQueryEntry[] {\n const queryEntries: DictionaryQueryEntry[] = [];\n\n type GroupedSubqlProjectDs = SubqlProjectDs & {\n groupedOptions?: SubqlEthereumProcessorOptions[];\n };\n\n const groupdDynamicDs: GroupedSubqlProjectDs[] = Object.values(\n groupBy(this.templateDynamicDatasouces, (ds) => ds.name),\n ).map((grouped: SubqlProjectDs[]) => {\n const options = grouped.map((ds) => ds.options);\n const ref = grouped[0];\n\n return {\n ...ref,\n groupedOptions: options,\n };\n });\n\n // Only run the ds that is equal or less than startBlock\n // sort array from lowest ds.startBlock to highest\n const filteredDs: GroupedSubqlProjectDs[] = this.project.dataSources\n .concat(groupdDynamicDs)\n .filter((ds) => ds.startBlock <= startBlock)\n .sort((a, b) => a.startBlock - b.startBlock);\n\n for (const ds of filteredDs) {\n for (const handler of ds.mapping.handlers) {\n let filterList: SubqlHandlerFilter[];\n filterList = [handler.filter];\n filterList = filterList.filter((f) => f);\n if (!filterList.length) return [];\n switch (handler.kind) {\n case EthereumHandlerKind.Block:\n return [];\n case EthereumHandlerKind.Call: {\n for (const filter of filterList as EthereumTransactionFilter[]) {\n if (\n filter.from !== undefined ||\n filter.to !== undefined ||\n filter.function\n ) {\n queryEntries.push(callFilterToQueryEntry(filter));\n } else {\n return [];\n }\n }\n break;\n }\n case EthereumHandlerKind.Event: {\n for (const filter of filterList as EthereumLogFilter[]) {\n if (ds.groupedOptions) {\n queryEntries.push(\n eventFilterToQueryEntry(filter, ds.groupedOptions),\n );\n } else if (ds.options?.address || filter.topics) {\n queryEntries.push(eventFilterToQueryEntry(filter, ds.options));\n } else {\n return [];\n }\n }\n break;\n }\n default:\n }\n }\n }\n\n return uniqBy(\n queryEntries,\n (item) =>\n `${item.entity}|${JSON.stringify(\n sortBy(item.conditions, (c) => c.field),\n )}`,\n );\n }\n\n updateDictionary(): void {\n this.dictionaryService.buildDictionaryEntryMap<SubqlProjectDs>(\n this.project.dataSources.concat(this.templateDynamicDatasouces),\n this.buildDictionaryQueryEntries.bind(this),\n );\n }\n\n private get useDictionary(): boolean {\n return (\n !!this.project.network.dictionary &&\n this.dictionaryGenesisMatches &&\n !!this.dictionaryService.getDictionaryQueryEntries(\n this.blockDispatcher.latestBufferedHeight ??\n Math.min(...this.project.dataSources.map((ds) => ds.startBlock)),\n ).length\n );\n }\n\n async init(startHeight: number): Promise<void> {\n if (this.project.network?.bypassBlocks !== undefined) {\n this.bypassBlocks = transformBypassBlocks(\n this.project.network.bypassBlocks,\n ).filter((blk) => blk >= startHeight);\n }\n if (this.api) {\n const CHAIN_INTERVAL = calcInterval(this.api) * INTERVAL_PERCENT;\n\n BLOCK_TIME_VARIANCE = Math.min(BLOCK_TIME_VARIANCE, CHAIN_INTERVAL);\n\n this.schedulerRegistry.addInterval(\n 'getLatestBlockHead',\n setInterval(() => void this.getBestBlockHead(), BLOCK_TIME_VARIANCE),\n );\n }\n await this.syncDynamicDatascourcesFromMeta();\n\n this.updateDictionary();\n this.eventEmitter.emit(IndexerEvent.UsingDictionary, {\n value: Number(this.useDictionary),\n });\n\n if (this.project.network.dictionary) {\n this.evmChainId = await this.dictionaryService.getEvmChainId();\n }\n\n await this.getFinalizedBlockHead();\n await this.getBestBlockHead();\n\n // Call metadata here, other network should align with this\n // For substrate, we might use the specVersion metadata in future if we have same error handling as in node-core\n const metadata = await this.dictionaryService.getMetadata();\n\n const validChecker = this.dictionaryValidation(metadata);\n\n if (validChecker) {\n this.dictionaryService.setDictionaryStartHeight(\n metadata._metadata.startHeight,\n );\n }\n\n await this.blockDispatcher.init(this.resetForNewDs.bind(this));\n void this.startLoop(startHeight);\n }\n\n getUseDictionary(): boolean {\n return this.useDictionary;\n }\n\n getLatestFinalizedHeight(): number {\n return this.latestFinalizedHeight;\n }\n\n @Interval(CHECK_MEMORY_INTERVAL)\n checkBatchScale(): void {\n if (this.nodeConfig['scale-batch-size']) {\n const scale = checkMemoryUsage(this.batchSizeScale, this.nodeConfig);\n\n if (this.batchSizeScale !== scale) {\n this.batchSizeScale = scale;\n }\n }\n }\n\n @Interval(BLOCK_TIME_VARIANCE * 1000)\n async getFinalizedBlockHead(): Promise<void> {\n if (!this.api) {\n logger.debug(`Skip fetch finalized block until API is ready`);\n return;\n }\n try {\n const currentFinalizedHeight = await this.api.getFinalizedBlockHeight();\n logger.debug(`finalized:${currentFinalizedHeight.toString()}`);\n const finalizedHeader = await this.api.getBlockByHeightOrHash(\n currentFinalizedHeight,\n );\n if (this.latestFinalizedHeight !== currentFinalizedHeight) {\n this.latestFinalizedHeight = currentFinalizedHeight;\n this.unfinalizedBlocksService.registerFinalizedBlock(finalizedHeader);\n if (!this.nodeConfig.unfinalizedBlocks) {\n if (!this.nodeConfig.unfinalizedBlocks) {\n this.eventEmitter.emit(IndexerEvent.BlockTarget, {\n height: this.latestFinalizedHeight,\n });\n }\n }\n }\n } catch (e) {\n logger.warn(e, `Having a problem when get finalized block`);\n }\n }\n\n @Interval(BLOCK_TIME_VARIANCE * 1000)\n async getBestBlockHead(): Promise<void> {\n if (!this.api) {\n logger.debug(`Skip fetch best block until API is ready`);\n return;\n }\n try {\n const currentBestHeight = await this.api.getBestBlockHeight();\n logger.debug(`best:${currentBestHeight.toString()}`);\n if (this.latestBestHeight !== currentBestHeight) {\n this.latestBestHeight = currentBestHeight;\n this.eventEmitter.emit(IndexerEvent.BlockBest, {\n height: this.latestBestHeight,\n });\n\n if (this.nodeConfig.unfinalizedBlocks) {\n this.eventEmitter.emit(IndexerEvent.BlockTarget, {\n height: this.latestBestHeight,\n });\n }\n }\n } catch (e) {\n logger.warn(e, `Having a problem when get best block`);\n }\n }\n private async startLoop(initBlockHeight: number): Promise<void> {\n await this.fillNextBlockBuffer(initBlockHeight);\n }\n\n getModulos(): number[] {\n const modulos: number[] = [];\n for (const ds of this.project.dataSources) {\n if (isCustomDs(ds)) {\n continue;\n }\n for (const handler of ds.mapping.handlers) {\n if (\n handler.kind === EthereumHandlerKind.Block &&\n handler.filter &&\n handler.filter.modulo\n ) {\n modulos.push(handler.filter.modulo);\n }\n }\n }\n return modulos;\n }\n\n getModuloBlocks(startHeight: number, endHeight: number): number[] {\n const modulos = this.getModulos();\n const moduloBlocks: number[] = [];\n for (let i = startHeight; i < endHeight; i++) {\n if (modulos.find((m) => i % m === 0)) {\n moduloBlocks.push(i);\n }\n }\n return moduloBlocks;\n }\n\n getEnqueuedModuloBlocks(startBlockHeight: number): number[] {\n return this.getModuloBlocks(\n startBlockHeight,\n this.nodeConfig.batchSize * Math.max(...this.getModulos()) +\n startBlockHeight,\n ).slice(0, this.nodeConfig.batchSize);\n }\n\n async fillNextBlockBuffer(initBlockHeight: number): Promise<void> {\n let startBlockHeight: number;\n let scaledBatchSize: number;\n const handlers = [].concat(\n ...this.project.dataSources.map((ds) => ds.mapping.handlers),\n );\n\n const getStartBlockHeight = (): number => {\n return this.blockDispatcher.latestBufferedHeight\n ? this.blockDispatcher.latestBufferedHeight + 1\n : initBlockHeight;\n };\n\n if (this.dictionaryService.startHeight > getStartBlockHeight()) {\n logger.warn(\n `Dictionary start height ${\n this.dictionaryService.startHeight\n } is beyond indexing height ${getStartBlockHeight()}, skipping dictionary for now`,\n );\n }\n\n while (!this.isShutdown) {\n startBlockHeight = getStartBlockHeight();\n\n scaledBatchSize = Math.max(\n Math.round(this.batchSizeScale * this.nodeConfig.batchSize),\n Math.min(MINIMUM_BATCH_SIZE, this.nodeConfig.batchSize * 3),\n );\n const latestHeight = this.nodeConfig.unfinalizedBlocks\n ? this.latestBestHeight\n : this.latestFinalizedHeight;\n\n if (\n this.blockDispatcher.freeSize < scaledBatchSize ||\n startBlockHeight > latestHeight\n ) {\n await delay(1);\n continue;\n }\n\n if (\n this.useDictionary &&\n startBlockHeight >= this.dictionaryService.startHeight\n ) {\n const queryEndBlock = startBlockHeight + DICTIONARY_MAX_QUERY_SIZE;\n const moduloBlocks = this.getModuloBlocks(\n startBlockHeight,\n queryEndBlock,\n );\n\n try {\n const dictionary =\n await this.dictionaryService.scopedDictionaryEntries(\n startBlockHeight,\n queryEndBlock,\n scaledBatchSize,\n );\n\n if (startBlockHeight !== getStartBlockHeight()) {\n logger.debug(\n `Queue was reset for new DS, discarding dictionary query result`,\n );\n continue;\n }\n\n if (\n dictionary &&\n this.dictionaryValidation(dictionary, startBlockHeight)\n ) {\n let { batchBlocks } = dictionary;\n\n batchBlocks = batchBlocks\n .concat(moduloBlocks)\n .sort((a, b) => a - b);\n if (batchBlocks.length === 0) {\n // There we're no blocks in this query range, we can set a new height we're up to\n this.blockDispatcher.enqueueBlocks(\n [],\n Math.min(\n queryEndBlock - 1,\n dictionary._metadata.lastProcessedHeight,\n ),\n );\n } else {\n const maxBlockSize = Math.min(\n batchBlocks.length,\n this.blockDispatcher.freeSize,\n );\n const enqueuingBlocks = batchBlocks.slice(0, maxBlockSize);\n const cleanedBatchBlocks =\n this.filteredBlockBatch(enqueuingBlocks);\n\n this.blockDispatcher.enqueueBlocks(\n cleanedBatchBlocks,\n this.getLatestBufferHeight(cleanedBatchBlocks, enqueuingBlocks),\n );\n }\n continue; // skip nextBlockRange() way\n }\n // else use this.nextBlockRange()\n } catch (e) {\n logger.debug(`Fetch dictionary stopped: ${e.message}`);\n this.eventEmitter.emit(IndexerEvent.SkipDictionary);\n }\n }\n\n const endHeight = this.nextEndBlockHeight(\n startBlockHeight,\n scaledBatchSize,\n );\n\n if (handlers.length && this.getModulos().length === handlers.length) {\n const enqueuingBlocks = this.getEnqueuedModuloBlocks(startBlockHeight);\n const cleanedBatchBlocks = this.filteredBlockBatch(enqueuingBlocks);\n this.blockDispatcher.enqueueBlocks(\n cleanedBatchBlocks,\n this.getLatestBufferHeight(cleanedBatchBlocks, enqueuingBlocks),\n );\n } else {\n const enqueuingBlocks = range(startBlockHeight, endHeight + 1);\n const cleanedBatchBlocks = this.filteredBlockBatch(enqueuingBlocks);\n this.blockDispatcher.enqueueBlocks(\n cleanedBatchBlocks,\n this.getLatestBufferHeight(cleanedBatchBlocks, enqueuingBlocks),\n );\n }\n }\n }\n\n private getLatestBufferHeight(\n cleanedBatchBlocks: number[],\n rawBatchBlocks: number[],\n ): number {\n return Math.max(...cleanedBatchBlocks, ...rawBatchBlocks);\n }\n private filteredBlockBatch(currentBatchBlocks: number[]): number[] {\n if (!this.bypassBlocks.length || !currentBatchBlocks) {\n return currentBatchBlocks;\n }\n\n const cleanedBatch = cleanedBatchBlocks(\n this.bypassBlocks,\n currentBatchBlocks,\n );\n\n const pollutedBlocks = this.bypassBlocks.filter(\n (b) => b < Math.max(...currentBatchBlocks),\n );\n if (pollutedBlocks.length) {\n logger.info(`Bypassing blocks: ${pollutedBlocks}`);\n }\n this.bypassBlocks = without(this.bypassBlocks, ...pollutedBlocks);\n return cleanedBatch;\n }\n\n private nextEndBlockHeight(\n startBlockHeight: number,\n scaledBatchSize: number,\n ): number {\n let endBlockHeight = startBlockHeight + scaledBatchSize - 1;\n\n if (endBlockHeight > this.latestFinalizedHeight) {\n if (this.nodeConfig.unfinalizedBlocks) {\n if (endBlockHeight >= this.latestBestHeight) {\n endBlockHeight = this.latestBestHeight;\n }\n } else {\n endBlockHeight = this.latestFinalizedHeight;\n }\n }\n return endBlockHeight;\n }\n\n private dictionaryValidation(\n dictionary: { _metadata: MetaData },\n startBlockHeight?: number,\n ): boolean {\n if (dictionary !== undefined) {\n const { _metadata: metaData } = dictionary;\n\n if (\n metaData.genesisHash !== this.api.getGenesisHash() &&\n this.evmChainId !== this.api.getChainId().toString()\n ) {\n logger.error(\n 'The dictionary that you have specified does not match the chain you are indexing, it will be ignored. Please update your project manifest to reference the correct dictionary',\n );\n this.dictionaryGenesisMatches = false;\n this.eventEmitter.emit(IndexerEvent.UsingDictionary, {\n value: Number(this.useDictionary),\n });\n this.eventEmitter.emit(IndexerEvent.SkipDictionary);\n return false;\n }\n\n if (startBlockHeight !== undefined) {\n if (metaData.lastProcessedHeight < startBlockHeight) {\n logger.warn(\n `Dictionary indexed block is behind current indexing block height`,\n );\n this.eventEmitter.emit(IndexerEvent.SkipDictionary);\n return false;\n }\n }\n return true;\n }\n return false;\n }\n\n async resetForNewDs(blockHeight: number): Promise<void> {\n await this.syncDynamicDatascourcesFromMeta();\n this.dynamicDsService.deleteTempDsRecords(blockHeight);\n this.updateDictionary();\n this.blockDispatcher.flushQueue(blockHeight);\n }\n\n async resetForIncorrectBestBlock(blockHeight: number): Promise<void> {\n await this.syncDynamicDatascourcesFromMeta();\n this.updateDictionary();\n this.blockDispatcher.flushQueue(blockHeight);\n }\n}\n"]}
@@ -29,7 +29,7 @@ export declare class IndexerManager {
29
29
  }>;
30
30
  start(): Promise<void>;
31
31
  private processUnfinalizedBlocks;
32
- private filterDataSources;
32
+ private assertDataSources;
33
33
  private indexBlockData;
34
34
  private indexBlockContent;
35
35
  private indexExtrinsic;
@@ -56,8 +56,9 @@ let IndexerManager = class IndexerManager {
56
56
  let operationHash = NULL_MERKEL_ROOT;
57
57
  let poiBlockHash;
58
58
  try {
59
- this.filteredDataSources = this.filterDataSources(blockHeight);
60
- const datasources = this.filteredDataSources.concat(...(await this.dynamicDsService.getDynamicDatasources()));
59
+ const datasources = await this.projectService.getAllDataSources(blockHeight);
60
+ // Check that we have valid datasources
61
+ this.assertDataSources(datasources, blockHeight);
61
62
  reindexBlockHeight = await this.processUnfinalizedBlocks(block, tx);
62
63
  await this.indexBlockData(blockContent, datasources,
63
64
  // eslint-disable-next-line @typescript-eslint/require-await
@@ -116,19 +117,12 @@ let IndexerManager = class IndexerManager {
116
117
  }
117
118
  return null;
118
119
  }
119
- filterDataSources(nextProcessingHeight) {
120
- const filteredDs = this.projectService.dataSources.filter((ds) => ds.startBlock <= nextProcessingHeight);
121
- if (filteredDs.length === 0) {
122
- logger.error(`Your start block is greater than the current indexed block height in your database. Either change your startBlock (project.yaml) to <= ${nextProcessingHeight}
120
+ assertDataSources(ds, blockHeight) {
121
+ if (!ds.length) {
122
+ logger.error(`Your start block is greater than the current indexed block height in your database. Either change your startBlock (project.yaml) to <= ${blockHeight}
123
123
  or delete your database and start again from the currently specified startBlock`);
124
124
  process.exit(1);
125
125
  }
126
- // perform filter for custom ds
127
- if (!filteredDs.length) {
128
- logger.error(`Did not find any datasources with associated processor`);
129
- process.exit(1);
130
- }
131
- return filteredDs;
132
126
  }
133
127
  async indexBlockData({ block, logs, transactions }, dataSources, getVM) {
134
128
  await this.indexBlockContent(block, dataSources, getVM);
@@ -1 +1 @@
1
- {"version":3,"file":"indexer.manager.js","sourceRoot":"","sources":["../../src/indexer/indexer.manager.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;AAGtC,2CAAoD;AACpD,yCAAiD;AACjD,4DAUgC;AAChC,gDAU0B;AAQ1B,yCAAmD;AACnD,kEAA+E;AAE/E,+DAAkE;AAClE,oCAAwC;AACxC,iEAGgC;AAChC,6DAAwD;AACxD,uDAAmD;AACnD,uDAAmD;AACnD,2EAAuE;AAEvE,MAAM,gBAAgB,GAAG,IAAA,eAAQ,EAAC,MAAM,CAAC,CAAC;AAE1C,MAAM,MAAM,GAAG,IAAA,qBAAS,EAAC,SAAS,CAAC,CAAC;AAG7B,IAAM,cAAc,GAApB,MAAM,cAAc;IAIzB,YACU,YAA0B,EAC1B,UAAsB,EACtB,UAAsB,EACtB,SAAoB,EACiB,OAAwB,EAC7D,UAAsB,EACtB,cAA8B,EAC9B,gBAAkC,EAClC,wBAAkD,EAClD,kBAAsC,EACtC,cAA8B;QAV9B,iBAAY,GAAZ,YAAY,CAAc;QAC1B,eAAU,GAAV,UAAU,CAAY;QACtB,eAAU,GAAV,UAAU,CAAY;QACtB,cAAS,GAAT,SAAS,CAAW;QACiB,YAAO,GAAP,OAAO,CAAiB;QAC7D,eAAU,GAAV,UAAU,CAAY;QACtB,mBAAc,GAAd,cAAc,CAAgB;QAC9B,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,6BAAwB,GAAxB,wBAAwB,CAA0B;QAClD,uBAAkB,GAAlB,kBAAkB,CAAoB;QACtC,mBAAc,GAAd,cAAc,CAAgB;QAEtC,MAAM,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC;QAErC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC;I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AmB,EAAE,EAAE,CAAC,IAAI;IAChE,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,CAAC,GAAgB,EAAE,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC;IACzE,CAAC,qCAAmB,CAAC,IAAI,CAAC,EAAE,CAAC,GAAgB,EAAE,EAAE,CAC/C,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC;CACjC,CAAC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Block } from '@ethersproject/abstract-provider';\nimport { Inject, Injectable } from '@nestjs/common';\nimport { hexToU8a, u8aEq } from '@polkadot/util';\nimport {\n isBlockHandlerProcessor,\n isCallHandlerProcessor,\n isEventHandlerProcessor,\n isCustomDs,\n isRuntimeDs,\n SubqlEthereumCustomDataSource,\n SubqlCustomHandler,\n EthereumHandlerKind,\n EthereumRuntimeHandlerInputMap,\n} from '@subql/common-ethereum';\nimport {\n ApiService,\n PoiBlock,\n StoreService,\n PoiService,\n NodeConfig,\n getLogger,\n profiler,\n profilerWrap,\n IndexerSandbox,\n} from '@subql/node-core';\nimport {\n EthereumTransaction,\n EthereumLog,\n SubqlRuntimeHandler,\n EthereumBlockWrapper,\n EthereumBlock,\n} from '@subql/types-ethereum';\nimport { Sequelize, Transaction } from 'sequelize';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { EthereumApi } from '../ethereum';\nimport { EthereumBlockWrapped } from '../ethereum/block.ethereum';\nimport { yargsOptions } from '../yargs';\nimport {\n asSecondLayerHandlerProcessor_1_0_0,\n DsProcessorService,\n} from './ds-processor.service';\nimport { DynamicDsService } from './dynamic-ds.service';\nimport { ProjectService } from './project.service';\nimport { SandboxService } from './sandbox.service';\nimport { UnfinalizedBlocksService } from './unfinalizedBlocks.service';\n\nconst NULL_MERKEL_ROOT = hexToU8a('0x00');\n\nconst logger = getLogger('indexer');\n\n@Injectable()\nexport class IndexerManager {\n private api: EthereumApi;\n private filteredDataSources: SubqlProjectDs[];\n\n constructor(\n private storeService: StoreService,\n private apiService: ApiService,\n private poiService: PoiService,\n private sequelize: Sequelize,\n @Inject('ISubqueryProject') private readonly project: SubqueryProject,\n private nodeConfig: NodeConfig,\n private sandboxService: SandboxService,\n private dynamicDsService: DynamicDsService,\n private unfinalizedBlocksService: UnfinalizedBlocksService,\n private dsProcessorService: DsProcessorService,\n private projectService: ProjectService,\n ) {\n logger.info('indexer manager start');\n\n this.api = this.apiService.api;\n }\n\n @profiler(yargsOptions.argv.profiler)\n async indexBlock(blockContent: EthereumBlockWrapper): Promise<{\n dynamicDsCreated: boolean;\n operationHash: Uint8Array;\n reindexBlockHeight: null;\n }> {\n const { block, blockHeight } = blockContent;\n let dynamicDsCreated = false;\n let reindexBlockHeight = null;\n const tx = await this.sequelize.transaction();\n this.storeService.setTransaction(tx);\n this.storeService.setBlockHeight(blockHeight);\n\n let operationHash = NULL_MERKEL_ROOT;\n let poiBlockHash: Uint8Array;\n\n try {\n this.filteredDataSources = this.filterDataSources(blockHeight);\n\n const datasources = this.filteredDataSources.concat(\n ...(await this.dynamicDsService.getDynamicDatasources()),\n );\n\n reindexBlockHeight = await this.processUnfinalizedBlocks(block, tx);\n\n await this.indexBlockData(\n blockContent,\n datasources,\n // eslint-disable-next-line @typescript-eslint/require-await\n async (ds: SubqlProjectDs) => {\n const vm = this.sandboxService.getDsProcessorWrapper(\n ds,\n this.api,\n blockContent,\n );\n\n // Inject function to create ds into vm\n vm.freeze(\n async (templateName: string, args?: Record<string, unknown>) => {\n const newDs = await this.dynamicDsService.createDynamicDatasource(\n {\n templateName,\n args,\n startBlock: blockHeight,\n },\n tx,\n );\n // Push the newly created dynamic ds to be processed this block on any future extrinsics/events\n datasources.push(newDs);\n dynamicDsCreated = true;\n },\n 'createDynamicDatasource',\n );\n\n return vm;\n },\n );\n\n await this.storeService.setMetadataBatch(\n [\n { key: 'lastProcessedHeight', value: blockHeight },\n { key: 'lastProcessedTimestamp', value: Date.now() },\n ],\n { transaction: tx },\n );\n // Db Metadata increase BlockCount, in memory ref to block-dispatcher _processedBlockCount\n await this.storeService.incrementJsonbCount('processedBlockCount', tx);\n\n // Need calculate operationHash to ensure correct offset insert all time\n operationHash = this.storeService.getOperationMerkleRoot();\n if (this.nodeConfig.proofOfIndex) {\n //check if operation is null, then poi will not be inserted\n if (!u8aEq(operationHash, NULL_MERKEL_ROOT)) {\n const poiBlock = PoiBlock.create(\n blockHeight,\n blockContent.block.hash,\n operationHash,\n await this.poiService.getLatestPoiBlockHash(),\n this.project.id,\n );\n poiBlockHash = poiBlock.hash;\n await this.storeService.setPoi(poiBlock, { transaction: tx });\n this.poiService.setLatestPoiBlockHash(poiBlockHash);\n await this.storeService.setMetadataBatch(\n [{ key: 'lastPoiHeight', value: blockHeight }],\n { transaction: tx },\n );\n }\n }\n } catch (e) {\n await tx.rollback();\n throw e;\n }\n\n await tx.commit();\n\n return {\n dynamicDsCreated,\n operationHash,\n reindexBlockHeight,\n };\n }\n\n async start(): Promise<void> {\n await this.projectService.init();\n logger.info('indexer manager started');\n }\n\n private async processUnfinalizedBlocks(\n block: EthereumBlock,\n tx: Transaction,\n ): Promise<number | null> {\n if (this.nodeConfig.unfinalizedBlocks) {\n return this.unfinalizedBlocksService.processUnfinalizedBlocks(block, tx);\n }\n return null;\n }\n\n private filterDataSources(nextProcessingHeight: number): SubqlProjectDs[] {\n const filteredDs = this.projectService.dataSources.filter(\n (ds) => ds.startBlock <= nextProcessingHeight,\n );\n\n if (filteredDs.length === 0) {\n logger.error(\n `Your start block is greater than the current indexed block height in your database. Either change your startBlock (project.yaml) to <= ${nextProcessingHeight}\n or delete your database and start again from the currently specified startBlock`,\n );\n process.exit(1);\n }\n // perform filter for custom ds\n if (!filteredDs.length) {\n logger.error(`Did not find any datasources with associated processor`);\n process.exit(1);\n }\n return filteredDs;\n }\n\n private async indexBlockData(\n { block, logs, transactions }: EthereumBlockWrapper,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n await this.indexBlockContent(block, dataSources, getVM);\n\n for (const tx of transactions) {\n await this.indexExtrinsic(tx, dataSources, getVM);\n\n for (const log of logs.filter((l) => l.transactionHash === tx.hash)) {\n await this.indexEvent(log, dataSources, getVM);\n }\n }\n }\n\n private async indexBlockContent(\n block: EthereumBlock,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Block, block, ds, getVM);\n }\n }\n\n private async indexExtrinsic(\n tx: EthereumTransaction,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Call, tx, ds, getVM);\n }\n }\n\n private async indexEvent(\n log: EthereumLog,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Event, log, ds, getVM);\n }\n }\n\n private async indexData<K extends EthereumHandlerKind>(\n kind: K,\n data: EthereumRuntimeHandlerInputMap[K],\n ds: SubqlProjectDs,\n getVM: (ds: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n let vm: IndexerSandbox;\n if (isRuntimeDs(ds)) {\n const handlers = (ds.mapping.handlers as SubqlRuntimeHandler[]).filter(\n (h) =>\n h.kind === kind &&\n FilterTypeMap[kind](\n data as any,\n h.filter as any,\n ds.options?.address,\n ),\n );\n\n if (!handlers.length) {\n return;\n }\n const parsedData = await DataAbiParser[kind](this.api)(data, ds);\n\n for (const handler of handlers) {\n vm = vm ?? (await getVM(ds));\n this.nodeConfig.profiler\n ? await profilerWrap(\n vm.securedExec.bind(vm),\n 'handlerPerformance',\n handler.handler,\n )(handler.handler, [parsedData])\n : await vm.securedExec(handler.handler, [parsedData]);\n }\n } else if (isCustomDs(ds)) {\n const handlers = this.filterCustomDsHandlers<K>(\n ds,\n data,\n ProcessorTypeMap[kind],\n (data, baseFilter) => {\n switch (kind) {\n case EthereumHandlerKind.Block:\n return EthereumBlockWrapped.filterBlocksProcessor(\n data as EthereumBlock,\n baseFilter,\n );\n case EthereumHandlerKind.Call:\n return EthereumBlockWrapped.filterTransactionsProcessor(\n data as EthereumTransaction,\n baseFilter,\n );\n case EthereumHandlerKind.Event:\n return EthereumBlockWrapped.filterLogsProcessor(\n data as EthereumLog,\n baseFilter,\n );\n default:\n throw new Error('Unsupported handler kind');\n }\n },\n );\n\n if (!handlers.length) {\n return;\n }\n\n const parsedData = await DataAbiParser[kind](this.api)(data, ds);\n\n for (const handler of handlers) {\n vm = vm ?? (await getVM(ds));\n await this.transformAndExecuteCustomDs(ds, vm, handler, parsedData);\n }\n }\n }\n\n private filterCustomDsHandlers<K extends EthereumHandlerKind>(\n ds: SubqlEthereumCustomDataSource<string, any>,\n data: EthereumRuntimeHandlerInputMap[K],\n baseHandlerCheck: ProcessorTypeMap[K],\n baseFilter: (\n data: EthereumRuntimeHandlerInputMap[K],\n baseFilter: any,\n ) => boolean,\n ): SubqlCustomHandler[] {\n const plugin = this.dsProcessorService.getDsProcessor(ds);\n\n return ds.mapping.handlers\n .filter((handler) => {\n const processor = plugin.handlerProcessors[handler.kind];\n if (baseHandlerCheck(processor)) {\n processor.baseFilter;\n\n return baseFilter(data, processor.baseFilter);\n }\n return false;\n })\n .filter((handler) => {\n const processor = asSecondLayerHandlerProcessor_1_0_0(\n plugin.handlerProcessors[handler.kind],\n );\n\n try {\n return processor.filterProcessor({\n filter: handler.filter,\n input: data,\n ds,\n });\n } catch (e) {\n logger.error(e, 'Failed to run ds processer filter.');\n throw e;\n }\n });\n }\n\n private async transformAndExecuteCustomDs<K extends EthereumHandlerKind>(\n ds: SubqlEthereumCustomDataSource<string, any>,\n vm: IndexerSandbox,\n handler: SubqlCustomHandler,\n data: EthereumRuntimeHandlerInputMap[K],\n ): Promise<void> {\n const plugin = this.dsProcessorService.getDsProcessor(ds);\n const assets = await this.dsProcessorService.getAssets(ds);\n\n const processor = asSecondLayerHandlerProcessor_1_0_0(\n plugin.handlerProcessors[handler.kind],\n );\n\n const transformedData = await processor\n .transformer({\n input: data,\n ds,\n api: this.api,\n filter: handler.filter,\n assets,\n })\n .catch((e) => {\n logger.error(e, 'Failed to transform data with ds processor.');\n throw e;\n });\n\n // We can not run this in parallel. the transformed data items may be dependent on one another.\n // An example of this is with Acala EVM packing multiple EVM logs into a single Substrate event\n for (const _data of transformedData) {\n await vm.securedExec(handler.handler, [_data]);\n }\n }\n}\n\ntype ProcessorTypeMap = {\n [EthereumHandlerKind.Block]: typeof isBlockHandlerProcessor;\n [EthereumHandlerKind.Event]: typeof isEventHandlerProcessor;\n [EthereumHandlerKind.Call]: typeof isCallHandlerProcessor;\n};\n\nconst ProcessorTypeMap = {\n [EthereumHandlerKind.Block]: isBlockHandlerProcessor,\n [EthereumHandlerKind.Event]: isEventHandlerProcessor,\n [EthereumHandlerKind.Call]: isCallHandlerProcessor,\n};\n\nconst FilterTypeMap = {\n [EthereumHandlerKind.Block]: EthereumBlockWrapped.filterBlocksProcessor,\n [EthereumHandlerKind.Event]: EthereumBlockWrapped.filterLogsProcessor,\n [EthereumHandlerKind.Call]: EthereumBlockWrapped.filterTransactionsProcessor,\n};\n\nconst DataAbiParser = {\n [EthereumHandlerKind.Block]: () => (data: EthereumBlock) => data,\n [EthereumHandlerKind.Event]: (api: EthereumApi) => api.parseLog.bind(api),\n [EthereumHandlerKind.Call]: (api: EthereumApi) =>\n api.parseTransaction.bind(api),\n};\n"]}
1
+ {"version":3,"file":"indexer.manager.js","sourceRoot":"","sources":["../../src/indexer/indexer.manager.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;AAGtC,2CAAoD;AACpD,yCA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CAAkB;QACtB,gCAAc;GAf7B,cAAc,CAsV1B;AAtVY,wCAAc;AA8V3B,MAAM,gBAAgB,GAAG;IACvB,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,yCAAuB;IACpD,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,yCAAuB;IACpD,CAAC,qCAAmB,CAAC,IAAI,CAAC,EAAE,wCAAsB;CACnD,CAAC;AAEF,MAAM,aAAa,GAAG;IACpB,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,qCAAoB,CAAC,qBAAqB;IACvE,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,qCAAoB,CAAC,mBAAmB;IACrE,CAAC,qCAAmB,CAAC,IAAI,CAAC,EAAE,qCAAoB,CAAC,2BAA2B;CAC7E,CAAC;AAEF,MAAM,aAAa,GAAG;IACpB,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,IAAmB,EAAE,EAAE,CAAC,IAAI;IAChE,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,CAAC,GAAgB,EAAE,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC;IACzE,CAAC,qCAAmB,CAAC,IAAI,CAAC,EAAE,CAAC,GAAgB,EAAE,EAAE,CAC/C,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC;CACjC,CAAC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Block } from '@ethersproject/abstract-provider';\nimport { Inject, Injectable } from '@nestjs/common';\nimport { hexToU8a, u8aEq } from '@polkadot/util';\nimport {\n isBlockHandlerProcessor,\n isCallHandlerProcessor,\n isEventHandlerProcessor,\n isCustomDs,\n isRuntimeDs,\n SubqlEthereumCustomDataSource,\n SubqlCustomHandler,\n EthereumHandlerKind,\n EthereumRuntimeHandlerInputMap,\n} from '@subql/common-ethereum';\nimport {\n ApiService,\n PoiBlock,\n StoreService,\n PoiService,\n NodeConfig,\n getLogger,\n profiler,\n profilerWrap,\n IndexerSandbox,\n} from '@subql/node-core';\nimport {\n EthereumTransaction,\n EthereumLog,\n SubqlRuntimeHandler,\n EthereumBlockWrapper,\n EthereumBlock,\n} from '@subql/types-ethereum';\nimport { Sequelize, Transaction } from 'sequelize';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { EthereumApi } from '../ethereum';\nimport { EthereumBlockWrapped } from '../ethereum/block.ethereum';\nimport { yargsOptions } from '../yargs';\nimport {\n asSecondLayerHandlerProcessor_1_0_0,\n DsProcessorService,\n} from './ds-processor.service';\nimport { DynamicDsService } from './dynamic-ds.service';\nimport { ProjectService } from './project.service';\nimport { SandboxService } from './sandbox.service';\nimport { UnfinalizedBlocksService } from './unfinalizedBlocks.service';\n\nconst NULL_MERKEL_ROOT = hexToU8a('0x00');\n\nconst logger = getLogger('indexer');\n\n@Injectable()\nexport class IndexerManager {\n private api: EthereumApi;\n private filteredDataSources: SubqlProjectDs[];\n\n constructor(\n private storeService: StoreService,\n private apiService: ApiService,\n private poiService: PoiService,\n private sequelize: Sequelize,\n @Inject('ISubqueryProject') private readonly project: SubqueryProject,\n private nodeConfig: NodeConfig,\n private sandboxService: SandboxService,\n private dynamicDsService: DynamicDsService,\n private unfinalizedBlocksService: UnfinalizedBlocksService,\n private dsProcessorService: DsProcessorService,\n private projectService: ProjectService,\n ) {\n logger.info('indexer manager start');\n\n this.api = this.apiService.api;\n }\n\n @profiler(yargsOptions.argv.profiler)\n async indexBlock(blockContent: EthereumBlockWrapper): Promise<{\n dynamicDsCreated: boolean;\n operationHash: Uint8Array;\n reindexBlockHeight: null;\n }> {\n const { block, blockHeight } = blockContent;\n let dynamicDsCreated = false;\n let reindexBlockHeight = null;\n const tx = await this.sequelize.transaction();\n this.storeService.setTransaction(tx);\n this.storeService.setBlockHeight(blockHeight);\n\n let operationHash = NULL_MERKEL_ROOT;\n let poiBlockHash: Uint8Array;\n\n try {\n const datasources = await this.projectService.getAllDataSources(\n blockHeight,\n );\n\n // Check that we have valid datasources\n this.assertDataSources(datasources, blockHeight);\n\n reindexBlockHeight = await this.processUnfinalizedBlocks(block, tx);\n\n await this.indexBlockData(\n blockContent,\n datasources,\n // eslint-disable-next-line @typescript-eslint/require-await\n async (ds: SubqlProjectDs) => {\n const vm = this.sandboxService.getDsProcessorWrapper(\n ds,\n this.api,\n blockContent,\n );\n\n // Inject function to create ds into vm\n vm.freeze(\n async (templateName: string, args?: Record<string, unknown>) => {\n const newDs = await this.dynamicDsService.createDynamicDatasource(\n {\n templateName,\n args,\n startBlock: blockHeight,\n },\n tx,\n );\n // Push the newly created dynamic ds to be processed this block on any future extrinsics/events\n datasources.push(newDs);\n dynamicDsCreated = true;\n },\n 'createDynamicDatasource',\n );\n\n return vm;\n },\n );\n\n await this.storeService.setMetadataBatch(\n [\n { key: 'lastProcessedHeight', value: blockHeight },\n { key: 'lastProcessedTimestamp', value: Date.now() },\n ],\n { transaction: tx },\n );\n // Db Metadata increase BlockCount, in memory ref to block-dispatcher _processedBlockCount\n await this.storeService.incrementJsonbCount('processedBlockCount', tx);\n\n // Need calculate operationHash to ensure correct offset insert all time\n operationHash = this.storeService.getOperationMerkleRoot();\n if (this.nodeConfig.proofOfIndex) {\n //check if operation is null, then poi will not be inserted\n if (!u8aEq(operationHash, NULL_MERKEL_ROOT)) {\n const poiBlock = PoiBlock.create(\n blockHeight,\n blockContent.block.hash,\n operationHash,\n await this.poiService.getLatestPoiBlockHash(),\n this.project.id,\n );\n poiBlockHash = poiBlock.hash;\n await this.storeService.setPoi(poiBlock, { transaction: tx });\n this.poiService.setLatestPoiBlockHash(poiBlockHash);\n await this.storeService.setMetadataBatch(\n [{ key: 'lastPoiHeight', value: blockHeight }],\n { transaction: tx },\n );\n }\n }\n } catch (e) {\n await tx.rollback();\n throw e;\n }\n\n await tx.commit();\n\n return {\n dynamicDsCreated,\n operationHash,\n reindexBlockHeight,\n };\n }\n\n async start(): Promise<void> {\n await this.projectService.init();\n logger.info('indexer manager started');\n }\n\n private async processUnfinalizedBlocks(\n block: EthereumBlock,\n tx: Transaction,\n ): Promise<number | null> {\n if (this.nodeConfig.unfinalizedBlocks) {\n return this.unfinalizedBlocksService.processUnfinalizedBlocks(block, tx);\n }\n return null;\n }\n\n private assertDataSources(ds: SubqlProjectDs[], blockHeight: number) {\n if (!ds.length) {\n logger.error(\n `Your start block is greater than the current indexed block height in your database. Either change your startBlock (project.yaml) to <= ${blockHeight}\n or delete your database and start again from the currently specified startBlock`,\n );\n process.exit(1);\n }\n }\n\n private async indexBlockData(\n { block, logs, transactions }: EthereumBlockWrapper,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n await this.indexBlockContent(block, dataSources, getVM);\n\n for (const tx of transactions) {\n await this.indexExtrinsic(tx, dataSources, getVM);\n\n for (const log of logs.filter((l) => l.transactionHash === tx.hash)) {\n await this.indexEvent(log, dataSources, getVM);\n }\n }\n }\n\n private async indexBlockContent(\n block: EthereumBlock,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Block, block, ds, getVM);\n }\n }\n\n private async indexExtrinsic(\n tx: EthereumTransaction,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Call, tx, ds, getVM);\n }\n }\n\n private async indexEvent(\n log: EthereumLog,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Event, log, ds, getVM);\n }\n }\n\n private async indexData<K extends EthereumHandlerKind>(\n kind: K,\n data: EthereumRuntimeHandlerInputMap[K],\n ds: SubqlProjectDs,\n getVM: (ds: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n let vm: IndexerSandbox;\n if (isRuntimeDs(ds)) {\n const handlers = (ds.mapping.handlers as SubqlRuntimeHandler[]).filter(\n (h) =>\n h.kind === kind &&\n FilterTypeMap[kind](\n data as any,\n h.filter as any,\n ds.options?.address,\n ),\n );\n\n if (!handlers.length) {\n return;\n }\n const parsedData = await DataAbiParser[kind](this.api)(data, ds);\n\n for (const handler of handlers) {\n vm = vm ?? (await getVM(ds));\n this.nodeConfig.profiler\n ? await profilerWrap(\n vm.securedExec.bind(vm),\n 'handlerPerformance',\n handler.handler,\n )(handler.handler, [parsedData])\n : await vm.securedExec(handler.handler, [parsedData]);\n }\n } else if (isCustomDs(ds)) {\n const handlers = this.filterCustomDsHandlers<K>(\n ds,\n data,\n ProcessorTypeMap[kind],\n (data, baseFilter) => {\n switch (kind) {\n case EthereumHandlerKind.Block:\n return EthereumBlockWrapped.filterBlocksProcessor(\n data as EthereumBlock,\n baseFilter,\n );\n case EthereumHandlerKind.Call:\n return EthereumBlockWrapped.filterTransactionsProcessor(\n data as EthereumTransaction,\n baseFilter,\n );\n case EthereumHandlerKind.Event:\n return EthereumBlockWrapped.filterLogsProcessor(\n data as EthereumLog,\n baseFilter,\n );\n default:\n throw new Error('Unsupported handler kind');\n }\n },\n );\n\n if (!handlers.length) {\n return;\n }\n\n const parsedData = await DataAbiParser[kind](this.api)(data, ds);\n\n for (const handler of handlers) {\n vm = vm ?? (await getVM(ds));\n await this.transformAndExecuteCustomDs(ds, vm, handler, parsedData);\n }\n }\n }\n\n private filterCustomDsHandlers<K extends EthereumHandlerKind>(\n ds: SubqlEthereumCustomDataSource<string, any>,\n data: EthereumRuntimeHandlerInputMap[K],\n baseHandlerCheck: ProcessorTypeMap[K],\n baseFilter: (\n data: EthereumRuntimeHandlerInputMap[K],\n baseFilter: any,\n ) => boolean,\n ): SubqlCustomHandler[] {\n const plugin = this.dsProcessorService.getDsProcessor(ds);\n\n return ds.mapping.handlers\n .filter((handler) => {\n const processor = plugin.handlerProcessors[handler.kind];\n if (baseHandlerCheck(processor)) {\n processor.baseFilter;\n\n return baseFilter(data, processor.baseFilter);\n }\n return false;\n })\n .filter((handler) => {\n const processor = asSecondLayerHandlerProcessor_1_0_0(\n plugin.handlerProcessors[handler.kind],\n );\n\n try {\n return processor.filterProcessor({\n filter: handler.filter,\n input: data,\n ds,\n });\n } catch (e) {\n logger.error(e, 'Failed to run ds processer filter.');\n throw e;\n }\n });\n }\n\n private async transformAndExecuteCustomDs<K extends EthereumHandlerKind>(\n ds: SubqlEthereumCustomDataSource<string, any>,\n vm: IndexerSandbox,\n handler: SubqlCustomHandler,\n data: EthereumRuntimeHandlerInputMap[K],\n ): Promise<void> {\n const plugin = this.dsProcessorService.getDsProcessor(ds);\n const assets = await this.dsProcessorService.getAssets(ds);\n\n const processor = asSecondLayerHandlerProcessor_1_0_0(\n plugin.handlerProcessors[handler.kind],\n );\n\n const transformedData = await processor\n .transformer({\n input: data,\n ds,\n api: this.api,\n filter: handler.filter,\n assets,\n })\n .catch((e) => {\n logger.error(e, 'Failed to transform data with ds processor.');\n throw e;\n });\n\n // We can not run this in parallel. the transformed data items may be dependent on one another.\n // An example of this is with Acala EVM packing multiple EVM logs into a single Substrate event\n for (const _data of transformedData) {\n await vm.securedExec(handler.handler, [_data]);\n }\n }\n}\n\ntype ProcessorTypeMap = {\n [EthereumHandlerKind.Block]: typeof isBlockHandlerProcessor;\n [EthereumHandlerKind.Event]: typeof isEventHandlerProcessor;\n [EthereumHandlerKind.Call]: typeof isCallHandlerProcessor;\n};\n\nconst ProcessorTypeMap = {\n [EthereumHandlerKind.Block]: isBlockHandlerProcessor,\n [EthereumHandlerKind.Event]: isEventHandlerProcessor,\n [EthereumHandlerKind.Call]: isCallHandlerProcessor,\n};\n\nconst FilterTypeMap = {\n [EthereumHandlerKind.Block]: EthereumBlockWrapped.filterBlocksProcessor,\n [EthereumHandlerKind.Event]: EthereumBlockWrapped.filterLogsProcessor,\n [EthereumHandlerKind.Call]: EthereumBlockWrapped.filterTransactionsProcessor,\n};\n\nconst DataAbiParser = {\n [EthereumHandlerKind.Block]: () => (data: EthereumBlock) => data,\n [EthereumHandlerKind.Event]: (api: EthereumApi) => api.parseLog.bind(api),\n [EthereumHandlerKind.Call]: (api: EthereumApi) =>\n api.parseTransaction.bind(api),\n};\n"]}
@@ -45,4 +45,5 @@ export declare class ProjectService {
45
45
  getProcessedBlockCount(): Promise<number>;
46
46
  private getStartBlockFromDataSources;
47
47
  reindex(targetBlockHeight: number): Promise<void>;
48
+ getAllDataSources(blockHeight: number): Promise<SubqlProjectDs[]>;
48
49
  }
@@ -297,6 +297,10 @@ let ProjectService = class ProjectService {
297
297
  const lastProcessedHeight = await this.getLastProcessedHeight();
298
298
  return (0, reindex_1.reindex)(this.getStartBlockFromDataSources(), await this.getMetadataBlockOffset(), targetBlockHeight, lastProcessedHeight, this.storeService, this.dynamicDsService, this.mmrService, this.sequelize);
299
299
  }
300
+ async getAllDataSources(blockHeight) {
301
+ const dynamicDs = await this.dynamicDsService.getDynamicDatasources();
302
+ return [...this.dataSources, ...dynamicDs].filter((ds) => ds.startBlock <= blockHeight);
303
+ }
300
304
  };
301
305
  ProjectService = __decorate([
302
306
  (0, common_1.Injectable)(),
@@ -1 +1 @@
1
- {"version":3,"file":"project.service.js","sourceRoot":"","sources":["../../src/indexer/project.service.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;;;;AAEtC,oDAA4B;AAC5B,mDAA8C;AAC9C,2CAAoD;AACpD,yDAAsD;AACtD,gDAY0B;AAC1B,yCAAsC;AACtC,kEAIsC;AACtC,8CAAqE;AACrE,8CAA2C;AAC3C,iEAA4D;AAC5D,6DAAwD;AAExD,2EAIqC;AAErC,8DAA8D;AAC9D,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAA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QAEhE,OAAO,IAAA,iBAAO,EACZ,IAAI,CAAC,4BAA4B,EAAE,EACnC,MAAM,IAAI,CAAC,sBAAsB,EAAE,EACnC,iBAAiB,EACjB,mBAAmB,EACnB,IAAI,CAAC,YAAY,EACjB,IAAI,CAAC,gBAAgB,EACrB,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,SAAS,CAEf,CAAC;IACJ,CAAC;CACF,CAAA;AAhWY,cAAc;IAD1B,IAAA,mBAAU,GAAE;IAaR,WAAA,IAAA,eAAM,EAAC,kBAAkB,CAAC,CAAA;qCALU,yCAAkB;QAC1B,sBAAU;QACV,sBAAU;QACR,sBAAU;QACb,qBAAS;QACiB,iCAAe;QACtC,wBAAY;QACd,sBAAU;QACJ,qCAAgB;QAC7B,6BAAa;QACF,oDAAwB;GAjBhD,cAAc,CAgW1B;AAhWY,wCAAc","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport assert from 'assert';\nimport { isMainThread } from 'worker_threads';\nimport { Inject, Injectable } from '@nestjs/common';\nimport { EventEmitter2 } from '@nestjs/event-emitter';\nimport {\n ApiService,\n MetadataFactory,\n MetadataRepo,\n NodeConfig,\n IndexerEvent,\n StoreService,\n PoiService,\n MmrService,\n getLogger,\n getExistingProjectSchema,\n getMetaDataInfo,\n} from '@subql/node-core';\nimport { Sequelize } from 'sequelize';\nimport {\n generateTimestampReferenceForBlockFilters,\n SubqlProjectDs,\n SubqueryProject,\n} from '../configure/SubqueryProject';\nimport { initDbSchema, initHotSchemaReload } from '../utils/project';\nimport { reindex } from '../utils/reindex';\nimport { DsProcessorService } from './ds-processor.service';\nimport { DynamicDsService } from './dynamic-ds.service';\nimport { BestBlocks } from './types';\nimport {\n METADATA_LAST_FINALIZED_PROCESSED_KEY,\n METADATA_UNFINALIZED_BLOCKS_KEY,\n UnfinalizedBlocksService,\n} from './unfinalizedBlocks.service';\n\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nconst { version: packageVersion } = require('../../package.json');\n\nconst DEFAULT_DB_SCHEMA = 'public';\n\nconst logger = getLogger('Project');\n\n@Injectable()\nexport class ProjectService {\n private _schema: string;\n private metadataRepo: MetadataRepo;\n private _startHeight: number;\n private _blockOffset: number;\n\n constructor(\n private readonly dsProcessorService: DsProcessorService,\n private readonly apiService: ApiService,\n private readonly poiService: PoiService,\n protected readonly mmrService: MmrService,\n private readonly sequelize: Sequelize,\n @Inject('ISubqueryProject') private readonly project: SubqueryProject,\n private readonly storeService: StoreService,\n private readonly nodeConfig: NodeConfig,\n private readonly dynamicDsService: DynamicDsService,\n private eventEmitter: EventEmitter2,\n private unfinalizedBlockService: UnfinalizedBlocksService,\n ) {}\n\n get schema(): string {\n return this._schema;\n }\n\n get dataSources(): SubqlProjectDs[] {\n return this.project.dataSources;\n }\n\n get blockOffset(): number {\n return this._blockOffset;\n }\n\n get startHeight(): number {\n return this._startHeight;\n }\n\n get isHistorical(): boolean {\n return this.storeService.historical;\n }\n\n // eslint-disable-next-line @typescript-eslint/require-await\n private async getExistingProjectSchema(): Promise<string> {\n return getExistingProjectSchema(this.nodeConfig, this.sequelize);\n }\n\n async init(): Promise<void> {\n // Used to load assets into DS-processor, has to be done in any thread\n await this.dsProcessorService.validateProjectCustomDatasources();\n // Do extra work on main thread to setup stuff\n this.project.dataSources = await generateTimestampReferenceForBlockFilters(\n this.project.dataSources,\n this.apiService.api,\n );\n if (isMainThread) {\n this._schema = await this.ensureProject();\n await this.initDbSchema();\n this.metadataRepo = await this.ensureMetadata();\n this.dynamicDsService.init(this.metadataRepo);\n\n await this.initHotSchemaReload();\n\n if (this.nodeConfig.proofOfIndex) {\n const blockOffset = await this.getMetadataBlockOffset();\n void this.setBlockOffset(Number(blockOffset));\n await this.poiService.init(this.schema);\n }\n\n this._startHeight = await this.getStartHeight();\n } else {\n this._schema = await this.getExistingProjectSchema();\n this.metadataRepo = await MetadataFactory(\n this.sequelize,\n this.schema,\n this.nodeConfig.multiChain,\n this.project.network.chainId,\n );\n\n this.dynamicDsService.init(this.metadataRepo);\n\n await this.sequelize.sync();\n\n assert(this._schema, 'Schema should be created in main thread');\n await this.initDbSchema();\n\n if (this.nodeConfig.proofOfIndex) {\n await this.poiService.init(this.schema);\n }\n }\n\n if (this.nodeConfig.unfinalizedBlocks && !this.isHistorical) {\n logger.error(\n 'Unfinalized blocks cannot be enabled without historical. You will need to reindex your project to enable historical',\n );\n process.exit(1);\n }\n\n const reindexedTo = await this.unfinalizedBlockService.init(\n this.metadataRepo,\n this.reindex.bind(this),\n );\n\n if (reindexedTo !== undefined) {\n this._startHeight = reindexedTo;\n }\n }\n\n private async ensureProject(): Promise<string> {\n let schema = await this.getExistingProjectSchema();\n if (!schema) {\n schema = await this.createProjectSchema();\n }\n this.eventEmitter.emit(IndexerEvent.Ready, {\n value: true,\n });\n\n return schema;\n }\n\n private async createProjectSchema(): Promise<string> {\n let schema: string;\n if (this.nodeConfig.localMode) {\n // create tables in default schema if local mode is enabled\n schema = DEFAULT_DB_SCHEMA;\n } else {\n schema = this.nodeConfig.dbSchema;\n const schemas = await this.sequelize.showAllSchemas(undefined);\n if (!(schemas as unknown as string[]).includes(schema)) {\n await this.sequelize.createSchema(`\"${schema}\"`, undefined);\n }\n }\n\n return schema;\n }\n\n private async initHotSchemaReload(): Promise<void> {\n await initHotSchemaReload(this.schema, this.storeService);\n }\n private async initDbSchema(): Promise<void> {\n await initDbSchema(this.project, this.schema, this.storeService);\n }\n\n private async ensureMetadata(): Promise<MetadataRepo> {\n const metadataRepo = await MetadataFactory(\n this.sequelize,\n this.schema,\n this.nodeConfig.multiChain,\n this.project.network.chainId,\n );\n\n this.eventEmitter.emit(\n IndexerEvent.NetworkMetadata,\n this.apiService.networkMeta,\n );\n\n const keys = [\n 'lastProcessedHeight',\n 'blockOffset',\n 'indexerNodeVersion',\n 'chain',\n 'specName',\n 'genesisHash',\n 'startHeight',\n 'chainId',\n 'processedBlockCount',\n 'lastFinalizedVerifiedHeight',\n 'schemaMigrationCount',\n 'unfinalizedBlocks',\n 'bypassBlocks',\n ] as const;\n\n const entries = await metadataRepo.findAll({\n where: {\n key: keys,\n },\n });\n\n const keyValue = entries.reduce((arr, curr) => {\n arr[curr.key] = curr.value;\n return arr;\n }, {} as { [key in typeof keys[number]]: string | boolean | number });\n\n const { chain, genesisHash, specName } = this.apiService.networkMeta;\n\n if (this.project.runner) {\n await Promise.all([\n metadataRepo.upsert({\n key: 'runnerNode',\n value: this.project.runner.node.name,\n }),\n metadataRepo.upsert({\n key: 'runnerNodeVersion',\n value: this.project.runner.node.version,\n }),\n metadataRepo.upsert({\n key: 'runnerQuery',\n value: this.project.runner.query.name,\n }),\n metadataRepo.upsert({\n key: 'runnerQueryVersion',\n value: this.project.runner.query.version,\n }),\n ]);\n }\n if (!keyValue.genesisHash) {\n await metadataRepo.upsert({ key: 'genesisHash', value: genesisHash });\n } else {\n // Check if the configured genesisHash matches the currently stored genesisHash\n assert(\n // Configured project yaml genesisHash only exists in specVersion v0.2.0, fallback to api fetched genesisHash on v0.0.1\n (this.project.network.genesisHash ?? genesisHash) ===\n keyValue.genesisHash,\n 'Specified project manifest chain id / genesis hash does not match database stored genesis hash, consider cleaning project schema using --force-clean',\n );\n }\n if (keyValue.chain !== chain) {\n await metadataRepo.upsert({ key: 'chain', value: chain });\n }\n\n if (keyValue.specName !== specName) {\n await metadataRepo.upsert({ key: 'specName', value: specName });\n }\n\n // If project was created before this feature, don't add the key. If it is project created after, add this key.\n if (!keyValue.processedBlockCount && !keyValue.lastProcessedHeight) {\n await metadataRepo.upsert({ key: 'processedBlockCount', value: 0 });\n }\n\n if (keyValue.indexerNodeVersion !== packageVersion) {\n await metadataRepo.upsert({\n key: 'indexerNodeVersion',\n value: packageVersion,\n });\n }\n if (!keyValue.schemaMigrationCount) {\n await metadataRepo.upsert({ key: 'schemaMigrationCount', value: 0 });\n }\n\n if (!keyValue.unfinalizedBlocks) {\n await metadataRepo.upsert({\n key: 'unfinalizedBlocks',\n value: '{}',\n });\n }\n if (!keyValue.startHeight) {\n await metadataRepo.upsert({\n key: 'startHeight',\n value: this.getStartBlockFromDataSources(),\n });\n }\n\n return metadataRepo;\n }\n\n async upsertMetadataBlockOffset(height: number): Promise<void> {\n await this.metadataRepo.upsert({\n key: 'blockOffset',\n value: height,\n });\n }\n\n async getMetadataUnfinalizedBlocks(): Promise<BestBlocks | undefined> {\n const val = await getMetaDataInfo<string>(\n this.metadataRepo,\n METADATA_UNFINALIZED_BLOCKS_KEY,\n );\n if (val) {\n return JSON.parse(val) as BestBlocks;\n }\n return undefined;\n }\n\n async getLastFinalizedVerifiedHeight(): Promise<number | undefined> {\n return getMetaDataInfo(\n this.metadataRepo,\n METADATA_LAST_FINALIZED_PROCESSED_KEY,\n );\n }\n\n async getMetadataBlockOffset(): Promise<number | undefined> {\n return getMetaDataInfo(this.metadataRepo, 'blockOffset');\n }\n\n async getLastProcessedHeight(): Promise<number | undefined> {\n return getMetaDataInfo(this.metadataRepo, 'lastProcessedHeight');\n }\n\n private async getStartHeight(): Promise<number> {\n let startHeight: number;\n const lastProcessedHeight = await this.getLastProcessedHeight();\n if (lastProcessedHeight !== null && lastProcessedHeight !== undefined) {\n startHeight = Number(lastProcessedHeight) + 1;\n } else {\n startHeight = this.getStartBlockFromDataSources();\n }\n return startHeight;\n }\n\n async setBlockOffset(offset: number): Promise<void> {\n if (\n this._blockOffset ||\n offset === null ||\n offset === undefined ||\n isNaN(offset)\n ) {\n return;\n }\n logger.info(`set blockOffset to ${offset}`);\n this._blockOffset = offset;\n return this.mmrService\n .syncFileBaseFromPoi(this.schema, offset)\n .catch((err) => {\n logger.error(err, 'failed to sync poi to mmr');\n process.exit(1);\n });\n }\n async getProcessedBlockCount(): Promise<number> {\n const res = await this.metadataRepo.findOne({\n where: { key: 'processedBlockCount' },\n });\n\n return res?.value as number | undefined;\n }\n\n private getStartBlockFromDataSources() {\n const startBlocksList = this.project.dataSources.map(\n (item) => item.startBlock ?? 1,\n );\n if (startBlocksList.length === 0) {\n logger.error(\n `Failed to find a valid datasource, Please check your endpoint if specName filter is used.`,\n );\n process.exit(1);\n } else {\n return Math.min(...startBlocksList);\n }\n }\n\n async reindex(targetBlockHeight: number): Promise<void> {\n const lastProcessedHeight = await this.getLastProcessedHeight();\n\n return reindex(\n this.getStartBlockFromDataSources(),\n await this.getMetadataBlockOffset(),\n targetBlockHeight,\n lastProcessedHeight,\n this.storeService,\n this.dynamicDsService,\n this.mmrService,\n this.sequelize,\n /* Not providing force clean service, it should never be needed */\n );\n }\n}\n"]}
1
+ {"version":3,"file":"project.service.js","sourceRoot":"","sources":["../../src/indexer/project.service.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;;;;AAEtC,oDAA4B;AAC5B,mDAA8C;AAC9C,2CAAoD;AACpD,yDAAsD;AACtD,gDAY0B;AAC1B,yCAAsC;AACtC,kEAIsC;AACtC,8CAAqE;AACrE,8CAA2C;AAC3C,iEAA4D;AAC5D,6DAAwD;AAExD,2EAIqC;AAErC,8DAA8D;AAC9D,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;AAElE,MAAM,iBAAiB,GAAG,QAAQ,CAAC;AAEnC,MAAM,MAAM,GAAG,IAAA,qBAAS,EAAC,SAAS,CAAC,CAAC;AAG7B,IAAM,cAAc,GAApB,MAAM,cAAc;IAMzB,YACmB,kBAAsC,EACtC,UAAsB,EACtB,UAAsB,EACpB,UAAsB,EACxB,SAAoB,EACQ,OAAwB,EACpD,YAA0B,EAC1B,UAAsB,EACtB,gBAAkC,EAC3C,YAA2B,EAC3B,uBAAiD;QAVxC,uBAAkB,GAAlB,kBAAkB,CAAoB;QACtC,eAAU,GAAV,UAAU,CAAY;QACtB,eAAU,GAAV,UAAU,CAAY;QACpB,eAAU,GAAV,UAAU,CAAY;QACxB,cAAS,GAAT,SAAS,CAAW;QACQ,YAAO,GAAP,OAAO,CAAiB;QACpD,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WAAW,CACrC,CAAC;IACJ,CAAC;CACF,CAAA;AAxWY,cAAc;IAD1B,IAAA,mBAAU,GAAE;IAaR,WAAA,IAAA,eAAM,EAAC,kBAAkB,CAAC,CAAA;qCALU,yCAAkB;QAC1B,sBAAU;QACV,sBAAU;QACR,sBAAU;QACb,qBAAS;QACiB,iCAAe;QACtC,wBAAY;QACd,sBAAU;QACJ,qCAAgB;QAC7B,6BAAa;QACF,oDAAwB;GAjBhD,cAAc,CAwW1B;AAxWY,wCAAc","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport assert from 'assert';\nimport { isMainThread } from 'worker_threads';\nimport { Inject, Injectable } from '@nestjs/common';\nimport { EventEmitter2 } from '@nestjs/event-emitter';\nimport {\n ApiService,\n MetadataFactory,\n MetadataRepo,\n NodeConfig,\n IndexerEvent,\n StoreService,\n PoiService,\n MmrService,\n getLogger,\n getExistingProjectSchema,\n getMetaDataInfo,\n} from '@subql/node-core';\nimport { Sequelize } from 'sequelize';\nimport {\n generateTimestampReferenceForBlockFilters,\n SubqlProjectDs,\n SubqueryProject,\n} from '../configure/SubqueryProject';\nimport { initDbSchema, initHotSchemaReload } from '../utils/project';\nimport { reindex } from '../utils/reindex';\nimport { DsProcessorService } from './ds-processor.service';\nimport { DynamicDsService } from './dynamic-ds.service';\nimport { BestBlocks } from './types';\nimport {\n METADATA_LAST_FINALIZED_PROCESSED_KEY,\n METADATA_UNFINALIZED_BLOCKS_KEY,\n UnfinalizedBlocksService,\n} from './unfinalizedBlocks.service';\n\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nconst { version: packageVersion } = require('../../package.json');\n\nconst DEFAULT_DB_SCHEMA = 'public';\n\nconst logger = getLogger('Project');\n\n@Injectable()\nexport class ProjectService {\n private _schema: string;\n private metadataRepo: MetadataRepo;\n private _startHeight: number;\n private _blockOffset: number;\n\n constructor(\n private readonly dsProcessorService: DsProcessorService,\n private readonly apiService: ApiService,\n private readonly poiService: PoiService,\n protected readonly mmrService: MmrService,\n private readonly sequelize: Sequelize,\n @Inject('ISubqueryProject') private readonly project: SubqueryProject,\n private readonly storeService: StoreService,\n private readonly nodeConfig: NodeConfig,\n private readonly dynamicDsService: DynamicDsService,\n private eventEmitter: EventEmitter2,\n private unfinalizedBlockService: UnfinalizedBlocksService,\n ) {}\n\n get schema(): string {\n return this._schema;\n }\n\n get dataSources(): SubqlProjectDs[] {\n return this.project.dataSources;\n }\n\n get blockOffset(): number {\n return this._blockOffset;\n }\n\n get startHeight(): number {\n return this._startHeight;\n }\n\n get isHistorical(): boolean {\n return this.storeService.historical;\n }\n\n // eslint-disable-next-line @typescript-eslint/require-await\n private async getExistingProjectSchema(): Promise<string> {\n return getExistingProjectSchema(this.nodeConfig, this.sequelize);\n }\n\n async init(): Promise<void> {\n // Used to load assets into DS-processor, has to be done in any thread\n await this.dsProcessorService.validateProjectCustomDatasources();\n // Do extra work on main thread to setup stuff\n this.project.dataSources = await generateTimestampReferenceForBlockFilters(\n this.project.dataSources,\n this.apiService.api,\n );\n if (isMainThread) {\n this._schema = await this.ensureProject();\n await this.initDbSchema();\n this.metadataRepo = await this.ensureMetadata();\n this.dynamicDsService.init(this.metadataRepo);\n\n await this.initHotSchemaReload();\n\n if (this.nodeConfig.proofOfIndex) {\n const blockOffset = await this.getMetadataBlockOffset();\n void this.setBlockOffset(Number(blockOffset));\n await this.poiService.init(this.schema);\n }\n\n this._startHeight = await this.getStartHeight();\n } else {\n this._schema = await this.getExistingProjectSchema();\n this.metadataRepo = await MetadataFactory(\n this.sequelize,\n this.schema,\n this.nodeConfig.multiChain,\n this.project.network.chainId,\n );\n\n this.dynamicDsService.init(this.metadataRepo);\n\n await this.sequelize.sync();\n\n assert(this._schema, 'Schema should be created in main thread');\n await this.initDbSchema();\n\n if (this.nodeConfig.proofOfIndex) {\n await this.poiService.init(this.schema);\n }\n }\n\n if (this.nodeConfig.unfinalizedBlocks && !this.isHistorical) {\n logger.error(\n 'Unfinalized blocks cannot be enabled without historical. You will need to reindex your project to enable historical',\n );\n process.exit(1);\n }\n\n const reindexedTo = await this.unfinalizedBlockService.init(\n this.metadataRepo,\n this.reindex.bind(this),\n );\n\n if (reindexedTo !== undefined) {\n this._startHeight = reindexedTo;\n }\n }\n\n private async ensureProject(): Promise<string> {\n let schema = await this.getExistingProjectSchema();\n if (!schema) {\n schema = await this.createProjectSchema();\n }\n this.eventEmitter.emit(IndexerEvent.Ready, {\n value: true,\n });\n\n return schema;\n }\n\n private async createProjectSchema(): Promise<string> {\n let schema: string;\n if (this.nodeConfig.localMode) {\n // create tables in default schema if local mode is enabled\n schema = DEFAULT_DB_SCHEMA;\n } else {\n schema = this.nodeConfig.dbSchema;\n const schemas = await this.sequelize.showAllSchemas(undefined);\n if (!(schemas as unknown as string[]).includes(schema)) {\n await this.sequelize.createSchema(`\"${schema}\"`, undefined);\n }\n }\n\n return schema;\n }\n\n private async initHotSchemaReload(): Promise<void> {\n await initHotSchemaReload(this.schema, this.storeService);\n }\n private async initDbSchema(): Promise<void> {\n await initDbSchema(this.project, this.schema, this.storeService);\n }\n\n private async ensureMetadata(): Promise<MetadataRepo> {\n const metadataRepo = await MetadataFactory(\n this.sequelize,\n this.schema,\n this.nodeConfig.multiChain,\n this.project.network.chainId,\n );\n\n this.eventEmitter.emit(\n IndexerEvent.NetworkMetadata,\n this.apiService.networkMeta,\n );\n\n const keys = [\n 'lastProcessedHeight',\n 'blockOffset',\n 'indexerNodeVersion',\n 'chain',\n 'specName',\n 'genesisHash',\n 'startHeight',\n 'chainId',\n 'processedBlockCount',\n 'lastFinalizedVerifiedHeight',\n 'schemaMigrationCount',\n 'unfinalizedBlocks',\n 'bypassBlocks',\n ] as const;\n\n const entries = await metadataRepo.findAll({\n where: {\n key: keys,\n },\n });\n\n const keyValue = entries.reduce((arr, curr) => {\n arr[curr.key] = curr.value;\n return arr;\n }, {} as { [key in typeof keys[number]]: string | boolean | number });\n\n const { chain, genesisHash, specName } = this.apiService.networkMeta;\n\n if (this.project.runner) {\n await Promise.all([\n metadataRepo.upsert({\n key: 'runnerNode',\n value: this.project.runner.node.name,\n }),\n metadataRepo.upsert({\n key: 'runnerNodeVersion',\n value: this.project.runner.node.version,\n }),\n metadataRepo.upsert({\n key: 'runnerQuery',\n value: this.project.runner.query.name,\n }),\n metadataRepo.upsert({\n key: 'runnerQueryVersion',\n value: this.project.runner.query.version,\n }),\n ]);\n }\n if (!keyValue.genesisHash) {\n await metadataRepo.upsert({ key: 'genesisHash', value: genesisHash });\n } else {\n // Check if the configured genesisHash matches the currently stored genesisHash\n assert(\n // Configured project yaml genesisHash only exists in specVersion v0.2.0, fallback to api fetched genesisHash on v0.0.1\n (this.project.network.genesisHash ?? genesisHash) ===\n keyValue.genesisHash,\n 'Specified project manifest chain id / genesis hash does not match database stored genesis hash, consider cleaning project schema using --force-clean',\n );\n }\n if (keyValue.chain !== chain) {\n await metadataRepo.upsert({ key: 'chain', value: chain });\n }\n\n if (keyValue.specName !== specName) {\n await metadataRepo.upsert({ key: 'specName', value: specName });\n }\n\n // If project was created before this feature, don't add the key. If it is project created after, add this key.\n if (!keyValue.processedBlockCount && !keyValue.lastProcessedHeight) {\n await metadataRepo.upsert({ key: 'processedBlockCount', value: 0 });\n }\n\n if (keyValue.indexerNodeVersion !== packageVersion) {\n await metadataRepo.upsert({\n key: 'indexerNodeVersion',\n value: packageVersion,\n });\n }\n if (!keyValue.schemaMigrationCount) {\n await metadataRepo.upsert({ key: 'schemaMigrationCount', value: 0 });\n }\n\n if (!keyValue.unfinalizedBlocks) {\n await metadataRepo.upsert({\n key: 'unfinalizedBlocks',\n value: '{}',\n });\n }\n if (!keyValue.startHeight) {\n await metadataRepo.upsert({\n key: 'startHeight',\n value: this.getStartBlockFromDataSources(),\n });\n }\n\n return metadataRepo;\n }\n\n async upsertMetadataBlockOffset(height: number): Promise<void> {\n await this.metadataRepo.upsert({\n key: 'blockOffset',\n value: height,\n });\n }\n\n async getMetadataUnfinalizedBlocks(): Promise<BestBlocks | undefined> {\n const val = await getMetaDataInfo<string>(\n this.metadataRepo,\n METADATA_UNFINALIZED_BLOCKS_KEY,\n );\n if (val) {\n return JSON.parse(val) as BestBlocks;\n }\n return undefined;\n }\n\n async getLastFinalizedVerifiedHeight(): Promise<number | undefined> {\n return getMetaDataInfo(\n this.metadataRepo,\n METADATA_LAST_FINALIZED_PROCESSED_KEY,\n );\n }\n\n async getMetadataBlockOffset(): Promise<number | undefined> {\n return getMetaDataInfo(this.metadataRepo, 'blockOffset');\n }\n\n async getLastProcessedHeight(): Promise<number | undefined> {\n return getMetaDataInfo(this.metadataRepo, 'lastProcessedHeight');\n }\n\n private async getStartHeight(): Promise<number> {\n let startHeight: number;\n const lastProcessedHeight = await this.getLastProcessedHeight();\n if (lastProcessedHeight !== null && lastProcessedHeight !== undefined) {\n startHeight = Number(lastProcessedHeight) + 1;\n } else {\n startHeight = this.getStartBlockFromDataSources();\n }\n return startHeight;\n }\n\n async setBlockOffset(offset: number): Promise<void> {\n if (\n this._blockOffset ||\n offset === null ||\n offset === undefined ||\n isNaN(offset)\n ) {\n return;\n }\n logger.info(`set blockOffset to ${offset}`);\n this._blockOffset = offset;\n return this.mmrService\n .syncFileBaseFromPoi(this.schema, offset)\n .catch((err) => {\n logger.error(err, 'failed to sync poi to mmr');\n process.exit(1);\n });\n }\n async getProcessedBlockCount(): Promise<number> {\n const res = await this.metadataRepo.findOne({\n where: { key: 'processedBlockCount' },\n });\n\n return res?.value as number | undefined;\n }\n\n private getStartBlockFromDataSources() {\n const startBlocksList = this.project.dataSources.map(\n (item) => item.startBlock ?? 1,\n );\n if (startBlocksList.length === 0) {\n logger.error(\n `Failed to find a valid datasource, Please check your endpoint if specName filter is used.`,\n );\n process.exit(1);\n } else {\n return Math.min(...startBlocksList);\n }\n }\n\n async reindex(targetBlockHeight: number): Promise<void> {\n const lastProcessedHeight = await this.getLastProcessedHeight();\n\n return reindex(\n this.getStartBlockFromDataSources(),\n await this.getMetadataBlockOffset(),\n targetBlockHeight,\n lastProcessedHeight,\n this.storeService,\n this.dynamicDsService,\n this.mmrService,\n this.sequelize,\n /* Not providing force clean service, it should never be needed */\n );\n }\n\n async getAllDataSources(blockHeight: number): Promise<SubqlProjectDs[]> {\n const dynamicDs = await this.dynamicDsService.getDynamicDatasources();\n\n return [...this.dataSources, ...dynamicDs].filter(\n (ds) => ds.startBlock <= blockHeight,\n );\n }\n}\n"]}
@@ -13,3 +13,4 @@ export declare function getProjectRoot(reader: Reader): Promise<string>;
13
13
  export declare function initDbSchema(project: SubqueryProject, schema: string, storeService: StoreService): Promise<void>;
14
14
  export declare function initHotSchemaReload(schema: string, storeService: StoreService): Promise<void>;
15
15
  export declare function retryOnFailEth<T>(request: () => Promise<T>, errors?: string[]): Promise<T>;
16
+ export declare function onlyHasLogDataSources(dataSources: SubqlProjectDs[]): boolean;
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  return (mod && mod.__esModule) ? mod : { "default": mod };
6
6
  };
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.retryOnFailEth = exports.initHotSchemaReload = exports.initDbSchema = exports.getProjectRoot = exports.loadDataSourceScript = exports.getChainTypes = exports.updateDataSourcesV0_2_0 = exports.isCustomHandler = exports.isBaseHandler = exports.getProjectEntry = exports.prepareProjectDir = void 0;
8
+ exports.onlyHasLogDataSources = exports.retryOnFailEth = exports.initHotSchemaReload = exports.initDbSchema = exports.getProjectRoot = exports.loadDataSourceScript = exports.getChainTypes = exports.updateDataSourcesV0_2_0 = exports.isCustomHandler = exports.isBaseHandler = exports.getProjectEntry = exports.prepareProjectDir = void 0;
9
9
  const fs_1 = __importDefault(require("fs"));
10
10
  const os_1 = __importDefault(require("os"));
11
11
  const path_1 = __importDefault(require("path"));
@@ -220,4 +220,16 @@ async function retryOnFailEth(request, errors = handledErrors) {
220
220
  return (0, node_core_1.retryOnFail)(request, (e) => !!errors.find((t) => t === (e === null || e === void 0 ? void 0 : e.reason)));
221
221
  }
222
222
  exports.retryOnFailEth = retryOnFailEth;
223
+ function onlyHasLogDataSources(dataSources) {
224
+ for (const ds of dataSources) {
225
+ for (const handler of ds.mapping.handlers) {
226
+ if (handler.kind !== common_ethereum_1.SubqlEthereumHandlerKind.EthEvent &&
227
+ handler.kind !== common_ethereum_1.SubqlEthereumHandlerKind.FlareEvent) {
228
+ return false;
229
+ }
230
+ }
231
+ }
232
+ return true;
233
+ }
234
+ exports.onlyHasLogDataSources = onlyHasLogDataSources;
223
235
  //# sourceMappingURL=project.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"project.js","sourceRoot":"","sources":["../../src/utils/project.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;AAEtC,4CAAoB;AACpB,4CAAoB;AACpB,gDAAwB;AACxB,0CAA8E;AAC9E,4DAagC;AAChC,gDAA6D;AAC7D,wCAAuD;AACvD,sDAA2B;AAC3B,8CAAsB;AAGf,KAAK,UAAU,iBAAiB,CAAC,WAAmB;IACzD,MAAM,KAAK,GAAG,YAAE,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IACvC,IAAI,KAAK,CAAC,MAAM,EAAE,EAAE;QAClB,MAAM,GAAG,GAAG,cAAI,CAAC,GAAG,CAAC;QACrB,MAAM,MAAM,GAAG,YAAE,CAAC,MAAM,EAAE,CAAC;QAC3B,MAAM,QAAQ,GAAG,YAAE,CAAC,WAAW,CAAC,GAAG,MAAM,GAAG,GAAG,EAAE,CAAC,CAAC;QACnD,oDAAoD;QACpD,MAAM,aAAG,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC,CAAC;QAClD,OAAO,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;KACpC;SAAM,IAAI,KAAK,CAAC,WAAW,EAAE,EAAE;QAC9B,OAAO,WAAW,CAAC;KACpB;AACH,CAAC;AAZD,8CAYC;AAED,gDAAgD;AAChD,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,IAAA,uBAAW,EAAC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,MAAK,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,MAAM,CAAA,CAAC,CAAC,CAAC;AAC5E,CAAC;AALD,wCAKC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport fs from 'fs';\nimport os from 'os';\nimport path from 'path';\nimport { GithubReader, IPFSReader, LocalReader, Reader } from '@subql/common';\nimport {\n ChainTypes,\n CustomDatasourceV0_2_0,\n isCustomDs,\n loadChainTypes,\n loadChainTypesFromJs,\n parseChainTypes,\n RuntimeDataSourceV0_2_0,\n SubqlRuntimeHandler,\n SubqlCustomHandler,\n SubqlHandler,\n EthereumHandlerKind,\n EthereumDatasourceKind,\n} from '@subql/common-ethereum';\nimport { retryOnFail, StoreService } from '@subql/node-core';\nimport { getAllEntitiesRelations } from '@subql/utils';\nimport yaml from 'js-yaml';\nimport tar from 'tar';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\n\nexport async function prepareProjectDir(projectPath: string): Promise<string> {\n const stats = fs.statSync(projectPath);\n if (stats.isFile()) {\n const sep = path.sep;\n const tmpDir = os.tmpdir();\n const tempPath = fs.mkdtempSync(`${tmpDir}${sep}`);\n // Will promote errors if incorrect format/extension\n await tar.x({ file: projectPath, cwd: tempPath });\n return tempPath.concat('/package');\n } else if (stats.isDirectory()) {\n return projectPath;\n }\n}\n\n// We cache this to avoid repeated reads from fs\nconst projectEntryCache: Record<string, string> = {};\n\nexport function getProjectEntry(root: string): string {\n const pkgPath = path.join(root, 'package.json');\n try {\n if (!projectEntryCache[pkgPath]) {\n const content = fs.readFileSync(pkgPath).toString();\n const pkg = JSON.parse(content);\n if (!pkg.main) {\n return './dist';\n }\n projectEntryCache[pkgPath] = pkg.main.startsWith('./')\n ? pkg.main\n : `./${pkg.main}`;\n }\n\n return projectEntryCache[pkgPath];\n } catch (err) {\n throw new Error(`can not find package.json within directory ${root}`);\n }\n}\n\nexport function isBaseHandler(\n handler: SubqlHandler,\n): handler is SubqlRuntimeHandler {\n return Object.values<string>(EthereumHandlerKind).includes(handler.kind);\n}\n\nexport function isCustomHandler(\n handler: SubqlHandler,\n): handler is SubqlCustomHandler {\n return !isBaseHandler(handler);\n}\n\nexport async function updateDataSourcesV0_2_0(\n _dataSources: (RuntimeDataSourceV0_2_0 | CustomDatasourceV0_2_0)[],\n reader: Reader,\n root: string,\n): Promise<SubqlProjectDs[]> {\n // force convert to updated ds\n return Promise.all(\n _dataSources.map(async (dataSource) => {\n if (dataSource.kind === 'flare/Runtime') {\n dataSource.kind = EthereumDatasourceKind.Runtime;\n }\n dataSource.mapping.handlers = dataSource.mapping.handlers.map(\n (handler) => {\n switch (handler.kind) {\n case 'flare/BlockHandler': {\n handler.kind = EthereumHandlerKind.Block;\n break;\n }\n case 'flare/TransactionHandler': {\n handler.kind = EthereumHandlerKind.Call;\n break;\n }\n case 'flare/LogHandler': {\n handler.kind = EthereumHandlerKind.Event;\n break;\n }\n default:\n }\n\n return handler;\n },\n );\n\n const entryScript = await loadDataSourceScript(\n reader,\n dataSource.mapping.file,\n );\n const file = await updateDataSourcesEntry(\n reader,\n dataSource.mapping.file,\n root,\n entryScript,\n );\n if (dataSource.assets) {\n for (const [, asset] of Object.entries(dataSource.assets)) {\n if (reader instanceof LocalReader) {\n asset.file = path.resolve(root, asset.file);\n } else {\n const res = await reader.getFile(asset.file);\n const outputPath = path.resolve(\n root,\n asset.file.replace('ipfs://', ''),\n );\n await fs.promises.writeFile(outputPath, res as string);\n asset.file = outputPath;\n }\n }\n }\n if (isCustomDs(dataSource)) {\n if (dataSource.processor) {\n dataSource.processor.file = await updateProcessor(\n reader,\n root,\n dataSource.processor.file,\n );\n }\n if (dataSource.assets) {\n for (const [, asset] of dataSource.assets) {\n if (reader instanceof LocalReader) {\n asset.file = path.resolve(root, asset.file);\n } else {\n const res = await reader.getFile(asset.file);\n const outputPath = path.resolve(\n root,\n asset.file.replace('ipfs://', ''),\n );\n await fs.promises.writeFile(outputPath, res as string);\n asset.file = outputPath;\n }\n }\n }\n return {\n ...dataSource,\n mapping: { ...dataSource.mapping, entryScript, file },\n };\n } else {\n return {\n ...dataSource,\n mapping: { ...dataSource.mapping, entryScript, file },\n };\n }\n }),\n );\n}\n\nasync function updateDataSourcesEntry(\n reader: Reader,\n file: string,\n root: string,\n script: string,\n): Promise<string> {\n if (reader instanceof LocalReader) return file;\n else if (reader instanceof IPFSReader || reader instanceof GithubReader) {\n const outputPath = `${path.resolve(root, file.replace('ipfs://', ''))}.js`;\n await fs.promises.writeFile(outputPath, script);\n return outputPath;\n }\n}\n\nasync function updateProcessor(\n reader: Reader,\n root: string,\n file: string,\n): Promise<string> {\n if (reader instanceof LocalReader) {\n return path.resolve(root, file);\n } else {\n const res = await reader.getFile(file);\n const outputPath = `${path.resolve(root, file.replace('ipfs://', ''))}.js`;\n await fs.promises.writeFile(outputPath, res);\n return outputPath;\n }\n}\n\nexport async function getChainTypes(\n reader: Reader,\n root: string,\n file: string,\n): Promise<ChainTypes> {\n // If the project is load from local, we will direct load them\n if (reader instanceof LocalReader) {\n return loadChainTypes(file, root);\n } else {\n // If it is stored in ipfs or other resources, we will use the corresponding reader to read the file\n // Because ipfs not provide extension of the file, it is difficult to determine its format\n // We will use yaml.load to try to load the script and parse them to supported chain types\n // if it failed, we will give it another another attempt, and assume the script written in js\n // we will download it to a temp folder, and load them within sandbox\n const res = await reader.getFile(file);\n let raw: unknown;\n try {\n raw = yaml.load(res);\n return parseChainTypes(raw);\n } catch (e) {\n const chainTypesPath = `${path.resolve(\n root,\n file.replace('ipfs://', ''),\n )}.js`;\n await fs.promises.writeFile(chainTypesPath, res);\n raw = loadChainTypesFromJs(chainTypesPath); //root not required, as it been packed in single js\n return parseChainTypes(raw);\n }\n }\n}\n\nexport async function loadDataSourceScript(\n reader: Reader,\n file?: string,\n): Promise<string> {\n let entry: string;\n //For RuntimeDataSourceV0_0_1\n if (!file) {\n const pkg = await reader.getPkg();\n if (pkg === undefined) throw new Error('Project package.json is not found');\n if (pkg.main) {\n entry = pkg.main.startsWith('./') ? pkg.main : `./${pkg.main}`;\n } else {\n entry = './dist';\n }\n }\n //Else get file\n const entryScript = await reader.getFile(file ? file : entry);\n if (entryScript === undefined) {\n throw new Error(`Entry file ${entry} for datasource not exist`);\n }\n return entryScript;\n}\n\nasync function makeTempDir(): Promise<string> {\n const sep = path.sep;\n const tmpDir = os.tmpdir();\n return fs.promises.mkdtemp(`${tmpDir}${sep}`);\n}\n\nexport async function getProjectRoot(reader: Reader): Promise<string> {\n if (reader instanceof LocalReader) return reader.root;\n if (reader instanceof IPFSReader || reader instanceof GithubReader) {\n return makeTempDir();\n }\n}\n\nexport async function initDbSchema(\n project: SubqueryProject,\n schema: string,\n storeService: StoreService,\n): Promise<void> {\n const modelsRelation = getAllEntitiesRelations(project.schema);\n await storeService.init(modelsRelation, schema);\n}\n\nexport async function initHotSchemaReload(\n schema: string,\n storeService: StoreService,\n): Promise<void> {\n await storeService.initHotSchemaReloadQueries(schema);\n}\n\nconst handledErrors = ['timeout'];\n// eslint-disable-next-line @typescript-eslint/require-await\nexport async function retryOnFailEth<T>(\n request: () => Promise<T>,\n errors = handledErrors,\n): Promise<T> {\n return retryOnFail(request, (e) => !!errors.find((t) => t === e?.reason));\n}\n"]}
1
+ {"version":3,"file":"project.js","sourceRoot":"","sources":["../../src/utils/project.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;AAEtC,4CAAoB;AACpB,4CAAoB;AACpB,gDAAwB;AACxB,0CAA8E;AAC9E,4DAcgC;AAChC,gDAA6D;AAC7D,wCAAuD;AACvD,sDAA2B;AAC3B,8CAAsB;AAGf,KAAK,UAAU,iBAAiB,CAAC,WAAmB;IACzD,MAAM,KAAK,GAAG,YAAE,CAA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AAY,EAAE;QAClE,OAAO,WAAW,EAAE,CAAC;KACtB;AACH,CAAC;AALD,wCAKC;AAEM,KAAK,UAAU,YAAY,CAChC,OAAwB,EACxB,MAAc,EACd,YAA0B;IAE1B,MAAM,cAAc,GAAG,IAAA,+BAAuB,EAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/D,MAAM,YAAY,CAAC,IAAI,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;AAClD,CAAC;AAPD,oCAOC;AAEM,KAAK,UAAU,mBAAmB,CACvC,MAAc,EACd,YAA0B;IAE1B,MAAM,YAAY,CAAC,0BAA0B,CAAC,MAAM,CAAC,CAAC;AACxD,CAAC;AALD,kDAKC;AAED,MAAM,aAAa,GAAG,CAAC,SAAS,CAAC,CAAC;AAClC,4DAA4D;AACrD,KAAK,UAAU,cAAc,CAClC,OAAyB,EACzB,MAAM,GAAG,aAAa;IAEtB,OAAO,IAAA,uBAAW,EAAC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,MAAK,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,MAAM,CAAA,CAAC,CAAC,CAAC;AAC5E,CAAC;AALD,wCAKC;AAED,SAAgB,qBAAqB,CAAC,WAA6B;IACjE,KAAK,MAAM,EAAE,IAAI,WAAW,EAAE;QAC5B,KAAK,MAAM,OAAO,IAAI,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE;YACzC,IACE,OAAO,CAAC,IAAI,KAAK,0CAAwB,CAAC,QAAQ;gBAClD,OAAO,CAAC,IAAI,KAAK,0CAAwB,CAAC,UAAU,EACpD;gBACA,OAAO,KAAK,CAAC;aACd;SACF;KACF;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAbD,sDAaC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport fs from 'fs';\nimport os from 'os';\nimport path from 'path';\nimport { GithubReader, IPFSReader, LocalReader, Reader } from '@subql/common';\nimport {\n ChainTypes,\n CustomDatasourceV0_2_0,\n isCustomDs,\n loadChainTypes,\n loadChainTypesFromJs,\n parseChainTypes,\n RuntimeDataSourceV0_2_0,\n SubqlRuntimeHandler,\n SubqlCustomHandler,\n SubqlHandler,\n EthereumHandlerKind,\n EthereumDatasourceKind,\n SubqlEthereumHandlerKind,\n} from '@subql/common-ethereum';\nimport { retryOnFail, StoreService } from '@subql/node-core';\nimport { getAllEntitiesRelations } from '@subql/utils';\nimport yaml from 'js-yaml';\nimport tar from 'tar';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\n\nexport async function prepareProjectDir(projectPath: string): Promise<string> {\n const stats = fs.statSync(projectPath);\n if (stats.isFile()) {\n const sep = path.sep;\n const tmpDir = os.tmpdir();\n const tempPath = fs.mkdtempSync(`${tmpDir}${sep}`);\n // Will promote errors if incorrect format/extension\n await tar.x({ file: projectPath, cwd: tempPath });\n return tempPath.concat('/package');\n } else if (stats.isDirectory()) {\n return projectPath;\n }\n}\n\n// We cache this to avoid repeated reads from fs\nconst projectEntryCache: Record<string, string> = {};\n\nexport function getProjectEntry(root: string): string {\n const pkgPath = path.join(root, 'package.json');\n try {\n if (!projectEntryCache[pkgPath]) {\n const content = fs.readFileSync(pkgPath).toString();\n const pkg = JSON.parse(content);\n if (!pkg.main) {\n return './dist';\n }\n projectEntryCache[pkgPath] = pkg.main.startsWith('./')\n ? pkg.main\n : `./${pkg.main}`;\n }\n\n return projectEntryCache[pkgPath];\n } catch (err) {\n throw new Error(`can not find package.json within directory ${root}`);\n }\n}\n\nexport function isBaseHandler(\n handler: SubqlHandler,\n): handler is SubqlRuntimeHandler {\n return Object.values<string>(EthereumHandlerKind).includes(handler.kind);\n}\n\nexport function isCustomHandler(\n handler: SubqlHandler,\n): handler is SubqlCustomHandler {\n return !isBaseHandler(handler);\n}\n\nexport async function updateDataSourcesV0_2_0(\n _dataSources: (RuntimeDataSourceV0_2_0 | CustomDatasourceV0_2_0)[],\n reader: Reader,\n root: string,\n): Promise<SubqlProjectDs[]> {\n // force convert to updated ds\n return Promise.all(\n _dataSources.map(async (dataSource) => {\n if (dataSource.kind === 'flare/Runtime') {\n dataSource.kind = EthereumDatasourceKind.Runtime;\n }\n dataSource.mapping.handlers = dataSource.mapping.handlers.map(\n (handler) => {\n switch (handler.kind) {\n case 'flare/BlockHandler': {\n handler.kind = EthereumHandlerKind.Block;\n break;\n }\n case 'flare/TransactionHandler': {\n handler.kind = EthereumHandlerKind.Call;\n break;\n }\n case 'flare/LogHandler': {\n handler.kind = EthereumHandlerKind.Event;\n break;\n }\n default:\n }\n\n return handler;\n },\n );\n\n const entryScript = await loadDataSourceScript(\n reader,\n dataSource.mapping.file,\n );\n const file = await updateDataSourcesEntry(\n reader,\n dataSource.mapping.file,\n root,\n entryScript,\n );\n if (dataSource.assets) {\n for (const [, asset] of Object.entries(dataSource.assets)) {\n if (reader instanceof LocalReader) {\n asset.file = path.resolve(root, asset.file);\n } else {\n const res = await reader.getFile(asset.file);\n const outputPath = path.resolve(\n root,\n asset.file.replace('ipfs://', ''),\n );\n await fs.promises.writeFile(outputPath, res as string);\n asset.file = outputPath;\n }\n }\n }\n if (isCustomDs(dataSource)) {\n if (dataSource.processor) {\n dataSource.processor.file = await updateProcessor(\n reader,\n root,\n dataSource.processor.file,\n );\n }\n if (dataSource.assets) {\n for (const [, asset] of dataSource.assets) {\n if (reader instanceof LocalReader) {\n asset.file = path.resolve(root, asset.file);\n } else {\n const res = await reader.getFile(asset.file);\n const outputPath = path.resolve(\n root,\n asset.file.replace('ipfs://', ''),\n );\n await fs.promises.writeFile(outputPath, res as string);\n asset.file = outputPath;\n }\n }\n }\n return {\n ...dataSource,\n mapping: { ...dataSource.mapping, entryScript, file },\n };\n } else {\n return {\n ...dataSource,\n mapping: { ...dataSource.mapping, entryScript, file },\n };\n }\n }),\n );\n}\n\nasync function updateDataSourcesEntry(\n reader: Reader,\n file: string,\n root: string,\n script: string,\n): Promise<string> {\n if (reader instanceof LocalReader) return file;\n else if (reader instanceof IPFSReader || reader instanceof GithubReader) {\n const outputPath = `${path.resolve(root, file.replace('ipfs://', ''))}.js`;\n await fs.promises.writeFile(outputPath, script);\n return outputPath;\n }\n}\n\nasync function updateProcessor(\n reader: Reader,\n root: string,\n file: string,\n): Promise<string> {\n if (reader instanceof LocalReader) {\n return path.resolve(root, file);\n } else {\n const res = await reader.getFile(file);\n const outputPath = `${path.resolve(root, file.replace('ipfs://', ''))}.js`;\n await fs.promises.writeFile(outputPath, res);\n return outputPath;\n }\n}\n\nexport async function getChainTypes(\n reader: Reader,\n root: string,\n file: string,\n): Promise<ChainTypes> {\n // If the project is load from local, we will direct load them\n if (reader instanceof LocalReader) {\n return loadChainTypes(file, root);\n } else {\n // If it is stored in ipfs or other resources, we will use the corresponding reader to read the file\n // Because ipfs not provide extension of the file, it is difficult to determine its format\n // We will use yaml.load to try to load the script and parse them to supported chain types\n // if it failed, we will give it another another attempt, and assume the script written in js\n // we will download it to a temp folder, and load them within sandbox\n const res = await reader.getFile(file);\n let raw: unknown;\n try {\n raw = yaml.load(res);\n return parseChainTypes(raw);\n } catch (e) {\n const chainTypesPath = `${path.resolve(\n root,\n file.replace('ipfs://', ''),\n )}.js`;\n await fs.promises.writeFile(chainTypesPath, res);\n raw = loadChainTypesFromJs(chainTypesPath); //root not required, as it been packed in single js\n return parseChainTypes(raw);\n }\n }\n}\n\nexport async function loadDataSourceScript(\n reader: Reader,\n file?: string,\n): Promise<string> {\n let entry: string;\n //For RuntimeDataSourceV0_0_1\n if (!file) {\n const pkg = await reader.getPkg();\n if (pkg === undefined) throw new Error('Project package.json is not found');\n if (pkg.main) {\n entry = pkg.main.startsWith('./') ? pkg.main : `./${pkg.main}`;\n } else {\n entry = './dist';\n }\n }\n //Else get file\n const entryScript = await reader.getFile(file ? file : entry);\n if (entryScript === undefined) {\n throw new Error(`Entry file ${entry} for datasource not exist`);\n }\n return entryScript;\n}\n\nasync function makeTempDir(): Promise<string> {\n const sep = path.sep;\n const tmpDir = os.tmpdir();\n return fs.promises.mkdtemp(`${tmpDir}${sep}`);\n}\n\nexport async function getProjectRoot(reader: Reader): Promise<string> {\n if (reader instanceof LocalReader) return reader.root;\n if (reader instanceof IPFSReader || reader instanceof GithubReader) {\n return makeTempDir();\n }\n}\n\nexport async function initDbSchema(\n project: SubqueryProject,\n schema: string,\n storeService: StoreService,\n): Promise<void> {\n const modelsRelation = getAllEntitiesRelations(project.schema);\n await storeService.init(modelsRelation, schema);\n}\n\nexport async function initHotSchemaReload(\n schema: string,\n storeService: StoreService,\n): Promise<void> {\n await storeService.initHotSchemaReloadQueries(schema);\n}\n\nconst handledErrors = ['timeout'];\n// eslint-disable-next-line @typescript-eslint/require-await\nexport async function retryOnFailEth<T>(\n request: () => Promise<T>,\n errors = handledErrors,\n): Promise<T> {\n return retryOnFail(request, (e) => !!errors.find((t) => t === e?.reason));\n}\n\nexport function onlyHasLogDataSources(dataSources: SubqlProjectDs[]): boolean {\n for (const ds of dataSources) {\n for (const handler of ds.mapping.handlers) {\n if (\n handler.kind !== SubqlEthereumHandlerKind.EthEvent &&\n handler.kind !== SubqlEthereumHandlerKind.FlareEvent\n ) {\n return false;\n }\n }\n }\n\n return true;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subql/node-ethereum",
3
- "version": "0.4.1-7",
3
+ "version": "0.4.1-9",
4
4
  "description": "",
5
5
  "author": "Ian He",
6
6
  "license": "Apache-2.0",
@@ -27,7 +27,7 @@
27
27
  "@subql/common": "1.5.0",
28
28
  "@subql/common-ethereum": "0.2.2-4",
29
29
  "@subql/node-core": "1.8.1-2",
30
- "@subql/types-ethereum": "0.2.2-5",
30
+ "@subql/types-ethereum": "0.2.2-6",
31
31
  "@subql/utils": "1.3.2-1",
32
32
  "@subql/x-merkle-mountain-range": "2.0.0-0.1.2",
33
33
  "@willsoto/nestjs-prometheus": "^4.4.0",
@@ -74,5 +74,5 @@
74
74
  "/dist",
75
75
  "/bin"
76
76
  ],
77
- "stableVersion": "0.4.1-6"
77
+ "stableVersion": "0.4.1-8"
78
78
  }