1
// Copyright (C) Parity Technologies (UK) Ltd.
2
// This file is part of Polkadot.
3

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

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

            
14
// You should have received a copy of the GNU General Public License
15
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! Custom origins for governance interventions.
18

            
19
pub use pallet_custom_origins::*;
20

            
21
#[frame_support::pallet]
22
pub mod pallet_custom_origins {
23
    use frame_support::pallet_prelude::*;
24

            
25
    #[pallet::config]
26
    pub trait Config: frame_system::Config {}
27

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

            
31
    #[derive(
32
        PartialEq,
33
        Eq,
34
        Clone,
35
        MaxEncodedLen,
36
        Encode,
37
        Decode,
38
        TypeInfo,
39
        RuntimeDebug,
40
        DecodeWithMemTracking,
41
    )]
42
    #[pallet::origin]
43
    pub enum Origin {
44
        /// Origin able to dispatch a whitelisted call.
45
        WhitelistedCaller,
46
    }
47

            
48
    macro_rules! decl_unit_ensures {
49
		( $name:ident: $success_type:ty = $success:expr ) => {
50
			pub struct $name;
51
			impl<O: Into<Result<Origin, O>> + From<Origin>>
52
				EnsureOrigin<O> for $name
53
			{
54
				type Success = $success_type;
55
				// Allow unreachable patterns for potentially future origins
56
				#[allow(unreachable_patterns)]
57
2
				fn try_origin(o: O) -> Result<Self::Success, O> {
58
2
					o.into().and_then(|o| match o {
59
2
						Origin::$name => Ok($success),
60
						r => Err(O::from(r)),
61
2
					})
62
2
				}
63
				#[cfg(feature = "runtime-benchmarks")]
64
				fn try_successful_origin() -> Result<O, ()> {
65
					Ok(O::from(Origin::$name))
66
				}
67
			}
68
		};
69
		( $name:ident ) => { decl_unit_ensures! { $name : () = () } };
70
		( $name:ident: $success_type:ty = $success:expr, $( $rest:tt )* ) => {
71
			decl_unit_ensures! { $name: $success_type = $success }
72
			decl_unit_ensures! { $( $rest )* }
73
		};
74
		( $name:ident, $( $rest:tt )* ) => {
75
			decl_unit_ensures! { $name }
76
			decl_unit_ensures! { $( $rest )* }
77
		};
78
		() => {}
79
	}
80
    decl_unit_ensures!(WhitelistedCaller);
81
}