Skip to content

Commit

Permalink
historical timestamp and db store (#130)
Browse files Browse the repository at this point in the history
* historical timestamp and db store

* fix inject

* fix testing.module.ts
  • Loading branch information
yoozo authored Nov 26, 2024
1 parent 5876208 commit 4e4ee31
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 214 deletions.
2 changes: 2 additions & 0 deletions packages/common-near/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Bump `@subql/common` dependency

## [4.1.4] - 2024-10-23
### Changed
Expand Down
2 changes: 1 addition & 1 deletion packages/common-near/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"main": "dist/index.js",
"license": "GPL-3.0",
"dependencies": {
"@subql/common": "^5.1.4",
"@subql/common": "^5.2.1",
"@subql/types-near": "workspace:*",
"js-yaml": "^4.1.0",
"reflect-metadata": "^0.1.13"
Expand Down
4 changes: 4 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Support for historical indexing by timestamp as well as block height
- Add an `--enable-cache` flag, allowing you to choose between DB or cache for IO operations.


## [4.0.5] - 2024-10-23
### Changed
Expand Down
4 changes: 2 additions & 2 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"@nestjs/event-emitter": "^2.0.0",
"@nestjs/platform-express": "^9.4.0",
"@nestjs/schedule": "^3.0.1",
"@subql/common": "^5.1.4",
"@subql/common": "^5.2.1",
"@subql/common-near": "workspace:*",
"@subql/node-core": "^14.1.6",
"@subql/node-core": "^15.0.3",
"@subql/types-near": "workspace:*",
"lodash": "^4.17.21",
"near-api-js": "^2.1.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import {
NodeConfig,
StoreCacheService,
IStoreModelProvider,
StoreService,
IProjectService,
BlockDispatcher,
Expand Down Expand Up @@ -37,7 +37,7 @@ export class BlockDispatcherService
@Inject('IProjectUpgradeService')
projectUpgradeService: IProjectUpgradeService,
storeService: StoreService,
storeCacheService: StoreCacheService,
@Inject('IStoreModelProvider') storeModelProvider: IStoreModelProvider,
poiSyncService: PoiSyncService,
@Inject('ISubqueryProject') project: SubqueryProject,
) {
Expand All @@ -47,7 +47,7 @@ export class BlockDispatcherService
projectService,
projectUpgradeService,
storeService,
storeCacheService,
storeModelProvider,
poiSyncService,
project,
async (blockNums: number[]): Promise<IBlock<BlockContent>[]> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
import {
NodeConfig,
StoreService,
StoreCacheService,
IStoreModelProvider,
IProjectService,
WorkerBlockDispatcher,
ConnectionPoolStateManager,
Expand All @@ -24,6 +24,7 @@ import { NearApiConnection } from '../nearApi.connection';
import { BlockContent } from '../types';
import { UnfinalizedBlocksService } from '../unfinalizedBlocks.service';
import { IIndexerWorker } from '../worker/worker';
import { FetchBlockResponse } from '../worker/worker.service';

type IndexerWorker = IIndexerWorker & {
terminate: () => Promise<number>;
Expand All @@ -42,7 +43,7 @@ export class WorkerBlockDispatcherService
projectUpgradeService: IProjectUpgradeService,
cacheService: InMemoryCacheService,
storeService: StoreService,
storeCacheService: StoreCacheService,
@Inject('IStoreModelProvider') storeModelProvider: IStoreModelProvider,
poiSyncService: PoiSyncService,
@Inject('ISubqueryProject') project: SubqueryProject,
dynamicDsService: DynamicDsService,
Expand All @@ -56,7 +57,7 @@ export class WorkerBlockDispatcherService
projectService,
projectUpgradeService,
storeService,
storeCacheService,
storeModelProvider,
poiSyncService,
project,
() =>
Expand All @@ -81,10 +82,10 @@ export class WorkerBlockDispatcherService
);
}

protected async fetchBlock(
async fetchBlock(
worker: IndexerWorker,
height: number,
): Promise<void> {
await worker.fetchBlock(height, 0 /* Not used*/);
): Promise<FetchBlockResponse> {
return worker.fetchBlock(height, 0 /* Not used*/);
}
}
10 changes: 5 additions & 5 deletions packages/node/src/indexer/fetch.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
import {
StoreService,
NodeConfig,
StoreCacheService,
IStoreModelProvider,
ConnectionPoolStateManager,
IProjectUpgradeService,
PoiSyncService,
Expand Down Expand Up @@ -55,7 +55,7 @@ import { UnfinalizedBlocksService } from './unfinalizedBlocks.service';
indexerManager: IndexerManager,
cacheService: InMemoryCacheService,
storeService: StoreService,
storeCacheService: StoreCacheService,
storeModelProvider: IStoreModelProvider,
poiSyncService: PoiSyncService,
project: SubqueryProject,
dynamicDsService: DynamicDsService,
Expand All @@ -71,7 +71,7 @@ import { UnfinalizedBlocksService } from './unfinalizedBlocks.service';
projectUpgradeService,
cacheService,
storeService,
storeCacheService,
storeModelProvider,
poiSyncService,
project,
dynamicDsService,
Expand All @@ -87,7 +87,7 @@ import { UnfinalizedBlocksService } from './unfinalizedBlocks.service';
projectService,
projectUpgradeService,
storeService,
storeCacheService,
storeModelProvider,
poiSyncService,
project,
),
Expand All @@ -100,7 +100,7 @@ import { UnfinalizedBlocksService } from './unfinalizedBlocks.service';
IndexerManager,
InMemoryCacheService,
StoreService,
StoreCacheService,
'IStoreModelProvider',
PoiSyncService,
'ISubqueryProject',
DynamicDsService,
Expand Down
6 changes: 3 additions & 3 deletions packages/node/src/indexer/fetch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
NodeConfig,
BaseFetchService,
getModulos,
StoreCacheService,
IStoreModelProvider,
Header,
} from '@subql/node-core';
import { NearBlock, NearDatasource } from '@subql/types-near';
Expand Down Expand Up @@ -45,7 +45,7 @@ export class FetchService extends BaseFetchService<
unfinalizedBlocksService: UnfinalizedBlocksService,
eventEmitter: EventEmitter2,
schedulerRegistry: SchedulerRegistry,
storeCacheService: StoreCacheService,
@Inject('IStoreModelProvider') storeModelProvider: IStoreModelProvider,
) {
super(
nodeConfig,
Expand All @@ -55,7 +55,7 @@ export class FetchService extends BaseFetchService<
eventEmitter,
schedulerRegistry,
unfinalizedBlocksService,
storeCacheService,
storeModelProvider,
);
}

