From 637069d723bf9a8afcd42af8b3162a20737ff501 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 30 Aug 2020 20:28:37 +0200 Subject: [PATCH] Shortcut registry for subsequent same-hash queries (#2539) --- packages/api/src/base/Init.ts | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/api/src/base/Init.ts b/packages/api/src/base/Init.ts index fe9dc6fe5e..ba2ac451cf 100644 --- a/packages/api/src/base/Init.ts +++ b/packages/api/src/base/Init.ts @@ -11,13 +11,14 @@ import { map, switchMap } from 'rxjs/operators'; import { Metadata, Text, u32, TypeRegistry } from '@polkadot/types'; import { LATEST_EXTRINSIC_VERSION } from '@polkadot/types/extrinsic/Extrinsic'; import { getMetadataTypes, getSpecAlias, getSpecTypes } from '@polkadot/types-known'; -import { BN_ZERO, assert, logger } from '@polkadot/util'; +import { BN_ZERO, assert, logger, u8aEq, u8aToU8a } from '@polkadot/util'; import { cryptoWaitReady } from '@polkadot/util-crypto'; import Decorate from './Decorate'; interface VersionedRegistry { isDefault: boolean; + lastBlockHash: Uint8Array | null; registry: Registry; specVersion: u32; } @@ -88,6 +89,14 @@ export default abstract class Init extends Decorate r.lastBlockHash && u8aEq(lastBlockHash, r.lastBlockHash)); + + if (existingViaHash) { + return this.setRegistry(existingViaHash.registry); + } + // We have to assume that on the RPC layer the calls used here does not call back into // the registry swap, so getHeader & getRuntimeVersion should not be historic const header = this._genesisHash?.eq(blockHash) @@ -97,10 +106,12 @@ export default abstract class Init extends Decorate specVersion.eq(version.specVersion)); + const existingViaVersion = this.#registries.find((r) => r.specVersion.eq(version.specVersion)); - if (existing) { - return this.setRegistry(existing.registry); + if (existingViaVersion) { + existingViaVersion.lastBlockHash = lastBlockHash; + + return this.setRegistry(existingViaVersion.registry); } const registry = this.setRegistry(new TypeRegistry()); @@ -120,7 +131,7 @@ export default abstract class Init extends Decorate extends Decorate