Files
pezkuwi-subxt/polkadot/node/overseer/examples/minimal-example.rs
T
Bernhard Schuster 3cc5a1eee9 feat/overseer: introduce closure init (#3775)
* feat/overseer: introduce closure init

Enables removal of the connected/disconnected overseer state.

* feat/overseer: allow replacement logic to access the original

Allows to re-use init-once types, which would otherwise error.

* feat/overseer: introduce external connector

Preparation for removal of `AllSubsystems`
which is another prerequisite for removing
the connect/disconnect state.

* fix/test: replace needs closure

* fixup

* simplify

* mea culpa

* all-subsystems-gen test
2021-09-04 08:07:07 +00:00

195 lines
4.6 KiB
Rust

// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Shows a basic usage of the `Overseer`:
//! * Spawning subsystems and subsystem child jobs
//! * Establishing message passing
use futures::{channel::oneshot, pending, pin_mut, select, stream, FutureExt, StreamExt};
use futures_timer::Delay;
use std::time::Duration;
use polkadot_node_primitives::{BlockData, PoV};
use polkadot_node_subsystem_types::messages::{
CandidateBackingMessage, CandidateValidationMessage,
};
use polkadot_overseer::{
self as overseer,
gen::{FromOverseer, SpawnedSubsystem},
AllMessages, AllSubsystems, HeadSupportsParachains, Overseer, OverseerSignal, SubsystemError,
};
use polkadot_primitives::v1::Hash;
struct AlwaysSupportsParachains;
impl HeadSupportsParachains for AlwaysSupportsParachains {
fn head_supports_parachains(&self, _head: &Hash) -> bool {
true
}
}
////////
struct Subsystem1;
impl Subsystem1 {
async fn run<Ctx>(mut ctx: Ctx) -> ()
where
Ctx: overseer::SubsystemContext<
Message = CandidateBackingMessage,
AllMessages = AllMessages,
Signal = OverseerSignal,
>,
{
'louy: loop {
match ctx.try_recv().await {
Ok(Some(msg)) => {
if let FromOverseer::Communication { msg } = msg {
tracing::info!("msg {:?}", msg);
}
continue 'louy
},
Ok(None) => (),
Err(_) => {
tracing::info!("exiting");
break 'louy
},
}
Delay::new(Duration::from_secs(1)).await;
let (tx, _) = oneshot::channel();
let msg = CandidateValidationMessage::ValidateFromChainState(
Default::default(),
PoV { block_data: BlockData(Vec::new()) }.into(),
tx,
);
ctx.send_message(<Ctx as overseer::SubsystemContext>::AllMessages::from(msg))
.await;
}
()
}
}
impl<Context> overseer::Subsystem<Context, SubsystemError> for Subsystem1
where
Context: overseer::SubsystemContext<
Message = CandidateBackingMessage,
AllMessages = AllMessages,
Signal = OverseerSignal,
>,
{
fn start(self, ctx: Context) -> SpawnedSubsystem<SubsystemError> {
let future = Box::pin(async move {
Self::run(ctx).await;
Ok(())
});
SpawnedSubsystem { name: "subsystem-1", future }
}
}
//////////////////
struct Subsystem2;
impl Subsystem2 {
async fn run<Ctx>(mut ctx: Ctx)
where
Ctx: overseer::SubsystemContext<
Message = CandidateValidationMessage,
AllMessages = AllMessages,
Signal = OverseerSignal,
>,
{
ctx.spawn(
"subsystem-2-job",
Box::pin(async {
loop {
tracing::info!("Job tick");
Delay::new(Duration::from_secs(1)).await;
}
}),
)
.unwrap();
loop {
match ctx.try_recv().await {
Ok(Some(msg)) => {
tracing::info!("Subsystem2 received message {:?}", msg);
continue
},
Ok(None) => {
pending!();
},
Err(_) => {
tracing::info!("exiting");
return
},
}
}
}
}
impl<Context> overseer::Subsystem<Context, SubsystemError> for Subsystem2
where
Context: overseer::SubsystemContext<
Message = CandidateValidationMessage,
AllMessages = AllMessages,
Signal = OverseerSignal,
>,
{
fn start(self, ctx: Context) -> SpawnedSubsystem<SubsystemError> {
let future = Box::pin(async move {
Self::run(ctx).await;
Ok(())
});
SpawnedSubsystem { name: "subsystem-2", future }
}
}
fn main() {
femme::with_level(femme::LevelFilter::Trace);
let spawner = sp_core::testing::TaskExecutor::new();
futures::executor::block_on(async {
let timer_stream = stream::repeat(()).then(|_| async {
Delay::new(Duration::from_secs(1)).await;
});
let all_subsystems = AllSubsystems::<()>::dummy()
.replace_candidate_validation(|_| Subsystem2)
.replace_candidate_backing(|orig| orig);
let (overseer, _handle) =
Overseer::new(vec![], all_subsystems, None, AlwaysSupportsParachains, spawner).unwrap();
let overseer_fut = overseer.run().fuse();
let timer_stream = timer_stream;
pin_mut!(timer_stream);
pin_mut!(overseer_fut);
loop {
select! {
_ = overseer_fut => break,
_ = timer_stream.next() => {
tracing::info!("tick");
}
complete => break,
}
}
});
}