diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ed853ff7a..690e495186 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,19 @@ # CHANGELOG +## master + +Changes: + +- Allow for optional known `RuntimeVersion` to short-circuit `api.at` + + ## 6.5.1 Oct 24, 2021 +Upgrade priority: Low. Internal maintenance upgrades. + Contributed: -- Add `Auction` to Polkadot pre-0.9.11 `ProxyType` (Thnaks to https://github.com/wirednkod) +- Add `Auction` to Polkadot pre-0.9.11 `ProxyType` (Thanks to https://github.com/wirednkod) Changes: @@ -13,7 +22,7 @@ Changes: - `TypeDef` structures now contain the `typeName` from metadata v14 on fields - Fix browser bundle imports for xcm-mapping (move to `types-known`) - Expose all extracted type names in `PortableRegistry` -- Adjust `toJSON` (Asicc) output detection on `Raw` +- Adjust `toJSON` (Ascii) output detection on `Raw` - Adjust `api.rpc.*.*.raw(..args)` return to the actual over-the-wire JSON result - Remove `api.rpc.*.*.json(..args)` since it now overlaps with new `.raw` - Bump static metadata for latest Substrate, Polkadot & Kusama diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index d88534d485..f4580be8dd 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -163,7 +163,7 @@ export abstract class Decorate extends Events { this._rx.hasSubscriptions = this._rpcCore.provider.hasSubscriptions; } - public abstract at (blockHash: Uint8Array | string): Promise>; + public abstract at (blockHash: Uint8Array | string, knownVersion?: RuntimeVersion): Promise>; /** * @description Return the current used registry diff --git a/packages/api/src/base/Init.ts b/packages/api/src/base/Init.ts index 04f7ae4e81..d93d9be8b5 100644 --- a/packages/api/src/base/Init.ts +++ b/packages/api/src/base/Init.ts @@ -3,7 +3,7 @@ import type { Observable, Subscription } from 'rxjs'; import type { Text } from '@polkadot/types'; -import type { ChainProperties, Hash, RuntimeVersion } from '@polkadot/types/interfaces'; +import type { ChainProperties, Hash, RuntimeVersion, RuntimeVersionPartial } from '@polkadot/types/interfaces'; import type { Registry } from '@polkadot/types/types'; import type { BN } from '@polkadot/util'; import type { ApiBase, ApiDecoration, ApiOptions, ApiTypes, DecorateMethod } from '../types'; @@ -111,15 +111,34 @@ export abstract class Init extends Decorate { /** * @description Returns a decorated API instance at a specific point in time */ - public async at (blockHash: Uint8Array | string): Promise> { + public async at (blockHash: Uint8Array | string, knownVersion?: RuntimeVersion): Promise> { const u8aHash = u8aToU8a(blockHash); - const registry = await this.getBlockRegistry(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, u8aHash).decoratedApi; } + private _getBlockRegistryViaVersion (blockHash: Uint8Array, version?: RuntimeVersionPartial): VersionedRegistry | null { + if (version) { + // check for pre-existing registries. We also check specName, e.g. it + // could be changed like in Westmint with upgrade from shell -> westmint + const existingViaVersion = this.#registries.find(({ specName, specVersion }) => + specName.eq(version.specName) && + specVersion.eq(version.specVersion) + ); + + if (existingViaVersion) { + existingViaVersion.lastBlockHash = blockHash; + + return existingViaVersion; + } + } + + return null; + } + private async _getBlockRegistry (blockHash: Uint8Array): Promise> { // ensure we have everything required assert(this._genesisHash && this._runtimeVersion, 'Cannot retrieve data on an uninitialized chain'); @@ -142,16 +161,9 @@ export abstract class Init extends Decorate { : await firstValueFrom(this._rpcCore.state.getRuntimeVersion.raw(header.parentHash)) ); - // check for pre-existing registries. We also check specName, e.g. it - // could be changed like in Westmint with upgrade from shell -> westmint - const existingViaVersion = this.#registries.find(({ specName, specVersion }) => - specName.eq(version.specName) && - specVersion.eq(version.specVersion) - ); + const existingViaVersion = this._getBlockRegistryViaVersion(blockHash, version); if (existingViaVersion) { - existingViaVersion.lastBlockHash = blockHash; - return existingViaVersion; } @@ -174,7 +186,7 @@ export abstract class Init extends Decorate { /** * @description Sets up a registry based on the block hash defined */ - public async getBlockRegistry (blockHash: Uint8Array): Promise> { + public async getBlockRegistry (blockHash: Uint8Array, knownVersion?: RuntimeVersion): Promise> { const existingViaHash = this.#registries.find(({ lastBlockHash }) => lastBlockHash && u8aEq(lastBlockHash, blockHash) ); @@ -183,6 +195,12 @@ export abstract class Init extends Decorate { return existingViaHash; } + const existingViaVersion = this._getBlockRegistryViaVersion(blockHash, knownVersion); + + if (existingViaVersion) { + return existingViaVersion; + } + const blockHashHex = u8aToHex(blockHash); let waiting = this.#waitingRegistries[blockHashHex];