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
im-online: account for block authorship #3973
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
|---|---|---|
|
|
@@ -213,10 +213,15 @@ decl_storage! { | |
| /// The current set of keys that may issue a heartbeat. | ||
| Keys get(fn keys): Vec<T::AuthorityId>; | ||
|
|
||
| /// For each session index we keep a mapping of `AuthorityId` | ||
| /// For each session index, we keep a mapping of `AuthIndex` | ||
| /// to `offchain::OpaqueNetworkState`. | ||
| ReceivedHeartbeats get(fn received_heartbeats): double_map SessionIndex, | ||
| blake2_256(AuthIndex) => Vec<u8>; | ||
|
|
||
| /// For each session index, we keep a mapping of `T::ValidatorId` to the | ||
| /// number of blocks authored by the given authority. | ||
| AuthoredBlocks get(fn authored_blocks): double_map SessionIndex, | ||
| blake2_256(T::ValidatorId) => u32; | ||
| } | ||
| add_extra_genesis { | ||
| config(keys): Vec<T::AuthorityId>; | ||
|
|
@@ -277,14 +282,63 @@ decl_module! { | |
| } | ||
| } | ||
|
|
||
| /// Keep track of number of authored blocks per authority, uncles are counted as | ||
| /// well since they're a valid proof of onlineness. | ||
| impl<T: Trait + authorship::Trait> authorship::EventHandler<T::ValidatorId, T::BlockNumber> for Module<T> { | ||
| fn note_author(author: T::ValidatorId) { | ||
| Self::note_authorship(author); | ||
| } | ||
|
|
||
| fn note_uncle(author: T::ValidatorId, _age: T::BlockNumber) { | ||
| Self::note_authorship(author); | ||
| } | ||
| } | ||
|
|
||
| impl<T: Trait> Module<T> { | ||
| /// Returns `true` if a heartbeat has been received for the authority at | ||
| /// `authority_index` in the authorities series or if the authority has | ||
| /// authored at least one block, during the current session. Otherwise | ||
| /// `false`. | ||
| pub fn is_online_in_current_session(authority_index: AuthIndex) -> bool { | ||
| let current_validators = <session::Module<T>>::validators(); | ||
|
|
||
| if authority_index >= current_validators.len() as u32 { | ||
| return false; | ||
| } | ||
|
|
||
| let authority = ¤t_validators[authority_index as usize]; | ||
|
|
||
| Self::is_online_in_current_session_aux(authority_index, authority) | ||
| } | ||
|
|
||
| fn is_online_in_current_session_aux(authority_index: AuthIndex, authority: &T::ValidatorId) -> bool { | ||
| let current_session = <session::Module<T>>::current_index(); | ||
|
|
||
| <ReceivedHeartbeats>::exists(¤t_session, &authority_index) || | ||
| <AuthoredBlocks<T>>::get( | ||
| ¤t_session, | ||
| authority, | ||
| ) != 0 | ||
| } | ||
|
|
||
| /// Returns `true` if a heartbeat has been received for the authority at `authority_index` in | ||
| /// the authorities series, during the current session. Otherwise `false`. | ||
| pub fn is_online_in_current_session(authority_index: AuthIndex) -> bool { | ||
| pub fn received_heartbeat_in_current_session(authority_index: AuthIndex) -> bool { | ||
| let current_session = <session::Module<T>>::current_index(); | ||
| <ReceivedHeartbeats>::exists(¤t_session, &authority_index) | ||
| } | ||
|
|
||
| /// Note that the given authority has authored a block in the current session. | ||
| fn note_authorship(author: T::ValidatorId) { | ||
| let current_session = <session::Module<T>>::current_index(); | ||
|
|
||
| <AuthoredBlocks<T>>::mutate( | ||
| ¤t_session, | ||
| author, | ||
| |authored| *authored += 1, | ||
| ); | ||
| } | ||
|
|
||
| pub(crate) fn offchain(now: T::BlockNumber) { | ||
| let next_gossip = <GossipAt<T>>::get(); | ||
| let check = Self::check_not_yet_gossipped(now, next_gossip); | ||
|
|
@@ -460,9 +514,7 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> { | |
| let current_validators = <session::Module<T>>::validators(); | ||
|
|
||
| for (auth_idx, validator_id) in current_validators.into_iter().enumerate() { | ||
| let auth_idx = auth_idx as u32; | ||
| let exists = <ReceivedHeartbeats>::exists(¤t_session, &auth_idx); | ||
| if !exists { | ||
| if !Self::is_online_in_current_session_aux(auth_idx as u32, &validator_id) { | ||
| let full_identification = T::FullIdentificationOf::convert(validator_id.clone()) | ||
| .expect( | ||
| "we got the validator_id from current_validators; | ||
|
|
@@ -476,6 +528,12 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> { | |
| } | ||
| } | ||
|
|
||
| // Remove all received heartbeats and number of authored blocks from the | ||
| // current session, they have already been processed and won't be needed | ||
| // anymore. | ||
| <ReceivedHeartbeats>::remove_prefix(&<session::Module<T>>::current_index()); | ||
| <AuthoredBlocks<T>>::remove_prefix(&<session::Module<T>>::current_index()); | ||
|
|
||
| if unresponsive.is_empty() { | ||
| return; | ||
| } | ||
|
|
@@ -488,10 +546,6 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> { | |
| }; | ||
|
|
||
| T::ReportUnresponsiveness::report_offence(vec![], offence); | ||
|
|
||
| // Remove all received heartbeats from the current session, they have | ||
| // already been processed and won't be needed anymore. | ||
| <ReceivedHeartbeats>::remove_prefix(&<session::Module<T>>::current_index()); | ||
|
Contributor
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. This was a bug, we would not cleanup the session heartbeats if there was no offence to report. |
||
| } | ||
|
|
||
| fn on_disabled(_i: usize) { | ||
|
|
||
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
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.
Why
ValidatorIdand notAuthIndextoo?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 guess that would require iterating over
keys- unless we extend the handler to pass index?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.
Yep, that's the reason. We can change the handler although for example for PoW it doesn't make sense to have an index there.