@subql/node-ethereum 1.0.3-1 → 1.0.3-3

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.
Files changed (64) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/app.module.js.map +1 -1
  3. package/dist/configure/SubqueryProject.d.ts +9 -11
  4. package/dist/configure/SubqueryProject.js.map +1 -1
  5. package/dist/configure/configure.module.js.map +1 -1
  6. package/dist/ethereum/api.service.ethereum.d.ts +2 -1
  7. package/dist/ethereum/api.service.ethereum.js.map +1 -1
  8. package/dist/ethereum/ethers/web/index.d.ts +4 -3
  9. package/dist/ethereum/ethers/web/types.d.ts +3 -2
  10. package/dist/indexer/blockDispatcher/block-dispatcher.service.d.ts +1 -1
  11. package/dist/indexer/blockDispatcher/block-dispatcher.service.js +1 -1
  12. package/dist/indexer/blockDispatcher/block-dispatcher.service.js.map +1 -1
  13. package/dist/indexer/blockDispatcher/worker-block-dispatcher.service.d.ts +2 -2
  14. package/dist/indexer/blockDispatcher/worker-block-dispatcher.service.js.map +1 -1
  15. package/dist/indexer/dictionary.service.js.map +1 -1
  16. package/dist/indexer/ds-processor.service.js.map +1 -1
  17. package/dist/indexer/dynamic-ds.service.js.map +1 -1
  18. package/dist/indexer/fetch.module.js +0 -7
  19. package/dist/indexer/fetch.module.js.map +1 -1
  20. package/dist/indexer/fetch.service.js +3 -5
  21. package/dist/indexer/fetch.service.js.map +1 -1
  22. package/dist/indexer/indexer.manager.d.ts +6 -5
  23. package/dist/indexer/indexer.manager.js +8 -9
  24. package/dist/indexer/indexer.manager.js.map +1 -1
  25. package/dist/indexer/indexer.module.js.map +1 -1
  26. package/dist/indexer/project.service.d.ts +1 -1
  27. package/dist/indexer/project.service.js.map +1 -1
  28. package/dist/indexer/sandbox.service.js +1 -1
  29. package/dist/indexer/sandbox.service.js.map +1 -1
  30. package/dist/indexer/types.d.ts +2 -2
  31. package/dist/indexer/unfinalizedBlocks.service.d.ts +1 -1
  32. package/dist/indexer/unfinalizedBlocks.service.js.map +1 -1
  33. package/dist/indexer/worker/worker.d.ts +10 -10
  34. package/dist/indexer/worker/worker.js.map +1 -1
  35. package/dist/indexer/worker/worker.module.js.map +1 -1
  36. package/dist/indexer/worker/worker.service.d.ts +7 -5
  37. package/dist/indexer/worker/worker.service.js +8 -4
  38. package/dist/indexer/worker/worker.service.js.map +1 -1
  39. package/dist/indexer/worker/worker.unfinalizedBlocks.service.d.ts +1 -1
  40. package/dist/indexer/worker/worker.unfinalizedBlocks.service.js.map +1 -1
  41. package/dist/main.js +3 -1
  42. package/dist/main.js.map +1 -1
  43. package/dist/meta/meta.controller.js.map +1 -1
  44. package/dist/meta/meta.module.js.map +1 -1
  45. package/dist/meta/meta.service.js.map +1 -1
  46. package/dist/subcommands/forceClean.module.js.map +1 -1
  47. package/dist/subcommands/forceClean.service.js.map +1 -1
  48. package/dist/subcommands/reindex.module.js.map +1 -1
  49. package/dist/subcommands/reindex.service.js.map +1 -1
  50. package/dist/subcommands/testing.init.d.ts +1 -0
  51. package/dist/subcommands/testing.init.js +34 -0
  52. package/dist/subcommands/testing.init.js.map +1 -0
  53. package/dist/subcommands/testing.module.d.ts +4 -0
  54. package/dist/subcommands/testing.module.js +77 -0
  55. package/dist/subcommands/testing.module.js.map +1 -0
  56. package/dist/subcommands/testing.service.d.ts +9 -0
  57. package/dist/subcommands/testing.service.js +43 -0
  58. package/dist/subcommands/testing.service.js.map +1 -0
  59. package/dist/utils/reindex.js +0 -4
  60. package/dist/utils/reindex.js.map +1 -1
  61. package/dist/yargs.d.ts +9 -7
  62. package/dist/yargs.js +21 -9
  63. package/dist/yargs.js.map +1 -1
  64. package/package.json +8 -8
