Adjust decoration cache for apiAt queries (#5133)

This commit is contained in:
Jaco
2022-08-05 08:03:32 +03:00
committed by GitHub
parent f1edf476e8
commit 74c7925491
2 changed files with 12 additions and 3 deletions
+2
View File
@@ -40,6 +40,7 @@ interface MetaDecoration {
}
interface FullDecoration<ApiType extends ApiTypes> {
createdAt?: Uint8Array;
decoratedApi: ApiDecoration<ApiType>;
decoratedMeta: DecoratedMeta;
}
@@ -260,6 +261,7 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
decoratedApi.runtimeVersion = registry.runtimeVersion;
return {
createdAt: blockHash,
decoratedApi,
decoratedMeta: registry.decoratedMeta
};
+10 -3
View File
@@ -29,6 +29,8 @@ function textToString (t: Text): string {
}
export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
#atMap: Record<string, ApiDecoration<ApiType>> = {};
#healthTimer: ReturnType<typeof setInterval> | null = null;
#registries: VersionedRegistry<ApiType>[] = [];
@@ -113,11 +115,16 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
*/
public async at (blockHash: Uint8Array | string, knownVersion?: RuntimeVersion): Promise<ApiDecoration<ApiType>> {
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<VersionedRegistry<ApiType>> {