paritytech / polkadot-sdk

The Parity Polkadot Blockchain SDK
https://polkadot.network/
1.77k stars 633 forks source link

Metadata V16: Enrich metadata with associated types of config traits #4519

Open lexnv opened 3 months ago

lexnv commented 3 months ago

Developers use associated types from pallet config traits to determine how to encode transactions (ie what hash is used by the node, the accountId, asset id etc). For example, subxt uses the following configs for Substrate and Polkadot to communicate with different chains.

This information can be exposed in the metadata V16, initially as unstable metadata (version u32::max). Types included in the metadata must implement scale_info::TypeInfo.

Default to include associated types to metadata

By default, all associated types from config traits are captured in the metadata:

https://github.com/paritytech/polkadot-sdk/blob/313fe0f9a277f27a4228634f0fb15a1c3fa21271/substrate/frame/system/src/lib.rs#L481-L518

Opt-out of associated types from metadata

Developers that do not want to include types into the metadata can specify it by the following attribute (name tbd):

#[pallet::config(without_metadata)]
pub trait Config {
  type CustomParaType: ...

  type SecondType: ...
}

Selectively include associated types

There might be scenarios where only a single associated type is needed to express the pallet in the metadata. One such case is the AssetId:

https://github.com/paritytech/polkadot-sdk/blob/313fe0f9a277f27a4228634f0fb15a1c3fa21271/substrate/frame/assets/src/lib.rs#L294-L295

#[pallet::config(without_metadata)]
pub trait Config {

  #[pallet::include_metadata]
  type AssetId: ...

  type SecondType: ...
}

In this example, the AssetId is selectively included in the metadata, where the SecondType is not included.

PoC to capture associated types: https://github.com/paritytech/polkadot-sdk/pull/4358 PoC in subx to use associated metadata types: https://github.com/paritytech/subxt/pull/1566

cc @jsdw @niklasad1 @bkchr @paritytech/subxt-team

bkchr commented 3 months ago

Looks like a reasonable proposal to me!