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
Fix session phase in early-exit #453
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
82bf203
Fix up session phase.
gavofyork 4d4ef21
Merge branch 'master' of github.com:paritytech/polkadot
gavofyork 999680a
Version bump
gavofyork f05d205
Fix session rotation properly and add test
gavofyork 03cad51
Update runtimes
gavofyork 7d3dd9c
Docs
gavofyork 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
Binary file modified
BIN
+356 Bytes
(100%)
demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm
Binary file not shown.
Binary file modified
BIN
+386 Bytes
(100%)
demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm
Binary file not shown.
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
Binary file modified
BIN
+475 Bytes
(100%)
polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.compact.wasm
Binary file not shown.
Binary file modified
BIN
+502 Bytes
(100%)
polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.wasm
Binary file not shown.
Binary file modified
BIN
-111 Bytes
(100%)
substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm
Binary file not shown.
Binary file modified
BIN
-30 Bytes
(100%)
substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.wasm
Binary file not shown.
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 |
|---|---|---|
|
|
@@ -79,6 +79,7 @@ decl_module! { | |
| fn force_new_session(normal_rotation: bool) -> Result = 1; | ||
| } | ||
| } | ||
|
|
||
| decl_storage! { | ||
| trait Store for Module<T: Trait>; | ||
|
|
||
|
|
@@ -93,6 +94,8 @@ decl_storage! { | |
| // Percent by which the session must necessarily finish late before we early-exit the session. | ||
| pub BrokenPercentLate get(broken_percent_late): b"ses:broken_percent_late" => required T::Moment; | ||
|
|
||
| // New session is being forced. | ||
| pub ForcingNewSession get(forcing_new_session): b"ses:forcing_new_session" => bool; | ||
| // Block at which the session length last changed. | ||
| LastLengthChange: b"ses:llc" => T::BlockNumber; | ||
| // The next key for a given validator. | ||
|
|
@@ -127,8 +130,8 @@ impl<T: Trait> Module<T> { | |
| } | ||
|
|
||
| /// Forces a new session. | ||
| fn force_new_session(normal_rotation: bool) -> Result { | ||
| Self::rotate_session(normal_rotation); | ||
| pub fn force_new_session(normal_rotation: bool) -> Result { | ||
| <ForcingNewSession<T>>::put(normal_rotation); | ||
| Ok(()) | ||
| } | ||
|
|
||
|
|
@@ -153,13 +156,16 @@ impl<T: Trait> Module<T> { | |
| let block_number = <system::Module<T>>::block_number(); | ||
| let is_final_block = ((block_number - Self::last_length_change()) % Self::length()).is_zero(); | ||
| let broken_validation = Self::broken_validation(); | ||
| if is_final_block || broken_validation { | ||
| Self::rotate_session(!broken_validation); | ||
| if let Some(normal_rotation) = Self::forcing_new_session() { | ||
| Self::rotate_session(normal_rotation, is_final_block); | ||
|
Contributor
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. Not sure about how
Member
Author
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. the idea is that if you use if |
||
| <ForcingNewSession<T>>::kill(); | ||
| } else if is_final_block || broken_validation { | ||
| Self::rotate_session(!broken_validation, is_final_block); | ||
| } | ||
| } | ||
|
|
||
| /// Move onto next session: register the new authority set. | ||
| pub fn rotate_session(normal_rotation: bool) { | ||
| pub fn rotate_session(normal_rotation: bool, is_final_block: bool) { | ||
| let now = <timestamp::Module<T>>::get(); | ||
| let time_elapsed = now.clone() - Self::current_start(); | ||
|
|
||
|
|
@@ -168,9 +174,14 @@ impl<T: Trait> Module<T> { | |
| <CurrentStart<T>>::put(now); | ||
|
|
||
| // Enact era length change. | ||
| if let Some(next_len) = <NextSessionLength<T>>::take() { | ||
| let block_number = <system::Module<T>>::block_number(); | ||
| let len_changed = if let Some(next_len) = <NextSessionLength<T>>::take() { | ||
| <SessionLength<T>>::put(next_len); | ||
| true | ||
| } else { | ||
| false | ||
| }; | ||
| if len_changed || !is_final_block { | ||
| let block_number = <system::Module<T>>::block_number(); | ||
| <LastLengthChange<T>>::put(block_number); | ||
| } | ||
|
|
||
|
|
@@ -354,6 +365,40 @@ mod tests { | |
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_work_with_early_exit() { | ||
| with_externalities(&mut new_test_ext(), || { | ||
| System::set_block_number(1); | ||
| assert_ok!(Session::set_length(10)); | ||
| assert_eq!(Session::blocks_remaining(), 1); | ||
| Session::check_rotate_session(); | ||
|
|
||
| System::set_block_number(2); | ||
| assert_eq!(Session::blocks_remaining(), 0); | ||
| Session::check_rotate_session(); | ||
| assert_eq!(Session::length(), 10); | ||
|
|
||
| System::set_block_number(7); | ||
| assert_eq!(Session::current_index(), 1); | ||
| assert_eq!(Session::blocks_remaining(), 5); | ||
| assert_ok!(Session::force_new_session(false)); | ||
| Session::check_rotate_session(); | ||
|
|
||
| System::set_block_number(8); | ||
| assert_eq!(Session::current_index(), 2); | ||
| assert_eq!(Session::blocks_remaining(), 9); | ||
| Session::check_rotate_session(); | ||
|
|
||
| System::set_block_number(17); | ||
| assert_eq!(Session::current_index(), 2); | ||
| assert_eq!(Session::blocks_remaining(), 0); | ||
| Session::check_rotate_session(); | ||
|
|
||
| System::set_block_number(18); | ||
| assert_eq!(Session::current_index(), 3); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn session_length_change_should_work() { | ||
| with_externalities(&mut new_test_ext(), || { | ||
|
|
||
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
Binary file modified
BIN
-68 Bytes
(100%)
...st-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm
Binary file not shown.
Binary file modified
BIN
-19 Bytes
(100%)
...trate/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.wasm
Binary file not shown.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be nice if it was documented what actually
boolmeans here and that it actually a tri-state (like empty, true and false)