Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Don't require module name in inherents (#6576)
Browse files Browse the repository at this point in the history
* Start

* Cleanup `construct_runtime!`

* Add tests

* Fix after merge

* Update the docs
  • Loading branch information
bkchr authored Jul 6, 2020
1 parent b851b75 commit c5368a1
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 161 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ construct_runtime!(
System: system::{Module, Call, Config, Storage, Event<T>},
RandomnessCollectiveFlip: randomness_collective_flip::{Module, Call, Storage},
Timestamp: timestamp::{Module, Call, Storage, Inherent},
Aura: aura::{Module, Config<T>, Inherent(Timestamp)},
Aura: aura::{Module, Config<T>, Inherent},
Grandpa: grandpa::{Module, Call, Storage, Config, Event},
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage},
Expand Down
2 changes: 1 addition & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ construct_runtime!(
{
System: frame_system::{Module, Call, Config, Storage, Event<T>},
Utility: pallet_utility::{Module, Call, Event},
Babe: pallet_babe::{Module, Call, Storage, Config, Inherent(Timestamp), ValidateUnsigned},
Babe: pallet_babe::{Module, Call, Storage, Config, Inherent, ValidateUnsigned},
Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent},
Authorship: pallet_authorship::{Module, Call, Storage, Inherent},
Indices: pallet_indices::{Module, Call, Storage, Config<T>, Event<T>},
Expand Down
2 changes: 1 addition & 1 deletion frame/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ impl<T: Trait + Send + Sync> sp_std::fmt::Debug for WatchDummy<T> {

impl<T: Trait + Send + Sync> SignedExtension for WatchDummy<T>
where
<T as frame_system::Trait>::Call: IsSubType<Module<T>, T>,
<T as frame_system::Trait>::Call: IsSubType<Call<T>>,
{
const IDENTIFIER: &'static str = "WatchDummy";
type AccountId = T::AccountId;
Expand Down
2 changes: 1 addition & 1 deletion frame/grandpa/src/equivocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl From<ReportEquivocationValidityError> for TransactionValidityError {

impl<T: super::Trait + Send + Sync> SignedExtension for ValidateEquivocationReport<T>
where
<T as frame_system::Trait>::Call: IsSubType<super::Module<T>, T>,
<T as frame_system::Trait>::Call: IsSubType<super::Call<T>>,
{
const IDENTIFIER: &'static str = "ValidateEquivocationReport";
type AccountId = T::AccountId;
Expand Down
2 changes: 1 addition & 1 deletion frame/proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub trait Trait: frame_system::Trait {

/// The overarching call type.
type Call: Parameter + Dispatchable<Origin=Self::Origin, PostInfo=PostDispatchInfo>
+ GetDispatchInfo + From<frame_system::Call<Self>> + IsSubType<Module<Self>, Self>
+ GetDispatchInfo + From<frame_system::Call<Self>> + IsSubType<Call<Self>>
+ IsType<<Self as frame_system::Trait>::Call>;

/// The currency mechanism.
Expand Down
2 changes: 1 addition & 1 deletion frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ pub trait Trait: frame_system::Trait + SendTransactionTypes<Call<Self>> {
type ElectionLookahead: Get<Self::BlockNumber>;

/// The overarching call type.
type Call: Dispatchable + From<Call<Self>> + IsSubType<Module<Self>, Self> + Clone;
type Call: Dispatchable + From<Call<Self>> + IsSubType<Call<Self>> + Clone;

/// Maximum number of balancing iterations to run in the offchain submission.
///
Expand Down
1 change: 1 addition & 0 deletions frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ smallvec = "1.4.0"
[dev-dependencies]
pretty_assertions = "0.6.1"
frame-system = { version = "2.0.0-rc4", path = "../system" }
parity-util-mem = { version = "0.6.1", features = ["primitive-types"] }

[features]
default = ["std"]
Expand Down
21 changes: 12 additions & 9 deletions frame/support/procedural/src/construct_runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ fn construct_runtime_parsed(definition: RuntimeDefinition) -> Result<TokenStream
let dispatch = decl_outer_dispatch(&name, modules.iter(), &scrate);
let metadata = decl_runtime_metadata(&name, modules.iter(), &scrate, &unchecked_extrinsic);
let outer_config = decl_outer_config(&name, modules.iter(), &scrate);
let inherent = decl_outer_inherent(&block, &unchecked_extrinsic, modules.iter(), &scrate);
let inherent = decl_outer_inherent(
&block,
&unchecked_extrinsic,
modules.iter(),
&scrate,
);
let validate_unsigned = decl_validate_unsigned(&name, modules.iter(), &scrate);
let integrity_test = decl_integrity_test(&scrate);

Expand Down Expand Up @@ -153,19 +158,17 @@ fn decl_outer_inherent<'a>(
) -> TokenStream2 {
let modules_tokens = module_declarations.filter_map(|module_declaration| {
let maybe_config_part = module_declaration.find_part("Inherent");
maybe_config_part.map(|config_part| {
let arg = config_part
.args
.as_ref()
.and_then(|parens| parens.content.inner.iter().next())
.unwrap_or(&module_declaration.name);
maybe_config_part.map(|_| {
let name = &module_declaration.name;
quote!(#name : #arg,)
quote!(#name,)
})
});
quote!(
#scrate::impl_outer_inherent!(
impl Inherents where Block = #block, UncheckedExtrinsic = #unchecked_extrinsic {
impl Inherents where
Block = #block,
UncheckedExtrinsic = #unchecked_extrinsic
{
#(#modules_tokens)*
}
);
Expand Down
30 changes: 0 additions & 30 deletions frame/support/procedural/src/construct_runtime/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,6 @@ impl ModulePartKeyword {
Ident::new(self.name(), self.span())
}

/// Returns `true` if this module part allows to have an argument.
///
/// For example `Inherent(Timestamp)`.
fn allows_arg(&self) -> bool {
Self::all_allow_arg().iter().any(|n| *n == self.name())
}

/// Returns the names of all module parts that allow to have an argument.
fn all_allow_arg() -> &'static [&'static str] {
&["Inherent"]
}

/// Returns `true` if this module part is allowed to have generic arguments.
fn allows_generic(&self) -> bool {
Self::all_generic_arg().iter().any(|n| *n == self.name())
Expand Down Expand Up @@ -321,7 +309,6 @@ impl Spanned for ModulePartKeyword {
pub struct ModulePart {
pub keyword: ModulePartKeyword,
pub generics: syn::Generics,
pub args: Option<ext::Parens<ext::Punctuated<Ident, Token![,]>>>,
}

impl Parse for ModulePart {
Expand All @@ -339,27 +326,10 @@ impl Parse for ModulePart {
);
return Err(syn::Error::new(keyword.span(), msg));
}
let args = if input.peek(token::Paren) {
if !keyword.allows_arg() {
let syn::group::Parens { token: parens, .. } = syn::group::parse_parens(input)?;
let valid_names = ModulePart::format_names(ModulePartKeyword::all_allow_arg());
let msg = format!(
"`{}` is not allowed to have arguments in parens. \
Only the following modules are allowed to have arguments in parens: {}.",
keyword.name(),
valid_names,
);
return Err(syn::Error::new(parens.span, msg));
}
Some(input.parse()?)
} else {
None
};

Ok(Self {
keyword,
generics,
args,
})
}
}
Expand Down
6 changes: 2 additions & 4 deletions frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,8 @@ pub fn decl_storage(input: TokenStream) -> TokenStream {
/// - `Event` or `Event<T>` (if the event is generic)
/// - `Origin` or `Origin<T>` (if the origin is generic)
/// - `Config` or `Config<T>` (if the config is generic)
/// - `Inherent ( $(CALL),* )` - If the module provides/can check inherents. The optional parameter
/// is for modules that use a `Call` from a different module as
/// inherent.
/// - `ValidateUnsigned` - If the module validates unsigned extrinsics.
/// - `Inherent` - If the module provides/can check inherents.
/// - `ValidateUnsigned` - If the module validates unsigned extrinsics.
///
/// # Note
///
Expand Down
136 changes: 68 additions & 68 deletions frame/support/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub trait Callable<T> {

// dirty hack to work around serde_derive issue
// https://github.com/rust-lang/rust/issues/51331
pub type CallableCallFor<A, T> = <A as Callable<T>>::Call;
pub type CallableCallFor<A, R> = <A as Callable<R>>::Call;

/// A type that can be used as a parameter in a dispatchable function.
///
Expand Down Expand Up @@ -1848,8 +1848,8 @@ macro_rules! decl_module {
}
}

pub trait IsSubType<T: Callable<R>, R> {
fn is_sub_type(&self) -> Option<&CallableCallFor<T, R>>;
pub trait IsSubType<T> {
fn is_sub_type(&self) -> Option<&T>;
}

/// Implement a meta-dispatch module to dispatch to other dispatchers.
Expand Down Expand Up @@ -1948,7 +1948,7 @@ macro_rules! impl_outer_dispatch {
}

$(
impl $crate::dispatch::IsSubType<$camelcase, $runtime> for $call_type {
impl $crate::dispatch::IsSubType<$crate::dispatch::CallableCallFor<$camelcase, $runtime>> for $call_type {
#[allow(unreachable_patterns)]
fn is_sub_type(&self) -> Option<&$crate::dispatch::CallableCallFor<$camelcase, $runtime>> {
match *self {
Expand Down Expand Up @@ -2372,72 +2372,72 @@ mod tests {
}

const EXPECTED_METADATA: &'static [FunctionMetadata] = &[
FunctionMetadata {
name: DecodeDifferent::Encode("aux_0"),
arguments: DecodeDifferent::Encode(&[]),
documentation: DecodeDifferent::Encode(&[
" Hi, this is a comment."
])
},
FunctionMetadata {
name: DecodeDifferent::Encode("aux_1"),
arguments: DecodeDifferent::Encode(&[
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data"),
ty: DecodeDifferent::Encode("Compact<u32>")
}
]),
documentation: DecodeDifferent::Encode(&[]),
},
FunctionMetadata {
name: DecodeDifferent::Encode("aux_2"),
arguments: DecodeDifferent::Encode(&[
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data"),
ty: DecodeDifferent::Encode("i32"),
},
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data2"),
ty: DecodeDifferent::Encode("String"),
}
]),
documentation: DecodeDifferent::Encode(&[]),
},
FunctionMetadata {
name: DecodeDifferent::Encode("aux_3"),
arguments: DecodeDifferent::Encode(&[]),
documentation: DecodeDifferent::Encode(&[]),
},
FunctionMetadata {
name: DecodeDifferent::Encode("aux_4"),
arguments: DecodeDifferent::Encode(&[
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data"),
ty: DecodeDifferent::Encode("i32"),
}
]),
documentation: DecodeDifferent::Encode(&[]),
},
FunctionMetadata {
name: DecodeDifferent::Encode("aux_5"),
arguments: DecodeDifferent::Encode(&[
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data"),
ty: DecodeDifferent::Encode("i32"),
},
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data2"),
ty: DecodeDifferent::Encode("Compact<u32>")
}
]),
documentation: DecodeDifferent::Encode(&[]),
FunctionMetadata {
name: DecodeDifferent::Encode("aux_0"),
arguments: DecodeDifferent::Encode(&[]),
documentation: DecodeDifferent::Encode(&[
" Hi, this is a comment."
])
},
FunctionMetadata {
name: DecodeDifferent::Encode("aux_1"),
arguments: DecodeDifferent::Encode(&[
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data"),
ty: DecodeDifferent::Encode("Compact<u32>")
}
]),
documentation: DecodeDifferent::Encode(&[]),
},
FunctionMetadata {
name: DecodeDifferent::Encode("aux_2"),
arguments: DecodeDifferent::Encode(&[
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data"),
ty: DecodeDifferent::Encode("i32"),
},
FunctionMetadata {
name: DecodeDifferent::Encode("operational"),
arguments: DecodeDifferent::Encode(&[]),
documentation: DecodeDifferent::Encode(&[]),
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data2"),
ty: DecodeDifferent::Encode("String"),
}
]),
documentation: DecodeDifferent::Encode(&[]),
},
FunctionMetadata {
name: DecodeDifferent::Encode("aux_3"),
arguments: DecodeDifferent::Encode(&[]),
documentation: DecodeDifferent::Encode(&[]),
},
FunctionMetadata {
name: DecodeDifferent::Encode("aux_4"),
arguments: DecodeDifferent::Encode(&[
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data"),
ty: DecodeDifferent::Encode("i32"),
}
]),
documentation: DecodeDifferent::Encode(&[]),
},
FunctionMetadata {
name: DecodeDifferent::Encode("aux_5"),
arguments: DecodeDifferent::Encode(&[
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data"),
ty: DecodeDifferent::Encode("i32"),
},
];
FunctionArgumentMetadata {
name: DecodeDifferent::Encode("_data2"),
ty: DecodeDifferent::Encode("Compact<u32>")
}
]),
documentation: DecodeDifferent::Encode(&[]),
},
FunctionMetadata {
name: DecodeDifferent::Encode("operational"),
arguments: DecodeDifferent::Encode(&[]),
documentation: DecodeDifferent::Encode(&[]),
},
];

pub struct TraitImpl {}

Expand Down
Loading

0 comments on commit c5368a1

Please sign in to comment.