From 5207285b4370d13c6c29b9392363ae80bf55def6 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 11 Oct 2018 21:28:47 +0200 Subject: [PATCH] Storage key are not length-prefixed anymore (#257) * Fix lenght prefix * Put add lenght prefix to compact --- .../type-storage/src/fromMetadata.spec.js | 1 + packages/type-storage/src/substrate.spec.js | 12 +++---- .../src/utils/createFunction.spec.js | 4 +-- .../type-storage/src/utils/createFunction.ts | 31 +++++++++-------- packages/types/src/StorageKey.spec.js | 33 +++++++++++++++++++ packages/types/src/codec/Compact.ts | 12 +++++++ 6 files changed, 68 insertions(+), 25 deletions(-) create mode 100644 packages/types/src/StorageKey.spec.js diff --git a/packages/type-storage/src/fromMetadata.spec.js b/packages/type-storage/src/fromMetadata.spec.js index 9706bf398c..7b6181fe0a 100644 --- a/packages/type-storage/src/fromMetadata.spec.js +++ b/packages/type-storage/src/fromMetadata.spec.js @@ -23,6 +23,7 @@ describe('fromMetadata', () => { // FIXME check again when we have a valid chain/UI it('should return the correct length-prefixed storage key', () => { expect(newStorage.balances.freeBalance('5GwPuAgYgP6q58uWTXp4uSg6FwfzQv9HfFZwAFEREUrQjCvy')).toEqual( + // U8a is length-prefixed Uint8Array.from([64, 52, 103, 23, 10, 157, 95, 244, 9, 70, 215, 120, 149, 1, 238, 109, 69]) ); }); diff --git a/packages/type-storage/src/substrate.spec.js b/packages/type-storage/src/substrate.spec.js index d0c3aa0be6..240c78a74f 100644 --- a/packages/type-storage/src/substrate.spec.js +++ b/packages/type-storage/src/substrate.spec.js @@ -6,28 +6,28 @@ import { authorityCount, authorityPrefix, changesTrieConfig, code, extrinsicInde describe('substrate', () => { it('authorityCount should return the correct storage key', () => { - expect(authorityCount()).toEqual(Uint8Array.from([58, 97, 117, 116, 104, 58, 108, 101, 110])); + expect(authorityCount()).toEqual(Uint8Array.from([36, 58, 97, 117, 116, 104, 58, 108, 101, 110])); // Length-prefixed }); it('authorityPrefix should return the correct storage key', () => { - expect(authorityPrefix()).toEqual(Uint8Array.from([58, 97, 117, 116, 104, 58])); + expect(authorityPrefix()).toEqual(Uint8Array.from([24, 58, 97, 117, 116, 104, 58])); // Length-prefixed }); it('changesTrieConfig should return the correct storage key', () => { - expect(changesTrieConfig()).toEqual(Uint8Array.from([58, 99, 104, 97, 110, 103, 101, 115, 95, 116, 114, 105, 101])); + expect(changesTrieConfig()).toEqual(Uint8Array.from([52, 58, 99, 104, 97, 110, 103, 101, 115, 95, 116, 114, 105, 101])); // Length-prefixed }); it('code should return the correct storage key', () => { - expect(code()).toEqual(Uint8Array.from([58, 99, 111, 100, 101])); + expect(code()).toEqual(Uint8Array.from([20, 58, 99, 111, 100, 101])); // Length-prefixed }); it('extrinsicIndex should return the correct storage key', () => { expect(extrinsicIndex()).toEqual( - Uint8Array.from([58, 101, 120, 116, 114, 105, 110, 115, 105, 99, 95, 105, 110, 100, 101, 120]) + Uint8Array.from([64, 58, 101, 120, 116, 114, 105, 110, 115, 105, 99, 95, 105, 110, 100, 101, 120]) // Length-prefixed ); }); it('heapPages should return the correct storage key', () => { - expect(heapPages()).toEqual(Uint8Array.from([58, 104, 101, 97, 112, 112, 97, 103, 101, 115])); + expect(heapPages()).toEqual(Uint8Array.from([40, 58, 104, 101, 97, 112, 112, 97, 103, 101, 115])); // Length-prefixed }); }); diff --git a/packages/type-storage/src/utils/createFunction.spec.js b/packages/type-storage/src/utils/createFunction.spec.js index 11b1cab8dd..19359a5c36 100644 --- a/packages/type-storage/src/utils/createFunction.spec.js +++ b/packages/type-storage/src/utils/createFunction.spec.js @@ -2,8 +2,6 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import hexToU8a from '@polkadot/util/hex/toU8a'; - import createFunction from './createFunction'; describe('createFunction', () => { @@ -15,7 +13,7 @@ describe('createFunction', () => { { type: {} } )() ).toEqual( - hexToU8a('0x0e4944cfd98d6f4cc374d16f5a4e3f9c') + Uint8Array.from([64, 14, 73, 68, 207, 217, 141, 111, 76, 195, 116, 209, 111, 90, 78, 63, 156]) // Length-prefixed ); }); diff --git a/packages/type-storage/src/utils/createFunction.ts b/packages/type-storage/src/utils/createFunction.ts index 43cad43689..cc7416ad98 100644 --- a/packages/type-storage/src/utils/createFunction.ts +++ b/packages/type-storage/src/utils/createFunction.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import Compact, { DEFAULT_LENGTH_BITS } from '@polkadot/types/codec/Compact'; +import Compact from '@polkadot/types/codec/Compact'; import { createType } from '@polkadot/types/codec'; import { StorageFunctionMetadata } from '@polkadot/types/Metadata'; import { StorageFunction } from '@polkadot/types/StorageKey'; @@ -40,7 +40,7 @@ export default function createFunction ( // assumption, but will break if the base substrate keys employ hashing as well if (options.isUnhashed) { storageFn = (): Uint8Array => - u8aFromUtf8(method.toString()); + Compact.addLengthPrefix(u8aFromUtf8(method.toString())); } else { // TODO Find better type than any // Can only have zero or one argument: @@ -48,9 +48,11 @@ export default function createFunction ( // - storage.timestamp.blockPeriod() storageFn = (arg?: any): Uint8Array => { if (!meta.type.isMap) { - return xxhash( - u8aFromUtf8(`${section.toString()} ${method.toString()}`), - 128 + return Compact.addLengthPrefix( + xxhash( + u8aFromUtf8(`${section.toString()} ${method.toString()}`), + 128 + ) ); } @@ -60,18 +62,15 @@ export default function createFunction ( const type = meta.type.asMap.key.toString(); // Argument type, as string - const methodAndArgs = xxhash( - u8aConcat( - u8aFromUtf8(`${section.toString()} ${method.toString()}`), - createType(type, arg).toU8a(true) - ), - 128 - ); - // StorageKey is a Bytes, so is length-prefixed - return u8aConcat( - Compact.encodeU8a(methodAndArgs.length, DEFAULT_LENGTH_BITS), - methodAndArgs + return Compact.addLengthPrefix( + xxhash( + u8aConcat( + u8aFromUtf8(`${section.toString()} ${method.toString()}`), + createType(type, arg).toU8a(true) + ), + 128 + ) ); }; } diff --git a/packages/types/src/StorageKey.spec.js b/packages/types/src/StorageKey.spec.js new file mode 100644 index 0000000000..20ce1bafbe --- /dev/null +++ b/packages/types/src/StorageKey.spec.js @@ -0,0 +1,33 @@ +// Copyright 2017-2018 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import storage from '@polkadot/storage/static' + +import StorageKey from './StorageKey' + +describe('StorageKey', () => { + it("should correctly get Alice's freeBalance storage key (hex)", () => { + expect( + new StorageKey( + storage + .balances + .freeBalance('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ') + ) + .toHex() + ).toBe('0xce3f3d8f09e3411403f5ca59d042a40e'); // FIXME OK this should be length-prefixed in reality + }); + + it("should correctly get Alice's freeBalance storage key (u8a)", () => { + expect( + new StorageKey( + storage + .balances + .freeBalance('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ') + ) + .toU8a() + ).toEqual( + Uint8Array.from([64, 206, 63, 61, 143, 9, 227, 65, 20, 3, 245, 202, 89, 208, 66, 164, 14]) // Length-prefixed + ); + }); +}); diff --git a/packages/types/src/codec/Compact.ts b/packages/types/src/codec/Compact.ts index e3dae56ebb..26114812f6 100644 --- a/packages/types/src/codec/Compact.ts +++ b/packages/types/src/codec/Compact.ts @@ -50,6 +50,18 @@ export default class Compact extends UInt { return [byteLength + 1, u8aToBn(input.subarray(1, 1 + byteLength), true)]; } + /** + * Prepend a Uint8Array with its compact length. + * + * @param u8a - The Uint8Array to be prefixed + */ + static addLengthPrefix (u8a: Uint8Array): Uint8Array { + return u8aConcat( + Compact.encodeU8a(u8a.length, DEFAULT_LENGTH_BITS), + u8a + ); + } + static encodeU8a (_value: UInt | BN | number, bitLength: UIntBitLength): Uint8Array { const value = _value instanceof UInt ? _value.toBn()