referrerpolicy=no-referrer-when-downgrade

sc_mixnet/
protocol.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
19use super::config::Config;
20use mixnet::core::PACKET_SIZE;
21use sc_network::{
22	config::{NonReservedPeerMode, SetConfig},
23	peer_store::PeerStoreProvider,
24	service::NotificationMetrics,
25	NetworkBackend, NotificationService, ProtocolName,
26};
27use sp_runtime::traits::Block as BlockT;
28
29/// Returns the protocol name to use for the mixnet controlled by the given chain.
30pub fn protocol_name(genesis_hash: &[u8], fork_id: Option<&str>) -> ProtocolName {
31	let name = if let Some(fork_id) = fork_id {
32		format!("/{}/{}/mixnet/1", array_bytes::bytes2hex("", genesis_hash), fork_id)
33	} else {
34		format!("/{}/mixnet/1", array_bytes::bytes2hex("", genesis_hash))
35	};
36	name.into()
37}
38
39/// Returns the peers set configuration for the mixnet protocol.
40pub fn peers_set_config<Block: BlockT, Network: NetworkBackend<Block, <Block as BlockT>::Hash>>(
41	name: ProtocolName,
42	config: &Config,
43	metrics: NotificationMetrics,
44	peerstore_handle: std::sync::Arc<dyn PeerStoreProvider>,
45) -> (Network::NotificationProtocolConfig, Box<dyn NotificationService>) {
46	let set_config = if config.substrate.num_gateway_slots != 0 {
47		// out_peers is always 0; we are only interested in connecting to mixnodes, which we do by
48		// setting them as reserved nodes
49		SetConfig {
50			in_peers: config.substrate.num_gateway_slots,
51			out_peers: 0,
52			reserved_nodes: Vec::new(),
53			non_reserved_mode: NonReservedPeerMode::Accept,
54		}
55	} else {
56		SetConfig {
57			in_peers: 0,
58			out_peers: 0,
59			reserved_nodes: Vec::new(),
60			non_reserved_mode: NonReservedPeerMode::Deny,
61		}
62	};
63
64	Network::notification_config(
65		name,
66		Vec::new(),
67		PACKET_SIZE as u64,
68		None,
69		set_config,
70		metrics,
71		peerstore_handle,
72	)
73}