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

Use augmented and generated types #1703

Merged
merged 30 commits into from
Jun 7, 2023
Merged

Use augmented and generated types #1703

merged 30 commits into from
Jun 7, 2023

Conversation

0xverin
Copy link
Contributor

@0xverin 0xverin commented May 16, 2023

resolves #1673
resolves #1667
resolves #1679 (@polkadot/api @polkadot/types @polkadot/kering)

@polkadot/typegen still cannot be upgraded,this issue comment has been mentioned.

  1. I used the latest typegen to generate types for litentry-parachain-metadata. Because when I use litentry-sidechain-metadata, I found that the types are incorrect.
  2. All the generated types have been successfully imported, and in the code, we can see what type it is, as shown in the screenshot below:
image

It is no longer like this:
image

@Kailai-Wang
Copy link
Collaborator

There's some misunderstanding, I think.

We mainly need the primitives types for sidechain, e.g. LitentryIdentity, LitentryValidationData etc. We hope these types are generated instead of being manually defined. That's why we need the sidechain metadata.

The parachain metadata only adds types/extrinsics for the parachain, which is nice-to-have, but not the main goal of the issue.
I'm unsure if we can merge both metadata into one, please check it.

Both parachain and sidechain have a pallet IdentityManagement, if that's a problem, we should check if metadata supports alias. If not, we need to change the pallet name in the worst case.

@0xverin
Copy link
Contributor Author

0xverin commented May 17, 2023

After checking, it is not possible to directly generate LitentryIdentity and LitentryValidationData using any kind of metadata.

  • The argument-types used for these two types are also being manually defined.
  • I have examined the code of idhub, and the result is the same.So, if the sidechain doesn't have the types we need, I believe there may be no need for a merge.
  • I couldn't find any information on merging metadata in either the official documentation or the GitHub repository. I believe it may not be possible to merge metadata.

@grumpygreenguy
Copy link
Contributor

You mean that the generated types in the idhub code are the same we have? 🤔

@0xverin
Copy link
Contributor Author

0xverin commented May 17, 2023

As of now, it appears to be the case.

You mean that the generated types in the idhub code are the same we have? 🤔

Not entirely,they made changes to the content of identity/definitions, so augment-types.ts will have some differences. What I actually meant was that the generation rules are the same. However, that is not enough for us. I am currently thinking about how to include the types generated from sidechain-meta as well.

@grumpygreenguy grumpygreenguy self-requested a review May 23, 2023 12:32
@0xverin
Copy link
Contributor Author

0xverin commented May 24, 2023

Updated:

  1. There was a mistake earlier:when querying the storage in the sidechain, the data passed to createType should come from the sidechain's construction, not the parachain's. The reason it appears to work is that the data structure of LitentryIdentity is correct and matches the expected format.

  2. Although we have generated the LitentryPrimitivesIdentity type from sidechain, the data composed of this structure is still the same as before. I don't think I have made a mistake(if I have,plz correct me,thanks!). Here is an example of how Phala uses XcmVersionedXcm type. Additionally, if the returned structure is not like the one constructed by buildIdentityHelper,, it is unlikely to succeed.So,I didn't change LitentryIdentity type.

  3. This pr, with the addition of tsconfig.json, type checking has been enabled, which means that when I pull other PRs, I encounter type check errors locally. It was my mistake, and in fact, the type checking should have been added to ts-test from the beginning instead of modifying the types now. Type checking is something that should be done from the start.

Comment on lines +8 to +13
"gen-type:parachain": "yarn gen-type-parachain:defs && yarn gen-type-parachain:meta && yarn format",
"gen-type-parachain:defs": "ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package . --input ./parachain-interfaces --endpoint ./litentry-parachain-metadata.json",
"gen-type-parachain:meta": "ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --package . --output ./parachain-interfaces --endpoint ./litentry-parachain-metadata.json",
"gen-type:sidechain": "yarn gen-type-sidechain:defs && yarn gen-type-sidechain:meta && yarn format",
"gen-type-sidechain:defs": "ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package . --input ./sidechain-interfaces --endpoint ./litentry-sidechain-metadata.json",
"gen-type-sidechain:meta": "ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --package . --output ./sidechain-interfaces --endpoint ./litentry-sidechain-metadata.json",
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have these documented somewhere? If so then that probably needs updated as well 🤔

Copy link
Contributor

@grumpygreenguy grumpygreenguy left a comment

Choose a reason for hiding this comment

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

LGTM; left minor comments/questions

Copy link
Collaborator

@Kailai-Wang Kailai-Wang left a comment

Choose a reason for hiding this comment

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

I'd expect things like LitentryIdentity and LitentryValidationData would be deleted from type-definitions.ts and types in metadata are used instead. Isn't it the case?

