Split parachains.derive calls (#1948)

* api.derive.registrar.parachains

* remove api parse

* suggestions

* some

* parachains: overview/info

* rm all

* rm all

* refine
This commit is contained in:
Keith Ingram
2020-02-26 11:26:04 +01:00
committed by GitHub
parent 68f2cb5bb2
commit 1a50298ffe
7 changed files with 196 additions and 125 deletions
+1
View File
@@ -46,6 +46,7 @@ const deriveAvail: Record<string, string[]> = {
democracy: ['democracy'],
elections: ['electionsPhragmen', 'elections'],
imOnline: ['imOnline'],
parachains: ['parachains', 'registrar'],
session: ['session'],
society: ['society'],
staking: ['staking'],
-120
View File
@@ -1,120 +0,0 @@
// Copyright 2017-2020 @polkadot/api-derive 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 { BlockNumber, CollatorId, ParaId, ParaInfo, Retriable, UpwardMessage } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { DeriveParachain, DeriveParachainActive } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Bytes, Option, Vec } from '@polkadot/types';
import { memo } from '../util';
type ParaIds = ParaId[];
type ParaInfoResult = Option<ParaInfo>;
type PendingSwap = Option<ParaId>;
type Active = ITuple<[ParaId, Option<ITuple<[CollatorId, Retriable]>>]>[];
type RetryQueue = ITuple<[ParaId, CollatorId]>[][]
type SelectedThreads = ITuple<[ParaId, CollatorId]>[][];
type Heads = Bytes;
type RelayDispatchQueue = Vec<UpwardMessage>
type Watermarks = Option<BlockNumber>;
type DidUpdate = Option<Vec<ParaId>>;
type Result = [
ParaId[],
[
Active,
RetryQueue,
SelectedThreads,
DidUpdate
],
ParaInfoResult[],
PendingSwap[],
Heads[],
RelayDispatchQueue[],
Watermarks[],
];
function parseActive (id: ParaId, active: Active): DeriveParachainActive | null {
const found = active.find(([paraId]): boolean => paraId === id);
if (found && found[1].isSome) {
const [collatorId, retriable] = found[1].unwrap();
return {
collatorId,
...(
retriable.isWithRetries
? {
isRetriable: true,
retries: retriable.asWithRetries.toNumber()
}
: {
isRetriable: false,
retries: 0
}
)
};
}
return null;
}
function parseCollators (id: ParaId, collatorQueue: SelectedThreads | RetryQueue): (CollatorId | null)[] {
return collatorQueue
.map((queue): CollatorId | null => {
const found = queue.find(([paraId]): boolean => paraId === id);
return found ? found[1] : null;
});
}
function parse ([ids, [active, retryQueue, selectedThreads, didUpdate], infos, pendingSwaps, heads, relayDispatchQueues, watermarks]: Result): DeriveParachain[] {
return ids.map((id, index): DeriveParachain => {
return {
active: parseActive(id, active),
didUpdate: didUpdate.isSome
? didUpdate.unwrap().some((paraId): boolean => paraId === id)
: false,
heads: heads[index],
id,
info: infos[index].unwrapOr(null),
pendingSwapId: pendingSwaps[index].unwrapOr(null),
relayDispatchQueue: relayDispatchQueues[index],
retryCollators: parseCollators(id, retryQueue),
selectedCollators: parseCollators(id, selectedThreads),
watermark: watermarks[index].unwrapOr(null)
};
});
}
export function all (api: ApiInterfaceRx): () => Observable<DeriveParachain[]> {
return memo((): Observable<DeriveParachain[]> =>
api.query.registrar?.parachains && api.query.parachains
? api.query.registrar.parachains<ParaIds>().pipe(
switchMap((paraIds) =>
combineLatest([
of(paraIds),
api.queryMulti([
api.query.registrar.active,
api.query.registrar.retryQueue,
api.query.registrar.selectedThreads,
api.query.parachains.didUpdate
]),
api.query.registrar.paras.multi<ParaInfo>(paraIds),
api.query.registrar.pendingSwap.multi<PendingSwap>(paraIds),
api.query.parachains.heads.multi<Heads>(paraIds),
api.query.parachains.relayDispatchQueue.multi<RelayDispatchQueue>(paraIds),
api.query.parachains.watermarks.multi<Watermarks>(paraIds)
]) as Observable<Result>
),
map((result: Result): DeriveParachain[] =>
parse(result)
)
)
: of([] as DeriveParachain[])
);
}
+2 -1
View File
@@ -2,4 +2,5 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export * from './all';
export * from './info';
export * from './overview';
@@ -0,0 +1,99 @@
// Copyright 2017-2020 @polkadot/api-derive 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 { CollatorId, ParaId } from '@polkadot/types/interfaces';
import { DeriveParachainInfo, DeriveParachainFull, DeriveParachainActive } from '../types';
import { Active, DidUpdate, Heads, ParaInfoResult, PendingSwap, RelayDispatchQueue, RetryQueue, SelectedThreads, Watermarks } from './types';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { createType } from '@polkadot/types';
import { memo } from '../util';
type Result = [
Active,
RetryQueue,
SelectedThreads,
DidUpdate,
ParaInfoResult,
PendingSwap,
Heads,
RelayDispatchQueue,
Watermarks
];
function parseActive (id: ParaId, active: Active): DeriveParachainActive | null {
const found = active.find(([paraId]): boolean => paraId === id);
if (found && found[1].isSome) {
const [collatorId, retriable] = found[1].unwrap();
return {
collatorId,
...(
retriable.isWithRetries
? {
isRetriable: true,
retries: retriable.asWithRetries.toNumber()
}
: {
isRetriable: false,
retries: 0
}
)
};
}
return null;
}
function parseCollators (id: ParaId, collatorQueue: SelectedThreads | RetryQueue): (CollatorId | null)[] {
return collatorQueue
.map((queue): CollatorId | null => {
const found = queue.find(([paraId]): boolean => paraId === id);
return found ? found[1] : null;
});
}
function parse (id: ParaId, [active, retryQueue, selectedThreads, didUpdate, info, pendingSwap, heads, relayDispatchQueue, watermarks]: Result): DeriveParachainFull {
return {
active: parseActive(id, active),
didUpdate: didUpdate.isSome
? !!didUpdate.unwrap().some((paraId): boolean => paraId.eq(id))
: false,
heads,
id,
info: { id, ...info.unwrapOr(null) } as DeriveParachainInfo,
pendingSwapId: pendingSwap.unwrapOr(null),
relayDispatchQueue,
retryCollators: parseCollators(id, retryQueue),
selectedCollators: parseCollators(id, selectedThreads),
watermark: watermarks.unwrapOr(null)
};
}
export function info (api: ApiInterfaceRx): (id: ParaId | number) => Observable<DeriveParachainFull | null> {
return memo((id: ParaId | number): Observable<DeriveParachainFull | null> =>
api.query.registrar && api.query.parachains
? api.queryMulti<Result>([
api.query.registrar.active,
api.query.registrar.retryQueue,
api.query.registrar.selectedThreads,
api.query.parachains.didUpdate,
[api.query.registrar.paras, id],
[api.query.registrar.pendingSwap, id],
[api.query.parachains.heads, id],
[api.query.parachains.relayDispatchQueue, id],
[api.query.parachains.watermarks, id]
])
.pipe(
map((result: Result): DeriveParachainFull =>
parse(createType(api.registry, 'ParaId', id), result)
)
)
: of(null)
);
}
@@ -0,0 +1,59 @@
// Copyright 2017-2020 @polkadot/api-derive 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 { ParaId } from '@polkadot/types/interfaces';
import { DeriveParachain, DeriveParachainInfo } from '../types';
import { DidUpdate, ParaInfoResult, PendingSwap, RelayDispatchQueueSize, Watermarks } from './types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { memo } from '../util';
type Result = [
ParaId[],
DidUpdate,
ParaInfoResult[],
PendingSwap[],
RelayDispatchQueueSize[],
Watermarks[],
];
function parse ([ids, didUpdate, infos, pendingSwaps, relayDispatchQueueSizes, watermarks]: Result): DeriveParachain[] {
return ids.map((id, index): DeriveParachain => {
return {
didUpdate: didUpdate.isSome
? !!didUpdate.unwrap().some((paraId): boolean => paraId.eq(id))
: false,
id,
info: { id, ...infos[index].unwrapOr(null) } as DeriveParachainInfo,
pendingSwapId: pendingSwaps[index].unwrapOr(null),
relayDispatchQueueSize: relayDispatchQueueSizes[index][0].toNumber(),
watermark: watermarks[index].unwrapOr(null)
};
});
}
export function overview (api: ApiInterfaceRx): () => Observable<DeriveParachain[]> {
return memo((): Observable<DeriveParachain[]> =>
api.query.registrar?.parachains && api.query.parachains
? api.query.registrar.parachains<ParaId[]>().pipe(
switchMap((paraIds) =>
combineLatest([
of(paraIds),
api.query.parachains.didUpdate<DidUpdate>(),
api.query.registrar.paras.multi<ParaInfoResult>(paraIds),
api.query.registrar.pendingSwap.multi<PendingSwap>(paraIds),
api.query.parachains.relayDispatchQueueSize.multi<RelayDispatchQueueSize>(paraIds),
api.query.parachains.watermarks.multi<Watermarks>(paraIds)
])
),
map((result: Result): DeriveParachain[] =>
parse(result)
)
)
: of([] as DeriveParachain[])
);
}
@@ -0,0 +1,20 @@
// Copyright 2017-2020 @polkadot/api-derive 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 { BlockNumber, CollatorId, ParaId, ParaInfo, Retriable, UpwardMessage } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { Bytes, Option, u32, Vec } from '@polkadot/types';
export type ParaInfoResult = Option<ParaInfo>;
export type PendingSwap = Option<ParaId>;
export type Active = Vec<ITuple<[ParaId, Option<ITuple<[CollatorId, Retriable]>>]>>;
export type RetryQueue = Vec<Vec<ITuple<[ParaId, CollatorId]>>>
export type SelectedThreads = Vec<Vec<ITuple<[ParaId, CollatorId]>>>
export type Code = Bytes;
export type Heads = Bytes;
export type RelayDispatchQueue = Vec<UpwardMessage>
export type RelayDispatchQueueSize = ITuple<[u32, u32]>;
export type Watermarks = Option<BlockNumber>;
export type DidUpdate = Option<Vec<ParaId>>;
+15 -4
View File
@@ -114,17 +114,28 @@ export interface DeriveParachainActive {
retries: number;
}
export interface DeriveParachainInfo extends ParaInfo {
id: ParaId;
icon?: string;
name?: string;
owner?: string;
}
export interface DeriveParachain {
active: DeriveParachainActive | null;
didUpdate: boolean;
pendingSwapId: ParaId | null;
heads: Bytes | null;
id: ParaId;
info: ParaInfo | null;
info: DeriveParachainInfo | null;
relayDispatchQueueSize?: number;
watermark: BlockNumber | null;
}
export interface DeriveParachainFull extends DeriveParachain {
active: DeriveParachainActive | null;
heads: Bytes | null;
relayDispatchQueue: UpwardMessage[];
retryCollators: (CollatorId | null)[];
selectedCollators: (CollatorId | null)[];
watermark: BlockNumber | null;
}
export interface DeriveProposalPreImage {