referrerpolicy=no-referrer-when-downgrade

polkadot_node_subsystem_types/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Subsystem trait definitions and message types.
18//!
19//! Node-side logic for Polkadot is mostly comprised of Subsystems, which are discrete components
20//! that communicate via message-passing. They are coordinated by an overseer, provided by a
21//! separate crate.
22
23#![warn(missing_docs)]
24
25use smallvec::SmallVec;
26use std::fmt;
27
28pub use polkadot_primitives::{Block, BlockNumber, Hash};
29
30/// Keeps the state of a specific block pinned in memory while the handle is alive.
31///
32/// The handle is reference counted and once the last is dropped, the
33/// block is unpinned.
34///
35/// This is useful for runtime API calls to blocks that are
36/// racing against finality, e.g. for slashing purposes.
37pub type UnpinHandle = sc_client_api::UnpinHandle<Block>;
38
39pub mod errors;
40pub mod messages;
41
42mod runtime_client;
43pub use runtime_client::{ChainApiBackend, DefaultSubsystemClient, RuntimeApiSubsystemClient};
44
45/// How many slots are stack-reserved for active leaves updates
46///
47/// If there are fewer than this number of slots, then we've wasted some stack space.
48/// If there are greater than this number of slots, then we fall back to a heap vector.
49const ACTIVE_LEAVES_SMALLVEC_CAPACITY: usize = 8;
50
51/// Activated leaf.
52#[derive(Debug, Clone)]
53pub struct ActivatedLeaf {
54	/// The block hash.
55	pub hash: Hash,
56	/// The block number.
57	pub number: BlockNumber,
58	/// A handle to unpin the block on drop.
59	pub unpin_handle: UnpinHandle,
60}
61
62/// Changes in the set of active leaves: the parachain heads which we care to work on.
63///
64/// Note that the activated and deactivated fields indicate deltas, not complete sets.
65#[derive(Clone, Default)]
66pub struct ActiveLeavesUpdate {
67	/// New relay chain block of interest.
68	pub activated: Option<ActivatedLeaf>,
69	/// Relay chain block hashes no longer of interest.
70	pub deactivated: SmallVec<[Hash; ACTIVE_LEAVES_SMALLVEC_CAPACITY]>,
71}
72
73impl ActiveLeavesUpdate {
74	/// Create a `ActiveLeavesUpdate` with a single activated hash
75	pub fn start_work(activated: ActivatedLeaf) -> Self {
76		Self { activated: Some(activated), ..Default::default() }
77	}
78
79	/// Create a `ActiveLeavesUpdate` with a single deactivated hash
80	pub fn stop_work(hash: Hash) -> Self {
81		Self { deactivated: [hash][..].into(), ..Default::default() }
82	}
83
84	/// Is this update empty and doesn't contain any information?
85	pub fn is_empty(&self) -> bool {
86		self.activated.is_none() && self.deactivated.is_empty()
87	}
88}
89
90impl PartialEq for ActiveLeavesUpdate {
91	/// Equality for `ActiveLeavesUpdate` doesn't imply bitwise equality.
92	///
93	/// Instead, it means equality when `activated` and `deactivated` are considered as sets.
94	fn eq(&self, other: &Self) -> bool {
95		self.activated.as_ref().map(|a| a.hash) == other.activated.as_ref().map(|a| a.hash) &&
96			self.deactivated.len() == other.deactivated.len() &&
97			self.deactivated.iter().all(|a| other.deactivated.contains(a))
98	}
99}
100
101impl fmt::Debug for ActiveLeavesUpdate {
102	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103		f.debug_struct("ActiveLeavesUpdate")
104			.field("activated", &self.activated)
105			.field("deactivated", &self.deactivated)
106			.finish()
107	}
108}
109
110/// Signals sent by an overseer to a subsystem.
111#[derive(PartialEq, Clone, Debug)]
112pub enum OverseerSignal {
113	/// Subsystems should adjust their jobs to start and stop work on appropriate block hashes.
114	ActiveLeaves(ActiveLeavesUpdate),
115	/// `Subsystem` is informed of a finalized block by its block hash and number.
116	BlockFinalized(Hash, BlockNumber),
117	/// Conclude the work of the `Overseer` and all `Subsystem`s.
118	Conclude,
119}