Skip to content

Commit

Permalink
Merge pull request #238 from UniqueNetwork/fix/harvester-v0.0.12
Browse files Browse the repository at this point in the history
Show logs
  • Loading branch information
icehuntmen committed Mar 24, 2023
2 parents 1c63582 + 68d0aaf commit 8664780
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 80 deletions.
2 changes: 1 addition & 1 deletion apps/crawler/src/crawler.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Scope } from '@nestjs/common';
import { SubscribersService } from './subscribers/subscribers.service';

@Injectable()
Expand Down
160 changes: 84 additions & 76 deletions apps/crawler/src/subscribers/blocks.subscriber.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export interface ItemsBatchProcessingResult {

@Injectable()
export class BlocksSubscriberService implements ISubscriberService {
private readonly logger = new Logger(BlocksSubscriberService.name);
private readonly logger = new Logger(BlocksSubscriberService.name, {
timestamp: true,
});
private isStartBlockService: boolean = false;
constructor(
private blockService: BlockService,
Expand Down Expand Up @@ -93,6 +95,7 @@ export class BlocksSubscriberService implements ISubscriberService {
stateNumber[1],
)) {
await this.upsertHandlerBlock(block);
this.logger.log(`Read block: # ${cyan(block.id)}`);
}
}

Expand All @@ -101,89 +104,94 @@ export class BlocksSubscriberService implements ISubscriberService {
let specDataChain = null;
let specLastUpgrade = null;

console.time(`█ ${yellow(blockData.id)} `);
const { id, timestamp, hash, parentHash, extrinsics } = blockData;
try {
console.time(`█ ${yellow(blockData.id)} `);
const { id, timestamp, hash, parentHash, extrinsics } = blockData;

const countEvents = this.collectEventsCount(extrinsics);
const countEvents = this.collectEventsCount(extrinsics);

// First start
if (specLastUpgrade === null) {
specDataChain = await this.sdkService.getSpecLastUpgrade(hash);
this.isStartBlockService = true;
specLastUpgrade = specDataChain;
}
// First start
if (specLastUpgrade === null) {
specDataChain = await this.sdkService.getSpecLastUpgrade(hash);
this.isStartBlockService = true;
specLastUpgrade = specDataChain;
}

// New spec chain
if (specHashData !== null && this.isStartBlockService) {
specDataChain = await this.sdkService.getSpecLastUpgrade(specHashData);
specLastUpgrade = specDataChain;
}
// New spec chain
if (specHashData !== null && this.isStartBlockService) {
specDataChain = await this.sdkService.getSpecLastUpgrade(specHashData);
specLastUpgrade = specDataChain;
}

const blockTimestamp = new Date(timestamp).getTime();
const blockTimestamp = new Date(timestamp).getTime();

const blockCommonData = {
block_number: +id,
block_hash: hash,
parent_hash: parentHash,
extrinsics_root: '0x000', // TODO: remove this ???
state_root: '0x000', // TODO: remove this ???
...specLastUpgrade,
timestamp: blockTimestamp,
total_events: countEvents.totalEvents,
num_transfers: countEvents.numTransfers,
new_accounts: countEvents.newAccounts,
total_extrinsics: extrinsics.length,
} as unknown as Block;
const blockCommonData = {
block_number: +id,
block_hash: hash,
parent_hash: parentHash,
extrinsics_root: '0x000', // TODO: remove this ???
state_root: '0x000', // TODO: remove this ???
...specLastUpgrade,
timestamp: blockTimestamp,
total_events: countEvents.totalEvents,
num_transfers: countEvents.numTransfers,
new_accounts: countEvents.newAccounts,
total_extrinsics: extrinsics.length,
} as unknown as Block;

const log = {
blockNumber: id,
};
const { speckHash } = await this.eventService.process(
extrinsics,
blockCommonData,
);
specHashData = speckHash;
await Promise.all([
this.blockService.upsert(blockCommonData),
this.extrinsicService.upsert(id, hash, extrinsics, blockTimestamp),
]);
const log = {
blockNumber: id,
};
const { speckHash } = await this.eventService.process(
extrinsics,
blockCommonData,
);
specHashData = speckHash;
await Promise.all([
this.blockService.upsert(blockCommonData),
this.extrinsicService.upsert(id, hash, extrinsics, blockTimestamp),
]);

await this.harvesterStore.updateState(id);
await this.harvesterStore.updateState(id);

// Logger
extrinsics.map((value) => {
if (
value.section !== 'parachainSystem' &&
value.section !== 'timestamp'
) {
console.timeLog(
`█ ${yellow(blockData.id)} `,
green(' ▶'),
blockCommonData.total_events > 2
? magenta(' extrinsic:')
: green(' extrinsic:'),
blockCommonData.total_extrinsics,
blockCommonData.total_events > 2
? magenta(' events:')
: cyan(' events:'),
blockCommonData.total_events,
extrinsics
.map((value) => {
if (
value.section !== 'parachainSystem' &&
value.section !== 'timestamp'
) {
return `${capitalize(value.section)}.${capitalize(
value.method,
)}`;
}
})
.filter((value) => !!value),
specLastUpgrade,
' readed!',
);
}
});
// Logger
extrinsics.map((value) => {
if (
value.section !== 'parachainSystem' &&
value.section !== 'timestamp'
) {
console.timeLog(
`█ ${yellow(blockData.id)} `,
green(' ▶'),
blockCommonData.total_events > 2
? magenta(' extrinsic:')
: green(' extrinsic:'),
blockCommonData.total_extrinsics,
blockCommonData.total_events > 2
? magenta(' events:')
: cyan(' events:'),
blockCommonData.total_events,
extrinsics
.map((value) => {
if (
value.section !== 'parachainSystem' &&
value.section !== 'timestamp'
) {
return `${capitalize(value.section)}.${capitalize(
value.method,
)}`;
}
})
.filter((value) => !!value),
specLastUpgrade.spec_version,
' readed!',
);
}
});
} catch (error) {
this.logger.error({ block: blockData.id, error: error.message || error });
this.sentry.instance().captureException({ block: blockData.id, error });
}
}

// private async upsertHandler(ctx): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.12",
"version": "2.0.13",
"description": "",
"author": "Unique Network Team",
"private": true,
Expand Down

0 comments on commit 8664780

Please sign in to comment.