Skip to content

Commit

Permalink
fix(/blocks): cache extrinsic base weight constants to improve perf…
Browse files Browse the repository at this point in the history
…ormance (#478)

This fixes a regression that happened when v2.1.2 was released. ie: [commit](5ec24e6). It achieves this by cacheing extrinsic weight constants from the metadata that are used in extrinsic fee calculations.
  • Loading branch information
TarikGul committed Mar 29, 2021
1 parent d566e31 commit 610db42
Show file tree
Hide file tree
Showing 29 changed files with 742 additions and 181 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ and then set the enviroment variable to point to your definitions:

```bash
export SAS_SUBSTRATE_TYPES=/path/to/my-chains-types.json
```
```

### Logging

Expand Down
2 changes: 2 additions & 0 deletions src/chains-config/defaultControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ export const defaultControllers: ControllerConfig = {
},
options: {
finalizes: true,
minCalcFeeRuntime: null,
blockWeightStore: {},
},
};
2 changes: 2 additions & 0 deletions src/chains-config/dockMainnetControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ export const dockMainnetControllers: ControllerConfig = {
},
options: {
finalizes: true,
minCalcFeeRuntime: null,
blockWeightStore: {},
},
};
2 changes: 2 additions & 0 deletions src/chains-config/dockTestnetControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ export const dockTestnetControllers: ControllerConfig = {
},
options: {
finalizes: true,
minCalcFeeRuntime: null,
blockWeightStore: {},
},
};
6 changes: 6 additions & 0 deletions src/chains-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import { defaultControllers } from './defaultControllers';
import { dockMainnetControllers } from './dockMainnetControllers';
import { dockTestnetControllers } from './dockTestnetControllers';
import { kulupuControllers } from './kulupuControllers';
import { kusamaControllers } from './kusamaControllers';
import { mandalaControllers } from './mandalaControllers';
import { polkadotControllers } from './polkadotControllers';
import { westendControllers } from './westendControllers';

const specToControllerMap = {
westend: westendControllers,
polkadot: polkadotControllers,
kusama: kusamaControllers,
kulupu: kulupuControllers,
mandala: mandalaControllers,
'dock-testnet': dockTestnetControllers,
Expand Down
2 changes: 2 additions & 0 deletions src/chains-config/kulupuControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ export const kulupuControllers: ControllerConfig = {
},
options: {
finalizes: false,
minCalcFeeRuntime: null,
blockWeightStore: {},
},
};
33 changes: 33 additions & 0 deletions src/chains-config/kusamaControllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ControllerConfig } from '../types/chains-config';
import { getBlockWeight } from './metadata-consts';

/**
* Kusama configuration for Sidecar.
*/
export const kusamaControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsStakingPayouts: true,
AccountsBalanceInfo: true,
AccountsStakingInfo: true,
AccountsVestingInfo: true,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
RuntimeCode: true,
RuntimeSpec: true,
RuntimeMetadata: true,
TransactionDryRun: true,
TransactionMaterial: true,
TransactionFeeEstimate: true,
TransactionSubmit: true,
PalletsStakingProgress: true,
PalletsStorage: true,
},
options: {
finalizes: true,
minCalcFeeRuntime: 1062,
blockWeightStore: getBlockWeight('kusama'),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/mandalaControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ export const mandalaControllers: ControllerConfig = {
},
options: {
finalizes: true,
minCalcFeeRuntime: null,
blockWeightStore: {},
},
};
78 changes: 78 additions & 0 deletions src/chains-config/metadata-consts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
BlockWeightStore,
ExtBaseWeightValue,
MetadataConsts,
PerClassValue,
} from '../../types/chains-config';
import { kusamaDefinitions } from './kusamaConsts';
import { polkadotDefinitions } from './polkadotConsts';
import { westendDefinitions } from './westendConsts';

/**
* Creates an object that maps each runtime to their appropriate weight data.
*
* Each runtime imports their own `chainDefinitions`. `chainDefinitions`
* are arrays that store objects which group a set of runtimes version and associated
* extrinsic weight data.
*
* Example return object:
* {
* ...
* 24: { extrinsicBaseWeight: 125000000 },
* 25: { extrinsicBaseWeight: 125000000 },
* 26: { extrinsicBaseWeight: 125000000 },
* 27: { blockWeights: { perClass: [Object] } },
* 28: { blockWeights: { perClass: [Object] } }
* ...
* }
*
* @param chainDefinitions An array of objects that group data based on runtimes
* and their extrinsicBaseWeight metadata
*/
export function generateBlockWeightStore(
chainDefinitions: MetadataConsts[]
): BlockWeightStore {
const blockWeightStore: BlockWeightStore = {};

for (const def of chainDefinitions) {
const runtimeVersions = def.runtimeVersions;
for (const version of runtimeVersions) {
blockWeightStore[version] = {};

if ((def as ExtBaseWeightValue).extrinsicBaseWeight) {
(blockWeightStore[
version
] as ExtBaseWeightValue).extrinsicBaseWeight = (def as ExtBaseWeightValue).extrinsicBaseWeight;
} else if ((def as PerClassValue).perClass) {
(blockWeightStore[
version
] as PerClassValue).perClass = (def as PerClassValue).perClass;
} else {
throw new Error(
'No Valid weight type found while generating block weight store'
);
}
}
}

return blockWeightStore;
}

/**
* Returns a set of runtimes pointing to their blockWeightDefinitions specific to
* a chain
*
* @param specName specName from the metadata of the current block being fetched
*/
export function getBlockWeight(specName: string): BlockWeightStore {
switch (specName) {
case 'polkadot':
return generateBlockWeightStore(polkadotDefinitions);
case 'kusama':
return generateBlockWeightStore(kusamaDefinitions);
case 'westend':
return generateBlockWeightStore(westendDefinitions);
default:
return {};
}
}
28 changes: 28 additions & 0 deletions src/chains-config/metadata-consts/kusamaConsts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { MetadataConsts } from '../../types/chains-config';
import { extrinsicBaseWeight, perClass } from './substrateConsts';

export const kusamaDefinitions: MetadataConsts[] = [
{
runtimeVersions: [
1062,
2005,
2007,
2008,
2011,
2012,
2013,
2015,
2019,
2022,
2023,
2024,
2025,
2026,
],
extrinsicBaseWeight,
},
{
runtimeVersions: [2027, 2028, 2029],
perClass,
},
];
34 changes: 34 additions & 0 deletions src/chains-config/metadata-consts/polkadotConsts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { MetadataConsts } from '../../types/chains-config';
import { extrinsicBaseWeight, perClass } from './substrateConsts';

export const polkadotDefinitions: MetadataConsts[] = [
{
runtimeVersions: [
0,
1,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
23,
24,
25,
26,
],
extrinsicBaseWeight,
},
{
runtimeVersions: [27, 28, 29],
perClass,
},
];
37 changes: 37 additions & 0 deletions src/chains-config/metadata-consts/substrateConsts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* WEIGHT NOTES (POLKADOT | KUSAMA)
*
* @constant extrinsicBaseWeight
* @constant blockWeights
*
* The following weights are used for Polkadot and Kusama chains. Both are using
* the same fee calculations currently, and share the same Integer values. Because
* of this we are allowing them both to share the same set of variables in order
* to keep things as DRY as possible. That being said, if Kusama or Polkadot ever
* change their definitions for weight calculation those definitions can be added
* directly to `./polkadotConsts`, and `./kusamaConsts` files.
*/

import { IPerClass } from 'src/types/chains-config';

/**
* Polkadot runtime versions before v27
* Kusama runtime versions before v2027
*/
export const extrinsicBaseWeight = BigInt(125000000);

/**
* Polkadot runtime versions after v26
* Kusama runtime versions after v2026
*/
export const perClass: IPerClass = {
normal: {
baseExtrinsic: BigInt(125000000),
},
operational: {
baseExtrinsic: BigInt(1),
},
mandatory: {
baseExtrinsic: BigInt(512000000000001),
},
};
30 changes: 30 additions & 0 deletions src/chains-config/metadata-consts/westendConsts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { MetadataConsts } from '../../types/chains-config';
import { extrinsicBaseWeight, perClass } from './substrateConsts';

export const westendDefinitions: MetadataConsts[] = [
{
runtimeVersions: [
6,
7,
8,
20,
28,
29,
30,
31,
32,
33,
24,
35,
41,
43,
44,
45,
],
extrinsicBaseWeight,
},
{
runtimeVersions: [47, 48, 49],
perClass,
},
];
33 changes: 33 additions & 0 deletions src/chains-config/polkadotControllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ControllerConfig } from '../types/chains-config';
import { getBlockWeight } from './metadata-consts';

/**
* Polkadot configuration for Sidecar.
*/
export const polkadotControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsStakingPayouts: true,
AccountsBalanceInfo: true,
AccountsStakingInfo: true,
AccountsVestingInfo: true,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
RuntimeCode: true,
RuntimeSpec: true,
RuntimeMetadata: true,
TransactionDryRun: true,
TransactionMaterial: true,
TransactionFeeEstimate: true,
TransactionSubmit: true,
PalletsStakingProgress: true,
PalletsStorage: true,
},
options: {
finalizes: true,
minCalcFeeRuntime: 0,
blockWeightStore: getBlockWeight('polkadot'),
},
};
33 changes: 33 additions & 0 deletions src/chains-config/westendControllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ControllerConfig } from '../types/chains-config';
import { getBlockWeight } from './metadata-consts';

/**
* Westend configuration for Sidecar.
*/
export const westendControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsStakingPayouts: true,
AccountsBalanceInfo: true,
AccountsStakingInfo: true,
AccountsVestingInfo: true,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
RuntimeCode: true,
RuntimeSpec: true,
RuntimeMetadata: true,
TransactionDryRun: true,
TransactionMaterial: true,
TransactionFeeEstimate: true,
TransactionSubmit: true,
PalletsStakingProgress: true,
PalletsStorage: true,
},
options: {
finalizes: true,
minCalcFeeRuntime: 6,
blockWeightStore: getBlockWeight('westend'),
},
};
Loading

0 comments on commit 610db42

Please sign in to comment.