Files
pezkuwi-subxt/polkadot/node/overseer/examples/minimal-example.rs
T
Andrei Sandu cddd5749d3 Malus: improvements in dispute ancestor and suggest garbage candidate implementation (#5011)
* Implement fake validation results

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* refactor

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* cargo lock

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* spell check

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* spellcheck

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* typos

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Review feedback

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* move stuff around

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* chores

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Impl valid - still wip

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fixes

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fmt

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Pull Ladi's implementation:
https://github.com/paritytech/polkadot/pull/4711

Co-authored-by: Lldenaurois <Ljdenaurois@gmail.com>
Co-authored-by: Andrei Sandu <andrei-mihail@parity.io>
Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Fix build

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Logs and comments

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* WIP: suggest garbage candidate + implement validation result caching

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fix

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Do commitment hash checks in candidate validation

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Minor refactor in approval, backing, dispute-coord

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Working version of suggest garbage candidate

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Dedup

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* cleanup #1

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Fix tests

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* remove debug leftovers

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fmt

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Accidentally commited some local test

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* spellcheck

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* some more fixes

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Refactor and fix it

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* review feedback

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* typo

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* tests review feedback

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* refactor disputer

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fix tests

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Fix zombienet disputes test

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* spellcheck

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fix

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* Fix ui tests

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* fix typo

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

Co-authored-by: Lldenaurois <Ljdenaurois@gmail.com>
2022-04-13 13:45:39 +00:00

204 lines
4.8 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 ::test_helpers::{dummy_candidate_descriptor, dummy_hash};
use polkadot_node_primitives::{BlockData, PoV};
use polkadot_node_subsystem_types::messages::{
CandidateBackingMessage, CandidateValidationMessage,
};
use polkadot_overseer::{
self as overseer,
dummy::dummy_overseer_builder,
gen::{FromOverseer, SpawnedSubsystem},
AllMessages, HeadSupportsParachains, OverseerSignal, SubsystemError,
};
use polkadot_primitives::v2::{CandidateReceipt, 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 {
gum::info!("msg {:?}", msg);
}
continue 'louy
},
Ok(None) => (),
Err(_) => {
gum::info!("exiting");
break 'louy
},
}
Delay::new(Duration::from_secs(1)).await;
let (tx, _) = oneshot::channel();
let candidate_receipt = CandidateReceipt {
descriptor: dummy_candidate_descriptor(dummy_hash()),
commitments_hash: Hash::zero(),
};
let msg = CandidateValidationMessage::ValidateFromChainState(
candidate_receipt,
PoV { block_data: BlockData(Vec::new()) }.into(),
Default::default(),
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 {
gum::info!("Job tick");
Delay::new(Duration::from_secs(1)).await;
}
}),
)
.unwrap();
loop {
match ctx.try_recv().await {
Ok(Some(msg)) => {
gum::info!("Subsystem2 received message {:?}", msg);
continue
},
Ok(None) => {
pending!();
},
Err(_) => {
gum::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 (overseer, _handle) = dummy_overseer_builder(spawner, AlwaysSupportsParachains, None)
.unwrap()
.replace_candidate_validation(|_| Subsystem2)
.replace_candidate_backing(|orig| orig)
.build()
.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() => {
gum::info!("tick");
}
complete => break,
}
}
});
}