This commit is contained in:
Jaco
2022-06-18 15:34:57 +03:00
committed by GitHub
parent 58a18e1da5
commit 771f3bc6fd
19 changed files with 192 additions and 167 deletions
+1 -1
View File
@@ -29,7 +29,7 @@
"@polkadot/types": "8.8.3-12",
"@polkadot/types-augment": "8.8.3-12",
"@polkadot/types-codec": "8.8.3-12",
"@polkadot/util": "^9.4.2-22"
"@polkadot/util": "^9.5.1"
},
"devDependencies": {
"@polkadot/types-support": "8.8.3-12"
+1 -1
View File
@@ -26,7 +26,7 @@
"@babel/runtime": "^7.18.3",
"@polkadot/rpc-core": "8.8.3-12",
"@polkadot/types": "8.8.3-12",
"@polkadot/util": "^9.4.2-22",
"@polkadot/util": "^9.5.1",
"rxjs": "^7.5.5"
}
}
+3 -3
View File
@@ -28,12 +28,12 @@
"@polkadot/types": "8.8.3-12",
"@polkadot/types-codec": "8.8.3-12",
"@polkadot/types-create": "8.8.3-12",
"@polkadot/util": "^9.4.2-22",
"@polkadot/util-crypto": "^9.4.2-22",
"@polkadot/util": "^9.5.1",
"@polkadot/util-crypto": "^9.5.1",
"rxjs": "^7.5.5"
},
"devDependencies": {
"@polkadot/api-augment": "8.8.3-12",
"@polkadot/keyring": "^9.4.2-22"
"@polkadot/keyring": "^9.5.1"
}
}
+2 -2
View File
@@ -30,8 +30,8 @@
"@polkadot/rpc-core": "8.8.3-12",
"@polkadot/types": "8.8.3-12",
"@polkadot/types-codec": "8.8.3-12",
"@polkadot/util": "^9.4.2-22",
"@polkadot/util-crypto": "^9.4.2-22",
"@polkadot/util": "^9.5.1",
"@polkadot/util-crypto": "^9.5.1",
"rxjs": "^7.5.5"
},
"devDependencies": {
+3 -3
View File
@@ -27,7 +27,7 @@
"@polkadot/api-augment": "8.8.3-12",
"@polkadot/api-base": "8.8.3-12",
"@polkadot/api-derive": "8.8.3-12",
"@polkadot/keyring": "^9.4.2-22",
"@polkadot/keyring": "^9.5.1",
"@polkadot/rpc-augment": "8.8.3-12",
"@polkadot/rpc-core": "8.8.3-12",
"@polkadot/rpc-provider": "8.8.3-12",
@@ -36,8 +36,8 @@
"@polkadot/types-codec": "8.8.3-12",
"@polkadot/types-create": "8.8.3-12",
"@polkadot/types-known": "8.8.3-12",
"@polkadot/util": "^9.4.2-22",
"@polkadot/util-crypto": "^9.4.2-22",
"@polkadot/util": "^9.5.1",
"@polkadot/util-crypto": "^9.5.1",
"eventemitter3": "^4.0.7",
"rxjs": "^7.5.5"
},
+1 -1
View File
@@ -27,6 +27,6 @@
"@polkadot/rpc-core": "8.8.3-12",
"@polkadot/types": "8.8.3-12",
"@polkadot/types-codec": "8.8.3-12",
"@polkadot/util": "^9.4.2-22"
"@polkadot/util": "^9.5.1"
}
}
+2 -2
View File
@@ -27,11 +27,11 @@
"@polkadot/rpc-augment": "8.8.3-12",
"@polkadot/rpc-provider": "8.8.3-12",
"@polkadot/types": "8.8.3-12",
"@polkadot/util": "^9.4.2-22",
"@polkadot/util": "^9.5.1",
"rxjs": "^7.5.5"
},
"devDependencies": {
"@polkadot/keyring": "^9.4.2-22",
"@polkadot/keyring": "^9.5.1",
"@polkadot/rpc-augment": "8.8.3-12"
}
}
+6 -6
View File
@@ -24,14 +24,14 @@
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.18.3",
"@polkadot/keyring": "^9.4.2-22",
"@polkadot/keyring": "^9.5.1",
"@polkadot/types": "8.8.3-12",
"@polkadot/types-support": "8.8.3-12",
"@polkadot/util": "^9.4.2-22",
"@polkadot/util-crypto": "^9.4.2-22",
"@polkadot/x-fetch": "^9.4.2-22",
"@polkadot/x-global": "^9.4.2-22",
"@polkadot/x-ws": "^9.4.2-22",
"@polkadot/util": "^9.5.1",
"@polkadot/util-crypto": "^9.5.1",
"@polkadot/x-fetch": "^9.5.1",
"@polkadot/x-global": "^9.5.1",
"@polkadot/x-ws": "^9.5.1",
"@substrate/connect": "0.7.6",
"eventemitter3": "^4.0.7",
"mock-socket": "^9.1.5",
+2 -2
View File
@@ -42,8 +42,8 @@
"@polkadot/types-codec": "8.8.3-12",
"@polkadot/types-create": "8.8.3-12",
"@polkadot/types-support": "8.8.3-12",
"@polkadot/util": "^9.4.2-22",
"@polkadot/x-ws": "^9.4.2-22",
"@polkadot/util": "^9.5.1",
"@polkadot/x-ws": "^9.5.1",
"handlebars": "^4.7.7",
"websocket": "^1.0.34",
"yargs": "^17.5.1"
+1 -1
View File
@@ -26,6 +26,6 @@
"@babel/runtime": "^7.18.3",
"@polkadot/types": "8.8.3-12",
"@polkadot/types-codec": "8.8.3-12",
"@polkadot/util": "^9.4.2-22"
"@polkadot/util": "^9.5.1"
}
}
+2 -2
View File
@@ -24,12 +24,12 @@
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.18.3",
"@polkadot/util": "^9.4.2-22"
"@polkadot/util": "^9.5.1"
},
"devDependencies": {
"@polkadot/types": "8.8.3-12",
"@polkadot/types-augment": "8.8.3-12",
"@polkadot/types-support": "8.8.3-12",
"@polkadot/util-crypto": "^9.4.2-22"
"@polkadot/util-crypto": "^9.5.1"
}
}
@@ -7,6 +7,10 @@ import { TypeRegistry } from '@polkadot/types';
import { Enum, Null, Text, U32 } from '@polkadot/types-codec';
import { u8aToHex } from '@polkadot/util';
import { performance } from '../test/performance';
const PEnum = Enum.with({ a: U32, b: U32 });
describe('Enum', (): void => {
const registry = new TypeRegistry();
@@ -449,4 +453,6 @@ describe('Enum', (): void => {
);
});
});
performance('Enum', 20_000, [[new Uint8Array([0, 31, 32, 33, 34])]], (v: Uint8Array) => new PEnum(registry, v));
});
+28 -9
View File
@@ -4,7 +4,7 @@
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, Codec, CodecClass, IEnum, Inspect, IU8a, Registry } from '../types';
import { isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
import { isHex, isNumber, isObject, isString, isU8a, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
import { mapToTypeMap, typesToMap } from '../utils';
import { Null } from './Null';
@@ -96,18 +96,37 @@ function extractDef (registry: Registry, _def: Record<string, string | CodecClas
};
}
function createFromValue (registry: Registry, def: TypesDef, index = 0, value?: unknown): Decoded {
const entry = Object.values(def).find((e) => e.index === index);
function getEntryType (def: TypesDef, checkIdx: number): CodecClass {
const values = Object.values(def);
if (isUndefined(entry)) {
throw new Error(`Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
for (let i = 0; i < values.length; i++) {
const { Type, index } = values[i];
if (index === checkIdx) {
return Type;
}
}
throw new Error(`Unable to create Enum via index ${checkIdx}, in ${Object.keys(def).join(', ')}`);
}
function createFromU8a (registry: Registry, def: TypesDef, index: number, value: Uint8Array): Decoded {
const Type = getEntryType(def, index);
return {
index,
value: value instanceof entry.Type
value: new Type(registry, value)
};
}
function createFromValue (registry: Registry, def: TypesDef, index = 0, value?: unknown): Decoded {
const Type = getEntryType(def, index);
return {
index,
value: value instanceof Type
? value
: new entry.Type(registry, value)
: new Type(registry, value)
};
}
@@ -139,7 +158,7 @@ function decodeEnum (registry: Registry, def: TypesDef, value?: unknown, index?:
// nested, we don't want to match isObject below
if (u8a.length) {
return createFromValue(registry, def, u8a[0], u8a.subarray(1));
return createFromU8a(registry, def, u8a[0], u8a.subarray(1));
}
} else if (value instanceof Enum) {
return createFromValue(registry, def, value.index, value.value);
@@ -187,7 +206,7 @@ export class Enum implements IEnum {
// shortcut isU8a as used in SCALE decoding
const decoded = isU8a(value) && value.length && !isNumber(index)
? createFromValue(registry, def, value[0], value.subarray(1))
? createFromU8a(registry, def, value[0], value.subarray(1))
: decodeEnum(registry, def, value, index);
this.registry = registry;
+1 -1
View File
@@ -25,7 +25,7 @@
"dependencies": {
"@babel/runtime": "^7.18.3",
"@polkadot/types-codec": "8.8.3-12",
"@polkadot/util": "^9.4.2-22"
"@polkadot/util": "^9.5.1"
},
"devDependencies": {
"@polkadot/types": "8.8.3-12"
+2 -2
View File
@@ -24,10 +24,10 @@
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.18.3",
"@polkadot/networks": "^9.4.2-22",
"@polkadot/networks": "^9.5.1",
"@polkadot/types": "8.8.3-12",
"@polkadot/types-codec": "8.8.3-12",
"@polkadot/types-create": "8.8.3-12",
"@polkadot/util": "^9.4.2-22"
"@polkadot/util": "^9.5.1"
}
}
+1 -1
View File
@@ -24,6 +24,6 @@
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.18.3",
"@polkadot/util": "^9.4.2-22"
"@polkadot/util": "^9.5.1"
}
}
+4 -4
View File
@@ -24,16 +24,16 @@
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.18.3",
"@polkadot/keyring": "^9.4.2-22",
"@polkadot/keyring": "^9.5.1",
"@polkadot/types-augment": "8.8.3-12",
"@polkadot/types-codec": "8.8.3-12",
"@polkadot/types-create": "8.8.3-12",
"@polkadot/util": "^9.4.2-22",
"@polkadot/util-crypto": "^9.4.2-22",
"@polkadot/util": "^9.5.1",
"@polkadot/util-crypto": "^9.5.1",
"rxjs": "^7.5.5"
},
"devDependencies": {
"@polkadot/keyring": "^9.4.2-22",
"@polkadot/keyring": "^9.5.1",
"@polkadot/types-support": "8.8.3-12"
}
}