Linked map support (#801)

* able to get head

* head actually working

* Correctly get linkage

* I think it is working

* Results now return both keys and values
This commit is contained in:
Jaco Greeff
2019-03-27 15:34:11 +01:00
committed by GitHub
parent d4646f99c6
commit cda0ef3486
10 changed files with 284 additions and 70 deletions
@@ -2,11 +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 { StorageFunctionMetadata } from '@polkadot/types/Metadata/v0/Modules';
import { StorageFunctionMetadata, StorageFunctionModifier, StorageFunctionType } from '@polkadot/types/Metadata/v0/Modules';
import { StorageFunction } from '@polkadot/types/primitive/StorageKey';
import { Compact, Text, createType } from '@polkadot/types';
import { Compact, Text, createType, StorageKey, Bytes, U8a } from '@polkadot/types';
import { assert, isNull, isUndefined, stringLowerFirst, stringToU8a, u8aConcat } from '@polkadot/util';
import { xxhashAsU8a } from '@polkadot/util-crypto';
import { PlainType } from '@polkadot/types/Metadata/v2/Storage';
export interface CreateItemOptions {
isUnhashed?: boolean;
@@ -24,16 +25,16 @@ export interface CreateItemOptions {
* by us manually at compile time.
*/
export default function createFunction (section: Text | string, method: Text | string, meta: StorageFunctionMetadata, options: CreateItemOptions = {}): StorageFunction {
const stringKey = options.key
? options.key
: `${section.toString()} ${method.toString()}`;
const rawKey = stringToU8a(stringKey);
// Can only have zero or one argument:
// - storage.balances.freeBalance(address)
// - storage.timestamp.blockPeriod()
const storageFn = (arg?: any): Uint8Array => {
let key = stringToU8a(
options.key
? options.key
: `${section.toString()} ${method.toString()}`
);
let key = rawKey;
if (meta.type.isMap) {
assert(!isUndefined(arg) && !isNull(arg), `${meta.name} expects one argument`);
@@ -51,6 +52,20 @@ export default function createFunction (section: Text | string, method: Text | s
);
};
if (meta.type.isMap && meta.type.asMap.isLinked) {
// TODO: there needs some better way to do this
const keyHash = new U8a(xxhashAsU8a(`head of ${stringKey}`, 128));
const keyFn: any = () => keyHash;
keyFn.meta = new StorageFunctionMetadata({
name: meta.name,
modifier: new StorageFunctionModifier('Required'),
type: new StorageFunctionType(new PlainType(meta.type.asMap.key), 0),
default: new Bytes(),
documentation: meta.documentation
});
storageFn.headKey = new StorageKey(keyFn);
}
storageFn.meta = meta;
storageFn.method = stringLowerFirst(method.toString());
storageFn.section = stringLowerFirst(section.toString());