Repository: paritytech/substrate
File: frame/message-queue/src/lib.rs (Rust)

Code before:

	pub fn do_try_state() -> Result<(), &'static str> {
		// Checking memory corruption for BookStateFor
		assert_eq!(
			BookStateFor::<T>::iter_keys().count(),
			BookStateFor::<T>::iter_values().count(),
			"Memory Corruption in BookStateFor"
		);

		// Checking memory corruption for Pages
		assert_eq!(
			Pages::<T>::iter_keys().count(),
			Pages::<T>::iter_values().count(),
			"Memory Corruption in Pages"
		);

		// No state to check
		if ServiceHead::<T>::get().is_none() {
			return Ok(())
		}

		//loop around this origin
		let starting_origin = ServiceHead::<T>::get().unwrap();

		while let Some(head) = Self::bump_service_head(&mut WeightMeter::max_limit()) {
			assert!(
				BookStateFor::<T>::contains_key(&head),
				"Service head must point to an existing book"
			);

			let head_book_state = BookStateFor::<T>::get(&head);
			assert!(
				head_book_state.message_count > 0,
				"There must be some messages if in ReadyRing"
			);
			assert!(head_book_state.size > 0, "There must be some messages if in ReadyRing");
			assert!(
				head_book_state.end > head_book_state.begin,
				"End > Begin if unprocessed messages exists"
			);
			assert!(
				head_book_state.ready_neighbours.is_some(),
				"There must be neighbours if in ReadyRing"
			);

			if head_book_state.ready_neighbours.as_ref().unwrap().next == head {
				assert_eq!(
					head_book_state.ready_neighbours.as_ref().unwrap().prev,
					head,
					"Can only happen if only queue in ReadyRing"
				);

Review comment (suggestion):
This should actually be `ensure!` here and down below - since the function returns an error.  
Sorry for not noticing earlier :see_no_evil:   

Please put your address in the MR description for a tip.

Code after:

	/// Ensure the correctness of state of this pallet.
	///
	/// # Assumptions-
	///
	/// If `serviceHead` points to a ready Queue, then BookState of that Queue has:
	///
	/// * `message_count` > 0
	/// * `size` > 0
	/// * `end` > `begin`
	/// * Some(ready_neighbours)
	/// * If `ready_neighbours.next` == self.origin, then `ready_neighbours.prev` == self.origin
	///   (only queue in ring)
	///
	/// For Pages(begin to end-1) in BookState:
	///
	/// * `remaining` > 0
	/// * `remaining_size` > 0
	/// * `first` <= `last`
	/// * Every page can be decoded into peek_* functions
	#[cfg(any(test, feature = "try-runtime"))]
	pub fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> {
		// Checking memory corruption for BookStateFor
		ensure!(
			BookStateFor::<T>::iter_keys().count() == BookStateFor::<T>::iter_values().count(),
			"Memory Corruption in BookStateFor"
		);
		// Checking memory corruption for Pages
		ensure!(
			Pages::<T>::iter_keys().count() == Pages::<T>::iter_values().count(),
			"Memory Corruption in Pages"
		);

		// No state to check
		if ServiceHead::<T>::get().is_none() {
			return Ok(())
		}

		//loop around this origin
		let starting_origin = ServiceHead::<T>::get().unwrap();

		while let Some(head) = Self::bump_service_head(&mut WeightMeter::max_limit()) {
			ensure!(
				BookStateFor::<T>::contains_key(&head),
				"Service head must point to an existing book"
			);

			let head_book_state = BookStateFor::<T>::get(&head);
			ensure!(
				head_book_state.message_count > 0,
				"There must be some messages if in ReadyRing"