From d632f25a9bef1daf422913f5479809d559e59988 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 2 Oct 2019 21:22:50 +0200 Subject: [PATCH] Cloned API should filter RPCs (#1437) --- packages/api/src/base/Decorate.ts | 6 +++++- packages/api/src/base/Init.ts | 30 ++++++++++++++++++++++++------ packages/api/src/types.ts | 2 ++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index 4cda84554e..6cd304a710 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -146,7 +146,7 @@ export default 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 filterRpcMethods (): Promise { + protected async filterRpc (): Promise { let methods: string[]; try { @@ -157,6 +157,10 @@ export default abstract class Decorate extends Events { methods = []; } + this.filterRpcMethods(methods); + } + + protected filterRpcMethods (methods: string[]): void{ // this is true when the RPC has entries const hasResults = methods.length !== 0; diff --git a/packages/api/src/base/Init.ts b/packages/api/src/base/Init.ts index 792ea4cc55..c5b38e5576 100644 --- a/packages/api/src/base/Init.ts +++ b/packages/api/src/base/Init.ts @@ -4,7 +4,7 @@ import { SignedBlock } from '@polkadot/types/interfaces'; import { RegistryTypes } from '@polkadot/types/types'; -import { ApiInterfaceRx, ApiOptions, ApiTypes, DecorateMethod } from '../types'; +import { ApiBase, ApiInterfaceRx, ApiOptions, ApiTypes, DecorateMethod } from '../types'; import constantsFromMeta from '@polkadot/api-metadata/consts/fromMetadata'; import extrinsicsFromMeta from '@polkadot/api-metadata/extrinsics/fromMetadata'; @@ -89,15 +89,33 @@ export default abstract class Init extends Decorate { if (!this._options.source || !this._options.source._isReady) { this._runtimeMetadata = await this.metaFromChain(metadata); } else { - this._extrinsicType = this._options.source.extrinsicVersion; - this._runtimeMetadata = this._options.source.runtimeMetadata; - this._runtimeVersion = this._options.source.runtimeVersion; - this._genesisHash = this._options.source.genesisHash; + this._runtimeMetadata = await this.metaFromSource(this._options.source); } return this.initFromMeta(this._runtimeMetadata); } + // eslint-disable-next-line @typescript-eslint/require-await + private async metaFromSource (source: ApiBase): Promise { + this._extrinsicType = source.extrinsicVersion; + this._runtimeVersion = source.runtimeVersion; + this._genesisHash = source.genesisHash; + + const methods: string[] = []; + + // manually build a list of all available methods in this RPC, we are + // going to filter on it to align the cloned RPC without making a call + Object.keys(source.rpc).forEach((section): void => { + Object.keys((source.rpc as any)[section]).forEach((method): void => { + methods.push(`${section}_${method}`); + }); + }); + + this.filterRpcMethods(methods); + + return source.runtimeMetadata; + } + private async metaFromChain (optMetadata: Record): Promise { const { typesChain = {}, typesSpec = {} } = this._options; const [genesisHash, runtimeVersion, chain, chainProps] = await Promise.all([ @@ -118,7 +136,7 @@ export default abstract class Init extends Decorate { }); // filter the RPC methods (this does an rpc-methods call) - await this.filterRpcMethods(); + await this.filterRpc(); // retrieve metadata, either from chain or as pass-in via options const metadataKey = `${genesisHash}-${runtimeVersion.specVersion}`; diff --git a/packages/api/src/types.ts b/packages/api/src/types.ts index 9c7f6a6318..a1e753ce1b 100644 --- a/packages/api/src/types.ts +++ b/packages/api/src/types.ts @@ -260,3 +260,5 @@ export interface Signer { */ update?: (id: number, status: Hash | SubmittableResultImpl) => void; } + +export { ApiBase };