-
Notifications
You must be signed in to change notification settings - Fork 67
A0-1820: implement substrate specific verifier for sync protocol #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9df5e16
Small refactor to prepare for verifier
maciejnems fc19a31
move justification verification to sync folder
maciejnems 9181a68
implement SessionVerifier cache
maciejnems d5fd63a
implement verifier for VerifierCache
maciejnems e48de41
simplify names of cache methods
maciejnems ce76b48
Merge branch 'main' into A0-1820-implement-substrate-verifier
maciejnems 0fcd864
fix clippy
maciejnems 95c8178
Merge branch 'main' into A0-1820-implement-substrate-verifier
maciejnems 7fd6ce7
apply renaming/comment related changes
maciejnems 4d84d96
add entry approach
maciejnems 444e177
minor cosmetic change
maciejnems 32580a9
Merge branch 'main' into A0-1820-implement-substrate-verifier
maciejnems 228deba
more expressive error message
maciejnems 4212caf
change to usize
maciejnems 6af0a26
Merge branch 'main' into A0-1820-implement-substrate-verifier
maciejnems 9cc2c9d
Merge branch 'main' into A0-1820-implement-substrate-verifier
maciejnems 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
move justification verification to sync folder
- Loading branch information
commit fc19a316da4290731c5274cef87133bf596d9c21
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
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| mod verifier; | ||
|
|
||
| pub use verifier::SessionVerifier; |
90 changes: 90 additions & 0 deletions
90
finality-aleph/src/sync/substrate/verification/verifier.rs
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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| use std::fmt::{Display, Error as FmtError, Formatter}; | ||
|
|
||
| use aleph_primitives::SessionAuthorityData; | ||
| use codec::Encode; | ||
| use log::warn; | ||
| use sp_runtime::{traits::Block as BlockT, RuntimeAppPublic}; | ||
|
|
||
| use crate::{ | ||
| crypto::AuthorityVerifier, | ||
| justification::{AlephJustification, Verifier as LegacyVerifier}, | ||
| AuthorityId, | ||
| }; | ||
|
|
||
| /// A justification verifier within a single session. | ||
| #[derive(Clone, PartialEq, Debug)] | ||
| pub struct SessionVerifier { | ||
| authority_verifier: AuthorityVerifier, | ||
| emergency_signer: Option<AuthorityId>, | ||
| } | ||
|
|
||
| impl From<SessionAuthorityData> for SessionVerifier { | ||
| fn from(authority_data: SessionAuthorityData) -> Self { | ||
| SessionVerifier { | ||
| authority_verifier: AuthorityVerifier::new(authority_data.authorities().to_vec()), | ||
| emergency_signer: authority_data.emergency_finalizer().clone(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Ways in which a justification can be wrong. | ||
| #[derive(Debug, PartialEq, Eq)] | ||
| pub enum SessionVerificationError { | ||
| BadMultisignature, | ||
| BadEmergencySignature, | ||
| NoEmergencySigner, | ||
| } | ||
|
|
||
| impl Display for SessionVerificationError { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { | ||
| use SessionVerificationError::*; | ||
| match self { | ||
| BadMultisignature => write!(f, "bad multisignature"), | ||
| BadEmergencySignature => write!(f, "bad emergency signature"), | ||
| NoEmergencySigner => write!(f, "no emergency signer defined"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl SessionVerifier { | ||
| /// Verifies the correctness of a justification for supplied bytes. | ||
| pub fn verify_bytes( | ||
| &self, | ||
| justification: &AlephJustification, | ||
| bytes: Vec<u8>, | ||
| ) -> Result<(), SessionVerificationError> { | ||
| use AlephJustification::*; | ||
| use SessionVerificationError::*; | ||
| match justification { | ||
| CommitteeMultisignature(multisignature) => { | ||
| match self.authority_verifier.is_complete(&bytes, multisignature) { | ||
| true => Ok(()), | ||
| false => Err(BadMultisignature), | ||
| } | ||
| } | ||
| EmergencySignature(signature) => match self | ||
| .emergency_signer | ||
| .as_ref() | ||
| .ok_or(NoEmergencySigner)? | ||
| .verify(&bytes, signature) | ||
| { | ||
| true => Ok(()), | ||
| false => Err(BadEmergencySignature), | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // This shouldn't be necessary after we remove the legacy justification sync. Then we can also | ||
| // rewrite the implementation above and make it simpler. | ||
| impl<B: BlockT> LegacyVerifier<B> for SessionVerifier { | ||
| fn verify(&self, justification: &AlephJustification, hash: B::Hash) -> bool { | ||
| match self.verify_bytes(justification, hash.encode()) { | ||
| Ok(()) => true, | ||
| Err(e) => { | ||
| warn!(target: "aleph-justification", "Bad justification for block {:?}: {}", hash, e); | ||
| false | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.