diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fb48a2da2..d9ad98397d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## master + +Changes: + +- Adjust `Weight` alias detection (moved from `PortableRegistry` -> `Registry`) + + ## 9.12.1 Jan 22, 2023 Changes: diff --git a/packages/types/src/create/registry.spec.ts b/packages/types/src/create/registry.spec.ts index 97998cabef..5ca208e0a7 100644 --- a/packages/types/src/create/registry.spec.ts +++ b/packages/types/src/create/registry.spec.ts @@ -19,8 +19,9 @@ describe('TypeRegistry', (): void => { }); it('throws on non-existent via getOrThrow', (): void => { - expect((): CodecClass => registry.getOrThrow('non-exist')).toThrow('type non-exist not found'); - expect((): CodecClass => registry.getOrThrow('non-exist', 'foo bar blah')).toThrow('foo bar blah'); + expect( + (): CodecClass => registry.getOrThrow('non-exist') + ).toThrow('type non-exist not found'); }); it('handles non exist type as Unknown (via getOrUnknown)', (): void => { diff --git a/packages/types/src/create/registry.ts b/packages/types/src/create/registry.ts index b7f3f79f73..975046b27b 100644 --- a/packages/types/src/create/registry.ts +++ b/packages/types/src/create/registry.ts @@ -4,7 +4,7 @@ import type { AnyString, Codec, CodecClass, IU8a } from '@polkadot/types-codec/types'; import type { CreateOptions, TypeDef } from '@polkadot/types-create/types'; import type { ExtDef } from '../extrinsic/signedExtensions/types'; -import type { ChainProperties, DispatchErrorModule, DispatchErrorModuleU8, DispatchErrorModuleU8a, EventMetadataLatest, Hash, MetadataLatest, SiField, SiLookupTypeId, SiVariant } from '../interfaces/types'; +import type { ChainProperties, DispatchErrorModule, DispatchErrorModuleU8, DispatchErrorModuleU8a, EventMetadataLatest, Hash, MetadataLatest, SiField, SiLookupTypeId, SiVariant, WeightV2 } from '../interfaces/types'; import type { CallFunction, CodecHasher, Definitions, DetectCodec, RegisteredTypes, Registry, RegistryError, RegistryTypes } from '../types'; import { DoNotConstruct, Json, Raw } from '@polkadot/types-codec'; @@ -421,11 +421,11 @@ export class TypeRegistry implements Registry { return this.#knownTypes?.typesBundle?.spec?.[specName.toString()]?.instances?.[moduleName] || this.#moduleMap[moduleName]; } - public getOrThrow > (name: K, msg?: string): CodecClass { + public getOrThrow > (name: K): CodecClass { const Clazz = this.get(name); if (!Clazz) { - throw new Error(msg || `type ${name} not found`); + throw new Error(`type ${name} not found`); } return Clazz as unknown as CodecClass; @@ -478,11 +478,11 @@ export class TypeRegistry implements Registry { this.#classes.set(arg1, arg2); } else { - this._registerObject(arg1); + this.#registerObject(arg1); } } - private _registerObject (obj: RegistryTypes): void { + #registerObject = (obj: RegistryTypes): void => { const entries = Object.entries(obj); for (let e = 0; e < entries.length; e++) { @@ -508,7 +508,7 @@ export class TypeRegistry implements Registry { this.#definitions.set(name, def); } } - } + }; // sets the chain properties public setChainProperties (properties?: ChainProperties): void { @@ -532,14 +532,38 @@ export class TypeRegistry implements Registry { lookup.register(); } + // register alias types alongside the portable/lookup setup + // (we don't combine this into setLookup since that would/could + // affect stand-along lookups, such as ABIs which don't have + // actual on-chain metadata) + #registerLookup = (lookup: PortableRegistry): void => { + // attach the lookup before we register any types + this.setLookup(lookup); + + // default to V1 - this includes 1.5 (with single field) + let weightType = 'WeightV1'; + const Clazz = this.get('SpWeightsWeightV2Weight'); + + // detection for WeightV2 type + if (Clazz) { + const weight = new Clazz(this); + + if (weight.refTime && weight.proofSize) { + weightType = 'SpWeightsWeightV2Weight'; + } + } + + this.register({ Weight: weightType }); + }; + // sets the metadata public setMetadata (metadata: Metadata, signedExtensions?: string[], userExtensions?: ExtDef): void { this.#metadata = metadata.asLatest; this.#metadataVersion = metadata.version; this.#firstCallIndex = null; - // attach the lookup at this point (before injecting) - this.setLookup(this.#metadata.lookup); + // attach the lookup at this point and register relevant types (before injecting) + this.#registerLookup(this.#metadata.lookup); injectExtrinsics(this, this.#metadata, this.#metadataVersion, this.#metadataCalls, this.#moduleMap); injectErrors(this, this.#metadata, this.#metadataVersion, this.#metadataErrors); diff --git a/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts b/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts index ef59b0468d..62a38e3881 100644 --- a/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts +++ b/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts @@ -422,27 +422,6 @@ function registerTypes (lookup: PortableRegistry, lookups: Record n === 'SpWeightsWeightV2Weight'); - - if (!weight) { - throw new Error('Unable to extract weight type from SpWeightsWeightV2Weight'); - } - - const weightDef = lookup.getTypeDef(`Lookup${weight[0]}`); - - lookup.registry.register({ - Weight: Array.isArray(weightDef.sub) && weightDef.sub.length !== 1 - // we have a complex structure - ? 'SpWeightsWeightV2Weight' - // single entry, fallback to weight V1 - : 'WeightV1' - }); - } } // this extracts aliases based on what we know the runtime config looks like in a