Repository: paritytech/substrate
File: frame/utility/src/lib.rs (Rust)

Code before:
		/// # <weight>
		/// - O(1).
		/// - Limited storage reads.
		/// - One DB write (event).
		/// - Weight of derivative `call` execution + 10,000.
		/// # </weight>
		#[pallet::weight({
			let dispatch_info = call.get_dispatch_info();
			(
				dispatch_info.weight
					.saturating_add(10_000)
					// AccountData for inner call origin accountdata.
					.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
				dispatch_info.class,
			)
		})]
		pub fn dispatch_as(
			origin: OriginFor<T>,
			as_origin: T::PalletsOrigin,
			call: Box<<T as Config>::Call>,
		) -> DispatchResult {
			ensure_root(origin)?;

			let res = call.dispatch_bypass_filter(as_origin.into());

			Self::deposit_event(Event::DsipatchAsDone(res.map(|_| ()).map_err(|e| e.error)));
			// Sudo user does not pay a fee.
			Ok(())
		}
	}
}

/// A pallet identifier. These are per pallet and should be stored in a registry somewhere.
#[derive(Clone, Copy, Eq, PartialEq, Encode, Decode)]
struct IndexedUtilityPalletId(u16);

impl TypeId for IndexedUtilityPalletId {
	const TYPE_ID: [u8; 4] = *b"suba";
}

impl<T: Config> Pallet<T> {
	/// Derive a derivative account ID from the owner account and the sub-account index.
	pub fn derivative_account_id(who: T::AccountId, index: u16) -> T::AccountId {
		let entropy = (b"modlpy/utilisuba", who, index).using_encoded(blake2_256);
		T::AccountId::decode(&mut &entropy[..]).unwrap_or_default()
	}
}


Review comment (suggestion):
```suggestion
			Self::deposit_event(Event::DispatchedAs(res.map(|_| ()).map_err(|e| e.error)));
```

Code after:
				};
				// Add the weight of this call.
				weight = weight.saturating_add(extract_actual_weight(&result, &info));
				result.map_err(|mut err| {
					// Take the weight of this function itself into account.
					let base_weight = T::WeightInfo::batch_all(index.saturating_add(1) as u32);
					// Return the actual used weight + base_weight of this call.
					err.post_info = Some(base_weight + weight).into();
					err
				})?;
				Self::deposit_event(Event::ItemCompleted);
			}
			Self::deposit_event(Event::BatchCompleted);
			let base_weight = T::WeightInfo::batch_all(calls_len as u32);
			Ok(Some(base_weight + weight).into())
		}

		/// Dispatches a function call with a provided origin.
		///
		/// The dispatch origin for this call must be _Root_.
		///
		/// # <weight>
		/// - O(1).
		/// - Limited storage reads.
		/// - One DB write (event).
		/// - Weight of derivative `call` execution + T::WeightInfo::dispatch_as().
		/// # </weight>
		#[pallet::weight({
			let dispatch_info = call.get_dispatch_info();
			(
				T::WeightInfo::dispatch_as()
					.saturating_add(dispatch_info.weight),
				dispatch_info.class,
			)
		})]
		pub fn dispatch_as(
			origin: OriginFor<T>,
			as_origin: Box<T::PalletsOrigin>,
			call: Box<<T as Config>::Call>,
		) -> DispatchResult {
			ensure_root(origin)?;

			let res = call.dispatch_bypass_filter((*as_origin).into());

			Self::deposit_event(Event::DispatchedAs(res.map(|_| ()).map_err(|e| e.error)));
			Ok(())
		}
	}
}

/// A pallet identifier. These are per pallet and should be stored in a registry somewhere.