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
// Originally created by Acala. Modified by GalacticCouncil.

// Copyright (C) 2020-2022 Acala Foundation, GalacticCouncil.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![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>;

        /// The origin which may set filter.
        type UpdateOrigin: EnsureOrigin<Self::Origin>;

        /// Weight information for the extrinsics in this module.
        type WeightInfo: WeightInfo;
    }

    #[pallet::error]
    pub enum Error<T> {
        /// can not pause
        CannotPause,
        /// invalid character encoding
        InvalidCharacter,
    }

    #[pallet::event]
    #[pallet::generate_deposit(fn deposit_event)]
    pub enum Event<T: Config> {
        /// Paused transaction
        TransactionPaused {
            pallet_name_bytes: Vec<u8>,
            function_name_bytes: Vec<u8>,
        },
        /// Unpaused transaction
        TransactionUnpaused {
            pallet_name_bytes: Vec<u8>,
            function_name_bytes: Vec<u8>,
        },
    }

    /// The paused transaction map
    ///
    /// map (PalletNameBytes, FunctionNameBytes) => Option<()>
    #[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)?;

            // not allowed to pause calls of this pallet to ensure safe
            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()))
    }
}