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

fix: Improve performance of blocks service by dependency injection #1483

Merged
merged 3 commits into from
Aug 27, 2024
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
5 changes: 1 addition & 4 deletions src/services/accounts/AccountsAssetsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,10 @@ export class AccountsAssetsService extends AbstractService {
*/
async fetchAssetBalances(hash: BlockHash, address: string, assets: number[]): Promise<IAccountAssetsBalances> {
const { api } = this;
const historicApi = await api.at(hash);

const [historicApi, { number }] = await Promise.all([api.at(hash), api.rpc.chain.getHeader(hash)]);
// Check if this runtime has the assets pallet
this.checkAssetsError(historicApi);

const { number } = await api.rpc.chain.getHeader(hash);

let response;
if (assets.length === 0) {
/**
Expand Down
4 changes: 1 addition & 3 deletions src/services/accounts/AccountsPoolAssetsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ export class AccountsPoolAssetsService extends AbstractService {
assets: number[],
): Promise<IAccountPoolAssetsBalances> {
const { api } = this;
const historicApi = await api.at(hash);
const [historicApi, { number }] = await Promise.all([api.at(hash), api.rpc.chain.getHeader(hash)]);

// Check if this runtime has the PoolAssets pallet
this.checkPoolAssetsError(historicApi);

const { number } = await api.rpc.chain.getHeader(hash);

let response;
if (assets.length === 0) {
/**
Expand Down
8 changes: 4 additions & 4 deletions src/services/accounts/AccountsStakingPayoutsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ export class AccountsStakingPayoutsService extends AbstractService {
: await this.api.rpc.chain.getBlockHash(earlyErasBlockInfo[era - 1].end);

let reward: Option<u128> = historicApi.registry.createType('Option<u128>');

const blockInfo = await this.api.rpc.chain.getBlock(nextEraStartBlockHash);

const allRecords = await historicApi.query.system.events();
const [blockInfo, allRecords] = await Promise.all([
this.api.rpc.chain.getBlock(nextEraStartBlockHash),
historicApi.query.system.events(),
]);

blockInfo.block.extrinsics.forEach((index) => {
allRecords
Expand Down
29 changes: 23 additions & 6 deletions src/services/blocks/BlocksService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export class BlocksService extends AbstractService {
}

const previousBlockHash = await this.fetchPreviousBlockHash(number);
const prevBlockHistoricApi = await api.at(previousBlockHash);
/**
* Fee calculation logic. This runs the extrinsics concurrently.
*/
Expand All @@ -203,7 +204,17 @@ export class BlocksService extends AbstractService {
for (let idx = 0; idx < block.extrinsics.length; ++idx) {
feeTasks.push(
pQueue.run(async () => {
await this.resolveExtFees(extrinsics, block, idx, noFees, previousBlockHash, specVersion, specName);
await this.resolveExtFees(
extrinsics,
block,
idx,
noFees,
previousBlockHash,
specVersion,
specName,
// Inject historic api here or undefined if not available, should save at least two calls per extrinsic
prevBlockHistoricApi,
);
}),
);
}
Expand Down Expand Up @@ -241,9 +252,9 @@ export class BlocksService extends AbstractService {
previousBlockHash: BlockHash,
specVersion: u32,
specName: Text,
historicApi?: ApiDecoration<'promise'>,
) {
const { api } = this;

if (noFees) {
extrinsics[idx].info = {};
return;
Expand Down Expand Up @@ -340,7 +351,7 @@ export class BlocksService extends AbstractService {
class: dispatchClass,
partialFee,
weight,
} = await this.fetchQueryInfo(block.extrinsics[idx], previousBlockHash);
} = await this.fetchQueryInfo(block.extrinsics[idx], previousBlockHash, historicApi);
const versionedWeight = (weight as Weight).refTime ? (weight as Weight).refTime.unwrap() : (weight as WeightV1);

let finalPartialFee = partialFee.toString(),
Expand All @@ -361,6 +372,7 @@ export class BlocksService extends AbstractService {
previousBlockHash,
weightInfo.weight,
versionedWeight.toString(),
historicApi,
);

dispatchFeeType = 'postDispatch';
Expand All @@ -371,6 +383,7 @@ export class BlocksService extends AbstractService {
previousBlockHash,
weightInfo.weight,
versionedWeight.toString(),
historicApi,
);
dispatchFeeType = 'postDispatch';
this.hasQueryFeeApi.setRegisterWithCall(specVersion.toNumber());
Expand Down Expand Up @@ -402,9 +415,11 @@ export class BlocksService extends AbstractService {
previousBlockHash: BlockHash,
extrinsicSuccessWeight: Weight,
estWeight: string,
historicApi?: ApiDecoration<'promise'>,
): Promise<string> {
const { api } = this;
const apiAt = await api.at(previousBlockHash);
// Get injected historicApi for previousBlockHash or create a new one
const apiAt = historicApi || (await api.at(previousBlockHash));

let inclusionFee;
if (apiAt.call.transactionPaymentApi.queryFeeDetails) {
Expand All @@ -430,9 +445,11 @@ export class BlocksService extends AbstractService {
private async fetchQueryInfo(
ext: GenericExtrinsic,
previousBlockHash: BlockHash,
historicApi?: ApiDecoration<'promise'>,
): Promise<RuntimeDispatchInfo | RuntimeDispatchInfoV1> {
const { api } = this;
const apiAt = await api.at(previousBlockHash);
// Get injected historicApi for previousBlockHash or create a new one
const apiAt = historicApi || (await api.at(previousBlockHash));
if (apiAt.call.transactionPaymentApi.queryInfo) {
const u8a = ext.toU8a();
return apiAt.call.transactionPaymentApi.queryInfo(u8a, u8a.length);
Expand Down Expand Up @@ -682,7 +699,7 @@ export class BlocksService extends AbstractService {
* @param registry type registry of the block the call belongs to
*/
private parseGenericCall(genericCall: GenericCall, registry: Registry): ISanitizedCall {
const newArgs = {};
const newArgs: Record<string, unknown> = {};

// Pull out the struct of arguments to this call
const callArgs = genericCall.get('args') as Struct;
Expand Down
9 changes: 4 additions & 5 deletions src/services/pallets/PalletsStakingProgressService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,18 @@ export class PalletsStakingProgressService extends AbstractService {
api.rpc.chain.getHeader(hash),
]);

let eraElectionStatus;
let eraElectionPromise;
/**
* Polkadot runtimes v0.8.30 and above do not support eraElectionStatus, so we check
* to see if eraElectionStatus is mounted to the api, and if were running on a
* runtime less than v0.8.30 it will return a successful result. If it doesn't
* we do nothing and let `eraElectionStatus` stay undefined.
*/
if (historicApi.query.staking.eraElectionStatus) {
eraElectionStatus = await historicApi.query.staking.eraElectionStatus();
eraElectionPromise = await historicApi.query.staking.eraElectionStatus();
}

const { eraLength, eraProgress, sessionLength, sessionProgress, activeEra } =
await this.deriveSessionAndEraProgress(historicApi);
const [eraElectionStatus, { eraLength, eraProgress, sessionLength, sessionProgress, activeEra }] =
await Promise.all([eraElectionPromise, this.deriveSessionAndEraProgress(historicApi)]);

const unappliedSlashesAtActiveEra = await historicApi.query.staking.unappliedSlashes(activeEra);

Expand Down
Loading