Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: pallet storage metadataV14 #710

Merged
merged 6 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

58 changes: 50 additions & 8 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,14 @@ paths:
required: true
schema:
type: string
- name: adjustMetadataV13
in: query
description: This gives the option to set historic blocks to use V13 metadata as oppose to V14. There are some
type differences between V14 and V13 when it comes to `StorageEntryType`. Specifally the 'map' key.
In turn this gives the ability to receive both older and newer storage responses while transitioning to V14.
Comment on lines +830 to +832
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description: This gives the option to set historic blocks to use V13 metadata as oppose to V14. There are some
type differences between V14 and V13 when it comes to `StorageEntryType`. Specifally the 'map' key.
In turn this gives the ability to receive both older and newer storage responses while transitioning to V14.
description: Instruct sidecar to return `StorageEntryType` in the V13 metdata 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 @@ -885,6 +893,14 @@ paths:
required: true
schema:
type: string
- name: adjustMetadataV13
in: query
description: This gives the option to set historic blocks to use V13 metadata as oppose to V14. There are some
type differences between V14 and V13 when it comes to `StorageEntryType`. Specifally the 'map' key.
In turn this gives the ability to receive both older and newer storage responses while transitioning to V14.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd adjust along the same lines as above.

required: false
schema:
type: boolean
- name: key1
in: query
description: Key for a map, or first key for a double map. Required for querying
Expand Down Expand Up @@ -1806,6 +1822,8 @@ components:
properties:
mortalEra:
type: array
items:
type: string
description: Tuple of a Phase, and Period. Each item in the array will be a string formatted as an integer.
immortalEra:
type: string
Expand Down Expand Up @@ -2044,14 +2062,9 @@ components:
description: Metadata of a storage item from a FRAME pallet.
PalletStorageType:
type: object
description: Info about the data structure used for storage.
example:
Map:
hasher: "Twox64Concat"
key:
example: "ReferendumIndex"
value: "ReferendumInfo"
linked: false
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add note about the temporary nature of adjustMetadataV13?

Para:
type: object
properties:
Expand Down Expand Up @@ -2430,6 +2443,35 @@ components:
items:
type: string
format: ss58
StorageEntryTypeV13:
type: object
properties:
hasher:
type: string
description: Returns a string deonting the storage hasher.
key:
type: string
description: Key of the queried pallet storageId.
value:
type: string
description: Value of the queried pallet storageId.
linked:
type: boolean
StorageEntryTypeV14:
type: object
properties:
hasher:
type: array
items:
type: string
description: Returns a string denoting the storage
hasher inside of an array.
key:
type: string
description: The SiLookupTypeId to identify the type.
value:
type: string
description: The SiLookupTypeId to identify the type.
TraceEvent:
type: object
properties:
Expand Down
18 changes: 12 additions & 6 deletions src/controllers/pallets/PalletsStorageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,51 @@ export default class PalletsStorageController extends AbstractController<Pallets

private getStorageItem: RequestHandler = async (
{
query: { at, key1, key2, metadata },
query: { at, key1, key2, metadata, adjustMetadataV13 },
params: { palletId, storageItemId },
},
res
): Promise<void> => {
const key1Arg = typeof key1 === 'string' ? key1 : undefined;
const key2Arg = typeof key2 === 'string' ? key2 : undefined;
const metadataArg = metadata === 'true' ? true : false;
const metadataArg = metadata === 'true';
const adjustMetadataV13Arg = adjustMetadataV13 === 'true';

const hash = await this.getHashFromAt(at);
const historicApi = await this.api.at(hash);

PalletsStorageController.sanitizedSend(
res,
await this.service.fetchStorageItem({
await this.service.fetchStorageItem(historicApi, {
hash,
// stringCamelCase ensures we don't have snake case or kebab case
palletId: stringCamelCase(palletId),
storageItemId: stringCamelCase(storageItemId),
key1: key1Arg,
key2: key2Arg,
metadata: metadataArg,
adjustMetadataV13Arg,
})
);
};

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

const hash = await this.getHashFromAt(at);
const historicApi = await this.api.at(hash);

PalletsStorageController.sanitizedSend(
res,
await this.service.fetchStorage({
await this.service.fetchStorage(historicApi, {
hash,
palletId: stringCamelCase(palletId),
onlyIds: onlyIdsArg,
adjustMetadataV13Arg,
})
);
};
Expand Down
31 changes: 22 additions & 9 deletions src/services/pallets/PalletsStorageService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ApiPromise } from '@polkadot/api';
import { ApiDecoration } from '@polkadot/api/types';
import { Hash } from '@polkadot/types/interfaces';

import { sanitizeNumbers } from '../../sanitize';
import { polkadotRegistry } from '../../test-helpers/registries';
Expand All @@ -13,13 +15,18 @@ const referendumInfoOfAt = () =>
polkadotRegistry.createType('ReferendumInfo');
});

