1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::unused_unit)]
use frame_support::{
dispatch::{CallMetadata, GetCallMetadata},
pallet_prelude::*,
traits::{Contains, PalletInfoAccess},
};
use frame_system::pallet_prelude::*;
use sp_runtime::DispatchResult;
use sp_std::{prelude::*, vec::Vec};
mod benchmarking;
mod mock;
mod tests;
pub mod weights;
pub use pallet::*;
pub use weights::WeightInfo;
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type UpdateOrigin: EnsureOrigin<Self::Origin>;
type WeightInfo: WeightInfo;
}
#[pallet::error]
pub enum Error<T> {
CannotPause,
InvalidCharacter,
}
#[pallet::event]
#[pallet::generate_deposit(fn deposit_event)]
pub enum Event<T: Config> {
TransactionPaused {
pallet_name_bytes: Vec<u8>,
function_name_bytes: Vec<u8>,
},
TransactionUnpaused {
pallet_name_bytes: Vec<u8>,
function_name_bytes: Vec<u8>,
},
}
#[pallet::storage]
#[pallet::getter(fn paused_transactions)]
pub type PausedTransactions<T: Config> = StorageMap<_, Twox64Concat, (Vec<u8>, Vec<u8>), (), OptionQuery>;
#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(T::WeightInfo::pause_transaction())]
pub fn pause_transaction(origin: OriginFor<T>, pallet_name: Vec<u8>, function_name: Vec<u8>) -> DispatchResult {
T::UpdateOrigin::ensure_origin(origin)?;
let pallet_name_string = sp_std::str::from_utf8(&pallet_name).map_err(|_| Error::<T>::InvalidCharacter)?;
ensure!(
pallet_name_string != <Self as PalletInfoAccess>::name(),
Error::<T>::CannotPause
);
PausedTransactions::<T>::mutate_exists((pallet_name.clone(), function_name.clone()), |maybe_paused| {
if maybe_paused.is_none() {
*maybe_paused = Some(());
Self::deposit_event(Event::TransactionPaused {
pallet_name_bytes: pallet_name,
function_name_bytes: function_name,
});
}
});
Ok(())
}
#[pallet::weight(T::WeightInfo::unpause_transaction())]
pub fn unpause_transaction(
origin: OriginFor<T>,
pallet_name: Vec<u8>,
function_name: Vec<u8>,
) -> DispatchResult {
T::UpdateOrigin::ensure_origin(origin)?;
if PausedTransactions::<T>::take((&pallet_name, &function_name)).is_some() {
Self::deposit_event(Event::TransactionUnpaused {
pallet_name_bytes: pallet_name,
function_name_bytes: function_name,
});
};
Ok(())
}
}
}
pub struct PausedTransactionFilter<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> Contains<T::Call> for PausedTransactionFilter<T>
where
<T as frame_system::Config>::Call: GetCallMetadata,
{
fn contains(call: &T::Call) -> bool {
let CallMetadata {
function_name,
pallet_name,
} = call.get_call_metadata();
PausedTransactions::<T>::contains_key((pallet_name.as_bytes(), function_name.as_bytes()))
}
}