Skip to content

Commit 1a50298

Browse files
authored
Split parachains.derive calls (#1948)
* api.derive.registrar.parachains * remove api parse * suggestions * some * parachains: overview/info * rm all * rm all * refine
1 parent 68f2cb5 commit 1a50298

File tree

7 files changed

+196
-125
lines changed

7 files changed

+196
-125
lines changed

packages/api-derive/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const deriveAvail: Record<string, string[]> = {
4646
democracy: ['democracy'],
4747
elections: ['electionsPhragmen', 'elections'],
4848
imOnline: ['imOnline'],
49+
parachains: ['parachains', 'registrar'],
4950
session: ['session'],
5051
society: ['society'],
5152
staking: ['staking'],

packages/api-derive/src/parachains/all.ts

Lines changed: 0 additions & 120 deletions
This file was deleted.

packages/api-derive/src/parachains/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
// This software may be modified and distributed under the terms
33
// of the Apache-2.0 license. See the LICENSE file for details.
44

5-
export * from './all';
5+
export * from './info';
6+
export * from './overview';
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
2+
// This software may be modified and distributed under the terms
3+
// of the Apache-2.0 license. See the LICENSE file for details.
4+
5+
import { CollatorId, ParaId } from '@polkadot/types/interfaces';
6+
import { DeriveParachainInfo, DeriveParachainFull, DeriveParachainActive } from '../types';
7+
import { Active, DidUpdate, Heads, ParaInfoResult, PendingSwap, RelayDispatchQueue, RetryQueue, SelectedThreads, Watermarks } from './types';
8+
9+
import { Observable, of } from 'rxjs';
10+
import { map } from 'rxjs/operators';
11+
import { ApiInterfaceRx } from '@polkadot/api/types';
12+
import { createType } from '@polkadot/types';
13+
14+
import { memo } from '../util';
15+
16+
type Result = [
17+
Active,
18+
RetryQueue,
19+
SelectedThreads,
20+
DidUpdate,
21+
ParaInfoResult,
22+
PendingSwap,
23+
Heads,
24+
RelayDispatchQueue,
25+
Watermarks
26+
];
27+
28+
function parseActive (id: ParaId, active: Active): DeriveParachainActive | null {
29+
const found = active.find(([paraId]): boolean => paraId === id);
30+
31+
if (found && found[1].isSome) {
32+
const [collatorId, retriable] = found[1].unwrap();
33+
34+
return {
35+
collatorId,
36+
...(
37+
retriable.isWithRetries
38+
? {
39+
isRetriable: true,
40+
retries: retriable.asWithRetries.toNumber()
41+
}
42+
: {
43+
isRetriable: false,
44+
retries: 0
45+
}
46+
)
47+
};
48+
}
49+
return null;
50+
}
51+
52+
function parseCollators (id: ParaId, collatorQueue: SelectedThreads | RetryQueue): (CollatorId | null)[] {
53+
return collatorQueue
54+
.map((queue): CollatorId | null => {
55+
const found = queue.find(([paraId]): boolean => paraId === id);
56+
57+
return found ? found[1] : null;
58+
});
59+
}
60+
61+
function parse (id: ParaId, [active, retryQueue, selectedThreads, didUpdate, info, pendingSwap, heads, relayDispatchQueue, watermarks]: Result): DeriveParachainFull {
62+
return {
63+
active: parseActive(id, active),
64+
didUpdate: didUpdate.isSome
65+
? !!didUpdate.unwrap().some((paraId): boolean => paraId.eq(id))
66+
: false,
67+
heads,
68+
id,
69+
info: { id, ...info.unwrapOr(null) } as DeriveParachainInfo,
70+
pendingSwapId: pendingSwap.unwrapOr(null),
71+
relayDispatchQueue,
72+
retryCollators: parseCollators(id, retryQueue),
73+
selectedCollators: parseCollators(id, selectedThreads),
74+
watermark: watermarks.unwrapOr(null)
75+
};
76+
}
77+
78+
export function info (api: ApiInterfaceRx): (id: ParaId | number) => Observable<DeriveParachainFull | null> {
79+
return memo((id: ParaId | number): Observable<DeriveParachainFull | null> =>
80+
api.query.registrar && api.query.parachains
81+
? api.queryMulti<Result>([
82+
api.query.registrar.active,
83+
api.query.registrar.retryQueue,
84+
api.query.registrar.selectedThreads,
85+
api.query.parachains.didUpdate,
86+
[api.query.registrar.paras, id],
87+
[api.query.registrar.pendingSwap, id],
88+
[api.query.parachains.heads, id],
89+
[api.query.parachains.relayDispatchQueue, id],
90+
[api.query.parachains.watermarks, id]
91+
])
92+
.pipe(
93+
map((result: Result): DeriveParachainFull =>
94+
parse(createType(api.registry, 'ParaId', id), result)
95+
)
96+
)
97+
: of(null)
98+
);
99+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
2+
// This software may be modified and distributed under the terms
3+
// of the Apache-2.0 license. See the LICENSE file for details.
4+
5+
import { ParaId } from '@polkadot/types/interfaces';
6+
import { DeriveParachain, DeriveParachainInfo } from '../types';
7+
import { DidUpdate, ParaInfoResult, PendingSwap, RelayDispatchQueueSize, Watermarks } from './types';
8+
9+
import { Observable, combineLatest, of } from 'rxjs';
10+
import { map, switchMap } from 'rxjs/operators';
11+
import { ApiInterfaceRx } from '@polkadot/api/types';
12+
13+
import { memo } from '../util';
14+
15+
type Result = [
16+
ParaId[],
17+
DidUpdate,
18+
ParaInfoResult[],
19+
PendingSwap[],
20+
RelayDispatchQueueSize[],
21+
Watermarks[],
22+
];
23+
24+
function parse ([ids, didUpdate, infos, pendingSwaps, relayDispatchQueueSizes, watermarks]: Result): DeriveParachain[] {
25+
return ids.map((id, index): DeriveParachain => {
26+
return {
27+
didUpdate: didUpdate.isSome
28+
? !!didUpdate.unwrap().some((paraId): boolean => paraId.eq(id))
29+
: false,
30+
id,
31+
info: { id, ...infos[index].unwrapOr(null) } as DeriveParachainInfo,
32+
pendingSwapId: pendingSwaps[index].unwrapOr(null),
33+
relayDispatchQueueSize: relayDispatchQueueSizes[index][0].toNumber(),
34+
watermark: watermarks[index].unwrapOr(null)
35+
};
36+
});
37+
}
38+
39+
export function overview (api: ApiInterfaceRx): () => Observable<DeriveParachain[]> {
40+
return memo((): Observable<DeriveParachain[]> =>
41+
api.query.registrar?.parachains && api.query.parachains
42+
? api.query.registrar.parachains<ParaId[]>().pipe(
43+
switchMap((paraIds) =>
44+
combineLatest([
45+
of(paraIds),
46+
api.query.parachains.didUpdate<DidUpdate>(),
47+
api.query.registrar.paras.multi<ParaInfoResult>(paraIds),
48+
api.query.registrar.pendingSwap.multi<PendingSwap>(paraIds),
49+
api.query.parachains.relayDispatchQueueSize.multi<RelayDispatchQueueSize>(paraIds),
50+
api.query.parachains.watermarks.multi<Watermarks>(paraIds)
51+
])
52+
),
53+
map((result: Result): DeriveParachain[] =>
54+
parse(result)
55+
)
56+
)
57+
: of([] as DeriveParachain[])
58+
);
59+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
2+
// This software may be modified and distributed under the terms
3+
// of the Apache-2.0 license. See the LICENSE file for details.
4+
5+
import { BlockNumber, CollatorId, ParaId, ParaInfo, Retriable, UpwardMessage } from '@polkadot/types/interfaces';
6+
import { ITuple } from '@polkadot/types/types';
7+
8+
import { Bytes, Option, u32, Vec } from '@polkadot/types';
9+
10+
export type ParaInfoResult = Option<ParaInfo>;
11+
export type PendingSwap = Option<ParaId>;
12+
export type Active = Vec<ITuple<[ParaId, Option<ITuple<[CollatorId, Retriable]>>]>>;
13+
export type RetryQueue = Vec<Vec<ITuple<[ParaId, CollatorId]>>>
14+
export type SelectedThreads = Vec<Vec<ITuple<[ParaId, CollatorId]>>>
15+
export type Code = Bytes;
16+
export type Heads = Bytes;
17+
export type RelayDispatchQueue = Vec<UpwardMessage>
18+
export type RelayDispatchQueueSize = ITuple<[u32, u32]>;
19+
export type Watermarks = Option<BlockNumber>;
20+
export type DidUpdate = Option<Vec<ParaId>>;

packages/api-derive/src/types.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,28 @@ export interface DeriveParachainActive {
114114
retries: number;
115115
}
116116

117+
export interface DeriveParachainInfo extends ParaInfo {
118+
id: ParaId;
119+
icon?: string;
120+
name?: string;
121+
owner?: string;
122+
}
123+
117124
export interface DeriveParachain {
118-
active: DeriveParachainActive | null;
119125
didUpdate: boolean;
120126
pendingSwapId: ParaId | null;
121-
heads: Bytes | null;
122127
id: ParaId;
123-
info: ParaInfo | null;
128+
info: DeriveParachainInfo | null;
129+
relayDispatchQueueSize?: number;
130+
watermark: BlockNumber | null;
131+
}
132+
133+
export interface DeriveParachainFull extends DeriveParachain {
134+
active: DeriveParachainActive | null;
135+
heads: Bytes | null;
124136
relayDispatchQueue: UpwardMessage[];
125137
retryCollators: (CollatorId | null)[];
126138
selectedCollators: (CollatorId | null)[];
127-
watermark: BlockNumber | null;
128139
}
129140

130141
export interface DeriveProposalPreImage {

0 commit comments

Comments
 (0)