Skip to content

Commit

Permalink
partial removal of typecasting
Browse files Browse the repository at this point in the history
  • Loading branch information
filvecchiato committed Dec 23, 2024
1 parent 8c3a776 commit 4a5b115
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/services/coretime/CoretimeService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { ApiPromise } from '@polkadot/api';
import { Hash } from '@polkadot/types/interfaces';
import type { ApiPromise } from '@polkadot/api';
import type { Hash } from '@polkadot/types/interfaces';

import { kusamaCoretimeMetadata } from '../../test-helpers/metadata/coretimeKusamaMetadata';
import { kusamaMetadataV1003003 } from '../../test-helpers/metadata/kusamaMetadataV1003003';
Expand Down
60 changes: 27 additions & 33 deletions src/services/coretime/CoretimeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { ApiDecoration, QueryableModuleStorage } from '@polkadot/api/types';
import { Option, StorageKey, U16, U32, Vec } from '@polkadot/types';
import { BlockHash, ParaId } from '@polkadot/types/interfaces';
import {
PalletBrokerConfigRecord,
PalletBrokerLeaseRecordItem,
PalletBrokerPotentialRenewalId,
PalletBrokerPotentialRenewalRecord,
PalletBrokerRegionId,
PalletBrokerRegionRecord,
PalletBrokerSaleInfoRecord,
PalletBrokerScheduleItem,
PalletBrokerStatusRecord,
import type { ApiDecoration, QueryableModuleStorage } from '@polkadot/api/types';
import type { Option, StorageKey, U32 } from '@polkadot/types';
import type { BlockHash, ParaId } from '@polkadot/types/interfaces';
import type {
// PalletBrokerConfigRecord,
// PalletBrokerLeaseRecordItem,
// PalletBrokerPotentialRenewalId,
// PalletBrokerPotentialRenewalRecord,
// PalletBrokerRegionId,
// PalletBrokerRegionRecord,
// PalletBrokerSaleInfoRecord,
// PalletBrokerScheduleItem,
// PalletBrokerStatusRecord,
PolkadotRuntimeParachainsAssignerCoretimeCoreDescriptor,
PolkadotRuntimeParachainsParasParaLifecycle,
} from '@polkadot/types/lookup';
import { BN } from '@polkadot/util';

import {
import type {
ICoretimeChainInfo,
ICoretimeCores,
ICoretimeLeases,
Expand Down Expand Up @@ -82,9 +82,7 @@ const SCALE = new BN(10000);
export class CoretimeService extends AbstractService {
private getAndDecodeRegions = async (api: ApiDecoration<'promise'>): Promise<TRegionInfo[]> => {
const regions = await api.query.broker.regions.entries();
const regs = regions as unknown as [StorageKey<[PalletBrokerRegionId]>, Option<PalletBrokerRegionRecord>][];

const regionsInfo = regs.map((region) => {
const regionsInfo = regions.map((region) => {
return extractRegionInfo([region[0], region[1]]);
});

Expand All @@ -93,26 +91,25 @@ export class CoretimeService extends AbstractService {

private getAndDecodeLeases = async (api: ApiDecoration<'promise'>): Promise<TLeaseInfo[]> => {
const leases = await api.query.broker.leases();
return (leases as unknown as Vec<PalletBrokerLeaseRecordItem>).map((lease) => extractLeaseInfo(lease));
return leases.map((lease) => extractLeaseInfo(lease));
};

private getAndDecodeWorkload = async (api: ApiDecoration<'promise'>): Promise<TWorkloadInfo[]> => {
const workloads = await api.query.broker.workload.entries();

const wls = workloads as unknown as [StorageKey<[U32]>, Vec<PalletBrokerScheduleItem>][];
return sortByCore(
wls.map((workload) => {
workloads.map((workload) => {
return extractWorkloadInfo(workload[1], workload[0].args[0].toNumber());
}),
);
};

private getAndDecodeWorkplan = async (api: ApiDecoration<'promise'>): Promise<TWorkplanInfo[]> => {
const workplans = await api.query.broker.workplan.entries();
const wpls = workplans as unknown as [StorageKey<[U32, U16]>, Option<Vec<PalletBrokerScheduleItem>>][];

const wplsInfo = sortByCore(
wpls.map(([key, val]) => {
const [timeslice, core] = key.args[0].toArray();
workplans.map(([key, val]) => {
const [timeslice, core] = key.args[0].map((a) => a.toNumber());
return extractWorkplanInfo(val, core, timeslice);
}),
);
Expand All @@ -121,30 +118,27 @@ export class CoretimeService extends AbstractService {
};

private getAndDecodeSaleInfo = async (api: ApiDecoration<'promise'>): Promise<TSaleInfo | null> => {
const saleInfo = (await api.query.broker.saleInfo()) as unknown as Option<PalletBrokerSaleInfoRecord>;
const saleInfo = await api.query.broker.saleInfo();
return saleInfo.isSome ? extractSaleInfo(saleInfo.unwrap()) : null;
};

private getAndDecodeStatus = async (api: ApiDecoration<'promise'>): Promise<TStatusInfo> => {
const status = await api.query.broker.status();

return extractStatusInfo(status as unknown as Option<PalletBrokerStatusRecord>);
return extractStatusInfo(status);
};

private getAndDecodeConfiguration = async (api: ApiDecoration<'promise'>): Promise<TConfigInfo> => {
const configuration = await api.query.broker.configuration();

return extractConfigInfo(configuration as unknown as Option<PalletBrokerConfigRecord>);
return extractConfigInfo(configuration);
};

private getAndDecodePotentialRenewals = async (api: ApiDecoration<'promise'>): Promise<TPotentialRenewalInfo[]> => {
const potentialRenewals = await api.query.broker.potentialRenewals.entries();
const renewals = potentialRenewals as unknown as [
StorageKey<[PalletBrokerPotentialRenewalId]>,
Option<PalletBrokerPotentialRenewalRecord>,
][];

const potentialRenewalsInfo = sortByCore(
renewals.map((renewal) => extractPotentialRenewalInfo(renewal[1], renewal[0])),
potentialRenewals.map((renewal) => extractPotentialRenewalInfo(renewal[1], renewal[0])),
);

return potentialRenewalsInfo;
Expand All @@ -153,7 +147,7 @@ export class CoretimeService extends AbstractService {
private getAndDecodeReservations = async (api: ApiDecoration<'promise'>): Promise<TReservationInfo[]> => {
const reservations = await api.query.broker.reservations();

return (reservations as unknown as Vec<Vec<PalletBrokerScheduleItem>>).map((res) => extractReservationInfo(res));
return reservations.map((res) => extractReservationInfo(res));
};

private getAndDecodeCoreSchedules = async (api: ApiDecoration<'promise'>): Promise<Record<string, unknown>[]> => {
Expand Down Expand Up @@ -591,7 +585,7 @@ export class CoretimeService extends AbstractService {
}

private getChainType(specName: string): ChainType {
const relay = ['polkadot', 'kusama', 'westend', 'rococo', 'paseo'];
const relay = ['polkadot', 'kusama', 'westend', 'paseo'];
if (relay.includes(specName.toLowerCase())) {
return ChainType.Relay;
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/services/coretime/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
PolkadotRuntimeParachainsAssignerCoretimeWorkState,
PolkadotRuntimeParachainsParasParaLifecycle,
} from '@polkadot/types/lookup';
import { Option, U32, Vec } from '@polkadot/types-codec';
import { Option, Vec } from '@polkadot/types-codec';
import { AnyTuple } from '@polkadot/types-codec/types';
import { BN } from '@polkadot/util';

import {
Expand Down Expand Up @@ -209,7 +210,7 @@ export function extractConfigInfo(info: Option<PalletBrokerConfigRecord>): TConf
}

export function extractCoreDescriptorInfo(
_key: StorageKey<[U32]>,
_key: StorageKey<AnyTuple>,
info: PolkadotRuntimeParachainsAssignerCoretimeCoreDescriptor,
): TCoreDescriptor {
const currentWork: PolkadotRuntimeParachainsAssignerCoretimeWorkState | null = info?.currentWork.isSome
Expand Down

0 comments on commit 4a5b115

Please sign in to comment.