Fix StorageData checks, fix Option toHex 00/01 (#1052)
* Option.toHex should not add 00/01 (implied) * Option tests * We have a failing test, finally * Adapt createType to cater for storage with length
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Option from './Option';
|
||||
import Bytes from '../primitive/Bytes';
|
||||
import U32 from '../primitive/U32';
|
||||
import Text from '../primitive/Text';
|
||||
|
||||
const testDecode = (type: string, input: any, expected: any) =>
|
||||
@@ -33,12 +35,28 @@ describe('Option', () => {
|
||||
).toEqual('hello');
|
||||
});
|
||||
|
||||
it('properly converts correctly toHex (Bytes)', () => {
|
||||
// Option<Bytes> for a parachain head, however, this is effectively an
|
||||
// Option<Option<Bytes>> (hence the length, since it is from storage)
|
||||
const HEX = '0x210100000000000000000000000000000000000000000000000000000000000000000000000000000000011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce';
|
||||
expect(
|
||||
new Option(Bytes, HEX).toHex().substr(2)
|
||||
).toEqual(HEX.substr(6));
|
||||
});
|
||||
|
||||
it.only('properly converts correctly toHex (U64)', () => {
|
||||
const HEX = '0x12345678';
|
||||
expect(
|
||||
(new Option(U32, HEX).unwrap() as U32).toNumber()
|
||||
).toEqual(0x12345678);
|
||||
});
|
||||
|
||||
testDecode('string (with)', 'foo', 'foo');
|
||||
testDecode('string (without)', undefined, '');
|
||||
testDecode('Uint8Array (with)', Uint8Array.from([1, 12, 102, 111, 111]), 'foo');
|
||||
testDecode('Uint8Array (without)', Uint8Array.from([0]), '');
|
||||
|
||||
testEncode('toHex', '0x010c666f6f');
|
||||
testEncode('toHex', '0x0c666f6f');
|
||||
testEncode('toString', 'foo');
|
||||
testEncode('toU8a', Uint8Array.from([1, 12, 102, 111, 111]));
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
|
||||
} else if (isU8a(value)) {
|
||||
// the isU8a check happens last in the if-tree - since the wrapped value
|
||||
// may be an instance of it, so Type and Option checks go in first
|
||||
return value[0] === 0
|
||||
return !value.length || value[0] === 0
|
||||
? new Null()
|
||||
: new Type(value.subarray(1));
|
||||
}
|
||||
@@ -105,7 +105,11 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
toHex (): string {
|
||||
return u8aToHex(this.toU8a());
|
||||
// This attempts to align with the JSON encoding - actually in this case
|
||||
// the isSome value is correct, however the `isNone` may be problematic
|
||||
return this.isNone
|
||||
? '0x'
|
||||
: u8aToHex(this.toU8a().subarray(1));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -250,7 +250,7 @@ describe('createType', () => {
|
||||
|
||||
expect(
|
||||
() => createType('DoubleMap<Vec<(BlockNumber,EventIndex)>>', base, true)
|
||||
).toThrow(/Encoding for input doesn't match output, created 0x00 from 0x/);
|
||||
).toThrow(/ Input doesn't match output, received 0x, created 0x00/);
|
||||
});
|
||||
|
||||
it('throw error when create base is a StorageData with null value and isPedantic is true', () => {
|
||||
@@ -258,6 +258,6 @@ describe('createType', () => {
|
||||
|
||||
expect(
|
||||
() => createType('Vec<(BlockNumber,EventIndex)>', base, true)
|
||||
).toThrow(/Encoding for input doesn't match output, created 0x00 from 0x/);
|
||||
).toThrow(/Input doesn't match output, received 0x, created 0x00/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@ import { assert } from '@polkadot/util';
|
||||
|
||||
import { Codec, Constructor } from '../types';
|
||||
import Null from '../primitive/Null';
|
||||
import StorageData from '../primitive/StorageData';
|
||||
import Text from '../primitive/Text';
|
||||
import Compact from './Compact';
|
||||
import Enum from './Enum';
|
||||
@@ -267,11 +268,19 @@ function initType (Type: Constructor, value?: any, isPedantic?: boolean): Codec
|
||||
|
||||
// in pedantic mode, actually check that the encoding matches that supplied - this
|
||||
// is much slower, but ensures that we have a 100% grasp on the actual provided value
|
||||
if (isPedantic && value && value.toHex) {
|
||||
if (isPedantic && value && value.toHex && value.toU8a) {
|
||||
const inHex = value.toHex(true);
|
||||
const crHex = created.toHex(true);
|
||||
|
||||
assert(crHex === inHex, `Encoding for input doesn't match output, created ${crHex} from ${inHex}`);
|
||||
assert(
|
||||
inHex === crHex || // check that the hex matches, if matching, all-ok
|
||||
(
|
||||
(value instanceof StorageData) && // input is from storage
|
||||
(created instanceof Uint8Array) && // we are a variable-lneght structure
|
||||
(value.toU8a(true).toString() === created.toU8a().toString()) // strip the input length
|
||||
),
|
||||
`Input doesn't match output, received ${inHex}, created ${crHex}`
|
||||
);
|
||||
}
|
||||
|
||||
return created;
|
||||
|
||||
@@ -60,7 +60,7 @@ export interface Codec {
|
||||
eq (other?: any): boolean;
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) repersentation
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
||||
*/
|
||||
toHex (isLe?: boolean): string;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user