Bytes & Extrinsic decoding cleanups (#1337)

* Bytes & Extrinsic decoding cleanups

* Add CHANGELOG entry for Extrinsic changes
This commit is contained in:
Jaco Greeff
2019-08-29 11:43:25 +02:00
committed by GitHub
parent 470698247d
commit a0194685b8
4 changed files with 79 additions and 63 deletions
+32 -28
View File
@@ -24,41 +24,45 @@ export default class Bytes extends U8a {
private static decodeBytes (value?: AnyU8a): Uint8Array | undefined {
if (Array.isArray(value) || isString(value)) {
const u8a = u8aToU8a(value);
return Bytes.decodeBytes(
Compact.addLengthPrefix(u8a)
return Bytes.decodeBytesU8a(
Compact.addLengthPrefix(u8aToU8a(value))
);
} else if (value instanceof ClassOf('StorageData')) {
// Here we cater for the actual StorageData that _could_ have a length prefix. In the
// case of `:code` it is not added, for others it is
const u8a = value as Uint8Array;
const [offset, length] = Compact.decodeU8a(u8a);
return u8a.length === length.addn(offset).toNumber()
? u8a.subarray(offset)
: u8a;
} else if (value instanceof U8a) {
// This is required. In the case of a U8a we already have gotten rid of the length,
// i.e. new Bytes(new Bytes(...)) will work as expected
return value;
} else if (isU8a(value)) {
if (!value.length) {
return new Uint8Array();
}
// handle all other Uint8Array inputs, these do have a length prefix
const [offset, length] = Compact.decodeU8a(value);
const total = offset + length.toNumber();
assert(total <= value.length, `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
return value.subarray(offset, total);
return Bytes.decodeBytesStorage(value);
} else if (!(value instanceof U8a) && isU8a(value)) {
// We are ensuring we are not a U8a instance. In the case of a U8a we already have gotten
// rid of the length, i.e. new Bytes(new Bytes(...)) will work as expected
return Bytes.decodeBytesU8a(value);
}
return value;
}
private static decodeBytesStorage (value: Uint8Array): Uint8Array {
// Here we cater for the actual StorageData that _could_ have a length prefix. In the
// case of `:code` it is not added, for others it is
const u8a = value as Uint8Array;
const [offset, length] = Compact.decodeU8a(u8a);
return u8a.length === length.addn(offset).toNumber()
? u8a.subarray(offset)
: u8a;
}
private static decodeBytesU8a (value: Uint8Array): Uint8Array {
if (!value.length) {
return new Uint8Array();
}
// handle all other Uint8Array inputs, these do have a length prefix
const [offset, length] = Compact.decodeU8a(value);
const total = offset + length.toNumber();
assert(total <= value.length, `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
return value.subarray(offset, total);
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
@@ -64,32 +64,9 @@ export default class Extrinsic extends Base<ExtrinsicVx | ExtrinsicUnknown> impl
public static decodeExtrinsic (value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, version: number = DEFAULT_VERSION): ExtrinsicVx | ExtrinsicUnknown {
if (Array.isArray(value) || isHex(value)) {
// Instead of the block below, it should simply be:
// return Extrinsic.decodeExtrinsic(hexToU8a(value as string));
const u8a = u8aToU8a(value);
// HACK 11 Jan 2019 - before https://github.com/paritytech/substrate/pull/1388
// extrinsics didn't have the length, cater for both approaches
const [offset, length] = Compact.decodeU8a(u8a);
const withPrefix = u8a.length === (offset + length.toNumber());
return Extrinsic.decodeExtrinsic(
withPrefix
? u8a
: Compact.addLengthPrefix(u8a),
version
);
return Extrinsic.decodeU8aLike(value, version);
} else if (isU8a(value)) {
if (!value.length) {
return Extrinsic.newFromValue(new Uint8Array(), version);
}
const [offset, length] = Compact.decodeU8a(value);
const total = offset + length.toNumber();
assert(total <= value.length, `Extrinsic: required length less than remainder, expected at least ${total}, found ${value.length}`);
return Extrinsic.decodeU8a(value.subarray(offset, total));
return Extrinsic.decodeU8a(value, version);
} else if (value instanceof ClassOf('Call')) {
return Extrinsic.newFromValue({ method: value }, version);
}
@@ -97,8 +74,38 @@ export default class Extrinsic extends Base<ExtrinsicVx | ExtrinsicUnknown> impl
return Extrinsic.newFromValue(value, version);
}
private static decodeU8a (value: Uint8Array): ExtrinsicVx | ExtrinsicUnknown {
return Extrinsic.newFromValue(value.subarray(1), value[0]);
private static decodeU8aLike (value: string | number[], version: number): ExtrinsicVx | ExtrinsicUnknown {
// Instead of the block below, it should simply be:
// return Extrinsic.decodeExtrinsic(hexToU8a(value as string));
const u8a = u8aToU8a(value);
// HACK 11 Jan 2019 - before https://github.com/paritytech/substrate/pull/1388
// extrinsics didn't have the length, cater for both approaches. This is very
// inconsistent with any other `Vec<u8>` implementation
const [offset, length] = Compact.decodeU8a(u8a);
const withPrefix = u8a.length === (offset + length.toNumber());
return Extrinsic.decodeU8a(
withPrefix
? u8a
: Compact.addLengthPrefix(u8a),
version
);
}
private static decodeU8a (value: Uint8Array, version: number): ExtrinsicVx | ExtrinsicUnknown {
if (!value.length) {
return Extrinsic.newFromValue(new Uint8Array(), version);
}
const [offset, length] = Compact.decodeU8a(value);
const total = offset + length.toNumber();
assert(total <= value.length, `Extrinsic: required length less than remainder, expected at least ${total}, found ${value.length}`);
const data = value.subarray(offset, total);
return Extrinsic.newFromValue(data.subarray(1), data[0]);
}
/**
@@ -63,14 +63,7 @@ export class MortalEra extends Tuple {
} else if (isU8a(value)) {
return MortalEra.decodeMortalU8a(value);
} else if (isObject(value)) {
const { current, period } = value;
let calPeriod = Math.pow(2, Math.ceil(Math.log2(period)));
calPeriod = Math.min(Math.max(calPeriod, 4), 1 << 16);
const phase = current % calPeriod;
const quantizeFactor = Math.max(calPeriod >> 12, 1);
const quantizedPhase = phase / quantizeFactor * quantizeFactor;
return [new U64(calPeriod), new U64(quantizedPhase)];
return MortalEra.decodeMortalObject(value);
} else if (!value) {
return [new U64(), new U64()];
}
@@ -78,6 +71,17 @@ export class MortalEra extends Tuple {
throw new Error('Invalid data passed to Mortal era');
}
private static decodeMortalObject (value: MortalMethod): MortalEraValue {
const { current, period } = value;
let calPeriod = Math.pow(2, Math.ceil(Math.log2(period)));
calPeriod = Math.min(Math.max(calPeriod, 4), 1 << 16);
const phase = current % calPeriod;
const quantizeFactor = Math.max(calPeriod >> 12, 1);
const quantizedPhase = phase / quantizeFactor * quantizeFactor;
return [new U64(calPeriod), new U64(quantizedPhase)];
}
private static decodeMortalU8a (value: Uint8Array): MortalEraValue {
if (value.length === 0) {
return [new U64(), new U64()];