support double map storage function (#934)

* support double map storage function

* add twox_64_concat, clean comments

* change Twox128Concat to Twox64Concat

* update decodeStorageKey

* fix: update decodeStorageKey

* test: add e2e test
This commit is contained in:
Alex Wang
2019-05-21 10:25:58 +02:00
committed by Jaco Greeff
parent dc8351a7ce
commit 833b49d012
10 changed files with 182 additions and 22 deletions
+1 -1
View File
@@ -170,7 +170,7 @@ export class StorageFunctionType extends Enum {
*/
toString (): string {
if (this.isDoubleMap) {
return `DoubleMap<${this.asDoubleMap.toString()}>`;
return `DoubleMap<${this.asDoubleMap.value.toString()}>`;
}
if (this.isMap) {
+10 -2
View File
@@ -63,10 +63,10 @@ describe('getTypeValue', () => {
it('returns a type structure', () => {
expect(
getTypeDef('(u32, Compact<u32>, Vec<u64>, Option<u128>, (Text, Vec<(Bool, u128)>))')
getTypeDef('(u32, Compact<u32>, Vec<u64>, Option<u128>, DoubleMap<u128>, (Text, Vec<(Bool, u128)>))')
).toEqual({
info: TypeDefInfo.Tuple,
type: '(u32, Compact<u32>, Vec<u64>, Option<u128>, (Text, Vec<(Bool, u128)>))',
type: '(u32, Compact<u32>, Vec<u64>, Option<u128>, DoubleMap<u128>, (Text, Vec<(Bool, u128)>))',
sub: [
{
info: TypeDefInfo.Plain,
@@ -96,6 +96,14 @@ describe('getTypeValue', () => {
type: 'u128'
}
},
{
info: TypeDefInfo.DoubleMap,
type: 'DoubleMap<u128>',
sub: {
info: TypeDefInfo.Plain,
type: 'u128'
}
},
{
info: TypeDefInfo.Tuple,
type: '(Text, Vec<(Bool, u128)>)',
+7 -2
View File
@@ -26,6 +26,7 @@ export enum TypeDefInfo {
Tuple,
Vector,
Linkage,
DoubleMap,
// anything not full supported (keep this as the last entry)
Null
}
@@ -147,8 +148,8 @@ export function getTypeDef (_type: Text | string, name?: string): TypeDef {
value.info = TypeDefInfo.Linkage;
value.sub = getTypeDef(subType);
} else if (startingWith(type, 'DoubleMap<', '>')) {
// FIXME As a first step, only allow parsing of DoubleMap, don't actually init
value.info = TypeDefInfo.Null;
value.info = TypeDefInfo.DoubleMap;
value.sub = getTypeDef(subType);
}
return value;
@@ -213,6 +214,10 @@ export function getTypeClass (value: TypeDef): Constructor {
return Linkage.withKey(
getTypeClass(value.sub as TypeDef)
);
case TypeDefInfo.DoubleMap:
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for DoubleMap');
return getTypeClass(value.sub as TypeDef);
case TypeDefInfo.Null:
return Null;
}
@@ -11,7 +11,7 @@ export default class StorageHasher extends Enum {
'Blake2_256',
'Twox128',
'Twox256',
'Twox128Concat'
'Twox64Concat'
], value);
}
@@ -44,9 +44,9 @@ export default class StorageHasher extends Enum {
}
/**
* @description Is the enum isTwox128Concat?
* @description Is the enum isTwox64Concat?
*/
get isTwox128Concat (): boolean {
get isTwox64Concat (): boolean {
return this.toNumber() === 4;
}
@@ -58,8 +58,8 @@ describe('StorageKey', () => {
expect(
new StorageKey(
storage
.balances
.freeBalance('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')
.balances
.freeBalance('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')
)
.toU8a()
).toEqual(
@@ -69,5 +69,29 @@ describe('StorageKey', () => {
])
);
});
it(`should correctly get the EventTopics double map storage key (hex)`, () => {
expect(
new StorageKey([
storage
.system
.eventTopics,
['any', [1, 2, 3]]
])
.toHex()
).toBe('0x3007c9ff7027f65900abcdfca4fdb107ead47e2a9e3558e01b691b0f4a5f8518d487326614f066416308bf6aa4e5041d1949928e4b26ede98e3cebb36a3b1726');
});
it(`should correctly get the EventTopics double map storage key (u8a)`, () => {
expect(
new StorageKey([
storage
.system
.eventTopics,
['any', [1, 2, 3]]
])
.toU8a()
).toEqual(Uint8Array.from([1, 1, 48, 7, 201, 255, 112, 39, 246, 89, 0, 171, 205, 252, 164, 253, 177, 7, 234, 212, 126, 42, 158, 53, 88, 224, 27, 105, 27, 15, 74, 95, 133, 24, 212, 135, 50, 102, 20, 240, 102, 65, 99, 8, 191, 106, 164, 229, 4, 29, 25, 73, 146, 142, 75, 38, 237, 233, 142, 60, 235, 179, 106, 59, 23, 38]));
});
});
});
+11 -3
View File
@@ -2,12 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AnyU8a } from '../types';
import { assert, isFunction, isString, isU8a } from '@polkadot/util';
import Bytes from './Bytes';
import U8a from '../codec/U8a';
import { StorageFunctionMetadata as MetaV4 } from '../Metadata/v4/Storage';
import { AnyU8a } from '../types';
import Bytes from './Bytes';
export interface StorageFunction {
(arg?: any): Uint8Array;
@@ -70,6 +70,14 @@ export default class StorageKey extends Bytes {
assert(isFunction(fn), 'Expected function input for key construction');
if (fn.meta && fn.meta.type.isDoubleMap) {
return {
key: new U8a(fn(...arg)), // skip compact length check in decodeBytes
method: fn.method,
section: fn.section
};
}
return {
key: fn(...arg),
method: fn.method,