From ee622f3d2be0a41d05935335b0bb52d52825fa39 Mon Sep 17 00:00:00 2001 From: Keith Ingram Date: Sat, 15 Feb 2020 02:52:40 -0500 Subject: [PATCH] Add api.derive.registrar.parachains (#1856) * api.derive.registrar.parachains * remove api parse * suggestions * some * Update packages/api-derive/src/parachains/all.ts * Update packages/api-derive/src/index.ts * Update packages/api-derive/src/index.ts Co-authored-by: Jaco Greeff --- packages/api-derive/src/index.ts | 3 +- packages/api-derive/src/parachains/all.ts | 120 ++++++++++++++++++++ packages/api-derive/src/parachains/index.ts | 5 + packages/api-derive/src/types.ts | 23 +++- 4 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 packages/api-derive/src/parachains/all.ts create mode 100644 packages/api-derive/src/parachains/index.ts diff --git a/packages/api-derive/src/index.ts b/packages/api-derive/src/index.ts index 9347e47787..fc2a70fd25 100644 --- a/packages/api-derive/src/index.ts +++ b/packages/api-derive/src/index.ts @@ -15,6 +15,7 @@ import * as council from './council'; import * as democracy from './democracy'; import * as elections from './elections'; import * as imOnline from './imOnline'; +import * as parachains from './parachains'; import * as session from './session'; import * as society from './society'; import * as staking from './staking'; @@ -23,7 +24,7 @@ import * as treasury from './treasury'; export * from './type'; -export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, session, society, staking, technicalCommittee, treasury }; +export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, parachains, session, society, staking, technicalCommittee, treasury }; type DeriveSection
= { [Method in keyof Section]: Section[Method] extends AnyFunction diff --git a/packages/api-derive/src/parachains/all.ts b/packages/api-derive/src/parachains/all.ts new file mode 100644 index 0000000000..45ac5e0255 --- /dev/null +++ b/packages/api-derive/src/parachains/all.ts @@ -0,0 +1,120 @@ +// 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; +type PendingSwap = Option; +type Active = ITuple<[ParaId, Option>]>[]; +type RetryQueue = ITuple<[ParaId, CollatorId]>[][] +type SelectedThreads = ITuple<[ParaId, CollatorId]>[][]; +type Heads = Bytes; +type RelayDispatchQueue = Vec +type Watermarks = Option; +type DidUpdate = Option>; + +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 { + return memo((): Observable => + api.query.registrar?.parachains && api.query.parachains + ? api.query.registrar.parachains().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(paraIds), + api.query.registrar.pendingSwap.multi(paraIds), + api.query.parachains.heads.multi(paraIds), + api.query.parachains.relayDispatchQueue.multi(paraIds), + api.query.parachains.watermarks.multi(paraIds) + ]) as Observable + ), + map((result: Result): DeriveParachain[] => + parse(result) + ) + ) + : of([] as DeriveParachain[]) + ); +} diff --git a/packages/api-derive/src/parachains/index.ts b/packages/api-derive/src/parachains/index.ts new file mode 100644 index 0000000000..2992bebd69 --- /dev/null +++ b/packages/api-derive/src/parachains/index.ts @@ -0,0 +1,5 @@ +// 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. + +export * from './all'; diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 2b119c1e75..8cc84a6c9c 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -2,10 +2,10 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AccountId, AccountIndex, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidKind, BlockNumber, EraIndex, EraPoints, Exposure, Hash, Index, Keys, Proposal, PropIndex, ProposalIndex, ReferendumInfo, RegistrationJudgement, RewardDestination, SessionIndex, SetIndex, SocietyVote, StakingLedger, StrikeCount, TreasuryProposal, ValidatorPrefs, Vote, Votes, VoteIndex, VouchingStatus } from '@polkadot/types/interfaces'; +import { AccountId, AccountIndex, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidKind, BlockNumber, CollatorId, EraIndex, EraPoints, Exposure, Hash, Index, Keys, ParaId, ParaInfo, Proposal, PropIndex, ProposalIndex, ReferendumInfo, RegistrationJudgement, RewardDestination, SessionIndex, SetIndex, SocietyVote, StakingLedger, StrikeCount, TreasuryProposal, UpwardMessage, ValidatorPrefs, Vote, Votes, VoteIndex, VouchingStatus } from '@polkadot/types/interfaces'; import BN from 'bn.js'; -import { u32, Vec } from '@polkadot/types'; +import { Bytes, u32, Vec } from '@polkadot/types'; export type AccountIndexes = Record; @@ -107,6 +107,25 @@ export interface RecentlyOffline { count: BN; } +export interface DeriveParachainActive { + collatorId: CollatorId; + isRetriable: boolean; + retries: number; +} + +export interface DeriveParachain { + active: DeriveParachainActive | null; + didUpdate: boolean; + pendingSwapId: ParaId | null; + heads: Bytes | null; + id: ParaId; + info: ParaInfo | null; + relayDispatchQueue: UpwardMessage[]; + retryCollators: (CollatorId | null)[]; + selectedCollators: (CollatorId | null)[]; + watermark: BlockNumber | null; +} + export interface DeriveProposalPreImage { at: BlockNumber; balance: Balance;