const mockApi = {
...defaultMockApi,
const mockHistoricApi = {
registry: polkadotRegistry,
query: {
democracy: {
referendumInfoOf: { at: referendumInfoOfAt },
referendumInfoOf: referendumInfoOfAt,
},
},
} as unknown as ApiDecoration<'promise'>;

const mockApi = {
...defaultMockApi,
at: (_hash: Hash) => mockHistoricApi,
} as unknown as ApiPromise;

/**
Expand All @@ -32,13 +39,14 @@ describe('PalletStorageService', () => {
it('works with a query to a single key storage map', async () => {
expect(
sanitizeNumbers(
await palletsStorageService.fetchStorageItem({
await palletsStorageService.fetchStorageItem(mockHistoricApi, {
hash: blockHash789629,
palletId: 'democracy',
storageItemId: 'referendumInfoOf',
key1: '0',
key2: undefined,
metadata: false,
adjustMetadataV13Arg: true,
})
)
).toMatchObject(fetchStorageItemRes);
Expand All @@ -47,13 +55,14 @@ describe('PalletStorageService', () => {
it('works with a index identifier', async () => {
expect(
sanitizeNumbers(
await palletsStorageService.fetchStorageItem({
await palletsStorageService.fetchStorageItem(mockHistoricApi, {
hash: blockHash789629,
palletId: '15',
storageItemId: 'referendumInfoOf',
key1: '0',
key2: undefined,
metadata: false,
adjustMetadataV13Arg: true,
})
)
).toMatchObject(fetchStorageItemRes);
Expand All @@ -62,13 +71,14 @@ describe('PalletStorageService', () => {
it('appropriately uses metadata params', async () => {
expect(
sanitizeNumbers(
await palletsStorageService.fetchStorageItem({
await palletsStorageService.fetchStorageItem(mockHistoricApi, {
hash: blockHash789629,
palletId: 'democracy',
storageItemId: 'referendumInfoOf',
key1: '0',
key2: undefined,
metadata: true,
adjustMetadataV13Arg: true,
})
)
).toMatchObject(fetchStorageItemRes);
Expand All @@ -79,10 +89,11 @@ describe('PalletStorageService', () => {
it('works with no query params', async () => {
expect(
sanitizeNumbers(
await palletsStorageService.fetchStorage({
await palletsStorageService.fetchStorage(mockHistoricApi, {
hash: blockHash789629,
palletId: 'democracy',
onlyIds: false,
adjustMetadataV13Arg: true,
})
)
).toStrictEqual(fetchStorageRes);
Expand All @@ -91,10 +102,11 @@ describe('PalletStorageService', () => {
it('work with a index identifier', async () => {
expect(
sanitizeNumbers(
await palletsStorageService.fetchStorage({
await palletsStorageService.fetchStorage(mockHistoricApi, {
hash: blockHash789629,
palletId: '15',
onlyIds: false,
adjustMetadataV13Arg: true,
})
)
).toStrictEqual(fetchStorageRes);
Expand All @@ -103,10 +115,11 @@ describe('PalletStorageService', () => {
it('only list storage item ids when onlyIds is true', async () => {
expect(
sanitizeNumbers(
await palletsStorageService.fetchStorage({
await palletsStorageService.fetchStorage(mockHistoricApi, {
hash: blockHash789629,
palletId: 'democracy',
onlyIds: true,
adjustMetadataV13Arg: true,
})
)
).toStrictEqual(fetchStorageIdsOnlyRes);
Expand Down
Loading