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
+94 -2
View File
@@ -17,19 +17,21 @@ import {
} from './types';
import EventEmitter from 'eventemitter3';
import { map } from 'rxjs/operators';
import { map, switchMap } from 'rxjs/operators';
import decorateDerive, { Derive as DeriveInterface } from '@polkadot/api-derive';
import extrinsicsFromMeta from '@polkadot/extrinsics/fromMetadata';
import RpcBase from '@polkadot/rpc-core';
import RpcRx from '@polkadot/rpc-rx';
import storageFromMeta from '@polkadot/storage/fromMetadata';
import { Event, getTypeRegistry, Hash, Metadata, Method, RuntimeVersion } from '@polkadot/types';
import { Event, getTypeRegistry, Hash, Metadata, Method, RuntimeVersion, Tuple, Null } from '@polkadot/types';
import { MethodFunction, ModulesWithMethods } from '@polkadot/types/primitive/Method';
import { StorageFunction } from '@polkadot/types/primitive/StorageKey';
import { assert, compactStripLength, isFunction, isObject, isUndefined, logger, u8aToHex } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { createSubmittableExtrinsic, SubmittableExtrinsic } from './SubmittableExtrinsic';
import { Linkage, LinkageResult } from '@polkadot/types/codec/Linkage';
import { Observable, of } from 'rxjs';
type MetaDecoration = {
callIndex?: Uint8Array,
@@ -491,6 +493,77 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
params = args.slice(0, args.length - 1);
}
if (method.headKey && params.length === 0) {
// Fetch all values for this linked map
const result: Map<Codec, Tuple> = new Map();
let head: Codec | null = null;
const getNext = (key: Codec): Observable<any> => {
if (head === null) {
head = key;
}
return this._rpcRx.state.subscribeStorage([[method, key]])
.pipe(
switchMap(([data]: [Tuple]) => {
const linkage = data[1] as Linkage<Codec>;
if (linkage.next && linkage.previous) {
result.set(key, data);
if (linkage.next && linkage.next.isSome) {
return getNext(linkage.next.unwrap());
}
}
const keys = [];
const values = [];
let nextKey = head;
while (nextKey) {
const entry = result.get(nextKey);
if (!entry) {
break;
}
const [item, linkage] = entry as any as [Codec, Linkage<Codec>];
keys.push(nextKey);
values.push(item);
nextKey = linkage.next && linkage.next.unwrapOr(null);
}
return of(
values.length
? new LinkageResult(
[keys[0].constructor as any, keys],
[values[0].constructor as any, values]
)
: new LinkageResult(
[Null, []],
[Null, []]
)
);
})
);
};
return onCall(
(arg: CodecArg) => this._rpcRx.state
.subscribeStorage([arg])
.pipe(
switchMap((result: Array<Codec>) => {
const key = result[0];
return getNext(key);
})
),
[method.headKey],
callback
);
}
return onCall(
(arg: CodecArg) => this._rpcRx.state
.subscribeStorage([[method, arg]])
@@ -530,6 +603,25 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
decorated.key = (arg?: CodecArg): string =>
u8aToHex(compactStripLength(method(arg))[1]);
// Linked Map support
if (method.headKey) {
decorated.head = (): C =>
onCall(
(arg: CodecArg) => this._rpcRx.state
.getStorage(arg)
.pipe(
switchMap(key => this._rpcRx.state.getStorage([method, key]))
)
,
[method.headKey]
) as C;
} else {
decorated.head = () => {
throw new Error(`${method.name} is not LinkedMap`);
};
}
return this.decorateFunctionMeta(method, decorated) as QueryableStorageFunction<C, S>;
}
+1
View File
@@ -59,6 +59,7 @@ export interface QueryableStorageFunctionBase<CodecResult, SubscriptionResult> e
hash: (arg?: CodecArg) => HashResult<CodecResult, SubscriptionResult>;
key: (arg?: CodecArg) => string;
size: (arg?: CodecArg) => U64Result<CodecResult, SubscriptionResult>;
head: () => CodecResult;
}
interface QueryableStorageFunctionPromise<CodecResult, SubscriptionResult> extends QueryableStorageFunctionBase<CodecResult, SubscriptionResult> {
@@ -0,0 +1,38 @@
// Copyright 2017-2019 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import Api from '../../src/promise';
import WsProvider from '../../../rpc-provider/src/ws';
describe.skip('alex queries', () => {
let api;
beforeAll(async () => {
api = await Api.create({
provider: new WsProvider('wss://poc3-rpc.polkadot.io/')
});
return api;
});
beforeEach(() => {
jest.setTimeout(30000);
});
it('retrieves the list of validators', (done) => {
api.query.staking.validators((res) => {
console.log('api.query.staking.validators():', res.toJSON());
done();
});
});
it('retrieves a single value', (done) => {
api.query.staking.validators('5EF7wdkP9XZq38fu2ioAvxoEhHUJUc1E2KbuSQSMGofRKhCL', (res) => {
console.log('api.query.staking.validators(id):', res.toJSON());
done();
});
});
});
+1 -1
View File
@@ -13,7 +13,7 @@ describe.skip('e2e queries', () => {
beforeEach(async (done) => {
api = await Api.create().toPromise();
jest.setTimeout(30000);
jest.setTimeout(3000000);
done();
});