Skip to content

Commit

Permalink
fix(bug): handle lengthToFee given runtime v9190 (#900)
Browse files Browse the repository at this point in the history
* fix: add v9190 polkadot metadata

* fix: add polkadot registry for v9190

* fix: create getPerByte to properly handle lengthToFee

* cleanup inline comments
  • Loading branch information
TarikGul committed Apr 21, 2022
1 parent 511fcf9 commit 992b458
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
54 changes: 53 additions & 1 deletion src/services/blocks/BlocksService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { ApiPromise } from '@polkadot/api';
import { ApiDecoration } from '@polkadot/api/types';
import { AugmentedConst } from '@polkadot/api/types';
import { PromiseRpcResult } from '@polkadot/api-base/types/rpc';
import { GenericExtrinsic, u128 } from '@polkadot/types';
import { GenericExtrinsic, u128, Vec } from '@polkadot/types';
import { GenericCall } from '@polkadot/types/generic';
import { BlockHash, Hash, SignedBlock } from '@polkadot/types/interfaces';
import { FrameSupportWeightsWeightToFeeCoefficient } from '@polkadot/types/lookup';
import { BadRequest } from 'http-errors';
import LRU from 'lru-cache';

Expand All @@ -13,7 +14,9 @@ import { createCall } from '../../test-helpers/createCall';
import {
kusamaRegistry,
polkadotRegistry,
polkadotRegistryV9190,
} from '../../test-helpers/registries';
import { TypeFactory } from '../../test-helpers/typeFactory';
import { ExtBaseWeightValue, PerClassValue } from '../../types/chains-config';
import { IBlock, IExtrinsic } from '../../types/responses/';
import {
Expand Down Expand Up @@ -265,6 +268,55 @@ describe('BlocksService', () => {
});
});

describe('getPerByte', () => {
it('Correctly handles LengthToFee', () => {
// Reset LRU cache
cache.reset();

/**
* Setup a mockApiClone specific to v9190
*/
const mockHistoricApiClone = {
...mockHistoricApi,
registry: polkadotRegistryV9190,
} as unknown as ApiDecoration<'promise'>;

/**
* Typecast here because the typefactory just needs the registry so this is safe.
* It's a shortcut so that constructing a augmented api is not necessary.
*/
const polkadotV9190TypeFactory = new TypeFactory(
mockHistoricApiClone as ApiPromise
);

/**
* Create a Vec<FrameSupportWeightsWeightToFeeCoefficient> type
* The `coeffInteger` value here should be different from
* `transactionByteFee` set in mockHistoricApi in order to properly
* distinquish values.
*/
const lengthToFeeStruct = polkadotRegistryV9190.createType(
'FrameSupportWeightsWeightToFeeCoefficient',
{ coeffInteger: 12345678 }
);
const lengthToFee = polkadotV9190TypeFactory.vecOf([lengthToFeeStruct]);

/**
* Remove TransactionByteFee from our clone and replace it with LengthToFee
*/
(mockHistoricApiClone.consts.transactionPayment[
'transactionByteFee'
] as unknown) = undefined;
mockHistoricApiClone.consts.transactionPayment['lengthToFee'] =
lengthToFee as unknown as Vec<FrameSupportWeightsWeightToFeeCoefficient> &
AugmentedConst<'promise'>;

const result = blocksService['getPerByte'](mockHistoricApiClone);

expect(sanitizeNumbers(result)).toEqual('12345678');
});
});

describe('BlocksService.getWeight', () => {
it('Should return correct `extrinsicBaseWeight`', () => {
// Reset LRU cache
Expand Down
32 changes: 28 additions & 4 deletions src/services/blocks/BlocksService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiPromise } from '@polkadot/api';
import { ApiDecoration } from '@polkadot/api/types';
import { extractAuthor } from '@polkadot/api-derive/type/util';
import { Compact, GenericCall, Struct, Vec } from '@polkadot/types';
import { Compact, GenericCall, Struct, u128, Vec } from '@polkadot/types';
import {
AccountId32,
Balance,
Expand Down Expand Up @@ -472,6 +472,7 @@ export class BlocksService extends AbstractService {
* Create calcFee from params or return `null` if calcFee cannot be created.
*
* @param api ApiPromise
* @param historicApi ApiDecoration to use for runtime specific querying
* @param parentHash Hash of the parent block
* @param block Block which the extrinsic is from
*/
Expand Down Expand Up @@ -506,8 +507,8 @@ export class BlocksService extends AbstractService {
const multiplier =
await api.query.transactionPayment?.nextFeeMultiplier?.at(parentHash);

const perByte = historicApi.consts.transactionPayment
?.transactionByteFee as Balance;
const perByte = this.getPerByte(historicApi);

const extrinsicBaseWeightExists =
historicApi.consts.system.extrinsicBaseWeight ||
historicApi.consts.system.blockWeights.perClass.normal.baseExtrinsic;
Expand Down Expand Up @@ -552,7 +553,30 @@ export class BlocksService extends AbstractService {
}

/**
* Get a formatted blockweight store value for the runtime corresponding to the given block hash.
* Retrieve the PerByte integer used to calculate fees.
* TransactionByteFee has been replaced with LengthToFee via runtime 9190.
* https://github.com/paritytech/polkadot/pull/5028
*
* @param historicApi ApiDecoration to use for runtime specific querying
*/
private getPerByte(
historicApi: ApiDecoration<'promise'>
): Balance | u128 | null {
const { transactionPayment } = historicApi.consts;

if (transactionPayment?.transactionByteFee) {
return transactionPayment?.transactionByteFee as Balance;
}

if (transactionPayment?.lengthToFee) {
return transactionPayment?.lengthToFee.toArray()[0].coeffInteger;
}

return null;
}

/**
* Get a formatted weight constant for the runtime corresponding to the given block hash.
*
* @param api ApiPromise
* @param blockHash Hash of a block in the runtime to get the extrinsic base weight(s) for
Expand Down
2 changes: 2 additions & 0 deletions src/test-helpers/metadata/polkadotV9190Metadata.ts

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/test-helpers/registries/polkadotRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { polkadotMetadataRpcV16 } from '../metadata/polkadotV16Metadata';
import { polkadotMetadataRpcV29 } from '../metadata/polkadotV29Metadata';
import { polkadotMetadataRpcV9110 } from '../metadata/polkadotV9110Metadata';
import { polkadotMetadataRpcV9122 } from '../metadata/polkadotV9122Metadata';
import { polkadotMetadataRpcV9190 } from '../metadata/polkadotV9190Metadata';

/**
* Create a type registry for Polkadot.
Expand Down Expand Up @@ -65,3 +66,11 @@ export const polkadotRegistryV9122 = createPolkadotRegistry(
9122,
polkadotMetadataRpcV9122
);

/**
* Polkadot v9190 TypeRegistry
*/
export const polkadotRegistryV9190 = createPolkadotRegistry(
9190,
polkadotMetadataRpcV9190
);

0 comments on commit 992b458

Please sign in to comment.