Skip to content

Commit

Permalink
fix: avoid use queryInfo rpc call (#1169)
Browse files Browse the repository at this point in the history
* avoid use queryInfo rpc call

* fallback to rpc call

* minor improvement

* update fetchQueryFeeDetails
  • Loading branch information
xlc authored Dec 20, 2022
1 parent f922247 commit 907ba92
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions src/services/blocks/BlocksService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ApiPromise } from '@polkadot/api';
import { ApiDecoration } from '@polkadot/api/types';
import { extractAuthor } from '@polkadot/api-derive/type/util';
import { Compact, GenericCall, Option, Struct, Vec } from '@polkadot/types';
import { GenericExtrinsic } from '@polkadot/types/extrinsic';
import {
AccountId32,
Block,
Expand All @@ -27,6 +28,7 @@ import {
EventRecord,
Header,
InclusionFee,
RuntimeDispatchInfo,
Weight,
} from '@polkadot/types/interfaces';
import { AnyJson, Codec, Registry } from '@polkadot/types/types';
Expand Down Expand Up @@ -260,10 +262,7 @@ export class BlocksService extends AbstractService {
class: dispatchClass,
partialFee,
weight,
} = await api.rpc.payment.queryInfo(
block.extrinsics[idx].toHex(),
previousBlockHash
);
} = await this.fetchQueryInfo(block.extrinsics[idx], previousBlockHash);

const transactionPaidFeeEvent = xtEvents.find(
({ method }) =>
Expand All @@ -286,7 +285,7 @@ export class BlocksService extends AbstractService {
);
if (doesQueryFeeDetailsExist === 'available') {
finalPartialFee = await this.fetchQueryFeeDetails(
block.extrinsics[idx].toHex(),
block.extrinsics[idx],
previousBlockHash,
weightInfo.weight,
weight
Expand All @@ -296,7 +295,7 @@ export class BlocksService extends AbstractService {
} else if (doesQueryFeeDetailsExist === 'unknown') {
try {
finalPartialFee = await this.fetchQueryFeeDetails(
block.extrinsics[idx].toHex(),
block.extrinsics[idx],
previousBlockHash,
weightInfo.weight,
weight
Expand Down Expand Up @@ -343,22 +342,36 @@ export class BlocksService extends AbstractService {
/**
* Fetch `payment_queryFeeDetails`.
*
* @param extHex
* @param ext
* @param previousBlockHash
* @param extrinsicSuccessWeight
* @param estWeight
*/
private async fetchQueryFeeDetails(
extHex: `0x${string}`,
ext: GenericExtrinsic,
previousBlockHash: BlockHash,
extrinsicSuccessWeight: Weight,
estWeight: Weight
): Promise<string> {
const { api } = this;
const { inclusionFee } = await api.rpc.payment.queryFeeDetails(
extHex,
previousBlockHash
);
const apiAt = await api.at(previousBlockHash);

let inclusionFee;
if (apiAt.call.transactionPaymentApi.queryFeeDetails) {
const u8a = ext.toU8a();
const result = await apiAt.call.transactionPaymentApi.queryFeeDetails(
u8a,
u8a.length
);
inclusionFee = result.inclusionFee;
} else {
const result = await api.rpc.payment.queryFeeDetails(
ext.toHex(),
previousBlockHash
);
inclusionFee = result.inclusionFee;
}

const finalPartialFee = this.calcPartialFee(
extrinsicSuccessWeight,
estWeight,
Expand All @@ -368,6 +381,27 @@ export class BlocksService extends AbstractService {
return finalPartialFee;
}

/**
* Fetch `payment_queryInfo`.
*
* @param ext
* @param previousBlockHash
*/
private async fetchQueryInfo(
ext: GenericExtrinsic,
previousBlockHash: BlockHash
): Promise<RuntimeDispatchInfo> {
const { api } = this;
const apiAt = await api.at(previousBlockHash);
if (apiAt.call.transactionPaymentApi.queryInfo) {
const u8a = ext.toU8a();
return apiAt.call.transactionPaymentApi.queryInfo(u8a, u8a.length);
} else {
// fallback to rpc call
return api.rpc.payment.queryInfo(ext.toHex(), previousBlockHash);
}
}

/**
* Retrieve the blockHash for the previous block to the one getting queried.
* If the block is the geneisis hash it will return the same blockHash.
Expand Down

0 comments on commit 907ba92

Please sign in to comment.