Skip to content

Commit

Permalink
fix!: removes metadata v13 (#1272)
Browse files Browse the repository at this point in the history
* remove metadata v13

* remove adjustedMetadataV13 references in docs

* update storage metadata docs
  • Loading branch information
marshacb committed May 5, 2023
1 parent f430b79 commit 287d8e3
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 211 deletions.
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

21 changes: 1 addition & 20 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1549,14 +1549,6 @@ paths:
required: true
schema:
type: string
- name: adjustMetadataV13
in: query
description: Instruct sidecar to return `StorageEntryType` in the V13 metadata
format rather than V14. This is a **temporary** flag to allow existing systems to migrate.
It will be deprecated and then removed in the future.
required: false
schema:
type: boolean
- name: onlyIds
in: query
description: Only return the names (IDs) of the storage items instead of all of each storage
Expand Down Expand Up @@ -1617,14 +1609,6 @@ paths:
required: true
schema:
type: string
- name: adjustMetadataV13
in: query
description: Instruct sidecar to return `StorageEntryType` in the V13 metadata
format rather than V14. This is a **temporary** flag to allow existing systems to migrate.
It will be deprecated and then removed in the future.
required: false
schema:
type: boolean
- name: keys
in: query
description: Set of N keys used for querying a storage map. It should be queried using the
Expand Down Expand Up @@ -3194,10 +3178,7 @@ components:
description: Metadata of a storage item from a FRAME pallet.
PalletStorageType:
type: object
description: If the query parameter 'adjustMetadataV13' is set to true, all historic blocks that are
pre v9110 will have the return type `StorageEntryTypeV13`, and all present and post v9110 blocks will
have a return type of `StorageEntryTypeV14`. Please check those types to see potential responses. This
will be deprecated and removed in the future, and will only live as `StorageEntryTypeV14`.
description: This is going to be formatted to the type of StorageEntryTypeV14.
Para:
type: object
properties:
Expand Down
21 changes: 2 additions & 19 deletions src/controllers/pallets/PalletsStorageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import { ApiPromise } from '@polkadot/api';
import { stringCamelCase } from '@polkadot/util';
import { RequestHandler } from 'express-serve-static-core';

import { validateBoolean } from '../..//middleware';
import { Log } from '../../logging/Log';
import { PalletsStorageService } from '../../services';
import {
IPalletsStorageParam,
Expand All @@ -41,17 +39,13 @@ import AbstractController from '../AbstractController';
* See `docs/src/openapi-v1.yaml` for usage information.
*/
export default class PalletsStorageController extends AbstractController<PalletsStorageService> {
private readonly deprecationMsg: string;
constructor(api: ApiPromise) {
super(api, '/pallets/:palletId/storage', new PalletsStorageService(api));

this.initRoutes();
this.deprecationMsg =
'The adjustMetadataV13 query parameter has been deprecated. It will still be available to use for historic blocks.';
}

protected initRoutes(): void {
this.router.use(this.path, validateBoolean(['adjustMetadataV13']));
this.safeMountAsyncGetHandlers([
['/:storageItemId', this.getStorageItem as RequestHandler],
['/', this.getStorage],
Expand All @@ -64,17 +58,11 @@ export default class PalletsStorageController extends AbstractController<Pallets
unknown,
IPalletsStorageQueryParam
> = async (
{
query: { at, keys, metadata, adjustMetadataV13 },
params: { palletId, storageItemId },
},
{ query: { at, keys, metadata }, params: { palletId, storageItemId } },
res
): Promise<void> => {
const parsedKeys = Array.isArray(keys) ? keys : [];
const metadataArg = metadata === 'true';
const adjustMetadataV13Arg = adjustMetadataV13 === 'true';

adjustMetadataV13Arg && Log.logger.warn(this.deprecationMsg);

const hash = await this.getHashFromAt(at);
const historicApi = await this.api.at(hash);
Expand All @@ -88,19 +76,15 @@ export default class PalletsStorageController extends AbstractController<Pallets
storageItemId: stringCamelCase(storageItemId),
keys: parsedKeys,
metadata: metadataArg,
adjustMetadataV13Arg,
})
);
};

private getStorage: RequestHandler = async (
{ params: { palletId }, query: { at, onlyIds, adjustMetadataV13 } },
{ params: { palletId }, query: { at, onlyIds } },
res
): Promise<void> => {
const onlyIdsArg = onlyIds === 'true';
const adjustMetadataV13Arg = adjustMetadataV13 === 'true';

adjustMetadataV13Arg && Log.logger.warn(this.deprecationMsg);

const hash = await this.getHashFromAt(at);
const historicApi = await this.api.at(hash);
Expand All @@ -111,7 +95,6 @@ export default class PalletsStorageController extends AbstractController<Pallets
hash,
palletId: stringCamelCase(palletId),
onlyIds: onlyIdsArg,
adjustMetadataV13Arg,
})
);
};
Expand Down
73 changes: 23 additions & 50 deletions src/services/AbstractPalletsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ import {
ErrorMetadataLatest,
EventMetadataLatest,
FunctionMetadataLatest,
MetadataV13,
MetadataV14,
ModuleMetadataV13,
PalletCallMetadataV14,
PalletConstantMetadataLatest,
PalletConstantMetadataV14,
PalletErrorMetadataV14,
PalletEventMetadataV14,
PalletMetadataV14,
PalletStorageMetadataV14,
StorageEntryMetadataV13,
StorageEntryMetadataV14,
StorageMetadataV13,
} from '@polkadot/types/interfaces';
import { stringCamelCase } from '@polkadot/util';
import { BadRequest } from 'http-errors';
Expand All @@ -41,7 +37,6 @@ import { InternalServerError } from 'http-errors';
import { AbstractService } from './AbstractService';

