-
Notifications
You must be signed in to change notification settings - Fork 258
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
codegen for root level error #930
Merged
tadeohepperle
merged 11 commits into
master
from
tadeo-hepperle-root-level-error-codegen
May 2, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
60db3e5
code gen for root error
tadeohepperle e4f29df
Merge branch 'master' into tadeo-hepperle-root-level-error-codegen
tadeohepperle 77c6fa6
cargo fmt
tadeohepperle fd192e2
polkadot.rs regenerated
tadeohepperle e030846
use pallet name and decode with metadata
tadeohepperle 7f4104f
remove pallet by name fn
tadeohepperle 00ecee6
Merge branch 'master' into tadeo-hepperle-root-level-error-codegen
tadeohepperle da0647c
Merge branch 'master' into tadeo-hepperle-root-level-error-codegen
jsdw e0359ae
test that we can decode a ModuleError via as_root_error
jsdw 0106d46
nits
tadeohepperle 7cea08b
Merge branch 'master' into tadeo-hepperle-root-level-error-codegen
tadeohepperle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2019-2023 Parity Technologies (UK) Ltd. | ||
// This file is dual-licensed as Apache-2.0 or GPL-3.0. | ||
// see LICENSE for license details. | ||
|
||
use frame_metadata::v15::PalletMetadata; | ||
use proc_macro2::TokenStream as TokenStream2; | ||
use quote::quote; | ||
use scale_info::form::PortableForm; | ||
|
||
use crate::types::TypeGenerator; | ||
|
||
use super::CodegenError; | ||
|
||
/// Generate error type alias from the provided pallet metadata. | ||
pub fn generate_error_type_alias( | ||
type_gen: &TypeGenerator, | ||
pallet: &PalletMetadata<PortableForm>, | ||
should_gen_docs: bool, | ||
) -> Result<TokenStream2, CodegenError> { | ||
let Some(error) = &pallet.error else { | ||
return Ok(quote!()); | ||
}; | ||
|
||
let error_type = type_gen.resolve_type_path(error.ty.id); | ||
let error_ty = type_gen.resolve_type(error.ty.id); | ||
let docs = &error_ty.docs; | ||
let docs = should_gen_docs | ||
.then_some(quote! { #( #[doc = #docs ] )* }) | ||
.unwrap_or_default(); | ||
Ok(quote! { | ||
#docs | ||
pub type Error = #error_type; | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
mod calls; | ||
mod constants; | ||
mod errors; | ||
mod events; | ||
mod storage; | ||
|
||
|
@@ -320,10 +321,13 @@ impl RuntimeGenerator { | |
should_gen_docs, | ||
)?; | ||
|
||
let errors = errors::generate_error_type_alias(&type_gen, pallet, should_gen_docs)?; | ||
|
||
Ok(quote! { | ||
pub mod #mod_name { | ||
use super::root_mod; | ||
use super::#types_mod_ident; | ||
#errors | ||
#calls | ||
#event | ||
#storage_mod | ||
|
@@ -371,6 +375,43 @@ impl RuntimeGenerator { | |
}) | ||
}); | ||
|
||
let outer_error_variants = self.metadata.pallets.iter().filter_map(|p| { | ||
let variant_name = format_ident!("{}", p.name); | ||
let mod_name = format_ident!("{}", p.name.to_string().to_snake_case()); | ||
let index = proc_macro2::Literal::u8_unsuffixed(p.index); | ||
|
||
p.error.as_ref().map(|_| { | ||
quote! { | ||
#[codec(index = #index)] | ||
#variant_name(#mod_name::Error), | ||
} | ||
}) | ||
}); | ||
|
||
let outer_error = quote! { | ||
#default_derives | ||
pub enum Error { | ||
#( #outer_error_variants )* | ||
} | ||
}; | ||
Comment on lines
+378
to
+396
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving here my thoughts, not necessarily to be done for this PR: This part of the code seems highly similar to the generation of the outer Event enum.
#[derive(ToTokens)]
enum OuterEnumName {
Error,
Event,
} |
||
|
||
let root_error_if_arms = self.metadata.pallets.iter().filter_map(|p| { | ||
let variant_name_str = &p.name; | ||
let variant_name = format_ident!("{}", variant_name_str); | ||
let mod_name = format_ident!("{}", variant_name_str.to_string().to_snake_case()); | ||
p.error.as_ref().map(|err| | ||
{ | ||
let type_id = err.ty.id; | ||
quote! { | ||
if pallet_name == #variant_name_str { | ||
let variant_error = #mod_name::Error::decode_with_metadata(cursor, #type_id, metadata)?; | ||
return Ok(Error::#variant_name(variant_error)); | ||
} | ||
} | ||
} | ||
) | ||
}); | ||
|
||
let mod_ident = &item_mod_ir.ident; | ||
let pallets_with_constants: Vec<_> = pallets_with_mod_names | ||
.iter() | ||
|
@@ -424,6 +465,16 @@ impl RuntimeGenerator { | |
} | ||
} | ||
|
||
#outer_error | ||
impl #crate_path::error::RootError for Error { | ||
fn root_error(pallet_bytes: &[u8], pallet_name: &str, metadata: &#crate_path::Metadata) -> Result<Self, #crate_path::Error> { | ||
use #crate_path::metadata::DecodeWithMetadata; | ||
let cursor = &mut &pallet_bytes[..]; | ||
#( #root_error_if_arms )* | ||
Err(#crate_path::ext::scale_decode::Error::custom(format!("Pallet name '{}' not found in root Error enum", pallet_name)).into()) | ||
} | ||
} | ||
|
||
pub fn constants() -> ConstantsApi { | ||
ConstantsApi | ||
} | ||
|
@@ -495,7 +546,7 @@ where | |
let ty = type_gen.resolve_type(type_id); | ||
|
||
let scale_info::TypeDef::Variant(variant) = &ty.type_def else { | ||
return Err(CodegenError::InvalidType(error_message_type_name.into())) | ||
return Err(CodegenError::InvalidType(error_message_type_name.into())); | ||
}; | ||
|
||
variant | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: We could name this something like
error_alias
to be a bit more explicit, however you prefer here