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, sync::Arc};
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
45pub use jaeger::*;
46pub use polkadot_node_jaeger as jaeger;
47
48/// How many slots are stack-reserved for active leaves updates
49///
50/// If there are fewer than this number of slots, then we've wasted some stack space.
51/// If there are greater than this number of slots, then we fall back to a heap vector.
52const ACTIVE_LEAVES_SMALLVEC_CAPACITY: usize = 8;
53
54/// Activated leaf.
55#[derive(Debug, Clone)]
56pub struct ActivatedLeaf {
57	/// The block hash.
58	pub hash: Hash,
59	/// The block number.
60	pub number: BlockNumber,
61	/// A handle to unpin the block on drop.
62	pub unpin_handle: UnpinHandle,
63	/// An associated [`jaeger::Span`].
64	///
65	/// NOTE: Each span should only be kept active as long as the leaf is considered active and
66	/// should be dropped when the leaf is deactivated.
67	pub span: Arc<jaeger::Span>,
68}
69
70/// Changes in the set of active leaves: the parachain heads which we care to work on.
71///
72/// Note that the activated and deactivated fields indicate deltas, not complete sets.
73#[derive(Clone, Default)]
74pub struct ActiveLeavesUpdate {
75	/// New relay chain block of interest.
76	pub activated: Option<ActivatedLeaf>,
77	/// Relay chain block hashes no longer of interest.
78	pub deactivated: SmallVec<[Hash; ACTIVE_LEAVES_SMALLVEC_CAPACITY]>,
79}
80
81impl ActiveLeavesUpdate {
82	/// Create a `ActiveLeavesUpdate` with a single activated hash
83	pub fn start_work(activated: ActivatedLeaf) -> Self {
84		Self { activated: Some(activated), ..Default::default() }
85	}
86
87	/// Create a `ActiveLeavesUpdate` with a single deactivated hash
88	pub fn stop_work(hash: Hash) -> Self {
89		Self { deactivated: [hash][..].into(), ..Default::default() }
90	}
91
92	/// Is this update empty and doesn't contain any information?
93	pub fn is_empty(&self) -> bool {
94		self.activated.is_none() && self.deactivated.is_empty()
95	}
96}
97
98impl PartialEq for ActiveLeavesUpdate {
99	/// Equality for `ActiveLeavesUpdate` doesn't imply bitwise equality.
100	///
101	/// Instead, it means equality when `activated` and `deactivated` are considered as sets.
102	fn eq(&self, other: &Self) -> bool {
103		self.activated.as_ref().map(|a| a.hash) == other.activated.as_ref().map(|a| a.hash) &&
104			self.deactivated.len() == other.deactivated.len() &&
105			self.deactivated.iter().all(|a| other.deactivated.contains(a))
106	}
107}
108
109impl fmt::Debug for ActiveLeavesUpdate {
110	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111		f.debug_struct("ActiveLeavesUpdate")
112			.field("activated", &self.activated)
113			.field("deactivated", &self.deactivated)
114			.finish()
115	}
116}
117
118/// Signals sent by an overseer to a subsystem.
119#[derive(PartialEq, Clone, Debug)]
120pub enum OverseerSignal {
121	/// Subsystems should adjust their jobs to start and stop work on appropriate block hashes.
122	ActiveLeaves(ActiveLeavesUpdate),
123	/// `Subsystem` is informed of a finalized block by its block hash and number.
124	BlockFinalized(Hash, BlockNumber),
125	/// Conclude the work of the `Overseer` and all `Subsystem`s.
126	Conclude,
127}