From 926d1e55d1f75bc35ed775b126802ab0838260e7 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 2 Jan 2021 09:44:08 +0100 Subject: [PATCH] Parallel RPC calls (metadata/methods alongside chain details) (#3011) * Parallel metadata retrieval * Update * Parallel genesisHash * Checks * rpc_methods in mock provider (now required) --- packages/api/src/base/Decorate.ts | 14 +---- packages/api/src/base/Init.ts | 76 +++++++++++-------------- packages/rpc-provider/src/mock/index.ts | 1 + 3 files changed, 36 insertions(+), 55 deletions(-) diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index f9d9d39f34..67fd1879e2 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -4,7 +4,7 @@ import type { DecoratedMeta } from '@polkadot/metadata/decorate/types'; import type { RpcInterface } from '@polkadot/rpc-core/types'; import type { Option, Raw, StorageKey, Text, u64 } from '@polkadot/types'; -import type { Call, Hash, RuntimeVersion } from '@polkadot/types/interfaces'; +import type { Call, Hash, RpcMethods, RuntimeVersion } from '@polkadot/types/interfaces'; import type { StorageEntry } from '@polkadot/types/primitive/types'; import type { AnyFunction, AnyTuple, CallFunction, Codec, CodecArg as Arg, DefinitionRpc, DefinitionRpcSub, IMethod, InterfaceTypes, IStorageKey, Registry, RegistryTypes } from '@polkadot/types/types'; import type { SubmittableExtrinsic } from '../submittable/types'; @@ -224,16 +224,8 @@ export abstract class Decorate extends Events { // manner to cater for both old and new: // - when the number of entries are 0, only remove the ones with isOptional (account & contracts) // - when non-zero, remove anything that is not in the array (we don't do this) - protected async _filterRpc (additional: Record>): Promise { - let methods: string[]; - - try { - // we ignore the version (adjust as versions change, for now only "1") - methods = (await this._rpcCore.rpc.methods().toPromise()).methods.map((t) => t.toString()); - } catch (error) { - // the method is not there, we adjust accordingly - methods = []; - } + protected _filterRpc (rpcMethods: RpcMethods, additional: Record>): void { + const methods = rpcMethods.methods.map((t) => t.toString()); // add any specific user-base RPCs if (Object.keys(additional).length !== 0) { diff --git a/packages/api/src/base/Init.ts b/packages/api/src/base/Init.ts index 0a3a968761..b68f972c6e 100644 --- a/packages/api/src/base/Init.ts +++ b/packages/api/src/base/Init.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { Text } from '@polkadot/types'; -import type { ChainProperties, RuntimeVersion, SignedBlock } from '@polkadot/types/interfaces'; +import type { ChainProperties, Hash, RuntimeVersion, SignedBlock } from '@polkadot/types/interfaces'; import type { Registry } from '@polkadot/types/types'; import type { Observable, Subscription } from '@polkadot/x-rxjs'; import type { ApiBase, ApiOptions, ApiTypes, DecorateMethod } from '../types'; @@ -79,7 +79,7 @@ export abstract class Init extends Decorate { /** * @description Decorates a registry based on the runtime version */ - private _initRegistry (registry: Registry, chain: Text, version: { specName: Text, specVersion: BN }, chainProps?: ChainProperties): Registry { + private _initRegistry (registry: Registry, chain: Text, version: { specName: Text, specVersion: BN }, metadata: Metadata, chainProps?: ChainProperties): Registry { registry.setChainProperties(chainProps || this.registry.getChainProperties()); registry.setKnownTypes(this._options); registry.register(getSpecTypes(registry, chain, version.specName, version.specVersion)); @@ -89,6 +89,8 @@ export abstract class Init extends Decorate { registry.knownTypes.typesAlias = getSpecAlias(registry, chain, version.specName); } + registry.setMetadata(metadata); + return registry; } @@ -131,51 +133,37 @@ export abstract class Init extends Decorate { } // nothing has been found, construct new - const registry = this._initRegistry(new TypeRegistry(), this._runtimeChain as Text, version); const metadata = await this._rpcCore.state.getMetadata(header.parentHash).toPromise(); + const registry = this._initRegistry(new TypeRegistry(), this._runtimeChain as Text, version, metadata); const result = { isDefault: false, lastBlockHash, metadata, metadataConsts: null, registry, specVersion: version.specVersion }; - registry.setMetadata(metadata); this.#registries.push(result); return result; } protected async _loadMeta (): Promise { - const genesisHash = await this._rpcCore.chain.getBlockHash(0).toPromise(); - // on re-connection to the same chain, we don't want to re-do everything from chain again - if (this._isReady && !this._options.source && genesisHash.eq(this._genesisHash)) { + if (this._isReady) { return true; - } - - if (this._genesisHash) { - l.warn('Connection to new genesis detected, re-initializing'); - } - - this._genesisHash = genesisHash; - - if (this.#updateSub) { + } else if (this.#updateSub) { this.#updateSub.unsubscribe(); } - const { metadata = {} } = this._options; - // only load from on-chain if we are not a clone (default path), alternatively // just use the values from the source instance provided - this._runtimeMetadata = this._options.source?._isReady + [this._genesisHash, this._runtimeMetadata] = this._options.source?._isReady ? await this._metaFromSource(this._options.source) - : await this._metaFromChain(metadata); + : await this._metaFromChain(this._options.metadata); return this._initFromMeta(this._runtimeMetadata); } // eslint-disable-next-line @typescript-eslint/require-await - private async _metaFromSource (source: ApiBase): Promise { + private async _metaFromSource (source: ApiBase): Promise<[Hash, Metadata]> { this._extrinsicType = source.extrinsicVersion; this._runtimeChain = source.runtimeChain; this._runtimeVersion = source.runtimeVersion; - this._genesisHash = source.genesisHash; const methods: string[] = []; @@ -189,7 +177,7 @@ export abstract class Init extends Decorate { this._filterRpcMethods(methods); - return source.runtimeMetadata; + return [source.genesisHash, source.runtimeMetadata]; } // subscribe to metadata updates, inject the types on changes @@ -219,11 +207,10 @@ export abstract class Init extends Decorate { // setup the data as per the current versions thisRegistry.metadata = metadata; thisRegistry.metadataConsts = null; - thisRegistry.registry.setMetadata(metadata); thisRegistry.specVersion = version.specVersion; // clear the registry types to ensure that we override correctly - this._initRegistry(thisRegistry.registry.init(), this._runtimeChain as Text, version); + this._initRegistry(thisRegistry.registry.init(), this._runtimeChain as Text, version, metadata); this.injectMetadata(metadata, false, thisRegistry.registry); return true; @@ -233,11 +220,16 @@ export abstract class Init extends Decorate { ).subscribe(); } - private async _metaFromChain (optMetadata: Record): Promise { - const [runtimeVersion, chain, chainProps] = await Promise.all([ + private async _metaFromChain (optMetadata?: Record): Promise<[Hash, Metadata]> { + const [genesisHash, runtimeVersion, chain, chainProps, rpcMethods, chainMetadata] = await Promise.all([ + this._rpcCore.chain.getBlockHash(0).toPromise(), this._rpcCore.state.getRuntimeVersion().toPromise(), this._rpcCore.system.chain().toPromise(), - this._rpcCore.system.properties().toPromise() + this._rpcCore.system.properties().toPromise(), + this._rpcCore.rpc.methods().toPromise(), + optMetadata + ? Promise.resolve(null) + : this._rpcCore.state.getMetadata().toPromise() ]); // set our chain version & genesisHash as returned @@ -245,20 +237,18 @@ export abstract class Init extends Decorate { this._runtimeVersion = runtimeVersion; this._rx.runtimeVersion = runtimeVersion; - // initializes the registry - this._initRegistry(this.registry, chain, runtimeVersion, chainProps); - this._subscribeUpdates(); - - // filter the RPC methods (this does an rpc-methods call) - await this._filterRpc(getSpecRpc(this.registry, chain, runtimeVersion.specName)); - // retrieve metadata, either from chain or as pass-in via options - const metadataKey = `${this._genesisHash?.toHex() || '0x'}-${runtimeVersion.specVersion.toString()}`; - const metadata = metadataKey in optMetadata - ? new Metadata(this.registry, optMetadata[metadataKey]) - : await this._rpcCore.state.getMetadata().toPromise(); + const metadataKey = `${genesisHash.toHex() || '0x'}-${runtimeVersion.specVersion.toString()}`; + const metadata = chainMetadata || ( + optMetadata && optMetadata[metadataKey] + ? new Metadata(this.registry, optMetadata[metadataKey]) + : await this._rpcCore.state.getMetadata().toPromise() + ); - this.registry.setMetadata(metadata); + // initializes the registry & RPC + this._initRegistry(this.registry, chain, runtimeVersion, metadata, chainProps); + this._filterRpc(rpcMethods, getSpecRpc(this.registry, chain, runtimeVersion.specName)); + this._subscribeUpdates(); // setup the initial registry, when we have none if (!this.#registries.length) { @@ -268,7 +258,7 @@ export abstract class Init extends Decorate { // get unique types & validate metadata.getUniqTypes(false); - return metadata; + return [genesisHash, metadata]; } private async _initFromMeta (metadata: Metadata): Promise { @@ -317,9 +307,7 @@ export abstract class Init extends Decorate { } this.#healthTimer = setInterval((): void => { - this._rpcCore.system.health().toPromise().catch((): void => { - // ignore - }); + this._rpcCore.system.health().toPromise().catch(() => null); }, KEEPALIVE_INTERVAL); } catch (_error) { const error = new Error(`FATAL: Unable to initialize the API: ${(_error as Error).message}`); diff --git a/packages/rpc-provider/src/mock/index.ts b/packages/rpc-provider/src/mock/index.ts index 5381e8e05f..9e1441a46f 100644 --- a/packages/rpc-provider/src/mock/index.ts +++ b/packages/rpc-provider/src/mock/index.ts @@ -60,6 +60,7 @@ export class MockProvider implements ProviderInterface { chain_getFinalizedHead: () => this.registry.createType('Header', rpcHeader.result).hash, // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access chain_getHeader: () => this.registry.createType('Header', rpcHeader.result).toJSON(), + rpc_methods: (): string[] => [], state_getKeys: (): string[] => [], state_getKeysPaged: (): string[] => [], state_getMetadata: (): string => rpcMetadata,