referrerpolicy=no-referrer-when-downgrade

sc_network_common/
role.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19// file-level lint whitelist to avoid problem with bitflags macro below
20// TODO: can be dropped after an update to bitflags 2.4
21#![allow(clippy::bad_bit_mask)]
22
23use codec::{self, Encode, EncodeLike, Input, Output};
24
25/// Role that the peer sent to us during the handshake, with the addition of what our local node
26/// knows about that peer.
27///
28/// > **Note**: This enum is different from the `Role` enum. The `Role` enum indicates what a
29/// >			node says about itself, while `ObservedRole` is a `Role` merged with the
30/// >			information known locally about that node.
31#[derive(Debug, Copy, Clone, PartialEq, Eq)]
32pub enum ObservedRole {
33	/// Full node.
34	Full,
35	/// Light node.
36	Light,
37	/// Third-party authority.
38	Authority,
39}
40
41impl ObservedRole {
42	/// Returns `true` for `ObservedRole::Light`.
43	pub fn is_light(&self) -> bool {
44		matches!(self, Self::Light)
45	}
46}
47
48impl From<Roles> for ObservedRole {
49	fn from(roles: Roles) -> Self {
50		if roles.is_authority() {
51			ObservedRole::Authority
52		} else if roles.is_full() {
53			ObservedRole::Full
54		} else {
55			ObservedRole::Light
56		}
57	}
58}
59
60/// Role of the local node.
61#[derive(Debug, Clone, Copy)]
62pub enum Role {
63	/// Regular full node.
64	Full,
65	/// Actual authority.
66	Authority,
67}
68
69impl Role {
70	/// True for [`Role::Authority`].
71	pub fn is_authority(&self) -> bool {
72		matches!(self, Self::Authority)
73	}
74}
75
76impl std::fmt::Display for Role {
77	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78		match self {
79			Self::Full => write!(f, "FULL"),
80			Self::Authority => write!(f, "AUTHORITY"),
81		}
82	}
83}
84
85bitflags::bitflags! {
86	/// Bitmask of the roles that a node fulfills.
87	pub struct Roles: u8 {
88		/// No network.
89		const NONE = 0b00000000;
90		/// Full node, does not participate in consensus.
91		const FULL = 0b00000001;
92		/// Light client node.
93		const LIGHT = 0b00000010;
94		/// Act as an authority
95		const AUTHORITY = 0b00000100;
96	}
97}
98
99impl Roles {
100	/// Does this role represents a client that holds full chain data locally?
101	pub fn is_full(&self) -> bool {
102		self.intersects(Self::FULL | Self::AUTHORITY)
103	}
104
105	/// Does this role represents a client that does not participates in the consensus?
106	pub fn is_authority(&self) -> bool {
107		*self == Self::AUTHORITY
108	}
109
110	/// Does this role represents a client that does not hold full chain data locally?
111	pub fn is_light(&self) -> bool {
112		!self.is_full()
113	}
114}
115
116impl<'a> From<&'a Role> for Roles {
117	fn from(roles: &'a Role) -> Self {
118		match roles {
119			Role::Full => Self::FULL,
120			Role::Authority => Self::AUTHORITY,
121		}
122	}
123}
124
125impl Encode for Roles {
126	fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
127		dest.push_byte(self.bits())
128	}
129}
130
131impl EncodeLike for Roles {}
132
133impl codec::Decode for Roles {
134	fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
135		Self::from_bits(input.read_byte()?).ok_or_else(|| codec::Error::from("Invalid bytes"))
136	}
137}