This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add try_state check to Pallet MessageQueue
#13502
Merged
paritytech-processbot
merged 12 commits into
paritytech:master
from
gitofdeepanshu:feat/try-state_message-queue
Aug 14, 2023
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a0c94a4
feat: Add try_state for message_queue
gitofdeepanshu 6a4a26e
Update frame/message-queue/src/lib.rs
gitofdeepanshu 73b8e3d
feat: update try_state, add checks for storage
gitofdeepanshu a9624a3
fix: add try_state builder for remaining tests
gitofdeepanshu d7f3f33
Update frame/message-queue/src/mock.rs
gitofdeepanshu a67f631
chore: assert statement to ensure
gitofdeepanshu 057f6ea
Merge branch 'master' into feat/try-state_message-queue
juangirini 70ea918
Merge remote-tracking branch 'origin/master' into feat/try-state_mess…
ggwpez 3684bc0
Fix tests
ggwpez 1c8da37
Use ensure instead of assert
ggwpez 980249f
Fix function signature and feature gate
ggwpez 0b0567e
Cleanup code
ggwpez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -571,6 +571,11 @@ pub mod pallet { | |
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn try_state(_n: BlockNumberFor<T>) -> Result<(), &'static str> { | ||
| Self::do_try_state() | ||
| } | ||
|
|
||
| /// Check all assumptions about [`crate::Config`]. | ||
| fn integrity_test() { | ||
| assert!(!MaxMessageLenOf::<T>::get().is_zero(), "HeapSize too low"); | ||
|
|
@@ -1085,6 +1090,111 @@ impl<T: Config> Pallet<T> { | |
| ItemExecutionStatus::Executed(is_processed) | ||
| } | ||
|
|
||
| /// Ensure the correctness of state of this pallet. | ||
| /// | ||
| /// # Assumptions- | ||
ggwpez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// | ||
| /// 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 | ||
|
|
||
| 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(()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there are still a few assumptions about books even if they are not in the ready ring, but it is okay for a first version. |
||
| } | ||
|
|
||
| //loop around this origin | ||
| let starting_origin = ServiceHead::<T>::get().unwrap(); | ||
|
|
||
| while let Some(head) = Self::bump_service_head(&mut WeightMeter::max_limit()) { | ||
| assert!( | ||
ggwpez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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" | ||
| ); | ||
| } | ||
|
|
||
| for page_index in head_book_state.begin..head_book_state.end { | ||
| let page = Pages::<T>::get(&head, page_index).unwrap(); | ||
| let remaining_messages = page.remaining; | ||
| let mut counted_remaining_messages = 0; | ||
| assert!( | ||
| remaining_messages > 0.into(), | ||
| "These must be some messages that have not been processed yet!" | ||
| ); | ||
|
|
||
| for i in 0..u32::MAX { | ||
| if let Some((_, processed, _)) = page.peek_index(i as usize) { | ||
| if !processed { | ||
| counted_remaining_messages += 1; | ||
| } | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| assert_eq!( | ||
| remaining_messages, | ||
| counted_remaining_messages.into(), | ||
| "Memory Corruption" | ||
| ); | ||
| } | ||
|
|
||
| if head_book_state.ready_neighbours.as_ref().unwrap().next == starting_origin { | ||
| break | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Print the pages in each queue and the messages in each page. | ||
| /// | ||
| /// Processed messages are prefixed with a `*` and the current `begin`ning page with a `>`. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,6 +285,16 @@ where | |
| ext | ||
| } | ||
|
|
||
| pub fn build_and_execute<T: Config>(test: impl FnOnce() -> ()) | ||
gitofdeepanshu marked this conversation as resolved.
Show resolved
Hide resolved
ggwpez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| where | ||
| <T as frame_system::Config>::BlockNumber: From<u32>, | ||
| { | ||
| new_test_ext::<T>().execute_with(|| { | ||
| test(); | ||
| MessageQueue::do_try_state().unwrap(); | ||
|
||
| }) | ||
| } | ||
|
|
||
| /// Set the weight of a specific weight function. | ||
| pub fn set_weight(name: &str, w: Weight) { | ||
| MockedWeightInfo::set_weight::<Test>(name, w); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.