import { OnApplicationShutdown } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { Sequelize } from '@subql/x-sequelize'; import { NodeConfig } from '../../configure'; import { MetadataRepo } from '../entities'; import { SyncedProofOfIndex } from '../entities/Poi.entity'; import { PlainPoiModel } from '../storeModelProvider/poi'; import { ISubqueryProject } from '../types'; /** * There are two status of POI: * - createdPoi: Poi been created with operationHash, but not include hash and parentHash. Creation of Poi in completed in base-block-dispatcher. * - syncedPoi: appended Poi hash and parentHash, and pois should be continuous * * This PoiSyncService sync Poi from createdPoi to syncedPoi only. And it not uses any cache service. */ export declare class PoiSyncService implements OnApplicationShutdown { protected readonly nodeConfig: NodeConfig; private eventEmitter; private project; private isShutdown; private _poiRepo?; private _metadataRepo?; private _sequelize?; private _latestSyncedPoi?; private _lastFlushTs; private isSyncing; private syncedPoiQueue; constructor(nodeConfig: NodeConfig, eventEmitter: EventEmitter2, project: ISubqueryProject); onApplicationShutdown(): void; get poiRepo(): PlainPoiModel; get metadataRepo(): MetadataRepo; get sequelize(): Sequelize; get projectId(): string; get latestSyncedPoi(): SyncedProofOfIndex; init(schema: string): Promise; syncPoi(exitHeight?: number): Promise; /** * Wait for current sync job stop, then stop the sync loop */ stopSync(): Promise; /** * Clear any cached data in poi sync service * - clear latestSyncedPoi * - clear syncedPoiQueue that waiting to be processed */ clear(): void; /** * Get the record of latestSyncedPoiHeight in metadata table * @private */ private getMetadataLatestSyncedPoi; private validateSyncedPoi; /** * Search backward from startHeight to find the last valid synced POI (with both hash and parentHash). * Uses direct model query for efficiency. * @param startHeight The height to start searching backward from * @private */ private findLastValidSyncedPoi; /** * Get latestSyncedPoi from metadata, and find from the Poi table and set it into the service. * This should only been called when service sync been called and _latestSyncedPoi is not set. * @private */ private syncLatestSyncedPoiFromDb; /** * Read first record from poi table (not cache), then create genesis poi * _latestSyncedPoi exist, return if genesis poi been created . * @private */ private ensureGenesisPoi; /** * Set the _latestSyncedPoi in the Poi sync service, also assert the order * @param poiBlock * @private */ private setLatestSyncedPoi; /** * Create Genesis Poi * @param genesisPoi * @private */ private createGenesisPoi; /** * Take an array of un-synced Pois, and sync them, add hash & parentHash. * If Poi blocks id is not continuous, addDefaultPoiBlocks between them. * After sync, setLatestSyncedPoi in memory * @param poiBlocks * @private */ private syncPoiJob; /** * Add default poi from latestSyncedPoi height to endHeight * @param endHeight * @private */ private addDefaultPoiBlocks; /** * Create new sequelize transaction for Synced Poi, also for metadata * @private */ private newTx; /** * Update latestSyncedPoiHeight in Metadata table immediately * @param height * @param tx * @private */ private updateMetadataSyncedPoi; /** * Append syncPoi to syncedPoiQueue, check flush If queue is full or overtime, then put input block. * @param syncedPoiBlock * @private */ private syncedPoiQueueAppend; /** * Check the poi queue is flushable, either: * - syncedPoiQueue is full * - over flush threshold time (same as store flush), and the queue is not empty * @private */ private isFlushable; /** * Take out synced Poi out from queue and process * Upsert to Poi table * Also, update last upstarted poi to metadata * @private */ private flushSyncedBlocks; }