@@ -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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mBAAQ,EAAC,mBAAmB,CAAC;;;;yDA0B7B;AAGD;IADC,IAAA,mBAAQ,EAAC,mBAAmB,CAAC;;;;oDAwB7B;AAhPU,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,UAGjC,sCAAiB;QAClB,qCAAgB;QACR,oDAAwB;QACpC,6BAAa;QACR,4BAAiB;GApBnC,YAAY,CA2fxB;AA3fY,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';\n\nimport {\n isCustomDs,\n EthereumHandlerKind,\n EthereumLogFilter,\n SubqlEthereumProcessorOptions,\n EthereumTransactionFilter,\n} from '@subql/common-ethereum';\nimport {\n ApiService,\n cleanedBatchBlocks,\n checkMemoryUsage,\n delay,\n getLogger,\n IndexerEvent,\n NodeConfig,\n transformBypassBlocks,\n waitForBatchSize,\n} from '@subql/node-core';\nimport {\n ApiWrapper,\n DictionaryQueryCondition,\n DictionaryQueryEntry,\n} from '@subql/types-ethereum';\nimport { MetaData } from '@subql/utils';\nimport { groupBy, range, sortBy, uniqBy, without } from 'lodash';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { calcInterval } from '../ethereum/utils.ethereum';\nimport { eventToTopic, functionToSighash } from '../utils/string';\nimport { IEthereumBlockDispatcher } 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: DictionaryQueryCondition[] = [];\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 // matcher: 'equals',\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({\n field,\n value: eventToTopic(topic),\n matcher: 'equalTo',\n });\n }\n }\n return {\n entity: 'evmLogs',\n conditions,\n };\n}\n\nfunction callFilterToQueryEntry(\n filter: EthereumTransactionFilter,\n): DictionaryQueryEntry {\n const conditions: DictionaryQueryCondition[] = [];\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 matcher: 'equalTo',\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 dictionaryMetaValid = false;\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')\n private blockDispatcher: IEthereumBlockDispatcher,\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 // No filters, cant use dictionary\n if (!handler.filter) return [];\n\n switch (handler.kind) {\n case EthereumHandlerKind.Block:\n return [];\n case EthereumHandlerKind.Call: {\n const filter = handler.filter 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 break;\n }\n case EthereumHandlerKind.Event: {\n const filter = handler.filter 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 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.dictionaryMetaValid &&\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 // 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 if (this.project.network.dictionary) {\n this.evmChainId = await this.dictionaryService.getEvmChainId();\n }\n const dictionaryValid = this.dictionaryValidation(metadata);\n\n await Promise.all([this.getFinalizedBlockHead(), this.getBestBlockHead()]);\n\n if (dictionaryValid) {\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)\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)\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 = this.blockDispatcher.smartBatchSize;\n\n if (scaledBatchSize === 0) {\n await waitForBatchSize(this.blockDispatcher.minimumHeapLimit);\n continue;\n }\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 await 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 await 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 const enqueuingBlocks =\n handlers.length && this.getModulos().length === handlers.length\n ? this.getEnqueuedModuloBlocks(startBlockHeight)\n : range(startBlockHeight, endHeight + 1);\n\n const cleanedBatchBlocks = this.filteredBlockBatch(enqueuingBlocks);\n await this.blockDispatcher.enqueueBlocks(\n cleanedBatchBlocks,\n this.getLatestBufferHeight(cleanedBatchBlocks, enqueuingBlocks),\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 const validate = (): 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 return false;\n }\n\n if (\n startBlockHeight !== undefined &&\n metaData.lastProcessedHeight < startBlockHeight\n ) {\n logger.warn(\n `Dictionary indexed block is behind current indexing block height`,\n );\n return false;\n }\n return true;\n }\n return false;\n };\n\n const valid = validate();\n\n this.dictionaryMetaValid = valid;\n this.eventEmitter.emit(IndexerEvent.UsingDictionary, {\n value: Number(this.useDictionary),\n });\n this.eventEmitter.emit(IndexerEvent.SkipDictionary);\n\n return valid;\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 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;AAE/D,4DAMgC;AAChC,gDAU0B;AAO1B,mCAAiE;AACjE,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,GAA+B,EAAE,CAAC;IAElD,IAAI,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAA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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;IACD,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;AA7UC;IAAC,IAAA,mBAAQ,EAAC,qBAAqB,CAAC;;;;mDAS/B;AAGK;IADL,IAAA,mBAAQ,EAAC,mBAAmB,CAAC;;;;yDA0B7B;AAGK;IADL,IAAA,mBAAQ,EAAC,mBAAmB,CAAC;;;;oDAwB7B;AA1OU,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,UAGjC,sCAAiB;QAClB,qCAAgB;QACR,oDAAwB;QACpC,6BAAa;QACR,4BAAiB;GApBnC,YAAY,CAwfxB;AAxfY,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';\n\nimport {\n isCustomDs,\n EthereumHandlerKind,\n EthereumLogFilter,\n SubqlEthereumProcessorOptions,\n EthereumTransactionFilter,\n} from '@subql/common-ethereum';\nimport {\n ApiService,\n cleanedBatchBlocks,\n checkMemoryUsage,\n delay,\n getLogger,\n IndexerEvent,\n NodeConfig,\n transformBypassBlocks,\n waitForBatchSize,\n} from '@subql/node-core';\nimport {\n ApiWrapper,\n DictionaryQueryCondition,\n DictionaryQueryEntry,\n} from '@subql/types-ethereum';\nimport { MetaData } from '@subql/utils';\nimport { groupBy, range, sortBy, uniqBy, without } from 'lodash';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { calcInterval } from '../ethereum/utils.ethereum';\nimport { eventToTopic, functionToSighash } from '../utils/string';\nimport { IEthereumBlockDispatcher } 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: DictionaryQueryCondition[] = [];\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 // matcher: 'equals',\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({\n field,\n value: eventToTopic(topic),\n matcher: 'equalTo',\n });\n }\n }\n return {\n entity: 'evmLogs',\n conditions,\n };\n}\n\nfunction callFilterToQueryEntry(\n filter: EthereumTransactionFilter,\n): DictionaryQueryEntry {\n const conditions: DictionaryQueryCondition[] = [];\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 matcher: 'equalTo',\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 dictionaryMetaValid = false;\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')\n private blockDispatcher: IEthereumBlockDispatcher,\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 // No filters, cant use dictionary\n if (!handler.filter) return [];\n\n switch (handler.kind) {\n case EthereumHandlerKind.Block:\n return [];\n case EthereumHandlerKind.Call: {\n const filter = handler.filter 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 break;\n }\n case EthereumHandlerKind.Event: {\n const filter = handler.filter 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 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.dictionaryMetaValid &&\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 // 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 if (this.project.network.dictionary) {\n this.evmChainId = await this.dictionaryService.getEvmChainId();\n }\n const dictionaryValid = this.dictionaryValidation(metadata);\n\n await Promise.all([this.getFinalizedBlockHead(), this.getBestBlockHead()]);\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)\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)\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 (\n this.useDictionary &&\n this.dictionaryService.startHeight > getStartBlockHeight()\n ) {\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 = this.blockDispatcher.smartBatchSize;\n\n if (scaledBatchSize === 0) {\n await waitForBatchSize(this.blockDispatcher.minimumHeapLimit);\n continue;\n }\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 await 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 await 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 const enqueuingBlocks =\n handlers.length && this.getModulos().length === handlers.length\n ? this.getEnqueuedModuloBlocks(startBlockHeight)\n : range(startBlockHeight, endHeight + 1);\n\n const cleanedBatchBlocks = this.filteredBlockBatch(enqueuingBlocks);\n await this.blockDispatcher.enqueueBlocks(\n cleanedBatchBlocks,\n this.getLatestBufferHeight(cleanedBatchBlocks, enqueuingBlocks),\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 const validate = (): 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 return false;\n }\n\n if (\n startBlockHeight !== undefined &&\n metaData.lastProcessedHeight < startBlockHeight\n ) {\n logger.warn(\n `Dictionary indexed block is behind current indexing block height`,\n );\n return false;\n }\n return true;\n }\n return false;\n };\n\n const valid = validate();\n\n this.dictionaryMetaValid = valid;\n this.eventEmitter.emit(IndexerEvent.UsingDictionary, {\n value: Number(this.useDictionary),\n });\n this.eventEmitter.emit(IndexerEvent.SkipDictionary);\n\n return valid;\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 async resetForIncorrectBestBlock(blockHeight: number): Promise<void> {\n await this.syncDynamicDatascourcesFromMeta();\n this.updateDictionary();\n this.blockDispatcher.flushQueue(blockHeight);\n }\n}\n"]}
@@ -1,20 +1,21 @@
1
- import { NodeConfig, ProcessBlockResponse, ApiService } from '@subql/node-core';
1
+ import { NodeConfig, ProcessBlockResponse, ApiService, IIndexerManager } from '@subql/node-core';
2
2
  import { EthereumBlockWrapper } from '@subql/types-ethereum';
