git.delta.rocks / unique-network / refs/commits / 950d276ffd36

difftreelog

source

pallets/maintenance/src/lib.rs4.0 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819pub use pallet::*;2021#[cfg(feature = "runtime-benchmarks")]22pub mod benchmarking;2324pub mod weights;2526#[frame_support::pallet]27pub mod pallet {28	use frame_support::{dispatch::*, pallet_prelude::*};29	use frame_support::{30		traits::{QueryPreimage, StorePreimage},31	};32	use frame_system::pallet_prelude::*;33	use sp_core::H256;3435	use crate::weights::WeightInfo;3637	#[pallet::config]38	pub trait Config: frame_system::Config {39		/// The overarching event type.40		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;4142		/// The runtime origin type.43		type RuntimeOrigin: From<RawOrigin<Self::AccountId>>44			+ IsType<<Self as frame_system::Config>::RuntimeOrigin>;4546		/// The aggregated call type.47		type RuntimeCall: Parameter48			+ Dispatchable<49				RuntimeOrigin = <Self as Config>::RuntimeOrigin,50				PostInfo = PostDispatchInfo,51			> + GetDispatchInfo52			+ From<frame_system::Call<Self>>;5354		/// The preimage provider with which we look up call hashes to get the call.55		type Preimages: QueryPreimage + StorePreimage;5657		/// Weight information for extrinsics in this pallet.58		type WeightInfo: WeightInfo;59	}6061	#[pallet::event]62	#[pallet::generate_deposit(pub(super) fn deposit_event)]63	pub enum Event<T: Config> {64		MaintenanceEnabled,65		MaintenanceDisabled,66	}6768	#[pallet::pallet]69	pub struct Pallet<T>(_);7071	#[pallet::storage]72	#[pallet::getter(fn is_enabled)]73	pub type Enabled<T> = StorageValue<_, bool, ValueQuery>;7475	#[pallet::error]76	pub enum Error<T> {}7778	#[pallet::call]79	impl<T: Config> Pallet<T> {80		#[pallet::call_index(0)]81		#[pallet::weight(<T as Config>::WeightInfo::enable())]82		pub fn enable(origin: OriginFor<T>) -> DispatchResult {83			ensure_root(origin)?;8485			<Enabled<T>>::set(true);8687			Self::deposit_event(Event::MaintenanceEnabled);8889			Ok(())90		}9192		#[pallet::call_index(1)]93		#[pallet::weight(<T as Config>::WeightInfo::disable())]94		pub fn disable(origin: OriginFor<T>) -> DispatchResult {95			ensure_root(origin)?;9697			<Enabled<T>>::set(false);9899			Self::deposit_event(Event::MaintenanceDisabled);100101			Ok(())102		}103104		/// Execute a runtime call stored as a preimage.105		///106		/// `weight_bound` is the maximum weight that the caller is willing107		/// to allow the extrinsic to be executed with.108		#[pallet::call_index(2)]109		#[pallet::weight(<T as Config>::WeightInfo::execute_preimage() + *weight_bound)]110		pub fn execute_preimage(111			origin: OriginFor<T>,112			hash: H256,113			weight_bound: Weight,114		) -> DispatchResultWithPostInfo {115			use codec::Decode;116117			ensure_root(origin)?;118119			let data = T::Preimages::fetch(&hash, None)?;120			weight_bound.set_proof_size(121				weight_bound122					.proof_size()123					.checked_sub(124						data.len()125							.try_into()126							.map_err(|_| DispatchError::Corruption)?,127					)128					.ok_or(DispatchError::Exhausted)?,129			);130131			let call = <T as Config>::RuntimeCall::decode(&mut &data[..])132				.map_err(|_| DispatchError::Corruption)?;133134			ensure!(135				call.get_dispatch_info().weight.all_lte(weight_bound),136				DispatchError::Exhausted137			);138139			match call.dispatch(frame_system::RawOrigin::Root.into()) {140				Ok(_) => Ok(Pays::No.into()),141				Err(error_and_info) => Err(DispatchErrorWithPostInfo {142					post_info: Pays::No.into(),143					error: error_and_info.error,144				}),145			}146		}147	}148}