Expand Down
10 changes: 5 additions & 5 deletions packages/node/src/indexer/unfinalizedBlocks.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import {
BaseUnfinalizedBlocksService,
Header,
NodeConfig,
StoreCacheService,
IStoreModelProvider,
mainThreadOnly,
} from '@subql/node-core';
import { nearHeaderToHeader } from '../utils/near';
Expand All @@ -18,9 +18,9 @@ export class UnfinalizedBlocksService extends BaseUnfinalizedBlocksService<Block
constructor(
private readonly apiService: ApiService,
nodeConfig: NodeConfig,
storeCache: StoreCacheService,
@Inject('IStoreModelProvider') storeModelProvider: IStoreModelProvider,
) {
super(nodeConfig, storeCache);
super(nodeConfig, storeModelProvider);
}

@mainThreadOnly()
Expand All @@ -38,7 +38,7 @@ export class UnfinalizedBlocksService extends BaseUnfinalizedBlocksService<Block
}

@mainThreadOnly()
protected async getHeaderForHeight(height: number): Promise<Header> {
async getHeaderForHeight(height: number): Promise<Header> {
return nearHeaderToHeader(
(await this.apiService.api.block({ blockId: height })).header,
);
Expand Down
8 changes: 4 additions & 4 deletions packages/node/src/indexer/worker/worker.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import {
IProjectUpgradeService,
BlockUnavailableError,
IBlock,
Header,
} from '@subql/node-core';

import { NearDatasource } from '@subql/types-near';
import { nearHeaderToHeader } from '../../utils/near';
import { ApiService } from '../api.service';
import { IndexerManager } from '../indexer.manager';
import { BlockContent } from '../types';

export type FetchBlockResponse = { parentHash: string } | undefined;
export type FetchBlockResponse = Header;

@Injectable()
export class WorkerService extends BaseWorkerService<
Expand Down Expand Up @@ -47,9 +49,7 @@ export class WorkerService extends BaseWorkerService<
}

protected toBlockResponse(block: BlockContent): FetchBlockResponse {
return {
parentHash: block?.block.header.prev_hash,
};
return nearHeaderToHeader(block.block.header);
}

protected async processFetchedBlock(
Expand Down
12 changes: 9 additions & 3 deletions packages/node/src/subcommands/reindex.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
// SPDX-License-Identifier: GPL-3.0

import { Module } from '@nestjs/common';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter';
import { SchedulerRegistry } from '@nestjs/schedule';
import {
DbModule,
ForceCleanService,
PoiService,
ReindexService,
StoreCacheService,
StoreService,
storeModelFactory,
NodeConfig,
} from '@subql/node-core';
import { Sequelize } from '@subql/x-sequelize';
import { ConfigureModule } from '../configure/configure.module';
import { ApiService } from '../indexer/api.service';
import { DsProcessorService } from '../indexer/ds-processor.service';
Expand All @@ -20,7 +22,11 @@ import { UnfinalizedBlocksService } from '../indexer/unfinalizedBlocks.service';

@Module({
providers: [
StoreCacheService,
{
provide: 'IStoreModelProvider',
useFactory: storeModelFactory,
inject: [NodeConfig, EventEmitter2, SchedulerRegistry, Sequelize],
},
StoreService,
ReindexService,
ForceCleanService,
Expand Down
7 changes: 7 additions & 0 deletions packages/node/src/subcommands/testing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
StoreService,
TestRunner,
SandboxService,
storeModelFactory,
} from '@subql/node-core';
import { Sequelize } from '@subql/x-sequelize';
import { ConfigureModule } from '../configure/configure.module';
import { ApiService } from '../indexer/api.service';
import { DsProcessorService } from '../indexer/ds-processor.service';
Expand All @@ -28,6 +30,11 @@ import { UnfinalizedBlocksService } from '../indexer/unfinalizedBlocks.service';
providers: [
InMemoryCacheService,
StoreService,
{
provide: 'IStoreModelProvider',
useFactory: storeModelFactory,
inject: [NodeConfig, EventEmitter2, SchedulerRegistry, Sequelize],
},
EventEmitter2,
PoiService,
PoiSyncService,
Expand Down
Loading

0 comments on commit 4e4ee31

Please sign in to comment.