-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A0-1822: add substrate chain status notifier (#839)
* add substrate chain status notifier
- Loading branch information
1 parent
f84e20d
commit 38fde8f
Showing
3 changed files
with
94 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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,78 @@ | ||
use std::fmt::{Display, Error as FmtError, Formatter}; | ||
|
||
use aleph_primitives::BlockNumber; | ||
use futures::StreamExt; | ||
use sc_client_api::client::{FinalityNotifications, ImportNotifications}; | ||
use sp_runtime::traits::{Block as BlockT, Header as SubstrateHeader}; | ||
use tokio::select; | ||
|
||
use crate::sync::{substrate::BlockId, ChainStatusNotification, ChainStatusNotifier, Header}; | ||
|
||
/// What can go wrong when waiting for next chain status notification. | ||
#[derive(Debug)] | ||
pub enum Error { | ||
JustificationStreamClosed, | ||
ImportStreamClosed, | ||
} | ||
|
||
impl Display for Error { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { | ||
use Error::*; | ||
match self { | ||
JustificationStreamClosed => { | ||
write!(f, "finalization notification stream has ended") | ||
} | ||
ImportStreamClosed => { | ||
write!(f, "import notification stream has ended") | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Substrate specific implementation of `ChainStatusNotifier`. | ||
pub struct SubstrateChainStatusNotifier<B> | ||
where | ||
B: BlockT, | ||
{ | ||
finality_notifications: FinalityNotifications<B>, | ||
import_notifications: ImportNotifications<B>, | ||
} | ||
|
||
impl<B> SubstrateChainStatusNotifier<B> | ||
where | ||
B: BlockT, | ||
{ | ||
fn new( | ||
finality_notifications: FinalityNotifications<B>, | ||
import_notifications: ImportNotifications<B>, | ||
) -> Self { | ||
Self { | ||
finality_notifications, | ||
import_notifications, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<B> ChainStatusNotifier<BlockId<B::Header>> for SubstrateChainStatusNotifier<B> | ||
where | ||
B: BlockT, | ||
B::Header: SubstrateHeader<Number = BlockNumber>, | ||
{ | ||
type Error = Error; | ||
|
||
async fn next(&mut self) -> Result<ChainStatusNotification<BlockId<B::Header>>, Self::Error> { | ||
select! { | ||
maybe_block = self.finality_notifications.next() => { | ||
maybe_block | ||
.map(|block| ChainStatusNotification::BlockFinalized(block.header.id())) | ||
.ok_or(Error::JustificationStreamClosed) | ||
}, | ||
maybe_block = self.import_notifications.next() => { | ||
maybe_block | ||
.map(|block| ChainStatusNotification::BlockImported(block.header.id())) | ||
.ok_or(Error::ImportStreamClosed) | ||
} | ||
} | ||
} | ||
} |