Skip to content

Commit

Permalink
Clean up, log if finalized is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Mar 22, 2023
1 parent 7d09114 commit 5e4ea5d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 42 deletions.
62 changes: 24 additions & 38 deletions packages/node-core/src/db/db.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,32 @@ const sequelizeFactory = (option: SequelizeOption) => async () => {
return sequelize;
};

const buildSequelizeOptions = (nodeConfig: NodeConfig, option: DbOption): SequelizeOption => {
const logger = getLogger('db');
return {
...option,
dialect: 'postgres',
ssl: nodeConfig.isPostgresSecureConnection,
dialectOptions: {
ssl: {
rejectUnauthorized: false,
ca: nodeConfig.postgresCACert,
key: nodeConfig.postgresClientKey,
cert: nodeConfig.postgresClientCert,
},
},
logging: nodeConfig.debug
? (sql: string, timing?: number) => {
// logger.debug(sql);
}
: false,
};
};

@Global()
export class DbModule {
static forRootWithConfig(nodeConfig: NodeConfig, option: DbOption = DEFAULT_DB_OPTION): DynamicModule {
const logger = getLogger('db');
const factory = sequelizeFactory({
...option,
dialect: 'postgres',
ssl: nodeConfig.isPostgresSecureConnection,
dialectOptions: {
ssl: {
rejectUnauthorized: false,
ca: nodeConfig.postgresCACert,
key: nodeConfig.postgresClientKey ?? '',
cert: nodeConfig.postgresClientCert ?? '',
},
},
logging: nodeConfig.debug
? (sql: string, timing?: number) => {
logger.debug(sql);
}
: false,
})();
const factory = sequelizeFactory(buildSequelizeOptions(nodeConfig, option))();

return {
module: DbModule,
Expand All @@ -100,25 +104,7 @@ export class DbModule {
providers: [
{
provide: Sequelize,
useFactory: (nodeConfig: NodeConfig) =>
sequelizeFactory({
...option,
dialect: 'postgres',
ssl: nodeConfig.isPostgresSecureConnection,
dialectOptions: {
ssl: {
rejectUnauthorized: false,
ca: nodeConfig.postgresCACert,
key: nodeConfig.postgresClientKey,
cert: nodeConfig.postgresClientCert,
},
},
logging: nodeConfig.debug
? (sql: string, timing?: number) => {
logger.debug(sql);
}
: false,
})(),
useFactory: (nodeConfig: NodeConfig) => sequelizeFactory(buildSequelizeOptions(nodeConfig, option))(),
inject: [NodeConfig],
},
],
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/indexer/fetch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
IndexerEvent,
NodeConfig,
transformBypassBlocks,
IBlockDispatcher,
} from '@subql/node-core';
import { DictionaryQueryEntry, SubstrateCustomHandler } from '@subql/types';
import { MetaData } from '@subql/utils';
Expand Down Expand Up @@ -438,6 +437,7 @@ export class FetchService implements OnApplicationShutdown {
Math.round(this.batchSizeScale * this.nodeConfig.batchSize),
Math.min(MINIMUM_BATCH_SIZE, this.nodeConfig.batchSize * 3),
);

const latestHeight = this.nodeConfig.unfinalizedBlocks
? this.latestBestHeight
: this.latestFinalizedHeight;
Expand Down
1 change: 0 additions & 1 deletion packages/node/src/indexer/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ export class ProjectService implements IProjectService {
if (reindexedTo !== undefined) {
this._startHeight = reindexedTo;
}

// Flush any pending operations to setup DB
await this.storeService.storeCache.flushCache();
} else {
Expand Down
6 changes: 6 additions & 0 deletions packages/node/src/indexer/unfinalizedBlocks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export class UnfinalizedBlocksService {
async init(
reindex: (targetHeight: number) => Promise<void>,
): Promise<number | undefined> {
logger.info(
`Unfinalized blocks is ${
this.nodeConfig.unfinalizedBlocks ? 'enabled' : 'disabled'
}`,
);
// unfinalized blocks
this.unfinalizedBlocks = await this.getMetadataUnfinalizedBlocks();
this.lastCheckedBlockHeight = await this.getLastFinalizedVerifiedHeight();
Expand Down Expand Up @@ -87,6 +92,7 @@ export class UnfinalizedBlocksService {
}

const forkedHeader = await this.hasForked();

if (!forkedHeader) {
// Remove blocks that are now confirmed finalized
this.deleteFinalizedBlock();
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { NestFactory } from '@nestjs/core';
import { findAvailablePort } from '@subql/common';
import { getLogger, IProjectService, NestLogger } from '@subql/node-core';
import { getLogger, NestLogger } from '@subql/node-core';
import { AppModule } from './app.module';
import { ApiService } from './indexer/api.service';
import { FetchService } from './indexer/fetch.service';
Expand Down
4 changes: 3 additions & 1 deletion packages/node/src/utils/substrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ export async function getBlockByHeight(
});

const block = await api.rpc.chain.getBlock(blockHash).catch((e) => {
logger.error(`failed to fetch Block ${blockHash}`);
logger.error(
`failed to fetch Block hash="${blockHash}" height="${height}"`,
);
throw e;
});
// validate block is valid
Expand Down

0 comments on commit 5e4ea5d

Please sign in to comment.