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

wip/PoC: Enrich metadata custom types with essential types for chain communications #4358

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f0ea720
metadata/ir: Extend IR with custom types
lexnv May 1, 2024
fecdd47
metadata/ir: Convert custom IR types to concrete metadata
lexnv May 1, 2024
1279594
frame/metadata: Populate custom types with runtime call of system config
lexnv May 1, 2024
ab4f6cc
frame/metadata: Expose AccountId to metadata
lexnv May 1, 2024
e7bee31
frame/metadata: Optionally expose the AssetId of the asset pallet
lexnv May 1, 2024
d0986cf
frame/metadata: Use runtime instance for extracting AssetId type
lexnv May 2, 2024
c8061aa
frame/metadata: Extract address ty
lexnv May 2, 2024
e63dae6
frame/metadata: Extract signature ty
lexnv May 2, 2024
c666a6f
frame/metadata: Extend with Hash and Hashing types
lexnv May 2, 2024
fbd75b0
frame/metadata: Extract block header to custom types
lexnv May 2, 2024
0fedb4d
frame/metadata: Remove call enum ty from custom types
lexnv May 2, 2024
17ade23
frame/proc: Refactor itemTrait extraction
lexnv May 16, 2024
616e38d
frame/proc: Refactor check_event_type
lexnv May 16, 2024
482a5ed
frame/proc: Refactor TraitItemType parsing
lexnv May 16, 2024
785436c
frame/constants: Improve documentation
lexnv May 17, 2024
a4d7ec7
frame/helpers: Refactor take_first_item_pallet_attr
lexnv May 17, 2024
c50d364
frame/config: Add associated types to parsed config
lexnv May 17, 2024
f40acd4
metadata-ir: Add type for PalletAssociatedTypesMetadataIR
lexnv May 17, 2024
7659b19
frame/config: Implement associated types metadata
lexnv May 17, 2024
ab0b1b1
frame/construct_runtime: Extract associated types from pallet config
lexnv May 17, 2024
b1d37e2
Add typeInfo to types and custom cargo patch
lexnv May 17, 2024
97396d0
frame/proc: Extract cfg attributes
lexnv May 17, 2024
3f5597e
frame/config: Add type attributes to metadata expansion
lexnv May 17, 2024
a6532ed
Add type info on other config types
lexnv May 20, 2024
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: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cumulus/pallets/collator-selection/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub trait WeightInfo {
}

