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

Code before:
	///
	/// General assertions:
	///
	/// * `ReferendumCount` must always be equal to the number of referenda in `ReferendumInfoFor`.
	/// * Referendum indices must be sorted in the `ReferendumInfoOf` storage map.
	/// * Referendum indices in `MetadataOf` must also be stored in `ReferendumInfoOf`.
	///
	/// Looking at referenda info:
	///
	/// - Data regarding ongoing phase:
	///
	/// * There must exist track info for the track of the referendum.
	/// * The deciding stage has to begin before confirmation period.
	/// * If alarm is set the nudge call has to be at most `UndecidingTimeout` blocks away
	///  from the submission block.
	///
	/// Looking at tracks:
	/// * The referendum indices stored in `TrackQueue` must exist as keys in the
	///   `ReferendumInfoFor`
	///  storage map.
	#[cfg(any(feature = "try-runtime", test))]
	fn do_try_state() -> Result<(), TryRuntimeError> {
		ensure!(
			ReferendumCount::<T, I>::get() as usize ==
				ReferendumInfoFor::<T, I>::iter_keys().count(),
			"Number of referenda in `ReferendumInfoFor` is greater than `ReferendumCount`"
		);

		MetadataOf::<T, I>::iter_keys().try_for_each(|referendum_index| -> DispatchResult {
			ensure!(
				ReferendumInfoFor::<T, I>::contains_key(referendum_index),
				"Referendum indices in `MetadataOf` must also be stored in `ReferendumInfoOf`"
			);
			Ok(())
		})?;

		ReferendumInfoFor::<T, I>::iter().try_for_each(|(_, referendum)| -> DispatchResult {
			match referendum {
				ReferendumInfo::Ongoing(status) => {
					ensure!(
						Self::track(status.track).is_some(),
						"No track info for the track of the referendum."
					);

					if let Some(deciding) = status.deciding {
						ensure!(
							deciding.since <
								deciding.confirming.unwrap_or(T::BlockNumber::max_value()),
								"Deciding status cannot begin before confirming stage."
						)
					}

Review comment (suggestion):
```suggestion
			"Number of referenda in `ReferendumInfoFor` is different than `ReferendumCount`"
```

Code after:
	///
	/// * [`ReferendumCount`] must always be equal to the number of referenda in
	///   [`ReferendumInfoFor`].
	/// * Referendum indices in [`MetadataOf`] must also be stored in [`ReferendumInfoFor`].
	#[cfg(any(feature = "try-runtime", test))]
	fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> {
		ensure!(
			ReferendumCount::<T, I>::get() as usize ==
				ReferendumInfoFor::<T, I>::iter_keys().count(),
			"Number of referenda in `ReferendumInfoFor` is different than `ReferendumCount`"
		);

		MetadataOf::<T, I>::iter_keys().try_for_each(|referendum_index| -> DispatchResult {
			ensure!(
				ReferendumInfoFor::<T, I>::contains_key(referendum_index),
				"Referendum indices in `MetadataOf` must also be stored in `ReferendumInfoOf`"
			);
			Ok(())
		})?;

		Self::try_state_referenda_info()?;
		Self::try_state_tracks()?;

		Ok(())
	}

	/// Looking at referenda info:
	///
	/// - Data regarding ongoing phase:
	///
	/// * There must exist track info for the track of the referendum.
	/// * The deciding stage has to begin before confirmation period.
	/// * If alarm is set the nudge call has to be at most [`UndecidingTimeout`] blocks away
	///  from the submission block.
	#[cfg(any(feature = "try-runtime", test))]
	fn try_state_referenda_info() -> Result<(), sp_runtime::TryRuntimeError> {
		ReferendumInfoFor::<T, I>::iter().try_for_each(|(_, referendum)| {
			match referendum {
				ReferendumInfo::Ongoing(status) => {
					ensure!(
						Self::track(status.track).is_some(),
						"No track info for the track of the referendum."
					);

					if let Some(deciding) = status.deciding {
						ensure!(
							deciding.since <
								deciding.confirming.unwrap_or(BlockNumberFor::<T>::max_value()),
							"Deciding status cannot begin before confirming stage."
						)
					}