diff --git a/copy-to-test.sh b/copy-to-test.sh new file mode 100755 index 0000000000..b8eebcf823 --- /dev/null +++ b/copy-to-test.sh @@ -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" diff --git a/packages/api/src/base/Init.ts b/packages/api/src/base/Init.ts index 260c5a33c6..834d41bd9e 100644 --- a/packages/api/src/base/Init.ts +++ b/packages/api/src/base/Init.ts @@ -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 extends Decorate r.specVersion.eq(version.specVersion)); if (existingViaVersion) { @@ -106,6 +117,7 @@ export default abstract class Init extends Decorate @@ -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]; +} diff --git a/packages/types-known/src/upgrades/index.ts b/packages/types-known/src/upgrades/index.ts new file mode 100644 index 0000000000..b70e55f81e --- /dev/null +++ b/packages/types-known/src/upgrades/index.ts @@ -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; diff --git a/packages/types-known/src/upgrades/kusama.ts b/packages/types-known/src/upgrades/kusama.ts new file mode 100644 index 0000000000..ea762453d2 --- /dev/null +++ b/packages/types-known/src/upgrades/kusama.ts @@ -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; diff --git a/packages/types-known/src/upgrades/polkadot.ts b/packages/types-known/src/upgrades/polkadot.ts new file mode 100644 index 0000000000..89b2c1da6d --- /dev/null +++ b/packages/types-known/src/upgrades/polkadot.ts @@ -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; diff --git a/packages/types/src/types/registry.ts b/packages/types/src/types/registry.ts index 71cdb89d85..703b062fe5 100644 --- a/packages/types/src/types/registry.ts +++ b/packages/types/src/types/registry.ts @@ -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; export interface OverrideBundleDefinition {