/// Weights for pallet_collator_selection using the Substrate node and recommended hardware.
#[derive(scale_info::TypeInfo)]
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn set_invulnerables(b: u32) -> Weight {
Expand Down
2 changes: 1 addition & 1 deletion cumulus/pallets/xcmp-queue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ cumulus-primitives-core = { path = "../../primitives/core", default-features = f

# Optional import for benchmarking
frame-benchmarking = { path = "../../../substrate/frame/benchmarking", default-features = false, optional = true }
bounded-collections = { version = "0.2.0", default-features = false }
bounded-collections = { path = "/home/lexnv/workspace/parity-common/bounded-collections", default-features = false }

# Bridges
bp-xcm-bridge-hub-router = { path = "../../../bridges/primitives/xcm-bridge-hub-router", default-features = false, optional = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1678,8 +1678,13 @@ cumulus_pallet_parachain_system::register_validate_block! {

parameter_types! {
// The deposit configuration for the singed migration. Specially if you want to allow any signed account to do the migration (see `SignedFilter`, these deposits should be high)
#[derive(scale_info::TypeInfo)]
pub const MigrationSignedDepositPerItem: Balance = CENTS;

#[derive(scale_info::TypeInfo)]
pub const MigrationSignedDepositBase: Balance = 2_000 * CENTS;

#[derive(scale_info::TypeInfo)]
pub const MigrationMaxKeyLen: u32 = 512;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ parameter_types! {
pub WestendTreasuryAccount: AccountId = WESTEND_TREASURY_PALLET_ID.into_account_truncating();
// The number of blocks a member must wait between giving a retirement notice and retiring.
// Supposed to be greater than time required to `kick_member` with alliance motion.
#[derive(scale_info::TypeInfo)]
pub const AllianceRetirementPeriod: BlockNumber = (90 * DAYS) + ALLIANCE_MOTION_DURATION;
}

Expand Down
3 changes: 2 additions & 1 deletion polkadot/parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ sp-core = { path = "../../substrate/primitives/core", default-features = false,
sp-weights = { path = "../../substrate/primitives/weights", default-features = false }
polkadot-core-primitives = { path = "../core-primitives", default-features = false }
derive_more = "0.99.11"
bounded-collections = { version = "0.2.0", default-features = false, features = ["serde"] }

bounded-collections = { path = "/home/lexnv/workspace/parity-common/bounded-collections", default-features = false, features = ["serde"] }

# all optional crates.
serde = { features = ["alloc", "derive"], workspace = true }
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ impl pallet_preimage::Config for Runtime {

parameter_types! {
pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK;
#[derive(scale_info::TypeInfo)]
pub ReportLongevity: u64 = EpochDurationInBlocks::get() as u64 * 10;
}

Expand Down
5 changes: 5 additions & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1367,8 +1367,13 @@ impl pallet_root_testing::Config for Runtime {

parameter_types! {
// The deposit configuration for the singed migration. Specially if you want to allow any signed account to do the migration (see `SignedFilter`, these deposits should be high)
#[derive(scale_info::TypeInfo)]
pub const MigrationSignedDepositPerItem: Balance = 1 * CENTS;

#[derive(scale_info::TypeInfo)]
pub const MigrationSignedDepositBase: Balance = 20 * CENTS * 100;

#[derive(scale_info::TypeInfo)]
pub const MigrationMaxKeyLen: u32 = 512;
}

Expand Down
2 changes: 1 addition & 1 deletion polkadot/xcm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ workspace = true

[dependencies]
array-bytes = "6.1"
bounded-collections = { version = "0.2.0", default-features = false, features = ["serde"] }
bounded-collections = { path = "/home/lexnv/workspace/parity-common/bounded-collections", default-features = false, features = ["serde"] }
derivative = { version = "2.2.0", default-features = false, features = ["use_core"] }
impl-trait-for-tuples = "0.2.2"
log = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion polkadot/xcm/pallet-xcm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license.workspace = true
workspace = true

[dependencies]
bounded-collections = { version = "0.2.0", default-features = false }
bounded-collections = { path = "/home/lexnv/workspace/parity-common/bounded-collections", default-features = false }
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive"] }
scale-info = { version = "2.11.1", default-features = false, features = ["derive"] }
serde = { optional = true, features = ["derive"], workspace = true, default-features = true }
Expand Down
1 change: 1 addition & 0 deletions substrate/bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl HandleCredit<AccountId, Assets> for CreditToBlockAuthor {
}
}

#[derive(scale_info::TypeInfo)]
pub struct AllianceIdentityVerifier;
impl IdentityVerifier<AccountId> for AllianceIdentityVerifier {
fn has_required_identities(who: &AccountId) -> bool {
Expand Down
18 changes: 18 additions & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ parameter_types! {
// Attempting to do so will brick block production.
pub const EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS;
pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK;

#[derive(scale_info::TypeInfo)]
pub const ReportLongevity: u64 =
BondingDuration::get() as u64 * SessionsPerEra::get() as u64 * EpochDuration::get();
}
Expand Down Expand Up @@ -1954,8 +1956,13 @@ impl pallet_whitelist::Config for Runtime {
}

parameter_types! {
#[derive(scale_info::TypeInfo)]
pub const MigrationSignedDepositPerItem: Balance = 1 * CENTS;

#[derive(scale_info::TypeInfo)]
pub const MigrationSignedDepositBase: Balance = 20 * DOLLARS;

#[derive(scale_info::TypeInfo)]
pub const MigrationMaxKeyLen: u32 = 512;
}

Expand All @@ -1978,8 +1985,13 @@ impl pallet_state_trie_migration::Config for Runtime {
const ALLIANCE_MOTION_DURATION_IN_BLOCKS: BlockNumber = 5 * DAYS;

parameter_types! {
#[derive(scale_info::TypeInfo)]
pub const AllianceMotionDuration: BlockNumber = ALLIANCE_MOTION_DURATION_IN_BLOCKS;

#[derive(scale_info::TypeInfo)]
pub const AllianceMaxProposals: u32 = 100;

#[derive(scale_info::TypeInfo)]
pub const AllianceMaxMembers: u32 = 100;
}

Expand All @@ -1999,6 +2011,7 @@ impl pallet_collective::Config<AllianceCollective> for Runtime {

parameter_types! {
pub const MaxFellows: u32 = AllianceMaxMembers::get();
#[derive(scale_info::TypeInfo)]
pub const MaxAllies: u32 = 100;
pub const AllyDeposit: Balance = 10 * DOLLARS;
pub const RetirementPeriod: BlockNumber = ALLIANCE_MOTION_DURATION_IN_BLOCKS + (1 * DAYS);
Expand Down Expand Up @@ -2065,6 +2078,7 @@ impl pallet_statement::Config for Runtime {
}

parameter_types! {
#[derive(scale_info::TypeInfo)]
pub MbmServiceWeight: Weight = Perbill::from_percent(80) * RuntimeBlockWeights::get().max_block;
}

Expand All @@ -2087,6 +2101,7 @@ parameter_types! {
pub const BrokerPalletId: PalletId = PalletId(*b"py/broke");
}

#[derive(scale_info::TypeInfo)]
pub struct IntoAuthor;
impl OnUnbalanced<Credit<AccountId, Balances>> for IntoAuthor {
fn on_nonzero_unbalanced(credit: Credit<AccountId, Balances>) {
Expand All @@ -2100,6 +2115,7 @@ parameter_types! {
pub storage CoretimeRevenue: Option<(BlockNumber, Balance)> = None;
}

#[derive(scale_info::TypeInfo)]
pub struct CoretimeProvider;
impl CoretimeInterface for CoretimeProvider {
type AccountId = AccountId;
Expand Down Expand Up @@ -2206,6 +2222,7 @@ impl Default for RuntimeParameters {
}
}

#[derive(scale_info::TypeInfo)]
pub struct DynamicParametersManagerOrigin;
impl EnsureOriginWithArg<RuntimeOrigin, RuntimeParametersKey> for DynamicParametersManagerOrigin {
type Success = ();
Expand Down Expand Up @@ -2336,6 +2353,7 @@ mod runtime {
pub type Offences = pallet_offences;

#[runtime::pallet_index(26)]
#[derive(scale_info::TypeInfo)]
pub type Historical = pallet_session_historical;

#[runtime::pallet_index(27)]
Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/alliance/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ impl Verify for AccountU64 {
}
}

#[derive(scale_info::TypeInfo)]
pub struct AllianceIdentityVerifier;
impl IdentityVerifier<AccountId> for AllianceIdentityVerifier {
fn has_required_identities(who: &AccountId) -> bool {
Expand Down Expand Up @@ -213,6 +214,7 @@ impl ProposalProvider<AccountId, H256, RuntimeCall> for AllianceProposalProvider

parameter_types! {
pub const MaxFellows: u32 = MaxMembers::get();
#[derive(scale_info::TypeInfo)]
pub const MaxAllies: u32 = 100;
pub const AllyDeposit: u64 = 25;
pub const RetirementPeriod: BlockNumber = MOTION_DURATION_IN_BLOCKS + 1;
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/babe/src/equivocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl<Offender: Clone> Offence<Offender> for EquivocationOffence<Offender> {
/// - On-chain validity checks and processing are mostly delegated to the user provided generic
/// types implementing `KeyOwnerProofSystem` and `ReportOffence` traits.
/// - Offence reporter for unsigned transactions is fetched via the the authorship pallet.
#[derive(scale_info::TypeInfo)]
pub struct EquivocationReportSystem<T, R, P, L>(sp_std::marker::PhantomData<(T, R, P, L)>);

impl<T, R, P, L>
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/beefy/src/equivocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ where
/// - On-chain validity checks and processing are mostly delegated to the user provided generic
/// types implementing `KeyOwnerProofSystem` and `ReportOffence` traits.
/// - Offence reporter for unsigned transactions is fetched via the the authorship pallet.
#[derive(scale_info::TypeInfo)]
pub struct EquivocationReportSystem<T, R, P, L>(sp_std::marker::PhantomData<(T, R, P, L)>);

/// Equivocation evidence convenience alias.
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ pub mod pallet {
}

/// A type providing default configurations for this pallet in testing environment.
#[derive(scale_info::TypeInfo)]
pub struct TestDefaultConfig;

impl<Output, BlockNumber> Randomness<Output, BlockNumber> for TestDefaultConfig {
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/executive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ parameter_types! {
pub static SystemCallbacksCalled: u32 = 0;
}

#[derive(scale_info::TypeInfo)]
pub struct MockedSystemCallbacks;
impl PreInherents for MockedSystemCallbacks {
fn pre_inherents() {
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/grandpa/src/equivocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl<Offender: Clone> Offence<Offender> for EquivocationOffence<Offender> {
/// - On-chain validity checks and processing are mostly delegated to the user provided generic
/// types implementing `KeyOwnerProofSystem` and `ReportOffence` traits.
/// - Offence reporter for unsigned transactions is fetched via the the authorship pallet.
#[derive(scale_info::TypeInfo)]
pub struct EquivocationReportSystem<T, R, P, L>(sp_std::marker::PhantomData<(T, R, P, L)>);

impl<T, R, P, L>
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/migrations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ pub mod pallet {

frame_support::parameter_types! {
/// Maximal weight per block that can be spent on migrations in tests.
#[derive(scale_info::TypeInfo)]
pub TestMaxServiceWeight: Weight = <<TestDefaultConfig as frame_system::DefaultConfig>::BlockWeights as Get<BlockWeights>>::get().max_block.div(2);
}

Expand Down
1 change: 1 addition & 0 deletions substrate/frame/multisig/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl pallet_balances::Config for Test {
type AccountStore = System;
}

#[derive(scale_info::TypeInfo)]
pub struct TestBaseCallFilter;
impl Contains<RuntimeCall> for TestBaseCallFilter {
fn contains(c: &RuntimeCall) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/proxy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
self == &ProxyType::Any || self == o
}
}

#[derive(scale_info::TypeInfo)]
pub struct BaseFilter;
impl Contains<RuntimeCall> for BaseFilter {
fn contains(c: &RuntimeCall) -> bool {
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/referenda/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ frame_support::construct_runtime!(
);

// Test that a fitlered call can be dispatched.
#[derive(scale_info::TypeInfo)]
pub struct BaseFilter;
impl Contains<RuntimeCall> for BaseFilter {
fn contains(call: &RuntimeCall) -> bool {
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/scheduler/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ frame_support::construct_runtime!(
);

// Scheduler must dispatch with root and no filter, this tests base filter is indeed not used.
#[derive(scale_info::TypeInfo)]
pub struct BaseFilter;
impl Contains<RuntimeCall> for BaseFilter {
fn contains(call: &RuntimeCall) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub fn expand_runtime_metadata(
scrate: &TokenStream,
extrinsic: &TokenStream,
system_path: &PalletPath,
assets_pallet: Option<&Pallet>,
header: &TokenStream,
) -> TokenStream {
let pallets = pallet_declarations
.iter()
Expand All @@ -50,6 +52,7 @@ pub fn expand_runtime_metadata(
let constants = expand_pallet_metadata_constants(runtime, decl);
let errors = expand_pallet_metadata_errors(runtime, decl);
let docs = expand_pallet_metadata_docs(runtime, decl);
let associated_types = expand_pallet_metadata_associated_types(runtime, decl);
let attr = decl.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| {
let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original()))
.expect("was successfully parsed before; qed");
Expand All @@ -70,11 +73,29 @@ pub fn expand_runtime_metadata(
constants: #constants,
error: #errors,
docs: #docs,
associated_types: #associated_types,
}
}
})
.collect::<Vec<_>>();

let maybe_asset_id = assets_pallet
.map(|pallet| {
let path = &pallet.path;
let instance = &pallet.instance;
let maybe_instance = instance.as_ref().map(|inst| quote! { <#inst> });

quote! {
(
"AssetId".into(),
#scrate::__private::scale_info::meta_type::<
<#runtime as #path::Config #maybe_instance>::AssetId
>(),
),
}
})
.unwrap_or_default();

quote! {
impl #runtime {
fn metadata_ir() -> #scrate::__private::metadata_ir::MetadataIR {
Expand Down Expand Up @@ -140,6 +161,43 @@ pub fn expand_runtime_metadata(
>(),
event_enum_ty: #scrate::__private::scale_info::meta_type::<RuntimeEvent>(),
error_enum_ty: #scrate::__private::scale_info::meta_type::<RuntimeError>(),
},
custom_types: #scrate::__private::metadata_ir::CustomMetadataIR {
map: [
(
"AccountId".into(),
#scrate::__private::scale_info::meta_type::<
<#runtime as #system_path::Config>::AccountId
>(),
),
(
"Address".into(),
address_ty,
),
(
"Signature".into(),
signature_ty,
),
Comment on lines +173 to +180
Copy link
Member

Choose a reason for hiding this comment

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

They already exist in the extrinsic metadata?

(
"Hash".into(),
#scrate::__private::scale_info::meta_type::<
<#runtime as #system_path::Config>::Hash
>(),
),
(
"Hashing".into(),
#scrate::__private::scale_info::meta_type::<
<#runtime as #system_path::Config>::Hashing
>(),
),
(
"Header".into(),
#scrate::__private::scale_info::meta_type::<#header>(),
),
#maybe_asset_id
]
.into_iter()
.collect(),
}
}
}
Expand Down Expand Up @@ -256,3 +314,12 @@ fn expand_pallet_metadata_docs(runtime: &Ident, decl: &Pallet) -> TokenStream {
#path::Pallet::<#runtime #(, #path::#instance)*>::pallet_documentation_metadata()
}
}

fn expand_pallet_metadata_associated_types(runtime: &Ident, decl: &Pallet) -> TokenStream {
let path = &decl.path;
let instance = decl.instance.as_ref().into_iter();

quote! {
#path::Pallet::<#runtime #(, #path::#instance)*>::pallet_associated_types_metadata()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub fn expand_outer_origin(
///
#[doc = #doc_string]
#[derive(Clone)]
#[derive(#scrate::__private::scale_info::TypeInfo)]
pub struct RuntimeOrigin {
pub caller: OriginCaller,
filter: #scrate::__private::sp_std::rc::Rc<Box<dyn Fn(&<#runtime as #system_path::Config>::RuntimeCall) -> bool>>,
Expand Down
Loading