minimal-example.rs 4.32 KB
Newer Older
Fedor Sakharov's avatar
Fedor Sakharov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 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 std::time::Duration;
use futures::{
23
	channel::oneshot,
24
	pending, pin_mut, select, stream,
Fedor Sakharov's avatar
Fedor Sakharov committed
25
26
27
28
29
	FutureExt, StreamExt,
};
use futures_timer::Delay;
use kv_log_macro as log;

asynchronous rob's avatar
asynchronous rob committed
30
use polkadot_primitives::v1::{BlockData, PoV};
31
use polkadot_overseer::{Overseer, AllSubsystems};
32

33
use polkadot_subsystem::{
asynchronous rob's avatar
asynchronous rob committed
34
	Subsystem, SubsystemContext, DummySubsystem,
35
36
	SpawnedSubsystem, FromOverseer,
};
37
use polkadot_subsystem::messages::{
38
	CandidateValidationMessage, CandidateBackingMessage, AllMessages,
Fedor Sakharov's avatar
Fedor Sakharov committed
39
40
41
42
43
};

struct Subsystem1;

impl Subsystem1 {
44
	async fn run(mut ctx: impl SubsystemContext<Message=CandidateBackingMessage>)  {
Fedor Sakharov's avatar
Fedor Sakharov committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
		loop {
			match ctx.try_recv().await {
				Ok(Some(msg)) => {
					if let FromOverseer::Communication { msg } = msg {
						log::info!("msg {:?}", msg);
					}
					continue;
				}
				Ok(None) => (),
				Err(_) => {
					log::info!("exiting");
					return;
				}
			}

			Delay::new(Duration::from_secs(1)).await;
61
62
			let (tx, _) = oneshot::channel();

63
			ctx.send_message(AllMessages::CandidateValidation(
asynchronous rob's avatar
asynchronous rob committed
64
				CandidateValidationMessage::ValidateFromChainState(
65
					Default::default(),
asynchronous rob's avatar
asynchronous rob committed
66
					PoV {
67
						block_data: BlockData(Vec::new()),
asynchronous rob's avatar
asynchronous rob committed
68
					}.into(),
69
70
					tx,
				)
Fedor Sakharov's avatar
Fedor Sakharov committed
71
72
73
74
75
			)).await.unwrap();
		}
	}
}

76
77
78
impl<C> Subsystem<C> for Subsystem1
	where C: SubsystemContext<Message=CandidateBackingMessage>
{
79
	fn start(self, ctx: C) -> SpawnedSubsystem {
80
		let future = Box::pin(async move {
Fedor Sakharov's avatar
Fedor Sakharov committed
81
			Self::run(ctx).await;
82
83
84
85
86
87
		});

		SpawnedSubsystem {
			name: "subsystem-1",
			future,
		}
Fedor Sakharov's avatar
Fedor Sakharov committed
88
89
90
91
92
93
	}
}

struct Subsystem2;

impl Subsystem2 {
94
	async fn run(mut ctx: impl SubsystemContext<Message=CandidateValidationMessage>)  {
95
96
97
98
99
100
101
102
103
		ctx.spawn(
			"subsystem-2-job",
			Box::pin(async {
				loop {
					log::info!("Job tick");
					Delay::new(Duration::from_secs(1)).await;
				}
			}),
		).await.unwrap();
Fedor Sakharov's avatar
Fedor Sakharov committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

		loop {
			match ctx.try_recv().await {
				Ok(Some(msg)) => {
					log::info!("Subsystem2 received message {:?}", msg);
					continue;
				}
				Ok(None) => { pending!(); }
				Err(_) => {
					log::info!("exiting");
					return;
				},
			}
		}
	}
}

121
122
123
impl<C> Subsystem<C> for Subsystem2
	where C: SubsystemContext<Message=CandidateValidationMessage>
{
124
	fn start(self, ctx: C) -> SpawnedSubsystem {
125
		let future = Box::pin(async move {
Fedor Sakharov's avatar
Fedor Sakharov committed
126
			Self::run(ctx).await;
127
128
129
130
131
132
		});

		SpawnedSubsystem {
			name: "subsystem-2",
			future,
		}
Fedor Sakharov's avatar
Fedor Sakharov committed
133
134
135
136
137
	}
}

fn main() {
	femme::with_level(femme::LevelFilter::Trace);
Bastian Köcher's avatar
Bastian Köcher committed
138
	let spawner = sp_core::testing::TaskExecutor::new();
Fedor Sakharov's avatar
Fedor Sakharov committed
139
140
141
142
143
	futures::executor::block_on(async {
		let timer_stream = stream::repeat(()).then(|_| async {
			Delay::new(Duration::from_secs(1)).await;
		});

144
145
146
147
148
149
		let all_subsystems = AllSubsystems {
			candidate_validation: Subsystem2,
			candidate_backing: Subsystem1,
			candidate_selection: DummySubsystem,
			statement_distribution: DummySubsystem,
			availability_distribution: DummySubsystem,
150
			bitfield_signing: DummySubsystem,
151
152
153
154
155
156
			bitfield_distribution: DummySubsystem,
			provisioner: DummySubsystem,
			pov_distribution: DummySubsystem,
			runtime_api: DummySubsystem,
			availability_store: DummySubsystem,
			network_bridge: DummySubsystem,
157
			chain_api: DummySubsystem,
158
159
			collation_generation: DummySubsystem,
			collator_protocol: DummySubsystem,
160
		};
Fedor Sakharov's avatar
Fedor Sakharov committed
161
		let (overseer, _handler) = Overseer::new(
162
			vec![],
163
			all_subsystems,
Fedor Sakharov's avatar
Fedor Sakharov committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
			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() => {
					log::info!("tick");
				}
				complete => break,
			}
		}
	});
}