type IPalletMetadata =
| Option<StorageMetadataV13>
| Option<PalletStorageMetadataV14>
| Option<PalletCallMetadataV14>
| Option<PalletErrorMetadataV14>
Expand All @@ -53,7 +48,6 @@ type IPalletFieldMeta =
| EventMetadataLatest
| FunctionMetadataLatest
| PalletConstantMetadataLatest
| StorageEntryMetadataV13
| StorageEntryMetadataV14;

type IMetadataFieldType =
Expand All @@ -65,17 +59,10 @@ type IMetadataFieldType =

export abstract class AbstractPalletsService extends AbstractService {
private getPalletMetadataType(
meta: ModuleMetadataV13 | PalletMetadataV14,
meta: PalletMetadataV14,
metadataFieldType: IMetadataFieldType
): IPalletMetadata {
if (
metadataFieldType === 'storage' &&
meta.toRawType().includes('MetadataV13')
) {
return this.getProperty(meta as ModuleMetadataV13, metadataFieldType);
} else {
return this.getProperty(meta as PalletMetadataV14, metadataFieldType);
}
return this.getProperty(meta, metadataFieldType);
}

private getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
Expand All @@ -88,38 +75,24 @@ export abstract class AbstractPalletsService extends AbstractService {
* @param palletId identifier for a FRAME pallet as a pallet name or index.
*/
protected findPalletMeta(
adjustedMetadata: MetadataV13 | MetadataV14,
adjustedMetadata: MetadataV14,
palletId: string,
metadataFieldType: IMetadataFieldType
): [PalletMetadataV14 | ModuleMetadataV13, number] {
const metadataType = adjustedMetadata.toRawType();

let pallets: Vec<PalletMetadataV14> | Vec<ModuleMetadataV13>;
let filtered: PalletMetadataV14[] | ModuleMetadataV13[];
if (metadataType.includes('MetadataV13')) {
pallets = adjustedMetadata['modules'] as Vec<ModuleMetadataV13>;
const palletMetaType = this.getPalletMetadataType(
pallets[0],
metadataFieldType
);
filtered = pallets.filter(
(mod) => !(mod[metadataFieldType] as typeof palletMetaType).isEmpty
);
} else {
pallets = adjustedMetadata['pallets'] as Vec<PalletMetadataV14>;
const palletMetaType = this.getPalletMetadataType(
pallets[0],
metadataFieldType
);
filtered = pallets.filter(
(mod) => !(mod[metadataFieldType] as typeof palletMetaType).isEmpty
);
}
): [PalletMetadataV14, number] {
const pallets: Vec<PalletMetadataV14> = adjustedMetadata['pallets'];
const palletMetaType = this.getPalletMetadataType(
pallets[0],
metadataFieldType
);

const filtered: PalletMetadataV14[] = pallets.filter(
(mod) => !(mod[metadataFieldType] as typeof palletMetaType).isEmpty
);

const { isValidPalletName, isValidPalletIndex, parsedPalletId } =
this.validPalletId(pallets, palletId);

let palletMeta: PalletMetadataV14 | ModuleMetadataV13 | undefined;
let palletMeta: PalletMetadataV14 | undefined;
let palletIdx: number | undefined;

if (isValidPalletIndex) {
Expand Down Expand Up @@ -169,7 +142,7 @@ export abstract class AbstractPalletsService extends AbstractService {
}

private validPalletId(
modules: Vec<PalletMetadataV14> | Vec<ModuleMetadataV13>,
modules: Vec<PalletMetadataV14>,
palletId: string
): {
isValidPalletName: boolean;
Expand Down Expand Up @@ -227,7 +200,7 @@ export abstract class AbstractPalletsService extends AbstractService {
*/
protected findPalletFieldItemMeta(
historicApi: ApiDecoration<'promise'>,
palletMeta: PalletMetadataV14 | ModuleMetadataV13,
palletMeta: PalletMetadataV14,
palletItemId: string,
metadataFieldType: IMetadataFieldType
): IPalletFieldMeta {
Expand All @@ -243,26 +216,26 @@ export abstract class AbstractPalletsService extends AbstractService {
} else if (metadataFieldType === 'errors') {
[palletItemIdx, palletItemMeta] = this.getErrorItemMeta(
historicApi,
palletMeta as PalletMetadataV14,
palletMeta,
palletItemIdx,
palletItemId
);
} else if (metadataFieldType === 'events') {
[palletItemIdx, palletItemMeta] = this.getEventItemMeta(
historicApi,
palletMeta as PalletMetadataV14,
palletMeta,
palletItemIdx,
palletItemId
);
} else if (metadataFieldType === 'calls') {
[palletItemIdx, palletItemMeta] = this.getDispatchablesItemMeta(
palletMeta as PalletMetadataV14,
palletMeta,
palletItemIdx,
palletItemId
);
} else {
[palletItemIdx, palletItemMeta] = this.getConstItemMeta(
palletMeta as PalletMetadataV14,
palletMeta,
palletItemIdx,
palletItemId
);
Expand Down Expand Up @@ -311,7 +284,7 @@ export abstract class AbstractPalletsService extends AbstractService {
}

private getConstItemMeta(
palletMeta: PalletMetadataV14 | ModuleMetadataV13,
palletMeta: PalletMetadataV14,
constItemMetaIdx: number,
constItemId: string
): [number, PalletConstantMetadataLatest] {
Expand Down Expand Up @@ -392,10 +365,10 @@ export abstract class AbstractPalletsService extends AbstractService {
}

private getStorageItemMeta(
palletMeta: PalletMetadataV14 | ModuleMetadataV13,
palletMeta: PalletMetadataV14,
storageItemMetaIdx: number,
storageItemId: string
): [number, StorageEntryMetadataV13 | StorageEntryMetadataV14] {
): [number, StorageEntryMetadataV14] {
if (palletMeta.storage.isNone) {
throw new InternalServerError(
`No storage items found in ${palletMeta.name.toString()}'s metadata`
Expand Down
6 changes: 0 additions & 6 deletions src/services/pallets/PalletsStorageService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ describe('PalletStorageService', () => {
storageItemId: 'referendumInfoOf',
keys: ['0'],
metadata: false,
adjustMetadataV13Arg: true,
})
)
).toMatchObject(fetchStorageItemRes);
Expand All @@ -76,7 +75,6 @@ describe('PalletStorageService', () => {
storageItemId: 'referendumInfoOf',
keys: ['0'],
metadata: false,
adjustMetadataV13Arg: true,
})
)
).toMatchObject(fetchStorageItemRes);
Expand All @@ -91,7 +89,6 @@ describe('PalletStorageService', () => {
storageItemId: 'referendumInfoOf',
keys: ['0'],
metadata: true,
adjustMetadataV13Arg: true,
})
)
).toMatchObject(fetchStorageItemRes);
Expand All @@ -106,7 +103,6 @@ describe('PalletStorageService', () => {
hash: blockHash789629,
palletId: 'democracy',
onlyIds: false,
adjustMetadataV13Arg: true,
})
)
).toStrictEqual(fetchStorageRes);
Expand All @@ -119,7 +115,6 @@ describe('PalletStorageService', () => {
hash: blockHash789629,
palletId: '15',
onlyIds: false,
adjustMetadataV13Arg: true,
})
)
).toStrictEqual(fetchStorageRes);
Expand All @@ -132,7 +127,6 @@ describe('PalletStorageService', () => {
hash: blockHash789629,
palletId: 'democracy',
onlyIds: true,
adjustMetadataV13Arg: true,
})
)
).toStrictEqual(fetchStorageIdsOnlyRes);
Expand Down
Loading

0 comments on commit 287d8e3

Please sign in to comment.