Known upgrade points for on-chain version skip (#2545)

* Known upgrade points for on-chain version skip

* Update packages/types-known/src/index.ts

* Update packages/api/src/base/Init.ts

* Actual known upgrade blocks for Polkadot & Kusama

* Update with known Kusama upgrade history

* linting
This commit is contained in:
Jaco Greeff
2020-08-31 13:09:52 +02:00
committed by GitHub
parent ac2a0a34ca
commit 6e6fa4a97c
7 changed files with 137 additions and 7 deletions
+23
View File
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
function copy_folder () {
SRC="packages/$1/build"
DST="../test/js-test/node_modules/@polkadot/$1"
echo "** Copying $SRC to $DST"
rm -rf $DST
cp -r $SRC $DST
}
yarn polkadot-dev-build-ts
copy_folder "api"
copy_folder "api-contract"
copy_folder "api-derive"
copy_folder "metadata"
copy_folder "rpc-core"
copy_folder "rpc-provider"
copy_folder "types"
copy_folder "types-known"
copy_folder "typegen"
+18 -6
View File
@@ -6,11 +6,12 @@ import { SignedBlock, RuntimeVersion } from '@polkadot/types/interfaces';
import { Registry } from '@polkadot/types/types';
import { ApiBase, ApiOptions, ApiTypes, DecorateMethod } from '../types';
import BN from 'bn.js';
import { Observable, Subscription, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { Metadata, Text, u32, TypeRegistry } from '@polkadot/types';
import { Metadata, Text, TypeRegistry } from '@polkadot/types';
import { LATEST_EXTRINSIC_VERSION } from '@polkadot/types/extrinsic/Extrinsic';
import { getMetadataTypes, getSpecAlias, getSpecTypes } from '@polkadot/types-known';
import { getMetadataTypes, getSpecAlias, getSpecTypes, getUpgradeVersion } from '@polkadot/types-known';
import { BN_ZERO, assert, logger, u8aEq, u8aToU8a } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto';
@@ -20,10 +21,11 @@ interface VersionedRegistry {
isDefault: boolean;
lastBlockHash: Uint8Array | null;
registry: Registry;
specVersion: u32;
specVersion: BN;
}
const KEEPALIVE_INTERVAL = 15000;
const DEFAULT_BLOCKNUMBER = { unwrap: () => BN_ZERO };
const l = logger('api/init');
@@ -89,15 +91,24 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
return existingViaHash.registry;
}
// ensure we have everything required
assert(this._genesisHash && this._runtimeVersion, 'Cannot retrieve data on an uninitialized chain');
// We have to assume that on the RPC layer the calls used here does not call back into
// the registry swap, so getHeader & getRuntimeVersion should not be historic
const header = this._genesisHash?.eq(blockHash)
? { parentHash: this._genesisHash }
const header = this._genesisHash.eq(blockHash)
? { number: DEFAULT_BLOCKNUMBER, parentHash: this._genesisHash }
: await this._rpcCore.chain.getHeader(blockHash).toPromise();
assert(header?.parentHash && !header.parentHash.isEmpty, 'Unable to retrieve header and parent from supplied hash');
const version = await this._rpcCore.state.getRuntimeVersion(header.parentHash).toPromise();
// get the runtime version, either on-chain or via an known upgrade history
const [firstVersion, lastVersion] = getUpgradeVersion(this._genesisHash, header.number.unwrap());
const version = (firstVersion && (lastVersion || firstVersion.specVersion.eq(this._runtimeVersion.specVersion)))
? { specName: this._runtimeVersion.specName, specVersion: firstVersion.specVersion }
: await this._rpcCore.state.getRuntimeVersion(header.parentHash).toPromise();
// check for pre-existing registries
const existingViaVersion = this.#registries.find((r) => r.specVersion.eq(version.specVersion));
if (existingViaVersion) {
@@ -106,6 +117,7 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
return existingViaVersion.registry;
}
// nothing has been found, construct new
const registry = new TypeRegistry();
registry.setChainProperties(this.registry.getChainProperties());
+21 -1
View File
@@ -3,7 +3,8 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import type BN from 'bn.js';
import { DefinitionRpc, DefinitionRpcSub, Registry, RegistryTypes, OverrideModuleType, OverrideVersionedType } from '@polkadot/types/types';
import { Hash } from '@polkadot/types/interfaces';
import { ChainUpgradeVersion, DefinitionRpc, DefinitionRpcSub, Registry, RegistryTypes, OverrideModuleType, OverrideVersionedType } from '@polkadot/types/types';
import { Text } from '@polkadot/types';
import { bnToBn, isUndefined } from '@polkadot/util';
@@ -12,6 +13,7 @@ import typesChain from './chain';
import typesMeta from './metadata';
import typesModules from './modules';
import typesSpec from './spec';
import upgrades from './upgrades';
import warnings from './warnings';
// flatten a VersionedType[] into a Record<string, string>
@@ -91,3 +93,21 @@ export function getSpecAlias ({ knownTypes }: Registry, chainName: Text | string
...(knownTypes.typesBundle?.chain?.[_chainName]?.alias || {})
};
}
/**
* @description Returns a version record for known chains where upgrades are being tracked
*/
export function getUpgradeVersion (genesisHash: Hash, blockNumber: BN): [ChainUpgradeVersion | undefined, ChainUpgradeVersion | undefined] {
const known = upgrades.find((u) => genesisHash.eq(u.genesisHash));
return known
? [
known.versions.reduce((last: ChainUpgradeVersion | undefined, version): ChainUpgradeVersion | undefined => {
return blockNumber.gt(version.blockNumber)
? version
: last;
}, undefined),
known.versions.find((version) => blockNumber.lte(version.blockNumber))
]
: [undefined, undefined];
}
@@ -0,0 +1,16 @@
// Copyright 2017-2020 @polkadot/types-known authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ChainUpgrades } from '@polkadot/types/types';
import kusama from './kusama';
import polkadot from './polkadot';
// Type overrides for specific spec types & versions as given in runtimeVersion
const upgrades: ChainUpgrades[] = [
kusama,
polkadot
];
export default upgrades;
@@ -0,0 +1,26 @@
// Copyright 2017-2020 @polkadot/types-known authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ChainUpgrades } from '@polkadot/types/types';
import BN from 'bn.js';
import { hexToU8a } from '@polkadot/util';
const upgrades: ChainUpgrades = {
genesisHash: hexToU8a('0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe'),
versions: [
[0, 1020], [26669, 1021], [38245, 1022], [54248, 1023], [59659, 1024],
[67651, 1025], [82191, 1027], [83238, 1028], [101503, 1029], [203466, 1030],
[295787, 1031], [461692, 1032], [504329, 1033], [569327, 1038], [587687, 1039],
[653183, 1040], [693488, 1042], [901442, 1045], [1375086, 1050], [1445458, 1051],
[1472960, 1052], [1475648, 1053], [1491596, 1054], [1574408, 1055], [2064961, 1058],
[2201991, 1062], [2671528, 2005], [2704202, 2007], [2728002, 2008], [2832534, 2011],
[2962294, 2012], [3240000, 2013], [3274408, 2015], [3323565, 2019], [3534175, 2022]
].map(([blockNumber, specVersion]) => ({
blockNumber: new BN(blockNumber),
specVersion: new BN(specVersion)
}))
};
export default upgrades;
@@ -0,0 +1,23 @@
// Copyright 2017-2020 @polkadot/types-known authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ChainUpgrades } from '@polkadot/types/types';
import BN from 'bn.js';
import { hexToU8a } from '@polkadot/util';
const upgrades: ChainUpgrades = {
genesisHash: hexToU8a('0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3'),
versions: [
[0, 0], [29231, 1], [188836, 5], [199405, 6], [214264, 7],
[244358, 8], [303079, 9], [314201, 10], [342400, 11], [443963, 12],
[528470, 13], [687751, 14], [746085, 15], [787923, 16], [799302, 17],
[1205128, 18]
].map(([blockNumber, specVersion]) => ({
blockNumber: new BN(blockNumber),
specVersion: new BN(specVersion)
}))
};
export default upgrades;
+10
View File
@@ -18,6 +18,16 @@ export interface OverrideVersionedType {
types: RegistryTypes;
}
export interface ChainUpgradeVersion {
blockNumber: BN;
specVersion: BN;
}
export interface ChainUpgrades {
genesisHash: Uint8Array;
versions: ChainUpgradeVersion[];
}
export type OverrideModuleType = Record<string, string>;
export interface OverrideBundleDefinition {