git.delta.rocks / unique-network / refs/commits / 011ebf22fd46

difftreelog

source

pallets/common/src/dispatch.rs2.0 KiBsourcehistory
1use frame_support::{2	dispatch::{3		DispatchResultWithPostInfo, PostDispatchInfo, Weight, DispatchErrorWithPostInfo,4		DispatchResult,5	},6	weights::Pays,7	traits::Get,8};9use up_data_structs::{CollectionId, CreateCollectionData};1011use crate::{pallet::Config, CommonCollectionOperations, CollectionHandle};1213// TODO: move to benchmarking14/// Price of [`dispatch_tx`] call with noop `call` argument15pub fn dispatch_weight<T: Config>() -> Weight {16	// Read collection17	<T as frame_system::Config>::DbWeight::get().reads(1)18	// Dynamic dispatch?19	+ 6_000_00020	// submit_logs is measured as part of collection pallets21}2223/// Helper function to implement substrate calls for common collection methods24pub fn dispatch_tx<25	T: Config,26	C: FnOnce(&dyn CommonCollectionOperations<T>) -> DispatchResultWithPostInfo,27>(28	collection: CollectionId,29	call: C,30) -> DispatchResultWithPostInfo {31	let handle =32		CollectionHandle::try_get(collection).map_err(|error| DispatchErrorWithPostInfo {33			post_info: PostDispatchInfo {34				actual_weight: Some(dispatch_weight::<T>()),35				pays_fee: Pays::Yes,36			},37			error,38		})?;39	handle40		.check_is_internal()41		.map_err(|error| DispatchErrorWithPostInfo {42			post_info: PostDispatchInfo {43				actual_weight: Some(dispatch_weight::<T>()),44				pays_fee: Pays::Yes,45			},46			error,47		})?;48	let dispatched = T::CollectionDispatch::dispatch(handle);49	let mut result = call(dispatched.as_dyn());50	match &mut result {51		Ok(PostDispatchInfo {52			actual_weight: Some(weight),53			..54		})55		| Err(DispatchErrorWithPostInfo {56			post_info: PostDispatchInfo {57				actual_weight: Some(weight),58				..59			},60			..61		}) => *weight += dispatch_weight::<T>(),62		_ => {}63	}64	result65}6667pub trait CollectionDispatch<T: Config> {68	fn create(69		sender: T::CrossAccountId,70		data: CreateCollectionData<T::AccountId>,71	) -> DispatchResult;72	fn destroy(sender: T::CrossAccountId, handle: CollectionHandle<T>) -> DispatchResult;7374	fn dispatch(handle: CollectionHandle<T>) -> Self;75	fn into_inner(self) -> CollectionHandle<T>;7677	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;78}