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

Code before:
	/// The following assertions must always apply.
	///
	/// 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:
	///
	/// * The submission deposit cannot be less than `T::SubmissionDeposit`.
	/// * There must exist track info for the track of the referendum.
	/// * The decision deposit if provided cannot be less than the decision deposit of the track.
	/// * The deciding stage has to begin before confirmation period.
	///
	/// - Data in `ReferendumInfo::Approved`, `ReferendumInfo::Rejected`,
	///   `ReferendumInfo::Cancelled` and `ReferendumInfo::TimedOut`:
	///
	/// * The submission deposit cannot be less than `T::SubmissionDeposit`.
	///
	/// Looking at tracks:
	/// * The `TrackQueue` should be empty if `DecidingCount` is less than
	///   `TrackInfo::max_deciding`.
	/// * 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() -> DispatchResult {
		use sp_runtime::traits::Bounded;

		ensure!(
			ReferendumCount::<T, I>::get() as usize ==
				ReferendumInfoFor::<T, I>::iter_keys().count(),
			DispatchError::Other(
				"Number of referenda in `ReferendumInfoFor` is greater than `ReferendumCount`"
			)
		);

		// The order of indices when running `queueing_works` is: 0, 4, 2, 1, 3 🤔 
		/*
		ensure!(
			ReferendumInfoFor::<T, I>::iter_keys()
				.collect::<Vec<_>>()
				.windows(2)
				.all(|w| w[0] < w[1]),
			DispatchError::Other(
				"Referenda indices must be sorted in `ReferendumInfoFor` storage map"
			)
		);
		*/

Review comment (suggestion):
```suggestion
	/// * The referendum indices stored in `TrackQueue` must exist as keys in the `ReferendumInfoFor`
```

Code after:
		let x = Perbill::from_rational(elapsed.min(period), period);
		support_needed.passing(x, tally.support(id)) &&
			approval_needed.passing(x, tally.approval(id))
	}

	/// Clear metadata if exist for a given referendum index.
	fn do_clear_metadata(index: ReferendumIndex) {
		if let Some(hash) = MetadataOf::<T, I>::take(index) {
			Self::deposit_event(Event::<T, I>::MetadataCleared { index, hash });
		}
	}

	/// Ensure the correctness of the state of this pallet.
	///
	/// The following assertions must always apply.
	///
	/// General assertions:
	///
	/// * [`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.