referrerpolicy=no-referrer-when-downgrade

sc_network_gossip/
lib.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//! Polite gossiping.
20//!
21//! This crate provides gossiping capabilities on top of a network.
22//!
23//! Gossip messages are separated by two categories: "topics" and consensus engine ID.
24//! The consensus engine ID is sent over the wire with the message, while the topic is not,
25//! with the expectation that the topic can be derived implicitly from the content of the
26//! message, assuming it is valid.
27//!
28//! Topics are a single 32-byte tag associated with a message, used to group those messages
29//! in an opaque way. Consensus code can invoke [`ValidatorContext::broadcast_topic`] to
30//! attempt to send all messages under a single topic to all peers who don't have them yet, and
31//! [`ValidatorContext::send_topic`] to send all messages under a single topic to a specific peer.
32//!
33//! # Usage
34//!
35//! - Implement the [`Network`] trait, representing the low-level networking primitives. It is
36//!   already implemented on `sc_network::NetworkService`.
37//! - Implement the [`Validator`] trait. See the section below.
38//! - Decide on a protocol name. Each gossiping protocol should have a different one.
39//! - Build a [`GossipEngine`] using these three elements.
40//! - Use the methods of the [`GossipEngine`] in order to send out messages and receive incoming
41//!   messages.
42//!
43//! The [`GossipEngine`] will automatically use [`Network::add_set_reserved`] and
44//! [`NetworkPeers::remove_peers_from_reserved_set`] to maintain a set of peers equal to the set of
45//! peers the node is syncing from. See the documentation of `sc-network` for more explanations
46//! about the concepts of peer sets.
47//!
48//! # What is a validator?
49//!
50//! The primary role of a [`Validator`] is to process incoming messages from peers, and decide
51//! whether to discard them or process them. It also decides whether to re-broadcast the message.
52//!
53//! The secondary role of the [`Validator`] is to check if a message is allowed to be sent to a
54//! given peer. All messages, before being sent, will be checked against this filter.
55//! This enables the validator to use information it's aware of about connected peers to decide
56//! whether to send messages to them at any given moment in time - In particular, to wait until
57//! peers can accept and process the message before sending it.
58//!
59//! Lastly, the fact that gossip validators can decide not to rebroadcast messages
60//! opens the door for neighbor status packets to be baked into the gossip protocol.
61//! These status packets will typically contain light pieces of information
62//! used to inform peers of a current view of protocol state.
63
64pub use self::{
65	bridge::GossipEngine,
66	state_machine::TopicNotification,
67	validator::{DiscardAll, MessageIntent, ValidationResult, Validator, ValidatorContext},
68};
69
70use sc_network::{types::ProtocolName, NetworkBlock, NetworkEventStream, NetworkPeers};
71use sc_network_sync::SyncEventStream;
72use sc_network_types::{
73	multiaddr::{Multiaddr, Protocol},
74	PeerId,
75};
76use sp_runtime::traits::{Block as BlockT, NumberFor};
77use std::iter;
78
79mod bridge;
80mod state_machine;
81mod validator;
82
83/// Abstraction over a network.
84pub trait Network<B: BlockT>: NetworkPeers + NetworkEventStream {
85	fn add_set_reserved(&self, who: PeerId, protocol: ProtocolName) {
86		let addr = Multiaddr::empty().with(Protocol::P2p(*who.as_ref()));
87		let result = self.add_peers_to_reserved_set(protocol, iter::once(addr).collect());
88		if let Err(err) = result {
89			log::error!(target: "gossip", "add_set_reserved failed: {}", err);
90		}
91	}
92	fn remove_set_reserved(&self, who: PeerId, protocol: ProtocolName) {
93		let result = self.remove_peers_from_reserved_set(protocol, iter::once(who).collect());
94		if let Err(err) = result {
95			log::error!(target: "gossip", "remove_set_reserved failed: {}", err);
96		}
97	}
98}
99
100impl<T, B: BlockT> Network<B> for T where T: NetworkPeers + NetworkEventStream {}
101
102/// Abstraction over the syncing subsystem.
103pub trait Syncing<B: BlockT>: SyncEventStream + NetworkBlock<B::Hash, NumberFor<B>> {}
104
105impl<T, B: BlockT> Syncing<B> for T where T: SyncEventStream + NetworkBlock<B::Hash, NumberFor<B>> {}