Simplify deposit_event declaration in decl_module (#3506)

* simplify module deposit_event declaration

* fix

* bump version

* fix

* fix test

* fix doc
This commit is contained in:
thiolliere
2019-08-29 14:34:02 +02:00
committed by Bastian Köcher
parent f830db9642
commit b07fd450e2
22 changed files with 43 additions and 67 deletions
@@ -35,7 +35,7 @@ decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { pub struct Module<T: Trait> for enum Call where origin: T::Origin {
// Initializing events // Initializing events
// this is needed only if you are using events in your module // this is needed only if you are using events in your module
fn deposit_event<T>() = default; fn deposit_event() = default;
// Just a dummy entry point. // Just a dummy entry point.
// function that can be called by the external world as an extrinsics call // function that can be called by the external world as an extrinsics call
+1 -1
View File
@@ -80,7 +80,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// implementation changes and behavior does not, then leave spec_version as // implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version. // is and increment impl_version.
spec_version: 154, spec_version: 154,
impl_version: 156, impl_version: 157,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
}; };
+1 -1
View File
@@ -149,7 +149,7 @@ pub trait Trait: system::Trait {
decl_module! { decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default; fn deposit_event() = default;
/// Issue a new class of fungible assets. There are, and will only ever be, `total` /// Issue a new class of fungible assets. There are, and will only ever be, `total`
/// such assets and they'll all belong to the `origin` initially. It will have an /// such assets and they'll all belong to the `origin` initially. It will have an
/// identifier `AssetId` instance: this will be specified in the `Issued` event. /// identifier `AssetId` instance: this will be specified in the `Issued` event.
+1 -1
View File
@@ -413,7 +413,7 @@ decl_module! {
/// The fee to be paid for making a transaction; the per-byte portion. /// The fee to be paid for making a transaction; the per-byte portion.
const TransactionByteFee: T::Balance = T::TransactionByteFee::get(); const TransactionByteFee: T::Balance = T::TransactionByteFee::get();
fn deposit_event<T, I>() = default; fn deposit_event() = default;
/// Transfer some liquid free balance to another account. /// Transfer some liquid free balance to another account.
/// ///
+1 -1
View File
@@ -138,7 +138,7 @@ decl_event!(
// operational class. // operational class.
decl_module! { decl_module! {
pub struct Module<T: Trait<I>, I: Instance=DefaultInstance> for enum Call where origin: <T as system::Trait>::Origin { pub struct Module<T: Trait<I>, I: Instance=DefaultInstance> for enum Call where origin: <T as system::Trait>::Origin {
fn deposit_event<T, I>() = default; fn deposit_event() = default;
/// Set the collective's membership manually to `new_members`. Be nice to the chain and /// Set the collective's membership manually to `new_members`. Be nice to the chain and
/// provide it pre-sorted. /// provide it pre-sorted.
+1 -1
View File
@@ -504,7 +504,7 @@ decl_module! {
/// default value is 10_000_000. /// default value is 10_000_000.
const BlockGasLimit: Gas = T::BlockGasLimit::get(); const BlockGasLimit: Gas = T::BlockGasLimit::get();
fn deposit_event<T>() = default; fn deposit_event() = default;
/// Updates the schedule for metering contracts. /// Updates the schedule for metering contracts.
/// ///
+1 -1
View File
@@ -354,7 +354,7 @@ decl_module! {
/// Period in blocks where an external proposal may not be re-submitted after being vetoed. /// Period in blocks where an external proposal may not be re-submitted after being vetoed.
const CooloffPeriod: T::BlockNumber = T::CooloffPeriod::get(); const CooloffPeriod: T::BlockNumber = T::CooloffPeriod::get();
fn deposit_event<T>() = default; fn deposit_event() = default;
/// Propose a sensitive action to be taken. /// Propose a sensitive action to be taken.
/// ///
+1 -1
View File
@@ -296,7 +296,7 @@ decl_module! {
/// The chunk size of the approval vector. /// The chunk size of the approval vector.
const APPROVAL_SET_SIZE: u32 = APPROVAL_SET_SIZE as u32; const APPROVAL_SET_SIZE: u32 = APPROVAL_SET_SIZE as u32;
fn deposit_event<T>() = default; fn deposit_event() = default;
/// Set candidate approvals. Approval slots stay valid as long as candidates in those slots /// Set candidate approvals. Approval slots stay valid as long as candidates in those slots
/// are registered. /// are registered.
+1 -1
View File
@@ -363,7 +363,7 @@ decl_module! {
/// It is also possible to provide a custom implementation. /// It is also possible to provide a custom implementation.
/// For non-generic events, the generic parameter just needs to be dropped, so that it /// For non-generic events, the generic parameter just needs to be dropped, so that it
/// looks like: `fn deposit_event() = default;`. /// looks like: `fn deposit_event() = default;`.
fn deposit_event<T>() = default; fn deposit_event() = default;
/// This is your public interface. Be extremely careful. /// This is your public interface. Be extremely careful.
/// This is just a simple example of how to interact with the module from the external /// This is just a simple example of how to interact with the module from the external
/// world. /// world.
+1 -1
View File
@@ -324,7 +324,7 @@ impl<AccountId> Into<PermissionVersions<AccountId>> for PermissionLatest<Account
decl_module! { decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default; fn deposit_event() = default;
/// Create a new kind of asset. /// Create a new kind of asset.
fn create(origin, options: AssetOptions<T::Balance, T::AccountId>) -> Result { fn create(origin, options: AssetOptions<T::Balance, T::AccountId>) -> Result {
+1 -1
View File
@@ -238,7 +238,7 @@ decl_storage! {
decl_module! { decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default; fn deposit_event() = default;
fn heartbeat( fn heartbeat(
origin, origin,
+1 -1
View File
@@ -73,7 +73,7 @@ pub trait Trait: system::Trait {
decl_module! { decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default; fn deposit_event() = default;
} }
} }
+1 -1
View File
@@ -99,7 +99,7 @@ decl_module! {
for enum Call for enum Call
where origin: T::Origin where origin: T::Origin
{ {
fn deposit_event<T, I>() = default; fn deposit_event() = default;
/// Add a member `who` to the set. /// Add a member `who` to the set.
/// ///
+1 -1
View File
@@ -228,7 +228,7 @@ decl_module! {
for enum Call for enum Call
where origin: T::Origin where origin: T::Origin
{ {
fn deposit_event<T, I>() = default; fn deposit_event() = default;
/// Every `Period` blocks the `Members` set is refreshed from the /// Every `Period` blocks the `Members` set is refreshed from the
/// highest scoring members in the pool. /// highest scoring members in the pool.
+2 -2
View File
@@ -674,7 +674,7 @@ decl_module! {
/// Number of eras that staked funds must remain bonded for. /// Number of eras that staked funds must remain bonded for.
const BondingDuration: EraIndex = T::BondingDuration::get(); const BondingDuration: EraIndex = T::BondingDuration::get();
fn deposit_event<T>() = default; fn deposit_event() = default;
fn on_finalize() { fn on_finalize() {
// Set the start of the first era. // Set the start of the first era.
@@ -1595,7 +1595,7 @@ impl<T, Reporter, Offender, R, O> ReportOffence<Reporter, Offender, O>
R::report_offence(reporters, offence) R::report_offence(reporters, offence)
} else { } else {
<Module<T>>::deposit_event( <Module<T>>::deposit_event(
RawEvent::OldSlashingReportDiscarded(offence_session).into() RawEvent::OldSlashingReportDiscarded(offence_session)
) )
} }
} }
+1 -1
View File
@@ -106,7 +106,7 @@ pub trait Trait: system::Trait {
decl_module! { decl_module! {
// Simple declaration of the `Module` type. Lets the macro know what it's working on. // Simple declaration of the `Module` type. Lets the macro know what it's working on.
pub struct Module<T: Trait> for enum Call where origin: T::Origin { pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default; fn deposit_event() = default;
/// Authenticates the sudo key and dispatches a function call with `Root` origin. /// Authenticates the sudo key and dispatches a function call with `Root` origin.
/// ///
+10 -37
View File
@@ -203,7 +203,7 @@ impl<T> Parameter for T where T: Codec + Clone + Eq {}
/// * `deposit_event`: Helper function for depositing an [event](https://docs.substrate.dev/docs/event-enum). /// * `deposit_event`: Helper function for depositing an [event](https://docs.substrate.dev/docs/event-enum).
/// The default behavior is to call `deposit_event` from the [System module](../srml_system/index.html). /// The default behavior is to call `deposit_event` from the [System module](../srml_system/index.html).
/// However, you can write your own implementation for events in your runtime. To use the default behavior, /// However, you can write your own implementation for events in your runtime. To use the default behavior,
/// add `fn deposit_event<T>() = default;` to your `Module`. /// add `fn deposit_event() = default;` to your `Module`.
/// ///
/// The following reserved functions also take the block number (with type `T::BlockNumber`) as an optional input: /// The following reserved functions also take the block number (with type `T::BlockNumber`) as an optional input:
/// ///
@@ -288,7 +288,7 @@ macro_rules! decl_module {
{ $( $constants:tt )* } { $( $constants:tt )* }
[ $( $dispatchables:tt )* ] [ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])* $(#[doc = $doc_attr:tt])*
$vis:vis fn deposit_event $(<$dpeg:ident $(, $dpeg_instance:ident)?>)* () = default; $vis:vis fn deposit_event() = default;
$($rest:tt)* $($rest:tt)*
) => { ) => {
$crate::decl_module!(@normalize $crate::decl_module!(@normalize
@@ -296,7 +296,7 @@ macro_rules! decl_module {
pub struct $mod_type<$trait_instance: $trait_name$(<I>, I: $instantiable $(= $module_default_instance)?)?> pub struct $mod_type<$trait_instance: $trait_name$(<I>, I: $instantiable $(= $module_default_instance)?)?>
for enum $call_type where origin: $origin_type, system = $system for enum $call_type where origin: $origin_type, system = $system
{ $( $other_where_bounds )* } { $( $other_where_bounds )* }
{ $vis fn deposit_event $(<$dpeg $(, $dpeg_instance)?>)* () = default; } { $vis fn deposit_event() = default; }
{ $( $on_initialize )* } { $( $on_initialize )* }
{ $( $on_finalize )* } { $( $on_finalize )* }
{ $( $offchain )* } { $( $offchain )* }
@@ -317,23 +317,11 @@ macro_rules! decl_module {
{ $( $constants:tt )* } { $( $constants:tt )* }
[ $( $dispatchables:tt )* ] [ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])* $(#[doc = $doc_attr:tt])*
$vis:vis fn deposit_event $(<$dpeg:ident $(, $dpeg_instance:ident)?>)* ( $vis:vis fn deposit_event
$($param_name:ident : $param:ty),*
) { $( $impl:tt )* }
$($rest:tt)* $($rest:tt)*
) => { ) => {
$crate::decl_module!(@normalize compile_error!(
$(#[$attr])* "`deposit_event` function is reserved and must follow the syntax: `$vis:vis fn deposit_event() = default;`"
pub struct $mod_type<$trait_instance: $trait_name$(<I>, I: $instantiable $(= $module_default_instance)?)?>
for enum $call_type where origin: $origin_type, system = $system
{ $( $other_where_bounds )* }
{ $vis fn deposit_event $(<$dpeg $(, $dpeg_instance)?>)* ($( $param_name: $param ),* ) { $( $impl )* } }
{ $( $on_initialize )* }
{ $( $on_finalize )* }
{ $( $offchain )* }
{ $( $constants )* }
[ $( $dispatchables )* ]
$($rest)*
); );
}; };
(@normalize (@normalize
@@ -685,25 +673,10 @@ macro_rules! decl_module {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?> $module<$trait_instance $(, $instance)?> impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?> $module<$trait_instance $(, $instance)?>
where $( $other_where_bounds )* where $( $other_where_bounds )*
{ {
$vis fn deposit_event(event: Event$(<$event_trait_instance $(, $event_instance)?>)?) { $vis fn deposit_event(
<$system::Module<$trait_instance>>::deposit_event( event: impl Into<< $trait_instance as $trait_name $(<$instance>)? >::Event>
<$trait_instance as $trait_name$(<$instance>)?>::Event::from(event).into() ) {
); <$system::Module<$trait_instance>>::deposit_event(event.into())
}
}
};
(@impl_deposit_event
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
$system:ident;
{ $( $other_where_bounds:tt )* }
$vis:vis fn deposit_event($param:ident : $param_ty:ty) { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?> $module<$trait_instance $(, $instance)?>
where $( $other_where_bounds )*
{
$vis fn deposit_event($param: $param_ty) {
$( $impl )*
} }
} }
}; };
@@ -55,7 +55,7 @@ mod module1 {
{ {
fn offchain_worker() {} fn offchain_worker() {}
fn deposit_event<T, I>() = default; fn deposit_event() = default;
fn one(origin) { fn one(origin) {
system::ensure_root(origin)?; system::ensure_root(origin)?;
@@ -132,7 +132,7 @@ mod module2 {
pub struct Module<T: Trait<I>, I: Instance=DefaultInstance> for enum Call where pub struct Module<T: Trait<I>, I: Instance=DefaultInstance> for enum Call where
origin: <T as system::Trait>::Origin origin: <T as system::Trait>::Origin
{ {
fn deposit_event<T, I>() = default; fn deposit_event() = default;
} }
} }
@@ -38,7 +38,7 @@ error: Invalid call fn name: `offchain_worker`, name is reserved and doesn't mat
| |
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error: Invalid call fn name: `deposit_event`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. error: `deposit_event` function is reserved and must follow the syntax: `$vis:vis fn deposit_event() = default;`
--> $DIR/on_initialize.rs:30:1 --> $DIR/on_initialize.rs:30:1
| |
30 | reserved!(on_finalize on_initialize on_finalise on_initialise offchain_worker deposit_event); 30 | reserved!(on_finalize on_initialize on_finalise on_initialise offchain_worker deposit_event);
+6 -3
View File
@@ -12,8 +12,11 @@ pub trait Trait: 'static + Eq + Clone {
srml_support::decl_module! { srml_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { pub struct Module<T: Trait> for enum Call where origin: T::Origin {
pub fn deposit_event(_event: T::Event) { }
} }
impl<T: Trait> Module<T> {
pub fn deposit_event(_event: impl Into<T::Event>) {
} }
} }
@@ -49,4 +52,4 @@ pub fn ensure_root<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), &'stati
where OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>> where OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>
{ {
o.into().map(|_| ()).map_err(|_| "bad origin: expected to be a root origin") o.into().map(|_| ()).map_err(|_| "bad origin: expected to be a root origin")
} }
+6 -6
View File
@@ -246,11 +246,6 @@ pub type KeyValue = (Vec<u8>, Vec<u8>);
decl_module! { decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// Deposits an event into this block's event record.
pub fn deposit_event(event: T::Event) {
Self::deposit_event_indexed(&[], event);
}
/// A big dispatch that will disallow any other transaction to be included. /// A big dispatch that will disallow any other transaction to be included.
// TODO: this must be preferable available for testing really (not possible at the moment). // TODO: this must be preferable available for testing really (not possible at the moment).
#[weight = SimpleDispatchInfo::MaxOperational] #[weight = SimpleDispatchInfo::MaxOperational]
@@ -545,6 +540,11 @@ pub fn ensure_none<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), &'stati
} }
impl<T: Trait> Module<T> { impl<T: Trait> Module<T> {
/// Deposits an event into this block's event record.
pub fn deposit_event(event: impl Into<T::Event>) {
Self::deposit_event_indexed(&[], event.into());
}
/// Deposits an event into this block's event record adding this event /// Deposits an event into this block's event record adding this event
/// to the corresponding topic indexes. /// to the corresponding topic indexes.
/// ///
@@ -815,7 +815,7 @@ impl<T: Trait> Module<T> {
Self::deposit_event(match r { Self::deposit_event(match r {
Ok(_) => Event::ExtrinsicSuccess, Ok(_) => Event::ExtrinsicSuccess,
Err(_) => Event::ExtrinsicFailed, Err(_) => Event::ExtrinsicFailed,
}.into()); });
let next_extrinsic_index = Self::extrinsic_index().unwrap_or_default() + 1u32; let next_extrinsic_index = Self::extrinsic_index().unwrap_or_default() + 1u32;
+1 -1
View File
@@ -139,7 +139,7 @@ decl_module! {
/// Percentage of spare funds (if any) that are burnt per spend period. /// Percentage of spare funds (if any) that are burnt per spend period.
const Burn: Permill = T::Burn::get(); const Burn: Permill = T::Burn::get();
fn deposit_event<T>() = default; fn deposit_event() = default;
/// Put forward a suggestion for spending. A deposit proportional to the value /// Put forward a suggestion for spending. A deposit proportional to the value
/// is reserved and slashed if the proposal is rejected. It is returned once the /// is reserved and slashed if the proposal is rejected. It is returned once the
/// proposal is awarded. /// proposal is awarded.