"paths": {
"@polkadot/api/augment": ["./parachain-interfaces/augment-api.ts"],
"@polkadot/types/augment": ["./parachain-interfaces/augment-types.ts"],
"@polkadot/types/lookup": ["./sidechain-interfaces/types-lookup.ts"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

These three paths are a mixture of parachain and sidechain interfaces, is this intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are different. The first two refer to the detection of API types (for example, when using api.tx, we need the parachain type). The last one refers to the configuration when we need to import a specific type (for example, importing LitentryPrimitivesIdentity).

Copy link
Collaborator

Choose a reason for hiding this comment

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

What if the types that we need are in the parachain types-lookup?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have contemplated merging them together: "@polkadot/types/lookup":[parachain..., sidechain...], but it is not supported. There are related issues mentioning this, so I think ultimately Rust needs to merge identity types into parachain.

tee-worker/ts-tests/common/utils/identity-helper.ts Outdated Show resolved Hide resolved
tee-worker/ts-tests/common/type-definitions.ts Outdated Show resolved Hide resolved
@0xverin
Copy link
Contributor Author

0xverin commented May 31, 2023

Updated:

  1. Removed custom types related to identity and replaced them with generated types.
  2. Modified parts of the code that involved assertions to use interfaces from the autogenerated types.

@0xverin
Copy link
Contributor Author

0xverin commented Jun 1, 2023

Major updates:

  1. Refactored all the assertions.
  2. I have made an effort to use the LitentryPrimitivesIdentity interface for data comparison between events and idGraph. I believe this approach is reasonable(usage).

Once the CI passes, it is ready for review.

@0xverin 0xverin force-pushed the 1673-use-generated-types branch from 7ffe8e5 to bda99a7 Compare June 1, 2023 16:04
@0xverin 0xverin force-pushed the 1673-use-generated-types branch from 575f7b2 to 514f003 Compare June 1, 2023 17:39
Comment on lines 27 to 32
const substrateExtensionIdentity = {
Substrate: {
address: '0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48', //Bob
network: 'Litentry',
},
} as any;
} as unknown as LitentryPrimitivesIdentity;
Copy link
Contributor

Choose a reason for hiding this comment

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

I get why we need the as when interfacing with the (mis-)augmented API, but why the assertion here?

If I get it correctly this is the input we want to pass to createType('LitentryPrimitivesIdentity', <_>), but from what we saw earlier that type is actually different from how LitentryPrimitivesIdentity is declared, no? For instance, this input doesn't have the type or isSubstrate props.

This might still be a job for the ExtractEnumVariant we discussed earlier 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This might still be a job for the ExtractEnumVariant we discussed earlier

Your understanding is correct. If we want perfection, we do need to do it that way(ExtractEnumVariant). However, you will find that in our ts-test, we mostly use interfaces for LitentryPrimitivesIdentity. I'm thinking that when assembling the parameters, we can define the LitentryPrimitivesIdentity type ,but need pass it to createType with Substrate: { address: '0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48', network: 'Litentry', }. Do you think this is significant? Would it be enough for IDHub to know the LitentryPrimitivesIdentity which interfaces it has?

If they want to define LitentryPrimitivesIdentity, would it be enough for them to assemble their own ExtractEnumVariant?

Copy link
Contributor

Choose a reason for hiding this comment

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

If we want perfection, we do need to do it that way(ExtractEnumVariant).

I don't think we want perfection, but we should at least strive to have accurate definitions for the input and output datatypes in the API (even if we cannot get them to be inferred correctly in each usage).

However, you will find that in our ts-test, we mostly use interfaces for LitentryPrimitivesIdentity.

I don't follow what you mean by this 🤔

but need pass it to createType with Substrate: { address: '0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48', network: 'Litentry', }.

My point is that AFAICT this value ({ Substrate: { ... } }) that we're passing to createType is not a LitentryPrimitivesIdentity, but rather some other type that at the moment doesn't have a name. We can derive that type from LitentryPrimitivesIdentity with something like ExtractEnumVariant, but they're not the same type and I think it only makes sense to make that distinction explicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have decided to address this matter in another PR/issue.

@0xverin
Copy link
Contributor Author

0xverin commented Jun 5, 2023

Now there is a final decision. Do we want to assemble the ExtractEnumVariant type to fully reconstruct the LitentryPrimitivesIdentity type? Personally, I believe it may be more meaningful for IDHub to understand how to use the interfaces of LitentryPrimitivesIdentity. They may need more interfaces to perform certain state checks (I inferred this from looking at their code, as they also use as unknown). They can refer to our usage of assert to utilize the LitentryPrimitivesIdentity interface. @Kailai-Wang @grumpygreenguy :P

Since this PR has been open for a while, I would like to make the final confirmation in the last stage. We can create a separate PR for the "looks-up" issue because that seems to be a TypeScript problem.

@Kailai-Wang
Copy link
Collaborator

I still see many conflicting files for this PR.
Can we resolve these ASAP and get it merged (if it's ready)? This PR has been there for 3 weeks :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants