From b321a4adbc088a0136779893c410de4e1bc3d645 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 4 Aug 2022 16:27:35 +0200 Subject: [PATCH] Keep count of runtime decorations (#5130) --- CHANGELOG.md | 1 + packages/api/src/base/Decorate.ts | 14 +++++++------- packages/api/src/base/Init.ts | 7 ++++--- packages/api/src/base/types.ts | 1 + 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fae95e7599..c3e227f963 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Contributed: Changes: - Add `noInitWarn: boolean` flag to API options, disabling runtime & RPC warning availability displays +- Only log missing runtime calls on new metadata instances (not for internal instances) ## 9.0.1 Jul 30, 2022 diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index f93e3995c6..1e02cb4172 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -228,7 +228,7 @@ export abstract class Decorate extends Events { } as ApiDecoration; } - protected _createDecorated (registry: VersionedRegistry, fromEmpty: boolean, decoratedApi: ApiDecoration | null, blockHash?: Uint8Array): FullDecoration { + protected _createDecorated (registry: VersionedRegistry, fromEmpty: boolean, withLogging: boolean, decoratedApi: ApiDecoration | null, blockHash?: Uint8Array): FullDecoration { if (!decoratedApi) { decoratedApi = this._emptyDecorated(registry.registry, blockHash); } @@ -237,8 +237,8 @@ export abstract class Decorate extends Events { registry.decoratedMeta = expandMetadata(registry.registry, registry.metadata); } - const runtime = this._decorateCalls(registry, this._decorateMethod, blockHash); - const runtimeRx = this._decorateCalls<'rxjs'>(registry, this._rxDecorateMethod, blockHash); + const runtime = this._decorateCalls(registry, this._decorateMethod, blockHash, withLogging); + const runtimeRx = this._decorateCalls<'rxjs'>(registry, this._rxDecorateMethod, blockHash, false); const storage = this._decorateStorage(registry.decoratedMeta, this._decorateMethod, blockHash); const storageRx = this._decorateStorage<'rxjs'>(registry.decoratedMeta, this._rxDecorateMethod, blockHash); @@ -271,7 +271,7 @@ export abstract class Decorate extends Events { registry.decoratedApi = this._emptyDecorated(registry.registry); } - const { decoratedApi, decoratedMeta } = this._createDecorated(registry, fromEmpty, registry.decoratedApi); + const { decoratedApi, decoratedMeta } = this._createDecorated(registry, fromEmpty, !registry.counter, registry.decoratedApi); this._call = decoratedApi.call; this._consts = decoratedApi.consts; @@ -303,7 +303,7 @@ export abstract class Decorate extends Events { * backwards compatible endpoint for metadata injection, may be removed in the future (However, it is still useful for testing injection) */ public injectMetadata (metadata: Metadata, fromEmpty?: boolean, registry?: Registry): void { - this._injectMetadata({ metadata, registry: registry || this.#registry, runtimeVersion: this.#registry.createType('RuntimeVersionPartial') }, fromEmpty); + this._injectMetadata({ counter: 0, metadata, registry: registry || this.#registry, runtimeVersion: this.#registry.createType('RuntimeVersionPartial') }, fromEmpty); } private _decorateFunctionMeta (input: MetaDecoration, output: MetaDecoration): MetaDecoration { @@ -494,7 +494,7 @@ export abstract class Decorate extends Events { } // pre-metadata decoration - protected _decorateCalls ({ registry, runtimeVersion: { apis, specName } }: VersionedRegistry, decorateMethod: DecorateMethod, blockHash?: Uint8Array | string | null): QueryableCalls { + protected _decorateCalls ({ registry, runtimeVersion: { apis, specName } }: VersionedRegistry, decorateMethod: DecorateMethod, blockHash?: Uint8Array | string | null, withLogging = true): QueryableCalls { const result = {} as QueryableCalls; const named: Record> = {}; const hashes: Record = {}; @@ -540,7 +540,7 @@ export abstract class Decorate extends Events { .filter(([a]) => !hashes[a]) .map(([a, v]) => `${this._runtimeMap[a] || a}/${v}`); - if (!this._options.noInitWarn) { + if (!this._options.noInitWarn && withLogging) { if (older.length) { l.warn(`Not decorating runtime apis without matching versions: ${older.join(', ')}`); } diff --git a/packages/api/src/base/Init.ts b/packages/api/src/base/Init.ts index 5319a68942..470dc927a1 100644 --- a/packages/api/src/base/Init.ts +++ b/packages/api/src/base/Init.ts @@ -117,7 +117,7 @@ export abstract class Init extends Decorate { // always create a new decoration - since we are pointing to a specific hash, this // means that all queries needs to use that hash (not a previous one already existing) - return this._createDecorated(registry, true, null, u8aHash).decoratedApi; + return this._createDecorated(registry, true, !registry.counter, null, u8aHash).decoratedApi; } private async _createBlockRegistry (blockHash: Uint8Array, header: HeaderPartial, version: RuntimeVersionPartial): Promise> { @@ -129,7 +129,7 @@ export abstract class Init extends Decorate { this._initRegistry(registry, this._runtimeChain as Text, version, metadata); // add our new registry - const result = { lastBlockHash: blockHash, metadata, registry, runtimeVersion: version }; + const result = { counter: 0, lastBlockHash: blockHash, metadata, registry, runtimeVersion: version }; this.#registries.push(result); @@ -168,6 +168,7 @@ export abstract class Init extends Decorate { ); if (existingViaVersion) { + existingViaVersion.counter++; existingViaVersion.lastBlockHash = blockHash; return existingViaVersion; @@ -337,7 +338,7 @@ export abstract class Init extends Decorate { // setup the initial registry, when we have none if (!this.#registries.length) { - this.#registries.push({ isDefault: true, metadata, registry: this.registry, runtimeVersion }); + this.#registries.push({ counter: 0, isDefault: true, metadata, registry: this.registry, runtimeVersion }); } // get unique types & validate diff --git a/packages/api/src/base/types.ts b/packages/api/src/base/types.ts index 72214ed7c6..1844b46ed5 100644 --- a/packages/api/src/base/types.ts +++ b/packages/api/src/base/types.ts @@ -8,6 +8,7 @@ import type { Registry } from '@polkadot/types/types'; import type { ApiDecoration, ApiTypes } from '../types'; export interface VersionedRegistry { + counter: number; decoratedApi?: ApiDecoration; decoratedMeta?: DecoratedMeta; isDefault?: boolean;