Expand nested getTypeDef tests (#3678)

* Expand nested getTypeDef tests

* Rename to fallback

* Remove --detectOpenHandles
This commit is contained in:
Jaco
2021-06-19 10:27:01 +02:00
committed by GitHub
parent 1de533ca9c
commit 03acde19b8
5 changed files with 105 additions and 14 deletions
+4
View File
@@ -4,6 +4,10 @@
Upgrade priority: Medium. Recommended for all chains where Xcm is used (addition of new `XcmOrigin`)
Contributed:
- Added `ChargeAssetTxPayment` extension (Thanks to https://github.com/apopiak)
Changes:
- Allow for type decoding of nested fixed vectors, tuples and structs
+1 -1
View File
@@ -20,7 +20,7 @@
"lint": "polkadot-dev-run-lint",
"clean": "polkadot-dev-clean-build",
"postinstall": "polkadot-dev-yarn-only",
"test": "polkadot-dev-run-test --coverage --detectOpenHandles --forceExit --runInBand --testPathIgnorePatterns e2e",
"test": "polkadot-dev-run-test --coverage --forceExit --runInBand --testPathIgnorePatterns e2e",
"test:one": "polkadot-dev-run-test --detectOpenHandles --forceExit",
"test:watch": "polkadot-dev-run-test --watch"
},
@@ -253,6 +253,30 @@ describe('getTypeDef', (): void => {
});
it('creates a nested fixed vec', (): void => {
expect(
getTypeDef('[[[bool; 3]; 6]; 9]')
).toEqual({
info: TypeDefInfo.VecFixed,
length: 9,
sub: {
info: TypeDefInfo.VecFixed,
length: 6,
sub: {
info: TypeDefInfo.VecFixed,
length: 3,
sub: {
info: TypeDefInfo.Plain,
type: 'bool'
},
type: '[bool;3]'
},
type: '[[bool;3];6]'
},
type: '[[[bool;3];6];9]'
});
});
it('creates a nested fixed vec (named)', (): void => {
expect(
getTypeDef('[[bool; 6]; 3; MyType]')
).toEqual({
@@ -416,6 +440,69 @@ describe('getTypeDef', (): void => {
});
});
it('creates a Vec with nested fixed', (): void => {
expect(
getTypeDef('Vec<[[[bool; 3]; 6]; 9]>')
).toEqual({
info: TypeDefInfo.Vec,
sub: {
info: TypeDefInfo.VecFixed,
length: 9,
sub: {
info: TypeDefInfo.VecFixed,
length: 6,
sub: {
info: TypeDefInfo.VecFixed,
length: 3,
sub: {
info: TypeDefInfo.Plain,
type: 'bool'
},
type: '[bool;3]'
},
type: '[[bool;3];6]'
},
type: '[[[bool;3];6];9]'
},
type: 'Vec<[[[bool;3];6];9]>'
});
});
it('creates a Vec with nested struct', (): void => {
expect(
getTypeDef('Vec<{ "a": "u32", "b": "(u32, bool)" }>')
).toEqual({
info: TypeDefInfo.Vec,
sub: {
info: TypeDefInfo.Struct,
sub: [
{
info: TypeDefInfo.Plain,
name: 'a',
type: 'u32'
},
{
info: TypeDefInfo.Tuple,
name: 'b',
sub: [
{
info: TypeDefInfo.Plain,
type: 'u32'
},
{
info: TypeDefInfo.Plain,
type: 'bool'
}
],
type: '(u32,bool)'
}
],
type: '{"a":"u32","b":"(u32,bool)"}'
},
type: 'Vec<{"a":"u32","b":"(u32,bool)"}>'
});
});
it('creates recursive structures', (): void => {
const registry = new TypeRegistry();
+4 -4
View File
@@ -15,7 +15,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto';
import { Json } from '../codec/Json';
import { Raw } from '../codec/Raw';
import { defaultExtensions, expandExtensionTypes, findUnknownExtensions } from '../extrinsic/signedExtensions';
import { expandExtensionTypes, fallbackExtensions, findUnknownExtensions } from '../extrinsic/signedExtensions';
import { GenericEventData } from '../generic/Event';
import * as baseTypes from '../index.types';
import * as definitions from '../interfaces/definitions';
@@ -132,7 +132,7 @@ export class TypeRegistry implements Registry {
#knownTypes: RegisteredTypes = {};
#signedExtensions: string[] = defaultExtensions;
#signedExtensions: string[] = fallbackExtensions;
#userExtensions?: ExtDef;
@@ -397,7 +397,7 @@ export class TypeRegistry implements Registry {
signedExtensions || (
metadata.asLatest.extrinsic.version.gt(BN_ZERO)
? metadata.asLatest.extrinsic.signedExtensions.map((key) => key.toString())
: defaultExtensions
: fallbackExtensions
),
userExtensions
);
@@ -409,7 +409,7 @@ export class TypeRegistry implements Registry {
}
// sets the available signed extensions
setSignedExtensions (signedExtensions: string[] = defaultExtensions, userExtensions?: ExtDef): void {
setSignedExtensions (signedExtensions: string[] = fallbackExtensions, userExtensions?: ExtDef): void {
this.#signedExtensions = signedExtensions;
this.#userExtensions = userExtensions;
@@ -12,16 +12,18 @@ import { substrate } from './substrate';
// we define the extra fields not as a Tuple, but rather as a struct so they can be named. These will be expanded
// into the various fields when added to the payload (we only support V4 onwards with these, V3 and earlier are
// deemed fixed and non-changeable)
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const allExtensions: ExtDef = {
export const allExtensions: ExtDef = {
...substrate,
...shell,
...polkadot,
...shell,
...statemint
};
// the v4 signed extensions (the order is important here, as applied by default)
const defaultExtensions: Array<keyof typeof allExtensions> = [
// the v4 signed extensions prior to the point of exposing these to the metadata.
// This may not match 100% with the current defaults and are used when not specified
// in the metadata (which is for very old chains). The order is important here, as
// applied by default
export const fallbackExtensions: Array<keyof typeof allExtensions> = [
'CheckVersion',
'CheckGenesis',
'CheckEra',
@@ -31,18 +33,16 @@ const defaultExtensions: Array<keyof typeof allExtensions> = [
'CheckBlockGasLimit'
];
function findUnknownExtensions (extensions: string[], userExtensions: ExtDef = {}): string[] {
export function findUnknownExtensions (extensions: string[], userExtensions: ExtDef = {}): string[] {
const names = [...Object.keys(allExtensions), ...Object.keys(userExtensions)];
return extensions.filter((key) => !names.includes(key));
}
function expandExtensionTypes (extensions: string[], type: keyof ExtInfo, userExtensions: ExtDef = {}): ExtTypes {
export function expandExtensionTypes (extensions: string[], type: keyof ExtInfo, userExtensions: ExtDef = {}): ExtTypes {
return extensions
// Always allow user extensions first - these should provide overrides
.map((key) => userExtensions[key] || allExtensions[key])
.filter((info): info is ExtInfo => !!info)
.reduce((result, info): ExtTypes => ({ ...result, ...info[type] }), {});
}
export { allExtensions, defaultExtensions, expandExtensionTypes, findUnknownExtensions };