diff --git a/packages/api/src/base/Init.ts b/packages/api/src/base/Init.ts index 834d41bd9e..d541f0f663 100644 --- a/packages/api/src/base/Init.ts +++ b/packages/api/src/base/Init.ts @@ -20,6 +20,7 @@ import Decorate from './Decorate'; interface VersionedRegistry { isDefault: boolean; lastBlockHash: Uint8Array | null; + metadata: Metadata; registry: Registry; specVersion: BN; } @@ -61,7 +62,7 @@ export default abstract class Init extends Decorate this.getBlockRegistry(hash)); + this._rpcCore.setRegistrySwap((hash: string | Uint8Array) => this.getBlockRegistry(hash)); this._rpcCore.provider.on('disconnected', this.#onProviderDisconnect); this._rpcCore.provider.on('error', this.#onProviderError); this._rpcCore.provider.on('connected', this.#onProviderConnect); @@ -78,17 +79,13 @@ export default abstract class Init extends Decorate { - if (!blockHash) { - return this.registry; - } - + public async getBlockRegistry (blockHash: string | Uint8Array): Promise { // shortcut in the case where we have an immediate-same request const lastBlockHash = u8aToU8a(blockHash); const existingViaHash = this.#registries.find((r) => r.lastBlockHash && u8aEq(lastBlockHash, r.lastBlockHash)); if (existingViaHash) { - return existingViaHash.registry; + return existingViaHash; } // ensure we have everything required @@ -114,7 +111,7 @@ export default abstract class Init extends Decorate extends Decorate { @@ -215,6 +212,7 @@ export default abstract class Init extends Decorate extends Decorate extends Decorate Promise; + #getBlockRegistry?: (blockHash: string | Uint8Array) => Promise<{ registry: Registry }>; readonly #storageCache = new Map(); @@ -136,7 +136,7 @@ export default class Rpc implements RpcInterface { /** * @description Sets a registry swap (typically from Api) */ - public setRegistrySwap (registrySwap: (blockHash?: string | Uint8Array | null) => Promise): void { + public setRegistrySwap (registrySwap: (blockHash: string | Uint8Array) => Promise<{ registry: Registry }>): void { this.#getBlockRegistry = registrySwap; } @@ -210,9 +210,9 @@ export default class Rpc implements RpcInterface { const hash = hashIndex === -1 ? undefined : values[hashIndex] as Uint8Array; - const registry = hash && this.#getBlockRegistry + const { registry } = hash && this.#getBlockRegistry ? await this.#getBlockRegistry(hash) - : this.#registryDefault; + : { registry: this.#registryDefault }; const params = this._formatInputs(registry, def, values); const data = await this.provider.send(rpcName, params.map((param): AnyJson => param.toJSON())) as AnyJson;