3
+ import { SubqlProjectDs } from '../configure/SubqueryProject';
3
4
  import { DsProcessorService } from './ds-processor.service';
4
5
  import { DynamicDsService } from './dynamic-ds.service';
5
6
  import { ProjectService } from './project.service';
6
7
  import { SandboxService } from './sandbox.service';
7
8
  import { UnfinalizedBlocksService } from './unfinalizedBlocks.service';
8
- export declare class IndexerManager {
9
+ export declare class IndexerManager implements IIndexerManager<EthereumBlockWrapper, SubqlProjectDs> {
9
10
  private apiService;
10
11
  private nodeConfig;
11
12
  private sandboxService;
13
+ private dsProcessorService;
12
14
  private dynamicDsService;
13
15
  private unfinalizedBlocksService;
14
- private dsProcessorService;
15
16
  private projectService;
16
- constructor(apiService: ApiService, nodeConfig: NodeConfig, sandboxService: SandboxService, dynamicDsService: DynamicDsService, unfinalizedBlocksService: UnfinalizedBlocksService, dsProcessorService: DsProcessorService, projectService: ProjectService);
17
- indexBlock(blockContent: EthereumBlockWrapper): Promise<ProcessBlockResponse>;
17
+ constructor(apiService: ApiService, nodeConfig: NodeConfig, sandboxService: SandboxService, dsProcessorService: DsProcessorService, dynamicDsService: DynamicDsService, unfinalizedBlocksService: UnfinalizedBlocksService, projectService: ProjectService);
18
+ indexBlock(blockContent: EthereumBlockWrapper, dataSources: SubqlProjectDs[]): Promise<ProcessBlockResponse>;
18
19
  start(): Promise<void>;
19
20
  private processUnfinalizedBlocks;
20
21
  private assertDataSources;
@@ -27,27 +27,26 @@ const sandbox_service_1 = require("./sandbox.service");
27
27
  const unfinalizedBlocks_service_1 = require("./unfinalizedBlocks.service");
28
28
  const logger = (0, node_core_1.getLogger)('indexer');
29
29
  let IndexerManager = class IndexerManager {
30
- constructor(apiService, nodeConfig, sandboxService, dynamicDsService, unfinalizedBlocksService, dsProcessorService, projectService) {
30
+ constructor(apiService, nodeConfig, sandboxService, dsProcessorService, dynamicDsService, unfinalizedBlocksService, projectService) {
31
31
  this.apiService = apiService;
32
32
  this.nodeConfig = nodeConfig;
33
33
  this.sandboxService = sandboxService;
34
+ this.dsProcessorService = dsProcessorService;
34
35
  this.dynamicDsService = dynamicDsService;
35
36
  this.unfinalizedBlocksService = unfinalizedBlocksService;
36
- this.dsProcessorService = dsProcessorService;
37
37
  this.projectService = projectService;
38
38
  logger.info('indexer manager start');
39
39
  }
40
- async indexBlock(blockContent) {
40
+ async indexBlock(blockContent, dataSources) {
41
41
  const { block, blockHeight } = blockContent;
42
42
  let dynamicDsCreated = false;
43
43
  let reindexBlockHeight = null;
44
- const datasources = await this.projectService.getAllDataSources(blockHeight);
45
44
  // Check that we have valid datasources
46
- this.assertDataSources(datasources, blockHeight);
45
+ this.assertDataSources(dataSources, blockHeight);
47
46
  reindexBlockHeight = await this.processUnfinalizedBlocks(block);
48
47
  // Only index block if we're not going to reindex
49
48
  if (!reindexBlockHeight) {
50
- await this.indexBlockData(blockContent, datasources,
49
+ await this.indexBlockData(blockContent, dataSources,
51
50
  // eslint-disable-next-line @typescript-eslint/require-await
52
51
  async (ds) => {
53
52
  const vm = this.sandboxService.getDsProcessorWrapper(ds, this.apiService.api, blockContent);
@@ -59,7 +58,7 @@ let IndexerManager = class IndexerManager {
59
58
  startBlock: blockHeight,
60
59
  });
61
60
  // Push the newly created dynamic ds to be processed this block on any future extrinsics/events
62
- datasources.push(newDs);
61
+ dataSources.push(newDs);
63
62
  dynamicDsCreated = true;
64
63
  }, 'createDynamicDatasource');
65
64
  return vm;
@@ -207,7 +206,7 @@ let IndexerManager = class IndexerManager {
207
206
  __decorate([
208
207
  (0, node_core_1.profiler)(yargs_1.yargsOptions.argv.profiler),
209
208
  __metadata("design:type", Function),
210
- __metadata("design:paramtypes", [Object]),
209
+ __metadata("design:paramtypes", [Object, Array]),
211
210
  __metadata("design:returntype", Promise)
212
211
  ], IndexerManager.prototype, "indexBlock", null);
213
212
  IndexerManager = __decorate([
@@ -216,9 +215,9 @@ IndexerManager = __decorate([
216
215
  __metadata("design:paramtypes", [node_core_1.ApiService,
217
216
  node_core_1.NodeConfig,
218
217
  sandbox_service_1.SandboxService,
218
+ ds_processor_service_1.DsProcessorService,
219
219
  dynamic_ds_service_1.DynamicDsService,
220
220
  unfinalizedBlocks_service_1.UnfinalizedBlocksService,
221
- ds_processor_service_1.DsProcessorService,
222
221
  project_service_1.ProjectService])
223
222
  ], IndexerManager);
224
223
  exports.IndexerManager = IndexerManager;
@@ -1 +1 @@
1
- {"version":3,"file":"indexer.manager.js","sourceRoot":"","sources":["../../src/indexer/indexer.manager.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;AAEtC,2CAAoD;AACpD,4DAUgC;AAChC,gDAQ0B;AAU1B,+DAAkE;AAClE,oCAAwC;AACxC,iEAGgC;AAChC,6DAAwD;AACxD,uDAAmD;AACnD,uDAAmD;AACnD,2EAAuE;AAEvE,MAAM,MAAM,GAAG,IAAA,qBAAS,EAAC,SAAS,CAAC,CAAC;AAGpC,IAAa,cAAc,GAA3B,MAAa,cAAc;IACzB,YACU,UAAsB,EACtB,UAAsB,EACtB,cAA8B,EAC9B,gBAAkC,EAClC,wBAAkD,EAClD,kBAAsC,EACX,cAA8B;QANzD,eAAU,GAAV,UAAU,CAAY;QACtB,eAAU,GAAV,UAAU,CAAY;QACtB,mBAAc,GAAd,cAAc,CAAgB;QAC9B,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,6BAAwB,GAAxB,wBAAwB,CAA0B;QAClD,uBAAkB,GAAlB,kBAAkB,CAAoB;QACX,mBAAc,GAAd,cAAc,CAAgB;QAEjE,MAAM,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC;IACvC,CAAC;IAGD,KAAK,CAAC,UAAU,CACd,YAAkC;QAElC,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,GAAG,YAAY,CAAC;QAC5C,IAAI,gBAAgB,GAAG,KAAK,CAAC;QAC7B,IAAI,kBAAkB,GAAG,IAAI,CAAC;QAE9B,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,iBAAiB,CAC7D,WAAW,CACZ,CAAC;QAEF,uCAAuC;QACvC,IAAI,CAAC,iBAAiB,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC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AC;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 { Inject, Injectable } from '@nestjs/common';\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 NodeConfig,\n getLogger,\n profiler,\n profilerWrap,\n IndexerSandbox,\n ProcessBlockResponse,\n ApiService,\n} from '@subql/node-core';\nimport {\n EthereumTransaction,\n EthereumLog,\n SubqlRuntimeHandler,\n EthereumBlockWrapper,\n EthereumBlock,\n} from '@subql/types-ethereum';\nimport { SubqlProjectDs } 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 logger = getLogger('indexer');\n\n@Injectable()\nexport class IndexerManager {\n constructor(\n private apiService: ApiService,\n private nodeConfig: NodeConfig,\n private sandboxService: SandboxService,\n private dynamicDsService: DynamicDsService,\n private unfinalizedBlocksService: UnfinalizedBlocksService,\n private dsProcessorService: DsProcessorService,\n @Inject('IProjectService') private projectService: ProjectService,\n ) {\n logger.info('indexer manager start');\n }\n\n @profiler(yargsOptions.argv.profiler)\n async indexBlock(\n blockContent: EthereumBlockWrapper,\n ): Promise<ProcessBlockResponse> {\n const { block, blockHeight } = blockContent;\n let dynamicDsCreated = false;\n let reindexBlockHeight = null;\n\n const datasources = await this.projectService.getAllDataSources(\n blockHeight,\n );\n\n // Check that we have valid datasources\n this.assertDataSources(datasources, blockHeight);\n reindexBlockHeight = await this.processUnfinalizedBlocks(block);\n\n // Only index block if we're not going to reindex\n if (!reindexBlockHeight) {\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.apiService.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 );\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\n return {\n dynamicDsCreated,\n blockHash: block.hash,\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 ): Promise<number | null> {\n if (this.nodeConfig.unfinalizedBlocks) {\n return this.unfinalizedBlocksService.processUnfinalizedBlocks(block);\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, 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.indexTransaction(tx, dataSources, getVM);\n\n for (const log of tx.logs ?? []) {\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 indexTransaction(\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.apiService.api)(\n data,\n ds,\n );\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.apiService.api)(\n data,\n ds,\n );\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.apiService.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.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AAG,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 { Inject, Injectable } from '@nestjs/common';\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 NodeConfig,\n getLogger,\n profiler,\n profilerWrap,\n IndexerSandbox,\n ProcessBlockResponse,\n ApiService,\n IIndexerManager,\n} from '@subql/node-core';\nimport {\n EthereumTransaction,\n EthereumLog,\n SubqlRuntimeHandler,\n EthereumBlockWrapper,\n EthereumBlock,\n} from '@subql/types-ethereum';\nimport { SubqlProjectDs } 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 logger = getLogger('indexer');\n\n@Injectable()\nexport class IndexerManager\n implements IIndexerManager<EthereumBlockWrapper, SubqlProjectDs>\n{\n constructor(\n private apiService: ApiService,\n private nodeConfig: NodeConfig,\n private sandboxService: SandboxService,\n private dsProcessorService: DsProcessorService,\n private dynamicDsService: DynamicDsService,\n private unfinalizedBlocksService: UnfinalizedBlocksService,\n @Inject('IProjectService') private projectService: ProjectService,\n ) {\n logger.info('indexer manager start');\n }\n\n @profiler(yargsOptions.argv.profiler)\n async indexBlock(\n blockContent: EthereumBlockWrapper,\n dataSources: SubqlProjectDs[],\n ): Promise<ProcessBlockResponse> {\n const { block, blockHeight } = blockContent;\n let dynamicDsCreated = false;\n let reindexBlockHeight = null;\n\n // Check that we have valid datasources\n this.assertDataSources(dataSources, blockHeight);\n reindexBlockHeight = await this.processUnfinalizedBlocks(block);\n\n // Only index block if we're not going to reindex\n if (!reindexBlockHeight) {\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.apiService.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 );\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\n return {\n dynamicDsCreated,\n blockHash: block.hash,\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 ): Promise<number | null> {\n if (this.nodeConfig.unfinalizedBlocks) {\n return this.unfinalizedBlocksService.processUnfinalizedBlocks(block);\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, 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.indexTransaction(tx, dataSources, getVM);\n\n for (const log of tx.logs ?? []) {\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 indexTransaction(\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.apiService.api)(\n data,\n ds,\n );\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.apiService.api)(\n data,\n ds,\n );\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.apiService.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 +1 @@
1
- {"version":3,"file":"indexer.module.js","sourceRoot":"","sources":["../../src/indexer/indexer.module.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;AAEtC,mDAA8C;AAC9C,2CAAwC;AACxC,yDAAsD;AACtD,gDAQ0B;AAE1B,0CAAiD;AAEjD,iEAA4D;AAC5D,6DAAwD;AACxD,uDAAmD;AACnD,uDAAmD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CAAG;AAAhB,sCAAa","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { isMainThread } from 'worker_threads';\nimport { Module } from '@nestjs/common';\nimport { EventEmitter2 } from '@nestjs/event-emitter';\nimport {\n ApiService,\n StoreService,\n PoiService,\n MmrService,\n ConnectionPoolService,\n StoreCacheService,\n WorkerDynamicDsService,\n} from '@subql/node-core';\nimport { SubqueryProject } from '../configure/SubqueryProject';\nimport { EthereumApiService } from '../ethereum';\nimport { EthereumApiConnection } from '../ethereum/api.connection';\nimport { DsProcessorService } from './ds-processor.service';\nimport { DynamicDsService } from './dynamic-ds.service';\nimport { IndexerManager } from './indexer.manager';\nimport { ProjectService } from './project.service';\nimport { SandboxService } from './sandbox.service';\nimport { UnfinalizedBlocksService } from './unfinalizedBlocks.service';\nimport { WorkerService } from './worker/worker.service';\nimport { WorkerUnfinalizedBlocksService } from './worker/worker.unfinalizedBlocks.service';\n\n@Module({\n providers: [\n IndexerManager,\n StoreCacheService,\n StoreService,\n ConnectionPoolService,\n EthereumApiService,\n {\n provide: ApiService,\n useFactory: async (\n project: SubqueryProject,\n connectionPoolService: ConnectionPoolService<EthereumApiConnection>,\n eventEmitter: EventEmitter2,\n ) => {\n const apiService = new EthereumApiService(\n project,\n connectionPoolService,\n eventEmitter,\n );\n await apiService.init();\n return apiService;\n },\n inject: ['ISubqueryProject', ConnectionPoolService, EventEmitter2],\n },\n SandboxService,\n DsProcessorService,\n {\n provide: DynamicDsService,\n useFactory: () => {\n if (isMainThread) {\n throw new Error('Expected to be worker thread');\n }\n return new WorkerDynamicDsService((global as any).host);\n },\n },\n PoiService,\n MmrService,\n {\n provide: 'IProjectService',\n useClass: ProjectService,\n },\n WorkerService,\n {\n provide: UnfinalizedBlocksService,\n useFactory: () => {\n if (isMainThread) {\n throw new Error('Expected to be worker thread');\n }\n return new WorkerUnfinalizedBlocksService((global as any).host);\n },\n },\n ],\n exports: [StoreService],\n})\nexport class IndexerModule {}\n"]}
1
+ {"version":3,"file":"indexer.module.js","sourceRoot":"","sources":["../../src/indexer/indexer.module.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;AAEtC,mDAA8C;AAC9C,2CAAwC;AACxC,yDAAsD;AACtD,gDAQ0B;AAE1B,0CAAiD;AAEjD,iEAA4D;AAC5D,6DAAwD;AACxD,uDAAmD;AACnD,uDAAmD;AACnD,uDAAmD;AACnD,2EAAuE;AACvE,4DAAwD;AACxD,gGAA2F;AAwDpF,IAAM,aAAa,GAAnB,MAAM,aAAa;CAAG,CAAA;AAAhB,aAAa;IAtDzB,IAAA,eAAM,EAAC;QACN,SAAS,EAAE;YACT,gCAAc;YACd,6BAAiB;YACjB,wBAAY;YACZ,iCAAqB;YACrB,6BAAkB;YAClB;gBACE,OAAO,EAAE,sBAAU;gBACnB,UAAU,EAAE,KAAK,EACf,OAAwB,EACxB,qBAAmE,EACnE,YAA2B,EAC3B,EAAE;oBACF,MAAM,UAAU,GAAG,IAAI,6BAAkB,CACvC,OAAO,EACP,qBAAqB,EACrB,YAAY,CACb,CAAC;oBACF,MAAM,UAAU,CAAC,IAAI,EAAE,CAAC;oBACxB,OAAO,UAAU,CAAC;gBACpB,CAAC;gBACD,MAAM,EAAE,CAAC,kBAAkB,EAAE,iCAAqB,EAAE,6BAAa,CAAC;aACnE;YACD,gCAAc;YACd,yCAAkB;YAClB;gBACE,OAAO,EAAE,qCAAgB;gBACzB,UAAU,EAAE,GAAG,EAAE;oBACf,IAAI,6BAAY,EAAE;wBAChB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;qBACjD;oBACD,OAAO,IAAI,kCAAsB,CAAE,MAAc,CAAC,IAAI,CAAC,CAAC;gBAC1D,CAAC;aACF;YACD,sBAAU;YACV,sBAAU;YACV;gBACE,OAAO,EAAE,iBAAiB;gBAC1B,QAAQ,EAAE,gCAAc;aACzB;YACD,8BAAa;YACb;gBACE,OAAO,EAAE,oDAAwB;gBACjC,UAAU,EAAE,GAAG,EAAE;oBACf,IAAI,6BAAY,EAAE;wBAChB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;qBACjD;oBACD,OAAO,IAAI,iEAA8B,CAAE,MAAc,CAAC,IAAI,CAAC,CAAC;gBAClE,CAAC;aACF;SACF;QACD,OAAO,EAAE,CAAC,wBAAY,CAAC;KACxB,CAAC;GACW,aAAa,CAAG;AAAhB,sCAAa","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { isMainThread } from 'worker_threads';\nimport { Module } from '@nestjs/common';\nimport { EventEmitter2 } from '@nestjs/event-emitter';\nimport {\n ApiService,\n StoreService,\n PoiService,\n MmrService,\n ConnectionPoolService,\n StoreCacheService,\n WorkerDynamicDsService,\n} from '@subql/node-core';\nimport { SubqueryProject } from '../configure/SubqueryProject';\nimport { EthereumApiService } from '../ethereum';\nimport { EthereumApiConnection } from '../ethereum/api.connection';\nimport { DsProcessorService } from './ds-processor.service';\nimport { DynamicDsService } from './dynamic-ds.service';\nimport { IndexerManager } from './indexer.manager';\nimport { ProjectService } from './project.service';\nimport { SandboxService } from './sandbox.service';\nimport { UnfinalizedBlocksService } from './unfinalizedBlocks.service';\nimport { WorkerService } from './worker/worker.service';\nimport { WorkerUnfinalizedBlocksService } from './worker/worker.unfinalizedBlocks.service';\n\n@Module({\n providers: [\n IndexerManager,\n StoreCacheService,\n StoreService,\n ConnectionPoolService,\n EthereumApiService,\n {\n provide: ApiService,\n useFactory: async (\n project: SubqueryProject,\n connectionPoolService: ConnectionPoolService<EthereumApiConnection>,\n eventEmitter: EventEmitter2,\n ) => {\n const apiService = new EthereumApiService(\n project,\n connectionPoolService,\n eventEmitter,\n );\n await apiService.init();\n return apiService;\n },\n inject: ['ISubqueryProject', ConnectionPoolService, EventEmitter2],\n },\n SandboxService,\n DsProcessorService,\n {\n provide: DynamicDsService,\n useFactory: () => {\n if (isMainThread) {\n throw new Error('Expected to be worker thread');\n }\n return new WorkerDynamicDsService((global as any).host);\n },\n },\n PoiService,\n MmrService,\n {\n provide: 'IProjectService',\n useClass: ProjectService,\n },\n WorkerService,\n {\n provide: UnfinalizedBlocksService,\n useFactory: () => {\n if (isMainThread) {\n throw new Error('Expected to be worker thread');\n }\n return new WorkerUnfinalizedBlocksService((global as any).host);\n },\n },\n ],\n exports: [StoreService],\n})\nexport class IndexerModule {}\n"]}
@@ -5,7 +5,7 @@ import { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';
5
5
  import { DsProcessorService } from './ds-processor.service';
6
6
  import { DynamicDsService } from './dynamic-ds.service';
7
7
  import { UnfinalizedBlocksService } from './unfinalizedBlocks.service';
8
- export declare class ProjectService implements IProjectService {
8
+ export declare class ProjectService implements IProjectService<SubqlProjectDs> {
9
9
  private readonly dsProcessorService;
10
10
  private readonly apiService;
11
11
  private readonly poiService;
@@ -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,gDAU0B;AAC1B,yCAAsC;AACtC,kEAIsC;AACtC,8CAAqE;AACrE,8CAA2C;AAC3C,iEAA4D;AAC5D,6DAAwD;AACxD,2EAAuE;AAEvE,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;AAGpC,IAAa,cAAc,GAA3B,MAAa,cAAc;IAKzB,YACmB,kBAAsC,EACtC,UAAsB,EACtB,UAAsB,EACpB,UAAsB,EACxB,SAAoB,EACQ,OAAwB,EACpD,YAA0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AAC,GAAG,CAAC,GAAG,eAAe,CAAC,CAAC;SACrC;IACH,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,iBAAyB;QACrC,MAAM,mBAAmB,GAAG,MAAM,IAAI,CAAC,sBAAsB,EAAE,CAAC;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,uBAAuB,EAC5B,IAAI,CAAC,gBAAgB,EACrB,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,SAAS,CAEf,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,iBAAiB,CAAC,WAAmB;QACzC,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,gBAAgB,CAAC,qBAAqB,EAAE,CAAC;QAEtE,OAAO,CAAC,GAAG,IAAI,CAAC,WAAW,EAAE,GAAG,SAAS,CAAC,CAAC,MAAM,CAC/C,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,UAAU,IAAI,WAAW,CACrC,CAAC;IACJ,CAAC;CACF,CAAA;AArRY,cAAc;IAD1B,IAAA,mBAAU,GAAE;IAYR,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;GAhBhD,cAAc,CAqR1B;AArRY,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 IProjectService,\n NodeConfig,\n IndexerEvent,\n StoreService,\n PoiService,\n MmrService,\n getLogger,\n getExistingProjectSchema,\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 { UnfinalizedBlocksService } 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 implements IProjectService {\n private _schema: string;\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 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 await this.ensureMetadata();\n this.dynamicDsService.init(this.storeService.storeCache.metadata);\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();\n }\n\n this._startHeight = await this.getStartHeight();\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.reindex.bind(this),\n );\n\n if (reindexedTo !== undefined) {\n this._startHeight = reindexedTo;\n }\n\n // Flush any pending operations to setup DB\n await this.storeService.storeCache.flushCache(true);\n } else {\n this._schema = await this.getExistingProjectSchema();\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();\n }\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<void> {\n const metadata = this.storeService.storeCache.metadata;\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 'processedBlockCount',\n 'lastFinalizedVerifiedHeight',\n 'schemaMigrationCount',\n ] as const;\n\n const existing = await metadata.findMany(keys);\n\n const { chain, genesisHash, specName } = this.apiService.networkMeta;\n\n if (this.project.runner) {\n const { node, query } = this.project.runner;\n\n metadata.setBulk([\n { key: 'runnerNode', value: node.name },\n { key: 'runnerNodeVersion', value: node.version },\n { key: 'runnerQuery', value: query.name },\n { key: 'runnerQueryVersion', value: query.version },\n ]);\n }\n if (!existing.genesisHash) {\n metadata.set('genesisHash', 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 existing.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 (existing.chain !== chain) {\n metadata.set('chain', chain);\n }\n\n if (existing.specName !== specName) {\n metadata.set('specName', 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 (!existing.processedBlockCount && !existing.lastProcessedHeight) {\n metadata.set('processedBlockCount', 0);\n }\n\n if (existing.indexerNodeVersion !== packageVersion) {\n metadata.set('indexerNodeVersion', packageVersion);\n }\n if (!existing.schemaMigrationCount) {\n metadata.set('schemaMigrationCount', 0);\n }\n if (!existing.startHeight) {\n metadata.set('startHeight', this.getStartBlockFromDataSources());\n }\n }\n\n private async getMetadataBlockOffset(): Promise<number | undefined> {\n return this.storeService.storeCache.metadata.find('blockOffset');\n }\n\n private async getLastProcessedHeight(): Promise<number | undefined> {\n return this.storeService.storeCache.metadata.find('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 !== undefined ||\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.syncFileBaseFromPoi(offset).catch((err) => {\n logger.error(err, 'failed to sync poi to mmr');\n process.exit(1);\n });\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.unfinalizedBlockService,\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"]}
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,gDAU0B;AAC1B,yCAAsC;AACtC,kEAIsC;AACtC,8CAAqE;AACrE,8CAA2C;AAC3C,iEAA4D;AAC5D,6DAAwD;AACxD,2EAAuE;AAEvE,8DAA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AAY;QACd,sBAAU;QACJ,qCAAgB;QAC7B,6BAAa;QACF,oDAAwB;GAhBhD,cAAc,CAqR1B;AArRY,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 IProjectService,\n NodeConfig,\n IndexerEvent,\n StoreService,\n PoiService,\n MmrService,\n getLogger,\n getExistingProjectSchema,\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 { UnfinalizedBlocksService } 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 implements IProjectService<SubqlProjectDs> {\n private _schema: string;\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 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 await this.ensureMetadata();\n this.dynamicDsService.init(this.storeService.storeCache.metadata);\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();\n }\n\n this._startHeight = await this.getStartHeight();\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.reindex.bind(this),\n );\n\n if (reindexedTo !== undefined) {\n this._startHeight = reindexedTo;\n }\n\n // Flush any pending operations to setup DB\n await this.storeService.storeCache.flushCache(true);\n } else {\n this._schema = await this.getExistingProjectSchema();\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();\n }\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<void> {\n const metadata = this.storeService.storeCache.metadata;\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 'processedBlockCount',\n 'lastFinalizedVerifiedHeight',\n 'schemaMigrationCount',\n ] as const;\n\n const existing = await metadata.findMany(keys);\n\n const { chain, genesisHash, specName } = this.apiService.networkMeta;\n\n if (this.project.runner) {\n const { node, query } = this.project.runner;\n\n metadata.setBulk([\n { key: 'runnerNode', value: node.name },\n { key: 'runnerNodeVersion', value: node.version },\n { key: 'runnerQuery', value: query.name },\n { key: 'runnerQueryVersion', value: query.version },\n ]);\n }\n if (!existing.genesisHash) {\n metadata.set('genesisHash', 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 existing.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 (existing.chain !== chain) {\n metadata.set('chain', chain);\n }\n\n if (existing.specName !== specName) {\n metadata.set('specName', 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 (!existing.processedBlockCount && !existing.lastProcessedHeight) {\n metadata.set('processedBlockCount', 0);\n }\n\n if (existing.indexerNodeVersion !== packageVersion) {\n metadata.set('indexerNodeVersion', packageVersion);\n }\n if (!existing.schemaMigrationCount) {\n metadata.set('schemaMigrationCount', 0);\n }\n if (!existing.startHeight) {\n metadata.set('startHeight', this.getStartBlockFromDataSources());\n }\n }\n\n private async getMetadataBlockOffset(): Promise<number | undefined> {\n return this.storeService.storeCache.metadata.find('blockOffset');\n }\n\n private async getLastProcessedHeight(): Promise<number | undefined> {\n return this.storeService.storeCache.metadata.find('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 !== undefined ||\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.syncFileBaseFromPoi(offset).catch((err) => {\n logger.error(err, 'failed to sync poi to mmr');\n process.exit(1);\n });\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.unfinalizedBlockService,\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"]}
@@ -39,7 +39,7 @@ let SandboxService = class SandboxService {
39
39
  // api: await this.apiService.getPatchedApi(),
40
40
  store,
41
41
  root: this.project.root,
42
- script: ds.mapping.entryScript,
42
+ // script: ds.mapping.entryScript,
43
43
  entry,
44
44
  }, this.nodeConfig);
45
45
  this.processorCache[entry] = processor;
@@ -1 +1 @@
1
- {"version":3,"file":"sandbox.service.js","sourceRoot":"","sources":["../../src/indexer/sandbox.service.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;AAEtC,mDAA8C;AAC9C,2CAAoD;AACpD,4DAGgC;AAChC,gDAK0B;AAG1B,kEAA+E;AAC/E,8CAAmD;AAGnD,IAAa,cAAc,GAA3B,MAAa,cAAc;IAGzB,YACmB,YAA0B,EAC1B,UAAsB,EACM,OAAwB;QAFpD,iBAAY,GAAZ,YAAY,CAAc;QAC1B,eAAU,GAAV,UAAU,CAAY;QACM,YAAO,GAAP,OAAO,CAAiB;QAL/D,mBAAc,GAAmC,EAAE,CAAC;IAMzD,CAAC;IAEJ,qBAAqB,CACnB,EAAkB,EAClB,GAAe,EACf,YAAkC;QAElC,MAAM,KAAK,GAAU,6BAAY;YAC/B,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,QAAQ,EAAE;YAC9B,CAAC,CAAC,IAAA,4BAAgB,EAAE,MAAc,CAAC,IAAI,CAAC,CAAC,CAAC,wBAAwB;QAEpE,MAAM,KAAK,GAAG,IAAI,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC;QAC1C,IAAI,SAAS,GAAG,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAC3C,IAAI,CAAC,SAAS,EAAE;YACd,SAAS,GAAG,IAAI,0BAAc,CAC5B;gBACE,8CAA8C;gBAC9C,KAAK;gBACL,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI;gBACvB,MAAM,EAAE,EAAE,CAAC,OAAO,CAAC,WAAW;gBAC9B,KAAK;aACN,EACD,IAAI,CAAC,UAAU,CAChB,CAAC;YACF,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;SACxC;QACD,GAAG,CAAC,SAAS,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;QACvC,OAAO,SAAS,CAAC;IACnB,CAAC;IAEO,kBAAkB,CAAC,EAA2B;QACpD,IAAI,IAAA,oCAAkB,EAAC,EAAE,CAAC,EAAE;YAC1B,OAAO,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;SACxB;aAAM;YACL,OAAO,IAAA,yBAAe,EAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;SAC3C;IACH,CAAC;CACF,CAAA;AA5CY,cAAc;IAD1B,IAAA,mBAAU,GAAE;IAOR,WAAA,IAAA,eAAM,EAAC,kBAAkB,CAAC,CAAA;qCAFI,wBAAY;QACd,sBAAU;QACe,iCAAe;GAN5D,cAAc,CA4C1B;AA5CY,wCAAc","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { isMainThread } from 'worker_threads';\nimport { Inject, Injectable } from '@nestjs/common';\nimport {\n isDatasourceV0_2_0,\n SubqlEthereumDataSource,\n} from '@subql/common-ethereum';\nimport {\n NodeConfig,\n StoreService,\n IndexerSandbox,\n hostStoreToStore,\n} from '@subql/node-core';\nimport { Store } from '@subql/types';\nimport { ApiWrapper, EthereumBlockWrapper } from '@subql/types-ethereum';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { getProjectEntry } from '../utils/project';\n\n@Injectable()\nexport class SandboxService {\n private processorCache: Record<string, IndexerSandbox> = {};\n\n constructor(\n private readonly storeService: StoreService,\n private readonly nodeConfig: NodeConfig,\n @Inject('ISubqueryProject') private readonly project: SubqueryProject,\n ) {}\n\n getDsProcessorWrapper(\n ds: SubqlProjectDs,\n api: ApiWrapper,\n blockContent: EthereumBlockWrapper,\n ): IndexerSandbox {\n const store: Store = isMainThread\n ? this.storeService.getStore()\n : hostStoreToStore((global as any).host); // Provided in worker.ts\n\n const entry = this.getDataSourceEntry(ds);\n let processor = this.processorCache[entry];\n if (!processor) {\n processor = new IndexerSandbox(\n {\n // api: await this.apiService.getPatchedApi(),\n store,\n root: this.project.root,\n script: ds.mapping.entryScript,\n entry,\n },\n this.nodeConfig,\n );\n this.processorCache[entry] = processor;\n }\n api.freezeApi(processor, blockContent);\n return processor;\n }\n\n private getDataSourceEntry(ds: SubqlEthereumDataSource): string {\n if (isDatasourceV0_2_0(ds)) {\n return ds.mapping.file;\n } else {\n return getProjectEntry(this.project.root);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"sandbox.service.js","sourceRoot":"","sources":["../../src/indexer/sandbox.service.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;AAEtC,mDAA8C;AAC9C,2CAAoD;AACpD,4DAGgC;AAChC,gDAK0B;AAG1B,kEAA+E;AAC/E,8CAAmD;AAG5C,IAAM,cAAc,GAApB,MAAM,cAAc;IAGzB,YACmB,YAA0B,EAC1B,UAAsB,EACM,OAAwB;QAFpD,iBAAY,GAAZ,YAAY,CAAc;QAC1B,eAAU,GAAV,UAAU,CAAY;QACM,YAAO,GAAP,OAAO,CAAiB;QAL/D,mBAAc,GAAmC,EAAE,CAAC;IAMzD,CAAC;IAEJ,qBAAqB,CACnB,EAAkB,EAClB,GAAe,EACf,YAAkC;QAElC,MAAM,KAAK,GAAU,6BAAY;YAC/B,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,QAAQ,EAAE;YAC9B,CAAC,CAAC,IAAA,4BAAgB,EAAE,MAAc,CAAC,IAAI,CAAC,CAAC,CAAC,wBAAwB;QAEpE,MAAM,KAAK,GAAG,IAAI,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC;QAC1C,IAAI,SAAS,GAAG,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAC3C,IAAI,CAAC,SAAS,EAAE;YACd,SAAS,GAAG,IAAI,0BAAc,CAC5B;gBACE,8CAA8C;gBAC9C,KAAK;gBACL,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI;gBACvB,kCAAkC;gBAClC,KAAK;aACN,EACD,IAAI,CAAC,UAAU,CAChB,CAAC;YACF,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;SACxC;QACD,GAAG,CAAC,SAAS,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;QACvC,OAAO,SAAS,CAAC;IACnB,CAAC;IAEO,kBAAkB,CAAC,EAA2B;QACpD,IAAI,IAAA,oCAAkB,EAAC,EAAE,CAAC,EAAE;YAC1B,OAAO,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;SACxB;aAAM;YACL,OAAO,IAAA,yBAAe,EAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;SAC3C;IACH,CAAC;CACF,CAAA;AA5CY,cAAc;IAD1B,IAAA,mBAAU,GAAE;IAOR,WAAA,IAAA,eAAM,EAAC,kBAAkB,CAAC,CAAA;qCAFI,wBAAY;QACd,sBAAU;QACe,iCAAe;GAN5D,cAAc,CA4C1B;AA5CY,wCAAc","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { isMainThread } from 'worker_threads';\nimport { Inject, Injectable } from '@nestjs/common';\nimport {\n isDatasourceV0_2_0,\n SubqlEthereumDataSource,\n} from '@subql/common-ethereum';\nimport {\n NodeConfig,\n StoreService,\n IndexerSandbox,\n hostStoreToStore,\n} from '@subql/node-core';\nimport { Store } from '@subql/types';\nimport { ApiWrapper, EthereumBlockWrapper } from '@subql/types-ethereum';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { getProjectEntry } from '../utils/project';\n\n@Injectable()\nexport class SandboxService {\n private processorCache: Record<string, IndexerSandbox> = {};\n\n constructor(\n private readonly storeService: StoreService,\n private readonly nodeConfig: NodeConfig,\n @Inject('ISubqueryProject') private readonly project: SubqueryProject,\n ) {}\n\n getDsProcessorWrapper(\n ds: SubqlProjectDs,\n api: ApiWrapper,\n blockContent: EthereumBlockWrapper,\n ): IndexerSandbox {\n const store: Store = isMainThread\n ? this.storeService.getStore()\n : hostStoreToStore((global as any).host); // Provided in worker.ts\n\n const entry = this.getDataSourceEntry(ds);\n let processor = this.processorCache[entry];\n if (!processor) {\n processor = new IndexerSandbox(\n {\n // api: await this.apiService.getPatchedApi(),\n store,\n root: this.project.root,\n // script: ds.mapping.entryScript,\n entry,\n },\n this.nodeConfig,\n );\n this.processorCache[entry] = processor;\n }\n api.freezeApi(processor, blockContent);\n return processor;\n }\n\n private getDataSourceEntry(ds: SubqlEthereumDataSource): string {\n if (isDatasourceV0_2_0(ds)) {\n return ds.mapping.file;\n } else {\n return getProjectEntry(this.project.root);\n }\n }\n}\n"]}
@@ -3,9 +3,9 @@ export declare enum OperationType {
3
3
  Set = "Set",
4
4
  Remove = "Remove"
5
5
  }
6
- export declare type OperationEntity = {
6
+ export type OperationEntity = {
7
7
  operation: OperationType;
8
8
  entityType: string;
9
9
  data: Entity | string;
10
10
  };
11
- export declare type BestBlocks = Record<number, string>;
11
+ export type BestBlocks = Record<number, string>;
@@ -3,7 +3,7 @@ import { ApiService, NodeConfig, StoreCacheService } from '@subql/node-core';
3
3
  import { EthereumBlock } from '@subql/types-ethereum';
4
4
  export declare const METADATA_UNFINALIZED_BLOCKS_KEY = "unfinalizedBlocks";
5
5
  export declare const METADATA_LAST_FINALIZED_PROCESSED_KEY = "lastFinalizedVerifiedHeight";
6
- declare type UnfinalizedBlocks = [blockHeight: number, blockHash: string][];
6
+ type UnfinalizedBlocks = [blockHeight: number, blockHash: string][];
7
7
  export interface IUnfinalizedBlocksService {
8
8
  processUnfinalizedBlocks(block: EthereumBlock | undefined): Promise<number | null>;
9
9
  }