Skip to content

Commit

Permalink
1953 defensive testing extrinsic (paritytech#1998)
Browse files Browse the repository at this point in the history
# Description

The `trigger_defensive` call has been added to the `root-testing`
pallet. The idea is to have this pallet running on `Rococo/Westend` and
use it to verify if the runtime monitoring works end-to-end.

To accomplish this, `trigger_defensive` dispatches an event when it is
called.

Closes paritytech#1953

# Checklist

- [x] My PR includes a detailed description as outlined in the
"Description" section above
- [ ] My PR follows the [labeling requirements](CONTRIBUTING.md#Process)
of this project (at minimum one label for `T`
  required)
- [ ] I have made corresponding changes to the documentation (if
applicable)
- [ ] I have added tests that prove my fix is effective or that my
feature works (if applicable)

You can remove the "Checklist" section once all have been checked. Thank
you for your contribution!

✄
-----------------------------------------------------------------------------

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
  • Loading branch information
adelarja and ggwpez authored Oct 31, 2023
1 parent 252de4d commit a175f84
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
4 changes: 3 additions & 1 deletion substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,9 @@ impl pallet_remark::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}

impl pallet_root_testing::Config for Runtime {}
impl pallet_root_testing::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}

parameter_types! {
pub const LaunchPeriod: BlockNumber = 28 * 24 * 60 * MINUTES;
Expand Down
24 changes: 21 additions & 3 deletions substrate/frame/root-testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::dispatch::DispatchResult;
use sp_runtime::Perbill;
use frame_support::{dispatch::DispatchResult, sp_runtime::Perbill};

pub use pallet::*;

Expand All @@ -36,11 +35,21 @@ pub mod pallet {
use frame_system::pallet_prelude::*;

#[pallet::config]
pub trait Config: frame_system::Config {}
pub trait Config: frame_system::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Event dispatched when the trigger_defensive extrinsic is called.
DefensiveTestCall,
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// A dispatch that will fill the block weight up to the given ratio.
Expand All @@ -50,5 +59,14 @@ pub mod pallet {
ensure_root(origin)?;
Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight(0)]
pub fn trigger_defensive(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
frame_support::defensive!("root_testing::trigger_defensive was called.");
Self::deposit_event(Event::DefensiveTestCall);
Ok(())
}
}
}
6 changes: 3 additions & 3 deletions substrate/frame/support/src/traits/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ impl VariantCount for () {
macro_rules! defensive {
() => {
frame_support::__private::log::error!(
target: "runtime",
target: "runtime::defensive",
"{}",
$crate::traits::DEFENSIVE_OP_PUBLIC_ERROR
);
debug_assert!(false, "{}", $crate::traits::DEFENSIVE_OP_INTERNAL_ERROR);
};
($error:expr $(,)?) => {
frame_support::__private::log::error!(
target: "runtime",
target: "runtime::defensive",
"{}: {:?}",
$crate::traits::DEFENSIVE_OP_PUBLIC_ERROR,
$error
Expand All @@ -72,7 +72,7 @@ macro_rules! defensive {
};
($error:expr, $proof:expr $(,)?) => {
frame_support::__private::log::error!(
target: "runtime",
target: "runtime::defensive",
"{}: {:?}: {:?}",
$crate::traits::DEFENSIVE_OP_PUBLIC_ERROR,
$error,
Expand Down
6 changes: 4 additions & 2 deletions substrate/frame/utility/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ frame_support::construct_runtime!(
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
Timestamp: pallet_timestamp::{Call, Inherent},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
RootTesting: pallet_root_testing::{Pallet, Call, Storage},
RootTesting: pallet_root_testing::{Pallet, Call, Storage, Event<T>},
Council: pallet_collective::<Instance1>,
Utility: utility::{Pallet, Call, Event},
Example: example::{Pallet, Call},
Expand Down Expand Up @@ -187,7 +187,9 @@ impl pallet_balances::Config for Test {
type MaxHolds = ();
}

impl pallet_root_testing::Config for Test {}
impl pallet_root_testing::Config for Test {
type RuntimeEvent = RuntimeEvent;
}

impl pallet_timestamp::Config for Test {
type Moment = u64;
Expand Down

0 comments on commit a175f84

Please sign in to comment.