Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize scan speed 3 #248

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/crawler/src/sdk/sdk.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export class SdkService {
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {}

getLastBlockHash(): Promise<string> {
return this.sdk.api.rpc.chain.getHeader().then(header => header.hash.toString());
}

@SdkCache('getApi')
async getApi(hash) {
const optionUpgrade = await this.sdk.api.query.system.events.at(hash);
Expand Down
26 changes: 25 additions & 1 deletion apps/crawler/src/subscribers/blocks.subscriber.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { BlockEntity } from '@unique-nft/harvester/src/database/entities';
import { SdkService } from '../sdk/sdk.service';
import { EventName } from '@common/constants';
import { Block } from '@entities/Block';
import { setIntervalAsync } from 'set-interval-async';

export interface IBlockCommonData {
blockNumber: number;
Expand Down Expand Up @@ -68,6 +69,10 @@ export class BlocksSubscriberService implements ISubscriberService {

private spec: ISpecSystemVersion | null = null;
private blankBlocks: BlockEntity[] = [];
private readFromHead = false;
private cutOff = false;
private readFromHeadInterval;
private lastHandledBlockHash = '';
constructor(
private blockService: BlockService,

Expand All @@ -90,6 +95,17 @@ export class BlocksSubscriberService implements ISubscriberService {
const chainProps = await this.sdkService.getChainProperties();
const stateNumber = await this.harvesterStore.getState();


this.readFromHeadInterval = setInterval(async () => {
if (
(await this.sdkService.getLastBlockHash()) === this.lastHandledBlockHash
) {
clearInterval(this.readFromHeadInterval);
this.readFromHead = true;
}
this.cutOff = true;
}, 60_000);

console.dir(stateNumber);
for await (const block of this.reader.readBlocks(
stateNumber[0],
Expand All @@ -99,8 +115,15 @@ export class BlocksSubscriberService implements ISubscriberService {
if (!this.spec) {
this.spec = await this.sdkService.getSpecLastUpgrade(block.parentHash);
}
this.lastHandledBlockHash = block.hash;
const { isBlank } = this.collectEventsCount(block.extrinsics);
if (!isBlank || this.blankBlocks.length >= 1000) {
if (
this.readFromHead ||
this.cutOff ||
!isBlank ||
this.blankBlocks.length >= 1000
) {
this.cutOff = false;
if (this.blankBlocks.length) {
this.logger.log(`Write ${this.blankBlocks.length} blank blocks`);
await this.handleBlankBlocks(this.blankBlocks);
Expand All @@ -112,6 +135,7 @@ export class BlocksSubscriberService implements ISubscriberService {
this.blankBlocks.push(block);
}
}

}

private getBlockCommonData(block: BlockEntity): Block {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polkastats-backend-uniquenetwork",
"version": "2.0.25",
"version": "2.0.26",
"description": "",
"author": "Unique Network Team",
"private": true,
Expand Down