Don't decode and re-encode storage queries, single pass (#1433)
* Don't decode and re-encode storage queries, single pass * Remove old SorageData <-> Bytes HACK tests * Add non-empty check to isPendantic
This commit is contained in:
@@ -51,8 +51,10 @@ export default class Linkage<T extends Codec> extends Struct {
|
||||
/**
|
||||
* @description Custom toU8a which with bare mode does not return the linkage if empty
|
||||
*/
|
||||
public toU8a (isBare?: boolean): Uint8Array {
|
||||
return isBare && this.isEmpty
|
||||
public toU8a (): Uint8Array {
|
||||
// As part of a storage query (where these appear), in the case of empty, the values
|
||||
// are NOT populated by the node - follow the same logic, leaving it empty
|
||||
return this.isEmpty
|
||||
? EMPTY
|
||||
: super.toU8a();
|
||||
}
|
||||
|
||||
@@ -5,23 +5,24 @@
|
||||
import { Codec, Constructor, InterfaceTypes } from '../../types';
|
||||
import { FromReg } from './types';
|
||||
|
||||
import { isU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { InterfaceRegistry } from '../../interfaceRegistry';
|
||||
import { createClass } from './createClass';
|
||||
|
||||
function u8aHasValue (value: Uint8Array): boolean {
|
||||
return value.some((v): boolean => !!v);
|
||||
}
|
||||
|
||||
// With isPedantic, actually check that the encoding matches that supplied. This
|
||||
// is much slower, but verifies that we have the correct types defined
|
||||
function checkInstance<T extends Codec = Codec, K extends string = string> (value: any, created: FromReg<T, K>): void {
|
||||
const inHex = value.toHex(true);
|
||||
function checkInstance<T extends Codec = Codec, K extends string = string> (value: Uint8Array, created: FromReg<T, K>): void {
|
||||
// For option, we are not adding the initial byte (this is via storage)
|
||||
const crHex = created.toHex(true);
|
||||
const hasMatch = (inHex === crHex) || (value.toU8a(true).toString() === (
|
||||
created instanceof Uint8Array
|
||||
// strip the input length
|
||||
? created.toU8a().toString()
|
||||
// compare raw. without additions
|
||||
: created.toU8a(true).toString()
|
||||
));
|
||||
const inHex = u8aToHex(value);
|
||||
|
||||
if (!hasMatch) {
|
||||
// if the hex doesn't match and the value for both is non-empty, complain... bitterly
|
||||
if (inHex !== crHex && (u8aHasValue(value) || u8aHasValue(created.toU8a(true)))) {
|
||||
console.warn(`${created.toRawType()}:: Input doesn't match output, received ${inHex}, created ${crHex}`);
|
||||
}
|
||||
}
|
||||
@@ -32,7 +33,7 @@ function initType<T extends Codec = Codec, K extends string = string> (Type: Con
|
||||
const created = new Type(...params);
|
||||
const [value] = params;
|
||||
|
||||
if (isPedantic && value && value.toU8a && !value.isEmpty) {
|
||||
if (isPedantic && isU8a(value)) {
|
||||
checkInstance(value, created);
|
||||
}
|
||||
|
||||
|
||||
@@ -43,11 +43,5 @@ describe('StorageChangeSet', (): void => {
|
||||
expect(set.changes).toHaveLength(1);
|
||||
expect(set.changes[0][0].toHex()).toEqual('0x54bdbdb5e438d574dd4da05ee6131cee');
|
||||
});
|
||||
|
||||
it('converts result data to Bytes', (): void => {
|
||||
const bytes = createType('Bytes', set.changes[0][1].unwrap());
|
||||
|
||||
expect(bytes).toHaveLength(4452);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -36,13 +36,5 @@ describe('Bytes', (): void => {
|
||||
).toU8a()
|
||||
).toEqual(CODE);
|
||||
});
|
||||
|
||||
it('creates via storagedata (with length prefix)', (): void => {
|
||||
expect(
|
||||
new Bytes(
|
||||
createType('StorageData', '0x143a636f6465')
|
||||
).toU8a()
|
||||
).toEqual(CODE);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,6 @@ import { AnyU8a } from '../types';
|
||||
|
||||
import { assert, isString, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { ClassOf } from '../codec/create';
|
||||
import Compact from '../codec/Compact';
|
||||
import U8a from '../codec/U8a';
|
||||
|
||||
@@ -24,11 +23,7 @@ export default class Bytes extends U8a {
|
||||
|
||||
private static decodeBytes (value?: AnyU8a): Uint8Array | undefined {
|
||||
if (Array.isArray(value) || isString(value)) {
|
||||
return Bytes.decodeBytesU8a(
|
||||
Compact.addLengthPrefix(u8aToU8a(value))
|
||||
);
|
||||
} else if (value instanceof ClassOf('StorageData')) {
|
||||
return Bytes.decodeBytesStorage(value);
|
||||
return u8aToU8a(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
|
||||
@@ -38,16 +33,6 @@ export default class Bytes extends U8a {
|
||||
return value;
|
||||
}
|
||||
|
||||
private static decodeBytesStorage (u8a: 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 [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();
|
||||
|
||||
Reference in New Issue
Block a user