Skip to content

Start supporting double map #843

Merged
amaury1093 merged 2 commits intopolkadot-js:masterfrom
xlc:double-map
May 13, 2019
Merged

Start supporting double map #843
amaury1093 merged 2 commits intopolkadot-js:masterfrom
xlc:double-map

Conversation

@xlc
Copy link
Contributor

@xlc xlc commented Apr 15, 2019

We are upgrading our modules to use double map so there will be a set of contributions from us to make double map actually works with polkadot.js.

This doesn't actually support double map, just fixed some crashing error I encountered while working on apps.

@jacogr
Copy link
Member

jacogr commented Apr 16, 2019

Thank you!

@xlc
Copy link
Contributor Author

xlc commented Apr 16, 2019

The metadata versions is starting getting messy and adding a new feature is not easy because it needs to downgrade from latest version back to v0.

Any plan to refactor so that it is performing upgrade instead of downgrade?

There is v4 (paritytech/substrate#2268) and maybe v5 (paritytech/substrate#2261) upcoming so it is better to do this refactor before they get merged in. (Read the code bit more, maybe they don't need metadata version bump)

@jacogr
Copy link
Member

jacogr commented Apr 17, 2019

Needs to be done, yes. Agreed that it is sprawling a bit atm. Have not been able to get there myself.

@amaurymartiny could you take a peek at this PR please - cannot give it the right kind of attention with limited access.

Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

Yeah, I agree there's some cleaning to be done here. It's planned, I'll be able to personally start cleaning stuff starting May.

}

export class MetadataStorageType extends EnumType<PlainType | MapType> {
export class MetadataStorageType extends EnumType<PlainType | MapType | DoubleMapType> {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can remove this class, and import StorageFunctionType from v0/modules

}

export class MetadataStorageType extends EnumType<PlainType | MapType> {
export class MetadataStorageType extends EnumType<PlainType | MapType | DoubleMapType> {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can remove this class, and import StorageFunctionType from v0/modules

}

export class StorageFunctionType extends EnumType<PlainType | MapType> {
export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMapType> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to understand your intent here: we agree that v0 doesn't have DoubleMapType, but you're still putting it here to be able to factorize code, as this change doesn't break compatibility, correct?

If yes, then see my 2 comments above where we can factorize code a bit more.

import Text from '../../primitive/Text';
import Type from '../../primitive/Type';
import { MetadataStorageModifier } from '../v1/Storage';
import { DoubleMapType } from '../v0/Modules';
Copy link
Contributor

@amaury1093 amaury1093 Apr 18, 2019

Choose a reason for hiding this comment

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

tiny nit: we tend to order things alphabetically by default, when there's no other natural ordering

Copy link
Contributor

Choose a reason for hiding this comment

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

Regarding the ordering and spacing rules, can we have them in tslint?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a good idea. But I think since TypeScript is officially moving towards eslint, we'd like to use eslint everywhere in the mid-term, this looks for for our case.

But now in this "transition" period before eslint is officially supported, i propose not to overbloat tslint.json, and to manually enforce this few rules.

In any case, you can create an issue on https://github.com/polkadot-js/dev

@amaury1093
Copy link
Contributor

amaury1093 commented Apr 26, 2019

Hey @xlc I refactored types/Metadata a bit, so that it's less entangled, but conflicts with this implementation. imo there's only small stuff like https://github.com/polkadot-js/api/pull/843/files#diff-023179d9a94bd59761a7319a645b1dd0R112 to be added in this PR (+ eventually the actual key hashing, but can be done later if needed)

@AlexZhenWang
Copy link
Contributor

Hi @amaurymartiny. Thank you for taking the time to review the code. I will change the code based on the suggestions and rebase this branch on the master to fix the conflict.

}

export class StorageFunctionType extends EnumType<PlainType | MapType> {
export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMapType> {
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no DoubleMap in v0, everything that's related to DoubleMap in this file should be deleted

Copy link
Contributor

@AlexZhenWang AlexZhenWang May 2, 2019

Choose a reason for hiding this comment

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

I got ‘FATAL: Unable to initialize the API: Object.values(...)[index] is not a constructor’ error at EnumType.ts:109:14 without having DoubleMapType here. I have to add DoubleMapType for fixing this issue.
In function
private static createValue (def: TypesDef, index: number = 0, value?: any): Decoded { return { index, value: new (Object.values(def)[index])(value) }; }
, index is 2 but def only have MapType and PlainType. Call stack says, it's because of const extrinsics = extrinsicsFromMeta(this.runtimeMetadata.asV0);

Copy link
Contributor

@amaury1093 amaury1093 May 2, 2019

Choose a reason for hiding this comment

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

Ah, I suspected this would happen...

I think there's a small refactoring to do, have a look at point 1 in #858 (comment):

  • create v0/ and v4/ folders in type-extrinsic
  • move current fromMetadata to fromMetadataV0
  • fromMetadata should call fromMetadataV0 if it's v0-3
  • in v4/, create a fromMetadataV4, which correctly creates extrinsics from v4

Have a look at type-storage, type-extrinsics should follow the same pattern, and this would allow adding hacks like adding DoubleMap in v0

Edit: if this seems confusing to you, ping me here and I'll do this small refactoring tomorrow 1st thing, so that you can rebase on my PR and then we can merge this PR

Copy link
Contributor

@AlexZhenWang AlexZhenWang May 3, 2019

Choose a reason for hiding this comment

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

Thank you @amaurymartiny. I got the idea. I will do the refactoring following your guide.

Copy link
Contributor

Choose a reason for hiding this comment

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

@AlexWZ0129 I've done similar stuffs, you can reuse code from my refactor branch.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ianhe8x Okay. Thank you, Ian!

import Text from '../../primitive/Text';
import Type from '../../primitive/Type';
import { PlainType, StorageFunctionModifier } from '../v1/Storage';
import { DoubleMapType } from '../v4/Storage';
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, remove DoubleMap from v2

key1: Type,
key2: Type,
value: Type,
key2Hasher: StorageHasher
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately key2Hasher is still a Text, revert. Please see https://github.com/paritytech/substrate/blob/master/srml/metadata/src/lib.rs

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for the review. When using Text, if the type of the key has trait prefix like ’T::AssetId’, I got an error ‘Unknown types found, no types for T::AccountId’. Using Type fix the issue. I guess it’s because Type does some format optimise when decoding (such as removing all the trait prefixes Type._removeTraits()).

Copy link
Contributor

Choose a reason for hiding this comment

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

Changed type of key2Hasher to Text. Thanks for the suggestion.

key1: Type,
key2: Type,
value: Type,
key2Hasher: StorageHasher
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

lgtm 👍

@amaury1093 amaury1093 removed the WIP Work in Progress label May 13, 2019
@AlexZhenWang
Copy link
Contributor

lgtm 👍

Thanks @amaurymartiny !

@amaury1093 amaury1093 merged commit 46fee31 into polkadot-js:master May 13, 2019
@xlc xlc deleted the double-map branch May 13, 2019 22:48
ianhe8x pushed a commit to plugblockchain/api.js that referenced this pull request Jun 12, 2019
* double map fix

* update metadata to prevent error using double map
ianhe8x pushed a commit to plugblockchain/api.js that referenced this pull request Jun 12, 2019
* double map fix

* update metadata to prevent error using double map
@polkadot-js-bot
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Jun 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants