-
Notifications
You must be signed in to change notification settings - Fork 68
A0-1823: add substrate specific chain status #844
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 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9e8941a
add substrate chain status
maciejnems 1b060f7
Merge branch 'main' into A0-1823-chain-status-source
maciejnems eaffa07
fix clippy
maciejnems d69b89b
remove can't decode aleph justification error
maciejnems da42661
Merge branch 'main' into A0-1823-chain-status-source
maciejnems 5b5d569
make pub
maciejnems 67f682d
Merge branch 'main' into A0-1823-chain-status-source
maciejnems aa3136f
apply suggested changes
maciejnems 8c282a6
Merge branch 'main' into A0-1823-chain-status-source
maciejnems a1cafe1
change to client error
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
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 |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| use std::{ | ||
| fmt::{Display, Error as FmtError, Formatter}, | ||
| marker::PhantomData, | ||
| }; | ||
|
|
||
| use aleph_primitives::{BlockNumber, ALEPH_ENGINE_ID}; | ||
| use log::warn; | ||
| use sp_blockchain::Backend; | ||
| use sp_runtime::{ | ||
| generic::BlockId as SubstrateBlockId, | ||
| traits::{Block as BlockT, Header as SubstrateHeader}, | ||
| }; | ||
|
|
||
| use crate::{ | ||
| justification::backwards_compatible_decode, | ||
| sync::{substrate::Justification, BlockStatus, ChainStatus, Header, LOG_TARGET}, | ||
| AlephJustification, | ||
| }; | ||
|
|
||
| /// What can go wrong when checking chain status | ||
| #[derive(Debug)] | ||
| pub enum Error<B: BlockT> { | ||
| MissingHash(B::Hash), | ||
| MissingJustification(B::Hash), | ||
| } | ||
|
|
||
| impl<B: BlockT> Display for Error<B> { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { | ||
| use Error::*; | ||
| match self { | ||
| MissingHash(hash) => { | ||
| write!(f, "no block for hash {:?}", hash) | ||
| } | ||
| MissingJustification(hash) => { | ||
| write!( | ||
| f, | ||
| "no justification for finalized block with hash {:?}", | ||
| hash | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
pmikolajczyk41 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Substrate implementation of ChainStatus trait | ||
| struct SubstrateChainStatus<B, BE> | ||
pmikolajczyk41 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| where | ||
| BE: Backend<B>, | ||
| B: BlockT, | ||
| B::Header: SubstrateHeader<Number = BlockNumber>, | ||
| { | ||
| client: BE, | ||
| _phantom: PhantomData<B>, | ||
| } | ||
|
|
||
| impl<B, BE> SubstrateChainStatus<B, BE> | ||
| where | ||
| BE: Backend<B>, | ||
| B: BlockT, | ||
| B::Header: SubstrateHeader<Number = BlockNumber>, | ||
| { | ||
| fn header(&self, hash: B::Hash) -> Option<B::Header> { | ||
| let id = SubstrateBlockId::<B>::Hash(hash); | ||
| self.client | ||
| .header(id) | ||
| .expect("substrate client must respond") | ||
pmikolajczyk41 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| fn justification(&self, hash: B::Hash) -> Option<AlephJustification> { | ||
| let id = SubstrateBlockId::<B>::Hash(hash); | ||
| let justification = self | ||
| .client | ||
| .justifications(id) | ||
| .expect("substrate client must respond") | ||
| .and_then(|j| j.into_justification(ALEPH_ENGINE_ID))?; | ||
|
|
||
| match backwards_compatible_decode(justification) { | ||
| Ok(justification) => Some(justification), | ||
| // This should not happen, as we only import correctly encoded justification. | ||
| Err(e) => { | ||
| warn!( | ||
| target: LOG_TARGET, | ||
| "Could not decode stored justification for block {:?}: {}", hash, e | ||
| ); | ||
| None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn best_hash(&self) -> B::Hash { | ||
| self.client.info().best_hash | ||
| } | ||
|
|
||
| fn finalized_hash(&self) -> B::Hash { | ||
| self.client.info().finalized_hash | ||
| } | ||
| } | ||
|
|
||
| impl<B, BE> ChainStatus<Justification<B::Header>> for SubstrateChainStatus<B, BE> | ||
| where | ||
| BE: Backend<B>, | ||
| B: BlockT, | ||
| B::Header: SubstrateHeader<Number = BlockNumber>, | ||
| { | ||
| type Error = Error<B>; | ||
|
|
||
| fn status_of( | ||
| &self, | ||
| id: <B::Header as Header>::Identifier, | ||
| ) -> BlockStatus<Justification<B::Header>> { | ||
| let header = match self.header(id.hash) { | ||
| Some(header) => header, | ||
| None => return BlockStatus::Unknown, | ||
| }; | ||
|
|
||
| if let Some(raw_justification) = self.justification(id.hash) { | ||
| BlockStatus::Justified(Justification { | ||
| header, | ||
| raw_justification, | ||
| }) | ||
| } else { | ||
| BlockStatus::Present(header) | ||
| } | ||
| } | ||
|
|
||
| fn best_block(&self) -> Result<B::Header, Self::Error> { | ||
| let best_hash = self.best_hash(); | ||
|
|
||
| self.header(best_hash).ok_or(Error::MissingHash(best_hash)) | ||
| } | ||
|
|
||
| fn top_finalized(&self) -> Result<Justification<B::Header>, Self::Error> { | ||
| let finalized_hash = self.finalized_hash(); | ||
|
|
||
| let header = self | ||
| .header(finalized_hash) | ||
| .ok_or(Error::MissingHash(finalized_hash))?; | ||
| let raw_justification = self | ||
| .justification(finalized_hash) | ||
| .ok_or(Error::MissingJustification(finalized_hash))?; | ||
|
|
||
| Ok(Justification { | ||
| header, | ||
| raw_justification, | ||
| }) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.