From 9ba5c5c2d78a2f34c06db4ddbad1003aaa9745ac Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Wed, 3 Jun 2020 11:58:38 +0300 Subject: [PATCH 1/3] Add block event forwarding into the Overseer --- Cargo.lock | 1 + overseer/Cargo.toml | 1 + overseer/src/lib.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 9294c90408ea..f246530285a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3309,6 +3309,7 @@ dependencies = [ "kv-log-macro", "log 0.4.8", "polkadot-primitives", + "sc-client-api", "streamunordered", ] diff --git a/overseer/Cargo.toml b/overseer/Cargo.toml index 0bef4442be4f..4bd63ad13619 100644 --- a/overseer/Cargo.toml +++ b/overseer/Cargo.toml @@ -10,6 +10,7 @@ log = "0.4.8" futures-timer = "3.0.2" streamunordered = "0.5.1" polkadot-primitives = { path = "../primitives" } +client = { package = "sc-client-api", git = "https://github.com/paritytech/substrate", branch = "master" } [dev-dependencies] futures = { version = "0.3.5", features = ["thread-pool"] } diff --git a/overseer/src/lib.rs b/overseer/src/lib.rs index 7f0849fcb592..e8e584fc7f3c 100644 --- a/overseer/src/lib.rs +++ b/overseer/src/lib.rs @@ -71,7 +71,8 @@ use futures::{ use futures_timer::Delay; use streamunordered::{StreamYield, StreamUnordered}; -use polkadot_primitives::{BlockNumber, Hash}; +use polkadot_primitives::{Block, BlockNumber, Hash}; +use client::{BlockImportNotification, BlockchainEvents, FinalityNotification}; /// An error type that describes faults that may happen /// @@ -154,6 +155,26 @@ pub struct BlockInfo { pub number: BlockNumber, } +impl From> for BlockInfo { + fn from(n: BlockImportNotification) -> Self { + BlockInfo { + hash: n.hash, + parent_hash: n.header.parent_hash, + number: n.header.number, + } + } +} + +impl From> for BlockInfo { + fn from(n: FinalityNotification) -> Self { + BlockInfo { + hash: n.hash, + parent_hash: n.header.parent_hash, + number: n.header.number, + } + } +} + /// Some event from outer world. enum Event { BlockImported(BlockInfo), @@ -172,6 +193,7 @@ pub enum OutboundMessage { /// A handler used to communicate with the [`Overseer`]. /// /// [`Overseer`]: struct.Overseer.html +#[derive(Clone)] pub struct OverseerHandler { events_tx: mpsc::Sender, } @@ -206,6 +228,38 @@ impl OverseerHandler { } } +pub async fn forward_events>( + client: P, + mut handler: OverseerHandler, +) -> SubsystemResult<()> { + let mut finality = client.finality_notification_stream(); + let mut imports = client.import_notification_stream(); + + loop { + select! { + f = finality.next() => { + match f { + Some(block) => { + handler.block_finalized(block.into()).await?; + } + None => break, + } + }, + i = imports.next() => { + match i { + Some(block) => { + handler.block_imported(block.into()).await?; + } + None => break, + } + }, + complete => break, + } + } + + Ok(()) +} + impl Debug for ToOverseer { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { From a43e38b2460d818fb74e01b28c140dcf396c46a5 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Wed, 3 Jun 2020 12:29:23 +0300 Subject: [PATCH 2/3] Add a doc comment --- overseer/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/overseer/src/lib.rs b/overseer/src/lib.rs index e8e584fc7f3c..ddc2dcf27fd0 100644 --- a/overseer/src/lib.rs +++ b/overseer/src/lib.rs @@ -228,6 +228,11 @@ impl OverseerHandler { } } +/// Glues together the `Overseer` and `BlockchainEvents` by forwarding +/// import and finality notifications into the `OverseerHandler`. +/// +/// [`Overseer`]: struct.Overseer.html +/// [`OverseerHandler`]: struct.OverseerHandler.html pub async fn forward_events>( client: P, mut handler: OverseerHandler, From 74aca0c1b1605da37317be89aa08ccd24edacd97 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Wed, 3 Jun 2020 12:33:48 +0300 Subject: [PATCH 3/3] Fix a doc comment --- overseer/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/overseer/src/lib.rs b/overseer/src/lib.rs index ddc2dcf27fd0..2265c89a62bb 100644 --- a/overseer/src/lib.rs +++ b/overseer/src/lib.rs @@ -228,8 +228,8 @@ impl OverseerHandler { } } -/// Glues together the `Overseer` and `BlockchainEvents` by forwarding -/// import and finality notifications into the `OverseerHandler`. +/// Glues together the [`Overseer`] and `BlockchainEvents` by forwarding +/// import and finality notifications into the [`OverseerHandler`]. /// /// [`Overseer`]: struct.Overseer.html /// [`OverseerHandler`]: struct.OverseerHandler.html