Metadata V15: Expose types for the overarching Call, Event, Error enums (#14143)

* frame-metadata: Point to unreleased branch

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame: Generalize outer enum generation for events and errors

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame: Remove individual generation of outer enum events

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* primitives/traits: Add marker trait for outer runtime enums

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame: Derive Clone, PartialEq, Eq for RuntimeEvents only

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/pallet: Include `#[pallet::error]` enum into pallet parts

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* metadata-ir: Include call, event, error types

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/metadata: Include outer enum types in V15 metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/tests: Ensure `RuntimeError` includes `#[pallet::error]` parts

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/support: Document the reserved name for `RuntimeError`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame: Use self-generated `RuntimeEvent` for `GetRuntimeOuterEnumTypes`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/ui: Fix UI tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/support: Remove unused system path

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/ui: Unexpected field and reintroduce frame_system::Config for RuntimeCall

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/support: Remove `GetRuntimeOuterEnumTypes` marker trait

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/support: Remove `;` from macro

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update frame-metadata to point to unreleased branch

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Rename error_enum_ty to module_error_enum_ty

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update module_error_ty documentation

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame: Implement from_dispatch_error

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/support: Adjust test to ModuleErrorType

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Fix clippy

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Improve documentation

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame/tests: Check `from_dispatch_error` impl

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update frame-metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Remove the module_error_ty

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Apply fmt

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert unneeded parts

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert "Revert unneeded parts"

This reverts commit b94bbd16078a025775a48da1095edec1705e6a4d.

Revert "Apply fmt"

This reverts commit 9b1c3e7b4ef27d32e10b35054a99916067e0397b.

Revert "Remove the module_error_ty"

This reverts commit 98de5b24653f9f9ec6ee842b749401b18a01758a.

* Update frame-metadata to origin/master

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Add outerEnums to the metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Add tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Keep backwards compatibility for explicit pallet parts

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Rename tt_error_part to be more generic

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Increase recursion_limit to 1k

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Rename `fully_expanded` to `expanded`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Improve documentation

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Adjust UI tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update UI tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update undefined_validate_unsigned_part.stderr UI test

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Adjust yet again

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Optimise macro expansions

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Use latest frame-metadata and rename `moduleErrorType` to `RuntimeError`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Fix comment

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Apply fmt

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update frame/support/procedural/src/construct_runtime/parse.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update frame/support/procedural/src/construct_runtime/parse.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update frame-metadata PR

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Remove `expanded` from error messages and fix typo

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Move docs to the function

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ui: Use the intermed syntax for pallet parts

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update frame-metadata with latest release

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* frame: Address feedback for `from_dispatch_error`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Alexandru Vasile
2023-06-28 12:44:05 +03:00
committed by GitHub
parent 4249643df2
commit 73a368c2e4
33 changed files with 1143 additions and 337 deletions
@@ -35,6 +35,7 @@ mod keyword {
syn::custom_keyword!(Call);
syn::custom_keyword!(Storage);
syn::custom_keyword!(Event);
syn::custom_keyword!(Error);
syn::custom_keyword!(Config);
syn::custom_keyword!(Origin);
syn::custom_keyword!(Inherent);
@@ -45,6 +46,7 @@ mod keyword {
syn::custom_keyword!(SlashReason);
syn::custom_keyword!(exclude_parts);
syn::custom_keyword!(use_parts);
syn::custom_keyword!(expanded);
}
/// Declaration of a runtime.
@@ -56,6 +58,7 @@ mod keyword {
pub enum RuntimeDeclaration {
Implicit(ImplicitRuntimeDeclaration),
Explicit(ExplicitRuntimeDeclaration),
ExplicitExpanded(ExplicitRuntimeDeclaration),
}
/// Declaration of a runtime with some pallet with implicit declaration of parts.
@@ -106,6 +109,13 @@ impl Parse for RuntimeDeclaration {
pallets,
pallets_token,
})),
PalletsConversion::ExplicitExpanded(pallets) =>
Ok(RuntimeDeclaration::ExplicitExpanded(ExplicitRuntimeDeclaration {
name,
where_section,
pallets,
pallets_token,
})),
}
}
}
@@ -188,6 +198,8 @@ impl Parse for WhereDefinition {
/// The declaration of a pallet.
#[derive(Debug, Clone)]
pub struct PalletDeclaration {
/// Is this pallet fully expanded?
pub is_expanded: bool,
/// The name of the pallet, e.g.`System` in `System: frame_system`.
pub name: Ident,
/// Optional attributes tagged right above a pallet declaration.
@@ -233,6 +245,7 @@ impl Parse for PalletDeclaration {
let _: Token![>] = input.parse()?;
res
} else if !(input.peek(Token![::]) && input.peek3(token::Brace)) &&
!input.peek(keyword::expanded) &&
!input.peek(keyword::exclude_parts) &&
!input.peek(keyword::use_parts) &&
!input.peek(Token![=]) &&
@@ -246,10 +259,21 @@ impl Parse for PalletDeclaration {
None
};
// Check if the pallet is fully expanded.
let (is_expanded, extra_parts) = if input.peek(keyword::expanded) {
let _: keyword::expanded = input.parse()?;
let _: Token![::] = input.parse()?;
(true, parse_pallet_parts(input)?)
} else {
(false, vec![])
};
// Parse for explicit parts
let pallet_parts = if input.peek(Token![::]) && input.peek3(token::Brace) {
let _: Token![::] = input.parse()?;
Some(parse_pallet_parts(input)?)
let mut parts = parse_pallet_parts(input)?;
parts.extend(extra_parts.into_iter());
Some(parts)
} else if !input.peek(keyword::exclude_parts) &&
!input.peek(keyword::use_parts) &&
!input.peek(Token![=]) &&
@@ -260,7 +284,7 @@ impl Parse for PalletDeclaration {
"Unexpected tokens, expected one of `::{`, `exclude_parts`, `use_parts`, `=`, `,`",
))
} else {
None
is_expanded.then_some(extra_parts)
};
// Parse for specified parts
@@ -288,7 +312,7 @@ impl Parse for PalletDeclaration {
None
};
Ok(Self { attrs, name, path, instance, pallet_parts, specified_parts, index })
Ok(Self { is_expanded, attrs, name, path, instance, pallet_parts, specified_parts, index })
}
}
@@ -371,6 +395,7 @@ pub enum PalletPartKeyword {
Call(keyword::Call),
Storage(keyword::Storage),
Event(keyword::Event),
Error(keyword::Error),
Config(keyword::Config),
Origin(keyword::Origin),
Inherent(keyword::Inherent),
@@ -393,6 +418,8 @@ impl Parse for PalletPartKeyword {
Ok(Self::Storage(input.parse()?))
} else if lookahead.peek(keyword::Event) {
Ok(Self::Event(input.parse()?))
} else if lookahead.peek(keyword::Error) {
Ok(Self::Error(input.parse()?))
} else if lookahead.peek(keyword::Config) {
Ok(Self::Config(input.parse()?))
} else if lookahead.peek(keyword::Origin) {
@@ -423,6 +450,7 @@ impl PalletPartKeyword {
Self::Call(_) => "Call",
Self::Storage(_) => "Storage",
Self::Event(_) => "Event",
Self::Error(_) => "Error",
Self::Config(_) => "Config",
Self::Origin(_) => "Origin",
Self::Inherent(_) => "Inherent",
@@ -441,7 +469,7 @@ impl PalletPartKeyword {
/// Returns the names of all pallet parts that allow to have a generic argument.
fn all_generic_arg() -> &'static [&'static str] {
&["Event", "Origin", "Config"]
&["Event", "Error", "Origin", "Config"]
}
}
@@ -452,6 +480,7 @@ impl ToTokens for PalletPartKeyword {
Self::Call(inner) => inner.to_tokens(tokens),
Self::Storage(inner) => inner.to_tokens(tokens),
Self::Event(inner) => inner.to_tokens(tokens),
Self::Error(inner) => inner.to_tokens(tokens),
Self::Config(inner) => inner.to_tokens(tokens),
Self::Origin(inner) => inner.to_tokens(tokens),
Self::Inherent(inner) => inner.to_tokens(tokens),
@@ -554,6 +583,8 @@ fn parse_pallet_parts_no_generic(input: ParseStream) -> Result<Vec<PalletPartNoG
/// The final definition of a pallet with the resulting fixed index and explicit parts.
#[derive(Debug, Clone)]
pub struct Pallet {
/// Is this pallet fully expanded?
pub is_expanded: bool,
/// The name of the pallet, e.g.`System` in `System: frame_system`.
pub name: Ident,
/// Either automatically infered, or defined (e.g. `MyPallet ... = 3,`).
@@ -586,9 +617,35 @@ impl Pallet {
}
/// Result of a conversion of a declaration of pallets.
///
/// # State Transitions
///
/// ```ignore
/// +----------+ +----------+ +------------------+
/// | Implicit | -> | Explicit | -> | ExplicitExpanded |
/// +----------+ +----------+ +------------------+
/// ```
enum PalletsConversion {
/// Pallets implicitely declare parts.
///
/// `System: frame_system`.
Implicit(Vec<PalletDeclaration>),
/// Pallets explicitly declare parts.
///
/// `System: frame_system::{Pallet, Call}`
///
/// However, for backwards compatibility with Polkadot/Kusama
/// we must propagate some other parts to the pallet by default.
Explicit(Vec<Pallet>),
/// Pallets explicitly declare parts that are fully expanded.
///
/// This is the end state that contains extra parts included by
/// default by Subtrate.
///
/// `System: frame_system expanded::{Error} ::{Pallet, Call}`
///
/// For this example, the `Pallet`, `Call` and `Error` parts are collected.
ExplicitExpanded(Vec<Pallet>),
}
/// Convert from the parsed pallet declaration to their final information.
@@ -604,6 +661,7 @@ fn convert_pallets(pallets: Vec<PalletDeclaration>) -> syn::Result<PalletsConver
let mut indices = HashMap::new();
let mut last_index: Option<u8> = None;
let mut names = HashMap::new();
let mut is_expanded = true;
let pallets = pallets
.into_iter()
@@ -698,7 +756,10 @@ fn convert_pallets(pallets: Vec<PalletDeclaration>) -> syn::Result<PalletsConver
})
.collect::<Result<Vec<_>>>()?;
is_expanded &= pallet.is_expanded;
Ok(Pallet {
is_expanded: pallet.is_expanded,
name: pallet.name,
index: final_index,
path: pallet.path,
@@ -709,5 +770,9 @@ fn convert_pallets(pallets: Vec<PalletDeclaration>) -> syn::Result<PalletsConver
})
.collect::<Result<Vec<_>>>()?;
Ok(PalletsConversion::Explicit(pallets))
if is_expanded {
Ok(PalletsConversion::ExplicitExpanded(pallets))
} else {
Ok(PalletsConversion::Explicit(pallets))
}
}