Refresh all type decorations on upgrade (#2615)

* Refresh all type decorations on upgrade

* Cleanups

* calls/events not part of init
This commit is contained in:
Jaco Greeff
2020-09-16 11:32:52 +02:00
committed by GitHub
parent 5f0c6ecd1b
commit 2edad85250
4 changed files with 54 additions and 58 deletions
+1
View File
@@ -6,6 +6,7 @@ Upgrade priority: High. Metadata v12 is the next major version containing struct
Changes:
- Ensure that upgrades override old registry types (non-specified in current)
- Support for Metadata v12 with fixed indices
- Cleanups for WebSocket class detection and creation
- Ensure that ignored map params yield an error on `iterKey`
+25 -48
View File
@@ -2,11 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SignedBlock, RuntimeVersion } from '@polkadot/types/interfaces';
import { ChainProperties, SignedBlock, RuntimeVersion } from '@polkadot/types/interfaces';
import { Registry } from '@polkadot/types/types';
import { ApiBase, ApiOptions, ApiTypes, DecorateMethod } from '../types';
import { VersionedRegistry } from './types';
import BN from 'bn.js';
import { Observable, Subscription, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { Metadata, Text, TypeRegistry } from '@polkadot/types';
@@ -68,6 +69,22 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
}
}
/**
* @description Decorates a registry based on the runtime version
*/
private _initRegistry (registry: Registry, chain: Text, version: { specName: Text, specVersion: BN }, chainProps?: ChainProperties): Registry {
registry.setChainProperties(chainProps || this.registry.getChainProperties());
registry.setKnownTypes(this._options);
registry.register(getSpecTypes(registry, chain, version.specName, version.specVersion));
// for bundled types, pull through the aliasses defined
if (registry.knownTypes.typesBundle) {
registry.knownTypes.typesAlias = getSpecAlias(registry, chain, version.specName);
}
return registry;
}
/**
* @description Sets up a registry based on the block hash defined
*/
@@ -107,17 +124,7 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
}
// nothing has been found, construct new
const registry = new TypeRegistry();
registry.setChainProperties(this.registry.getChainProperties());
registry.setKnownTypes(this._options);
registry.register(getSpecTypes(registry, this._runtimeChain as Text, version.specName, version.specVersion));
if (registry.knownTypes.typesBundle) {
this._adjustBundleTypes(registry, this._runtimeChain as Text, version.specName);
}
// retrieve the metadata now that we have all types set
const registry = this._initRegistry(new TypeRegistry(), this._runtimeChain as Text, version);
const metadata = await this._rpcCore.state.getMetadata(header.parentHash).toPromise();
const result = { isDefault: false, lastBlockHash, metadata, metadataConsts: null, registry, specVersion: version.specVersion };
@@ -204,10 +211,13 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
assert(thisRegistry, 'Initialization error, cannot find the default registry');
// setup the data as per the current versions
thisRegistry.metadata = metadata;
thisRegistry.metadataConsts = null;
thisRegistry.specVersion = version.specVersion;
thisRegistry.registry.register(getSpecTypes(thisRegistry.registry, this._runtimeChain as Text, version.specName, version.specVersion));
// clear the registry types to ensure that we override correctly
this._initRegistry(thisRegistry.registry.init(), this._runtimeChain as Text, version);
this.injectMetadata(metadata, false, thisRegistry.registry);
return true;
@@ -217,33 +227,6 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
).subscribe();
}
private _adjustBundleTypes (registry: Registry, chain: Text, specName: Text): void {
// adjust known type aliases
registry.knownTypes.typesAlias = getSpecAlias(registry, chain, specName);
// FIXME For the first round, we are not adjusting the user-injected RPCs
// inject any user-level RPCs now that we have the chain/spec
// this._rpcCore.addUserInterfaces(getSpecRpc(this.registry, chain, specName));
// const extraRpc = this._decorateRpc(this._rpcCore, this._decorateMethod);
// // FIXME this is a mess
// Object.entries(extraRpc).forEach(([section, value]): void => {
// if (this._rpc) {
// if (!this._rpc[section as 'author']) {
// this._rpc[section as 'author'] = {} as DecoratedRpcSection<ApiType, RpcInterface['author']>;
// }
// Object.entries(value).forEach(([name, method]): void => {
// if (this._rpc && !this._rpc[section as 'author'][name as 'hasKey']) {
// // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
// this._rpc[section as 'author'][name as 'hasKey'] = method;
// }
// });
// }
// });
}
private async _metaFromChain (optMetadata: Record<string, string>): Promise<Metadata> {
const [runtimeVersion, chain, chainProps] = await Promise.all([
this._rpcCore.state.getRuntimeVersion().toPromise(),
@@ -256,14 +239,8 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
this._runtimeVersion = runtimeVersion;
this._rx.runtimeVersion = runtimeVersion;
// adjust types based on bundled info
if (this.registry.knownTypes.typesBundle) {
this._adjustBundleTypes(this.registry, chain, runtimeVersion.specName);
}
// do the setup for the specific chain
this.registry.setChainProperties(chainProps);
this.registerTypes(getSpecTypes(this.registry, chain, runtimeVersion.specName, runtimeVersion.specVersion));
// initializes the registry
this._initRegistry(this.registry, chain, runtimeVersion, chainProps);
this._subscribeUpdates();
// filter the RPC methods (this does an rpc-methods call)
+27 -10
View File
@@ -93,9 +93,9 @@ function decorateExtrinsics (registry: Registry, metadata: RegistryMetadata, met
}
export class TypeRegistry implements Registry {
readonly #classes = new Map<string, Constructor>();
#classes = new Map<string, Constructor>();
readonly #definitions = new Map<string, string>();
#definitions = new Map<string, string>();
readonly #metadataCalls: Record<string, CallFunction> = {};
@@ -103,32 +103,49 @@ export class TypeRegistry implements Registry {
readonly #metadataEvents: Record<string, Constructor<EventData>> = {};
readonly #unknownTypes = new Map<string, boolean>();
#unknownTypes = new Map<string, boolean>();
#chainProperties?: ChainProperties;
#hasher: (data: Uint8Array) => Uint8Array = blake2AsU8a;
readonly #knownDefaults: RegistryTypes;
readonly #knownDefinitions: Record<string, { types: RegistryTypes }>;
#knownTypes: RegisteredTypes = {};
#signedExtensions: string[] = defaultExtensions;
constructor () {
// we only want to import these on creation, i.e. we want to avoid types
// weird side-effects from circular references. (Since registry is injected
// into types, this can be a real concern now)
// we only want to import these on creation, i.e. we want to avoid weird
// side-effects from circular references. (Since registry is injected
// into types, this can be a real concern now)
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const baseTypes: RegistryTypes = require('../index.types');
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const definitions: Record<string, { types: RegistryTypes }> = require('../interfaces/definitions');
// since these are classes, they are injected first
this.register({ Raw, ...baseTypes });
this.#knownDefaults = { Raw, ...baseTypes };
this.#knownDefinitions = definitions;
// since these are definitions, they would only get created when needed
Object.values(definitions).forEach(({ types }): void =>
this.init();
}
public init (): this {
// start clean
this.#classes = new Map<string, Constructor>();
this.#definitions = new Map<string, string>();
this.#unknownTypes = new Map<string, boolean>();
this.#knownTypes = {};
// register know, first classes then on-demand-created definitions
this.register(this.#knownDefaults);
Object.values(this.#knownDefinitions).forEach(({ types }): void =>
this.register(types)
);
return this;
}
public get chainDecimals (): number {
+1
View File
@@ -163,6 +163,7 @@ export interface Registry {
hasDef (name: string): boolean;
hasType (name: string): boolean;
hash (data: Uint8Array): Uint8Array;
init (): Registry;
register (type: Constructor | RegistryTypes): void;
register (name: string, type: Constructor): void;
register (arg1: string | Constructor | RegistryTypes, arg2?: Constructor): void;