diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index 1e02cb4172..cb94dc1c06 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -40,6 +40,7 @@ interface MetaDecoration { } interface FullDecoration { + createdAt?: Uint8Array; decoratedApi: ApiDecoration; decoratedMeta: DecoratedMeta; } @@ -260,6 +261,7 @@ export abstract class Decorate extends Events { decoratedApi.runtimeVersion = registry.runtimeVersion; return { + createdAt: blockHash, decoratedApi, decoratedMeta: registry.decoratedMeta }; diff --git a/packages/api/src/base/Init.ts b/packages/api/src/base/Init.ts index 470dc927a1..f175acac83 100644 --- a/packages/api/src/base/Init.ts +++ b/packages/api/src/base/Init.ts @@ -29,6 +29,8 @@ function textToString (t: Text): string { } export abstract class Init extends Decorate { + #atMap: Record> = {}; + #healthTimer: ReturnType | null = null; #registries: VersionedRegistry[] = []; @@ -113,11 +115,16 @@ export abstract class Init extends Decorate { */ public async at (blockHash: Uint8Array | string, knownVersion?: RuntimeVersion): Promise> { const u8aHash = u8aToU8a(blockHash); + const u8aHex = u8aToHex(u8aHash); const registry = await this.getBlockRegistry(u8aHash, knownVersion); - // 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, !registry.counter, null, u8aHash).decoratedApi; + if (!this.#atMap[u8aHex]) { + // 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) + this.#atMap[u8aHex] = this._createDecorated(registry, true, !registry.counter, null, u8aHash).decoratedApi; + } + + return this.#atMap[u8aHex]; } private async _createBlockRegistry (blockHash: Uint8Array, header: HeaderPartial, version: RuntimeVersionPartial): Promise> {