From 60d4da934ac2fd58472dc1cc1020f405faab8c6d Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 6 Oct 2021 14:12:13 +0300 Subject: [PATCH] Single getMetadata on parallel same-hash queries (#4026) --- packages/api/src/base/Init.ts | 50 ++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/packages/api/src/base/Init.ts b/packages/api/src/base/Init.ts index 67044abe8b..cd23c319f4 100644 --- a/packages/api/src/base/Init.ts +++ b/packages/api/src/base/Init.ts @@ -14,7 +14,7 @@ import { firstValueFrom, map, of, switchMap } from 'rxjs'; import { Metadata, TypeRegistry } from '@polkadot/types'; import { getSpecAlias, getSpecExtensions, getSpecHasher, getSpecRpc, getSpecTypes, getUpgradeVersion } from '@polkadot/types-known'; -import { assert, BN_ZERO, logger, stringify, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util'; +import { assert, BN_ZERO, isUndefined, logger, stringify, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util'; import { cryptoWaitReady } from '@polkadot/util-crypto'; import { detectedCapabilities } from './capabilities'; @@ -31,6 +31,8 @@ export abstract class Init extends Decorate { #updateSub?: Subscription | null = null; + #waitingRegistries: Record>> = {}; + constructor (options: ApiOptions, type: ApiTypes, decorateMethod: DecorateMethod) { super(options, type, decorateMethod); @@ -118,18 +120,7 @@ export abstract class Init extends Decorate { return this._createDecorated(registry, true, u8aHash).decoratedApi; } - /** - * @description Sets up a registry based on the block hash defined - */ - public async getBlockRegistry (blockHash: Uint8Array): Promise> { - const existingViaHash = this.#registries.find(({ lastBlockHash }) => - lastBlockHash && u8aEq(lastBlockHash, blockHash) - ); - - if (existingViaHash) { - return existingViaHash; - } - + private async _getBlockRegistry (blockHash: Uint8Array): Promise> { // ensure we have everything required assert(this._genesisHash && this._runtimeVersion, 'Cannot retrieve data on an uninitialized chain'); @@ -177,12 +168,39 @@ export abstract class Init extends Decorate { this.#registries.push(result); - // TODO This could be useful for historic, disabled due to cross-looping, i.e. .at queries - // this._detectCapabilities(registry, blockHash); - return result; } + /** + * @description Sets up a registry based on the block hash defined + */ + public async getBlockRegistry (blockHash: Uint8Array): Promise> { + const existingViaHash = this.#registries.find(({ lastBlockHash }) => + lastBlockHash && u8aEq(lastBlockHash, blockHash) + ); + + if (existingViaHash) { + return existingViaHash; + } + + const blockHashHex = u8aToHex(blockHash); + let waiting = this.#waitingRegistries[blockHashHex]; + + if (isUndefined(waiting)) { + waiting = this._getBlockRegistry(blockHash); + this.#waitingRegistries[blockHashHex] = waiting; + + // when we have resolved, remove this from the waiting list + waiting + .then((): void => { + delete this.#waitingRegistries[blockHashHex]; + }) + .catch(() => undefined); + } + + return waiting; + } + protected async _loadMeta (): Promise { // on re-connection to the same chain, we don't want to re-do everything from chain again if (this._isReady) {