Some minor cleanups (#1966)

* Some minor cleanups

* Small additions

* Cleanup ExtrinsicEra decoding

* Minor adjustments

* Remove support for metadata v0 parsing

* Adjust refCountDelay

* refCountDelay comments

* Adjust Metadata tests

* Metadata default creation

* Funky lines

* Cleanup as AccountIdAndIndex

* Some derive returns

* Try and cleanup info extraction
This commit is contained in:
Jaco Greeff
2020-02-27 15:01:20 +01:00
committed by GitHub
parent efd6c5331d
commit 4e756704ea
28 changed files with 194 additions and 247 deletions
+10 -6
View File
@@ -17,18 +17,22 @@ function notEntry (value: any): boolean {
return !Array.isArray(value) || value.length !== 2;
}
function compareMapArray (a: Map<any, any>, b: [any, any][]): boolean {
// equal number of entries and each entry in the array should match
return (a.size === b.length) && !b.some((entry): boolean =>
notEntry(entry) || hasMismatch(a.get(entry[0]), entry[1])
);
}
// NOTE These are used internally and when comparing objects, expects that
// when the second is an Map<string, Codec> that the first has to be as well
export default function compareMap (a: Map<any, any>, b?: any): boolean {
if (Array.isArray(b)) {
// equal number of entries and each entry in the array should match
return (a.size === b.length) && !b.some((entry): boolean =>
notEntry(entry) || hasMismatch(a.get(entry[0]), entry[1])
);
return compareMapArray(a, b);
} else if (b instanceof Map) {
return compareMap(a, [...b.entries()]);
return compareMapArray(a, [...b.entries()]);
} else if (isObject(b)) {
return compareMap(a, Object.entries(b));
return compareMapArray(a, Object.entries(b));
}
return false;
+8 -4
View File
@@ -4,16 +4,20 @@
import { isObject } from '@polkadot/util';
function compareSetArray (a: Set<any>, b: any[]): boolean {
// equal number of entries and each entry in the array should match
return (a.size === b.length) && !b.some((entry): boolean => !a.has(entry));
}
// NOTE These are used internally and when comparing objects, expects that
// when the second is an Set<string, Codec> that the first has to be as well
export default function compareSet (a: Set<any>, b?: any): boolean {
if (Array.isArray(b)) {
// equal number of entries and each entry in the array should match
return (a.size === b.length) && !b.some((entry): boolean => !a.has(entry));
return compareSetArray(a, b);
} else if (b instanceof Set) {
return compareSet(a, [...b.entries()]);
return compareSetArray(a, [...b.entries()]);
} else if (isObject(b)) {
return compareSet(a, Object.entries(b));
return compareSetArray(a, Object.entries(b));
}
return false;
+61 -59
View File
@@ -38,64 +38,7 @@ const VERSIONS: (keyof InterfaceTypes)[] = [
export { TRANSACTION_VERSION as LATEST_EXTRINSIC_VERSION } from './v4/Extrinsic';
/**
* @name GenericExtrinsic
* @description
* Representation of an Extrinsic in the system. It contains the actual call,
* (optional) signature and encodes with an actual length prefix
*
* {@link https://github.com/paritytech/wiki/blob/master/Extrinsic.md#the-extrinsic-format-for-node}.
*
* Can be:
* - signed, to create a transaction
* - left as is, to create an inherent
*/
export default class Extrinsic extends Base<ExtrinsicVx | ExtrinsicUnknown> implements IExtrinsic {
constructor (registry: Registry, value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, { version }: CreateOptions = {}) {
super(registry, Extrinsic.decodeExtrinsic(registry, value, version));
}
/** @internal */
private static newFromValue (registry: Registry, value: any, version: number): ExtrinsicVx | ExtrinsicUnknown {
if (value instanceof Extrinsic) {
return value.raw;
}
const isSigned = (version & BIT_SIGNED) === BIT_SIGNED;
const type = VERSIONS[version & UNMASK_VERSION] || VERSIONS[0];
// we cast here since the VERSION definition is incredibly broad - we don't have a
// slice for "only add extrinsic types", and more string definitions become unwieldy
return registry.createType(type, value, { isSigned, version }) as ExtrinsicVx;
}
/** @internal */
public static decodeExtrinsic (registry: Registry, value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, version: number = DEFAULT_VERSION): ExtrinsicVx | ExtrinsicUnknown {
if (isU8a(value) || Array.isArray(value) || isHex(value)) {
return Extrinsic.decodeU8a(registry, u8aToU8a(value), version);
} else if (value instanceof registry.createClass('Call')) {
return Extrinsic.newFromValue(registry, { method: value }, version);
}
return Extrinsic.newFromValue(registry, value, version);
}
/** @internal */
private static decodeU8a (registry: Registry, value: Uint8Array, version: number): ExtrinsicVx | ExtrinsicUnknown {
if (!value.length) {
return Extrinsic.newFromValue(registry, new Uint8Array(), version);
}
const [offset, length] = Compact.decodeU8a(value);
const total = offset + length.toNumber();
assert(total <= value.length, `Extrinsic: length less than remainder, expected at least ${total}, found ${value.length}`);
const data = value.subarray(offset, total);
return Extrinsic.newFromValue(registry, data.subarray(1), data[0]);
}
abstract class ExtrinsicBase extends Base<ExtrinsicVx | ExtrinsicUnknown> {
/**
* @description The arguments passed to for the call, exposes args so it is compatible with [[Call]]
*/
@@ -214,6 +157,65 @@ export default class Extrinsic extends Base<ExtrinsicVx | ExtrinsicUnknown> impl
public get version (): number {
return this.type | (this.isSigned ? BIT_SIGNED : BIT_UNSIGNED);
}
}
/**
* @name GenericExtrinsic
* @description
* Representation of an Extrinsic in the system. It contains the actual call,
* (optional) signature and encodes with an actual length prefix
*
* {@link https://github.com/paritytech/wiki/blob/master/Extrinsic.md#the-extrinsic-format-for-node}.
*
* Can be:
* - signed, to create a transaction
* - left as is, to create an inherent
*/
export default class Extrinsic extends ExtrinsicBase implements IExtrinsic {
constructor (registry: Registry, value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, { version }: CreateOptions = {}) {
super(registry, Extrinsic.decodeExtrinsic(registry, value, version));
}
/** @internal */
private static newFromValue (registry: Registry, value: any, version: number): ExtrinsicVx | ExtrinsicUnknown {
if (value instanceof Extrinsic) {
return value.raw;
}
const isSigned = (version & BIT_SIGNED) === BIT_SIGNED;
const type = VERSIONS[version & UNMASK_VERSION] || VERSIONS[0];
// we cast here since the VERSION definition is incredibly broad - we don't have a
// slice for "only add extrinsic types", and more string definitions become unwieldy
return registry.createType(type, value, { isSigned, version }) as ExtrinsicVx;
}
/** @internal */
public static decodeExtrinsic (registry: Registry, value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, version: number = DEFAULT_VERSION): ExtrinsicVx | ExtrinsicUnknown {
if (isU8a(value) || Array.isArray(value) || isHex(value)) {
return Extrinsic.decodeU8a(registry, u8aToU8a(value), version);
} else if (value instanceof registry.createClass('Call')) {
return Extrinsic.newFromValue(registry, { method: value }, version);
}
return Extrinsic.newFromValue(registry, value, version);
}
/** @internal */
private static decodeU8a (registry: Registry, value: Uint8Array, version: number): ExtrinsicVx | ExtrinsicUnknown {
if (!value.length) {
return Extrinsic.newFromValue(registry, new Uint8Array(), version);
}
const [offset, length] = Compact.decodeU8a(value);
const total = offset + length.toNumber();
assert(total <= value.length, `Extrinsic: length less than remainder, expected at least ${total}, found ${value.length}`);
const data = value.subarray(offset, total);
return Extrinsic.newFromValue(registry, data.subarray(1), data[0]);
}
/**
* @description Injects an already-generated signature into the extrinsic
@@ -289,7 +291,7 @@ export default class Extrinsic extends Base<ExtrinsicVx | ExtrinsicUnknown> impl
*/
public toU8a (isBare?: boolean): Uint8Array {
// we do not apply bare to the internal values, rather this only determines out length addition,
// where we strip all lengths this creates an un-decodable extrinsic
// where we strip all lengths this creates an extrinsic that cannot be decoded
const encoded = u8aConcat(new Uint8Array([this.version]), this.raw.toU8a());
return isBare
+11 -17
View File
@@ -214,25 +214,21 @@ export default class ExtrinsicEra extends Enum implements IExtrinsicEra {
/** @internal */
// eslint-disable-next-line @typescript-eslint/ban-types
private static decodeExtrinsicEra (value: IExtrinsicEra | MortalMethod | MortalEnumDef | ImmortalEnumDef | Uint8Array | string = new Uint8Array()): Uint8Array | Object | undefined {
if (!value) {
return new Uint8Array([0]);
} else if (value instanceof ExtrinsicEra) {
if (value instanceof ExtrinsicEra) {
return ExtrinsicEra.decodeExtrinsicEra(value.toU8a());
} else if (isHex(value)) {
return ExtrinsicEra.decodeExtrinsicEra(hexToU8a(value));
} else if (isU8a(value)) {
return (!value.length || value[0] === 0)
} else if (!value || isU8a(value)) {
return (!value?.length || value[0] === 0)
? new Uint8Array([0])
: new Uint8Array([1, value[0], value[1]]);
} else if (isObject(value)) {
// this is to de-serialize from JSON
if ((value as MortalEnumDef).MortalEra) {
return { MortalEra: (value as MortalEnumDef).MortalEra };
} else if ((value as ImmortalEnumDef).ImmortalEra) {
return { ImmortalEra: (value as ImmortalEnumDef).ImmortalEra };
}
return { MortalEra: value };
return (value as MortalEnumDef).MortalEra
? { MortalEra: (value as MortalEnumDef).MortalEra }
: (value as ImmortalEnumDef).ImmortalEra
? { ImmortalEra: (value as ImmortalEnumDef).ImmortalEra }
: { MortalEra: value };
}
throw new Error('Invalid data passed to Era');
@@ -242,11 +238,9 @@ export default class ExtrinsicEra extends Enum implements IExtrinsicEra {
* @description Override the encoded length method
*/
public get encodedLength (): number {
if (this.index === 0) {
return this.asImmortalEra.encodedLength;
} else {
return this.asMortalEra.encodedLength;
}
return this.isImmortalEra
? this.asImmortalEra.encodedLength
: this.asMortalEra.encodedLength;
}
/**
+4
View File
@@ -21,6 +21,10 @@ describe('Vote', (): void => {
expect(new Vote(registry, false).isAye).toBe(false);
});
it('constructs via undefined', (): void => {
expect(new Vote(registry).isNay).toBe(true);
});
it('has isYay for positive', (): void => {
// eslint-disable-next-line no-new-wrappers
expect(new Vote(registry, true).isAye).toBe(true);
+1 -3
View File
@@ -41,9 +41,7 @@ function decodeVoteU8a (value: Uint8Array): Uint8Array {
/** @internal */
function decodeVote (registry: Registry, value?: InputTypes): Uint8Array {
if (isUndefined(value)) {
return decodeVoteBool(false);
} else if (value instanceof Boolean || isBoolean(value)) {
if (isUndefined(value) || value instanceof Boolean || isBoolean(value)) {
return decodeVoteBool(new Bool(registry, value).isTrue);
} else if (isNumber(value)) {
return decodeVoteBool(value < 0);
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadataV0 from '@polkadot/metadata/Metadata/v0/static';
import rpcMetadataV1 from '@polkadot/metadata/Metadata/v1/static';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import { TypeRegistry } from '../../create';
@@ -16,7 +16,7 @@ describe('EventRecord', (): void => {
describe('EventRecordTo76', (): void => {
beforeEach((): void => {
// eslint-disable-next-line no-new
new Metadata(registry, rpcMetadataV0);
new Metadata(registry, rpcMetadataV1);
});
it('decodes correctly', (): void => {